Community
    • Login

    Is it possible to map multiple keys to notepad++ functions?

    Scheduled Pinned Locked Moved General Discussion
    26 Posts 4 Posters 2.2k 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.
    • Anthony BlincoA
      Anthony Blinco
      last edited by Anthony Blinco

      Thank you Alan. I must say your esponses have been the most concise and “to the point” answers i have ever had in a forum, and i am a member of a few. Kudos to you matey.

      I did not know how to embed code or a file so here is a link to the python script. It’s my first go at pythoneering so i’m sure there are things i should have done differently.

      [https://drive.google.com/file/d/1TtBFP1XT3aGzylxJyDHAQFWCdAdpyE2v/view?usp=sharing](link url)

      If you want to embed it in the thread for a more permanent record then go right ahead.
      Thanks again
      I owe you several beers

      1 Reply Last reply Reply Quote 1
      • Alan KilbornA
        Alan Kilborn
        last edited by

        Here’s the script:

        # -*- coding: utf-8 -*-
        
        from Npp import *
        from os import *
        
        def getFilename(name):
            i = len(name) - 1
            new = ""
            while i >= 0 and name[i] <> "\\":
                new = name[i] + new
                i = i - 1
            return new
        
        
        search = editor.getSelText()
        
        console.show()
        console.clear()
        
        if search == "":
            console.write("Please select a search string")
        else:
        
            console.write('Finding : '+ search + '\n')
            
            editor.setCurrentPos(0)
            editor.searchAnchor()
        
            caret = 0
        
            while caret > -1:
                caret = editor.searchNext(0, search)
                if caret < 0:
                    break
                lineno = editor.lineFromPosition(caret)
                line = editor.getLine(lineno)
                new = caret+len(search)
                editor.setCurrentPos(new)
                editor.searchAnchor()    
                console.write("  File \""+getFilename(notepad.getCurrentFilename())+"\", line "+str(lineno+1)+" "+line )
        #        console.write(os.path.basename(notepad.getCurrentFilename()))
        
            console.write('Search complete\n') 
        
        1 Reply Last reply Reply Quote 0
        • Anthony BlincoA
          Anthony Blinco
          last edited by

          One last question (Hopefully): I want to change the colours of my message to the console.
          I think i need to use console.style.styleSetFore(style, (r, g, b))

          How do i associate colors with text?

          I would like;
          “Finding : text” to be one color
          “File filename, line 999” to be another color
          and the line to be yet another color

          EkopalypseE 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn
            last edited by Alan Kilborn

            While I’m not one to mess with something that works, I think you went in some directions with your script that were a bit “diversionary”. Here’s how I would have done it:

            # -*- coding: utf-8 -*-
            
            from Npp import editor, notepad
            import os
            
            sel_text = editor.getSelText()
            if len(sel_text) == 0:
                notepad.messageBox('No selected text.', '')
            else:
                matches = []
                editor.search(sel_text, lambda m: matches.append(m.span(0)[0]))
                if len(matches) > 0:
                    console.show()
                    for pos in matches:
                        line_number = editor.lineFromPosition(pos)
                        line_content = editor.getLine(line_number).rstrip()
                        console.write('  File "{f}", line {l}, {c}\n'.format(
                            f = notepad.getCurrentFilename().rsplit(os.sep)[-1],
                            l = line_number + 1,
                            c = line_content))
            

            Assuming the script is called t.py (my favorite name for a “test” script I’m working on, before giving it a real name if I keep it), if one runs the script with some text selected:

            201dac1a-0f94-43da-b77a-387ce48707bc-image.png


            The output looks like this:

            c2c8564a-8263-47d9-86da-b5c57fda3d84-image.png

            1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn
              last edited by

              @Anthony-Blinco said in Is it possible to map multiple keys to notepad++ functions?:

              How do i associate colors with text?

              I normally let Notepad++ do this sort of coloring for me (it’s called lexing), so I actually don’t know the answer to this one. I suspect it might be more effort than you’d want to go to, for this simple application. Certainly doable, however.

              Are colors going to add a lot of value to it? A question you have to answer, for yourself.

              1 Reply Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn
                last edited by

                A possibility not involving color might be to show the matching text in this fashion:

                  File "t.py", line 10,     matches = []
                                            ^^^^^^^
                  File "t.py", line 11,     editor.search(sel_text, lambda m: matches.append(m.span(0)[0]))
                                                                              ^^^^^^^
                  File "t.py", line 12,     if len(matches) > 0:
                                                   ^^^^^^^
                  File "t.py", line 14,         for pos in matches:
                                                           ^^^^^^^
                
                1 Reply Last reply Reply Quote 0
                • EkopalypseE
                  Ekopalypse @Anthony Blinco
                  last edited by

                  @Anthony-Blinco

                  for example with something like this

                  from Npp import console
                  
                  console.editor.styleSetFore(60, (128, 255, 128))
                  console.editor.styleSetFore(61, (255, 128, 255))
                  console.editor.setReadOnly(False)
                  console.editor.addStyledText(Cell('Test\n', [60]))
                  console.editor.addStyledText(Cell('Test\n', [61]))
                  console.editor.addStyledText(Cell('Test\n', [60,61,60,61]))
                  console.editor.setReadOnly(True)
                  

                  5b007e26-81f5-4827-b59a-b544865d3f9d-image.png

                  1 Reply Last reply Reply Quote 2
                  • Anthony BlincoA
                    Anthony Blinco
                    last edited by Anthony Blinco

                    WOW! You guys are the best.
                    I will persue those points today
                    Thanks heaps!

                    1 Reply Last reply Reply Quote 1
                    • Anthony BlincoA
                      Anthony Blinco
                      last edited by Anthony Blinco

                      Thanks for the coding tip. I will definitely implement it. Much more readable

                      Thanks for the addStyledText() tip. I can get it to work for my purposes but it doesn’t seem to register for the click-and-goto-line functionality when i use it to print text.

                      Another question related to this;
                      Currently i use NppExec to compile my program, run my program and several other functions including launching the find dialog (using F8)
                      The problem is It always asks me to cancel the current command before it executes the next one. This can be problematic when i, for example, run my program and then try to compile another program or even launch the find dialog.
                      Is here a way to turn off this checking by NppExe or some way around this functionality?
                      Thanks again for your awesome help

                      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                      • Alan KilbornA
                        Alan Kilborn @Anthony Blinco
                        last edited by

                        @Anthony-Blinco

                        Sorry; you’ll have to wait for someone else to reply on NppExec.
                        While the contributors to this thread are aware of NppExec, we may not know a lot about it because we’re better at PythonScripting (and thus don’t have much of a need for what NppExec offers).

                        1 Reply Last reply Reply Quote 0
                        • PeterJonesP
                          PeterJones @Anthony Blinco
                          last edited by

                          @Anthony-Blinco said in Is it possible to map multiple keys to notepad++ functions?:

                          The problem is It always asks me to cancel the current command before it executes the next one

                          That means you’ve left some process open. Without seeing your NppExec script, we cannot tell you why it might still have the process open.

                          The compile-and-run FAQ gives some examples of NppExec scripts that compile source code then run the resulting program, but don’t have a problem of leaving a process still running. You might want to compare your scripts to those.

                          One trick, which is used in those examples, is to give the executable a new cmd window, instead of having it run directly inside the NppExec console – as called by npp_run cmd.exe /k "$(NAME_PART)", which spawns a new external process (npp_run), starts the command interpreter (cmd.exe) in a way that will leave it open after its argument finished (/k), and runs the compiled program in that new process ($(NAME_PART)) – npp_run is basically the NppExec-equivalent of the cmd.exe START command. (The same could also probably be accomplished in NppExec with cmd /c START "$(NAME_PART)".)

                          You can also search the forum for @Michael-Vincent posts that contain “NppExec”, as he is a power-user of NppExec (or wait for him to show up, as he might have more advice than I have). And @Vitaliy-Dovgan, the author of NppExec, does come here on occasion

                          1 Reply Last reply Reply Quote 3
                          • Anthony BlincoA
                            Anthony Blinco
                            last edited by

                            Perfect! Problem solved. Great advice once again.
                            Thank you all so much. You guys really know your stuff!

                            1 Reply Last reply Reply Quote 2
                            • Anthony BlincoA
                              Anthony Blinco
                              last edited by Anthony Blinco

                              Sorry for asking all the stupid questions.
                              I tried the block comment/uncomment but it doesn’t work with my particular compiler.

                              Can i change the comment/uncomment logic for my language (COBOL)?

                              or

                              It is a litle nuanced

                              1. I need to save the character at column 7 (which is where the comment indicator is *). because it could be one of a couple of values and i would need to restore it when they uncomment
                              2. I’m thinking i’m going to have to implement it in python. In which case i need to know how i can replace a line in the editing window

                              Thanks again for all your awesome advice

                              Also, i couldn’t find the editor.search() function in the python documentation. Am i looking at the right one
                              http://npppythonscript.sourceforge.net/docs/latest/scintilla.html#helper-methods

                              Alan KilbornA 1 Reply Last reply Reply Quote 0
                              • Alan KilbornA
                                Alan Kilborn @Anthony Blinco
                                last edited by

                                @Anthony-Blinco

                                Regarding: “comment…COBOL”: It’s probably best to start a new thread when you want to ask a totally unrelated question to what has come before in the current thread.

                                Regarding editor.search() – which is proper for this thread since we’ve been discussing PythonScript – why not just move your caret to an instance of that in your text and invoke Context-Help from the PythonScript menu, right in Notepad++ ?

                                Am i looking at the right one

                                http://npppythonscript.sourceforge.net/docs/latest/scintilla.html#helper-methods

                                I’d give that a “no”.

                                PeterJonesP 1 Reply Last reply Reply Quote 2
                                • PeterJonesP
                                  PeterJones @Alan Kilborn
                                  last edited by

                                  @Anthony-Blinco,

                                  @Alan-Kilborn said in Is it possible to map multiple keys to notepad++ functions?:

                                  Regarding editor.search() – which is proper for this thread since we’ve been discussing PythonScript – why not just move your caret to an instance of that in your text and invoke Context-Help from the PythonScript menu, right in Notepad++ ?

                                  Am i looking at the right one

                                  http://npppythonscript.sourceforge.net/docs/latest/scintilla.html#helper-methods

                                  I’d give that a “no”.

                                  Specifically, the old sourceforge copy of the documentation is severely out of date.

                                  When you install PythonScript, it also installs a local up to date version of the PythonScript documentation, which can be accessed through Notepad++ menu Plugins > PythonScript > Context-Help (and, as @Alan-Kilborn mentioned, if your caret is on the editor.search() command, it should open the help to the right place). Use that for all documentation needs on PythonScript. But yes, it is in the helper-methods section of the editor object documentation.

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