help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Lockin


From: Nordlöw
Subject: Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Locking Function Matchers
Date: Wed, 8 Oct 2008 05:24:16 -0700 (PDT)
User-agent: G2/1.0

How can I programmatically iterate over all sexps in an emacs-lisp
expression without risking getting an error? I have tried forward-
sexp() and backward-sexp() and up/down-list() but these generate an
error when I get to the beginning or end in each "direction". Why, on
earth, doesn't these functions have an optional argument, say no-
error, that inhibits errors in these cases and instead indicates this
event by returning nil?

I believe I need these functions because I am currently extending font-
locking in emacs-lisp-mode to highlight variables in let and defun-
like statements.

Here is my moccup so far:

(defun pnw-fancy/setq-args-matcher (limit)
  (let ((start (point)))    ; remember beginning
    (if (looking-at (concat "[[:blank:]\n]*" "'?\\(\\w+\\)")) ; One
more SYM?
        (progn (goto-char (match-end 1))
               (forward-sexp); skip VAL
               t)); signal hit
    ))
(defun pnw-fancy/emacs-lisp-variables-font-locking ()
  (font-lock-add-keywords
   nil
   (list
    ;; setq-statements: (setq SYM VAL ...)
    `(;; MATCHER: (SETQ
      ,(concat "(" (regexp-opt '("set"
                                 "setq"
                                 "setq-default"
                                 "setq-mode-local") t)
               "[[:blank:]\n]*")
      ;; SUBEXP-HIGHLIGHTER
      (1 'font-lock-function-call-face keep)
      ;; ANCHORED-HIGHLIGHTER: (setq SYM VAL ...)
      (;; ANCHORED-MATCHER
       pnw-fancy/setq-args-matcher
       nil ; PRE-FORM
       nil ; POST-FORM
       (1 'font-lock-variable-name-face prepend) ; SUBEXP-HIGHLIGHTERS
       ))
    ) t))
(add-hook 'emacs-lisp-mode-hook 'pnw-fancy/emacs-lisp-variables-font-
locking)

Test on these examples:

(setq a 1 b 2 c 3)
(setq a 1
      b 2
      c 3)
(setq x-1 '(1 1) x-2 '(2 2) x-3 '(3 3))

It currently doesn't work for multi-line variants of setq-form. Is
this beyond the capabilities of font-lock?

As I have said above it siliently errors when the last VAL is missing
like in the following statement:
(setq a 1 b 2 c)
What is the most clever way of checking when we are at the last sexp
(forward-sexp will fail as mentioned above)?

Thanks in advance,
Nordlöw


reply via email to

[Prev in Thread] Current Thread [Next in Thread]