all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Adding a font-lock face to a major mode
@ 2006-01-04 17:16 Tim Johnson
  2006-01-05  2:07 ` Adding a font-lock face to a major mode/revisited Tim Johnson
  0 siblings, 1 reply; 7+ messages in thread
From: Tim Johnson @ 2006-01-04 17:16 UTC (permalink / raw)


Hi:

I would like to be able to add a font-lock face to a major mode at load
time.

I've written the following code in a file called lisp-mode++. 
I need some help in adding the face properly.
FYI: *If possible*, I'd like this to be cross-compatible to Xemacs.
      but is not a critical need.
;; code follows:
(defconst tj-word-end "\\)\\b")
(defconst tj-word-begin "\\b\\(")
(defconst tj-lisp-user-keywords 
          (concat tj-word-begin ;; test some symbols not highlighted
                  (regexp-opt '( "and" "apply" "concatenate" "format" "funcall" "list" "lambda" 
                                 "mapcar" "not" "print" "push" "return-from " "setf" "setq")) 
                  tj-word-end))
(add-hook 'lisp-mode-hook
          '(lambda ()
             (require 'extra-faces) ;; font-lock-user-keyword-face defined here
             'turn-on-font-lock
             (cons font-lock-defaults  ;; this is wrong, I know!
                   (list tj-lisp-user-keywords '1 'font-lock-user-keyword-face))
             (message "lisp-mode++ loaded")))   ;; test!
(provide 'lisp-mode++) ;; .emacs has (require 'lisp-mode++)
;;

TIA
tim

-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com

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

* Adding a font-lock face to a major mode/revisited
  2006-01-04 17:16 Adding a font-lock face to a major mode Tim Johnson
@ 2006-01-05  2:07 ` Tim Johnson
  2006-01-05  2:11   ` Tim Johnson
  2006-01-05 16:38   ` Kevin Rodgers
  0 siblings, 2 replies; 7+ messages in thread
From: Tim Johnson @ 2006-01-05  2:07 UTC (permalink / raw)


After studying documentation, I have taken a different approach.
Code for lisp-mode++.el is not as follows:
;;
(defconst tj-word-end "\\)\\b")
(defconst tj-lisp-user-keywords 
          (concat tj-word-begin 
                  (regexp-opt '("and" "apply" "concatenate" "format" "funcall" "list" "lambda" 
                                "mapcar" "not" "print" "push" "return-from " "setf" "setq") 
                  ) tj-word-end))
