notepad++ custom function

notepad++ provides a wealth of self-expansion functions. Here we only introduce the extension of Python's plugin.

How to develop your own extension function? Please refer to the official documentation of PythonScript.chm in the attachment.

Function 1: How to insert time address in notepad++: http://kanpiaoxue.iteye.com/admin/blogs/2340937

Function 2: Remove the space on the right:

#rstrip the contents
def testContents(contents, lineNumber, totalLines):                
	newContents = contents.rstrip()
	editor.replaceLine(lineNumber, newContents)

editor.forEachLine(testContents)

 

[Notepad++'s own Python objects and usage need to refer to its own documentation]

The plugin python script needs to be installed first.

The official description of the plugin is as follows:

================== start

Scripting plugin for Notepad++, documentation included

As easy as:

 notepad.open('filename.txt')

 editor.appendText("Hello World")

Full programmatic access to Notepad++ features and menus

Full programmatic access to all of Scintilla features

Call other plugin menu items

Assign menu items, shortcuts and toolbar icons to scripts

Process Notepad++ and Scintilla events, direct from a Python script

Python console built-in

Full regular expression support for search and replace - script Python regular expression replaces

Start external programs and pipe the output direct to a Notepad++ document, or filter it, or simply to the console window

Full documentation for all the objects and methods

Author: Dave Brotherstone + Jocelyn Legault

Source: http://github.com/davegb3/PythonScript

Homepage: http://npppythonscript.sourceforge.net

Latest update: Important bug fix for editor.pymlreplace()

Added editor.getCharacterPointer() that returns a string

Added notepad.getPluginVersion() to get version of Python Script

================== end

The location of its documentation is: npp.7.2.1.bin\plugins\doc\PythonScript\PythonScript.chm

In this document, you can find various notepad++'s own objects and methods to call.

Directory for custom Python scripts: npp.7.2.1.bin\plugins\Config\PythonScript\scripts

 

Function

hot key

for the script

insert date

Alt + Shift + d

ActionInsertDate.py

Insert date and time

Alt + Shift + f

ActionInsertDateTime.py

insert timestamp

Alt + Shift + t

ActionInsertTimestamp.py

Insert dividing line

Alt + Shift + =

ActionInsertSplitLine.py

remove spaces on the right

Alt + Shift + r

ActionRstripLine.py

Format Windows-style paths to Unix-style

Alt + Shift + l

ActionForReplaceLinuxPathSplit.py

Calculate sum and average for column mode

without

ActionCalculateNumberSumForColumnModel.py

 

 

Screenplay name

script content

ActionInsertDate.py

import datetime

now = datetime.datetime.now()

editor.addText(now.strftime('%Y/%m/%d'))


 

ActionInsertDateTime.py

import datetime

now = datetime.datetime.now()

editor.addText(now.strftime('%Y/%m/%d %H:%M'))

 

ActionInsertTimestamp.py

import datetime

now = datetime.datetime.now()

editor.addText(now.strftime('%Y%m%d%H%M%S'))

ActionInsertSplitLine.py

import datetime

now = datetime.datetime.now()

nowString = now.strftime('%Y%m%d%H%M%S')

formatString = '[%s %s]'.center(100,'=')

result_start = formatString % ('start', nowString)

result_end = formatString % ('  end', nowString)

editor.addText(result_start)

editor.addText('\n')

 

editor.addText(result_end)

ActionRstripLine.py

#rstrip the contents

def testContents(contents, lineNumber, totalLines):               

newContents = contents.rstrip()

editor.replaceLine(lineNumber, newContents)

 

editor.forEachLine(testContents)

 

ActionForReplaceLinuxPathSplit.py

import re

 

def replaceAction(strObject):

return strObject.replace('\\','/')

 

def replaceFunc(m):

if m :

if m.group(1):

return '/' + m.group(1) + replaceAction(m.group(2))

else:

return replaceAction(m.group(0))

return None

 

def doAction():

#wPath = r'd:\baidu\workspaces\workspace_java\dmap-console'

wPath = editor.getSelText()

if '' != wPath.strip():

distPatten = r'^(?:(\w):)?(.*)$'

rs = re.sub(distPatten,replaceFunc, wPath)

if rs:

console.write(rs + '\n')

else:

console.write('can not find right path of windows style\n')

else:

console.write('select any nothing!\n')

 

 

#==============================================[start 2017/01/05 21:11:09]===============================================

doAction()

console.show()

editor.setFocus(True) 

#==============================================[ end  2017/01/05 21:11:09]===============================================

 

ActionCalculateNumberSumForColumnModel.py

def calculateNumberSumForColumnModel():

wPath = editor.getSelText()

if '' != wPath.strip():

lst = wPath.split('\n')

 

numSum = 0

numCount = 0

for numStr in lst:

if '' == numStr.strip():

continue

else:

numSum = numSum + float(numStr)

numCount = numCount + 1

averageNumString = 'denominator is 0'

if 0 != numCount:

averageNumString = str(numSum / numCount)

result = 'sum:%s, average:%s\n' % (str(numSum), averageNumString)

console.write(result)

else:

console.write('select any nothing!\n')

 

#==============================================[start 2017/01/11 14:36:12]===============================================

console.show()

calculateNumberSumForColumnModel()

editor.setFocus(True)

 

#==============================================[ end  2017/01/11 14:36:12]===============================================

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326858867&siteId=291194637