Page 1 of 1

Some of my keyboard macros

Posted: Thu Aug 24, 2006 5:04 pm
by codeguy
I like home key to go to first word or first column. This macro toggles between the two:

Code: Select all

-- home.lua
function key_macro()
	local tab   = 9
	local space = 32

	local oldpos = get_cursor_pos()

	MoveLineHome()
	local chdata = get_char_at()
	if (chdata == space) or (chdata == tab) then
		MoveLineFirst()
		if get_cursor_pos() == oldpos then
			MoveLineHome()
		end
	end
end

key_macro()
I like Ctrl-C to copy only one word if nothing is highlighted, so I have Ctrl-C bound to this:

Code: Select all

-- copy.lua
function key_macro()
  -- make sure we have a document file
  if is_document() == 0 then
    message("This macro only works for document files!")
    return
  end

  -- check for a mark
  if is_marked() == 0 then
    -- try to use the current word
    MarkWordCurrent()
  end

  MarkCopy();
end

key_macro()
Paste is nice, so I leave Ctrl-V alone. But sometimes I want to paste over the current word, so I'v bound Ctrl-B to:

Code: Select all

 -- paste.lua
function key_macro()
  -- make sure we have a document file
  if is_document() == 0 then
    message("This macro only works for document files!")
    return
  end

  -- check for a mark
  if is_marked() == 0 then
    MarkWordCurrent()
  end

  MarkDelete()
  MarkPaste()

end

key_macro()
Ctrl-Alt-Up is used to find a previous instance of the current word. Its bound to:

Code: Select all

-- findprev.lua
function findprev()
    local text = macro_tag("$W")
    local scope = get_search_option("Scope")
    local use_case = get_search_option("UseCase")
    local whole_word = get_search_option("WholeWord")
	local regexp = get_search_option("RegExpress")

    set_search_option("Scope", 1)
    set_search_option("UseCase", 0)
    set_search_option("WholeWord", 1)
    set_search_option("RegExpress", 0)

    set_find_text(text)
    SearchPrevious()

    set_search_option("Scope", scope)
    set_search_option("UseCase", use_case)
    set_search_option("WholeWord", whole_word)
    set_search_option("RegExpress", regexp)
end

findprev()
Ctrl-Alt-Down does the same thing, but searches next. Its macro is the same but SearchPrevious() replaced with SearchNext()

-Andy

Posted: Mon Jul 29, 2013 5:01 am
by jussij
The later version of Zeus has a search_options_restore macro function and this allows the findprev macro to be written as follows:

Code: Select all

-- findprev.lua
function findprev()
    local text = macro_tag("$W")
    local scope = get_search_option("Scope")
    local use_case = get_search_option("UseCase")
    local whole_word = get_search_option("WholeWord")
    local regexp = get_search_option("RegExpress")

    -- save current search settings
    search_options_save()

    set_search_option("Scope", 1)
    set_search_option("UseCase", 0)
    set_search_option("WholeWord", 1)
    set_search_option("RegExpress", 0)

    set_find_text(text)

    SearchPrevious()

    -- restore the search options
    search_options_restore()
end

findprev()
Some nice macros by the way :)