notepad++ 提供了丰富的自扩展功能,这里只介绍一下Python的plugin的扩展。
如何开发属于自己的扩展功能呢?请参考附件中的PythonScript.chm官方文档。
功能1:notepad++如何插入时间 地址:http://kanpiaoxue.iteye.com/admin/blogs/2340937
功能2:去掉右侧空格:
#rstrip the contents def testContents(contents, lineNumber, totalLines): newContents = contents.rstrip() editor.replaceLine(lineNumber, newContents) editor.forEachLine(testContents)
【 notepad++的Python自身对象和用法需要参考它自带的文档】
需要先安装插件 python script。
插件的官方说明如下:
================== 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
它文档的位置在: npp.7.2.1.bin\plugins\doc\PythonScript\PythonScript.chm
在这个文档里面可以查找到各种notepad++的自身对象和调用的方法。
自定义Python脚本的目录: npp.7.2.1.bin\plugins\Config\PythonScript\scripts
功能 |
快捷键 |
对于脚本 |
插入日期 |
Alt + Shift + d |
ActionInsertDate.py |
插入日期和时间 |
Alt + Shift + f |
ActionInsertDateTime.py |
插入时间戳 |
Alt + Shift + t |
ActionInsertTimestamp.py |
插入分割线 |
Alt + Shift + = |
ActionInsertSplitLine.py |
去掉右侧空格 |
Alt + Shift + r |
ActionRstripLine.py |
格式化Windows样式的路径为Unix样式 |
Alt + Shift + l |
ActionForReplaceLinuxPathSplit.py |
为列模式计算求和与平均值 |
无 |
ActionCalculateNumberSumForColumnModel.py |
脚本名称 |
脚本内容 |
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]=============================================== |