[Closed] setting the cursor in a script editor window
I’m working on a script that uses data from a text file.
In case of an incorrect value, I’d like the file to be opened in an editor window and the cursor to be located where the error occured.
It’s the last part I can’t figure. I don’t know if there is a way to access the CTRL-J feature of the editor through maxscript. Or maybe a workaround.
Can anyone enlighten me?
No luck with setting the cursor position in an editor window…
You can use the MaxScript Listener, but you won’t get any syntax coloring, and I still hit snags anyway…
tempFile = createFile "$temp\ est.txt"
format "hello
world
foo
baz
" to:tempFile
for i = 1 to 200 do ( format "%
" i to:tempFile )
close tempFile
-- f = file being parsed
-- l = line number the error occurred in
fn showError f l = (
clearListener()
f_handle = openFile f mode:"r"
l_cur = 1
while (not (eof f_handle)) do (
format "%
" (readline f_handle)
if (l_cur == l) then (
p = filePos f_handle
)
l_cur += 1
)
close f_handle
setListenerSel #(p,p)
format "^^^^^ ERROR OCCURRED HERE ^^^^^
"
setListenerSel #(p,p)
)
showError "$temp\ est.txt" 2
The snag I hit is that at the end of the function, the value OK is returned. I need to return a completely empty value so that the value does not get printed to the Listener, muddying up what is displayed.
Initially I had it selecting the line (instead of positioning the cursor)… but the aforementioned problem means that immediately after the line is selected, the return value ‘OK’ replaces the selection. Whoops.
Are you looking to open the file in an external editor? I tend to use Textpad for a lot of things here, and its possible to pass in command line arguments…
from the help on command line…
If the filename to be edited (not printed) is followed by “(<line>[,<col>])”, with no intervening spaces, the file will be opened with the cursor at that position. If <line> is a hex number (eg. 0x1a22), a hex view of the file will be created, with the cursor at that address.
eg. TEXTPAD.EXE -ac “Read me.txt”(51,20)
Command Line ParametersIn this example TextPad will start up and open “Read me.txt” at line 51, column 20 and display it in a cascaded window.
I’m sure you could do it with other editors also… thats just the one i had open for easy f1’ing
The script uses XYZ coordinates stored in a text file.
The idea is simply to make it easier for the user to correct an eventual error in the file by opening it in an editor with the cursor already set where the error occured (finding line 153 by hand can be fun, though).
The listener window doesn’t seem appropriate for the job. I’d like the user to be able to simply correct the error and save.
Shell launching notepad was my first idea as it is (or shoulb be) present on all windows platforms (don’t quote me on that).
Unfortunately I couldn’t find any agument to position the cursor once the file is open. Appending the name with line and col numbers – as in the above example with textpad – doen’t work, unless I’m getting the syntax wrong.
Thanks both of you for your time anyway.
The script editor windows seemed the ideal choice, being in max, and looked promising with its CTRL-J option. But I haven’t find anything in the maxscript reference to control the cursor positionning through a script.
If someone knows for sure that it cannot be done, please let me know and I’ll stop searching.
Thanks again, guys.
Too late
Notepad is present on pretty much any windows version, but it doesn’t have any arguments for setting the caret (cursor). Textpad is a third party editor, but not everybody will have that installed.
If the script is just for your use, then you can always install Textpad, of course.
Not 100% sure, but 99% anyway – sorry.
It might be possible that there’s windows messaging access to the script windows to set the caret position, but I wouldn’t bet on it… and the last time several people (myself included) looked into iwndows messaging and the new editor, we concluded it was too much work with too little payoff.
Perhaps something for the next max version to consider… more access to the new editor.
I wouldn’t bet 10p I found down the back of the couch on that… it took us 10 years to get multiple undo’s! :banghead:
Well, that’s settled then. Thank you for your help. I’ll probably end up with a simple messagebox…
Or you could try creating a simple dialog with just a dotnet (rich) textfield docked in it. That should give you full control over the text (and even highlighting colors if you wanted to) and cursor position/text selections.
Martijn
Like this?
rollout test "test" width:620 height:220 (
dotNetControl dnc_richedit "System.Windows.Forms.RichTextBox" width:600 height:200
)
createDialog test
re = test.dnc_richedit
re.text = "Hello - This is a rich edit control"
re.Select 10 10
re.SelectionBackColor = (dotNetClass "System.Drawing.Color").Red
re.Select 30 10
re.SelectionBackColor = (dotNetClass "System.Drawing.Color").Green
Edit: More useful example.. parses a file and if any line does not evaluate to an integer (note that there's some alphanumerics that evaluate to an integer.. this is just a simple example), pops up the error dialog with the offending line highlighted in red.
rollout test "test" width:620 height:220 (
dotNetControl dnc_richedit "System.Windows.Forms.RichTextBox" width:600 height:200
on dnc_richedit keyUp args do (
if (args.KeyCode == (dotNetClass "Keys").Enter) do (
dnc_richedit.SelectedText = "
"
)
)
on dnc_richedit keyDown do (
if (dnc_richedit.SelectionBackColor != dnc_richedit.BackColor) then (
dnc_richedit.SelectionBackColor = dnc_richedit.BackColor
)
)
on test open do (
dnc_richedit.HideSelection = false
dnc_richedit.WordWrap = false
)
)
fn showError f l = (
createDialog test
re = test.dnc_richedit
re.loadFile f (dotNetClass "RichTextBoxStreamType").PlainText
start = re.GetFirstCharIndexFromLine (l - 1)
re.select start re.Lines[l].count
re.SelectionBackColor = (dotNetClass "System.Drawing.Color").Red
re.select start 0
re.scrollToCaret()
)
fname = "$temp\ emp.txt"
/*
tempFile = createFile fname
format "5
" to:tempFile
format "10
" to:tempFile
format "a
" to:tempFile
format "77
" to:tempFile
close tempFile
*/
f = openFile fname mode:"r"
ln = 0
errLine = undefined
while (not (eof f)) do (
ln += 1
l = readline f
if ((errLine == undefined) AND ((l as integer) == undefined)) do (
errLine = ln
)
)
close f
if (errLine != undefined) do (
showError (getFiles fname)[1] errLine
)
But then what incentive would there be to add the functionality to the max editor... "just use .NET, dude!" ;)
Edit 1: More useful example
Edit 2: Silly .LoadFile .. Was using readline->stringstream->re.text as .LoadFile didn't autorecognize the file as ASCII.. have to specify it after all with the RichTextBoxStreamType class PlainText.
Edit 3: Scrolling to the error position now.
Edit 4: better disable word wrap.
Edit 5: Psh… lame. Enter key support has to be hacked in? keyUp (no trigger on keyDown/previewKeyDown/etc.), compare to Enter key, insert crlf. Yeesh. Done.
Edit 6: And fixed the error line’s colors bleeding into newly entered text.
I’ve never used dotnet before. I’ll give it a try.
Thank you for the idea (and the code!)