unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Nordlöw <per.nordlow@gmail.com>
To: help-gnu-emacs@gnu.org
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)	[thread overview]
Message-ID: <bb2699ec-1d05-46eb-939b-d2387a56ac5a@p10g2000prf.googlegroups.com> (raw)

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	other threads:[~2008-10-08 12:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-08 12:24 Nordlöw [this message]
2008-10-08 12:31 ` Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Locking Function Matchers Nordlöw
2008-10-08 14:15 ` Nikolaj Schumacher
     [not found] ` <mailman.565.1223475359.25473.help-gnu-emacs@gnu.org>
2008-10-08 14:30   ` Nordlöw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bb2699ec-1d05-46eb-939b-d2387a56ac5a@p10g2000prf.googlegroups.com \
    --to=per.nordlow@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).