Community
    • Login

    [New Plugin] LuaScript

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    29 Posts 9 Posters 67.8k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • dailD
      dail @Ethan Piliavin
      last edited by

      @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.

      1 Reply Last reply Reply Quote 0
      • dailD
        dail
        last edited by

        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)
        
        Claudia FrankC 1 Reply Last reply Reply Quote 1
        • Claudia FrankC
          Claudia Frank @dail
          last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • dailD
            dail
            last edited by dail

            @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.

            1 Reply Last reply Reply Quote 0
            • Claudia FrankC
              Claudia Frank
              last edited by

              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

              1 Reply Last reply Reply Quote 0
              • dailD
                dail
                last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • Claudia FrankC
                  Claudia Frank
                  last edited by Claudia Frank

                  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

                  1 Reply Last reply Reply Quote 1
                  • dailD
                    dail
                    last edited by

                    @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.

                    1 Reply Last reply Reply Quote 0
                    • guy038G
                      guy038
                      last edited by guy038

                      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

                      1 Reply Last reply Reply Quote 0
                      • dailD
                        dail
                        last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • guy038G
                          guy038
                          last edited by

                          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

                          1 Reply Last reply Reply Quote 0
                          • dailD
                            dail
                            last edited by dail

                            With the two shortcuts I’ve posted they work to enhance the normal copy and cut functionality. If there is a selection they both work as normal copy/cut. If there is no selection then they behave by copying or cutting the line. However, SCI_LINECUT and SCI_LINECOPY doesn’t pay any attention the the selection.

                            Secondly, to quote the documentation about SCI_COPYALLOWLINE

                            On Windows, an extra “MSDEVLineSelect” marker is added to the clipboard which is then used in SCI_PASTE to paste the whole line before the current line.

                            So the difference is when it is pasted it replaces the entire line instead of pasting the line directly at the cursor’s position.

                            Similar effects can be achieved both ways but users that are use to Visual Studios will definitely notice a difference.

                            1 Reply Last reply Reply Quote 0
                            • YaronY
                              Yaron
                              last edited by

                              Hello all,

                              @Claudia-Frank,
                              Thank you for your contribution (and generosity…).

                              @guy038,
                              Thank you for your contribution. Nice to see an old friend. :)

                              @dail,
                              Thank you for further developing and improving this important plugin. Great work.

                              And thanks also for referring to my Copy Line request. Really nice.

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

                              I actually meant an option to copy the entire line even if part of it is selected.
                              In a future version. :)

                              And, with your permission, two more requests:

                              1. A context-menu in the Console Output field.
                              2. An option to change text direction (LTR/RTL) in the Console Input field.
                                (PythonScript uses the System Edit-Context-Menu which includes this functionality).

                              Best regards.

                              1 Reply Last reply Reply Quote 0
                              • dailD
                                dail
                                last edited by

                                @Yaron

                                I actually meant an option to copy the entire line even if part of it is selected.

                                If I understand what you are wanting you can simply use editor:LineCopy()

                                A context-menu in the Console Output field.

                                Would be simple to add. If you want you can open a feature request on Github and we can discuss it more there.

                                An option to change text direction (LTR/RTL) in the Console Input field.

                                One of the change I made from PythonScript was replace the input field with a Scintilla control. So I’m not sure how easy this would be to change the text direction.

                                1 Reply Last reply Reply Quote 0
                                • guy038G
                                  guy038
                                  last edited by

                                  Hello Dail,

                                  Thanks for your quick reply ! But, even before opening my laptop and seeing your post, this morning ( in France ! ), I had understood the differences between the two behaviours. Indeed, although I ran the copy and cut commands, with a previous selection, I did not see the differences, at first sight !

                                  Sorry. It would have saved you to reply me about this matter, as you, certainly, need time to develop your Lua plugin :-(

                                  Best Regards

                                  guy038

                                  BTW, it’s a chance that you give the Visual Studio’s method, about copy/cut text ! Because, the two commands editor:LineCopy() and editor:LineDelete() are absent, from your Lua Script documentation, at the address below :

                                  https://github.com/dail8859/LuaScript/blob/master/doc/editor.md

                                  1 Reply Last reply Reply Quote 0
                                  • YaronY
                                    Yaron
                                    last edited by

                                    Hello dail,

                                    Thanks for replying.

                                    Yes, I was looking for ‘editor:LineCopy()’.
                                    Not finding it in the documentation page, I assumed it was not available.

                                    I’ve opened two issues on GitHub.

                                    Best regards.

                                    1 Reply Last reply Reply Quote 0
                                    • dailD
                                      dail
                                      last edited by

                                      A new version has been release. v0.4.0

                                      Now with API documentation. https://dail8859.github.io/LuaScript/

                                      This release includes bug fixes, GUI enhancements, and a large portion of the Notepad++ message and notifications are available.

                                      As always, comments/questions are welcome.

                                      1 Reply Last reply Reply Quote 0
                                      • M
                                        MaximilianKohler
                                        last edited by MaximilianKohler

                                        The startup script doesn’t seem to work with the PortableApps dot com version. I was trying to use this:

                                        community.notepad-plus-plus dot org/topic/11233/disable-ascii-control-characters-shortcuts/5

                                        I’ll post to the portableapps forum and see what they say:
                                        portableapps dot com/node/70143

                                        Alan KilbornA mpheathM 2 Replies Last reply Reply Quote 0
                                        • Alan KilbornA
                                          Alan Kilborn @MaximilianKohler
                                          last edited by

                                          @MaximilianKohler said in [New Plugin] LuaScript:

                                          with the PortableApps dot com version

                                          This isn’t a supported version.
                                          Best thing to do is not use it.
                                          Notepad++ itself distributes a “portable” version – use that.

                                          1 Reply Last reply Reply Quote 0
                                          • mpheathM
                                            mpheath @MaximilianKohler
                                            last edited by mpheath

                                            @MaximilianKohler Do a simple test to debug.

                                            startup.lua:

                                            -- Startup script
                                            -- Changes will take effect once Notepad++ is restarted
                                            
                                            print('Hello World')
                                            

                                            Console:

                                            Hello World
                                            Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
                                            

                                            With the PortableApps distribution of Notepad++, If you install a plugin and Notepad++ restarts then the environmental variables related to PortableApps are not set from that instance of Notepad++. You need to close that instance and start a new instance using Notepad++Portable.exe to have those environmental variables set. You can check by running CMD and use the command set from Notepad++ to see all the extra environmental variables that are added or values being modified.

                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors