• Login
Community
  • Login

improve SCI_LINEDELETE shortcut

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
27 Posts 4 Posters 2.8k 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.
  • V
    Victorel Petrovich
    last edited by Victorel Petrovich Aug 20, 2023, 4:50 AM Aug 20, 2023, 4:49 AM

    I hope that those who think I’m spamming here, have unsubscribed from the topic :) .

    Next “works” for all kinds of selections (column, not column, mixed…)… BUT … has a little glitch at Ist line in doc for the case of multiple non-column selections: won’t remove EOL for that particular case

    def main():
        editor.beginUndoAction()
    
        if editor.selectionIsRectangle(): 
            p1=editor.getSelectionStart()
            p2=editor.getSelectionEnd()
            editor.deleteRange(p1,p2-p1)
            editor.lineDelete()    
        else:
            editor.deleteBackNotLine()
            editor.delLineLeft()
            editor.delLineRight()
            if editor.getCurrentPos()!=0: 
                editor.deleteBack()
                editor.charRight()
            else:
                editor.clear()
                # compromise: makes it work for single selection, at expense of multi-selections
                
        editor.endUndoAction()
    
    main()
    

    Okay, probably can’t do better without looping over selections…

    A 1 Reply Last reply Aug 20, 2023, 10:32 AM Reply Quote 0
    • A
      Alan Kilborn @Victorel Petrovich
      last edited by Aug 20, 2023, 10:32 AM

      @Victorel-Petrovich

      No one thinks you are spamming…

      Are you just practicing your scripting skills in case Scintilla doesn’t address your concern about SCI_LINEDELETE?

      Ref: https://sourceforge.net/p/scintilla/feature-requests/1489/
      Ref: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/13921

      V 1 Reply Last reply Aug 20, 2023, 1:34 PM Reply Quote 1
      • V
        Victorel Petrovich @Alan Kilborn
        last edited by Victorel Petrovich Aug 20, 2023, 2:58 PM Aug 20, 2023, 1:34 PM

        @Alan-Kilborn said in improve SCI_LINEDELETE shortcut:

        No one thinks you are spamming…

        Nice to know.

        Are you just practicing your scripting skills in case Scintilla doesn’t address your concern about SCI_LINEDELETE?

        That’s right; and who knows how many months till that change will be accepted?
        Also, it’s a good general practice, and … a fun challenge :-).

        Hope will be instructive for others as well happening on this thread.

        For example, the glitch in my last script:
        at first seemed insurmountable, editor.clear() (which is just “delete” function) just doesn’t want to work with several carets
        on EOL…
        But this morning I thought: what would I do if on keyboard, my “delete” key didn’t work (say, temporarily) ?
        Little easy puzzle … and the glitch is gone :) :

        def main():
            editor.beginUndoAction()
        
            if editor.selectionIsRectangle(): 
                p1=editor.getSelectionStart()
                p2=editor.getSelectionEnd()
                editor.deleteRange(p1,p2-p1)
                editor.lineDelete()    
            else:
                editor.deleteBackNotLine()
                editor.delLineLeft()
                editor.delLineRight()
                editor.charRight()
                editor.deleteBack()
                    
            editor.endUndoAction()
        
        main()
        
        A 1 Reply Last reply Aug 20, 2023, 3:17 PM Reply Quote 0
        • A
          Alan Kilborn @Victorel Petrovich
          last edited by Aug 20, 2023, 3:17 PM

          @Victorel-Petrovich said in improve SCI_LINEDELETE shortcut:

          and who knows how many months till that change will be accepted?

          I don’t see (at that Scintilla issue) that you submitted the patch? Hard to be accepted if nothing is submitted. :-)

          1 Reply Last reply Reply Quote 0
          • A
            Alan Kilborn
            last edited by Aug 21, 2023, 3:34 PM

            If I were updating my original LineDelete.py script to handle all types of selections that could be active when the user invokes the script, maybe this is how I’d do it:

            # -*- coding: utf-8 -*-
            from __future__ import print_function
            
            # references:
            #  https://community.notepad-plus-plus.org/topic/23096/improve-sci_linedelete-shortcut
            
            from Npp import *
            
            #-------------------------------------------------------------------------------
            
            class LD(object):
            
                def __init__(self):
                    line_range_tup_list = []
                    for sel_num in range(editor.getSelections()):
                        sel_start_pos = editor.getSelectionNStart(sel_num)
                        sel_end_pos = editor.getSelectionNEnd(sel_num)
                        sel_line_start = editor.lineFromPosition(sel_start_pos)
                        sel_line_end = editor.lineFromPosition(sel_end_pos)
                        if sel_start_pos != sel_end_pos and sel_end_pos == editor.positionFromLine(sel_line_end):
                            # adjust for case where there is some selected text and caret is on a line by itself, i.e., no actual selected text on line of caret:
                            sel_line_end -= 1
                        line_range_tup_list.append( (sel_line_start, sel_line_end) )
                    editor.beginUndoAction()
                    # delete lines from bottom of doc towards the top, to avoid having to constantly adjust for previously deleted lines:
                    for __ in sorted(list(self.consolidate_tup_list(line_range_tup_list)), reverse=True):
                        self.del_line_range(*__)
                    editor.endUndoAction()
                    # leave single caret:
                    __ = editor.getCurrentPos()
                    editor.setSel(__, __)
                    editor.chooseCaretX()
            
                def del_line_range(self, start_line_to_del, end_line_to_del):
                    #print('start_line_to_del:', start_line_to_del+1, 'end_line_to_del:', end_line_to_del+1)
                    start_pos_to_del = editor.positionFromLine(start_line_to_del)
                    end_pos_to_del = editor.positionFromLine(end_line_to_del) + editor.lineLength(end_line_to_del)
                    #print('start_pos_to_del:', start_pos_to_del+1, 'end_pos_to_del:', end_pos_to_del+1)
                    editor.deleteRange(start_pos_to_del, end_pos_to_del - start_pos_to_del)
            
                def consolidate_tup_list(self, tup_list):
                    # inspired by https://stackoverflow.com/questions/5679638/merging-a-list-of-time-range-tuples-that-have-overlapping-time-ranges
                    tup_list = sorted(tup_list)
                    held_list = list(tup_list[0])
                    for (start, end) in tup_list[1:]:
                        if held_list[1] <= start <= held_list[1] + 1:
                            held_list[1] = end
                        else:
                            yield tuple(held_list)
                            held_list[0] = start
                            held_list[1] = end
                    yield tuple(held_list)
            
            #-------------------------------------------------------------------------------
            
            if __name__ == '__main__': LD()
            
            V 1 Reply Last reply Aug 23, 2023, 2:25 AM Reply Quote 0
            • A Alan Kilborn referenced this topic on Aug 21, 2023, 3:34 PM
            • V
              Victorel Petrovich @Alan Kilborn
              last edited by Victorel Petrovich Aug 23, 2023, 4:07 AM Aug 23, 2023, 2:25 AM

              That’s advanced; I’ll look into it.

              Meanwhile, I’ve also been working on a version with looping over selections.

              """
              This does not work correctly if some selections share one line (examples: 2 selections or just 2 carets on same line; a selection begins/ends on same line that  another ends/begins); 
              In such cases, the program will delete more lines than necessary. 
              To solve this, I think would need more than one loop. 
              """
              def main():
                  editor.beginUndoAction()
                  
                  selN=editor.getSelections()-1
                  while selN>-1:
                      p1=editor.getSelectionNStart(selN)
                      p2=editor.getSelectionNEnd(selN)
                      editor.deleteRange(p1,p2-p1)
                      editor.lineDelete() 
                      editor.dropSelectionN(selN)
                      selN-=1
                      
                  editor.endUndoAction()
              main()
              
              A 1 Reply Last reply Aug 23, 2023, 11:30 AM Reply Quote 0
              • A
                Alan Kilborn @Victorel Petrovich
                last edited by Aug 23, 2023, 11:30 AM

                @Victorel-Petrovich said in improve SCI_LINEDELETE shortcut:

                That’s advanced

                :-)

                V 1 Reply Last reply Aug 23, 2023, 2:48 PM Reply Quote 0
                • V
                  Victorel Petrovich @Alan Kilborn
                  last edited by Victorel Petrovich Aug 23, 2023, 3:00 PM Aug 23, 2023, 2:48 PM

                  Who doesn’t like a bit of praise :)
                  From my tests of your script, in following cases:

                  1. put caret on start of a line, press shift+Down one or more times; the script doesn’t delete the line where the caret end
                  2. Similarly for the line where anchor is at column0, when press shift+Up 1+ times.

                  It appears you did so on purpose with these lines:

                  if sel_start_pos != sel_end_pos and sel_end_pos == editor.positionFromLine(sel_line_end):
                      # adjust for case where there is some selected text and caret is on a line by itself, i.e., no actual selected text on line of caret:
                      sel_line_end -= 1
                  

                  Although, I agree it’s debatable whether that line should be deleted in case 2. above : in that case, it doesn’t look like the second line has been touched by selection at all. It looks almost same as when fully select first line from End to Start:
                  87ef58e1-7e93-42a6-beac-06bdc8b8ea8a-Capture.PNG

                  On the other hand, in case 1. above, it is clear that the line has been marked:
                  30d5e542-87a9-4cf7-8ee7-69b6f7e1e2d1-Capture.PNG

                  So, perhaps the ideal rule would be:
                  Delete all lines either with selection within or where caret resides.

                  But, for patch to Scintilla, I’ll probably have to delete the line in both cases, for consistency with SCI_LINECOPY and SCI_LINECUT.

                  A 2 Replies Last reply Aug 23, 2023, 3:32 PM Reply Quote 0
                  • A
                    Alan Kilborn @Victorel Petrovich
                    last edited by Aug 23, 2023, 3:32 PM

                    @Victorel-Petrovich said in improve SCI_LINEDELETE shortcut:

                    It appears you did so on purpose with these lines

                    Indeed. If I can see a selection (in “inverse video”), then only likes where I can see that inverse video are the ones I’d want to be deleted.

                    That’s my preference, but it is also the way a lot of Notepad++ line operations work.

                    V 1 Reply Last reply Aug 23, 2023, 4:00 PM Reply Quote 0
                    • V
                      Victorel Petrovich @Alan Kilborn
                      last edited by Victorel Petrovich Aug 23, 2023, 4:12 PM Aug 23, 2023, 4:00 PM

                      Well, but since caret by itself can be accepted as marker of line, then it also makes sense to accept it after one(or more)fully selected lines. (second pic above).

                      Which “inverse video”?

                      EDIT: it’s not about your script per see (anymore). Just exchanging opinions. Maybe one day I’ll suggest this to Scintilla about all of SCI_ COPY/CUT/DELETE.

                      A 1 Reply Last reply Aug 23, 2023, 4:13 PM Reply Quote 0
                      • A
                        Alan Kilborn @Victorel Petrovich
                        last edited by Aug 23, 2023, 4:13 PM

                        @Victorel-Petrovich said in improve SCI_LINEDELETE shortcut:

                        but since caret by itself can be accepted as marker of line, then it also makes sense to accept it after one(or more)fully selected lines

                        For you maybe; not for me. And, as I said, not for a lot of of Notepad++ functions that work for lines in selection.

                        One of the virtues of scripts; everyone can easily have what they want, by tweaking the code.

                        The “caret by itself” situation is handled to delete its line. Just not “caret by itself on an otherwise non-selected line”.

                        inverse video

                        Just a quick way of saying what the text that is selected looks like.

                        V 1 Reply Last reply Aug 23, 2023, 4:26 PM Reply Quote 0
                        • A
                          Alan Kilborn @Victorel Petrovich
                          last edited by Alan Kilborn Aug 23, 2023, 4:16 PM Aug 23, 2023, 4:16 PM

                          @Victorel-Petrovich said in improve SCI_LINEDELETE shortcut:

                          for patch to Scintilla, I’ll probably have to delete the line in both cases, for consistency with SCI_LINECOPY and SCI_LINECUT.

                          Please tell me you aren’t prototyping a Scintilla patch using these scripts you’ve submitted in this thread. Such a patch should be much simpler than that.

                          1 Reply Last reply Reply Quote 0
                          • V
                            Victorel Petrovich @Alan Kilborn
                            last edited by Victorel Petrovich Aug 23, 2023, 4:28 PM Aug 23, 2023, 4:26 PM

                            @Alan-Kilborn said in improve SCI_LINEDELETE shortcut:

                            The “caret by itself” situation is handled to delete its line. Just not “caret by itself on an otherwise non-selected line”.

                            I prefer less exceptions; but to each his own.
                            Indeed, scripting solves the differences.

                            @Alan-Kilborn said in improve SCI_LINEDELETE shortcut:

                            Please tell me you aren’t prototyping a Scintilla patch using these scripts you’ve submitted in this thread. Such a patch should be much simpler than that.

                            I know.

                            1 Reply Last reply Reply Quote 1
                            24 out of 27
                            • First post
                              24/27
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors