all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Format av menu keymaps
@ 2006-01-08 22:27 Lennart Borgman
  2006-01-09  2:27 ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Lennart Borgman @ 2006-01-08 22:27 UTC (permalink / raw)


I am loooking for the format of a menu keymap. There seem to be several 
different formats that are possible for a submenu, but I can not find 
where these are described. Are there any functions to check if an entry 
in a keymap list is a submenu?

I have invented something like the function below to use. It works, but 
I have no idea whether it will work always:

;; Function to get submenu and title
(defun appmenu-get-submenu(menu-command)
  (let (subtitle submenumap)
    (if (eq 'menu-item (car menu-command))
        (progn (setq subtitle   (cadr  menu-command))
               (setq submenumap (caddr menu-command)))
      (setq subtitle   (car menu-command))
      (setq submenumap (cdr menu-command)))
    (unless (keymapp submenumap) (error "submenu not a keymap=%s" submenu))
    (cons subtitle submenumap)))

;; A typical usage in my case:
          (map-keymap
           (lambda(binding command)
             (let* ((tit-map (appmenu-get-submenu command))
                    (subtitle (car tit-map))
                    (submenumap (cdr tit-map)))
               (define-key map [appmenu-major]
                 (list 'menu-item
                       subtitle submenumap
                       ))))
           (cdr (assoc 'menu-bar major-map)))

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

* Re: Format av menu keymaps
  2006-01-08 22:27 Format av menu keymaps Lennart Borgman
@ 2006-01-09  2:27 ` Stefan Monnier
  2006-01-09 21:45   ` Lennart Borgman
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2006-01-09  2:27 UTC (permalink / raw)
  Cc: Emacs Devel

> I am loooking for the format of a menu keymap. There seem to be several
> different formats that are possible for a submenu, but I can not find where
> these are described.  Are there any functions to check if an entry in
> a keymap list is a submenu?

Any keymap is potentially a menu. It depends on whether you use it like one
or not (e.g. if you pass it to x-popup-menu or bind it to some mouse event).
So I think the question is wrongly phrased.  Could you give us some context
and more concrete details of what you're trying to do?


        Stefan

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

* Re: Format av menu keymaps
  2006-01-09  2:27 ` Stefan Monnier
@ 2006-01-09 21:45   ` Lennart Borgman
  2006-01-09 23:10     ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Lennart Borgman @ 2006-01-09 21:45 UTC (permalink / raw)
  Cc: Emacs Devel

Stefan Monnier wrote:

>>I am loooking for the format of a menu keymap. There seem to be several
>>different formats that are possible for a submenu, but I can not find where
>>these are described.  Are there any functions to check if an entry in
>>a keymap list is a submenu?
>>    
>>
>
>Any keymap is potentially a menu. It depends on whether you use it like one
>or not (e.g. if you pass it to x-popup-menu or bind it to some mouse event).
>So I think the question is wrongly phrased.  Could you give us some context
>and more concrete details of what you're trying to do?
>
I am trying to "reuse submenu keymaps".

Let us say I have defined a little minor mode that adds a submenu to the 
menu-bar. I may later want to use this submenu in another place, say in 
another submenu on the menu-bar. I may also want to use it in a popup menu.

Is this clear enough or does it just sound crazy? Maybe an example can help:


**** I have a menu keymap like this that can be used by itself (which is 
a good thing):
(defconst xhtml-help-mode-keymap
  (let ((map (make-sparse-keymap "Xhtml-Help")))
    (define-key map [menu-bar xh-help] (cons "Xhtml-Help" 
(make-sparse-keymap "second")))
    (define-key map [menu-bar xh-help css-help] '("CSS Help" . 
xhtml-help-show-css-ref))
    (define-key map [menu-bar xh-help tag-help] '("Xhtml Tag Help" . 
xhtml-help-show-tag-ref))
    map))

**** However now I decided I want to use that in another submenu in 
menu-bar. Then I do something like this:
      (when (featurep 'xhtml-help)
        (let ((menu-bar-entry (cdr (assoc 'menu-bar 
xhtml-help-mode-keymap))))
          (when menu-bar-entry
            (map-keymap
             (lambda(binding command)
               (let* ((tit-map (appmenu-get-submenu command))
                      (subtitle (car tit-map))
                      (submenu  (cdr tit-map)))
                 (define-key map
                   [nxhtml-xhtml-help]
                   (list 'menu-item
                         subtitle submenu
                         :help "XHTML help access"))))
             menu-bar-entry)
            (define-key map [nxhtml-nxhtml-help-separator] (list 
'menu-item "--"))

***** Where this little function tries to get the submenu:
(defun appmenu-get-submenu(menu-command)
  (let (subtitle submenumap)
    (if (eq 'menu-item (car menu-command))
        (progn (setq subtitle   (cadr  menu-command))
               (setq submenumap (caddr menu-command)))
      (setq subtitle   (car menu-command))
      (setq submenumap (cdr menu-command)))
    (unless (keymapp submenumap) (error "submenu not a keymap=%s" submenu))
    (cons subtitle submenumap)))


There are two things I do not really like, appmenu-get-submenu and the 
use of map-keymap. I do not know what to do instead. For 
appmenu-get-submenu I would like at least some documentation for how to 
write this. Even better would be defun in Emacs for something like it. 
Since we are close to a release (I hope) I really just wanted to say 
that it seems to me like documentation is missing.

And if the code looks wrong then of course I would like some hints.

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

* Re: Format av menu keymaps
  2006-01-09 21:45   ` Lennart Borgman
@ 2006-01-09 23:10     ` Stefan Monnier
  2006-01-09 23:57       ` Lennart Borgman
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2006-01-09 23:10 UTC (permalink / raw)
  Cc: Emacs Devel

> **** I have a menu keymap like this that can be used by itself (which is
>      a good thing):
> (defconst xhtml-help-mode-keymap
>   (let ((map (make-sparse-keymap "Xhtml-Help")))
>     (define-key map [menu-bar xh-help] (cons "Xhtml-Help"
>     (make-sparse-keymap "second")))
>     (define-key map [menu-bar xh-help css-help] '("CSS Help"
>     . xhtml-help-show-css-ref))
>     (define-key map [menu-bar xh-help tag-help] '("Xhtml Tag Help"
>     . xhtml-help-show-tag-ref))
>     map))

So far so good.

> **** However now I decided I want to use that in another submenu in
>      menu-bar. Then I do something like this:

I'm not sure what "use" means here.

>       (when (featurep 'xhtml-help)
>         (let ((menu-bar-entry (cdr (assoc 'menu-bar xhtml-help-mode-keymap))))

Rather than `assoc' you should probably use lookup-key.

>           (when menu-bar-entry
>             (map-keymap
>              (lambda(binding command)
>                (let* ((tit-map (appmenu-get-submenu command))
>                       (subtitle (car tit-map))
>                       (submenu  (cdr tit-map)))
>                  (define-key map
>                    [nxhtml-xhtml-help]
>                    (list 'menu-item
>                          subtitle submenu
>                          :help "XHTML help access"))))
>              menu-bar-entry)

There's something odd in this code snice for each iteration of map-keymap
you end up rebinding the same key with `define-key' so only ther last one
will have an effect.

I assume `map' is some arbitrary keymap to which you want to add the entries
found in xhtml-help-mode-keymap?

Why do you use `appmenu-get-submenu' to destructure the menu-item only to
rebuild it right after.

I'd just do

      (when (featurep 'xhtml-help)
        (let ((menu-bar-entry (lookup-key xhtml-help-mode-keymap [menu-bar])))
          (when menu-bar-entry
            (map-keymap
             (lambda (key command)
               (define-key map
                           (vector (intern (concat "nxhtml-" (symbol-name key))))
                           command))
             menu-bar-entry)
            (define-key map [nxhtml-nxhtml-help-separator] (list 'menu-item
            "--"))

but maybe that's not what you're trying to do.


        Stefan

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

* Re: Format av menu keymaps
  2006-01-09 23:10     ` Stefan Monnier
@ 2006-01-09 23:57       ` Lennart Borgman
  2006-01-10  4:21         ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Lennart Borgman @ 2006-01-09 23:57 UTC (permalink / raw)
  Cc: Emacs Devel

Stefan Monnier wrote:

>>      (when (featurep 'xhtml-help)
>>        (let ((menu-bar-entry (cdr (assoc 'menu-bar xhtml-help-mode-keymap))))
>>    
>>
>
>Rather than `assoc' you should probably use lookup-key.
>  
>
Thanks.

>  
>
>>          (when menu-bar-entry
>>            (map-keymap
>>             (lambda(binding command)
>>               (let* ((tit-map (appmenu-get-submenu command))
>>                      (subtitle (car tit-map))
>>                      (submenu  (cdr tit-map)))
>>                 (define-key map
>>                   [nxhtml-xhtml-help]
>>                   (list 'menu-item
>>                         subtitle submenu
>>                         :help "XHTML help access"))))
>>             menu-bar-entry)
>>    
>>
>
>There's something odd in this code snice for each iteration of map-keymap
>you end up rebinding the same key with `define-key' so only ther last one
>will have an effect.
>  
>
Yes, it is odd. I am using map-keymap just to get the 'command'. (There 
is only one entry in menu-bar-entry.) Is there another way?

>I assume `map' is some arbitrary keymap to which you want to add the entries
>found in xhtml-help-mode-keymap?
>  
>
Yes.

>Why do you use `appmenu-get-submenu' to destructure the menu-item only to
>rebuild it right after.
>  
>
The submenu I gave is just an example. I do not know the format of the 
submenu normally. `appmenu-get-submenu' (see previous message) handles 
those two formats for a submenu that I have seen. There may be more, but 
I do not know. Are there more?

>I'd just do
>
>      (when (featurep 'xhtml-help)
>        (let ((menu-bar-entry (lookup-key xhtml-help-mode-keymap [menu-bar])))
>          (when menu-bar-entry
>            (map-keymap
>             (lambda (key command)
>               (define-key map
>                           (vector (intern (concat "nxhtml-" (symbol-name key))))
>                           command))
>             menu-bar-entry)
>            (define-key map [nxhtml-nxhtml-help-separator] (list 'menu-item
>            "--"))
>
>but maybe that's not what you're trying to do.
>  
>
It is not exactly what I want to do, but some of the syntax I can surely 
use, thanks.

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

* Re: Format av menu keymaps
  2006-01-09 23:57       ` Lennart Borgman
@ 2006-01-10  4:21         ` Stefan Monnier
       [not found]           ` <43C36D10.1050003@student.lu.se>
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2006-01-10  4:21 UTC (permalink / raw)
  Cc: Emacs Devel

>> There's something odd in this code snice for each iteration of map-keymap
>> you end up rebinding the same key with `define-key' so only ther last one
>> will have an effect.
> Yes, it is odd.  I am using map-keymap just to get the 'command'.  (There
> is only one entry in menu-bar-entry.) Is there another way?

So you assume there will only be one entry.  What happens if there are
2 entries?  If you know for sure that there'll only ever be 1 entry in that
map, is that the only thing you know about that map, or do you also know
more about it?

>> Why do you use `appmenu-get-submenu' to destructure the menu-item only to
>> rebuild it right after.
> The submenu I gave is just an example. I do not know the format of the
> submenu normally.

The question still stands: why massage the format, whichever it is: if it
works in [menu-bar] it should work where you'll use it, without touching it
at all.

> `appmenu-get-submenu' (see previous message) handles those two formats for
> a submenu that I have seen. There may be more, but I do not know.  Are
> there more?

I don't think so, although the (menu-item ...) format is sufficiently rich
that the way you handle it is incomplete.  See the elisp manual for
a complete description.


        Stefan

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

* Re: Format av menu keymaps
       [not found]             ` <jwvmzi43xn9.fsf-monnier+emacs@gnu.org>
@ 2006-01-10 17:00               ` Lennart Borgman
  2006-01-10 17:40                 ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Lennart Borgman @ 2006-01-10 17:00 UTC (permalink / raw)


Stefan Monnier wrote:

>
>>Do you mean something like this should work:
>>    
>>
>
>  
>
>>          (let ((menu-bar-entry (lookup-key emacs-lisp-mode-map
>>          [menu-bar])))
>>            (when menu-bar-entry
>>              (setq some-major t)
>>              (define-key map [appmenu-major] menu-bar-entry)))
>>    
>>
>
>No, I men something like what I sent before:
>
>            (map-keymap
>             (lambda (key command)
>               (define-key map
>                           (vector (intern (concat "nxhtml-" (symbol-name key))))
>                           command))
>             menu-bar-entry)
>
>Note how the `command' is left untouched rather than go through
>appmenu-get-submenu and then be rebuilt with menu-item.
>  
>
Thanks very much, this works perfectly, using the value returned from 
lookup-key for menu-bar-entry. I thought I tried something very similar, 
but obviously I did not. And now the code also handles the keys with 
several entries in menu-bar which I just overlooked.

Lesson one is that I have learned to use lookup-key (instead of fumbling 
with assoc). This gives less complexity and less opportunities for 
mistakes. And doing it the way above (without using appmenu-get-submenu) 
I obviously do not have to know the internal format of the submenu. So 
there was really no reason to ask here on this list. Sorry for 
disturbing here then.

But I wonder why you prefer to use

    (vector (intern (concat "appmenu-major-" (symbol-name key))))

instead of

    (read (concat "[appmenu-major-" (symbol-name key) "]"))

Just a matter of taste or? You mentioned some time ago that it normally 
was a mistake to use (read ...).

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

* Re: Format av menu keymaps
  2006-01-10 17:00               ` Lennart Borgman
@ 2006-01-10 17:40                 ` Stefan Monnier
  0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2006-01-10 17:40 UTC (permalink / raw)
  Cc: Emacs Devel

> Thanks very much, this works perfectly, using the value returned from
> lookup-key for menu-bar-entry.

Well... duh!  That's the code I sent in the first place.

> But I wonder why you prefer to use
>     (vector (intern (concat "appmenu-major-" (symbol-name key))))
> instead of
>     (read (concat "[appmenu-major-" (symbol-name key) "]"))

For example because this fails if `key' is a symbol whose name is ")".
`read' is a very high-level operation with complex semantics which are
neither useful nor harmless here.


        Stefan

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

end of thread, other threads:[~2006-01-10 17:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-08 22:27 Format av menu keymaps Lennart Borgman
2006-01-09  2:27 ` Stefan Monnier
2006-01-09 21:45   ` Lennart Borgman
2006-01-09 23:10     ` Stefan Monnier
2006-01-09 23:57       ` Lennart Borgman
2006-01-10  4:21         ` Stefan Monnier
     [not found]           ` <43C36D10.1050003@student.lu.se>
     [not found]             ` <jwvmzi43xn9.fsf-monnier+emacs@gnu.org>
2006-01-10 17:00               ` Lennart Borgman
2006-01-10 17:40                 ` 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.