Commit | Line | Data |
---|---|---|
235aa29b ER |
1 | ;;; menu-bar.el --- define a default menu bar. |
2 | ||
3 | ;; Author: RMS | |
4 | ;; Keywords: internals | |
5 | ||
1db87953 RS |
6 | ;; Copyright (C) 1993 Free Software Foundation, Inc. |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | ||
235aa29b | 24 | ;;; Code: |
1db87953 | 25 | |
b132f2b1 RM |
26 | ;; Don't clobber an existing menu-bar keymap, to preserve any menu-bar key |
27 | ;; definitions made in loaddefs.el. | |
28 | (or (lookup-key global-map [menu-bar]) | |
29 | (define-key global-map [menu-bar] (make-sparse-keymap "menu-bar"))) | |
22390f50 | 30 | (defvar menu-bar-help-menu (make-sparse-keymap "Help")) |
da183f87 RS |
31 | ;; Put Help item last. |
32 | (setq menu-bar-final-items '(help)) | |
33 | (define-key global-map [menu-bar help] (cons "Help" menu-bar-help-menu)) | |
22390f50 | 34 | (defvar menu-bar-edit-menu (make-sparse-keymap "Edit")) |
40954111 | 35 | (define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu)) |
22390f50 | 36 | (defvar menu-bar-file-menu (make-sparse-keymap "File")) |
40954111 | 37 | (define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu)) |
db774a16 | 38 | |
ca9b40a1 | 39 | (define-key menu-bar-file-menu [exit-emacs] |
db774a16 | 40 | '("Exit Emacs" . save-buffers-kill-emacs)) |
2f1139a4 RS |
41 | (define-key menu-bar-file-menu [kill-buffer] |
42 | '("Kill Buffer" . kill-this-buffer)) | |
43 | (define-key menu-bar-file-menu [delete-frame] '("Delete Frame" . delete-frame)) | |
44 | (define-key menu-bar-file-menu [print-buffer] '("Print Buffer" . print-buffer)) | |
45 | (define-key menu-bar-file-menu [revert-buffer] | |
46 | '("Revert Buffer" . revert-buffer)) | |
47 | (define-key menu-bar-file-menu [write-file] | |
48 | '("Save Buffer As..." . write-file)) | |
49 | (define-key menu-bar-file-menu [save-buffer] '("Save Buffer" . save-buffer)) | |
76202b57 | 50 | (define-key menu-bar-file-menu [dired] '("Open Directory..." . dired)) |
2f1139a4 RS |
51 | (define-key menu-bar-file-menu [open-file] '("Open File..." . find-file)) |
52 | (define-key menu-bar-file-menu [new-frame] '("New Frame" . new-frame)) | |
db774a16 | 53 | |
9e18f0a0 RS |
54 | |
55 | (define-key menu-bar-edit-menu [spell] '("Spell..." . ispell-menu-map)) | |
057d49d1 RS |
56 | (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region)) |
57 | (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region)) | |
75340584 | 58 | (define-key menu-bar-edit-menu [choose-next-paste] |
b289b04e | 59 | '("Choose Next Paste" . mouse-menu-choose-yank)) |
057d49d1 RS |
60 | (define-key menu-bar-edit-menu [paste] '("Paste" . yank)) |
61 | (define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save)) | |
62 | (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region)) | |
63 | (define-key menu-bar-edit-menu [undo] '("Undo" . undo)) | |
64 | ||
65 | (put 'fill-region 'menu-enable 'mark-active) | |
66 | (put 'kill-region 'menu-enable 'mark-active) | |
67 | (put 'kill-ring-save 'menu-enable 'mark-active) | |
68 | (put 'yank 'menu-enable '(x-selection-exists-p)) | |
69 | (put 'delete-region 'menu-enable 'mark-active) | |
70 | (put 'undo 'menu-enable '(if (eq last-command 'undo) | |
71 | pending-undo-list | |
72 | (consp buffer-undo-list))) | |
db774a16 | 73 | |
9e18f0a0 RS |
74 | (autoload 'ispell-menu-map "ispell" nil t 'keymap) |
75 | ||
ca9b40a1 | 76 | (define-key menu-bar-help-menu [emacs-tutorial] |
db774a16 | 77 | '("Emacs Tutorial" . help-with-tutorial)) |
2f1139a4 RS |
78 | (define-key menu-bar-help-menu [man] '("Man..." . manual-entry)) |
79 | (define-key menu-bar-help-menu [describe-variable] | |
80 | '("Describe Variable..." . describe-variable)) | |
81 | (define-key menu-bar-help-menu [describe-function] | |
82 | '("Describe Function..." . describe-function)) | |
83 | (define-key menu-bar-help-menu [describe-key] | |
84 | '("Describe Key..." . describe-key)) | |
85 | (define-key menu-bar-help-menu [list-keybindings] | |
86 | '("List Keybindings" . describe-bindings)) | |
87 | (define-key menu-bar-help-menu [command-apropos] | |
88 | '("Command Apropos..." . command-apropos)) | |
89 | (define-key menu-bar-help-menu [describe-mode] | |
90 | '("Describe Mode" . describe-mode)) | |
91 | (define-key menu-bar-help-menu [info] '("Info" . info)) | |
db774a16 | 92 | |
2f1139a4 | 93 | (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news)) |
db774a16 RS |
94 | (defun kill-this-buffer () ; for the menubar |
95 | "Kills the current buffer." | |
96 | (interactive) | |
97 | (kill-buffer (current-buffer))) | |
98 | ||
2f1139a4 RS |
99 | (defun kill-this-buffer-enabled-p () |
100 | (let ((count 0) | |
101 | (buffers (buffer-list))) | |
102 | (while buffers | |
103 | (or (string-match "^ " (buffer-name (car buffers))) | |
104 | (setq count (1+ count))) | |
105 | (setq buffers (cdr buffers))) | |
106 | (> count 1))) | |
107 | ||
db774a16 | 108 | (put 'save-buffer 'menu-enable '(buffer-modified-p)) |
da183f87 RS |
109 | (put 'revert-buffer 'menu-enable |
110 | '(or revert-buffer-function revert-buffer-insert-file-contents-function | |
111 | (and (buffer-file-name) | |
660fa562 RM |
112 | (or (buffer-modified-p) |
113 | (not (verify-visited-file-modtime (current-buffer))))))) | |
a0213a97 RS |
114 | ;; Permit deleting frame if it would leave a visible or iconified frame. |
115 | (put 'delete-frame 'menu-enable | |
116 | '(let ((frames (frame-list)) | |
117 | (count 0)) | |
118 | (while frames | |
119 | (if (cdr (assq 'visibility (frame-parameters (car frames)))) | |
120 | (setq count (1+ count))) | |
121 | (setq frames (cdr frames))) | |
122 | (> count 1))) | |
2f1139a4 RS |
123 | (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p)) |
124 | ||
db774a16 RS |
125 | (put 'advertised-undo 'menu-enable |
126 | '(and (not (eq t buffer-undo-list)) | |
127 | (if (eq last-command 'undo) | |
2f1139a4 RS |
128 | (and (boundp 'pending-undo-list) |
129 | pending-undo-list) | |
130 | buffer-undo-list))) | |
2877eac2 RS |
131 | |
132 | (defvar yank-menu-length 100 | |
133 | "*Maximum length of an item in the menu for \ | |
134 | \\[mouse-menu-choose-yank].") | |
135 | ||
136 | (defun mouse-menu-choose-yank (event) | |
137 | "Pop up a menu of the kill-ring for selection with the mouse. | |
138 | The kill-ring-yank-pointer is moved to the selected element. | |
139 | A subsequent \\[yank] yanks the choice just selected." | |
140 | (interactive "e") | |
141 | (let* ((count 0) | |
142 | (menu (mapcar (lambda (string) | |
143 | (if (> (length string) yank-menu-length) | |
144 | (setq string (substring string | |
145 | 0 yank-menu-length))) | |
146 | (prog1 (cons string count) | |
147 | (setq count (1+ count)))) | |
dfabc98f RM |
148 | kill-ring)) |
149 | (arg (x-popup-menu event | |
150 | (list "Yank Menu" | |
75340584 | 151 | (cons "Choose Next Yank" menu))))) |
dfabc98f RM |
152 | ;; A mouse click outside the menu returns nil. |
153 | ;; Avoid a confusing error from passing nil to rotate-yank-pointer. | |
154 | ;; XXX should this perhaps do something other than simply return? -rm | |
155 | (if arg | |
156 | (progn | |
157 | (rotate-yank-pointer arg) | |
158 | (if (interactive-p) | |
159 | (message "The next yank will insert the selected text.") | |
160 | (current-kill 0)))))) | |
161 | (put 'mouse-menu-choose-yank 'menu-enable 'kill-ring) | |
40954111 | 162 | \f |
98bee59b | 163 | (define-key global-map [menu-bar buffer] '("Buffers" . mouse-menu-bar-buffers)) |
40954111 RS |
164 | |
165 | (defvar complex-buffers-menu-p nil | |
166 | "*Non-nil says, offer a choice of actions after you pick a buffer. | |
167 | This applies to the Buffers menu from the menu bar.") | |
168 | ||
169 | (defvar buffers-menu-max-size 10 | |
170 | "*Maximum number of entries which may appear on the Buffers menu. | |
171 | If this is 10, then only the ten most-recently-selected buffers are shown. | |
172 | If this is nil, then all buffers are shown. | |
173 | A large number or nil slows down menu responsiveness.") | |
174 | ||
d0690d12 RS |
175 | (defvar list-buffers-directory nil) |
176 | ||
98bee59b | 177 | (defun mouse-menu-bar-buffers (event) |
40954111 RS |
178 | "Pop up a menu of buffers for selection with the mouse. |
179 | This switches buffers in the window that you clicked on, | |
180 | and selects that window." | |
181 | (interactive "e") | |
182 | (let ((buffers (buffer-list)) | |
183 | menu) | |
184 | ;; If requested, list only the N most recently selected buffers. | |
185 | (if (and (integerp buffers-menu-max-size) | |
186 | (> buffers-menu-max-size 1)) | |
187 | (if (> (length buffers) buffers-menu-max-size) | |
188 | (setcdr (nthcdr buffers-menu-max-size buffers) nil))) | |
189 | (setq menu | |
e936d399 RM |
190 | (cons "Select Buffer" |
191 | (let ((tail buffers) | |
192 | (maxbuf 0) | |
193 | (maxlen 0) | |
194 | head) | |
195 | (while tail | |
196 | (or (eq ?\ (aref (buffer-name (car tail)) 0)) | |
197 | (setq maxbuf | |
198 | (max maxbuf | |
199 | (length (buffer-name (car tail)))))) | |
200 | (setq tail (cdr tail))) | |
201 | (setq tail buffers) | |
202 | (while tail | |
203 | (let ((elt (car tail))) | |
204 | (if (not (string-match "^ " | |
205 | (buffer-name elt))) | |
206 | (setq head (cons | |
207 | (cons | |
208 | (format | |
209 | (format "%%%ds %%s%%s %%s" | |
210 | maxbuf) | |
211 | (buffer-name elt) | |
212 | (if (buffer-modified-p elt) | |
213 | "*" " ") | |
214 | (save-excursion | |
215 | (set-buffer elt) | |
216 | (if buffer-read-only "%" " ")) | |
75e4aa06 RS |
217 | (or (buffer-file-name elt) |
218 | (save-excursion | |
219 | (set-buffer elt) | |
220 | list-buffers-directory) | |
221 | "")) | |
e936d399 RM |
222 | elt) |
223 | head))) | |
224 | (and head (> (length (car (car head))) maxlen) | |
225 | (setq maxlen (length (car (car head)))))) | |
226 | (setq tail (cdr tail))) | |
227 | (nconc (nreverse head) | |
228 | (list (cons | |
229 | (concat (make-string (max (- (/ maxlen | |
230 | 2) | |
231 | 8) | |
232 | 0) ?\ ) | |
233 | "List All Buffers") | |
234 | 'list-buffers)))))) | |
235 | (setq menu (list menu)) | |
40954111 | 236 | |
e936d399 RM |
237 | (if (cdr (frame-list)) |
238 | (setq menu | |
239 | (cons (cons "Select Frame" | |
240 | (mapcar (lambda (frame) | |
241 | (cons (cdr (assq 'name | |
242 | (frame-parameters frame))) | |
243 | frame)) | |
244 | (frame-list))) | |
245 | menu))) | |
246 | (setq menu (cons "Buffer and Frame Menu" menu)) | |
40954111 RS |
247 | |
248 | (let ((buf (x-popup-menu (if (listp event) event | |
249 | (cons '(0 0) (selected-frame))) | |
250 | menu)) | |
251 | (window (and (listp event) (posn-window (event-start event))))) | |
e936d399 RM |
252 | (cond ((framep buf) |
253 | (make-frame-visible buf) | |
254 | (raise-frame buf) | |
255 | (select-frame buf)) | |
256 | ((eq buf 'list-buffers) | |
257 | (list-buffers)) | |
258 | (buf | |
259 | (if complex-buffers-menu-p | |
260 | (let ((action (x-popup-menu | |
261 | (if (listp event) event | |
262 | (cons '(0 0) (selected-frame))) | |
263 | '("Buffer Action" | |
264 | ("" | |
265 | ("Save Buffer" . save-buffer) | |
266 | ("Kill Buffer" . kill-buffer) | |
267 | ("Select Buffer" . switch-to-buffer)))))) | |
268 | (if (eq action 'save-buffer) | |
269 | (save-excursion | |
270 | (set-buffer buf) | |
271 | (save-buffer)) | |
272 | (funcall action buf))) | |
273 | (and (windowp window) | |
274 | (select-window window)) | |
275 | (switch-to-buffer buf))))))) | |
2f1139a4 | 276 | |
40954111 RS |
277 | ;; this version is too slow |
278 | ;;;(defun format-buffers-menu-line (buffer) | |
279 | ;;; "Returns a string to represent the given buffer in the Buffer menu. | |
280 | ;;;nil means the buffer shouldn't be listed. You can redefine this." | |
281 | ;;; (if (string-match "\\` " (buffer-name buffer)) | |
282 | ;;; nil | |
283 | ;;; (save-excursion | |
284 | ;;; (set-buffer buffer) | |
285 | ;;; (let ((size (buffer-size))) | |
286 | ;;; (format "%s%s %-19s %6s %-15s %s" | |
287 | ;;; (if (buffer-modified-p) "*" " ") | |
288 | ;;; (if buffer-read-only "%" " ") | |
289 | ;;; (buffer-name) | |
290 | ;;; size | |
291 | ;;; mode-name | |
292 | ;;; (or (buffer-file-name) "")))))) | |
293 | \f | |
057d49d1 | 294 | (defun menu-bar-mode (flag) |
dfd29450 | 295 | "Toggle display of a menu bar on each frame. |
057d49d1 RS |
296 | This command applies to all frames that exist and frames to be |
297 | created in the future. | |
298 | With a numeric argument, if the argument is negative, | |
dfd29450 | 299 | turn off menu bars; otherwise, turn on menu bars." |
dad8e392 RM |
300 | (interactive "P") |
301 | (if flag (setq flag (prefix-numeric-value flag))) | |
302 | ||
303 | ;; Obtain the current setting by looking at default-frame-alist. | |
304 | (let ((menu-bar-mode | |
305 | (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist))) | |
306 | (if assq (cdr assq) 0)))))) | |
307 | ||
308 | ;; Tweedle it according to the argument. | |
309 | (setq menu-bar-mode (if (null flag) (not menu-bar-mode) | |
310 | (or (not (numberp flag)) (>= flag 0)))) | |
311 | ||
312 | ;; Apply it to default-frame-alist. | |
313 | (let ((parameter (assq 'menu-bar-lines default-frame-alist))) | |
314 | (if (consp parameter) | |
315 | (setcdr parameter (if menu-bar-mode 1 0)) | |
316 | (setq default-frame-alist | |
317 | (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0)) | |
318 | default-frame-alist)))) | |
319 | ||
320 | ;; Apply it to existing frames. | |
321 | (let ((frames (frame-list))) | |
322 | (while frames | |
323 | (modify-frame-parameters (car frames) | |
324 | (list (cons 'menu-bar-lines | |
325 | (if menu-bar-mode 1 0)))) | |
326 | (setq frames (cdr frames)))))) | |
1db87953 RS |
327 | |
328 | ;; Make frames created from now on have a menu bar. | |
d0dbd3bf RS |
329 | (if window-system |
330 | (menu-bar-mode t)) | |
1db87953 | 331 | |
bffa5d4d RS |
332 | (provide 'menu-bar) |
333 | ||
235aa29b | 334 | ;;; menu-bar.el ends here |