📜 ⬆️ ⬇️

Javascript for Notepad ++

Hello Bravelovelok,

Today I decided to share with you a plugin for Notepad ++, which will allow you using JavaScript to automate certain actions on the text and not only.

The birth of an idea


A lot of time has passed since I discovered Notepad ++. I use it both at home and at work. Sometimes you have to handle files in a routine manner and in most cases regular expressions are sufficient. But there are cases when you want a little more logic to make a replacement in one go or something to count. For complete happiness, VBA is not enough :-).
Looking around for a while, I found a plugin called “Simple script”. He allows using a certain set of functions consistently process the text. In general, it turned out to be a too SIMPLE script to satisfy my small needs in principle at that time. Friendship with “Simple script”
never worked out.
')
On duty, I became acquainted with Microsoft Windows Script, with which I then automated the generation and processing of XML files. Once I read the documentation for it, I discovered that it can be screwed to my application. Having looked on the Internet for further information and experience of implementation in my applications, I found a complete example on RSDN.ru.

It was clear to me that I want high-grade JavaScript in Notepad ++ and that this is possible.

Implementation


Numerous attempts to start a business failed. The lack of ATL on a home computer, as well as the lack of desire and time to deal with this library, created a relatively large threshold to begin with. But once preparing for the next raid, I came across two key articles: COM without ATL and Using Scripting . With the help of these two articles, the example with RSDN.ru without using ATL was reworked. There was no limit to my delight when the first prototype was made.

Pretty quickly, I implemented the original idea - the execution of selected or copied to the clipboard JavaScript code. Then I wanted to hang this code on the menu elements. Having conjured a little and changing the function of the window, I managed to break into the main menu of Npp. The ability to work with the menu has expanded the scope of the new plug-in (see below). With the advent of the menu, I wanted to make it context sensitive. For example, when switching from file to file [de], activate certain menu items. So the opportunity to hang event handlers.

Preparing examples of working with Plugin, I laid the foundations for reading and saving settings.

Using


Oh, and I probably tired of you with my stories! We proceed to practice.

The Editor is available in the global scope from JavaScript. Access to its properties and methods is also possible without specifying its name.
/* Properties of Editor */ /* read only */ firstView; // a View object currentView;// a View object secondView;// a View object clipBoard; langs; // Array of language names pluginConfigDir; nppDir; /* read write, bool */ tabBarHidden; toolBarHidden; menuHidden; statusBarHidden; /* Methods */ alert(/**String*/value); saveAll(); open(/**String*/value); addMenu(/**String*/ text); // returns a Menu object decodeFrom(/** Number*/ codepage, /**String*/ value); encodeTo(/** Number*/ codepage, /**String*/ value); runMenuCmd(/** Number*/ cmd); // Runs a standart Menu command (see MenuCmds.js) setListener(/** Object */ cfg); // see GlobalListener in start.js addSystemHotKey(/** Object */ cfg); // adds system hotkey (see example in run.js) 

To work with text editors, the Editor provides the properties [firs | current | second] View.
 /* Properties of View */ /* read write*/ text; file; // number of current file selection; // currently selected text codepage; // Number of current codepage lang; // Number of current language (see Editor.langs) line; // Number of current line of text pos; // Cursor position column; // Cursor position from line begin anchor; // Helps to set a selection. Selection is text in interval [anchor, pos] /* read only */ files; // Array of strings 

To add to the main menu, you need to call Editor.addMenu ("Your Sub menu"). Direct addition to the main menu of items is not
provided for.
 /* Properties of Menu */ /* read write */ text; checked; disabled; /* Methods */ addMenu(/*String*/ text); // adds and returns next submenu addMenuItem(/*Object*/cfg); // adds and returns menu item /* Properties of MenuItem */ /* read write */ text; checked; disabled; /* Methods */ remove(); 

For the seed, I decided to give only a small example (just 749 bytes):
 var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); if (xmlHttp){ //   var habrMenu = Editor.addMenu(""); xmlHttp.open('GET', 'http://habrahabr.ru/rss/1c9d5ed4f0953ded81510256ed34c9be/', true); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.responseXML) { var titles = xmlHttp.responseXML.selectNodes("/rss/channel/item/title"); for(var i=0, c=titles.length; i<c; i++){ //      RSS    habrMenu.addItem({ text: titles[i].text, //   link: titles[i].selectSingleNode("../link").text, //      cmd:function(){ //        var shell = new ActiveXObject("WScript.Shell"); if (shell && this.link){ shell.run(this.link); //     } }}); } } }; xmlHttp.send(null); } 

The example shows the capabilities of Plugin, as well as working with XML and Shell using ActiveX.
www.softwarecanoe.de/pic/nppscripting.habr.png

For further features, see the included scripts from the includes folder.

A couple of links:
Project page on Google Code ,
Microsoft Windows Scripting

Happy Scripting!

UPD : Thanks to the efforts of chikuyonok , an error was detected, and Zen Coding was adapted for Notepad ++ !!!
The error has already been fixed.

UPD 2 : Thanks to the efforts of t0H , another error was discovered. No new submenus were added to the hidden main menu.
The error has already been fixed.

UPD 3 : New version and API for it. I had to rename the plug-in because it already existed with the same name for programming on the LUA. So do not forget to delete the previous version of the plug-in'a (NppScripting.dll and NppScripting folder).

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


All Articles