elisp简单菜单定位

时间:2014-10-29 18:28:11

标签: emacs elisp

这是将emacs用户定义的菜单放在菜单栏中的某个位置。

以下是我使用的不同选项。没有最佳

[来自第一原则]

;; Define a top level Menue item "Openflow" and position it after "Tools"

(define-key-after global-map [menu-bar openflow] 
  (cons "Openflow" (make-sparse-keymap "Openflow")) 'Tools)

;; CAUTION: Menus are defined in reverse order. END comes first!!

(define-key global-map [menu-bar openflow openflow-download-mp]   
  '("download openflow mp image to target" . openflow-download-mp-image-to-target))

(define-key global-map [menu-bar openflow telnet-to-target] 
  '("Telnet to target" . (lambda () (interactive) (telnet target))))


(define-key global-map [menu-bar openflow separator4] '("---"))
;; Reading Upwards: All debugging  etc starts here

;; But here we need to add the new subheading first
(define-key global-map [menu-bar openflow cleanups] (cons "Cleanups" (make-sparse-keymap "Cleanups")))

(define-key global-map [menu-bar openflow cleanups lp-emake-clean] 
  '("LP emake_wrapper clean" . (lambda () (interactive) 
                        (openflow-emake-wrapper  my-openflow-lp-build-path "clean"))))


;; etc etc etc...

[弱点]

非常凌乱......非常混乱的esp。反向顺序+子菜单defs按正向顺序排列: - )

简易模式太自动

如果我在简单模式下做同样的事情..

(easy-menu-define openflow2 global-map "Openflow2"
  '("Openflow2"
    ["download openflow mp image to target" openflow-download-mp-image-to-target t]
    ["Telnet to target" (telnet target)]
    "-----"
    ("Cleanups"
     ["LP emake_wrapper clean" (openflow-emake-wrapper  my-openflow-lp-build-path "clean")] )))

[弱点]

非常直观,非常自动,消除了lamda功能等等。

然而,它在“文件”之前的第一个位置添加 - 看起来很糟糕。没有办法“定义键后”

[混合模型]

“混合模型”(在稀疏键映射和easy-menu-add-item之后的define-key)很复杂但很混乱

(define-key-after global-map [menu-bar openflow3]
  (cons "Openflow3" (make-sparse-keymap "Openflow3")) 'Tools)

(easy-menu-add-item nil '("Openflow3") 
       ["download openflow mp image to target" openflow-download-mp-image-to-target])

(easy-menu-add-item nil '("Openflow3") 
    ["Telnet to target" (telnet target)])

(easy-menu-add-item nil '("Openflow3") 
    "-----")

(define-key-after global-map [menu-bar openflow3 cleanups3] 
  (cons "Cleanups3" (make-sparse-keymap "Cleanups3")) '("-----"))


(easy-menu-add-item nil '("Cleanups3") 
     ["LP emake_wrapper clean" (openflow-emake-wrapper  my-openflow-lp-build-path "clean")])

这里我不能做嵌套菜单

请告诉新手我做错了什么?

3 个答案:

答案 0 :(得分:0)

"嵌套菜单是什么意思"?如果您指的是子菜单,那么答案就是为顶级菜单做同样的事情:使用define-key将密钥绑定到 cons car 是子菜单名称,其 cdr 是菜单键映射。

以下是dired+.el的示例。它定义了Dired菜单栏菜单Mark的子菜单,该菜单位于变量diredp-menu-bar-mark-menu上。

子菜单的菜单键映射是变量diredp-mark-tags-menu的值,就像其父菜单(菜单栏菜单)的菜单键映射是变量diredp-menu-bar-mark-menu的值一样。

;; Menu-bar `Mark' Menu.
(defvar diredp-menu-bar-mark-menu (make-sparse-keymap "Mark"))
(define-key dired-mode-map [menu-bar mark] (cons "Mark" diredp-menu-bar-mark-menu))


;; `Mark' > `Tagged' submenu.
(defvar diredp-mark-tags-menu (make-sparse-keymap "Tagged")
  "`Tags' submenu for Dired menu-bar `Mark' menu.")
(define-key diredp-menu-bar-mark-menu [mark-tags] (cons "Tagged" diredp-mark-tags-menu))

(define-key diredp-mark-tags-menu [diredp-unmark-files-tagged-none]
  '(menu-item "Unmark Not Tagged with Any..." diredp-unmark-files-tagged-none
    :help "Unmark files that are not tagged with *any* of the tags you enter"))

有关详细信息,请参阅Elisp手册节点Defining Menus


如果您真的想使用简单菜单来定义子菜单(正如您在下面的评论中所说),请查看库facemenu+.el。它是这样的(对于弹出菜单,而不是菜单栏菜单,但想法是相同的。)

例如:

(easy-menu-add-item 
 facemenu-menu
 () (easy-menu-create-menu              ; (menu-name menu-items)
     "Syntax Highlighting (Font Lock)"
     '(["In All Buffers" global-font-lock-mode
        :style toggle :selected global-font-lock-mode
        :active t]
       ["In Current Buffer" font-lock-mode
        :style toggle :selected font-lock-mode :active t]
       "--"
       ["More In Current Buffer" font-lock-fontify-more
        (nth 2 (if (boundp 'flm-font-lock-fontify-level)
                   flm-font-lock-fontify-level
                 font-lock-fontify-level))]
       ["Less In Current Buffer" font-lock-fontify-less
        (nth 1 (if (boundp 'flm-font-lock-fontify-level)
                   flm-font-lock-fontify-level
                 font-lock-fontify-level))]
       "--"
       ["Cycle Keywords (Icicles)"
        icicle-next-font-lock-keywords-repeat
        :active
        (and (boundp 'icicle-mode)  icicle-mode
         (fboundp 'icicle-next-font-lock-keywords-repeat))]
       ["+ Choose Keywords (Icicles)" icicle-font-lock-keyword
        :active (and (boundp 'icicle-mode)  icicle-mode
                 (fboundp 'icicle-font-lock-keyword))]))
 "Syntax Highlighting (Font Lock)")

对于现有菜单中子菜单的相对定位,请使用define-key-after(照例)。

答案 1 :(得分:0)

我正在使用这样的东西:

(easy-menu-define openflow2 nil "Openflow2"
  '("Openflow2"
    ["download openflow mp image to target" openflow-download-mp-image-to-target t]
    ["Telnet to target" (telnet target)]
    "-----"
    ("Cleanups"
     ["LP emake_wrapper clean" (openflow-emake-wrapper  my-openflow-lp-build-path "clean")]
(define-key-after (lookup-key global-map [menu-bar])
  [openflow]
  (cons "Openflow" openflow2) 'tools)

使用easy-menu-define定义菜单,但将其放在菜单栏中的正确位置。

答案 2 :(得分:0)

可以使用Easy Menu执行类似define-key-after的操作 - 但之前就是这样。我把它添加到了我的.emacs.el:

;***
;*** Edit Menu additions

(easy-menu-define edit-occur-menu nil "Edit Occur menu"
  '("Occur"
    ["Occur..." occur]
    ["Flush Matching Lines..." flush-lines]
    ["Keep Matching Lines..." keep-lines]
    "---"
    ["How Many..." how-many]
    ))
(easy-menu-add-item nil '("Edit") edit-occur-menu "Go To")

最后一行将新edit-occur-menu添加到Edit菜单项前Go To菜单中的全局菜单栏。