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