unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Derived Mode 102 Keybindings and Menu
@ 2006-03-17 22:56 Tim Johnson
  2006-03-21 17:01 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Johnson @ 2006-03-17 22:56 UTC (permalink / raw)


This is the continuation of the Derived Mode 101 HOWTO thread.
Johan and Stefan were able to help me to set up syntax highlighting.
The following file extends the derived mode by adding keymaps, bindings
and a menu.

It seems to work for me on both emacs and XEmacs. I would welcome
comments and observations. My idea for this is to then change the
substring "newlisp" to something like "dmode" and (require 'scheme)
to (require 'parent-mode) and then we would have a derived-mode template
that could be used to pretty much derive for any programming mode.
Documentary comments would also be included.

;; code follows:
(require 'scheme)
;; ==========================================================================
(defface font-lock-newlisp-keywords-face
  '((((class color) (background dark)) (:foreground "yellow"))
    (((class color) (background light)) (:foreground "green4"))
    (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
    (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
    (t (:bold t)))
  "Font Lock mode face used to highlight
   keywords for Newlisp programming language."
  :group 'font-lock-faces)
;; ==========================================================================
(defface font-lock-newlisp-user-keywords-face
  '((((class color) (background dark)) (:foreground "red"))
    (((class color) (background light)) (:foreground "darkred"))
    (((class grayscale) (background light)) (:foreground "DimGray" :italic t))
    (((class grayscale) (background dark)) (:foreground "LightGray" :italic t))
    (t (:bold t)))
  "Font Lock mode face used to highlight
   user keywords for Newlisp programming language."
  :group 'font-lock-faces)
;; ==========================================================================
(defconst
  newlisp-keywords ;; just a few
   (regexp-opt '(
    "acos" "add" "and" "append" "append" "apply" "args" "array"
    )))
;; ==========================================================================
(defconst
  newlisp-user-keywords ;; just a few
   (regexp-opt '(
    "mysql" "oracle" "html" "gtk"
    )))
;; ==========================================================================
(defvar newlisp-font-lock-keywords
   `(,@scheme-font-lock-keywords
     (,(concat "\\<\\(" newlisp-keywords "\\)\\>")
      0 'font-lock-newlisp-keywords-face)
     (,(concat "\\<\\(" newlisp-user-keywords"\\)\\>")
        0 'font-lock-newlisp-user-keywords-face))
   "List of newlisp keywords and faces")
;; ==========================================================================
(defun test-fun ()
  "test function"
  (interactive)
  (message-box "OK. It works"))
;; ==========================================================================
(defvar newlisp-menu-var nil "Newlisp Menu Definition")
(defun newlisp-menu-emacs ()
  "Newlisp Menu for GNU emacs"
  (interactive)
   (easy-menu-add-item
	 nil nil
	 (easy-menu-create-menu  ;; XEmacs does not recognize
	   "Newlisp"
	   '(["Test" test-fun :active t]))))
;; ==========================================================================
(defun newlisp-menu ()
  "Newlisp Menu for XEmacs"
  (interactive)
  (easy-menu-define
	newlisp-menu-var newlisp-keymap "Newlisp Mode Menu"
	'("Newlisp"
	   ["Test" test-fun]))
  (easy-menu-add-item nil nil newlisp-menu-var))
;; ==========================================================================
(defun newlisp-set-keymap ()
  (defvar newlisp-keymap (make-sparse-keymap) "Newlisp mode keymap")
  (defcustom newlisp-keymap-prefix [(control c) (control c)]
    "*Keymap prefix string for newlisp-keymap"
    :type 'string
    :group 'newlisp-mode)
    (local-set-key newlisp-keymap-prefix newlisp-keymap)
    (define-key newlisp-keymap "b" 'test-fun))
;; ==========================================================================
(define-derived-mode newlisp-mode scheme-mode "newlisp"
  "A major mode for Newlisp."
  (newlisp-set-keymap)
  (set (make-local-variable 'font-lock-defaults)
       (cons 'newlisp-font-lock-keywords
             ;; Copy the rest of font-lock-defaults from
             ;; scheme-mode if available.
             (or (cdr font-lock-defaults)
                 '(nil t 
					   ;(("+-*/.<>=!?$%_&~^:" . "w"))
					   ((?+ . "w") (?- . "w") (?* . "w") (?/ . "w")
					    (?. . "w") (?< . "w") (?> . "w") (?= . "w")
                        (?? . "w") (?$ . "w") (?% . "w") (?_ . "w")
                        (?& . "w") (?~ . "w") (?^ . "w") (?: . "w"))))))
  (newlisp-menu)
  (message "Newlisp Derived Mode Loaded!"))
;; end

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

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

* Re: Derived Mode 102 Keybindings and Menu
  2006-03-17 22:56 Derived Mode 102 Keybindings and Menu Tim Johnson
@ 2006-03-21 17:01 ` Stefan Monnier
  2006-03-21 20:55   ` Tim Johnson
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2006-03-21 17:01 UTC (permalink / raw)


> This is the continuation of the Derived Mode 101 HOWTO thread.
> Johan and Stefan were able to help me to set up syntax highlighting.
> The following file extends the derived mode by adding keymaps, bindings
> and a menu.

Google around and see how other packages use the easymenu package.
The typical way to use it is to have somewhere at the toplevel:

(easy-menu-define foo-menu foo-mode-map "Foo mode menu."
  '("Foo"
    [...]
    [...]
    ...))

and then in foo-mode you do

  (easy-menu-add foo-menu)

and that's all there is to it and it should work on both Emacs and XEmacs
(cross-compatibility is the raison d'être of easymenu.el).


        Stefan

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

* Re: Derived Mode 102 Keybindings and Menu
  2006-03-21 17:01 ` Stefan Monnier
@ 2006-03-21 20:55   ` Tim Johnson
  2006-03-21 22:13     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Johnson @ 2006-03-21 20:55 UTC (permalink / raw)


On 2006-03-21, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> This is the continuation of the Derived Mode 101 HOWTO thread.
>> Johan and Stefan were able to help me to set up syntax highlighting.
>> The following file extends the derived mode by adding keymaps, bindings
>> and a menu.
>
> Google around and see how other packages use the easymenu package.
> The typical way to use it is to have somewhere at the toplevel:

 :-) Just realized that the code I included *did not* include a menu.
      
> (easy-menu-define foo-menu foo-mode-map "Foo mode menu."
>   '("Foo"
>     [...]
>     [...]
>     ...))
>
> and then in foo-mode you do

 and I have menu working for both forks. For cross-compatibility XEmacs
 needs a more verbose approach, including passing the keymap as an
 argument. I will post the template later, when I've got time to
 annotate with some documentation.

 Thanks
 tim

>   (easy-menu-add foo-menu)
>
> and that's all there is to it and it should work on both Emacs and XEmacs
> (cross-compatibility is the raison d'être of easymenu.el).
>
>
>         Stefan


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

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

* Re: Derived Mode 102 Keybindings and Menu
  2006-03-21 20:55   ` Tim Johnson
@ 2006-03-21 22:13     ` Stefan Monnier
  2006-03-22  1:55       ` Tim Johnson
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2006-03-21 22:13 UTC (permalink / raw)


>> (easy-menu-define foo-menu foo-mode-map "Foo mode menu."
>> '("Foo"
>> [...]
>> [...]
>> ...))
>> 
>> and then in foo-mode you do

>>   (easy-menu-add foo-menu)

>  For cross-compatibility XEmacs needs a more verbose approach, including
>  passing the keymap as an argument.

I've used the above code in sml-mode and AFAIK it worked both in Emacs and
XEmacs.  So, I don't think you need "a more verbose approach" for XEmacs.


        Stefan

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

* Re: Derived Mode 102 Keybindings and Menu
  2006-03-21 22:13     ` Stefan Monnier
@ 2006-03-22  1:55       ` Tim Johnson
  2006-03-22  3:55         ` Tim Johnson
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Johnson @ 2006-03-22  1:55 UTC (permalink / raw)


On 2006-03-21, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>>> (easy-menu-define foo-menu foo-mode-map "Foo mode menu."
>>> '("Foo"
>>> [...]
>>> [...]
>>> ...))
>>> 
>>> and then in foo-mode you do
>
>>>   (easy-menu-add foo-menu)
>
>>  For cross-compatibility XEmacs needs a more verbose approach, including
>>  passing the keymap as an argument.
>
> I've used the above code in sml-mode and AFAIK it worked both in Emacs and
> XEmacs.  So, I don't think you need "a more verbose approach" for XEmacs.

Here's what I have done:
;; toplevel, called from derive-mode form
(defun newlisp-menu ()
  "Create Newlisp Menu for emacs/XEmacs"
  (defvar newlisp-menu-var nil "Newlisp Menu Definition")
  (easy-menu-define
   newlisp-menu-var newlisp-keymap "Newlisp Mode Menu"
     '("Newlisp"
     ["Test" test-fun]))
  (easy-menu-add-item nil nil newlisp-menu-var))
;; Works on both forks.
;; Tried changing
  (easy-menu-add-item nil nil newlisp-menu-var)
  to:
  (easy-menu-add-item newlisp-menu-var)
  ;; worked on Xemacs not on gnu emacs
-- 
Tim Johnson <tim@johnsons-web.com>
      http://www.alaska-internet-solutions.com

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

* Re: Derived Mode 102 Keybindings and Menu
  2006-03-22  1:55       ` Tim Johnson
@ 2006-03-22  3:55         ` Tim Johnson
  0 siblings, 0 replies; 6+ messages in thread
From: Tim Johnson @ 2006-03-22  3:55 UTC (permalink / raw)


On 2006-03-22, Tim Johnson <tim@johnsons-web.com> wrote:
The previous posting had a typo: see below
> On 2006-03-21, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>>>> (easy-menu-define foo-menu foo-mode-map "Foo mode menu."
>>>> '("Foo"
>>>> [...]
>>>> [...]
>>>> ...))
>>>> 
>>>> and then in foo-mode you do
>>
>>>>   (easy-menu-add foo-menu)
>>
>>>  For cross-compatibility XEmacs needs a more verbose approach, including
>>>  passing the keymap as an argument.
>>
>> I've used the above code in sml-mode and AFAIK it worked both in Emacs and
>> XEmacs.  So, I don't think you need "a more verbose approach" for XEmacs.
>
> Here's what I have done:
> ;; toplevel, called from derive-mode form
> (defun newlisp-menu ()
>   "Create Newlisp Menu for emacs/XEmacs"
>   (defvar newlisp-menu-var nil "Newlisp Menu Definition")
>   (easy-menu-define
>    newlisp-menu-var newlisp-keymap "Newlisp Mode Menu"
>      '("Newlisp"
>      ["Test" test-fun]))
>   (easy-menu-add-item nil nil newlisp-menu-var))
> ;; Works on both forks.
> ;; Tried changing
>   (easy-menu-add-item nil nil newlisp-menu-var)
>   to:
;; typo below
>   (easy-menu-add-item newlisp-menu-var)
;; should have been
   (easy-menu-add newlisp-menu-var)
;; Sorry!

>   ;; worked on Xemacs not on gnu emacs


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

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

end of thread, other threads:[~2006-03-22  3:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-17 22:56 Derived Mode 102 Keybindings and Menu Tim Johnson
2006-03-21 17:01 ` Stefan Monnier
2006-03-21 20:55   ` Tim Johnson
2006-03-21 22:13     ` Stefan Monnier
2006-03-22  1:55       ` Tim Johnson
2006-03-22  3:55         ` Tim Johnson

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).