2011-06-22 28 views
2

我目前在PythonScript中爲文本文件中的基本ASCII複選框支持寫了一個非常簡單的腳本。Notepad ++腳本:如何刪除Newline?

這個計劃是,當我按Alt + F2時,如果行以複選框開始,或者在當前位置插入[],編輯器會將[]切換爲[x]和[x]至[]。

我寫了一個腳本工作......幾乎

from Npp import * 
import string 

# If the line starts with [ ] or [x] the script toggles the value between the two possibilites 
# if the line doesn't contains [ ] the script adds the empty box at the current position 
curLine = editor.getCurLine() 
curPos = editor.getCurrentPos() 
curLineNr = editor.lineFromPosition(curPos) 
strippedLine = curLine.lstrip() 

if (strippedLine.startswith('[ ]')): 
    curLine = curLine.replace('[ ]', '[x]', 1).rstrip('\n') 
    editor.replaceWholeLine(curLineNr, curLine) 
    editor.gotoPos(curPos) 
elif (strippedLine.startswith('[x]')): 
    curLine = curLine.replace('[x]', '[ ]', 1).rstrip('\n') 
    editor.replaceWholeLine(curLineNr, curLine) 
    editor.gotoPos(curPos) 
else: 
    editor.addText('[ ] ') 

但這腳本將被替換的編輯器行後換行。一個非常愚蠢的工作將是刪除新插入的行,但我不想將其插入到第一個位置。

編輯:/ 瞭解它的工作。只需使用editor.replaceWholeLine方法,它就像一個魅力。

+1

如果你解決了它,你爲什麼不發表一個答案給自己的問題?那麼我們可以考慮解決這個問題。 – rafalotufo

回答

1

使用editor.replaceWholeLine方法和editor.replaceLine方法。

上面的腳本現在工作