How do I move bookmarks to a copy of the same file?
-
@Alan-Kilborn said in How do I move bookmarks to a copy of the same file?:
It changed from 24 to 20.
I meant for example this information (ID-Number of Bookmarks)
-
@Alan-Kilborn I need your help in writing a plugin that I would call “all”. You may know that Kedit and Xedit have the command “all /string/” (both editors have a command line). This has the effect that only the lines with “string” in it are displayed and may be edited. I used that command a lot in my Xedit/Kedit days.
Looking at your script it occurred to me that it is fairly easy to write a script with nearly the same functionality. I experimented with the following Npp steps:
- “Mark All” of selected string with bookmarks on.
- Invert the bookmarks.
- Apply the following derivative of your script:
NPP_BOOKMARK_MARKER_ID_NUMBER = 20 # from N++ source code: MARK_BOOKMARK = 20; NPP_BOOKMARK_MARKER_MASK = 1 << NPP_BOOKMARK_MARKER_ID_NUMBER line_nbr = editor.markerNext(0, NPP_BOOKMARK_MARKER_MASK) editor.hideLines(line_nbr, line_nbr) while line_nbr != -1: line_nbr = editor.markerNext(line_nbr + 1, NPP_BOOKMARK_MARKER_MASK) editor.hideLines(line_nbr, line_nbr)
As far as I can see the result is very near the behavior of Kedit’s “all /string/” . What I very much like to have is a script that combines the three steps into one. I assume that you (and Peter) know how the first two steps can be achieved by Scintilla commands. This then could be cast into a single script, which in turn could be assigned to a key combination.
-
I think I have such a combined script already, or at least close to your desires. Since it is off-topic to this current thread, I’ll create a new thread and post the script and discussion there (later today).
EDIT: I had a look at my script to which I referred, and to post it, it needs a bit of rework. I will get to it, and will post the results, but it may not be the promised “later today”. Surely in the next week, though.
-
I adjusted the script from @Alan-Kilborn to fullfill exactly my needs - one script to save all bookmarks to a file, and a second one to reload them from a file. This is helpful for me as I do edit files on a remote machine; when I reload these files many times the bookmarks get lost.
Maybe someone else will find this helpful also, so I provide it here.
One thing I did not get a solution for: I wanted to add a file selection, but I was not able to do it. So right now a fixed file location/file name has to be adjusted in both scripts.bookmark_save.py:
# -*- coding: utf-8 -* from Npp import editor, notepad, NOTIFICATION, SCINTILLANOTIFICATION import datetime NPP_BOOKMARK_MARKER_ID_NUMBER = 20 # from N++ source code: MARK_BOOKMARK = 20; NPP_BOOKMARK_MARKER_MASK = 1 << NPP_BOOKMARK_MARKER_ID_NUMBER filenameInclPath='D:\Test\Marker.txt' f = open(filenameInclPath,"w") line_nbr = editor.markerNext(0, NPP_BOOKMARK_MARKER_MASK) bookmark_line_list = [] outstring='' while line_nbr != -1: bookmark_line_list.append(line_nbr) line_nbr = editor.markerNext(line_nbr + 1, NPP_BOOKMARK_MARKER_MASK) for line in bookmark_line_list: outstring=outstring+str(line+1)+'\n' outstring=outstring[:-1] f.write(outstring) f.close()
bookmark_load.py
# -*- coding: utf-8 -* from Npp import editor, notepad, NOTIFICATION, SCINTILLANOTIFICATION import datetime NPP_BOOKMARK_MARKER_ID_NUMBER = 20 # from N++ source code: MARK_BOOKMARK = 20; NPP_BOOKMARK_MARKER_MASK = 1 << NPP_BOOKMARK_MARKER_ID_NUMBER filenameInclPath='D:\Test\Marker.txt' f = open(filenameInclPath,"r") numbBookmarks=0 for bookmark in f: numbBookmarks+=1 lineNo=int(bookmark.strip()) # print(numbBookmarks, lineNo) editor.markerAdd(lineNo-1, NPP_BOOKMARK_MARKER_ID_NUMBER) f.close()
-
@S-Merk said in How do I move bookmarks to a copy of the same file?:
One thing I did not get a solution for: I wanted to add a file selection
What do you mean by this? You wanted like a Windows File Open/Save box to open where you could select a file??
-
@Alan-Kilborn yes, exactly. I do not like things like hardcoded file- and pathnames. But anyway, this is more a cosmetic thing - the scripts as they are now make my programming life much easier already :-)
-
-
@Alan-Kilborn Thanks again! ok, I will have a look to the “EasyDialogs”. But as already said, I am quite happy with what I have right now!
-
Hi all,
a little hint to my previously posted litte bookmark_save script (I am not allowed to edit the previous post any more): I already destroyed my saved bookmark-file with my new tool. I used the save-script while I had selected the wrong tab without any bookmark, so i got an empty file.
If anyone else who is as chaotic as I am uses this little tool, it is maybe a good idea to use the following instead. This adds a little security to the script. If there are less than 5 bookmarks (or whatever you declare in MIN_BOOKMARKS), the file will not be written. :-)# -*- coding: utf-8 -* from Npp import editor, notepad, NOTIFICATION, SCINTILLANOTIFICATION MIN_BOOKMARKS = 5 NPP_BOOKMARK_MARKER_ID_NUMBER = 20 # from N++ source code: MARK_BOOKMARK = 20; NPP_BOOKMARK_MARKER_MASK = 1 << NPP_BOOKMARK_MARKER_ID_NUMBER filenameInclPath='D:\Temp\Marker.txt' print('Bookmark_Save: filenameInclPath: ', filenameInclPath) line_nbr = editor.markerNext(0, NPP_BOOKMARK_MARKER_MASK) bookmark_line_list = [] outstring='' while line_nbr != -1: bookmark_line_list.append(line_nbr) line_nbr = editor.markerNext(line_nbr + 1, NPP_BOOKMARK_MARKER_MASK) if len(bookmark_line_list) < MIN_BOOKMARKS: msg = "Bookmark_Save: not enough bookmarks (" + str(len(bookmark_line_list)) + "): Cancelled!" print(msg) else: for line in bookmark_line_list: outstring=outstring+str(line+1)+'\n' outstring=outstring[:-1] f = open(filenameInclPath,"w") f.write(outstring) f.close()
-
-
Further discussion regarding:
I need your help in writing a plugin that I would call “all”. You may know that Kedit and Xedit have the command “all /string/” (both editors have a command line). This has the effect that only the lines with “string” in it are displayed and may be edited. I used that command a lot in my Xedit/Kedit days.
I think I have such a combined script already, or at least close to your desires. Since it is off-topic to this current thread, I’ll create a new thread and post the script and discussion there (later today).
EDIT: I had a look at my script to which I referred, and to post it, it needs a bit of rework. I will get to it, and will post the results, but it may not be the promised “later today”. Surely in the next week, though.…is continued HERE.
-
-
@Paul-Wormer said in How do I move bookmarks to a copy of the same file?:
You may know that Kedit and Xedit have the command “all /string/” (both editors have a command line). This has the effect that only the lines with “string” in it are displayed and may be edited. I used that command a lot in my Xedit/Kedit days.
BTW, Paul, your idea is not “new” to Notepad++ users – not that you said it was. HERE’s someone from over 7(!) years ago, wanting the same thing.
-
@Paul-Wormer
I prepared a pedestrian script that mimicks theall command
of Xedit and Kedit. Just like those two editors (that both date from before color screens came into existence) my script doesn’t do any coloring. I hope that the bug in the Scintilla routinehideLines
is circumvented by invoking it twice. [During my writing of the script, it indeed happened several times that not all lines were hidden after issuingeditor.hideLines(0, int(editor.getLineCount())-1 )
].Here comes the script:
# -*- coding: utf-8 -*- from __future__ import print_function # Python 2.7 from Npp import * def disp_only_lines(string_to_show): ''' Display only the lines that contain `string_to_show`. ''' Lines_to_show = set() def handle(m): Lines_to_show.add(editor.lineFromPosition(m.start())) # Loop over whole file, calling `handle` for each find: editor.search(string_to_show, handle) # Sort numbers of lines containing `string_to_show`: Lines_to_show_sorted = sorted(list(Lines_to_show)) # Hide all lines (twice because of bug in Scintilla): editor.hideLines(0, int(editor.getLineCount())-1 ) editor.hideLines(0, int(editor.getLineCount())-1 ) # Actual showing of lines to be shown: for line_shown in Lines_to_show_sorted: editor.showLines(line_shown, line_shown) def tab_callback(args): ''' Called after return from other tab. ''' global save_bufferID, display_all, string_to_show if save_bufferID == args['bufferID'] and not display_all: disp_only_lines(string_to_show) #### Main #### save_bufferID = notepad.getCurrentBufferID() notepad.clearCallbacks(tab_callback) notepad.callback(tab_callback, [NOTIFICATION.BUFFERACTIVATED]) # Role of boolean `display_all`: # if true all lines are displayed, else some lines are hidden. try: if display_all: string_to_show = editor.getSelText() editor.setEmptySelection(editor.getCurrentPos()) # Undo selection except NameError: # First time in, `display_all` does not exist display_all = True string_to_show = editor.getSelText() editor.setEmptySelection(editor.getCurrentPos()) # Undo selection if len(string_to_show) == 0 and display_all: notepad.messageBox('No string selected', 'all.py') else: if display_all: disp_only_lines(string_to_show) display_all = False # Not all lines are displayed else: editor.showLines(0, int(editor.getLineCount())-1 ) display_all = True # All lines are displayed string_to_show = ''
-