• Login
Community
  • Login

Pythonscript hidelines()

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
14 Posts 4 Posters 814 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.
  • A
    Alan Kilborn
    last edited by Dec 30, 2021, 5:02 PM

    So @dail 's point is a good one, but I’ll just point out that there is no real equivalent/good way to restore hidden lines to a shown state using script code. That is, there’s no Notepad++ control for “clicking a chevron” to show some previously hidden lines. You can probably call editor.showLines() but that is subverting Notepad++'s higher level control and thus it probably won’t play nicely.

    1 Reply Last reply Reply Quote 0
    • P
      Paul Wormer @cmeriaux
      last edited by Dec 31, 2021, 8:51 AM

      @cmeriaux said in Pythonscript hidelines():

      notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES)

      Unfortunately it does not work. I have hidelines in a loop and tried the view_hidelines setting both inside the loop and just before the loop. In neither case I got an error message. I don’t see any effect of the setting. My Npp properties are:

      Notepad++ v8.1.9.3 (64-bit)
      Build time : Dec 6 2021 - 19:21:37
      Path : C:\Program Files\Notepad++\notepad++.exe
      Command Line :
      Admin mode : OFF
      Local Conf mode : OFF
      Cloud Config : OFF
      OS Name : Windows 10 Home (64-bit)
      OS Version : 2009
      OS Build : 19043.1415
      Current ANSI codepage : 1252
      Plugins : ColumnTools.dll ComparePlugin.dll DSpellCheck.dll HexEditor.dll mimeTools.dll NppConverter.dll NppExport.dll PythonScript.dll

      A 2 Replies Last reply Dec 31, 2021, 11:59 AM Reply Quote 0
      • A
        Alan Kilborn @Paul Wormer
        last edited by Dec 31, 2021, 11:59 AM

        @paul-wormer said in Pythonscript hidelines():

        notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES)

        Unfortunately it does not work.

        It works for me.

        P 1 Reply Last reply Jan 2, 2022, 12:50 PM Reply Quote 0
        • P
          Paul Wormer @Alan Kilborn
          last edited by Jan 2, 2022, 12:50 PM

          @alan-kilborn
          That’s encouraging. Could you please compare your Python installation parameters with those of mine given in my earlier post?
          If you find any differences, would you be so kind as to post them here? Thank you.

          A 1 Reply Last reply Jan 2, 2022, 12:55 PM Reply Quote 0
          • A
            Alan Kilborn @Paul Wormer
            last edited by Jan 2, 2022, 12:55 PM

            @paul-wormer said in Pythonscript hidelines():

            Could you please compare your Python installation parameters with those of mine given in my earlier post?

            What are these “parameters”?

            1 Reply Last reply Reply Quote 0
            • A
              Alan Kilborn
              last edited by Jan 2, 2022, 12:59 PM

              @paul-wormer said in Pythonscript hidelines():

              Unfortunately it does not work

              Why don’t you start simple?:

              • put your caret on a line that you want to hide, somewhere in the middle of a document
              • execute notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES) in the PythonScript console
              • see if the line your caret was on is now hidden

              The PythonScript console is shown by going to the Plugins > Python Script > Show Console menu. You’d paste the above command after the >>> prompt, in the box:

              37ff06d6-b7d6-4430-82b6-729f82a1e846-image.png

              1 Reply Last reply Reply Quote 1
              • A
                Alan Kilborn @Paul Wormer
                last edited by Jan 2, 2022, 1:05 PM

                @paul-wormer said in Pythonscript hidelines():

                In neither case I got an error message.

                Unless you make an error of Python syntax, you really won’t get error messages. Meaning that you can do something that really should be an error, and simply nothing will happen.

                As an example, in PythonScript there’s a command to set the current position of your caret in the document. This function takes an argument of the desired position. Positions range from 0 to the size of the document. But, if you do something like:

                editor.setCurrentPos(-1234)

                …this is clearly a problem as such a position does not exist. However, no error of any sort is generated, just “silence”. Actually what happens is that the current position is moved to the very first position in the file. And that’s ok, I guess, for this type of function (doing the best it can to achieve your goal).

                Others (myself included) would like to see some sort of error exception thrown in this circumstance.

                The moral: Don’t expect a lot of non-syntax errors from PythonScript programming…

                P 1 Reply Last reply Jan 2, 2022, 3:54 PM Reply Quote 1
                • P
                  Paul Wormer @Alan Kilborn
                  last edited by Jan 2, 2022, 3:54 PM

                  @alan-kilborn
                  I used editor.hideLines(start, end), which indeed hid the lines indicated, but with the problems outlined at the beginning of this thread.

                  Now you have taught me something useful, by pointing out that instead of editor.hidelines(), I must use
                  notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES).
                  And indeed this works for a selected set of lines without the above problems. (I misunderstood cmeriaux, I thought this cmd was only for the setting of parameters, but I understand now that it actively hides lines).

                  My next question is: how do select a contiguous block of lines to hide? Do I use the select mechanism? Or is there another way, like in editor.hidelines(start, end)?

                  A 1 Reply Last reply Jan 2, 2022, 4:45 PM Reply Quote 1
                  • A
                    Alan Kilborn @Paul Wormer
                    last edited by Jan 2, 2022, 4:45 PM

                    @paul-wormer said in Pythonscript hidelines():

                    how do select a contiguous block of lines to hide? Do I use the select mechanism?

                    The menu command for Hide Lines acts on a selected range of lines if one is active when the command is invoked. Thus if you are scripting something and invoking the menu command, if your script selects some lines before invoking the command, the command will act on that selection.

                    So you could use editor.setSelection() before doing the menu command.

                    P 1 Reply Last reply Jan 4, 2022, 1:04 PM Reply Quote 2
                    • P
                      Paul Wormer @Alan Kilborn
                      last edited by Jan 4, 2022, 1:04 PM

                      @alan-kilborn
                      I thank you and the others in this thread for their patient advice. To show what I did with it, I copy my script and sample input.

                      """
                          Hide entries in a diary.
                          
                          - Daily entries are preceded by an empty line 
                            followed by the date on a single line. 
                          - Dates can be either in American or in European format. 
                          - Days occur with irregular intervals.
                      """
                      
                      from __future__ import print_function # Python 2.7
                      from Npp import *
                      
                      def getStartPositionNextLine(pos):
                          """
                              Get start position of line below line of `pos`.
                              Note that positions in scripts start at 0 whereas the
                              statusline gives a position starting at 1.
                          """
                          rpos = editor.getLineEndPosition(editor.lineFromPosition(pos)) + 2 #skip \r\n
                          return rpos
                          
                      def getEndPositionPreviousLine(pos): 
                          rpos = editor.getLineEndPosition(editor.lineFromPosition(pos)-1)  
                          return rpos
                          
                      regex    = r'^\d{1,2}(-|/)\d{1,2}(-|/)\d{4}?' # European or American date
                      lenFile  = editor.getLength()
                               
                      # Find start and end position of first date string:
                      startDate, endDate   = editor.findText(FINDOPTION.REGEXP, 0, lenFile, regex)
                      
                      startSel = getStartPositionNextLine(endDate) 
                      editor.setSelectionStart(startSel)
                      
                      while endDate < lenFile:
                          startDate0, endDate0 = startDate, endDate 
                          try:
                              startDate, endDate = editor.findText(FINDOPTION.REGEXP, endDate0, lenFile, regex)
                          except:
                              break
                          else:
                              # Go back 2 lines skipping empty line:
                              endSel   = getEndPositionPreviousLine(getEndPositionPreviousLine(endDate))
                              editor.setSelectionEnd(endSel)
                              notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES)
                              
                              # New selection starts at line below date line:
                              startSel = getStartPositionNextLine(endDate)
                              editor.setSelectionStart(startSel)
                      
                      editor.setSelectionEnd(lenFile)
                      notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES)
                      
                      

                      A sample diary:

                      DE BELLO GALLICO
                      
                      3-1-2019
                      Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur. Hi omnes lingua, institutis, legibus inter se differunt. Gallos ab Aquitanis Garumna flumen, a Belgis Matrona et Sequana dividit. Horum omnium fortissimi sunt Belgae, propterea quod a cultu atque humanitate provinciae longissime absunt, minimeque ad  eos mercatores saepe commeant atque ea quae ad effeminandos animos pertinent important, proximique sunt Germanis, qui trans Rhenum incolunt, quibuscum continenter ellum gerunt. 
                                            
                      7/6/2019         
                      Qua de causa Helvetii quoque reliquos Gallos virtute praecedunt, quod fere cotidianis  proeliis cum Germanis contendunt, cum aut suis finibus eos prohibent aut ipsi in eorum finibus bellum gerunt. Eorum una pars, quam Gallos obtinere dictum est, initium capit a  flumine Rhodano, continetur Garumna flumine, Oceano, finibus Belgarum, attingit etiam ab  Sequanis et Helvetiis flumen Rhenum, vergit ad septentriones. Belgae ab extremis Galliae  finibus oriuntur, pertinent ad inferiorem partem fluminis Rheni, spectant in septentrionem  et orientem solem. Aquitania a Garumna flumine ad Pyrenaeos montes et eam partem Oceani quae est ad  ispaniam pertinet; spectat inter occasum solis et septentriones.
                                
                      10/9/2019 (ibidem)
                      Apud Helvetios longe nobilissimus fuit et ditissimus Orgetorix. Is M. Messala, M. Pisone consulibus regni cupiditate inductus coniurationem nobilitatis fecit et civitati persuasit ut de finibus suis cum omnibus copiis exirent: perfacile esse, cum virtute omnibus praestarent, totius Galliae imperio potiri. Id hoc facilius iis persuasit,  quod undique loci natura Helvetii continentur : una ex parte flumine Rheno latissimo  atque altissimo, qui agrum elvetium a Germanis dividit; altera ex parte monte Iura altissimo,  qui est inter Sequanos et Helvetios; tertia lacu Lemanno et flumine odano, qui provinciam nostram ab Helvetiis dividit.
                      
                      22-12-2019
                      His rebus fiebat ut et minus late vagarentur et minus facile finitimis bellum inferre possent; qua ex parte homines bellandi cupidi agno dolore adficiebantur. Pro multitudine autem hominum et progloria belli atque fortitudinis angustos se fines habere arbitrabantur, qui in longitudinem milia passuum CCXL, in latitudinem CLXXX patebant.
                      
                      
                      1 Reply Last reply Reply Quote 2
                      14 out of 14
                      • First post
                        14/14
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors