Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    General Discussion
    4
    26
    365
    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 Blinco
      Anthony Blinco last edited by

      Thanks to your awesome help i have found how to trigger functions within NppExec so i can map functions to multiple keys.

      I would like to be able to select some text in the edit window and then hit Shift+F8 and Notepad++ would search the document for all occurences of the selected text.
      Effectively launching the search dialog (npp_sendmsg NPPM_MENUCOMMAND 0 IDM_SEARCH_FIND) and then triggering the “Find All in Current Document” button (Which is where i need help)

      Thanks again for your help

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

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

        Effectively launching the search dialog (npp_sendmsg NPPM_MENUCOMMAND 0 IDM_SEARCH_FIND) and then triggering the “Find All in Current Document” button

        You’d probably also want to set some options a certain way, e.g. Match case, Search Mode, etc. before initiating your search.

        Overall, this is not an “easy” thing to do.

        Here are some previous attempts on such a topic:

        • https://community.notepad-plus-plus.org/topic/20312/how-to-start-a-search-automatically
        • https://community.notepad-plus-plus.org/topic/12747/right-click-find-all-in-current-document
        Anthony Blinco 1 Reply Last reply Reply Quote 0
        • Anthony Blinco
          Anthony Blinco @Alan Kilborn last edited by

          @Alan-Kilborn Thanks again for all the help. I really appreciate it.

          I am thinking i might have to implement a search using python/scintilla. I figure i will have to;

          1. get the selected text using editor.getText()??
          2. loop through using editor.search(), writing the results to the console()

          Does that sound possible and/or wise?

          Also, Is it possible to attach a python script to a keystroke like shift+ctrl+f

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

            I have progressed. Using the python scintilla functions i have managed to find all ocurences of the selected text.
            Very very stupidly, i have not considered, at all, how i am going to allow the user to double click on a console line and go to the found line.
            Any ideas?

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

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

              Is it possible to attach a python script to a keystroke like shift+ctrl+f

              Yes, THIS POSTING shows how to bind a keycombo of your choosing to the running of a script.

              how i am going to allow the user to double click on a console line and go to the found line.
              Any ideas?

              Yes, if, in your output to the console window, you mimic Python’s own error format, your filepath will be underlined and a single click on it will open the file to the correct line number. I suppose this won’t quite get you to the matching column, but it gets you fairly close.

              So, for example, if I put a z character at an arbitrary place in a script file and run the file, I get something like this in the console:

              bb8c092d-c807-444a-8531-0575620f864b-image.png

              The underlined path is a clickable link; this line shows the format you would recreate in your own console output in order to cause the console parser to turn it into a link for you.

              1 Reply Last reply Reply Quote 3
              • Anthony Blinco
                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 Kilborn
                  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 Blinco
                    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

                    Ekopalypse 1 Reply Last reply Reply Quote 0
                    • Alan Kilborn
                      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 Kilborn
                        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 Kilborn
                          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
                          • Ekopalypse
                            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 Blinco
                              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 Blinco
                                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 Kilborn PeterJones 2 Replies Last reply Reply Quote 0
                                • Alan Kilborn
                                  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
                                  • PeterJones
                                    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 Blinco
                                      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 Blinco
                                        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 Kilborn 1 Reply Last reply Reply Quote 0
                                        • Alan Kilborn
                                          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”.

                                          PeterJones 1 Reply Last reply Reply Quote 2
                                          • PeterJones
                                            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
                                            Copyright © 2014 NodeBB Forums | Contributors