[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extending lua-mode syntax table (report from help-gnu-emacs)
From: |
Stefan Monnier |
Subject: |
Re: Extending lua-mode syntax table (report from help-gnu-emacs) |
Date: |
Sun, 11 Apr 2010 16:15:46 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) |
> (defun lua-mode ()
> ;; ....
> (set (make-local-variable 'font-lock-defaults)
> '(lua-font-lock-keywords nil nil ((?_ . "w"))))
> (defvar lua-font-lock-keywords
> (eval-when-compile
> (list
> ;; .....
> ;; Long strings.
> '("\\(?:^\\|[^[-]\\)\\(\\[\\(=*\\)\\[\\(?:.\\|\n\\)*?\\]\\2\\]\\)"
> (1 "\""))
You want to use font-lock-syntactic-keywords. E.g. (warning: 100%
guaranteed untested code ahead):
(defun lua-mode ()
;; ....
(set (make-local-variable 'font-lock-defaults)
'(lua-font-lock-keywords nil nil ((?_ . "w")) nil
(font-lock-syntactic-keywords . lua-font-lock-syntactic-keywords)))
(defvar lua-font-lock-syntactic-keywords
(eval-when-compile
(list
;; .....
;; Long strings.
'("\\(\\[\\)\\[\\|]\\(]\\)"
(1 (if (null (nth 8 (syntax-ppss))) "\"") nil lax)
(2 (if (nth 8 (syntax-ppss)) "\"") nil lax)))))
> (defun lua-syntax-status ()
> "Returns the syntactic status of the character after the point."
> (parse-partial-sexp (save-excursion (beginning-of-line) (point))
> (point)))
(save-excursion (beginning-of-line) (point)) can be replaced by
(line-beginning-position).
> (defun lua-string-p ()
> "Returns true if the point is in a string."
> (elt (lua-syntax-status) 3))
> As you can see, the parse-partial-sexp function gets a range of text
> from the start of the line until the point. The long strings can span
> multiple lines (in fact, that's the whole point). So I'm worried that
> the parse-partial-sexp doesn't have enough context to work with. Is
> this true?
Yes.
> If so, is there a work-around (other than parsing from the start of
> the buffer), or do I need to implement something clever and specific?
You'll probably want to use (syntax-ppss) as a replacement for
(lua-syntax-status), which does exactly the same thing except that it
parses the whole buffer, but uses caching internally to try and make it
still fast enough.
Stefan