Community
    • Login

    How do I move bookmarks to a copy of the same file?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    20 Posts 5 Posters 2.0k 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.
    • S. MerkS
      S. Merk @Alan Kilborn
      last edited by

      @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)

      1 Reply Last reply Reply Quote 0
      • Paul WormerP
        Paul Wormer @Alan Kilborn
        last edited by

        @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:

        1. “Mark All” of selected string with bookmarks on.
        2. Invert the bookmarks.
        3. 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.

        Alan KilbornA Paul WormerP 3 Replies Last reply Reply Quote 1
        • Alan KilbornA
          Alan Kilborn @Paul Wormer
          last edited by Alan Kilborn

          @Paul-Wormer

          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.

          1 Reply Last reply Reply Quote 1
          • S. MerkS
            S. Merk @Alan Kilborn
            last edited by S. Merk

            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()
            
            Alan KilbornA 1 Reply Last reply Reply Quote 3
            • Alan KilbornA
              Alan Kilborn @S. Merk
              last edited by

              @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??

              S. MerkS 1 Reply Last reply Reply Quote 0
              • S. MerkS
                S. Merk @Alan Kilborn
                last edited by

                @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 KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @S. Merk
                  last edited by

                  @S-Merk

                  It is an “open issue” for the PythonScript plugin to provide such standard dialog support, see HERE.

                  Quite a while ago, I took some advice in the comments of that issue, and integrated “EasyDialogs” code into the things that I do.

                  S. MerkS 1 Reply Last reply Reply Quote 2
                  • S. MerkS
                    S. Merk @Alan Kilborn
                    last edited by

                    @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!

                    S. MerkS 1 Reply Last reply Reply Quote 0
                    • S. MerkS
                      S. Merk @S. Merk
                      last edited by S. Merk

                      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()	
                      
                      Alan KilbornA 1 Reply Last reply Reply Quote 2
                      • Alan KilbornA Alan Kilborn referenced this topic on
                      • Alan KilbornA
                        Alan Kilborn @S. Merk
                        last edited by

                        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.

                        1 Reply Last reply Reply Quote 2
                        • Alan KilbornA Alan Kilborn referenced this topic on
                        • Alan KilbornA
                          Alan Kilborn @Paul Wormer
                          last edited by Alan Kilborn

                          @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.

                          1 Reply Last reply Reply Quote 0
                          • Paul WormerP
                            Paul Wormer @Paul Wormer
                            last edited by

                            @Paul-Wormer
                            I prepared a pedestrian script that mimicks the all 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 routine hideLines is circumvented by invoking it twice. [During my writing of the script, it indeed happened several times that not all lines were hidden after issuing editor.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 = ''
                            
                            
                            1 Reply Last reply Reply Quote 1
                            • Paul WormerP Paul Wormer referenced this topic on
                            • First post
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors