[code] [textadept] Thoughts about custom key maps for command entry input?

From: Peter Kazmier <pete.att.kazmier.com>
Date: Sun, 7 Apr 2013 15:01:12 -0500

I've been playing around with the key bindings for incremental search such that while searching, pressing:

- RET leaves the caret at its current location and ends the search,
- Ctl-S moves forward to the next match,
- Ctl-R moves backward to the previous match,
- etc ...

I generally like this type of functionality better as I can quickly move backward and forward while in the incremental search. This was very easy to add:

    events_connect(events.COMMAND_ENTRY_KEYPRESS, function(code)
      if not find.incremental then return end
      if keys.KEYSYMS[code] == 'esc' then
        find.incremental = nil
      elseif keys.KEYSYMS[code] == '\b' then
        find_incremental(gui.command_entry.entry_text:sub(1, -2))
      elseif code == 19 then -- Ctl-S move forward to next match
        find.incremental_start = buffer.current_pos + 1
        find_incremental(gui.command_entry.entry_text)
        return true
      elseif code == 18 then -- Ctl-R move backward to prior match
        find.incremental_start = buffer.current_pos + 1
        find_incremental(gui.command_entry.entry_text, false)
        return true
      elseif code < 256 then
        find_incremental(gui.command_entry.entry_text..string.char(code))
      end
    end, 1) -- place before command_entry.lua's handler (if necessary)

While hacking that, I thought that a find command entry keymap would be very handy as users could then easily and quickly bind various functions to keys while in command entry mode. In this case, I could simply have defined a `find_next` and `find_prev` functions and bound them to the command entry keymap if it existed. Thoughts? Assume we have these functions defined:

    function find.end_search()
      find.incremental = nil
    end
    function find.delete_character_search()
      find_incremental(gui.command_entry.entry_text:sub(1, -2))
    end
    function find.find_next()
      -- Bind this to Ctl-S for example to move forward to next match
      find.incremental_start = buffer.current_pos + 1
      find_incremental(gui.command_entry.entry_text)
      return true
    end
    function find.find_prev()
      -- Bind to Ctl-R for example to move backward to prior match
      find.incremental_start = buffer.current_pos + 1
      find_incremental(gui.command_entry.entry_text, false)
      return true
    end

Then we can provide a key map, maybe like:

    keys['find_command_entry'] = {
      ['esc'] = find.end_search,
      ['\b'] = find.delete_character_search,
      ['\n'] = find.end_search,
      ['cs'] = find.find_next,
      ['cr'] = find.find_prev,
    }

Then maybe we connect to the events like this (pseudo code):

    events_connect(events.COMMAND_ENTRY_KEYPRESS, function(code)
      if not find.incremental then return end
      if keys.find_command_entry[code] then
        return keys.find_command_entry[code]()
      elseif code < 256 then
        find_incremental(gui.command_entry.entry_text..string.char(code))
      end
    end, 1) -- place before command_entry.lua's handler (if necessary)

    events_connect(events.COMMAND_ENTRY_COMMAND, function(text)
      if find.incremental then
        if keys.find_command_entry['\n'] then
          return keys.find_command_entry['\n']()
        end
    end, 1) -- place before command_entry.lua's handle

Pete

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Sun 07 Apr 2013 - 16:01:12 EDT

This archive was generated by hypermail 2.2.0 : Mon 08 Apr 2013 - 06:37:04 EDT