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