News and Reviews > Best Text Editor
A Macro+Tool config for ultraedit for procesing blocks of text externally
(1/1)
mouser:
Here is the combination of MACRO and TOOL that i use to let me select a block of text in a file and process it by an external exe program, capture the output and paste it back replacing the originally selected text.
The key feature of this technique is that it allows you to use an easy undo to undo the change (Ctrl+Z) and preserves your place in the file after the operation.
The sample tool below runs the open source Astyle exe (C++ coder formatter) on the selected block, and is configured to implement whitesmith style c++ indentation (http://en.wikipedia.org/wiki/Indent_style#Whitesmiths_style), which i love.
MyAstyle MACRO
-------------------------
--- ---InsertMode
ColumnModeOff
HexOff
UnixReOff
Copy
NewFile
Paste
SaveAs "C:\Temp\Astyle.tmp"
CloseFile
RunTool "MyAstyle"
Open "C:\Temp\Astyle.tmp"
SelectAll
Copy
CloseFile
ToggleBookmark
Paste
Open "C:\Temp\Astyle.tmp.orig"
SelectAll
Copy
CloseFile
PreviousBookmarkSelect
ToggleBookmark
MyAstyle TOOL Configuration:
-----------------------------
--- ---COMMANDLINE -> D:\Libraries\astyle\astyle.exe --mode=c --indent-brackets --brackets=break --indent=tab --one-line=keep-blocks --one-line=keep-statements C:\Temp\Astyle.tmp
WORKINGDIR -> C:\Temp\
MENUITEMNAME-> MyAstyle
DONT CHECK "CAPTURE OUTPUT" - this has the exe write output to a file.
if your exe outputs to stdout, then you should check "capture output" and modify the macro and remove the lines from the first Open to the subsequent Closefile.
Jussi Jumppanen:
Here is the combination of MACRO and TOOL that i use to let me select a block of text in a file and process it by an external exe program, capture the output and paste it back replacing the originally selected text.-mouser (July 11, 2005, 02:09 PM)
--- End quote ---
Just out of curiosity I whippped up this Zeus Lua macro which performs the same task :)
--- -----
-- Name: Astyle Macro
-- Author: Jussi Jumppanen
-- Language: Lua Macro
-- Description: A simple Lua macro that takes the currently selected
-- text, saves it to the c:\temp\astlye.tmp file, runs it
-- through the astyle.exe utility, then pastes the result
-- back into the cdocument.
--
-- How To Run: Save to style.lua in the zScript directory.
--
-- All Zeus macros can be run using the macro execute menu
-- item or they can be bound to the keyboard or can be
-- attached to the macro drop down or popup menus.
--
function key_macro()
-- macro only works for read/write documents
if is_read_only() == 1 then
message("The current document is marked as read only!")
beep()
return
end
-- macro needs some marked text
if (is_marked() == 0) then
message("To use this macro you need to first mark some text.")
beep()
return 1
end
-- get the current marked text
local marked_text = macro_tag("$M")
-- file that will be created
local file_name = "c:\\temp\\astyle.tmp"
-- open the temp file in binary write mode
local file_out = io.open(file_name, "w+b")
if file_out ~= nil then
-- write the text to file
file_out:write(marked_text)
file_out:close()
-- command line to be run
local command = "astyle.exe --mode=c --indent-brackets --brackets=break --indent=tab --one-line=keep-blocks --one-line=keep-statements " .. file_name
-- command options: wait for tool to finish
local flags = 32
-- run command which should display a DOS window
if system(command , 0, flags) == 0 then
-- open the temp file in text reading mode
local file_in = io.open(file_name, "r+t")
if file_in ~= nil then
local result = ""
-- read in the file
for line in file_in:lines() do
result = result .. line .. "\r\n"
end
-- close the input
file_in:close()
-- add the result to the clipboard
set_clipboard_text(result)
-- paste the result into the document deleting the original text
MarkPaste()
else
-- failed to open file
message_box(1, "Error opening the '" .. file_name .. "' results file.")
end
else
-- failed to run tool
message_box(1, command .. "\nError running program!'")
end
else
-- failed to create file
message_box(1, "Error creating the '" .. file_name .. "' file.")
end
end
key_macro()
Navigation
[0] Message Index
Go to full version