emacs-devel
[Top][All Lists]
Advanced

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

Re: Patch for sql.el


From: Michael Mauger
Subject: Re: Patch for sql.el
Date: Thu, 6 May 2004 15:30:12 -0700 (PDT)

--- Stefan Monnier <address@hidden> wrote:
> I guess the "cleanest" way to do it with the current
> font-lock.el code would be to only put the ANSI patterns on
> sql-font-lock-keywords and then use font-lock-add-keywords and
> font-lock-remove-keywords for the product-specific patterns
> (and call font-lock-fontify-buffer to refresh the buffer).
> 
> But that does not allow you to play with the syntax-alist property.
> 

Thanks, Stefan, for your efforts on this.  I inherited much of this code
and you've helped me clean it up significantly.

Now, back to font-lock's syntax-alist property.  What if it were possible
to add and remove syntax-alist entries ala keywords.

For example:


(defun font-lock-add-syntax (syntax-alist)
  (dolist (selem syntax-alist)
    ;; The character to modify may be a single CHAR or a STRING.
    (let ((syntax (cdr selem)))
      (dolist (char (if (numberp (car selem))
                        (list (car selem))
                      (mapcar 'identity (car selem))))
        ;; Modify the entry for CHAR to be SYNTAX.
        (modify-syntax-entry char syntax font-lock-syntax-table)))))

(defun font-lock-remove-syntax (syntax-alist)
  (dolist (selem syntax-alist)
    ;; The character to modify may be a single CHAR or a STRING.
    (let ((syntax (string-to-syntax (cdr selem))))
      (dolist (char (if (numberp (car selem))
                        (list (car selem))
                      (mapcar 'identity (car selem))))
        ;; If the entry for CHAR is SYNTAX then reset to base entry.
        (if (equal (aref font-lock-syntax-table char) syntax))
            (aset font-lock-syntax-table char
                  (aref (syntax-table) char)))))))


Basically, `font-lock-add-syntax' duplicates the code in
`font-lock-set-defaults' and could be used there. 
`font-lock-remove-syntax' takes the same parameter as `-add-syntax' and
restores the syntax setting from the buffer's base sytax table *only* if
the value in font-lock's syntax table is the same as the value in the
SYNTAX-ALIST argument.

With the above functions loaded, try this in *scratch*

  ;; This should be a comment
  (aref font-lock-syntax-table ?\;) ;; => (11)
  ;; Reset the semicolon character class
  (font-lock-add-syntax '((?\; . "w")))
  (font-lock-fontify-buffer)
  ;; Comments should now not be highlighted
  (aref font-lock-syntax-table ?\;) ;; => (2)

  (font-lock-remove-syntax '((?\; . "(")))
  (font-lock-fontify-buffer)
  ;; Comments should still not be highlighted 
  ;; because semis were set to words not parens
  (aref font-lock-syntax-table ?\;) ;; => (2)

  (font-lock-remove-syntax '((?\; . "w")))
  (font-lock-fontify-buffer)
  ;; They're baaaack!
  (aref font-lock-syntax-table ?\;) ;; => (11)

If this is worth doing in font-lock, I can implement the incremental
loading and unloading of product specific keywords and syntax entries in
sql.el.

Or are we pushing the feature freeze...?  

-- Michael





reply via email to

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