-
Notifications
You must be signed in to change notification settings - Fork 1
/
ee-menubar.el
executable file
·200 lines (164 loc) · 6.59 KB
/
ee-menubar.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
;;; ee-menubar.el --- categorized access to Emacs menu-bar
;; Copyright (C) 2002, 2003, 2004, 2010 Juri Linkov <juri@jurta.org>
;; Author: Juri Linkov <juri@jurta.org>
;; Keywords: ee, tools
;; This file is [not yet] part of GNU Emacs.
;; This package is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This package is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this package; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; See the file README and documentation for more information.
;;; Suggested keybindings:
;; (define-key global-map "\M-`" 'ee-menubar)
;; (define-key global-map [f10] 'ee-menubar)
;;; Code:
(require 'ee)
(eval-when-compile
(require 'tmm nil t))
;;; Constants
(defconst ee-menubar-mode-name "ee-menubar")
;;; Customizable Variables
(defgroup ee-menubar nil
"Categorized access to Emacs menu-bar."
:prefix "ee-menubar-"
:group 'ee)
(defcustom ee-menubar-record-flag nil
"Put the called command in the command-history."
:type 'boolean
:group 'ee-menubar)
;;; Data Description
(defvar ee-menubar-data
'[(meta
(format-version . "0.0.1")
(database-version . "0.0.1")
(view-data-file . "view/menubar.ee")
(collector . ee-menubar-data-collect)
(fields category name title level command help event binding ())
(key-fields name category))])
;;; Data Extraction
(defun ee-menubar-data-collect (data)
(let ((old-data (if ee-data
(mapcar (lambda (r)
(ee-field 'name r))
(ee-data-records-find ee-data '(read . t)))))
(new-data
(ee-data-convert-lists-to-vectors
(ee-menubar-data-collect-items
(ee-data-field-names data)
(tmm-get-keybind [menu-bar])
0))))
(aset new-data 0 (aref data 0))
(if old-data
(ee-data-records-do
new-data
(lambda (r ri)
(if (member (ee-field 'name r) old-data)
(ee-field-set 'read t r (ee-data-meta-setters-get-set new-data))))))
new-data))
(defun ee-menubar-data-collect-items (field-names menu level)
(if (keymapp menu)
(mapcan
(lambda (item)
(if (consp item)
(let* ((name (car-safe item))
(title (if (eq (car-safe (cdr-safe item)) 'menu-item)
(car (cdr-safe (cdr-safe item)))
(car-safe (cdr-safe item))))
(keymap (cond ((keymapp (cdr-safe (cdr-safe item)))
(indirect-function (cdr (cdr item))))
((keymapp (car-safe (cdr-safe (cdr-safe (cdr-safe item)))))
(indirect-function (car (cdr (cdr (cdr item))))))))
(command (or keymap
(car-safe (cdr-safe (cdr-safe (cdr-safe item)))))))
(append
(if (and name title)
(list (mapcar (lambda (field-name)
(cond
((eq field-name 'name) name)
((eq field-name 'title) title)
((eq field-name 'level) level)
((eq field-name 'command) command)))
field-names)))
(if keymap
(ee-menubar-data-collect-items
field-names keymap (1+ level)))))))
(reverse (cdr menu)))))
;; this is the same as ee-outline-c-tree-builder - TODO: make generic function
(defun ee-menubar-c-tree-builder (&optional record-index)
;; input: [[level1 "name1" apos1 bpos1] [level2 "name2" apos2 bpos2] ...]
;; output: (1 (2 ...) ...)
;; with assumption that order of input vector elements corresponds to tree pre-order
(let ((data ee-data)
(level -1))
(with-temp-buffer
(insert (format "(%s" ee-mark-record-tree))
(ee-data-records-do
data
(lambda (r ri)
(let* ((elt-level (ee-field 'level r))
(diff (- elt-level level)))
(if (> diff 0)
(dotimes (i (- diff 1)) (insert "(nil "))
(dotimes (i (+ (abs diff) 1)) (insert ")")))
(insert "(")
(insert (format "%s" ri))
(setq level elt-level))))
(dotimes (i (+ level 1)) (insert ")"))
(insert ")")
(goto-char (point-min))
(read (current-buffer)))))
;;; Actions
(defun ee-menubar-select (&optional arg)
(interactive)
(if (ee-view-on-expansion-line-p)
(ee-view-expansion-show-or-hide)
(let ((command (ee-field 'command))
(parent-buffer ee-parent-buffer))
(ee-field-set 'read t)
(ee-view-update '(read)) ;; (ee-view-record-update)
(when (and command parent-buffer)
(switch-to-buffer parent-buffer)
(call-interactively command ee-menubar-record-flag)))))
(defun ee-menubar-execute (r marks)
(interactive))
;;; Key Bindings
(defvar ee-menubar-keymap nil
"Local keymap for ee-menubar mode.")
(defun ee-menubar-keymap-make-default ()
"Defines default key bindings for `ee-menubar-keymap'.
It inherits key bindings from `ee-mode-map'."
(or ee-mode-map
(ee-mode-map-make-default))
(let ((map (copy-keymap ee-mode-map)))
;; (define-key map "o" 'ee-menubar-select)
(setq ee-menubar-keymap map)))
(or ee-menubar-keymap
(ee-menubar-keymap-make-default))
;;; Top-Level Functions
;;;###autoload
(defun ee-menubar (&optional arg)
"Categorized access to Emacs menu-bar."
(interactive "P")
(require 'tmm)
;; TODO: remove next two lines, when submenus will be implemented
;; as categories (not records) in the ee-menubar-c-tree-builder,
;; so ee-hidden-expansions could be used to retain opened menus
(if (get-buffer (format "*%s*" ee-menubar-mode-name))
(switch-to-buffer (format "*%s*" ee-menubar-mode-name))
(ee-view-buffer-create
(format "*%s*" ee-menubar-mode-name)
ee-menubar-mode-name
ee-menubar-keymap
ee-menubar-data)))
;;;###autoload (defalias 'ee-tmm 'ee-menubar)
(provide 'ee-menubar)
;;; ee-menubar.el ends here