Macro to select next/previous block of consecutive non-blank lines

Macro to select next/previous block of consecutive non-blank lines

44
Basic UserBasic User
44

    Feb 19, 2019#1

    I have been having a go at creating a macro that will select the next/previous block of consecutive non-blank lines (working in a similar way for UNIX/DOS files).

    This is what I have so far:

    Select down

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    PerlReOn
    StartSelect
    Find RegExp Select "(\r\n|\n)*..*(?=(\r\n\r\n|\n\n))"
    
    Select up

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    PerlReOn
    StartSelect
    Find RegExp Up Select "(\r\n|\n)*..*(?=(\r\n\r\n|\n\n))"
    
    The select down macro seems to work as I want, selecting to the last piece of text just before the first instance of LINE-ENDING+LINE-ENDING; the select up isn't working as expected, selecting past LINE-ENDING+LINE-ENDING and to the next line.

    I realize this is is a regular expression issue, not necessarily a macro issue, but wanted to see if anyone had another way to achieve this with a macro?

    Rob
    :)
    UltraEdit - licensed for life!

    http://robertmarkbramprogrammer.blogspot.com

    6,686585
    Grand MasterGrand Master
    6,686585

      Feb 19, 2019#2

      I suggest as macro to select next paragraph:

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      PerlReOn
      IfColNum 1
      IfCharIs 13
      Find MatchCase RegExp "(?s)(?:\r?\n)*\K.+?(?:\r?\n(?=\r?\n)|\z|\r?\n\z)"
      ExitMacro
      EndIf
      IfCharIs 10
      Find MatchCase RegExp "(?s)(?:\r?\n)*\K.+?(?:\r?\n(?=\r?\n)|\z|\r?\n\z)"
      ExitMacro
      EndIf
      EndIf
      Find MatchCase RegExp "(?s)(?:\r?\n){2,}\K.+?(?:\r?\n(?=\r?\n)|\z|\r?\n\z)"
      
      I suggest as macro to select previous or current paragraph in case of caret is inside a paragraph:

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      PerlReOn
      Find MatchCase RegExp Up "(?:\r?\n){2,}(?=[^\r\n])\K|\A"
      Find MatchCase RegExp "(?s).+?(?:\r?\n(?=\r?\n)|\z|\r?\n\z)"
      
      I tested both macros with UltraEdit v22.20.0.49 on Windows XP and UltraEdit v25.20.0.166 on Windows 7.

      Please note that \A is interpreted not as beginning of character stream buffer as defined by Boost Perl regular expression library, but as beginning of file as most user expect, since UltraEdit for Windows v24.00 and UEStudio v17.00. \z is also interpreted since UltraEdit for Windows v24.00 and UEStudio v17.00 as end of file and not anymore as end of character stream buffer as defined by the Boost Perl regular expression library. I have not tested the two macros on a very large file on the use cases for selecting last and first paragraph.
      Best regards from an UC/UE/UES for Windows user from Austria

      44
      Basic UserBasic User
      44

        Feb 19, 2019#3

        Thanks for that Mofi, that's marvelous! I made a couple of adjustments to suit what I am trying to achieve: If I am within a block, just select from current position to start/end of that block; plus keep the selection active so that multiple invocations of the same macro will continue to extend the selection in either direction.

        Select next block:

        Code: Select all

        InsertMode
        ColumnModeOff
        HexOff
        PerlReOn
        IfColNum 1
        IfCharIs 13
        StartSelect
        Find MatchCase RegExp Select "(?s)(?:\r?\n)*\K.+?(?:\r?\n(?=\r?\n)|\z|\r?\n\z)"
        ExitMacro
        EndIf
        IfCharIs 10
        StartSelect
        Find MatchCase RegExp Select "(?s)(?:\r?\n)*\K.+?(?:\r?\n(?=\r?\n)|\z|\r?\n\z)"
        ExitMacro
        EndIf
        EndIf
        StartSelect
        Find MatchCase RegExp Select "(?s)(?:\r?\n){2,}"

        Select previous block:

        Code: Select all

        InsertMode
        ColumnModeOff
        HexOff
        PerlReOn
        StartSelect
        Find MatchCase RegExp Up Select "(?:\r?\n){2,}(?=[^\r\n])\K|\A"
        UltraEdit - licensed for life!

        http://robertmarkbramprogrammer.blogspot.com