Community
    • 登入

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

    已排程 已置頂 已鎖定 已移動 General Discussion
    26 貼文 4 Posters 2.7k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Anthony BlincoA
      Anthony Blinco @Alan Kilborn
      最後由 編輯

      @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 條回覆 最後回覆 回覆 引用 1
      • Anthony BlincoA
        Anthony Blinco
        最後由 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 KilbornA 1 條回覆 最後回覆 回覆 引用 0
        • Alan KilbornA
          Alan Kilborn @Anthony Blinco
          最後由 編輯

          @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 條回覆 最後回覆 回覆 引用 3
          • Anthony BlincoA
            Anthony Blinco
            最後由 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 條回覆 最後回覆 回覆 引用 1
            • Alan KilbornA
              Alan Kilborn
              最後由 編輯

              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 條回覆 最後回覆 回覆 引用 0
              • Anthony BlincoA
                Anthony Blinco
                最後由 編輯

                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 條回覆 最後回覆 回覆 引用 0
                • Alan KilbornA
                  Alan Kilborn
                  最後由 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 條回覆 最後回覆 回覆 引用 0
                  • Alan KilbornA
                    Alan Kilborn
                    最後由 編輯

                    @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 條回覆 最後回覆 回覆 引用 1
                    • Alan KilbornA
                      Alan Kilborn
                      最後由 編輯

                      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 條回覆 最後回覆 回覆 引用 0
                      • EkopalypseE
                        Ekopalypse @Anthony Blinco
                        最後由 編輯

                        @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 條回覆 最後回覆 回覆 引用 2
                        • Anthony BlincoA
                          Anthony Blinco
                          最後由 Anthony Blinco 編輯

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

                          1 條回覆 最後回覆 回覆 引用 1
                          • Anthony BlincoA
                            Anthony Blinco
                            最後由 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 條回覆 最後回覆 回覆 引用 0
                            • Alan KilbornA
                              Alan Kilborn @Anthony Blinco
                              最後由 編輯

                              @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 條回覆 最後回覆 回覆 引用 0
                              • PeterJonesP
                                PeterJones @Anthony Blinco
                                最後由 編輯

                                @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 條回覆 最後回覆 回覆 引用 3
                                • Anthony BlincoA
                                  Anthony Blinco
                                  最後由 編輯

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

                                  1 條回覆 最後回覆 回覆 引用 2
                                  • Anthony BlincoA
                                    Anthony Blinco
                                    最後由 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 條回覆 最後回覆 回覆 引用 0
                                    • Alan KilbornA
                                      Alan Kilborn @Anthony Blinco
                                      最後由 編輯

                                      @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 條回覆 最後回覆 回覆 引用 2
                                      • PeterJonesP
                                        PeterJones @Alan Kilborn
                                        最後由 編輯

                                        @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 條回覆 最後回覆 回覆 引用 3
                                        • 第一個貼文
                                          最後的貼文
                                        The Community of users of the Notepad++ text editor.
                                        Powered by NodeBB | Contributors