【问题标题】:notepad++ pythonscript mark wordsnotepad++ pythonscript 标记单词
【发布时间】:2016-02-09 11:21:46
【问题描述】:

我正在尝试为 notepad++ 构建一个 pythonscript 宏,它可以自动突出显示几个单词的搜索对话框。

很遗憾,我找不到“突出显示”的必要功能。脚本应该和我使用 Ctrl+F 对话框一样,选中“Wrap around”并按“Mark All”

这个类似,虽然它不使用 pythonscript: notepad++ mark style with macro

有了可用的文档,我可以做到以下几点:

# Function for marking a line if a certain pattern was found
def bookmarks(lineText, lineNumber, totalLines):    
    patterns = ['word1','word2'] 
    for p in patterns:
        if lineText.find(p) > -1:
            editor.markerAdd(lineNumber, 24) 
    return 1

# mark interesting lines:
editor.markerDeleteAll(24);
editor.forEachLine(bookmarks);

这将为当前文件中包含“word1”或“word2”的所有行创建书签。但是,我想像“标记所有”一样突出显示出现的情况,并且我想在所有打开的文件中这样做。

也许我缺少文档资源,或者是因为我不太了解 python。至少我找不到合适的功能来突出显示匹配项。我确实使用了谷歌,并查阅了以下文档页面。 此外,任何有关更有价值文档的提示都非常感谢!

http://npppythonscript.sourceforge.net/docs/latest/index.html http://sourceforge.net/p/npppythonscript/wiki/Home/

【问题讨论】:

    标签: python macros notepad++


    【解决方案1】:

    http://npppythonscript.sourceforge.net/docs/latest/scintilla.html?highlight=mark#Editor.markerEnableHighlight

    阅读文档,我认为这可以帮助您:

    Editor.markerEnableHighlight(enabled)
    

    或者这个:

    Editor.markerAdd(line, markerNumber)
    

    以后我会试试的

    【讨论】:

    • 感谢您的快速答复。 Marker.Add 我已经在我的示例中使用过,它会按预期创建书签,但不会标记单词(即突出显示单词)。我尝试添加 Editor.markerEnableHighlight(True)。但是没有效果。
    【解决方案2】:

    这是一个老话题了,但是对于还在 Notepad++ 中使用“pythonscript 宏”的人来说,我找到了一种方法来模仿搜索和替换 Mark-All 的行为。

    您需要使用闪烁指示器来突出显示感兴趣的单词。请参阅reference 了解闪烁指标。

    您首先需要选择一个指标编号(约 32 个可用)。请注意,指标在我不知道的定义范围内与 NPP 和 linter 共享,所以我发现自己使用 20th,因为它刚刚工作。避免 1-9 范围和 19 和 29。

    然后您可以使用文档来设置指标的样式。最后使用包装方法 editor.indicatorFillRange(start, lengthFill) 应用指标。

    这是一个使用 python re 模块突出显示正则表达式匹配的脚本。

    from Npp import editor, notepad
    import re
    
    patern = notepad.prompt('Please insert a valid python regex', 'regex highlighter', '')
    
    # Highlight matches
    for match in re.finditer(patern, editor.getText()):
        editor.indicSetStyle(20, 8)
        editor.indicSetFore(20, (192, 0, 0))
        editor.indicSetAlpha(20, 50)
    
        editor.setIndicatorCurrent(20)
        editor.indicatorFillRange(match.start(), match.end() - match.start())
    
    # clear the mess
    #    editor.setIndicatorCurrent(20)
    #    editor.indicatorClearRange(0, editor.getLength())
    

    附注:NPP Python 脚本的文档可在您的本地文件Notepad++/plugins/PythonScript/doc/scintilla.html 中找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多