all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Error-Free Navigation in Trees of Sub-Expressions (sexp) in  Font-Locking Function Matchers
@ 2008-10-08 12:24 Nordlöw
  2008-10-08 12:31 ` Nordlöw
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Nordlöw @ 2008-10-08 12:24 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Error-Free Navigation in Trees of Sub-Expressions (sexp) in  Font-Locking Function Matchers
  2008-10-08 12:24 Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Locking Function Matchers Nordlöw
@ 2008-10-08 12:31 ` Nordlöw
  2008-10-08 14:15 ` Nikolaj Schumacher
       [not found] ` <mailman.565.1223475359.25473.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 4+ messages in thread
From: Nordlöw @ 2008-10-08 12:31 UTC (permalink / raw)
  To: help-gnu-emacs

Ooops...

My initial posting contained two errors.
Here is the correct test code that works in Vanilla Emacs:

(defun pnw-fancy/setq-args-matcher (limit)
  (let ((start (point)))    ; remember beginning
    (if (looking-at (concat "[[:blank:]\n]*" "'?\\(\\w+\\)"))
        (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-name-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)

/Nordlöw


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Locking Function Matchers
  2008-10-08 12:24 Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Locking Function Matchers Nordlöw
  2008-10-08 12:31 ` Nordlöw
@ 2008-10-08 14:15 ` Nikolaj Schumacher
       [not found] ` <mailman.565.1223475359.25473.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 4+ messages in thread
From: Nikolaj Schumacher @ 2008-10-08 14:15 UTC (permalink / raw)
  To: Nordlöw; +Cc: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> wrote:

> What is the most clever way of checking when we are at the last sexp
> (forward-sexp will fail as mentioned above)?

You could use either:

(condition-case err
   (while t
     (forward-sexp)
     ...)
 (scan-error . nil))

to break the loop on an error, or:

(while (ignore-errors (forward-sexp) t)
  ...)

to get a nil when forward-sexp fails.


regards,
Nikolaj Schumacher




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Error-Free Navigation in Trees of Sub-Expressions (sexp) in  Font-Locking Function Matchers
       [not found] ` <mailman.565.1223475359.25473.help-gnu-emacs@gnu.org>
@ 2008-10-08 14:30   ` Nordlöw
  0 siblings, 0 replies; 4+ messages in thread
From: Nordlöw @ 2008-10-08 14:30 UTC (permalink / raw)
  To: help-gnu-emacs

On 8 Okt, 16:15, Nikolaj Schumacher <m...@nschum.de> wrote:
> Nordlöw <per.nord...@gmail.com> wrote:
> > What is the most clever way of checking when we are at the last sexp
> > (forward-sexp will fail as mentioned above)?
>
> You could use either:
>
> (condition-case err
>    (while t
>      (forward-sexp)
>      ...)
>  (scan-error . nil))
>
> to break the loop on an error, or:
>
> (while (ignore-errors (forward-sexp) t)
>   ...)
>
> to get a nil when forward-sexp fails.
>
> regards,
> Nikolaj Schumacher

Superb!

One can do everything in elisp, if you only know how ;)

/Nordlöw


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-10-08 14:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-08 12:24 Error-Free Navigation in Trees of Sub-Expressions (sexp) in Font-Locking Function Matchers Nordlöw
2008-10-08 12:31 ` 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

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.