Copy character from line above

Copy character from line above

13
Basic UserBasic User
13

    Mar 09, 2010#1

    In the Semware editor TSE there was a macro which I liked a great deal, which copied the character in the line above the cursor to the insert point.

    The SAL macro code - for the Mofi's of the world who understand SAL - goes like this:

    Code: Select all

    integer proc mCopyCharAbove()
    
    integer c,sv = Set(RemoveTrailingWhite,OFF)
    
    	PushPosition()			/* Save position */
    	c = iif(up(),CurrChar(),-1)	/* Try for char on line above */
    	PopPosition()			/* Back to where we were */
    	Set(RemoveTrailingWhite,sv)
    	return(c >= 0 and InsertText(Chr(c)))
    
    end mCopyCharAbove
    I have this assigned to Alt+Up in TSE like this:

    Code: Select all

    <Alt CursorUp>		mCopyCharAbove()
    It is a much-loved macro with a natural feeling key assignment, which I'd like to port into UE, but before I reinvent the wheel, is there already something for this in UE... and is this better in a macro or script?

    6,681583
    Grand MasterGrand Master
    6,681583

      Mar 09, 2010#2

      I think nobody has yet coded that and published. With a macro you could hold Alt and CursorUp to run it repeated times while when coding it with a script you have to press the keys always again and again because the Cancel dialog can't be suppressed at the moment. But I think it is very difficult to code that with a macro because no support for variables which you surely need here for saving/restoring position in current line and check position in the line above.
      Best regards from an UC/UE/UES for Windows user from Austria

      13
      Basic UserBasic User
      13

        Mar 09, 2010#3

        It hadn't occurred to me that typematic repeat wouldn't work in scripts. Can you invoke a script from a macro?

        6,681583
        Grand MasterGrand Master
        6,681583

          Mar 10, 2010#4

          rstaveley wrote:Can you invoke a script from a macro?
          No, this is not possible.

          However, I think I get the task correct coded with a macro. Here is the macro code for copying it into the edit macro dialog. You should disable both macro properties for this macro.

          This macro worked for me with UE v16.00.0.1025 with configuration setting Allow positioning beyond line end (Configuration - Editor Display - Cursor/Caret) not enabled. The macro modifies the second line if the cursor is currently on the first line of the file. So don't execute the macro on the first line of a file.

          InsertMode
          ColumnModeOff
          HexOff
          IfCharIs 13
          Key UP ARROW
          IfCharIs 13
          Key DOWN ARROW
          ExitMacro
          EndIf
          StartSelect
          Key RIGHT ARROW
          Clipboard 9
          Copy
          EndSelect
          Key DOWN ARROW
          Paste
          ClearClipboard
          Clipboard 0
          ExitMacro
          EndIf
          IfCharIs 10
          Key UP ARROW
          IfCharIs 10
          Key DOWN ARROW
          ExitMacro
          EndIf
          StartSelect
          Key RIGHT ARROW
          Clipboard 9
          Copy
          EndSelect
          Key DOWN ARROW
          Paste
          ClearClipboard
          Clipboard 0
          ExitMacro
          EndIf
          IfEof
          Key UP ARROW
          IfCharIs 13
          Key DOWN ARROW
          ExitMacro
          EndIf
          IfCharIs 10
          Key DOWN ARROW
          ExitMacro
          EndIf
          StartSelect
          Key RIGHT ARROW
          Clipboard 9
          Copy
          EndSelect
          Key DOWN ARROW
          Paste
          ClearClipboard
          Clipboard 0
          ExitMacro
          EndIf
          Key UP ARROW
          IfCharIs 13
          Key DOWN ARROW
          ExitMacro
          EndIf
          IfCharIs 10
          Key DOWN ARROW
          ExitMacro
          EndIf
          StartSelect
          Key RIGHT ARROW
          Clipboard 9
          Copy
          EndSelect
          Key DOWN ARROW
          Key LEFT ARROW
          Paste
          ClearClipboard
          Clipboard 0

          Here is the macro code as UEM (UltraEdit Macro) with comments for a better understanding. See Macro examples and reference for beginners and experts how to work with macro code in *.uem files.

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          // Is the cursor at end of the current line terminated with
          // a carriage return - DOS files or not converted MAC files?
          IfCharIs 13
          // Move cursor up to line above. If the cursor is now at end of this
          // line, nothing can be copied and therefore the cursor is just moved
          // back to previous position.
             Key UP ARROW
             IfCharIs 13
                Key DOWN ARROW
                ExitMacro
             EndIf
          // Otherwise select the character, copy it to clipboard, move cursor
          // down to original line which is automatically placed again at end
          // of that line and simply paste there the copied character.
             StartSelect
             Key RIGHT ARROW
             Clipboard 9
             Copy
             EndSelect
             Key DOWN ARROW
             Paste
             ClearClipboard
             Clipboard 0
             ExitMacro
          EndIf
          // Is the cursor at end of the current line terminated with
          // a line feed - not converted UNIX files? Yes, same code as
          // above with the only difference to check for a line feed.
          IfCharIs 10
             Key UP ARROW
             IfCharIs 10
                Key DOWN ARROW
                ExitMacro
             EndIf
             StartSelect
             Key RIGHT ARROW
             Clipboard 9
             Copy
             EndSelect
             Key DOWN ARROW
             Paste
             ClearClipboard
             Clipboard 0
             ExitMacro
          EndIf
          // Is the cursor at end of the file, use the same code as above with
          // the only difference to check additionally for a carriage return.
          IfEof
             Key UP ARROW
             IfCharIs 13
                Key DOWN ARROW
                ExitMacro
             EndIf
             IfCharIs 10
                Key DOWN ARROW
                ExitMacro
             EndIf
             StartSelect
             Key RIGHT ARROW
             Clipboard 9
             Copy
             EndSelect
             Key DOWN ARROW
             Paste
             ClearClipboard
             Clipboard 0
             ExitMacro
          EndIf
          // The cursor is not at end of the current line or end of file.
          // Move cursor up to line above. If the cursor is now at end of this
          // line, nothing can be copied and therefore the cursor is just moved
          // back to previous position.
          Key UP ARROW
          IfCharIs 13
             Key DOWN ARROW
             ExitMacro
          EndIf
          IfCharIs 10
             Key DOWN ARROW
             ExitMacro
          EndIf
          // Otherwise select the character, copy it to clipboard and move cursor
          // down to original line. Because of selecting the character in the line
          // above the cursor is now one position right the original position. So
          // the cursor must be set left before the copied character can be inserted.
          // This is the important difference in behavior in comparison to the code
          // above when cursor is at end of a line or end of the file.
          StartSelect
          Key RIGHT ARROW
          Clipboard 9
          Copy
          EndSelect
          Key DOWN ARROW
          Key LEFT ARROW
          Paste
          ClearClipboard
          Clipboard 0
          Best regards from an UC/UE/UES for Windows user from Austria

          13
          Basic UserBasic User
          13

            Mar 10, 2010#5

            Wow! You're a star, Mofi. I'm really pleased with this. :)

              Mar 10, 2010#6

              Mofi wrote:I think we will never see enhancements like support for variables for macros because scripts can be used for complex tasks...
              Do you reckon then that being able to suppress the cancel dialog would make scripts suitable for key commands like this in future, or are there other challenges too?

              6,681583
              Grand MasterGrand Master
              6,681583

                Mar 10, 2010#7

                The Cancel dialog always displayed when running a script is the only problem for small scripts designed for repeated executions. Of course small macros are faster executed especially when the macro file containing all the small macros is loaded at startup of UltraEdit into memory. And script interpreting be the Javascript engine is surely also slower than the macro execution, think on reading the text file, check syntax, etc. not done for the compiled macros. But with the caching mechanisms of the operating systems and the fast hardware using also caching nowadays users can't see anymore any difference in speed when executing a small macro in comparison to a small script. So the difference in execution time for small scripts in comparison to small macros can be only measured with the fast processor, but not by a (slow) user.
                Best regards from an UC/UE/UES for Windows user from Austria

                3
                NewbieNewbie
                3

                  Mar 19, 2010#8

                  I have once created a macro for this, which is considering the current editing mode (insert/override) and also handles tabs correctly. Here it is:
                  --------------------------------------------------
                  "CharFromAbove"

                  Inserts/overwrites one character from the line above.
                  Clipboard 9 is used.
                  --------------------------------------------------
                  ColumnModeOff
                  HexOff
                  UnixReOff
                  IfSel
                  IfEof
                  Key END
                  Else
                  Key RIGHT ARROW
                  Key LEFT ARROW
                  EndIf
                  ExitMacro
                  EndIf
                  Key LEFT ARROW
                  Key RIGHT ARROW
                  Key UP ARROW
                  IfCharIs 13
                  Key DOWN ARROW
                  ExitMacro
                  EndIf
                  IfCharIs 10
                  Key DOWN ARROW
                  ExitMacro
                  EndIf
                  Clipboard 9
                  StartSelect
                  Key RIGHT ARROW
                  Copy
                  EndSelect
                  Key LEFT ARROW
                  Key DOWN ARROW
                  " "
                  Key LEFT ARROW
                  Find " "
                  Replace "^c"
                  Clipboard 0
                  ------------------
                  In case you have the file in DOS format internally, then you can remove the "If CharIs 10" block.
                  Have a try.