Frage

The following code fails to highlight 23's in 23-23 if pasted and evaluated in the scratch buffer, but not if done in a text buffer.

;; Example 1


'(1234 23 23-23 end)

(progn
  (font-lock-add-keywords nil
                          `(("\\b23\\b"
                             (0 'success))
                            "end"))
  (font-lock-fontify-buffer))

Why does it fail when M-x isearch-forward-regexp RET \b23\b still matches 23's in 23-23?

Even if I change to the following code, only the first 23 in 23-23 gets highlighted.

;;; Example 2

'(1234 23 23-23 end)

(progn
  (font-lock-add-keywords nil
                          `((,(rx (or word-boundary
                                      "-")
                                  (group "23")
                                  (or word-boundary
                                      "-"))
                             (1 'success))
                            "end"))
  (font-lock-fontify-buffer))

Side note: "end" is there so that I can detect if the highlighter for 23 is ill formed. If it is ill formed or signals errors silently, end won't get highlighted.


;;; Example 3 (with xy instead of 23. also passing t and 'append.)
;;; if evaluated in the scratch buffer, it doesn't highlight xy in xy-xy

'(wxyz xy xy-xy end)

(progn
  (font-lock-add-keywords nil
                          `(("\\bxy\\b"
                             (0 'success t))
                            "end")
                          'append)
  (font-lock-fontify-buffer))
War es hilfreich?

Lösung

The fact that it does not in buffer *scratch* suggests that it is a problem with the current mode. There are two main possibilities:

  • What @wvcvw suggested: check what the syntax class of chars 2 and 3 is.

  • The font-lock-keywords already defined for the mode interact with your code -- e.g., they override it. Try adding 'APPEND as a third arg to font-lock-add-keywords. Try adding t as a HIGHLIGHT expression to your highlighter sexp (see the doc). That should let your highlighting override any that might already be there otherwise.

BTW, you say it does not work in a "text buffer", but what does that mean? From emacs -Q, evaluating your code in a buffer in text-mode shows that it does work. Investigate what your "text buffer" mode is and try the suggestions above (both bullets if necessary, but try the second one first).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top