Support for notepad++ plug-in development by line number Area text selection

Source: Internet
Author: User



recently found that notepad++ does not support the text copy by line number range, I want to develop a notepad++ plug-in, support to enter the starting line number and end line number, and then copy the text of the area to a new document or copy to the system Clipboard, convenient text operation. Effects such as:






This paper mainly introduces the basic process of notepad++ plug-in development and the related points of attention.





1, notepad++ Introduction





notepad++ is an open source free text editor developed using C + +, it is a multi-language version of the editor, contains simplified Chinese, using it can easily edit C, C + +, Java, C #, XML, HTML, PHP, CSS and other plain text files, support regular search, folder search , encoding conversion, file comparison, etc., can be comparable to ultraedit, the most important is to support their own expansion plug-in development.



The address of Notepad++ official website is: http://notepad-plus-plus.org/



SourceForge Address: Http://sourceforge.net/apps/mediawiki/notepad-plus



In addition, NPP was developed based on the open source control Scintilla. Scintilla website Address: http://www.scintilla.org/



Plug-in Center: http://docs.notepad-plus-plus.org/index.php/Plugin_Central


2. notepad++ Development process


It takes about six steps to develop a notepad++ plugin:



First, download the np++ plugin template project (http://notepad-plus.sourceforge.net/commun/pluginDemoTemplate/NppPluginTemplate.zip) and unzip it locally.



Second, you can rename the project by using VS to open the nppplugintemplate.vcproj project in the project .



Third, define the plug-in name in the PluginDefinition.h header file, i.e.



Const TCHAR npp_plugin_name[] = TEXT ("notepad++ PLUGIN template");



Finally, define the number of menus in the plugin const int nbfunc = 2;



Five, bind the menu name and callback function in PluginDefinition.cpp .



Six, implement the callback function of the related menu.


3. Main data structure interacting with notepad++ main interface


A. Interaction handles




struct NppData {
	HWND _nppHandle;
	HWND _scintillaMainHandle;
	HWND _scintillaSecondHandle;
};

The data structure contains a handle to the interaction with the np++, which contains the main window and the secondary window handle. This data member is passed in when the plug-in is loaded for message interaction between the plug-in and the np++ main interface.





Through the interface extern "C" __declspec (dllexport) void SetInfo (Nppdata notpadplusdata) is passed into the plug-in, when writing the plug-in, you need to save the passed notpadplusdata value.



B. Menu and callback function bindings




struct FuncItem {
	TCHAR _itemName[nbChar];
	PFUNCPLUGINCMD _pFunc;
	int _cmdID;
	bool _init2Check;
	ShortcutKey *_pShKey;
};
The structure defines the corresponding relationship between the plug-in menu name and the corresponding callback function. Such as


/ / Register the menu callback function
FuncItem CRunlogMenu::arrFuncItems[N_NBFUNCITEMS] = {
     { TEXT("Line Range Copy"), funcRangeCopy, 0, false, NULL },
     //{ TEXT("Statistics"), funcStatistics, 0, false, NULL },
     { TEXT(""), NULL, 0, false, NULL }, // separator
     { TEXT("About"), funcAbout, 0, false, NULL }
};



4. Message interaction with notepad++


Interaction with the main interface is done by sending a message that begins with the nppm_, such as getting the full path name of the current file:


TCHAR str[100];
int strLen = 100;
::SendMessage(m_nppData._nppHandle, NPPM_GETNPPDIRECTORY, (WPARAM) strLen, (LPARAM) str);
Other relevant NPPM_ messages are presented at: http://docs.notepad-plus-plus.org/index.php/Messages_And_Notifications




5. Get the current document text related properties


manipulating the current text is obtained through the SCI message, such as getting the selected text content



first get the current document handle:   


int currentView = 0;
    SendNppMsg( NPPM_GETCURRENTSCINTILLA, 0, (LPARAM) ¤tView );
    return ( (currentView == 0) ?
      m_nppData._scintillaMainHandle : m_nppData._scintillaSecondHandle );
The handle is then sent with the Sci_getseltext message





:: SendMessage (M_hsciwnd, umsg, WParam, LParam);



Here are a few of the more important SCI messages for reference in the process of developing a plugin:






Sci_gettextrange (<unused>, sci_textrange *tr)
This collects the text between the positions and andcpMincpMaxcopies it to (see inlpstrTextstructSci_TextRangeScintilla.h). IfcpMaxis-1, text isreturned to the end of the document. The text is 0 terminated, so you mustsupply a buffer that's at least 1 character longer than the number ofcharacters you wish to read. The return value is the length of the Returnedtext not including the terminating 0.





Get headquarters



Sci_getlinecount
This returns the number of lines in thedocument. An empty document contains 1 line. A document Holding only an end ofline sequence have 2 lines.






Gets the total number of documents bytes Sci_gettextlength
Sci_getlength

Both These messages return the length of thedocument in byte






Gets whether the document is modified



Sci_getmodify
This returns Non-zero if the document ismodified and 0 if it is unmodified. The modified status of a document is Determinedby the "undo position relative to the" save point. The save pointSCI_SETSAVEPOINTwas set by and usually when you had saved data to a file.






Select text



Sci_setsel (int anchorpos, intcurrentpos)
This message sets both the anchor and the currentposition. IfcurrentPosis negative, itmeans the end of the document. IfanchorPosis negative, Itmeans remove any selection (i.e. set the anchor to the same position ascurrentPos). The caret is scrolled to view after this operation.






Jump to a line



Sci_gotoline (int line)
This removes any selection and sets the caret atthe start of line number andlinescrolls the view (if needed) to make it Visible. The anchor position is set the same as thecurrent position. Iflineis outside the Linesin the document (first line was 0), the line set is the first or last.






Get the current location



Sci_getcurrentpos
This is returns the current position.






Get mouse position Sci_getanchor
This returns the current anchor position.






Select All text



Sci_selectall
This selects all the text in the document. Thecurrent position is isn't scrolled into view.






Gets the first position of a row



Sci_positionfromline (int line)
This returns the document position Thatcorresponds and the start of the line. Iflineis negative, the positionof the line holding the start of the the selection is returned. Iflineis greater than the lines in the document, the return valueis-1. Iflineis equal to thenumber of lines in the document (i.e. 1 line past the last line), the returnvalue is the end of th E document.






Get the length of a row



Sci_linelength (int line)
This returns is the length of the line and Includingany line end characters. Iflineis negative orbeyond the last line of the document, the result is 0. If you want the Lengthof the line isn't including any end of line characters, useSCI_GETLINEENDPOSITION(line)-SCI_POSITIONFROMLINE(line).






Copy the currently selected text



Sci_getseltext (<unused>,char *text nul-terminated)
This copies the currently selected text and aterminating 0 bytes to thetextbuffer. The buffersize should is determined by calling and a NULL pointer for thetextargumentSCI_GETSELTEXT(0,0). This allowsfor rectangular and discontiguous selections as well as simple selections. See multipleselection for information on how Multipleand rectangular selections and virtual space is copied.



6. Precautions





The operation of the text in Unicode mode requires conversion, otherwise garbled characters occur:



#ifdef UNICODE
   WCHAR	wText[65];
   ListView_GetItemText(_hListCtrl, _pCurProp->cursorItem, _pCurProp->cursorSubItem, wText, SUBITEM_LENGTH);
   wText[_pCurProp->cursorPos] = (TCHAR)wParam;
   ::WideCharToMultiByte(CP_ACP, 0, wText, -1, text, SUBITEM_LENGTH, NULL, NULL);
#else
   ListView_GetItemText(_hListCtrl, _pCurProp->cursorItem, _pCurProp->cursorSubItem, text, SUBITEM_LENGTH);
#endif





PS: attached to the development of the line-based text region copy of the plug-in, extracted after the NppPluginTextSelect.dll placed in the notepad++ installation directory Notepad++\plugins can, and then restart notepad++ software, in the "plug-in" Find the "Text Selection Helper" menu item in the menu.



http://download.csdn.net/detail/xiaoding133/8912559








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Support for notepad++ plug-in development by line number Area text selection


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.