如何将“输入”添加到我的宏中,以替换 Notepad++ 中的文本?

Wol*_*'08 6 notepad++ python

我正在尝试使用与替换功能协同作用的“输入”弹出对话框制作宏。它应该是这样的:“粘贴文本-> 要求输入-> 用输入值替换粘贴文本中的变量”。有谁知道如何做到这一点?对找到变量/输入选项特别感兴趣。

当前代码:

import * from npp
my_prompt = Notepad()
initial_string="""

  THIS IS A HUGE
  STRING
  WITH COMMENTS
  AND FUNCTIONS
  AND
    NESTING
    MISSPELLEDD WORDS
    VARIABLES
  ALL KINDS OF CRAZY STUFF"""


print initial_string
user_replacement_string=Notepad.prompt(my_prompt) //RuntimeError: This class cannot be instantiated from Python
Editor.replace("varr", user_replacement_string) //untested
Run Code Online (Sandbox Code Playgroud)

重要编辑

另外,我想要一个生成这样的脚本的通用表单,类似于宏生成器生成宏的方式。看到答案。我取出了几乎所有的编码。您所要做的就是创建一个带有变量名称的模式字符串,您可以记住将其嵌入其中,并将生成的宏文件保存在正确的目录中。基本上,它允许用户宏粘贴一个值,然后轻松替换粘贴内容中的多个变量。

Wol*_*'08 6

第一的:

  1. 转到“插件”下拉菜单。
  2. 打开Plugins Manager.
  3. 下载PythonScript记事本++。
  4. 通过提示,重新启动 Notepad++。

下一个:

  1. 转到下拉菜单中的插件。
  2. 单击PythonScript -> new script
  3. 使用.py扩展名保存脚本。

复制并粘贴此代码,然后对其进行编辑:

#***   IMPORTS   ***
from Npp import *

#*** DEFINITIONS ***
#*** http://docs.python.org/release/1.4/tut/node70.html (.h triple-string variables)***
initial_string="""

  THIS IS A HUGE
  STRING
  WITH COMMENTS
  AND FUNCTIONS
  AND
    NESTING varr
    MISSPELLEDD WORDS
    VARIABLES Varr
  ALL KINDS OF CRAZY STUFF""" 

#***  I am using a case-sensitive replacement;     ***
#***  therefore, I use two replacement operations  ***

#***  First replacement, lowercase ***
print initial_string
user_replacement_string=notepad.prompt(notepad, 'Replace', "")

#*** Second replacement, uppercase ***
editor.replace("varr", user_replacement_string) 
user_replacement_string=notepad.prompt(notepad, 'Replace', "")
editor.replace("Varr", user_replacement_string)
Run Code Online (Sandbox Code Playgroud)

保存

现在,创建一个新文件ctrl-n并测试脚本。去Plugins->PythonScript->Run Last Script。如果它有效,您可以在您正在处理的文件中将其付诸实施。

为方便起见,我ctrl-shift-e为该Run Previous Script (#yourscriptname.py)命令制作了快捷键。在下拉菜单中,转到Settings ->Shortcut Mapper。单击Plugin commands选项卡。接近尾声,Run Previous Script。非常便利。

有趣的是,您可以使用此脚本为不同的模式复制自身。我真的很想知道如何做到这一点。请参阅我的问题的编辑。

重要编辑

下面的代码将使您的 Notepad++ 创建自己的脚本。运行此脚本,并将文件保存在您的c:/program files/Notepad++/plugins/config/PythonScript/scripts目录 { Windows default } 中:

#imports
from Npp import *
#variables
pattern_string=notepad.prompt(notepad, 'Pattern String')
number_of_replacements=int(notepad.prompt(notepad, 'Number of Replacements'))
variable_to_replace = []
replacement_title = []
the_header = """#imports
from Npp import *

#definitions
"""
a = "pattern_string = """ + '"' + '""' + pattern_string + '""' + '"'
b = """

print pattern_string
user_replacement_string"""
c = """=notepad.prompt(notepad, 'Replace', "Replaces: """
d = '"' + """)
editor.replace(""" + '"'
e = """", user_replacement_string"""
f = ")"
#function
for i in range (0, number_of_replacements):
    replacement_title.append(str("Replacement Item ") + str(i+1))
    variable_to_replace.append(notepad.prompt(notepad, replacement_title))

print the_header + a
print b + str(i) + c + variable_to_replace[i] + d + variable_to_replace[i] + e + str(i) + f
#thanks Wolfpack08.
Run Code Online (Sandbox Code Playgroud)

它将使您可以导入经常粘贴到文件中的字符串,以及经常需要替换的变量。用变量写你的输入模式(即,

This is my stupid input pattern foo, and I use the common variable bar.

运行脚本时,您在第一个弹出窗口中指定该输入模式。然后在输入模式中指定变量的数量:foo 和 bar(例如,“2”变量)。在出现的弹出输入框中,您可以按照要替换的顺序指定变量:(" ", " ")。

我没有放入“默认值”函数,因为我认为替换我只能想象在 lexis 中真正东西会很烦人。不过,我认为对于那些需要输入大量数据并需要这种宏的人来说,拥有默认值会非常有用。如果有人提出,我会添加它。