(make-local-variable 'lisp-font-lock-keywords-tj)
(defvar lisp-font-lock-keywords-tj  
  (list
    (list tj-lisp-user-keywords '1 'font-lock-user-keyword-face))
  "Additional keywords and groups for lisp-mode")
(add-hook 'lisp-mode-hook
          '(lambda ()
             (require 'extra-faces) ;; font-lock-user-keyword-face defined here
             'turn-on-font-lock
             ;; I have tried both of the lines below, with no good effect
             (put 'lisp-mode 'font-lock-defaults '(lisp-font-lock-keywords-tj))
             ;(cons font-lock-defaults lisp-font-lock-keywords-tj)
             (message "lisp-mode++ loaded")))
(provide 'lisp-mode++) ;; .emacs has (require 'lisp-mode++)
;;
What works:  
  'add hook is being executed. 
     I see the message 
     Even with "debug on error" turned on, there are no errors.
  the 'require form is being evaluated as 
    'font-lock-user-keyword-face appears as a defined variable.
  lisp-font-lock-keywords-tj appears as a defined variable.
    The regex part of the variable seems to work as tested with
      re-builder
What doesn't work:
  No syntax highlighting appears for the keywords.
  c-h v font-lock-defaults => provides a printed representation
    that does not show lisp-font-lock-keywords-tj as a component of
    that variable.

Unfortunately I have not yet found any examples to work from. I suspect 
that neither the 'put nor the 'cons calls are wrong.

Any help would be appreciated.
tim

-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com

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

* Re: Adding a font-lock face to a major mode/revisited
  2006-01-05  2:07 ` Adding a font-lock face to a major mode/revisited Tim Johnson
@ 2006-01-05  2:11   ` Tim Johnson
  2006-01-05 16:38   ` Kevin Rodgers
  1 sibling, 0 replies; 7+ messages in thread
From: Tim Johnson @ 2006-01-05  2:11 UTC (permalink / raw)


* Tim Johnson <tim@johnsons-web.com> [060104 17:09]:
There is a typo here: 
  where I said:
> Unfortunately I have not yet found any examples to work from. I suspect 
> that neither the 'put nor the 'cons calls are wrong.

 I meant to say that it appears that 
    neither the 'put nor the 'cons cells are correct.
 Ssoorryy!
 tj

> After studying documentation, I have taken a different approach.
> Code for lisp-mode++.el is not as follows:
> ;;
> (defconst tj-word-end "\\)\\b")
> (defconst tj-lisp-user-keywords 
>           (concat tj-word-begin 
>                   (regexp-opt '("and" "apply" "concatenate" "format" "funcall" "list" "lambda" 
>                                 "mapcar" "not" "print" "push" "return-from " "setf" "setq") 
>                   ) tj-word-end))
> (make-local-variable 'lisp-font-lock-keywords-tj)
> (defvar lisp-font-lock-keywords-tj  
>   (list
>     (list tj-lisp-user-keywords '1 'font-lock-user-keyword-face))
>   "Additional keywords and groups for lisp-mode")
> (add-hook 'lisp-mode-hook
>           '(lambda ()
>              (require 'extra-faces) ;; font-lock-user-keyword-face defined here
>              'turn-on-font-lock
>              ;; I have tried both of the lines below, with no good effect
>              (put 'lisp-mode 'font-lock-defaults '(lisp-font-lock-keywords-tj))
>              ;(cons font-lock-defaults lisp-font-lock-keywords-tj)
>              (message "lisp-mode++ loaded")))
> (provide 'lisp-mode++) ;; .emacs has (require 'lisp-mode++)
> ;;
> What works:  
>   'add hook is being executed. 
>      I see the message 
>      Even with "debug on error" turned on, there are no errors.
>   the 'require form is being evaluated as 
>     'font-lock-user-keyword-face appears as a defined variable.
>   lisp-font-lock-keywords-tj appears as a defined variable.
>     The regex part of the variable seems to work as tested with
>       re-builder
> What doesn't work:
>   No syntax highlighting appears for the keywords.
>   c-h v font-lock-defaults => provides a printed representation
>     that does not show lisp-font-lock-keywords-tj as a component of
>     that variable.
> 
> Unfortunately I have not yet found any examples to work from. I suspect 
> that neither the 'put nor the 'cons calls are wrong.
> 
> Any help would be appreciated.
> tim
> 
> -- 
> Tim Johnson <tim@johnsons-web.com>
>       http://www.alaska-internet-solutions.com
> 
> 
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com

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

* Re: Adding a font-lock face to a major mode/revisited
  2006-01-05  2:07 ` Adding a font-lock face to a major mode/revisited Tim Johnson
  2006-01-05  2:11   ` Tim Johnson
@ 2006-01-05 16:38   ` Kevin Rodgers
  1 sibling, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2006-01-05 16:38 UTC (permalink / raw)


Tim Johnson wrote:
 > (make-local-variable 'lisp-font-lock-keywords-tj)

Why?

 > (add-hook 'lisp-mode-hook
 >           '(lambda ()
 >              (require 'extra-faces) ;; font-lock-user-keyword-face 
defined here
 >              'turn-on-font-lock

That quoted symbol inside the lambda body has no effect whatsoever.  I
think you want to call the function, so either do so: (turn-on-font-lock)
or add it as a separate hook: (add-hook 'lisp-mode-hook 'turn-on-font-lock)

 >              ;; I have tried both of the lines below, with no good effect
 >              (put 'lisp-mode 'font-lock-defaults 
'(lisp-font-lock-keywords-tj))
 >              ;(cons font-lock-defaults lisp-font-lock-keywords-tj)

I don't see anything in the documentation that suggests that the
font-lock-defaults property of a major-mode has any effect.  I think you
want:

(set (make-local-variable 'font-lock-defaults) lisp-font-lock-keywords-tj)

or perhaps:

(set (make-local-variable 'font-lock-defaults)
      (append font-lock-defaults lisp-font-lock-keywords-tj))

 >              (message "lisp-mode++ loaded")))

-- 
Kevin Rodgers

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

* Re: Adding a font-lock face to a major mode
       [not found] <mailman.21393.1136395091.20277.help-gnu-emacs@gnu.org>
@ 2006-01-05 17:58 ` Stefan Monnier
  2006-01-05 20:44   ` Tim Johnson
  2006-01-05 21:01   ` Stefan Monnier
  0 siblings, 2 replies; 7+ messages in thread
From: Stefan Monnier @ 2006-01-05 17:58 UTC (permalink / raw)


> I would like to be able to add a font-lock face to a major mode at load
> time.

Try font-lock-add-keywords.


        Stefan

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

* Re: Adding a font-lock face to a major mode
  2006-01-05 17:58 ` Adding a font-lock face to a major mode Stefan Monnier
@ 2006-01-05 20:44   ` Tim Johnson
  2006-01-05 21:01   ` Stefan Monnier
  1 sibling, 0 replies; 7+ messages in thread
From: Tim Johnson @ 2006-01-05 20:44 UTC (permalink / raw)


* Stefan Monnier <monnier@iro.umontreal.ca> [060105 10:19]:
> > I would like to be able to add a font-lock face to a major mode at load
> > time.
> 
> Try font-lock-add-keywords.
 
  <grin> Works like a charm</grin> the kicker, however, is that
  <sigh> I'm tasked with solutions that can be applied to both
         gnu and X emacs. Xemacs does not have that function and
         furthermore, simply exploiting the code doesn't work 
         either, because Xemacs apparently has a significantly different
         structure for syntax highlighting.

  Thanks for the tip Stefan.

  For now it will be a gnu-only solution. Later I can use an 'cond
  form to test the Xemacs and provide an alternate code.

  cheers
  tim

-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com

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

* Re: Adding a font-lock face to a major mode
  2006-01-05 17:58 ` Adding a font-lock face to a major mode Stefan Monnier
  2006-01-05 20:44   ` Tim Johnson
@ 2006-01-05 21:01   ` Stefan Monnier
  1 sibling, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2006-01-05 21:01 UTC (permalink / raw)


>> I would like to be able to add a font-lock face to a major mode at load
>> time.

> Try font-lock-add-keywords.

Don't listen to what he says: font-lock-add-keywords is not for use by
derived modes, so it's OK to use it if you put it in lisp-mode-hook, but if
you end up creating your own major mode by deriving from lisp-mode, you
should create your own font-lock-defaults value and your own
font-lock-keywords value (built from lisp-font-lock-keywords, of course).


        Stefan "the other one"

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

end of thread, other threads:[~2006-01-05 21:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-04 17:16 Adding a font-lock face to a major mode Tim Johnson
2006-01-05  2:07 ` Adding a font-lock face to a major mode/revisited Tim Johnson
2006-01-05  2:11   ` Tim Johnson
2006-01-05 16:38   ` Kevin Rodgers
     [not found] <mailman.21393.1136395091.20277.help-gnu-emacs@gnu.org>
2006-01-05 17:58 ` Adding a font-lock face to a major mode Stefan Monnier
2006-01-05 20:44   ` Tim Johnson
2006-01-05 21:01   ` Stefan Monnier

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.