Print: Show Spaces/Tabs

Help with writing and playing macros

Print: Show Spaces/Tabs

Postby asjones » Mon Mar 12, 2007 5:27 pm

I sometimes like to have the View Menu option "Show Spaces / Tabs" turned on.

However I have not found a way to print the file with the spaces, tabs, or the hard return characters.

this seemed like something one should be able to do that I am missing ...

Can anyone help?

BTW, I also tried printing in Word and it not print with the special characters.

thanks

Alan
User avatar
asjones
Newbie
 
Posts: 3
Joined: Mon Jul 26, 2004 11:00 pm

Re: Print: Show Spaces/Tabs

Postby Mofi » Tue Mar 13, 2007 7:34 am

Following macro named for example ShowSpacesOn with macro property Continue if a Find with Replace not found checked when creating the macro will convert all spaces, tabs and DOS line terminations to the characters shown when you use View - Show Spaces/Tabs - whole file or only current selection. Then you can print the file or the selection only.

InsertMode
ColumnModeOff
HexOff
IfSel
Find "^t"
Replace All SelectText "»"
Find " "
Replace All SelectText "·"
Find "^p"
Replace All SelectText "¶^p"
Else
Top
Find "^t"
Replace All "»"
Find " "
Replace All "·"
Find "^p"
Replace All "¶^p"
EndIf

After printing the file WITHOUT saving it you can use 3 times CTRL+Z (=Undo) to restore original content or use following macro named for example ShowSpacesOff to convert the file back:

InsertMode
ColumnModeOff
HexOff
IfSel
Find "»"
Replace All SelectText "^t"
Find "·"
Replace All SelectText " "
Find "¶^p"
Replace All SelectText "^p"
Else
Top
Find "»"
Replace All "^t"
Find "·"
Replace All " "
Find "¶^p"
Replace All "^p"
EndIf

This second macro needs Continue if a Find with Replace not found also checked.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Print: Show Spaces/Tabs

Postby scallanh » Tue Mar 13, 2007 5:43 pm

Rather than replacing tabs with just "»" it might help to replace them with "»" followed by one tab (so basically you'd be inserting "»" before every tab). Assuming your tab width is at least 2 that should match the "show spaces/tabs" setting, except for tabs where the next tab stop is only one character away.
User avatar
scallanh
Basic User
Basic User
 
Posts: 31
Joined: Mon Oct 24, 2005 11:00 pm

Re: Print: Show Spaces/Tabs

Postby Mofi » Wed Mar 14, 2007 7:40 am

Scallanh, your idea is very good! Unfortunately I have read it after I have developed following.

My next fast attempt to solve this problem was following macro:

InsertMode
ColumnModeOff
HexOff
UnixReOff
IfSel
Find " "
Replace All SelectText "·"
Find "^p"
Replace All SelectText "¶^p"
TabsToSpaces
Find RegExp " ^( ++^)"
Replace All SelectText "»^1"
Else
Top
Find " "
Replace All "·"
Find "^p"
Replace All "¶^p"
TabsToSpaces
Find RegExp " ^( ++^)"
Replace All "»^1"
EndIf

But this time I have tested it and have seen that the macro has some disadvantages.

  1. TabsToSpaces breaks the Undo chain for the current file.
  2. The macro uses regular expression replaces and so is not independent anymore from the setting for the regular expression engine.
  3. The selection after macro execution is different to before macro execution because of TabsToSpaces. Reselection is required for correct printing.
  4. But most important: multiple tabs are not correct converted because after TabsToSpaces a sequence of tabs is now a sequence of spaces. So only the first space is replaced by the tab character and the converted file does not contain the correct number of tab place holders anymore although the alignment is correct. But an undo is now possible only by using File - Revert to Saved when the file was saved before macro execution.

After lot of thinking how to solve all these problems, here is the final solution which really works.

First the current selection or the whole file is copied to user clipboard 9 because working on the source is not possible for a selection and I don't like to break the undo chain on the source file.

Next the current file name with full path is copied to user clipboard 8 before a new file is created where the copied file name is pasted. The file path is replaced by the path of a temp directory. Please modify this red highlighted part in the macro code to your environment. Unfortunately the environment variable %temp% cannot be used inside a macro for the SaveAs command. The macro inserts also an underscore before the file name for security, if your source file is already in the specified temp directory.

It is important that the new file is saved with the same extension as the source file because the tab stop value(s) can be file extension specific. So next the content of the source file is pasted into the now empty new file and saved with the slightly modified file name in the specified temp directory.

Now the macro really starts by replacing all spaces and DOS line terminations accordingly.

The last part is the loop which was the hard one. It converts tab by tab the tabs of the file to the tab place holder character with correct amount of spaces for correct alignment. Unfortunately a single tab selected is not converted with TabsToSpaces into correct number of spaces according to the tab stop value setting. This is only done correct by UE/UES when at least 1 non white character before and after is also selected. Tabs at start of the line are no problem (at least in my tests).

So at top of the loop a single tab character is searched and after it the character # is inserted in case of the following character is also a tab character.

If no tab character was found, the file is saved and the loop is breaked (= end of macro).

The inserted #, the found tab and the character/word before are now selected and the tab is converted with TabsToSpaces to correct number of spaces.

After this conversion the cursor must be moved before the spaces created by TabsToSpaces and the first space is now replaced by the tab place holder character.

Next everything to the inserted # is selected and all spaces are replace by a non breaking space (ANSI code 160 decimal).

Check with Search - Character Properties with cursor placed in an UltraEdit edit window on the non breaking space between the blue highlighted double quotes, if after copying the code from the browser window to an UE edit window this space character has really decimal value 160 and not 32!

Correct it if necessary with inserting the non breaking space with View - ASCII Table or by temporarily switching to hex edit mode and replacing 20 by A0.

Last the inserted # is removed before the loop continues with the search for the next tab.

After macro execution you can print now the temp file and then delete it with File - Special Functions - Delete Active File.

The macro property Continue if a Find with Replace not found must be checked for this macro (default since UE v13.00 and UES v6.20).

InsertMode
ColumnModeOff
HexOff
IfSel
Else
SelectAll
EndIf
Clipboard 9
Copy
Clipboard 8
CopyFilePath
NewFile
Paste
Find Up "\"
SelectToTop
"C:\Windows\Temp\_"
SelectAll
Cut
Clipboard 9
Paste
ClearClipboard
Clipboard 8
SaveAs "^c"
ClearClipboard
Clipboard 0
Top
Find " "
Replace All "·"
Find "^p"
Replace All "¶^p"
Loop
Find "^t"
Replace "^t#"
IfNotFound
Save
Top
ExitLoop
EndIf
StartSelect
Key LEFT ARROW
Key Ctrl+LEFT ARROW
TabsToSpaces
EndSelect
Key Ctrl+LEFT ARROW
Key Ctrl+LEFT ARROW
Find " "
Replace "»"
StartSelect
Find Select "#"
Find " "
Replace All SelectText " "
EndSelect
Key LEFT ARROW
Delete
EndLoop
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros