Pythonscript hidelines()
-
If you want to “hide lines” the correct way, you need to tell Notepad++ to do it for you as @Alan-Kilborn mentioned all the extra stuff Notepad++ does.
I don’t know how to do it in PythonScript but you need to trigger the
IDM_VIEW_HIDELINES
menu item. In LuaScript it is as easy asnpp:MenuCommand(IDM_VIEW_HIDELINES)
and then you get the chevrons, placeholder, and don’t loose the hidden lines. -
here is the python code of lua dail code
editor.hideLines(1, 33) # hide line 1 to 33 notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES)
-
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. -
@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 -
@paul-wormer said in Pythonscript hidelines():
notepad.menuCommand(MENUCOMMAND.VIEW_HIDELINES)
Unfortunately it does not work.
It works for me.
-
@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. -
@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”?
-
@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: -
@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…
-
@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)?
-
@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. -
@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.