开发者

Notepad++ Scripting: How to remove Newline?

I'm currently writing a very simple script in PythonScript for a basic ASCII checkbox support in a text file.

The plan is that when I hit Alt+F2 the editor toggles [ ] to [x] and [x] to [ ] if the line starts with the checkboxes or otherwise to just insert a [ ] at the current position.

I've written a script which works ... almost

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('[ ] ')

But this script adds a newline after the replaced editor line. A very stupid work arround would be to delete the newly inserted line but I d开发者_StackOverflow社区on't want to insert it in the first place.

Edit:/ Got it working. Just use the editor.replaceWholeLine method and it works like a charm.


Use the editor.replaceWholeLine method with the editor.replaceLine method.

The script above works now

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