Mercurial > emacs
annotate lisp/emacs-lisp/lmenu.el @ 2503:fb3509fdf8b7
Sat Apr 10 00:39:29 1993 Jim Blandy (jimb@totoro.cs.oberlin.edu)
* paragraphs.el (sentence-end, forward-sentence): Doc fixes.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sat, 10 Apr 1993 06:21:16 +0000 |
parents | fb0ed5a1d0f3 |
children | f95808ad4b95 |
rev | line source |
---|---|
2232
4f9d60f7de9d
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2231
diff
changeset
|
1 ;;; lmenu.el --- emulate Lucid's menubar support |
4f9d60f7de9d
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2231
diff
changeset
|
2 |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
3 ;; Keywords: emulations |
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
4 |
2231 | 5 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc. |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | |
2232
4f9d60f7de9d
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2231
diff
changeset
|
23 ;;; Code: |
4f9d60f7de9d
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2231
diff
changeset
|
24 |
2231 | 25 |
26 ;; First, emulate the Lucid menubar support in GNU Emacs 19. | |
27 | |
28 ;; Arrange to use current-menubar to set up part of the menu bar. | |
29 | |
30 (setq recompute-lucid-menubar 'recompute-lucid-menubar) | |
31 (defun recompute-lucid-menubar () | |
32 (define-key lucid-menubar-map [menu-bar] | |
33 (condition-case nil | |
34 (make-lucid-menu-keymap "menu-bar" current-menubar) | |
35 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar") | |
36 (sit-for 1) | |
37 (setq lucid-failing-menubar current-menubar | |
38 current-menubar nil)))) | |
39 (setq lucid-menu-bar-dirty-flag nil)) | |
40 | |
41 (defvar lucid-menubar-map (make-sparse-keymap)) | |
42 (or (assq 'current-menubar minor-mode-map-alist) | |
43 (setq minor-mode-map-alist | |
44 (cons (cons 'current-menubar lucid-menubar-map) | |
45 minor-mode-map-alist))) | |
46 | |
47 (defun set-menubar-dirty-flag () | |
48 (force-mode-line-update) | |
49 (setq lucid-menu-bar-dirty-flag t)) | |
50 | |
51 (defvar add-menu-item-count 0) | |
52 | |
53 ;; Return a menu keymap corresponding to a Lucid-style menu list | |
54 ;; MENU-ITEMS, and with name MENU-NAME. | |
55 (defun make-lucid-menu-keymap (menu-name menu-items) | |
56 (let ((menu (make-sparse-keymap menu-name))) | |
57 ;; Process items in reverse order, | |
58 ;; since the define-key loop reverses them again. | |
59 (setq menu-items (reverse menu-items)) | |
60 (while menu-items | |
61 (let* ((item (car menu-items)) | |
62 (callback (if (vectorp item) (aref item 1))) | |
63 command enabler name) | |
64 (cond ((stringp item) | |
65 (setq command nil) | |
66 (setq name item)) | |
67 ((consp item) | |
68 (setq command (make-lucid-menu-keymap (car item) (cdr item))) | |
69 (setq name (car item))) | |
70 ((vectorp item) | |
71 (setq command (make-symbol (format "menu-function-%d" | |
72 add-menu-item-count))) | |
73 (setq enabler (make-symbol (format "menu-function-%d-enabler" | |
74 add-menu-item-count))) | |
75 (setq add-menu-item-count (1+ add-menu-item-count)) | |
76 (put command 'menu-enable enabler) | |
77 (set enabler (aref item 2)) | |
78 (setq name (aref item 0)) | |
79 (if (symbolp callback) | |
80 (fset command callback) | |
81 (fset command (list 'lambda () '(interactive) callback))))) | |
82 (if name | |
83 (define-key menu (vector (intern name)) (cons name command)))) | |
84 (setq menu-items (cdr menu-items))) | |
85 menu)) | |
86 | |
87 (defun popup-menu (menu-desc) | |
88 "Pop up the given menu. | |
89 A menu is a list of menu items, strings, and submenus. | |
90 | |
91 The first element of a menu must be a string, which is the name of the | |
92 menu. This is the string that will be displayed in the parent menu, if | |
93 any. For toplevel menus, it is ignored. This string is not displayed | |
94 in the menu itself. | |
95 | |
96 A menu item is a vector of three or four elements: | |
97 | |
98 - the name of the menu item (a string); | |
99 - the `callback' of that item; | |
100 - whether this item is active (selectable); | |
101 - and an optional string to append to the name. | |
102 | |
103 If the `callback' of a menu item is a symbol, then it must name a command. | |
104 It will be invoked with `call-interactively'. If it is a list, then it is | |
105 evaluated with `eval'. | |
106 | |
107 The fourth element of a menu item is a convenient way of adding the name | |
108 of a command's ``argument'' to the menu, like ``Kill Buffer NAME''. | |
109 | |
110 If an element of a menu is a string, then that string will be presented in | |
111 the menu as unselectable text. | |
112 | |
113 If an element of a menu is a string consisting solely of hyphens, then that | |
114 item will be presented as a solid horizontal line. | |
115 | |
116 If an element of a menu is a list, it is treated as a submenu. The name of | |
117 that submenu (the first element in the list) will be used as the name of the | |
118 item representing this menu on the parent. | |
119 | |
120 The syntax, more precisely: | |
121 | |
122 form := <something to pass to `eval'> | |
123 command := <a symbol or string, to pass to `call-interactively'> | |
124 callback := command | form | |
125 active-p := <t or nil, whether this thing is selectable> | |
126 text := <string, non selectable> | |
127 name := <string> | |
128 argument := <string> | |
129 menu-item := '[' name callback active-p [ argument ] ']' | |
130 menu := '(' name [ menu-item | menu | text ]+ ')' | |
131 " | |
132 (let ((menu (make-lucid-menu-keymap (car menu-desc) (cdr menu-desc))) | |
133 (pos (mouse-position)) | |
134 answer) | |
135 (setq answer (x-popup-menu (list (list (nth 1 pos) (nthcdr 2 pos)) | |
136 (car pos)) | |
137 menu)) | |
138 (setq cmd (lookup-key menu (vector answer))) | |
139 (if cmd (call-interactively cmd)))) | |
140 | |
141 (defconst default-menubar | |
142 '(("File" ["New Frame" x-new-frame t] | |
143 ["Open File..." find-file t] | |
144 ["Save Buffer" save-buffer t nil] | |
145 ["Save Buffer As..." write-file t] | |
146 ["Revert Buffer" revert-buffer t nil] | |
147 "-----" | |
148 ["Print Buffer" lpr-buffer t nil] | |
149 "-----" | |
150 ["Delete Frame" delete-frame t] | |
151 ;; ["Kill Buffer..." kill-buffer t] | |
152 ["Kill Buffer" kill-this-buffer t nil] | |
153 ["Exit Emacs" save-buffers-kill-emacs t] | |
154 ) | |
155 ("Edit" ["Undo" advertised-undo t] | |
156 ["Cut" x-kill-primary-selection t] | |
157 ["Copy" x-copy-primary-selection t] | |
158 ["Paste" x-yank-clipboard-selection t] | |
159 ["Clear" x-delete-primary-selection t] | |
160 ) | |
161 ("Buffers" "") | |
162 | |
163 nil ; the partition: menus after this are flushright | |
164 | |
165 ("Help" ["Info" info t] | |
166 ["Describe Mode" describe-mode t] | |
167 ["Command Apropos..." command-apropos t] | |
168 ["List Keybindings" describe-bindings t] | |
169 ["Describe Key..." describe-key t] | |
170 ["Describe Function..." describe-function t] | |
171 ["Describe Variable..." describe-variable t] | |
172 "-----" | |
173 ["Man..." manual-entry t] | |
174 ["Emacs Tutorial" help-with-tutorial t] | |
175 ["Emacs News" view-emacs-news t] | |
176 ) | |
177 )) | |
178 | |
179 | |
180 (defun kill-this-buffer () ; for the menubar | |
181 "Kills the current buffer." | |
182 (interactive) | |
183 (kill-buffer (current-buffer))) | |
184 | |
185 (defun x-new-frame (&optional frame-name) | |
186 "Creates a new Emacs frame (that is, a new X window.)" | |
187 (interactive) | |
188 (select-frame (x-create-frame | |
189 (append (if frame-name | |
190 (list (cons 'name frame-name)) | |
191 nil) | |
192 frame-default-alist))) | |
193 (switch-to-buffer (get-buffer-create "*scratch*")) | |
194 ) | |
195 | |
196 (defun set-menubar (menubar) | |
197 "Set the default menubar to be menubar." | |
198 (setq-default current-menubar (copy-sequence menubar)) | |
199 (set-menubar-dirty-flag)) | |
200 | |
201 (defun set-buffer-menubar (menubar) | |
202 "Set the buffer-local menubar to be menubar." | |
203 (make-local-variable 'current-menubar) | |
204 (setq current-menubar (copy-sequence menubar)) | |
205 (set-menubar-dirty-flag)) | |
206 | |
207 | |
208 ;;; menu manipulation functions | |
209 | |
210 (defun find-menu-item (menubar item-path-list &optional parent) | |
211 "Searches MENUBAR for item given by ITEM-PATH-LIST. | |
212 Returns (ITEM . PARENT), where PARENT is the immediate parent of | |
213 the item found. | |
214 Signals an error if the item is not found." | |
215 (or parent (setq item-path-list (mapcar 'downcase item-path-list))) | |
216 (if (not (consp menubar)) | |
217 nil | |
218 (let ((rest menubar) | |
219 result) | |
220 (while rest | |
221 (if (and (car rest) | |
222 (equal (car item-path-list) | |
223 (downcase (if (vectorp (car rest)) | |
224 (aref (car rest) 0) | |
225 (if (stringp (car rest)) | |
226 (car rest) | |
227 (car (car rest))))))) | |
228 (setq result (car rest) rest nil) | |
229 (setq rest (cdr rest)))) | |
230 (if (cdr item-path-list) | |
231 (if (consp result) | |
232 (find-menu-item (cdr result) (cdr item-path-list) result) | |
233 (if result | |
234 (signal 'error (list "not a submenu" result)) | |
235 (signal 'error (list "no such submenu" (car item-path-list))))) | |
236 (cons result parent))))) | |
237 | |
238 | |
239 (defun disable-menu-item (path) | |
240 "Make the named menu item be unselectable. | |
241 PATH is a list of strings which identify the position of the menu item in | |
242 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" | |
243 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the | |
244 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." | |
245 (let* ((menubar current-menubar) | |
246 (pair (find-menu-item menubar path)) | |
247 (item (car pair)) | |
248 (menu (cdr pair))) | |
249 (or item | |
250 (signal 'error (list (if menu "No such menu item" "No such menu") | |
251 path))) | |
252 (if (consp item) (error "can't disable menus, only menu items")) | |
253 (aset item 2 nil) | |
254 (set-menubar-dirty-flag) | |
255 item)) | |
256 | |
257 | |
258 (defun enable-menu-item (path) | |
259 "Make the named menu item be selectable. | |
260 PATH is a list of strings which identify the position of the menu item in | |
261 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" | |
262 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the | |
263 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." | |
264 (let* ((menubar current-menubar) | |
265 (pair (find-menu-item menubar path)) | |
266 (item (car pair)) | |
267 (menu (cdr pair))) | |
268 (or item | |
269 (signal 'error (list (if menu "No such menu item" "No such menu") | |
270 path))) | |
271 (if (consp item) (error "%S is a menu, not a menu item" path)) | |
272 (aset item 2 t) | |
273 (set-menubar-dirty-flag) | |
274 item)) | |
275 | |
276 | |
277 (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before) | |
278 (if before (setq before (downcase before))) | |
279 (let* ((menubar current-menubar) | |
280 (menu (condition-case () | |
281 (car (find-menu-item menubar menu-path)) | |
282 (error nil))) | |
283 (item (if (listp menu) | |
284 (car (find-menu-item (cdr menu) (list item-name))) | |
285 (signal 'error (list "not a submenu" menu-path))))) | |
286 (or menu | |
287 (let ((rest menu-path) | |
288 (so-far menubar)) | |
289 (while rest | |
290 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest))))) | |
291 (setq menu | |
292 (if (eq so-far menubar) | |
293 (car (find-menu-item so-far (list (car rest)))) | |
294 (car (find-menu-item (cdr so-far) (list (car rest)))))) | |
295 (or menu | |
296 (let ((rest2 so-far)) | |
297 (while (and (cdr rest2) (car (cdr rest2))) | |
298 (setq rest2 (cdr rest2))) | |
299 (setcdr rest2 | |
300 (nconc (list (setq menu (list (car rest)))) | |
301 (cdr rest2))))) | |
302 (setq so-far menu) | |
303 (setq rest (cdr rest))))) | |
304 (or menu (setq menu menubar)) | |
305 (if item | |
306 nil ; it's already there | |
307 (if item-p | |
308 (setq item (vector item-name item-data enabled-p)) | |
309 (setq item (cons item-name item-data))) | |
310 ;; if BEFORE is specified, try to add it there. | |
311 (if before | |
312 (setq before (car (find-menu-item menu (list before))))) | |
313 (let ((rest menu) | |
314 (added-before nil)) | |
315 (while rest | |
316 (if (eq before (car (cdr rest))) | |
317 (progn | |
318 (setcdr rest (cons item (cdr rest))) | |
319 (setq rest nil added-before t)) | |
320 (setq rest (cdr rest)))) | |
321 (if (not added-before) | |
322 ;; adding before the first item on the menubar itself is harder | |
323 (if (and (eq menu menubar) (eq before (car menu))) | |
324 (setq menu (cons item menu) | |
325 current-menubar menu) | |
326 ;; otherwise, add the item to the end. | |
327 (nconc menu (list item)))))) | |
328 (if item-p | |
329 (progn | |
330 (aset item 1 item-data) | |
331 (aset item 2 (not (null enabled-p)))) | |
332 (setcar item item-name) | |
333 (setcdr item item-data)) | |
334 (set-menubar-dirty-flag) | |
335 item)) | |
336 | |
337 (defun add-menu-item (menu-path item-name function enabled-p &optional before) | |
338 "Add a menu item to some menu, creating the menu first if necessary. | |
339 If the named item exists already, it is changed. | |
340 MENU-PATH identifies the menu under which the new menu item should be inserted. | |
341 It is a list of strings; for example, (\"File\") names the top-level \"File\" | |
342 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\". | |
343 ITEM-NAME is the string naming the menu item to be added. | |
344 FUNCTION is the command to invoke when this menu item is selected. | |
345 If it is a symbol, then it is invoked with `call-interactively', in the same | |
346 way that functions bound to keys are invoked. If it is a list, then the | |
347 list is simply evaluated. | |
348 ENABLED-P controls whether the item is selectable or not. | |
349 BEFORE, if provided, is the name of a menu item before which this item should | |
350 be added, if this item is not on the menu already. If the item is already | |
351 present, it will not be moved." | |
352 (or menu-path (error "must specify a menu path")) | |
353 (or item-name (error "must specify an item name")) | |
354 (add-menu-item-1 t menu-path item-name function enabled-p before)) | |
355 | |
356 | |
357 (defun delete-menu-item (path) | |
358 "Remove the named menu item from the menu hierarchy. | |
359 PATH is a list of strings which identify the position of the menu item in | |
360 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" | |
361 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the | |
362 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"." | |
363 (let* ((menubar current-menubar) | |
364 (pair (find-menu-item menubar path)) | |
365 (item (car pair)) | |
366 (menu (or (cdr pair) menubar))) | |
367 (if (not item) | |
368 nil | |
369 ;; the menubar is the only special case, because other menus begin | |
370 ;; with their name. | |
371 (if (eq menu current-menubar) | |
372 (setq current-menubar (delq item menu)) | |
373 (delq item menu)) | |
374 (set-menubar-dirty-flag) | |
375 item))) | |
376 | |
377 | |
378 (defun relabel-menu-item (path new-name) | |
379 "Change the string of the specified menu item. | |
380 PATH is a list of strings which identify the position of the menu item in | |
381 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\" | |
382 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the | |
383 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\". | |
384 NEW-NAME is the string that the menu item will be printed as from now on." | |
385 (or (stringp new-name) | |
386 (setq new-name (signal 'wrong-type-argument (list 'stringp new-name)))) | |
387 (let* ((menubar current-menubar) | |
388 (pair (find-menu-item menubar path)) | |
389 (item (car pair)) | |
390 (menu (cdr pair))) | |
391 (or item | |
392 (signal 'error (list (if menu "No such menu item" "No such menu") | |
393 path))) | |
394 (if (and (consp item) | |
395 (stringp (car item))) | |
396 (setcar item new-name) | |
397 (aset item 0 new-name)) | |
398 (set-menubar-dirty-flag) | |
399 item)) | |
400 | |
401 (defun add-menu (menu-path menu-name menu-items &optional before) | |
402 "Add a menu to the menubar or one of its submenus. | |
403 If the named menu exists already, it is changed. | |
404 MENU-PATH identifies the menu under which the new menu should be inserted. | |
405 It is a list of strings; for example, (\"File\") names the top-level \"File\" | |
406 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\". | |
407 If MENU-PATH is nil, then the menu will be added to the menubar itself. | |
408 MENU-NAME is the string naming the menu to be added. | |
409 MENU-ITEMS is a list of menu item descriptions. | |
410 Each menu item should be a vector of three elements: | |
411 - a string, the name of the menu item; | |
412 - a symbol naming a command, or a form to evaluate; | |
413 - and t or nil, whether this item is selectable. | |
414 BEFORE, if provided, is the name of a menu before which this menu should | |
415 be added, if this menu is not on its parent already. If the menu is already | |
416 present, it will not be moved." | |
417 (or menu-name (error "must specify a menu name")) | |
418 (or menu-items (error "must specify some menu items")) | |
419 (add-menu-item-1 nil menu-path menu-name menu-items t before)) | |
420 | |
421 | |
422 | |
423 (defvar put-buffer-names-in-file-menu t) | |
424 | |
425 (defun sensitize-file-and-edit-menus-hook () | |
426 "For use as a value of activate-menubar-hook. | |
427 This function changes the sensitivity of these File and Edit menu items: | |
428 | |
429 Cut sensitive only when emacs owns the primary X Selection. | |
430 Copy sensitive only when emacs owns the primary X Selection. | |
431 Clear sensitive only when emacs owns the primary X Selection. | |
432 Paste sensitive only when there is an owner for the X Clipboard Selection. | |
433 Undo sensitive only when there is undo information. | |
434 While in the midst of an undo, this is changed to \"Undo More\". | |
435 | |
436 Kill Buffer has the name of the current buffer appended to it. | |
437 Print Buffer has the name of the current buffer appended to it. | |
438 Save Buffer has the name of the current buffer appended to it, and is | |
439 sensitive only when the current buffer is modified. | |
440 Revert Buffer has the name of the current buffer appended to it, and is | |
441 sensitive only when the current buffer has a file. | |
442 Delete Frame sensitive only when there is more than one visible frame." | |
443 ;; | |
444 ;; the hair in here to not update the menubar unless something has changed | |
445 ;; isn't really necessary (the menubar code is fast enough) but it makes | |
446 ;; me feel better (and creates marginally less list garbage.) | |
447 (let* ((file-menu (cdr (car (find-menu-item current-menubar '("File"))))) | |
448 (edit-menu (cdr (car (find-menu-item current-menubar '("Edit"))))) | |
449 (save (car (find-menu-item file-menu '("Save Buffer")))) | |
450 (rvt (car (find-menu-item file-menu '("Revert Buffer")))) | |
451 (del (car (find-menu-item file-menu '("Delete Frame")))) | |
452 (print (car (find-menu-item file-menu '("Print Buffer")))) | |
453 (kill (car (find-menu-item file-menu '("Kill Buffer")))) | |
454 (cut (car (find-menu-item edit-menu '("Cut")))) | |
455 (copy (car (find-menu-item edit-menu '("Copy")))) | |
456 (paste (car (find-menu-item edit-menu '("Paste")))) | |
457 (clear (car (find-menu-item edit-menu '("Clear")))) | |
458 (undo (or (car (find-menu-item edit-menu '("Undo"))) | |
459 (car (find-menu-item edit-menu '("Undo More"))))) | |
460 (name (buffer-name)) | |
461 (emacs-owns-selection-p (x-selection-owner-p)) | |
462 (clipboard-exists-p (x-selection-exists-p 'CLIPBOARD)) | |
463 undo-available undoing-more | |
464 (undo-info-available (not (null (and (not (eq t buffer-undo-list)) | |
465 (if (eq last-command 'undo) | |
466 (setq undoing-more | |
467 (and (boundp 'pending-undo-list) | |
468 pending-undo-list) | |
469 buffer-undo-list)))))) | |
470 undo-name undo-state | |
471 (change-p | |
472 (or (and cut (not (eq emacs-owns-selection-p (aref cut 2)))) | |
473 (and copy (not (eq emacs-owns-selection-p (aref copy 2)))) | |
474 (and clear (not (eq emacs-owns-selection-p (aref clear 2)))) | |
475 (and paste (not (eq clipboard-exists-p (aref paste 2)))) | |
476 (and save (not (eq (buffer-modified-p) (aref save 2)))) | |
477 (and rvt (not (eq (not (not buffer-file-name)) (aref rvt 2)))) | |
478 (and del (not (eq (null (cdr (visible-frame-list))) (aref del 2)))) | |
479 ))) | |
480 (if (not put-buffer-names-in-file-menu) | |
481 nil | |
482 (if (= (length save) 4) (progn (aset save 3 name) (setq change-p t))) | |
483 (if (= (length rvt) 4) (progn (aset rvt 3 name) (setq change-p t))) | |
484 (if (= (length print) 4) (progn (aset print 3 name) (setq change-p t))) | |
485 (if (= (length kill) 4) (progn (aset kill 3 name) (setq change-p t)))) | |
486 (if save (aset save 2 (buffer-modified-p))) | |
487 (if rvt (aset rvt 2 (not (not buffer-file-name)))) | |
488 (if del (aset del 2 (null (cdr (visible-frame-list))))) | |
489 (if cut (aset cut 2 emacs-owns-selection-p)) | |
490 (if copy (aset copy 2 emacs-owns-selection-p)) | |
491 (if clear (aset clear 2 emacs-owns-selection-p)) | |
492 (if paste (aset paste 2 clipboard-exists-p)) | |
493 | |
494 ;; we could also do this with the third field of the item. | |
495 (if (eq last-command 'undo) | |
496 (setq undo-name "Undo More" | |
497 undo-state (not (null (and (boundp 'pending-undo-list) | |
498 pending-undo-list)))) | |
499 (setq undo-name "Undo" | |
500 undo-state (and (not (eq buffer-undo-list t)) | |
501 (not (null | |
502 (or buffer-undo-list | |
503 (and (boundp 'pending-undo-list) | |
504 pending-undo-list))))))) | |
505 (if buffer-read-only (setq undo-state nil)) | |
506 (if (and undo | |
507 (or (not (equal undo-name (aref undo 0))) | |
508 (not (eq undo-state (aref undo 2))))) | |
509 (progn (aset undo 0 undo-name) | |
510 (aset undo 2 undo-state) | |
511 (setq change-p t))) | |
512 ;; if we made any changes, return nil | |
513 ;; otherwise return t to indicate that we haven't done anything. | |
514 (not change-p))) | |
515 | |
516 ;; this version is too slow | |
517 (defun format-buffers-menu-line (buffer) | |
518 "Returns a string to represent the given buffer in the Buffer menu. | |
519 nil means the buffer shouldn't be listed. You can redefine this." | |
520 (if (string-match "\\` " (buffer-name buffer)) | |
521 nil | |
522 (save-excursion | |
523 (set-buffer buffer) | |
524 (let ((size (buffer-size))) | |
525 (format "%s%s %-19s %6s %-15s %s" | |
526 (if (buffer-modified-p) "*" " ") | |
527 (if buffer-read-only "%" " ") | |
528 (buffer-name) | |
529 size | |
530 mode-name | |
531 (or (buffer-file-name) "")))))) | |
532 | |
533 (defun format-buffers-menu-line (buffer) | |
534 (if (string-match "\\` " (setq buffer (buffer-name buffer))) | |
535 nil | |
536 buffer)) | |
537 | |
538 (defvar buffers-menu-max-size 10 | |
539 "*Maximum number of entries which may appear on the \"Buffers\" menu. | |
540 If this is 10, then only the ten most-recently-selected buffers will be | |
541 shown. If this is nil, then all buffers will be shown. Setting this to | |
542 a large number or nil will slow down menu responsiveness.") | |
543 | |
544 (defvar complex-buffers-menu-p nil | |
545 "*If true, the buffers menu will contain several commands, as submenus | |
546 of each buffer line. If this is false, then there will be only one command: | |
547 select that buffer.") | |
548 | |
549 (defvar buffers-menu-switch-to-buffer-function 'switch-to-buffer | |
550 "*The function to call to select a buffer from the buffers menu. | |
551 `switch-to-buffer' is a good choice, as is `pop-to-buffer'.") | |
552 | |
553 | |
554 (defun buffer-menu-save-buffer (buffer) | |
555 (save-excursion | |
556 (set-buffer buffer) | |
557 (save-buffer))) | |
558 | |
559 (defun buffer-menu-write-file (buffer) | |
560 (save-excursion | |
561 (set-buffer buffer) | |
562 (write-file (read-file-name | |
563 (concat "Write " (buffer-name (current-buffer)) | |
564 " to file: "))))) | |
565 | |
566 | |
567 (defsubst build-buffers-menu-internal (buffers) | |
568 (let (name line) | |
569 (mapcar | |
570 (if complex-buffers-menu-p | |
571 (function | |
572 (lambda (buffer) | |
573 (if (setq line (format-buffers-menu-line buffer)) | |
574 (list line | |
575 (vector "Switch to Buffer" | |
576 (list buffers-menu-switch-to-buffer-function | |
577 (setq name (buffer-name buffer))) | |
578 t) | |
579 (if (and (buffer-modified-p buffer) | |
580 (buffer-file-name buffer)) | |
581 (vector "Save Buffer" | |
582 (list 'buffer-menu-save-buffer name) t) | |
583 ["Save Buffer" nil nil]) | |
584 (vector "Save Buffer As..." | |
585 (list 'buffer-menu-write-file name) t) | |
586 (vector "Kill Buffer" (list 'kill-buffer name) t))))) | |
587 (function (lambda (buffer) | |
588 (if (setq line (format-buffers-menu-line buffer)) | |
589 (vector line | |
590 (list buffers-menu-switch-to-buffer-function | |
591 (buffer-name buffer)) | |
592 t))))) | |
593 buffers))) | |
594 | |
595 (defun build-buffers-menu-hook () | |
596 "For use as a value of activate-menubar-hook. | |
597 This function changes the contents of the \"Buffers\" menu to correspond | |
598 to the current set of buffers. Only the most-recently-used few buffers | |
599 will be listed on the menu, for efficiency reasons. You can control how | |
600 many buffers will be shown by setting `buffers-menu-max-size'. | |
601 You can control the text of the menu items by redefining the function | |
602 `format-buffers-menu-line'." | |
603 (let ((buffer-menu (car (find-menu-item current-menubar '("Buffers")))) | |
604 name | |
605 buffers) | |
606 (if (not buffer-menu) | |
607 nil | |
608 (setq buffers (buffer-list)) | |
609 | |
610 (if (and (integerp buffers-menu-max-size) | |
611 (> buffers-menu-max-size 1)) | |
612 (if (> (length buffers) buffers-menu-max-size) | |
613 (setcdr (nthcdr buffers-menu-max-size buffers) nil))) | |
614 | |
615 (setq buffers (build-buffers-menu-internal buffers)) | |
616 (setq buffers (nconc (delq nil buffers) | |
617 '("----" ["List All Buffers" list-buffers t]))) | |
618 ;; slightly (only slightly) more efficient to not install the menubar | |
619 ;; if it hasn't visibly changed. | |
620 (if (equal buffers (cdr buffer-menu)) | |
621 t ; return t meaning "no change" | |
622 (setcdr buffer-menu buffers) | |
623 (set-menubar-dirty-flag) | |
624 nil)))) | |
625 | |
626 (add-hook 'activate-menubar-hook 'build-buffers-menu-hook) | |
627 (add-hook 'activate-menubar-hook 'sensitize-file-and-edit-menus-hook) | |
628 | |
629 (let ((frames (frame-list))) | |
630 (while frames | |
631 (modify-frame-parameters (car frames) '((menu-bar-lines . 1))) | |
632 (setq frames (cdr frames)))) | |
633 (or (assq 'menu-bar-lines default-frame-alist) | |
634 (setq default-frame-alist | |
635 (cons '(menu-bar-lines . 1) default-frame-alist))) | |
636 | |
637 (set-menubar default-menubar) | |
638 | |
639 (provide 'menubar) | |
640 | |
2232
4f9d60f7de9d
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2231
diff
changeset
|
641 ;;; lmenu.el ends here |