I'd like to have certain keymap active when the cursor is in a particular highlighted area. For instance, I would like to bind hl-todo-next to some easy key combination, but only when the cursor is on such a TODO item, as marked by ht-todo-mode.

I understand that a piece of text or an overlay can have its own keymap property which would work that way.

How do I best augment keymaps for highlighting already put by existing modes, such as hl-todo-mode or highlight-symbol-mode? Can I e.g. associate it with a particular named face?

Specifically, what would be a good way to:

  • Add / update a keymap for a particular already highlighted fragment of text on which the cursor is currently positioned?
  • Remove / restore that keymap when the highlighting vanishes (because of the cursor movement or the text being changed).

I definitely can just run a hook at every cursor movement, or character typed, or time elapsed; I wonder if there are nicer ways to do that, and are there any subtleties not immediately obvious from the elisp documentation.

  • 2
    AFAIK, you cannot associate a keymap to a face, but you can associate a keymap to text as a text property or to an overlay. You need to create the keymap and assign it as the value of the keymap text property of the text in question or of the overlay. – NickD Jan 22 at 21:39
  • Your last paragraph expresses the question. Scan the buffer, looking for the text highlighted by those modes, and put property keymap on that highlighted text (or the overlay, if the highlighting is by overlay). Or if you know which overlays are used, give them the keymap property you want. Or advise the functions that do the highlighting, to make them also add property keymap. – Drew Jan 22 at 22:59
  • @Drew: Thanks! I just thought that maybe there are already some existing tools that do a similar thing. How do I advise? E.g. when highlight-symbol-mode is on, symbols get highlighted dynamically; how do I best update the keymap for the symbol under cursor when it happens? And how do I best remove the keymap when the highlight is gone? – 9000 Jan 23 at 14:07
  • Please pose specific separate questions for your follow-up questions. Comments can be deleted at any time. Each question should be specific, which solicits pertinent, specific answers. Thx. – Drew Jan 23 at 17:04
  • @Drew: I hope I have made my question more specific. – 9000 Jan 23 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.