📜 ⬆️ ⬇️

Script for Notepad ++ in Python

Introduction


I think many people know Notepad ++ , a handy free utility that acts as an “advanced” replacement for the standard Windows Notepad. As with any text editor, in Notepad ++ from time to time it becomes necessary to automate any repetitive actions that, because of the complexity of the logic, cannot be recorded as a macro. Fortunately, to solve this problem, there is no need to switch from Notepad ++ to, for example, Word, in order to use the VB built into it.

Among plug-ins for Notepad ++, there are extensions that implement the possibility of writing scripts for Notepad ++ in different languages, such as JavaScript, Lua, PHP or Python. It was at the last that I decided to stop to solve my problem.

Formulation of the problem


Suppose we have the next task (taken from life).

1. In the selected text fragment (if nothing is selected, then in the whole document):
2. When starting the script, a request should be issued - from which number to start the numbering of the characters '@'. By default (by pressing "Enter"), the numbering should begin with 1. If you do not enter a numeric value, the query window should reappear until a number is entered.
')
3. If in the selected fragment of the text (or in the whole document - in case there is no selection) the '@' symbol is missing, the corresponding error message should be displayed.

4. Must be able to run the script:

Decision


First we need to install a plugin for Notepad ++ called Python Script . With it, you can perform any operations with editable text, open / close files, switch tabs, execute menu commands Notepad ++ , etc. - in a word, practically everything that can be done in Notepad ++ .

Next, selecting Notepad ++ Plugins-> Python Script-> New Script from the menu, create a script:

# -*- coding: utf-8 -*- #  Notepad++,       "@",     #    text = editor.getSelText() isSelection = True #    ,      if not text: isSelection = False text = editor.getText() #    "@",     import re occurrencesCount = len(re.findall('^@', text, flags=re.MULTILINE)) #        "@"   ,     if occurrencesCount == 0: notepad.messageBox('      1  "@"   ', '   ', MESSAGEBOXFLAGS.ICONEXCLAMATION) #   "@" ,  else: countStartFrom = '' #     ,      while not countStartFrom.isdigit(): countStartFrom = notepad.prompt(' ,          "@":', '   "@"', '1') if countStartFrom == None: break if countStartFrom != None: #    text = re.sub('\r\n\\s*\r\n', '\r\n', text) #         / text = re.sub('\r\n\s*$|^\s*\r\n', '', text, flags=re.MULTILINE) #  countStartFrom      countStartFrom = int(countStartFrom) # ,   "@"        def addNumber(matchobj): global countStartFrom countStartFrom += 1 return '@'+str(countStartFrom-1) #     "@",     text = re.sub('^@', addNumber, text, flags=re.MULTILINE) #       if isSelection: editor.replaceSel(text) else: editor.setText(text) 

If we called the script “Empty Lines And Count”, then it can be launched from the menu Plugins-> Python Script-> Scripts-> Empty Lines And Count . To add its button to the toolbar and make it possible to launch using the keyboard shortcut , in the plugin settings ( Plugins-> Python Script-> Configuration ), select the script we created and add it to the menu and to the toolbar. Now after restarting Notepad ++, the corresponding button will appear on the toolbar.

You can assign a shortcut to a script in the Settings-> Shortcut mapper menu in the Plugin commands section.

To add a script to the context menu of Notepad ++ , add the following lines to the xml-file of settings ( Settings-> Edit Popup ContextMenu ) in the place you need (for example, before the first element):

 <Item PluginEntryName="Python Script" PluginCommandItemName="Empty Lines And Count" ItemNameAs="      '@'"/> <Item id="0"/> 



That's all. Now even routine actions with complex logic can be performed with one click.

Useful links:

Source: https://habr.com/ru/post/135822/


All Articles