Inserting New Lines

Post Reply
Jor'Mox
Posts: 1154
Joined: Wed Apr 03, 2013 2:19 am

Inserting New Lines

Post by Jor'Mox »

I have been trying to write text above the current working line via a trigger, and it needs to play nicely with other possibly present text (obviously). The problem is that when I use insertText("\n") a duplicate of the line I just wrote. I can "fix" it, but it is a bit cumbersome. Is there something I'm missing here, or is it a bug that is either already known and being addressed, or unknown?

Example code:
Code: [show] | [select all] lua
moveCursor(0,getLineNumber()-1)
insertText("\nblah") -- shows "blah" on the line, as expected
insertText("\nmore blah") -- now shows three lines, instead of two as expected, blah, blah, and more blah
My current "fix":
Code: [show] | [select all] lua
function insertNewLine()
   insertText("\n")
   selectString(getCurrentLine(),1)
   replace("")
end
moveCursor(0,getLineNumber()-1)
insertNewLine()
insertText("blah")
insertNewLine()
insertText("more blah")
Edit:
The above code worked fine when testing with an alias, but didn't work at all in a trigger (which I found unusual). As a result, I adjusted the code until I achieved the desired result, below:
Code: [show] | [select all] lua
local function insertNewLine()
	local curNum = getLineNumber()
	insertText("\n")
	wrapLine(curNum)
	moveCursor(0,curNum+1)
	selectCurrentLine()
	replace("")
end

User avatar
Vadi
Posts: 5050
Joined: Sat Mar 14, 2009 3:13 pm

Re: Inserting New Lines

Post by Vadi »

I don't get duplication, but the newlines do not get interpreted - so you need to use wrapLine:
Code: [show] | [select all] lua
moveCursor(0,getLineNumber()-1)
insertText("\nblah") -- shows "blah" on the line, as expected
insertText("\nmore blah") -- now shows three lines, instead of two as expected, blah, blah, and more blah
wrapLine("main", getLineNumber())

Jor'Mox
Posts: 1154
Joined: Wed Apr 03, 2013 2:19 am

Re: Inserting New Lines

Post by Jor'Mox »

If I use this function instead of the one I posted, I get the lines written in order, and then in reverse order, six lines instead of three.
Code: [show] | [select all] lua
local function insertNewLine()
        local curNum = getLineNumber()
        insertText("\n")
        wrapLine(curNum)
        moveCursor(0,curNum+1)
end
So, I'm going to assume that the duplication issue has been fixed in the next release?

User avatar
Vadi
Posts: 5050
Joined: Sat Mar 14, 2009 3:13 pm

Re: Inserting New Lines

Post by Vadi »

I think it has been, there was some fix about that.

Post Reply