2

I've added some keywords using the following construct (where tag-todo and tag-note are variable holding a SVG image and tag-key () is a function returning a SVG image):

(font-lock-add-keywords nil
        '(("\\(\:TODO\:\\)" 1 `(face nil display ,tag-todo))
          ("\\(\:NOTE\:\\)" 1 `(face nil display ,tag-note))
          ("\\(=[0-9a-zA-Z- ]+?=\\)" 1
                              `(face nil display ,(tag-key (match-string 0))))))

This works great (see reddit post) but I would like now to do the same using a list of regexp / tag (or function):

(setq tags '((":TODO:"            . tag-todo)
             (":NOTE:"            . tag-note)
             ("=[0-9a-zA-Z- ]+?=" . tag-key)))

I can iterate over the list using dolist but then I'not sure how to write the actual call to font-lock-add-keywords (building the regexp and make sure the tag variable (or function) is called):

(dolist (item tags)
  (let ((regexp (car item))
        (tag    (cdr item)))
    (message (format "%s : %s" regexp tag))
    ;; HOW TO CALL font-lock-add-keywords with regexp and tag ?
))

Update

I've tried using mapcar, but the (cdr item) does not evaluate to the actual tag content.

(defun f (item)
  `( ,(concat "\\(" (car item) "\\)") 1
     `(face nil display (cdr item))))
(mapcar 'f tags)
Drew
  • 75,699
  • 9
  • 109
  • 225
Nicolas Rougier
  • 487
  • 3
  • 15

1 Answers1

1

This may get you there. (Do you really want/need a \ before each colon (:)?)

(defun foo ()
  "..."
  (font-lock-add-keywords nil (mapcar 'bar tags)))

(defun bar (pat.fac)
  "..."
  (let ((pat  (format "\\(%s\\)" (car pat.fac)))
        (fac  (cdr pat.fac)))
    (when (eq fac 'tag-key) (setq fac  '(tag-key (match-string 0))))
    (setq fac  ``(face nil display ,,fac))
    `(,pat 1 ,fac)))

(mapcar 'bar tags) returns this:

(("\\(:TODO:\\)" 1
  `(face nil display ,tag-todo))
 ("\\(:NOTE:\\)" 1
  `(face nil display ,tag-note))
 ("\\(=[0-9a-zA-Z- ]+?=\\)" 1
  `(face nil display ,(tag-key (match-string 0)))))

The key to the trouble you were having, I think, is to use two levels of backquoting.

Drew
  • 75,699
  • 9
  • 109
  • 225