help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Commands with more than one keybinding in menus


From: Thorsten Jolitz
Subject: Re: Commands with more than one keybinding in menus
Date: Wed, 14 Aug 2013 15:25:50 +0200
User-agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.3 (gnu/linux)

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Thorsten Jolitz <tjolitz@gmail.com> writes:
>
>> Its looks good so far, but I could not make easy-menu to show the
>> advertised keybindings. The whole thing looks like this:
>>
>> ,-----------------------------------------------------------------------
>> | ;; ** Menus
>> | ;; *** Advertise Bindings
>> |
>> | (put 'outline-cycle :advertised-binding [TAB])
>> | (put 'outshine-cycle-buffer :advertised-binding [BACKTAB])
>> | (put 'outline-promote :advertised-binding [M-S-left])
>> | (put 'outline-demote :advertised-binding [M-S-right])
>> | [...snip...]
>> | (put 'outline-up-heading :advertised-binding [M-\# M-u])
>> | (put 'outorg-edit-as-org :advertised-binding [M-\# M-\#])
>> |
>> | ;; *** Define Menu
>> |
>> | (easy-menu-define outshine-menu outline-minor-mode-map "Outshine menu"
>> |   '("Outshine"
>> |      ["Cycle Subtree" outline-cycle
>> |       :active (outline-on-heading-p)]
>> |       [...snip...]
>> |      ["Up Heading" outline-up-heading t]
>> |     "--"
>> |      ["Edit As Org" outorg-edit-as-org t]))
>> |
>> | ;; add "Outshine" menu item
>> | (easy-menu-add outshine-menu outline-minor-mode-map)
>> | ;; get rid of "Outline" menu item
>> | (define-key outline-minor-mode-map [menu-bar outline] 'undefined)
>> `-----------------------------------------------------------------------
>>
>> but the advertised bindings don't appear in the menu, either the
>> alternative bindings or no bindings at all show up.
>
> Looks ok to me.  But presumably Emacs doesn't match your actual bindings
> with the advertised bindings.
>
> I wonder how the actual bindings are compared with the
> :advertised-binding property - equal? (Stefan?)
>
>> I should mention that I use a specialised macro to define keybindings
>> conditional on point position, in order to not mess with user setting
>> (this is a minor-mode).
>
> Please post the according code.

Here is the macro (everything that follows is from
[[https://github.com/tj64/outshine/blob/master/outshine.el][outshine.el]]):

#+begin_src emacs-lisp
(defmacro outshine-define-key-with-fallback
  (keymap key def condition &optional mode)
  "Define key with fallback.
Binds KEY to definition DEF in keymap KEYMAP, the binding is
active when the CONDITION is true. Otherwise turns MODE off and
re-enables previous definition for KEY. If MODE is nil, tries to
recover it by stripping off \"-map\" from KEYMAP name."
  `(define-key
     ,keymap
     ,key
     (lambda (&optional arg)
       (interactive "P")
       (if ,condition ,def
         (let* ((,(if mode mode
                    (let* ((keymap-str (symbol-name keymap))
                           (mode-name-end
                            (- (string-width keymap-str) 4)))
                      (if (string=
                           "-map"
                           (substring keymap-str mode-name-end))
                          (intern (substring keymap-str 0 mode-name-end))
                        (message
                         "Could not deduce mode name from keymap name")
                        (intern "dummy-sym"))
                      )) nil)
                (original-func (key-binding ,key)))
           (condition-case nil
               (call-interactively original-func)
             (error nil)))))))
#+end_src

Here are the principal keybindings (those I want to advertise):

#+begin_src emacs-lisp
(let ((map outline-minor-mode-map))
  ;; Visibility Cycling
  (outshine-define-key-with-fallback
   map (kbd "TAB") (outline-cycle arg) (outline-on-heading-p))
  (define-key
    map (kbd "<backtab>") 'outshine-cycle-buffer)
  (outshine-define-key-with-fallback
   map (kbd "M-<left>") (outline-hide-more) (outline-on-heading-p))
  (outshine-define-key-with-fallback
   map (kbd "M-<right>") (outline-show-more) (outline-on-heading-p))
  ;; Headline Insertion
  (outshine-define-key-with-fallback
   map (kbd "M-RET") (outshine-insert-heading) (outline-on-heading-p))
  ;; Structure Editing
  (outshine-define-key-with-fallback
   map (kbd "M-S-<left>") (outline-promote) (outline-on-heading-p))
  (outshine-define-key-with-fallback
   map (kbd "M-S-<right>") (outline-demote) (outline-on-heading-p))
  (outshine-define-key-with-fallback
   map (kbd "M-S-<up>") (outline-move-subtree-up) (outline-on-heading-p))
  (outshine-define-key-with-fallback
   map (kbd "M-S-<down>") (outline-move-subtree-down) (outline-on-heading-p))
  ;; Motion
  (define-key
    map (kbd "M-<up>") 'outline-previous-visible-heading)
  (define-key
    map (kbd "M-<down>") 'outline-next-visible-heading))
#+end_src

and here an excerpt from the 'alternative' keybindings:

#+begin_src emacs-lisp
;; Set the outline-minor-mode-prefix key in your init-file
;; before loading outline-mode
(let ((map (lookup-key outline-minor-mode-map outline-minor-mode-prefix)))
  [...]
  (define-key map (kbd "I") 'outline-previous-visible-heading)
  (define-key map (kbd "K") 'outline-next-visible-heading)
  ;; for use with 'C-c' prefix
  (define-key map "\C-t" 'hide-body)
  (define-key map "\C-a" 'show-all)
  [...]
  ;; for use with 'M-#' prefix
  (define-key map "\M-t" 'hide-body)
  (define-key map "\M-a" 'show-all)
  [...]
#+end_src

I don't know how to copy the actual appearance of the "Outshine" menu item,
but it looks like this (with 'M-#' being my `outline-minor-mode-prefix' set in
my init file):

,----------------------------------
| Previous Visible Heading    M-# I
| Next Visible Heading        M-# K
`----------------------------------

and should look somehow like this when advertising works:

,----------------------------------
| Previous Visible Heading    M-<up>
| Next Visible Heading        M-<down>
`----------------------------------


--
cheers,
Thorsten




reply via email to

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