Macro to Reformat a Paragraph

Macro to Reformat a Paragraph

3
NewbieNewbie
3

    Dec 04, 2005#1

    Hello,

    The menu command for Reformat Paragraph is not
    available for use in Macros. I would like to be able to
    reformat a pure text paragraph (one with hard returns,
    not word wrapped) from one right margin setting to a
    different right margin setting.

    For instance, I have the following paragraph with a
    right margin setting at column 65,

    EXAMPLE WITH HARD RETURNS AT COL 65
    ============================================================
    Don't you know what that is? It's spring fever. That is what the
    name of it is. And when you've got it, you want-oh, you don't
    quite know what it is you DO want, but it just fairly makes your
    ============================================================

    and I would like to use a macro to be able to change it
    to a different right margin setting (one with hard
    returns, not word wrapped), say at column 55 like this,

    EXAMPLE WITH HARD RETURNS AT COL 55
    =======================================================
    Don't you know what that is? It's spring fever. That is
    what the name of it is. And when you've got it, you
    want-oh, you don't quite know what it is you DO want,
    but it just fairly makes your
    =======================================================

    How do I do that simply using a macro?

    Thank you,

    The tom Guy

    6,675585
    Grand MasterGrand Master
    6,675585

      Dec 04, 2005#2

      This macro does it, but it is not simple. I have added some comments with green color. Remove these lines. Don't forget to activate macro property Continue if a Find with Replace not Found. This macro is only for files with DOS line terminations!

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      // Set cursor to begin of current paragraph which could be also first paragraph at top of the file.
      Find RegExp Up "^p^p[~^p]"
      IfFound
      EndSelect
      Key HOME
      Else
      Top
      Key RIGHT ARROW
      Key HOME
      EndIf
      EndIf
      // Now select whole paragraph even if it is the last paragraph of the file and cut it to clipboard 9.
      Clipboard 9
      StartSelect
      Find RegExp Select "^p^p"
      IfSel
      Key UP ARROW
      Cut
      Else
      SelectToBottom
      Cut
      EndIf
      // Paste the paragraph into a new file, delete trailing spaces and make sure, the last line ends with a line termination.
      NewFile
      Paste
      TrimTrailingSpaces
      IfColNum 1
      Else
      "
      "
      EndIf
      Top
      Find RegExp "%>+ ++"
      Replace All ""

      // Simply delete all line terminations followed by a space.
      Find "^p "
      Replace All " "
      // Replace all other line terminations with a single space. Now all lines are joined together to a single line.
      Find "^p"
      Replace All " "
      // Break all strings with more than 54 characters without a space into parts with a maximum of 54 characters. Looks not very beautiful, but without it the line splitting below must be done with an additional macro instead of a simple find up for a space.
      Attention: I have split the long find string into 4 parts to avoid problems with the html view here in the forum. Join it together before paste it into the macro editor.
      Loop
      Find RegExp "[~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ]
      [~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ]
      [~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ]
      [~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ][~ ]"
      IfFound
      EndSelect
      Key LEFT ARROW
      Key RIGHT ARROW
      " "
      Else
      ExitLoop
      EndIf
      EndLoop
      Key HOME
      // Now search in a loop for spaces. If the space found is at a column greater than 55 (cursor right to space is at column 56), find up previous space and replace it by a line termination. The loops end if not space is found anymore.
      Loop
      Find " "
      IfNotFound
      ExitLoop
      Else
      // IfColNumGt does not work with a selection. So the space must be unselected.
      EndSelect
      Key LEFT ARROW
      Key RIGHT ARROW
      IfColNumGt 56
      Key LEFT ARROW
      Find Up " "
      Replace "^p"
      Key RIGHT ARROW
      EndIf
      EndIf
      EndLoop
      // Now the end of the temporary file is reached. Check if the last space was converted to a line termination or not. If not, do it now. That's the reason, why the macro has maked sure, that the last line of the temporary file ends with a line termination. The paragraph is now always correct terminated with a CRLF.
      IfColNum 1
      Else
      Key LEFT ARROW
      IfCharIs 32
      Key DEL
      "
      "
      EndIf
      // Delete once again trailing spaces, if they are now ones. Than select reformated paragraph, copy it to clipboard, delete temporary file and paste it into original source, where it was cutted before.
      TrimTrailingSpaces
      Top
      Find RegExp "%^([~^p]^)"
      Replace All "> ^1"

      SelectAll
      Copy
      CloseFile NoSave
      Paste
      ClearClipboard
      Clipboard 0
      UnixReOn

      Remove the last red command, if you use regular expression in UltraEdit style by default instead of Unix style.
      For UltraEdit v11.10c and lower see Advanced - Configuration - Find - Unix style Regular Expressions.
      For UltraEdit v11.20 and higher see Advanced - Configuration - Searching - Unix style Regular Expressions.
      Macro commands UnixReOn/UnixReOff modifies this setting.


      2005-12-05: I added in blue the code necessary for deleting existing "> " (and variants like ">> ", ">") at begin of each line and add it again after reformatting the paragraph.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Dec 05, 2005#3

        Guao, Mofi, you are definitely THE Jedi Warrior of Uedit macros! I am astounded by the complexity required to solve what would appear to be a simple problem on the surface.

        I wonder if there might be a simpler "hack" to do this, such as exporting the paragraph to a working file that had a dummy file extension with a word wrap definition of the shorter word wrap length and then re-importing the paragraph back into the primary desired file? I've been trying to do this to see if I could make it work, but have not yet been successful.

        But, Jesus, there MUST be an easier way to do this, no? Golly, it's just a matter of changing the paragraph word wrap format for pitty sakes! And it is sooooo easy to do manually with the pull down menus, such as I just did manually from the last paragraph to this one.

        If Uedit included a macro command equivalent to the menu command, "Reformat Paragraph", there would not even be an issue. Is there a technical issue why that command is not included as a choice on the macro command list? As you are one of he beta testers, I'm thinking that would be a good suggestion, no?

        Anyway, de todos modos, a miilion thanks for that macro and your solution to the problem. If you should think of, or come across, a simpler way, please let me know.

        Actually, all I'm trying to do is add the quote mark in front of imported text from an email so that when I export it back to my email client it will stay wrapped with the quote marks in front like below, i.e., change the word wrap from 65 or 60 to 58 to allow for adding the quote mark and space in front of each line.

        > Actually, all I'm trying to do is add the quote mark in
        > front of imported text from an email so that when I export
        > it back to my email client it will stay wrapped with the
        > quote marks in front like this,

        Perhaps, there is a work around to achieve my final result easier than the generalized problem that I initially proposed?
        And ideas, O Jedi Warrior of Uedit Macros,

        Thanks again, Mofi. You're super, a real Thomas Anderson (Neo),

        Tom

          Dec 06, 2005#4

          Hi Again Mofi.

          I think I can make that macro work for me. I don't need to
          worry about having a string greater than 54 chars, so I
          can remove that part.

          Remember that all I'm trying to do is add the email quote
          sign to an unquoted paragraph.

          I have this:

          HARD RETURNS AT COL 65
          ============================================================
          Don't you know what that is? It's spring fever. That is what the
          name of it is. And when you've got it, you want-oh, you don't
          quite know what it is you DO want, but it just fairly makes your
          ============================================================

          and I'm trying to make a single paragraph look like this,

          HARD RETURNS AT COL 55
          =======================================================
          > Actually, all I'm trying to do is add the quote mark
          > in front of imported text from an email so that when
          > I export it back to my email client it will stay
          > wrapped with the quote marks in front like this,
          =======================================================

          I think I can make your macro work, but I'm stuck because
          your macro relies on using an "IfColNumGt" statement, and
          now I need to fez up to you that I am using Uedit Ver 9.20
          that doesn't include that command.

          Do you have a suggested work around for that?

          Thanks again,

          Tom