Jump to next/previous word by whitespace only

Jump to next/previous word by whitespace only

44
Basic UserBasic User
44

    Feb 12, 2014#1

    I am trying to create two macros that will jump to next/previous word but only using whitespace as delimiters

    A use case for this is that I should be able to use the macro three times to get from one end of this snippet to the other (so it ignores repeated newlines and skips non alphanumeric characters too.

    Code: Select all

    z.z,z;z:z'z"z_z-z!z@z#z$z%z^z&z*z(z)z 
    z.z,z;z:z'z"z_z-z!z@z#z$z%z^z&z*z(z)z 
    
    z.z,z;z:z'z"z_z-z!z@z#z$z%z^z&z*z(z)z
    Here is the "jump to previous" macro that I have so far.

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    PerlReOn
    Find RegExp Up Select "[^\s][\s]"
    Key LEFT ARROW
    But it seems to fail at the end of paragraphs (testing in a DOS 1252 (ANSI - Latin 1) file), always leaving me back at the end of the same line. This may be because I am misunderstanding how \r\n works?

    Two other questions:

    1) I am trying to write this because control+left arrow/right arrow treats blocks of non alphanumeric characters as delimiters. Is there a macro command to change what UltraEdit treats as delimiters for this purpose?

    2) I find that I need the "Key LEFT ARROW" command at the end to make sure the cursor is in the right point.. but that also means I can't use the macro to select text. Any thoughts on how I could accomplish this?
    UltraEdit - licensed for life!

    http://robertmarkbramprogrammer.blogspot.com

    6,681583
    Grand MasterGrand Master
    6,681583

      Feb 12, 2014#2

      The following topics should help you:
      Please note that for additionally selecting you need further macros which select on caret move.
      Best regards from an UC/UE/UES for Windows user from Austria

      44
      Basic UserBasic User
      44

        Feb 12, 2014#3

        Hi Mofi,
        Mofi wrote:The following topics should help you:
        Please note that for additionally selecting you need further macros which select on caret move.
        I reviewed all three posts, but am not sure how they help. This isn't something I want to change universally, which is why I am attempting to create it as a macro. I suspect my issue is with the regex I am attempting to use.
        Mofi wrote:Please note that for additionally selecting you need further macros which select on caret move.
        Understood.
        UltraEdit - licensed for life!

        http://robertmarkbramprogrammer.blogspot.com

        6,681583
        Grand MasterGrand Master
        6,681583

          Feb 13, 2014#4

          As the existing macros demonstrate, moving caret to next previous string not containing a whitespace character works only if the character at current position is taken into account before running the finds.


          This first macro is like Ctrl+Right Arrow with the difference that it treats every non whitespace character as word character and not just letters, digits and underscores.

          Macro NextNonWhiteChar

          Code: Select all

          PerlReOn
          IfCharGt 32
          Find MatchCase RegExp "\s+"
          IfNotFound
          Bottom
          ExitMacro
          EndIf
          EndIf
          Find MatchCase RegExp "\S"
          IfFound
          Key LEFT ARROW
          Else
          Bottom
          EndIf
          Explanation for above macro:

          If caret is currently positioned on a character which has a code value greater than 32 (a space character) and is therefore not a control character or whitespace character (with the exception of non breaking space and some other rare whitespaces), find downwards 1 or more whitespace characters. If not found up to end of file, move caret to end of file.

          Then search downwards for next non whitespace character and move caret back 1 character if indeed found. Otherwise move caret to end of file.


          The second macro is (nearly) like Ctrl+Left Arrow with the difference that it treats every non whitespace character as word character and not just letters, digits and underscores.

          Moving caret upwards to first character after a whitespace character is more difficult as it must be distinguished if the caret is currently already on first character after a whitespace character or anywhere inside a string of non whitespace characters.

          Macro PrevNonWhiteChar

          Code: Select all

          PerlReOn
          IfCharGt 32
          Key LEFT ARROW
          IfCharGt 32
          Find MatchCase RegExp Up "\s"
          IfNotFound
          Top
          ExitMacro
          EndIf
          Find MatchCase RegExp "\S"
          Key LEFT ARROW
          ExitMacro
          EndIf
          EndIf
          Find MatchCase RegExp Up "\S"
          IfNotFound
          Top
          ExitMacro
          EndIf
          Find MatchCase RegExp Up "\s"
          IfNotFound
          Top
          ExitMacro
          EndIf
          Find MatchCase RegExp "\S"
          Key LEFT ARROW
          Explanation for above macro:

          If caret is currently positioned on a character which has a code value greater than 32 (a space character) and is therefore not a control character or whitespace character (with the exception of non breaking space and some other rare whitespaces), move character one character to the left.

          If the character to left of original position is not a whitespace (with the exception of non breaking space and some other rare whitespaces), the caret was originally within a string of non whitespace characters and must be therefore moved upwards to the first character of this string.

          Please note that this does not work on soft wrapped lines as when caret is currently positioned on first word of a soft wrapped line moving caret one character to left results in moving the caret to end of the soft wrapped line above but is positioned there still one same character as before at beginning of the soft wrapped line below. I have currently no solution for this special problem.

          If caret was originally positioned on a whitespace character or first non whitespace character after a whitespace character, a search upwards for a non whitespace character is processed to move caret to last non whitespace character of previous string.

          Then a search upwards for a whitespace character is processed to move caret to the position before beginning of this non whitespace string. With a final search downwards for a non whitespace character and moving character back the caret is finally positioned on first non whitespace character after a whitespace character.
          Best regards from an UC/UE/UES for Windows user from Austria

          44
          Basic UserBasic User
          44

            Feb 14, 2014#5

            Wow, thanks Mofi. It is very interesting that this is not as simple as the regex I was attempting to use.

            Have you noticed that PrevNonWhiteChar is significantly slower than NextNonWhiteChar? PrevNonWhiteChar will often take a a second or more just to traverse one word to the previous word.
            UltraEdit - licensed for life!

            http://robertmarkbramprogrammer.blogspot.com

            6,681583
            Grand MasterGrand Master
            6,681583

              Feb 14, 2014#6

              StaticGhost wrote:Have you noticed that PrevNonWhiteChar is significantly slower than NextNonWhiteChar?
              Sure, as it is more difficult. The extra check where within a string of non whitespace characters the caret is currently positioned takes a little more time. And there are more finds to execute. Well, the most reason why a human can even see what happens is the GUI of latest UltraEdit. All those customizations on GUI takes a lot of time on display update. For example with UE v14.20 the macros are executed so fast that nobody can see what happens. But many of the user community wanted a stylish and fully customizable GUI and now they have it with the disadvantage that macros and scripts running lots of commands very quickly, much quicker than a human can execute them, and which result in many display updates, are much slower now in latest UE than in older versions of UltraEdit.
              Best regards from an UC/UE/UES for Windows user from Austria