Macroing documentation

Get help with the installation and running of the Zeus IDE. Please do not post bug reports or feature requests here. When in doubt post your question here.
Post Reply
martinlooker
Posts: 20
Joined: Tue Oct 11, 2005 12:55 pm

Macroing documentation

Post by martinlooker »

Hi I'm trying to find the macroing documentation the help says it should be here:

http://www.zeusedit.com/archives

Specifically I'm looking to do a something like:

SearchReplace (search="^\t", replace="\t\t", RegExp=true, selection=true, All)

Now I can see the functions to set the search and replace strings, can I also set the regexp, replace in selection and replace all flags through macro functions ?
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Hi I'm trying to find the macroing documentation the help says it should be here: http://www.zeusedit.com/archives
This link no longer exists. The macro functions are now documented in the Zeus online help and you can also find lots of example in the zScript directory.
Now I can see the functions to set the search and replace string, can I also set the regexp, replace in selection and replace all flags through macro functions?
What you will need todo is copy the current marked area to a local variable, then do the search and replace using the string functions of the macro language and then paste the text back into the document.

For manipulation of the local variable you can use what ever language you find most comfortable (ie Python, Lua, JavaScrpt, VbScript etc).

As an example this macro: http://www.zeusedit.com/forum/viewtopic.php?t=221 does something similar in that it takes the selected text sends it to Artistic Style for reformating and then put it back into the document.

You just need to replace the Artistic Style step with a simple local variable search and replace function.

Jussi
martinlooker
Posts: 20
Joined: Tue Oct 11, 2005 12:55 pm

Post by martinlooker »

Thanks for your help, again and on the tabbing issue, I'll try out the suggestion above.

I was hoping to do something like the following, (it only just occurred to me to use the keyboard functions to set the sensitivities etc)

SaveSearchOptions
SearchCaseSensitivitySet
SeearchRegExpSet
SearchScopeMarked
SetSearchString
SetReplaceString
>> Couldn't find a replace all function
RestoreSearchOptions

Do you have any plans to extend the built in macroing functions in order to surface paramters that affect that function ?

Breif had the translate ({pattern}, {replacement}, {global}, [regexp], [case], [block], [forward]) function effectively surfacing all the options of replacing for macroing purposes.
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Do you have any plans to extend the built in macroing functions in order to surface parameters that affect that function?
Some sort of ReplaceAll macro function is definitely needed and this feature request has been added to the Zeus todo list.

The SetSearchString, SetSearchString functions already exist in the form of set_find_text and set_replace_text.

Just put the cursor on these words and use the Help, Quick Help menu for more details.

Cheers Jussi
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

The Lua macro shown below is one way of simulating this SearchReplace function.

Jussi

Code: Select all

--
--  Macro Name: SearchReplace.lua
--
-- Description: This macro implements this functionality:
--
--    SearchReplace (search="^\t", replace="\t\t", RegExp=true, selection=true, All) 
--
--       Usage: Mark some text area in a document and run this macro. The 
--              search and replace will be done on the marked area.
--
-- For more details refer to Lua Documentation:
--
--   General Documentation: http://www.lua.org/docs.html
--   Strings Documentation: http://www.lua.org/manual/5.0/manual.html#5.3
--
function SearchReplace(line_text) 

  -- debugging: count = number of replaces made
  -- local result, count = string.gsub(line_text, "^(\t)(.)", "xx:%2")
  -- message_box(1, result, count)

  -- search and replace the line of text replacing a starting tab with two tabs
  local result = string.gsub(line_text, "^(\t)(.)", "\t\t%2")

  -- add a zeus line feed character
  result = result .. "\r"

  -- return the new line
  return result
end

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")

  -- NOTE: The Zeus lines of marked text will terminated by the \r\n 
  --       end of line marker so look for these markers and run the 
  --       search and replace on all lines found.

  -- run the search and replace
  local result = string.gsub(marked_text, "(.-)(\r\n)", SearchReplace)

  -- the result of the search and replace
  -- message_box(1, result, "Debugging")

  -- put add the result to the clipboard
  set_clipboard_text(result)

  -- paste the result back into document deleting the marked text
  MarkPaste()
end

key_macro()
martinlooker
Posts: 20
Joined: Tue Oct 11, 2005 12:55 pm

Post by martinlooker »

Thanks for the above it is my most needed macro!

If I can just be cheeky and ask is there a way to then re-mark the text that is pasted into the document with the MarkPaste function so I can run this macro repeatedly ?
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

There are a set of Zeus macro functions that return the currently marked area and also allow allow the cursor to be moved and the marked area set, so indirectly the answer is yes :)

For more information refer to the example macros in the zScript directory and the Zeus online help,

Cheers Jussi
martinlooker
Posts: 20
Joined: Tue Oct 11, 2005 12:55 pm

Post by martinlooker »

Ha ha well I knew I was being cheeky, I don't normally expect people to write my macros for me, I just thought it would take five minutes to add that versus half an hour for me to figure it out. The function descriptions could do with some expansion in places.

A few things that would have made this easier would be:
A MarkPaste option that marks the pasted text.
A method to find out what marking mode was used to make a selection, my additions placed Zeus into line marking mode.

Anyway below is the macro with my updates, if it proves useful to anyone else. If there is a more elegant way to store and reselect text I'd be intrested to hear it.

Code: Select all

--
--  Macro Name: zMartin_SearchReplaceIndent.lua
--
-- Description: This macro implements this functionality:
--
--    SearchReplace (search="^\t", replace="\t\t", RegExp=true, selection=true, All)
--
--       Usage: Mark some text area in a document and run this macro. The
--              search and replace will be done on the marked area.
--              Note that line marking mode is then used to re-mark
--              the indented lines.
--
-- For more details refer to Lua Documentation:
--
--   General Documentation: http://www.lua.org/docs.html
--   Strings Documentation: http://www.lua.org/manual/5.0/manual.html#5.3
--
function SearchReplace(line_text)

  -- debugging: count = number of replaces made
  -- local result, count = string.gsub(line_text, "^(\t)(.)", "xx:%2")
  -- message_box(1, result, count)

  -- search and replace the line of text replacing a starting tab with two tabs
  local result = string.gsub(line_text, "^(\t)(.)", "\t\t%2")

  -- add a zeus line feed character
  result = result .. "\r"

  -- return the new line
  return result
end

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

  -- note the currently marked text
  local marked_left   = get_marked_left()
  local marked_top    = get_marked_top()
  local marked_right  = get_marked_right()
  local marked_bottom = get_marked_bottom()

  -- get the current marked text
  local marked_text = macro_tag("$M")

  -- NOTE: The Zeus lines of marked text will terminated by the \r\n
  --       end of line marker so look for these markers and run the
  --       search and replace on all lines found.

  -- run the search and replace
  local result = string.gsub(marked_text, "(.-)(\r\n)", SearchReplace)

  -- the result of the search and replace
  -- message_box(1, result, "Debugging")

  -- put add the result to the clipboard
  set_clipboard_text(result)

  -- paste the result back into document deleting the marked text
  MarkPaste()

  -- Re-mark the lines that were originally marked
  MarkLineReset()
  local markedstart = set_line_pos(marked_top, marked_left)
  MarkLineSetEx()
  local markedend = set_line_pos(marked_bottom, marked_right)
end

key_macro()
Post Reply