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