[New Plugin] LuaScript



  • This is a first test release of LuaScript. Yes, another scripting language extension plugin…

    LuaScript provides control over all of Scintilla’s features and options using the light-weight, fully-functional programming language, Lua. The plugin is a single DLL, requires no special permissions, and uses <50Kb RAM. It has an interactive console and also a startup script to run to configure Notepad++ every time it is started.

    Screen Shot

    You can do things like tweak Scintilla since Notepad++ does not expose all the options, like using prettier bookmark icons:

    -- Notepad++ uses 24 internally
    editor:MarkerDefine(24, SC_MARK_BOOKMARK)
    editor.MarkerFore[24] = 0xFF0000
    editor.MarkerBack[24] = 0xFF0000
    

    Or allow Notepad++ to scroll one page past the last line of text:

    editor.EndAtLastLine = false
    

    You can even use it to do find/replace:

    for m in editor:match("(image)(\\d+)(\\.(jpg|png))", SCFIND_REGEXP) do
        i = tonumber(editor.Tag[2])
        m:replace(editor.Tag[1] .. i + 1 .. editor.Tag[3])
    end
    

    Plus it is really convenient for just seeing how Scintilla works.

    There are probably some issues but it is still a work in progress.

    Download here - Just download the zip and extract the DLL to your plugin directory.
    Check out the ReadMe
    Source is hosted on Github

    Suggestions/comments/questions welcome.



  • This is great work. Thanks!



  • Hello dail,

    Thank you for “LuaScript”. I appreciate your work.

    I’ve just created a simple short script

    editor:CopyAllowLine()
    

    and saved the file “Copy Current Line.lua” in the config folder (startup.lua was located there).

    On restarting NPP, the new script was not available in the menu.
    Am I missing anything?

    And, with your permission, a suggestion relating to Copy Current Line:
    It would be nice to have a function coping the current line regardless of any selection (like editor.lineCopy() in PythonScript).

    Best regards.



  • @Yaron

    Thanks for trying this out.

    On restarting NPP, the new script was not available in the menu.

    Currently that is not possible. In the future I hope to have some way of running scripts or specific functions by using shortcut keys, menu, etc.

    It would be nice to have a function coping the current line regardless of any selection (like editor.lineCopy() in PythonScript).

    I agree having more small helper functions like this would be nice. So far what I did was just steal code from SciTE and implement the same API they have for using Lua.



  • Hello dail,

    Thanks for replying.

    I use Dave’s PythonScript and absolutely love it.
    Still, one can always learn and benefit from other good options.

    I’m glad you plan to continue developing “LuaScript”. I’ll be looking forward to the next versions.

    Thanks again for your great work.

    Best regards.



  • I agree the PythonScript plugin is very useful. I plan on stealing borrowing lots of code from that :)



  • I’m sure Dave would approve the borrowing. :)
    That would be great.

    When you start working on displaying custom scripts in the menu, please consider the following point:
    As far as I know, you can’t assign a nested plugin command to NPP context-menu.
    So it would be nice to have an option to display scripts in the LuaScript’s main menu.

    Thanks again and best of luck.



  • Version 0.1.1 has been released to fix several important bugs.

    Thanks @Claudia-Frank for the testing!



  • Wow! Very exciting. I am looking forward to testing this as a lightweight replacement for PythonScript.

    I agree that we should have an easy/smart way to access the scripts from the menu/toolbar/context.

    Can you provide some explanation of how to store and access scripts? What folder do they go into? Is it possible to include other (external) lua libraries from ?



  • @Ethan-Piliavin said:

    Can you provide some explanation of how to store and access scripts? What folder do they go into? Is it possible to include other (external) lua libraries from ?

    Since this isn’t possible yet I haven’t really given it much thought at all. My knowledge is very lacking of the Win32 API to create menus dynamically, etc. More than likely I’m going to re-use code (like any good software engineer should do) from the PythonScript plugin. So most likely you will see a lot of similarities. That kind of capability might be a little while in the future since for now I’m focusing on just getting the Lua API working as I want.



  • v0.2.0 has been released! Comments/questions/suggestions are welcome.

    Major updates include:

    • Upgraded Lua to 5.3
    • Ability to assign shortcut keys to Lua functions
    • Register callback functions for several events

    For example many editors support finding the next match and also selecting it so that multiple instances can be edited at the same time. Placing this code in the startup script adds that functionality to a shortcut.

    function SelectionAddNext()
    	-- From SciTEBase.cxx
    	local flags = 0 -- can use SCFIND_MATCHCASE
    	if not editor.SelectionEmpty and editor:IsRangeWord(editor.SelectionStart, editor.SelectionEnd) then
    		flags = flags | SCFIND_WHOLEWORD
    	end
    	editor:SetTargetRange(0, editor.TextLength)
    	editor.SearchFlags = flags
    	
    	-- From Editor.cxx
    	if editor.SelectionEmpty or not editor.MultipleSelection then
    		local startWord = editor:WordStartPosition(editor.CurrentPos, true)
    		local endWord = editor:WordEndPosition(startWord, true)
    		editor:SetSelection(startWord, endWord)
    	else
    		local i = editor.MainSelection
    		local s = editor:textrange(editor.SelectionNStart[i], editor.SelectionNEnd[i])
    		local searchRanges = {{editor.SelectionNEnd[i], editor.TargetEnd}, {editor.TargetStart, editor.SelectionNStart[i]}}
    		for _, range in pairs(searchRanges) do
    			editor:SetTargetRange(range[1], range[2])
    			if editor:SearchInTarget(s) ~= -1 then
    				editor:AddSelection(editor.TargetStart, editor.TargetEnd)
    				editor:ScrollRange(editor.TargetStart, editor.TargetEnd)
    				break
    			end
    		end
    	end
    end
    
    npp.AddShortcut("Selection Add Next", "Ctrl+Shift+D", SelectionAddNext)
    


  • @dail

    Comments/questions/suggestions are welcome.

    You asked for ;-)

    • make it theme aware
    • make userdata visible so that reflection can be used to execute code
    • possibility to clear console

    BUT, regardless of my whises - GOOD JOB

    Cheers
    Claudia



  • @Claudia-Frank

    You asked for ;-)

    Yep! Alot of this has been driven by my specific needs so the development can be kind of narrow. So it is always good to hear others’ ideas!

    make it theme aware

    Notepad++ doesn’t expose near enough API to make this possible without very ugly hacks. I tried a few hacks without much luck. However, there is a way partial way around this. There is a console object that you can use that is just like the editor object but it controls (no surprise) the console. In your startup script you can manually configure the styles for your theme doing something like this for each style Lua uses:

    console.StyleFore[SCE_LUA_COMMENT] = 0x272822
    

    This is not dynamic in any way, but if you are desperate you can hard-code alot of values.

    make userdata visible so that reflection can be used to execute code

    Care to elaborate a bit?

    possibility to clear console

    Good idea. Until then you can use this code:

    function clear()
    	console.ReadOnly = false
    	console:ClearAll()
    	console.ReadOnly = true
    end
    

    But agree a more user-friendly and intuitive option should also be added.



  • In python script you could do

    dir(editor)
    

    to get the list of editor functions.
    With lua you could use getmetatable(editor) to get the same
    but editor must have a metatable of course.

    My idea behind is to use that kind of reflection to create automated test cases and
    easily see if there are new functions. Another idea might be to enhance npp with
    lua script for a dynamic intellisense and debugging functionality.

    Cheers
    Claudia



  • Alot of the internal code came directly from SciTE. Mainly because I had no idea what I was doing (and honestly still don’t understand alot of it) ;)

    Since the editor object has >400 methods/properties (not including getters and setters) there isn’t a Lua table that holds all this kind of information. Methods and properties are looked up as they are used, which helps reduce memory, etc especially since only a hand full of them are probably going to be used on a regular basis. That being said I’m not opposed to exposing some way to get this data. I’m certainly open to suggestions and use-cases.



  • To be honest, I just read about the possibility of having userdata exposing metable a few days ago. ;-)
    Seems to be a new feature in lua 5.3 - I will try to get more details about it and if I feel I understood enough
    I might open a feature request ;-)

    Cheers
    Claudia



  • @Yaron

    In case you are curious, this is how you’d get Visual Studio-like copy and paste functionality:

    -- Mimic Visual Studio's "Ctrl+C" that copies the entire line if nothing is selected
    npp.AddShortcut("Copy Allow Line", "Ctrl+C", function()
        editor:CopyAllowLine()
    end)
    
    -- Mimic Visual Studio's "Ctrl+X" that cuts the line if nothing is selected
    npp.AddShortcut("Cut Allow Line", "Ctrl+X", function()
        if editor.SelectionEmpty then
            editor:CopyAllowLine()
            editor:LineDelete()
        else
            editor:Cut()
        end
    end)
    

    Also shortcuts show up in the plugin’s main menu so in theory you can assign them to N++'s context menu.



  • Hi All,

    A simple method to mimic the Visual Studio’s shortcuts ( CTRL +C/X ) is to change the default shortcuts of the lines 91 and 92, of the Scintilla commands tab, in the Shortcut Mapper !

    Just set the shortcut ALT + CTRL + X, in line 91 and the shortcut ALT + CTRL + C, in line 92

    Of course, I need to use the additional ALT key. So, it’s not as neat as the Dail’s Lua script :-) But this may help, anyway !

    REMARK : If I remember correctly, I think that :

    • The ALT + CTRL + C shortcut is used, by default, to open the Color Picker plugin

    • The ALT + CTRL + X shortcut performs, by default, the option Translate CamelCase/underscore_case of the Translate plugin

    So, be aware of that, if you use the Color Picker and/or Translate plugins !

    Cheers,

    guy038



  • Just using SCI_LINECUT and SCI_LINECOPY doesn’t quite match Visual Studio’s behavior. ;)



  • Hi Dail,

    I, first, based on your two comments, in your Lua script, below :

    – Mimic Visual Studio’s “Ctrl+C” that copies the entire line if nothing is selected

    – Mimic Visual Studio’s “Ctrl+X” that cuts the line if nothing is selected

    As you said that SCI_LINECUT and SCI_LINECOPY don’t have exactly the same behaviour :

    • I downloaded your last 0.2.0 version of your Lua plugin

    • After some searches, in your Lua documentation, I understood that the Notepad object npp.AddShortcut must be called, on startup only !

    • So, I put your lua script text, inside the StartUp script and save it. Then, I closed and restarted my 6.8.8 N++.

    => As expected, the new commands CTRL + C and CTRL + X do the job, nicely, even if no selection exists !

    Unfortunately, I couldn not see any difference with the Scintilla commands SCI_LINECUT and SCI_LINECOPY ( that I kept, with the ALT + CTRL + X/C shortcuts ), even after pasting the clipboard contents, in an other document !

    What I’m missing ?

    Cheers,

    guy038


Log in to reply