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.
- TabsToSpaces breaks the Undo chain for the current file.
- The macro uses regular expression replaces and so is not independent anymore from the setting for the regular expression engine.
- The selection after macro execution is different to before macro execution because of TabsToSpaces. Reselection is required for correct printing.
- 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