3

I'm trying to make a mode where certain strings in the buffer have a special keymap active when the point is inside them. The documentation for font-lock-extra-managed-props and the Elisp info manual section Search-based Fontification suggest this is possible:

However, FACESPEC can also evaluate to a list of this form: (face FACE PROP1 VAL1 PROP2 VAL2...) to specify the face FACE and various additional text properties to put on the text that matches. If you do this, be sure to add the other text property names that you set in this way to the value of ‘font-lock-extra-managed-props’ so that the properties will also be cleared out when they are no longer appropriate. Alternatively, you can set the variable ‘font-lock-unfontify-region-function’ to a function that clears these properties.

Unfortunately the documentation isn't very clear on the format of FACESPEC--I've tried variously quoted versions and none have the desired effect of a keymap active only in a certain area. So here's a simplified version of my code:

;;; Face to highlight trigger zone
(defface trigger-zone-face '((t (:foreground "gold" :background "red")))
  "Highlight a trigger zone.")
;;; Function to trigger
(defun trigger-zone-function ()
  "Function to test trigger zones."
  (interactive)
  (message "Trigger-zone activated!"))
;;; Map active in trigger zone
(defvar trigger-zone-map
  (let ((keymap (make-sparse-keymap)))
    (define-key keymap (kbd "C-c i") #'trigger-zone-function))
  "Map to activate in trigger zone.")
;;; Font locking
(setq trigger-zone-highlights
      '(("trigger-zone" face trigger-zone-face keymap trigger-zone-map)))
;;; Trigger zone mode
(define-derived-mode trigger-zone-mode text-mode "trigger"
  "Major mode to trigger commands from text."
  (setq font-lock-extra-managed-props '(keymap help-echo))
  (setq font-lock-defaults '(trigger-zone-highlights)))  

Anybody have any idea what I'm doing wrong? I think I may be formatting the FACESPEC wrong, but I can't find any documentation to clarify e.g. what parts of the list should be quoted.

2

Try

(defconst trigger-zone-highlights
          '(("trigger-zone"
             (0 '(face trigger-zone-face keymap trigger-zone-map)))))
  • Thanks, this works to set the keymap property, thanks! I had to make some other changes to achieve the desired behavior: `(("trigger-zone" (0 '(face trigger-zone-face keymap ,trigger-zone-keymap)))) – gajogacchati Dec 5 '19 at 6:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.