Commit | Line | Data |
---|---|---|
235aa29b ER |
1 | ;;; menu-bar.el --- define a default menu bar. |
2 | ||
3 | ;; Author: RMS | |
b7f66977 | 4 | ;; Keywords: internal |
235aa29b | 5 | |
f8c25f1b | 6 | ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. |
1db87953 RS |
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")) |
ffbdd83d RS |
31 | |
32 | ;; Force Help item to come last, after the major mode's own items. | |
da183f87 | 33 | (setq menu-bar-final-items '(help)) |
ffbdd83d | 34 | |
da183f87 | 35 | (define-key global-map [menu-bar help] (cons "Help" menu-bar-help-menu)) |
ffbdd83d RS |
36 | (defvar menu-bar-search-menu (make-sparse-keymap "Search")) |
37 | (define-key global-map [menu-bar search] (cons "Search" menu-bar-search-menu)) | |
22390f50 | 38 | (defvar menu-bar-edit-menu (make-sparse-keymap "Edit")) |
40954111 | 39 | (define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu)) |
ffbdd83d RS |
40 | (defvar menu-bar-tools-menu (make-sparse-keymap "Tools")) |
41 | (define-key global-map [menu-bar tools] (cons "Tools" menu-bar-tools-menu)) | |
42 | (defvar menu-bar-files-menu (make-sparse-keymap "Files")) | |
43 | (define-key global-map [menu-bar files] (cons "Files" menu-bar-files-menu)) | |
1b009b97 RS |
44 | |
45 | ;; This alias is for compatibility with 19.28 and before. | |
46 | (defvar menu-bar-file-menu menu-bar-files-menu) | |
9bb8e471 | 47 | \f |
9dcdc43d RS |
48 | (defvar vc-menu-map (make-sparse-keymap "Version Control")) |
49 | ||
e1726722 | 50 | (define-key menu-bar-tools-menu [calendar] '("Display Calendar" . calendar)) |
ffbdd83d RS |
51 | (define-key menu-bar-tools-menu [rmail] '("Read Mail" . rmail)) |
52 | (define-key menu-bar-tools-menu [gnus] '("Read Net News" . gnus)) | |
53 | ||
54 | (define-key menu-bar-tools-menu [separator-vc] | |
55 | '("--")) | |
56 | ||
57 | (define-key menu-bar-tools-menu [vc-menu] | |
58 | (cons "Version Control" vc-menu-map)) | |
9bb8e471 | 59 | |
ffbdd83d | 60 | (define-key menu-bar-tools-menu [separator-compare] |
9bb8e471 RS |
61 | '("--")) |
62 | ||
ffbdd83d | 63 | (define-key menu-bar-tools-menu [epatch] |
080ac1ac | 64 | '("Apply Patch" . menu-bar-epatch-menu)) |
ffbdd83d | 65 | (define-key menu-bar-tools-menu [ediff-merge] |
5438ecd3 | 66 | '("Merge" . menu-bar-ediff-merge-menu)) |
ffbdd83d | 67 | (define-key menu-bar-tools-menu [ediff] |
40a8cde1 | 68 | '("Compare" . menu-bar-ediff-menu)) |
9bb8e471 | 69 | |
ffbdd83d | 70 | (define-key menu-bar-tools-menu [separator-print] |
9bb8e471 RS |
71 | '("--")) |
72 | ||
ffbdd83d RS |
73 | (put 'print-region 'menu-enable 'mark-active) |
74 | (put 'ps-print-region-with-faces 'menu-enable 'mark-active) | |
75 | ||
76 | (define-key menu-bar-tools-menu [ps-print-region] | |
77 | '("Postscript Print Region" . ps-print-region-with-faces)) | |
78 | (define-key menu-bar-tools-menu [ps-print-buffer] | |
79 | '("Postscript Print Buffer" . ps-print-buffer-with-faces)) | |
80 | (define-key menu-bar-tools-menu [print-region] | |
81 | '("Print Region" . print-region)) | |
82 | (define-key menu-bar-tools-menu [print-buffer] | |
83 | '("Print Buffer" . print-buffer)) | |
84 | \f | |
85 | (define-key menu-bar-files-menu [exit-emacs] | |
86 | '("Exit Emacs" . save-buffers-kill-emacs)) | |
87 | ||
88 | (define-key menu-bar-files-menu [separator-exit] | |
89 | '("--")) | |
90 | ||
91 | (define-key menu-bar-files-menu [one-window] | |
92 | '("One Window" . delete-other-windows)) | |
93 | ||
94 | (define-key menu-bar-files-menu [split-window] | |
95 | '("Split Window" . split-window-vertically)) | |
9bb8e471 | 96 | |
ca1a9692 RS |
97 | (if (fboundp 'delete-frame) |
98 | (progn | |
ffbdd83d | 99 | (define-key menu-bar-files-menu [delete-frame] |
ca1a9692 | 100 | '("Delete Frame" . delete-frame)) |
ffbdd83d RS |
101 | (define-key menu-bar-files-menu [make-frame-on-display] |
102 | '("Open New Display..." . make-frame-on-display)) | |
103 | (define-key menu-bar-files-menu [make-frame] | |
ca1a9692 | 104 | '("Make New Frame" . make-frame)))) |
9bb8e471 | 105 | |
ffbdd83d | 106 | (define-key menu-bar-files-menu [separator-buffers] |
9bb8e471 RS |
107 | '("--")) |
108 | ||
ffbdd83d RS |
109 | (define-key menu-bar-files-menu [kill-buffer] |
110 | '("Kill Current Buffer" . kill-this-buffer)) | |
111 | (define-key menu-bar-files-menu [insert-file] | |
df4d8e01 | 112 | '("Insert File" . insert-file)) |
ffbdd83d | 113 | (define-key menu-bar-files-menu [revert-buffer] |
2f1139a4 | 114 | '("Revert Buffer" . revert-buffer)) |
ffbdd83d | 115 | (define-key menu-bar-files-menu [write-file] |
2f1139a4 | 116 | '("Save Buffer As..." . write-file)) |
ffbdd83d RS |
117 | (define-key menu-bar-files-menu [save-buffer] '("Save Buffer" . save-buffer)) |
118 | (define-key menu-bar-files-menu [dired] '("Open Directory..." . dired)) | |
119 | (define-key menu-bar-files-menu [open-file] '("Open File..." . find-file)) | |
40a8cde1 RS |
120 | |
121 | ;; This is just one element of the ediff menu--the first. | |
122 | (define-key menu-bar-ediff-menu [window] | |
123 | '("This Window And Next Window" . compare-windows)) | |
9bb8e471 | 124 | \f |
ffbdd83d RS |
125 | (defun nonincremental-search-forward (string) |
126 | "Read a string and search for it nonincrementally." | |
127 | (interactive "sSearch for string: ") | |
128 | (if (equal string "") | |
129 | (search-forward (car search-ring)) | |
130 | (isearch-update-ring string nil) | |
131 | (search-forward string))) | |
132 | ||
133 | (defun nonincremental-search-backward (string) | |
134 | "Read a string and search backward for it nonincrementally." | |
135 | (interactive "sSearch for string: ") | |
136 | (if (equal string "") | |
137 | (search-backward (car search-ring)) | |
138 | (isearch-update-ring string nil) | |
139 | (search-backward string))) | |
140 | ||
141 | (defun nonincremental-re-search-forward (string) | |
142 | "Read a regular expression and search for it nonincrementally." | |
143 | (interactive "sSearch for regexp: ") | |
144 | (if (equal string "") | |
145 | (re-search-forward (car regexp-search-ring)) | |
146 | (isearch-update-ring string t) | |
147 | (re-search-forward string))) | |
148 | ||
149 | (defun nonincremental-re-search-backward (string) | |
150 | "Read a regular expression and search backward for it nonincrementally." | |
151 | (interactive "sSearch for regexp: ") | |
152 | (if (equal string "") | |
153 | (re-search-backward (car regexp-search-ring)) | |
154 | (isearch-update-ring string t) | |
155 | (re-search-backward string))) | |
156 | ||
157 | (defun noninteractive-repeat-search-forward () | |
158 | "Search forward for the previous search string." | |
159 | (interactive) | |
160 | (search-forward (car search-ring))) | |
161 | ||
162 | (defun noninteractive-repeat-search-backward () | |
163 | "Search backward for the previous search string." | |
164 | (interactive) | |
165 | (search-backward (car search-ring))) | |
166 | ||
167 | (defun noninteractive-repeat-re-search-forward () | |
168 | "Search forward for the previous regular expression." | |
169 | (interactive) | |
170 | (re-search-forward (car regexp-search-ring))) | |
171 | ||
172 | (defun noninteractive-repeat-re-search-backward () | |
173 | "Search backward for the previous regular expression." | |
174 | (interactive) | |
175 | (re-search-backward (car regexp-search-ring))) | |
176 | ||
177 | (define-key menu-bar-search-menu [query-replace] | |
33aa8946 | 178 | '("Query Replace" . query-replace)) |
ffbdd83d RS |
179 | (define-key menu-bar-search-menu [find-tag] |
180 | '("Find Tag" . find-tag)) | |
181 | (put 'find-tag 'menu-enable 'tags-table-list) | |
182 | (define-key menu-bar-search-menu [bookmark] | |
183 | '("Bookmarks" . menu-bar-bookmark-map)) | |
184 | ||
185 | (define-key menu-bar-search-menu [separator-search] | |
186 | '("--")) | |
187 | ||
188 | (define-key menu-bar-search-menu [nonincremental-repeat-re-search-back] | |
189 | '("Repeat Regexp Backwards" . nonincremental-repeat-re-search-backward)) | |
190 | (define-key menu-bar-search-menu [nonincremental-repeat-search-back] | |
191 | '("Repeat Backwards" . nonincremental-repeat-search-backward)) | |
192 | (define-key menu-bar-search-menu [nonincremental-repeat-re-search-fwd] | |
193 | '("Repeat Regexp" . nonincremental-repeat-re-search-forward)) | |
194 | (define-key menu-bar-search-menu [nonincremental-repeat-search-fwd] | |
195 | '("Repeat Search" . nonincremental-repeat-search-forward)) | |
196 | ||
197 | (define-key menu-bar-search-menu [separator-repeat] | |
9bb8e471 RS |
198 | '("--")) |
199 | ||
ffbdd83d RS |
200 | (define-key menu-bar-search-menu [re-search-back] |
201 | '("Regexp Search Backwards" . nonincremental-re-search-backward)) | |
202 | (define-key menu-bar-search-menu [search-back] | |
203 | '("Search Backwards" . nonincremental-search-backward)) | |
204 | (define-key menu-bar-search-menu [re-search-fwd] | |
205 | '("Regexp Search" . nonincremental-re-search-forward)) | |
206 | (define-key menu-bar-search-menu [search-fwd] | |
207 | '("Search" . nonincremental-search-forward)) | |
208 | \f | |
9bb8e471 RS |
209 | (define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map)) |
210 | (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region)) | |
6507ffb1 | 211 | (define-key menu-bar-edit-menu [props] '("Text Properties" . facemenu-menu)) |
9bb8e471 RS |
212 | |
213 | (define-key menu-bar-edit-menu [separator-edit] | |
214 | '("--")) | |
215 | ||
216 | (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region)) | |
3dd92899 | 217 | |
e1726722 | 218 | (define-key menu-bar-edit-menu [paste] '("Paste Most Recent" . yank)) |
3dd92899 KH |
219 | |
220 | (defvar yank-menu (cons "Select Yank" nil)) | |
221 | (fset 'yank-menu (cons 'keymap yank-menu)) | |
222 | (define-key menu-bar-edit-menu [select-paste] '("Select and Paste" . yank-menu)) | |
4c0317b1 | 223 | (define-key menu-bar-edit-menu [copy] '("Copy" . menu-bar-kill-ring-save)) |
057d49d1 RS |
224 | (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region)) |
225 | (define-key menu-bar-edit-menu [undo] '("Undo" . undo)) | |
226 | ||
4c0317b1 RS |
227 | (defun menu-bar-kill-ring-save (beg end) |
228 | (interactive "r") | |
229 | (if (mouse-region-match) | |
230 | (message "Select a region with the mouse does `copy' automatically") | |
231 | (kill-ring-save beg end))) | |
232 | ||
057d49d1 RS |
233 | (put 'fill-region 'menu-enable 'mark-active) |
234 | (put 'kill-region 'menu-enable 'mark-active) | |
4c0317b1 | 235 | (put 'menu-bar-kill-ring-save 'menu-enable 'mark-active) |
057d49d1 | 236 | (put 'yank 'menu-enable '(x-selection-exists-p)) |
3dd92899 | 237 | (put 'yank-menu 'menu-enable '(cdr yank-menu)) |
4c0317b1 RS |
238 | (put 'delete-region 'menu-enable '(and mark-active |
239 | (not (mouse-region-match)))) | |
057d49d1 RS |
240 | (put 'undo 'menu-enable '(if (eq last-command 'undo) |
241 | pending-undo-list | |
242 | (consp buffer-undo-list))) | |
3a841b0b | 243 | (put 'query-replace 'menu-enable '(not buffer-read-only)) |
db774a16 | 244 | |
9e18f0a0 RS |
245 | (autoload 'ispell-menu-map "ispell" nil t 'keymap) |
246 | ||
f9cf0be2 | 247 | ;; These are alternative definitions for the cut, paste and copy |
4c0317b1 | 248 | ;; menu items. Use them if your system expects these to use the clipboard. |
f9cf0be2 | 249 | |
f9cf0be2 RS |
250 | (put 'clipboard-kill-region 'menu-enable 'mark-active) |
251 | (put 'clipboard-kill-ring-save 'menu-enable 'mark-active) | |
252 | (put 'clipboard-yank 'menu-enable | |
253 | '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD))) | |
254 | ||
255 | (defun clipboard-yank () | |
256 | "Reinsert the last stretch of killed text, or the clipboard contents." | |
257 | (interactive) | |
258 | (let ((x-select-enable-clipboard t)) | |
259 | (yank))) | |
260 | ||
261 | (defun clipboard-kill-ring-save (beg end) | |
262 | "Copy region to kill ring, and save in the X clipboard." | |
263 | (interactive "r") | |
264 | (let ((x-select-enable-clipboard t)) | |
265 | (kill-ring-save beg end))) | |
266 | ||
267 | (defun clipboard-kill-region (beg end) | |
268 | "Kill the region, and save it in the X clipboard." | |
269 | (interactive "r") | |
270 | (let ((x-select-enable-clipboard t)) | |
271 | (kill-region beg end))) | |
272 | ||
273 | (defun menu-bar-enable-clipboard () | |
5cbdeb30 RS |
274 | "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard. |
275 | Do the same for the keys of the same name." | |
f9cf0be2 | 276 | (interactive) |
5754d7f2 RS |
277 | ;; We can't use constant list structure here because it becomes pure, |
278 | ;; and because it gets modified with cache data. | |
279 | (define-key menu-bar-edit-menu [paste] | |
280 | (cons "Paste" 'clipboard-yank)) | |
281 | (define-key menu-bar-edit-menu [copy] | |
282 | (cons "Copy" 'clipboard-kill-ring-save)) | |
283 | (define-key menu-bar-edit-menu [cut] | |
5cbdeb30 RS |
284 | (cons "Cut" 'clipboard-kill-region)) |
285 | ||
286 | (define-key global-map [f20] 'clipboard-kill-region) | |
287 | (define-key global-map [f16] 'clipboard-kill-ring-save) | |
288 | (define-key global-map [f18] 'clipboard-yank) | |
289 | ;; X11R6 versions | |
290 | (define-key global-map [cut] 'clipboard-kill-region) | |
291 | (define-key global-map [copy] 'clipboard-kill-ring-save) | |
292 | (define-key global-map [paste] 'clipboard-yank)) | |
f9cf0be2 | 293 | \f |
efb166ff RS |
294 | (define-key menu-bar-help-menu [emacs-version] |
295 | '("Show Version" . emacs-version)) | |
48433a65 RS |
296 | (define-key menu-bar-help-menu [report-emacs-bug] |
297 | '("Send Bug Report" . report-emacs-bug)) | |
ca9b40a1 | 298 | (define-key menu-bar-help-menu [emacs-tutorial] |
db774a16 | 299 | '("Emacs Tutorial" . help-with-tutorial)) |
2f1139a4 RS |
300 | (define-key menu-bar-help-menu [man] '("Man..." . manual-entry)) |
301 | (define-key menu-bar-help-menu [describe-variable] | |
302 | '("Describe Variable..." . describe-variable)) | |
303 | (define-key menu-bar-help-menu [describe-function] | |
304 | '("Describe Function..." . describe-function)) | |
305 | (define-key menu-bar-help-menu [describe-key] | |
306 | '("Describe Key..." . describe-key)) | |
307 | (define-key menu-bar-help-menu [list-keybindings] | |
308 | '("List Keybindings" . describe-bindings)) | |
309 | (define-key menu-bar-help-menu [command-apropos] | |
310 | '("Command Apropos..." . command-apropos)) | |
311 | (define-key menu-bar-help-menu [describe-mode] | |
312 | '("Describe Mode" . describe-mode)) | |
e1726722 | 313 | (define-key menu-bar-help-menu [info] '("Browse Manuals" . info)) |
889560ed | 314 | (define-key menu-bar-help-menu [emacs-faq] '("Emacs FAQ" . view-emacs-FAQ)) |
2f1139a4 | 315 | (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news)) |
889560ed | 316 | |
db774a16 RS |
317 | (defun kill-this-buffer () ; for the menubar |
318 | "Kills the current buffer." | |
319 | (interactive) | |
320 | (kill-buffer (current-buffer))) | |
321 | ||
2f1139a4 RS |
322 | (defun kill-this-buffer-enabled-p () |
323 | (let ((count 0) | |
324 | (buffers (buffer-list))) | |
325 | (while buffers | |
326 | (or (string-match "^ " (buffer-name (car buffers))) | |
327 | (setq count (1+ count))) | |
328 | (setq buffers (cdr buffers))) | |
329 | (> count 1))) | |
330 | ||
db774a16 | 331 | (put 'save-buffer 'menu-enable '(buffer-modified-p)) |
da183f87 RS |
332 | (put 'revert-buffer 'menu-enable |
333 | '(or revert-buffer-function revert-buffer-insert-file-contents-function | |
334 | (and (buffer-file-name) | |
660fa562 RM |
335 | (or (buffer-modified-p) |
336 | (not (verify-visited-file-modtime (current-buffer))))))) | |
a0213a97 RS |
337 | ;; Permit deleting frame if it would leave a visible or iconified frame. |
338 | (put 'delete-frame 'menu-enable | |
339 | '(let ((frames (frame-list)) | |
340 | (count 0)) | |
341 | (while frames | |
342 | (if (cdr (assq 'visibility (frame-parameters (car frames)))) | |
343 | (setq count (1+ count))) | |
344 | (setq frames (cdr frames))) | |
345 | (> count 1))) | |
2f1139a4 RS |
346 | (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p)) |
347 | ||
db774a16 RS |
348 | (put 'advertised-undo 'menu-enable |
349 | '(and (not (eq t buffer-undo-list)) | |
350 | (if (eq last-command 'undo) | |
2f1139a4 RS |
351 | (and (boundp 'pending-undo-list) |
352 | pending-undo-list) | |
353 | buffer-undo-list))) | |
2877eac2 | 354 | |
c3e1d435 | 355 | (defvar yank-menu-length 20 |
3dd92899 KH |
356 | "*Maximum length to display in the yank-menu.") |
357 | ||
358 | (defun menu-bar-update-yank-menu (string old) | |
359 | (let ((front (car (cdr yank-menu))) | |
360 | (menu-string (if (<= (length string) yank-menu-length) | |
361 | string | |
c3e1d435 RS |
362 | (concat |
363 | (substring string 0 (/ yank-menu-length 2)) | |
364 | "..." | |
365 | (substring string (- (/ yank-menu-length 2))))))) | |
3dd92899 KH |
366 | ;; If we're supposed to be extending an existing string, and that |
367 | ;; string really is at the front of the menu, then update it in place. | |
368 | (if (and old (or (eq old (car front)) | |
369 | (string= old (car front)))) | |
dfabc98f | 370 | (progn |
3dd92899 KH |
371 | (setcar front string) |
372 | (setcar (cdr front) menu-string)) | |
373 | (setcdr yank-menu | |
374 | (cons | |
375 | (cons string (cons menu-string 'menu-bar-select-yank)) | |
376 | (cdr yank-menu))))) | |
377 | (if (> (length (cdr yank-menu)) kill-ring-max) | |
378 | (setcdr (nthcdr kill-ring-max yank-menu) nil))) | |
379 | ||
380 | (defun menu-bar-select-yank () | |
381 | (interactive "*") | |
382 | (push-mark (point)) | |
383 | (insert last-command-event)) | |
40954111 | 384 | \f |
09642d97 RS |
385 | (define-key global-map [menu-bar buffer] '("Buffers" . menu-bar-buffers)) |
386 | ||
387 | (defalias 'menu-bar-buffers (make-sparse-keymap "Buffers")) | |
40954111 | 388 | |
40954111 RS |
389 | (defvar buffers-menu-max-size 10 |
390 | "*Maximum number of entries which may appear on the Buffers menu. | |
391 | If this is 10, then only the ten most-recently-selected buffers are shown. | |
392 | If this is nil, then all buffers are shown. | |
393 | A large number or nil slows down menu responsiveness.") | |
394 | ||
d0690d12 RS |
395 | (defvar list-buffers-directory nil) |
396 | ||
08e8171f RS |
397 | (defvar menu-bar-update-buffers-maxbuf) |
398 | ||
09642d97 RS |
399 | (defun menu-bar-select-buffer () |
400 | (interactive) | |
401 | (switch-to-buffer last-command-event)) | |
402 | ||
403 | (defun menu-bar-select-frame () | |
404 | (interactive) | |
405 | (make-frame-visible last-command-event) | |
406 | (raise-frame last-command-event) | |
407 | (select-frame last-command-event)) | |
408 | ||
c171b42f RS |
409 | (defun menu-bar-update-buffers-1 (elt) |
410 | (cons (format | |
08e8171f | 411 | (format "%%%ds %%s%%s %%s" menu-bar-update-buffers-maxbuf) |
c171b42f RS |
412 | (cdr elt) |
413 | (if (buffer-modified-p (car elt)) | |
414 | "*" " ") | |
415 | (save-excursion | |
416 | (set-buffer (car elt)) | |
417 | (if buffer-read-only "%" " ")) | |
418 | (let ((file | |
419 | (or (buffer-file-name (car elt)) | |
420 | (save-excursion | |
421 | (set-buffer (car elt)) | |
422 | list-buffers-directory) | |
423 | ""))) | |
424 | (setq file (or (file-name-directory file) | |
425 | "")) | |
426 | (if (> (length file) 20) | |
427 | (setq file (concat "..." (substring file -17)))) | |
428 | file)) | |
429 | (car elt))) | |
430 | ||
09642d97 | 431 | (defun menu-bar-update-buffers () |
29397c58 | 432 | ;; If user discards the Buffers item, play along. |
4d587a6c | 433 | (and (lookup-key (current-global-map) [menu-bar buffer]) |
29397c58 RS |
434 | (frame-or-buffer-changed-p) |
435 | (let ((buffers (buffer-list)) | |
436 | (frames (frame-list)) | |
437 | buffers-menu frames-menu) | |
438 | ;; If requested, list only the N most recently selected buffers. | |
439 | (if (and (integerp buffers-menu-max-size) | |
440 | (> buffers-menu-max-size 1)) | |
441 | (if (> (length buffers) buffers-menu-max-size) | |
442 | (setcdr (nthcdr buffers-menu-max-size buffers) nil))) | |
443 | ||
444 | ;; Make the menu of buffers proper. | |
445 | (setq buffers-menu | |
446 | (cons "Select Buffer" | |
c171b42f RS |
447 | (let* ((buffer-list |
448 | (mapcar 'list buffers)) | |
449 | tail | |
08e8171f | 450 | (menu-bar-update-buffers-maxbuf 0) |
c171b42f RS |
451 | (maxlen 0) |
452 | alist | |
453 | head) | |
454 | ;; Put into each element of buffer-list | |
455 | ;; the name for actual display, | |
456 | ;; perhaps truncated in the middle. | |
457 | (setq tail buffer-list) | |
458 | (while tail | |
459 | (let ((name (buffer-name (car (car tail))))) | |
460 | (setcdr (car tail) | |
461 | (if (> (length name) 27) | |
462 | (concat (substring name 0 12) | |
463 | "..." | |
464 | (substring name -12)) | |
465 | name))) | |
466 | (setq tail (cdr tail))) | |
467 | ;; Compute the maximum length of any name. | |
468 | (setq tail buffer-list) | |
29397c58 | 469 | (while tail |
c171b42f | 470 | (or (eq ?\ (aref (cdr (car tail)) 0)) |
08e8171f RS |
471 | (setq menu-bar-update-buffers-maxbuf |
472 | (max menu-bar-update-buffers-maxbuf | |
c171b42f | 473 | (length (cdr (car tail)))))) |
29397c58 | 474 | (setq tail (cdr tail))) |
c171b42f RS |
475 | ;; Set ALIST to an alist of the form |
476 | ;; ITEM-STRING . BUFFER | |
477 | (setq tail buffer-list) | |
29397c58 RS |
478 | (while tail |
479 | (let ((elt (car tail))) | |
c171b42f | 480 | (or (eq ?\ (aref (cdr elt) 0)) |
29397c58 | 481 | (setq alist (cons |
c171b42f | 482 | (menu-bar-update-buffers-1 elt) |
29397c58 RS |
483 | alist))) |
484 | (and alist (> (length (car (car alist))) maxlen) | |
485 | (setq maxlen (length (car (car alist)))))) | |
486 | (setq tail (cdr tail))) | |
487 | (setq alist (nreverse alist)) | |
488 | (nconc (mapcar '(lambda (pair) | |
489 | ;; This is somewhat risque, to use | |
490 | ;; the buffer name itself as the event | |
491 | ;; type to define, but it works. | |
492 | ;; It would not work to use the buffer | |
493 | ;; since a buffer as an event has its | |
494 | ;; own meaning. | |
495 | (nconc (list (buffer-name (cdr pair)) | |
496 | (car pair) | |
497 | (cons nil nil)) | |
498 | 'menu-bar-select-buffer)) | |
499 | alist) | |
500 | (list | |
096ec7e7 | 501 | (cons |
29397c58 RS |
502 | 'list-buffers |
503 | (cons | |
504 | (concat (make-string (max (- (/ maxlen 2) 8) 0) | |
505 | ?\ ) | |
506 | "List All Buffers") | |
507 | 'list-buffers))))))) | |
508 | ||
509 | ||
510 | ;; Make a Frames menu if we have more than one frame. | |
511 | (if (cdr frames) | |
512 | (setq frames-menu | |
513 | (cons "Select Frame" | |
514 | (mapcar '(lambda (frame) | |
515 | (nconc (list frame | |
516 | (cdr (assq 'name | |
517 | (frame-parameters frame))) | |
518 | (cons nil nil)) | |
519 | 'menu-bar-select-frame)) | |
520 | frames)))) | |
521 | (if buffers-menu | |
522 | (setq buffers-menu (cons 'keymap buffers-menu))) | |
523 | (if frames-menu | |
524 | (setq frames-menu (cons 'keymap frames-menu))) | |
4d587a6c | 525 | (define-key (current-global-map) [menu-bar buffer] |
29397c58 RS |
526 | (cons "Buffers" |
527 | (if (and buffers-menu frames-menu) | |
528 | (list 'keymap "Buffers and Frames" | |
529 | (cons 'buffers (cons "Buffers" buffers-menu)) | |
530 | (cons 'frames (cons "Frames" frames-menu))) | |
531 | (or buffers-menu frames-menu 'undefined))))))) | |
09642d97 RS |
532 | |
533 | (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers) | |
2f1139a4 | 534 | |
40954111 RS |
535 | ;; this version is too slow |
536 | ;;;(defun format-buffers-menu-line (buffer) | |
537 | ;;; "Returns a string to represent the given buffer in the Buffer menu. | |
538 | ;;;nil means the buffer shouldn't be listed. You can redefine this." | |
539 | ;;; (if (string-match "\\` " (buffer-name buffer)) | |
540 | ;;; nil | |
541 | ;;; (save-excursion | |
542 | ;;; (set-buffer buffer) | |
543 | ;;; (let ((size (buffer-size))) | |
544 | ;;; (format "%s%s %-19s %6s %-15s %s" | |
545 | ;;; (if (buffer-modified-p) "*" " ") | |
546 | ;;; (if buffer-read-only "%" " ") | |
547 | ;;; (buffer-name) | |
548 | ;;; size | |
549 | ;;; mode-name | |
550 | ;;; (or (buffer-file-name) "")))))) | |
551 | \f | |
7b7d6615 RS |
552 | (defvar menu-bar-mode nil) |
553 | ||
057d49d1 | 554 | (defun menu-bar-mode (flag) |
dfd29450 | 555 | "Toggle display of a menu bar on each frame. |
057d49d1 RS |
556 | This command applies to all frames that exist and frames to be |
557 | created in the future. | |
558 | With a numeric argument, if the argument is negative, | |
dfd29450 | 559 | turn off menu bars; otherwise, turn on menu bars." |
dad8e392 | 560 | (interactive "P") |
dad8e392 | 561 | |
7b7d6615 RS |
562 | ;; Make menu-bar-mode and default-frame-alist consistent. |
563 | (let ((default (assq 'menu-bar-lines default-frame-alist))) | |
564 | (if default | |
565 | (setq menu-bar-mode (not (eq (cdr default) 0))) | |
566 | (setq default-frame-alist | |
567 | (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0)) | |
568 | default-frame-alist)))) | |
569 | ||
570 | ;; Toggle or set the mode, according to FLAG. | |
571 | (setq menu-bar-mode (if (null flag) (not menu-bar-mode) | |
572 | (> (prefix-numeric-value flag) 0))) | |
573 | ||
574 | ;; Apply it to default-frame-alist. | |
575 | (let ((parameter (assq 'menu-bar-lines default-frame-alist))) | |
576 | (if (consp parameter) | |
577 | (setcdr parameter (if menu-bar-mode 1 0)) | |
578 | (setq default-frame-alist | |
579 | (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0)) | |
580 | default-frame-alist)))) | |
581 | ||
582 | ;; Apply it to existing frames. | |
583 | (let ((frames (frame-list))) | |
584 | (while frames | |
585 | (let ((height (cdr (assq 'height (frame-parameters (car frames)))))) | |
586 | (modify-frame-parameters (car frames) | |
587 | (list (cons 'menu-bar-lines | |
588 | (if menu-bar-mode 1 0)))) | |
589 | (modify-frame-parameters (car frames) | |
590 | (list (cons 'height height)))) | |
591 | (setq frames (cdr frames))))) | |
1db87953 | 592 | |
bffa5d4d RS |
593 | (provide 'menu-bar) |
594 | ||
235aa29b | 595 | ;;; menu-bar.el ends here |