Scite Lua Prompt

lua-users home
wiki

Interactive SciTE Lua

This is a little script which uses the SciteExtMan script manager and is one of the examples in the download.

It uses the extman scite_OnOutputLine event to grab lines entered in SciTE's output pane, and compile them using loadstring. It gives you a very useful way to interactively evaluate Lua expressions. One-line functions can be defined.

Scite/Lua
> = 10 + 20
30
> print 'hello dolly'
hello dolly
> function showkeys(t) for i in t do print(i) end end
> showkeys(table)
setn
insert
getn
foreachi
foreach
sort
remove
concat
> 

Event if you don't see yourself as doing Lua programming, this Lua prompt is useful as an intelligent calculator.

Any global function is available. A function load is supplied, which will load Lua code from a file. If called with no argument, it will use the current buffer. The function edit will open the specified file in a new buffer. There is pop-down history list available using Tools|Last Command, which is Ctrl-Alt-P.

It is particularly useful for learning the SciTE and Scintilla API, since both the editor and output pane objects are directly accessible. You can test a particular operation before writing a full-blown script.

> = output.Length
405
> output:AppendText('hello, world!\n')
hello, world!
> 

Issues

The SciTE Lua environment has a nasty suprise for people expecting global objects to survive for the whole session. If you switch buffers in SciTE, the global environment is cleared out. Only global objects created initially in files loaded by your Lua startup script will be preserved.

Source

The source is found in Files:wiki_insecure/editors/SciTE/extman.zip.

 -- prompt.lua
 scite_Command('Last Command|do_command_list|Ctrl+Alt+P')

 local prompt = '> '
 local history_len = 4
 local prompt_len = string.len(prompt)
 print 'Scite/Lua'
 trace(prompt)

 function load(file)
	if not file then file = props['FilePath'] end
	dofile(file)
 end

 function edit(file)
	scite.Open(file)
 end

 local sub = string.sub
 local commands = {}

 local function strip_prompt(line)
   if sub(line,1,prompt_len) == prompt then
        line = sub(line,prompt_len+1)
    end	
	return line
 end
  
 scite_OnOutputLine (function (line)
	line = strip_prompt(line)
   table.insert(commands,1,line)
    if table.getn(commands) > history_len then
        table.remove(commands,history_len+1)
    end
    if sub(line,1,1) == '=' then
        line = 'print('..sub(line,2)..')'
    end    
    local f,err = loadstring(line,'local')
    if not f then 
      print(err)
    else
      local ok,res = pcall(f)
      if ok then
         if res then print('result= '..res) end
      else
         print(res)
      end      
    end
    trace(prompt)
    return true
end)

function insert_command(cmd)
	output:AppendText(cmd)
	output:GotoPos(output.Length)
end

function do_command_list()
     if table.getn(commands) > 1 then 
		scite_UserListShow(commands,1,insert_command)
     end
end

RecentChanges · preferences
edit · history
Last edited November 15, 2006 10:34 am GMT (diff)