Community
    • Login

    improve SCI_LINEDELETE shortcut

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    27 Posts 4 Posters 2.7k 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.
    • Alan KilbornA
      Alan Kilborn @Victorel Petrovich
      last edited by

      @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
      • Alan KilbornA
        Alan Kilborn
        last edited by

        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()
        
        Victorel PetrovichV 1 Reply Last reply Reply Quote 0
        • Alan KilbornA Alan Kilborn referenced this topic on
        • Victorel PetrovichV
          Victorel Petrovich @Alan Kilborn
          last edited by Victorel Petrovich

          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()
          
          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Victorel Petrovich
            last edited by

            @Victorel-Petrovich said in improve SCI_LINEDELETE shortcut:

            That’s advanced

            :-)

            Victorel PetrovichV 1 Reply Last reply Reply Quote 0
            • Victorel PetrovichV
              Victorel Petrovich @Alan Kilborn
              last edited by Victorel Petrovich

              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.

              Alan KilbornA 2 Replies Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @Victorel Petrovich
                last edited by

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

                Victorel PetrovichV 1 Reply Last reply Reply Quote 0
                • Victorel PetrovichV
                  Victorel Petrovich @Alan Kilborn
                  last edited by Victorel Petrovich

                  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.

                  Alan KilbornA 1 Reply Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @Victorel Petrovich
                    last edited by

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

                    Victorel PetrovichV 1 Reply Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @Victorel Petrovich
                      last edited by Alan Kilborn

                      @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
                      • Victorel PetrovichV
                        Victorel Petrovich @Alan Kilborn
                        last edited by Victorel Petrovich

                        @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
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors