1;;; menu-bar.el --- define a default menu bar. 2 3;; Author: RMS 4;; Keywords: internal 5 6;; Copyright (C) 1993, 1994 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 24;;; Code: 25 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"))) 30(defvar menu-bar-help-menu (make-sparse-keymap "Help")) 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)) 34(defvar menu-bar-edit-menu (make-sparse-keymap "Edit")) 35(define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu)) 36(defvar menu-bar-file-menu (make-sparse-keymap "File")) 37(define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu)) 38 39(define-key menu-bar-file-menu [exit-emacs] 40 '("Exit Emacs" . save-buffers-kill-emacs)) 41(define-key menu-bar-file-menu [kill-buffer] 42 '("Kill Buffer" . kill-this-buffer)) 43(define-key menu-bar-file-menu [delete-frame] '("Delete Frame" . delete-frame)) 44(define-key menu-bar-file-menu [epatch] 45 '("Apply Patch" . menu-bar-epatch-menu)) 46(define-key menu-bar-file-menu [ediff] 47 '("Compare Files" . menu-bar-ediff-menu)) 48(define-key menu-bar-file-menu [emerge] '("Emerge" . menu-bar-emerge-menu)) 49(define-key menu-bar-file-menu [calendar] '("Calendar" . calendar)) 50(define-key menu-bar-file-menu [rmail] '("Read Mail" . rmail)) 51(define-key menu-bar-file-menu [gnus] '("Read Net News" . gnus)) 52(define-key menu-bar-file-menu [bookmark] 53 '("Bookmarks" . menu-bar-bookmark-map)) 54(define-key menu-bar-file-menu [print-buffer] '("Print Buffer" . print-buffer)) 55(define-key menu-bar-file-menu [revert-buffer] 56 '("Revert Buffer" . revert-buffer)) 57(define-key menu-bar-file-menu [write-file] 58 '("Save Buffer As..." . write-file)) 59(define-key menu-bar-file-menu [save-buffer] '("Save Buffer" . save-buffer)) 60(define-key menu-bar-file-menu [dired] '("Open Directory..." . dired)) 61(define-key menu-bar-file-menu [open-file] '("Open File..." . find-file)) 62(define-key menu-bar-file-menu [make-frame] '("Make New Frame" . make-frame)) 63 64(define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map)) 65(define-key menu-bar-edit-menu [fill] '("Fill" . fill-region)) 66(define-key menu-bar-edit-menu [clear] '("Clear" . delete-region)) 67(define-key menu-bar-edit-menu [query-replace] 68 '("Query Replace" . query-replace)) 69(define-key menu-bar-edit-menu [re-search-back] 70 '("Regexp Search Backwards" . re-search-backward)) 71(define-key menu-bar-edit-menu [search-back] 72 '("Search Backwards" . search-backward)) 73(define-key menu-bar-edit-menu [re-search-fwd] 74 '("Regexp Search" . re-search-forward)) 75(define-key menu-bar-edit-menu [search-fwd] 76 '("Search" . search-forward)) 77(define-key menu-bar-edit-menu [choose-next-paste] 78 '("Choose Next Paste >" . mouse-menu-choose-yank)) 79(define-key menu-bar-edit-menu [paste] '("Paste" . yank)) 80(define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save)) 81(define-key menu-bar-edit-menu [cut] '("Cut" . kill-region)) 82(define-key menu-bar-edit-menu [undo] '("Undo" . undo)) 83 84(put 'fill-region 'menu-enable 'mark-active) 85(put 'kill-region 'menu-enable 'mark-active) 86(put 'kill-ring-save 'menu-enable 'mark-active) 87(put 'yank 'menu-enable '(x-selection-exists-p)) 88(put 'delete-region 'menu-enable 'mark-active) 89(put 'undo 'menu-enable '(if (eq last-command 'undo) 90 pending-undo-list 91 (consp buffer-undo-list))) 92(put 'query-replace 'menu-enable (not buffer-read-only)) 93 94(autoload 'ispell-menu-map "ispell" nil t 'keymap) 95 96;; These are alternative definitions for the cut, paste and copy 97;; menu items. Use them if your system expects these to use the clipboard 98 99(put 'clipboard-kill-region 'menu-enable 'mark-active) 100(put 'clipboard-kill-ring-save 'menu-enable 'mark-active) 101(put 'clipboard-yank 'menu-enable 102 '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD))) 103 104(defun clipboard-yank () 105 "Reinsert the last stretch of killed text, or the clipboard contents." 106 (interactive) 107 (let ((x-select-enable-clipboard t)) 108 (yank))) 109 110(defun clipboard-kill-ring-save (beg end) 111 "Copy region to kill ring, and save in the X clipboard." 112 (interactive "r") 113 (let ((x-select-enable-clipboard t)) 114 (kill-ring-save beg end))) 115 116(defun clipboard-kill-region (beg end) 117 "Kill the region, and save it in the X clipboard." 118 (interactive "r") 119 (let ((x-select-enable-clipboard t)) 120 (kill-region beg end))) 121 122(defun menu-bar-enable-clipboard () 123 "Make the menu bar CUT, PASTE and COPY items use the clipboard." 124 (interactive) 125 ;; We can't use constant list structure here because it becomes pure, 126 ;; and because it gets modified with cache data. 127 (define-key menu-bar-edit-menu [paste] 128 (cons "Paste" 'clipboard-yank)) 129 (define-key menu-bar-edit-menu [copy] 130 (cons "Copy" 'clipboard-kill-ring-save)) 131 (define-key menu-bar-edit-menu [cut] 132 (cons "Cut" 'clipboard-kill-region))) 133 134;; Sun expects these commands on these keys, so why not? 135(define-key global-map [f20] 'clipboard-kill-region) 136(define-key global-map [f16] 'clipboard-kill-ring-save) 137(define-key global-map [f18] 'clipboard-yank) 138 139(define-key menu-bar-help-menu [emacs-version] 140 '("Show Version" . emacs-version)) 141(define-key menu-bar-help-menu [report-emacs-bug] 142 '("Send Bug Report" . report-emacs-bug)) 143(define-key menu-bar-help-menu [emacs-tutorial] 144 '("Emacs Tutorial" . help-with-tutorial)) 145(define-key menu-bar-help-menu [man] '("Man..." . manual-entry)) 146(define-key menu-bar-help-menu [describe-variable] 147 '("Describe Variable..." . describe-variable)) 148(define-key menu-bar-help-menu [describe-function] 149 '("Describe Function..." . describe-function)) 150(define-key menu-bar-help-menu [describe-key] 151 '("Describe Key..." . describe-key)) 152(define-key menu-bar-help-menu [list-keybindings] 153 '("List Keybindings" . describe-bindings)) 154(define-key menu-bar-help-menu [command-apropos] 155 '("Command Apropos..." . command-apropos)) 156(define-key menu-bar-help-menu [describe-mode] 157 '("Describe Mode" . describe-mode)) 158(define-key menu-bar-help-menu [info] '("Info" . info)) 159 160(define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news)) 161(defun kill-this-buffer () ; for the menubar 162 "Kills the current buffer." 163 (interactive) 164 (kill-buffer (current-buffer))) 165 166(defun kill-this-buffer-enabled-p () 167 (let ((count 0) 168 (buffers (buffer-list))) 169 (while buffers 170 (or (string-match "^ " (buffer-name (car buffers))) 171 (setq count (1+ count))) 172 (setq buffers (cdr buffers))) 173 (> count 1))) 174 175(put 'save-buffer 'menu-enable '(buffer-modified-p)) 176(put 'revert-buffer 'menu-enable 177 '(or revert-buffer-function revert-buffer-insert-file-contents-function 178 (and (buffer-file-name) 179 (or (buffer-modified-p) 180 (not (verify-visited-file-modtime (current-buffer))))))) 181;; Permit deleting frame if it would leave a visible or iconified frame. 182(put 'delete-frame 'menu-enable 183 '(let ((frames (frame-list)) 184 (count 0)) 185 (while frames 186 (if (cdr (assq 'visibility (frame-parameters (car frames)))) 187 (setq count (1+ count))) 188 (setq frames (cdr frames))) 189 (> count 1))) 190(put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p)) 191 192(put 'advertised-undo 'menu-enable 193 '(and (not (eq t buffer-undo-list)) 194 (if (eq last-command 'undo) 195 (and (boundp 'pending-undo-list) 196 pending-undo-list) 197 buffer-undo-list))) 198 199(defvar yank-menu-length 100 200 "*Maximum length of an item in the menu for \ 201\\[mouse-menu-choose-yank].") 202 203(defun mouse-menu-choose-yank (event) 204 "Pop up a menu of the kill-ring for selection with the mouse. 205The kill-ring-yank-pointer is moved to the selected element. 206A subsequent \\[yank] yanks the choice just selected." 207 (interactive "e") 208 (let* ((count 0) 209 (menu (mapcar (lambda (string) 210 (if (> (length string) yank-menu-length) 211 (setq string (substring string 212 0 yank-menu-length))) 213 (prog1 (cons string count) 214 (setq count (1+ count)))) 215 kill-ring)) 216 (arg (x-popup-menu event 217 (list "Yank Menu" 218 (cons "Choose Next Yank" menu))))) 219 ;; A mouse click outside the menu returns nil. 220 ;; Avoid a confusing error from passing nil to rotate-yank-pointer. 221 ;; XXX should this perhaps do something other than simply return? -rm 222 (if arg 223 (progn 224 ;; We don't use `rotate-yank-pointer' because we want to move 225 ;; relative to the beginning of kill-ring, not the current 226 ;; position. Also, that would ask for any new X selection and 227 ;; thus change the list of items the user just chose from, which 228 ;; would be highly confusing. 229 (setq kill-ring-yank-pointer (nthcdr arg kill-ring)) 230 (if (interactive-p) 231 (message "The next yank will insert the selected text.") 232 (current-kill 0)))))) 233(put 'mouse-menu-choose-yank 'menu-enable 'kill-ring) 234 235(define-key global-map [menu-bar buffer] '("Buffers" . menu-bar-buffers)) 236 237(defalias 'menu-bar-buffers (make-sparse-keymap "Buffers")) 238 239(defvar complex-buffers-menu-p nil 240 "*Non-nil says, offer a choice of actions after you pick a buffer. 241This applies to the Buffers menu from the menu bar.") 242 243(defvar buffers-menu-max-size 10 244 "*Maximum number of entries which may appear on the Buffers menu. 245If this is 10, then only the ten most-recently-selected buffers are shown. 246If this is nil, then all buffers are shown. 247A large number or nil slows down menu responsiveness.") 248 249(defvar list-buffers-directory nil) 250 251(defun menu-bar-select-buffer () 252 (interactive) 253 (switch-to-buffer last-command-event)) 254 255(defun menu-bar-select-frame () 256 (interactive) 257 (make-frame-visible last-command-event) 258 (raise-frame last-command-event) 259 (select-frame last-command-event)) 260 261(defvar menu-bar-update-buffers-last-buffers nil) 262(defvar menu-bar-update-buffers-last-frames nil) 263 264(defun menu-bar-update-buffers () 265 (let ((buffers (buffer-list)) 266 (frames (frame-list)) 267 buffers-info 268 buffers-menu frames-menu) 269 (setq buffers-info 270 (mapcar (function (lambda (buffer) 271 (list buffer (buffer-modified-p buffer) 272 (save-excursion 273 (set-buffer buffer) 274 buffer-read-only)))) 275 buffers)) 276 (if (and (equal buffers-info menu-bar-update-buffers-last-buffers) 277 (equal frames menu-bar-update-buffers-last-frames)) 278 nil 279 (setq menu-bar-update-buffers-last-buffers buffers-info) 280 (setq menu-bar-update-buffers-last-frames frames) 281 ;; If requested, list only the N most recently selected buffers. 282 (if (and (integerp buffers-menu-max-size) 283 (> buffers-menu-max-size 1)) 284 (if (> (length buffers) buffers-menu-max-size) 285 (setcdr (nthcdr buffers-menu-max-size buffers) nil))) 286 287 ;; Make the menu of buffers proper. 288 (setq buffers-menu 289 (cons "Select Buffer" 290 (let ((tail buffers) 291 (maxbuf 0) 292 (maxlen 0) 293 alist 294 head) 295 (while tail 296 (or (eq ?\ (aref (buffer-name (car tail)) 0)) 297 (setq maxbuf 298 (max maxbuf 299 (length (buffer-name (car tail)))))) 300 (setq tail (cdr tail))) 301 (setq tail buffers) 302 (while tail 303 (let ((elt (car tail))) 304 (or (eq ?\ (aref (buffer-name elt) 0)) 305 (setq alist (cons 306 (cons 307 (format 308 (format "%%%ds %%s%%s %%s" 309 maxbuf) 310 (buffer-name elt) 311 (if (buffer-modified-p elt) 312 "*" " ") 313 (save-excursion 314 (set-buffer elt) 315 (if buffer-read-only "%" " ")) 316 (or (buffer-file-name elt) 317 (save-excursion 318 (set-buffer elt) 319 list-buffers-directory) 320 "")) 321 elt) 322 alist))) 323 (and alist (> (length (car (car alist))) maxlen) 324 (setq maxlen (length (car (car alist)))))) 325 (setq tail (cdr tail))) 326 (setq alist (nreverse alist)) 327 (nconc (mapcar '(lambda (pair) 328 ;; This is somewhat risque, to use 329 ;; the buffer name itself as the event type 330 ;; to define, but it works. 331 ;; It would not work to use the buffer 332 ;; since a buffer as an event has its 333 ;; own meaning. 334 (nconc (list (buffer-name (cdr pair)) 335 (car pair) 336 (cons nil nil)) 337 'menu-bar-select-buffer)) 338 alist) 339 (list (cons 'list-buffers 340 (cons 341 (concat (make-string (max (- (/ maxlen 342 2) 343 8) 344 0) ?\ ) 345 "List All Buffers") 346 'list-buffers))))))) 347 348 349 ;; Make a Frames menu if we have more than one frame. 350 (if (cdr frames) 351 (setq frames-menu 352 (cons "Select Frame" 353 (mapcar '(lambda (frame) 354 (nconc (list frame 355 (cdr (assq 'name 356 (frame-parameters frame))) 357 (cons nil nil)) 358 'menu-bar-select-frame)) 359 frames)))) 360 (if buffers-menu 361 (setq buffers-menu (cons 'keymap buffers-menu))) 362 (if frames-menu 363 (setq frames-menu (cons 'keymap frames-menu))) 364 (define-key global-map [menu-bar buffer] 365 (cons "Buffers" 366 (if (and buffers-menu frames-menu) 367 (list 'keymap "Buffers and Frames" 368 (cons 'buffers (cons "Buffers" buffers-menu)) 369 (cons 'frames (cons "Frames" frames-menu))) 370 (or buffers-menu frames-menu 'undefined))))))) 371 372(add-hook 'menu-bar-update-hook 'menu-bar-update-buffers) 373 374;; this version is too slow 375;;;(defun format-buffers-menu-line (buffer) 376;;; "Returns a string to represent the given buffer in the Buffer menu. 377;;;nil means the buffer shouldn't be listed. You can redefine this." 378;;; (if (string-match "\\` " (buffer-name buffer)) 379;;; nil 380;;; (save-excursion 381;;; (set-buffer buffer) 382;;; (let ((size (buffer-size))) 383;;; (format "%s%s %-19s %6s %-15s %s" 384;;; (if (buffer-modified-p) "*" " ") 385;;; (if buffer-read-only "%" " ") 386;;; (buffer-name) 387;;; size 388;;; mode-name 389;;; (or (buffer-file-name) "")))))) 390 391(defun menu-bar-mode (flag) 392 "Toggle display of a menu bar on each frame. 393This command applies to all frames that exist and frames to be 394created in the future. 395With a numeric argument, if the argument is negative, 396turn off menu bars; otherwise, turn on menu bars." 397 (interactive "P") 398 399 ;; Obtain the current setting by looking at default-frame-alist. 400 (let ((menu-bar-mode 401 (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist))) 402 (if assq (cdr assq) 0)))))) 403 404 ;; Tweedle it according to the argument. 405 (setq menu-bar-mode (if (null flag) (not menu-bar-mode) 406 (> (prefix-numeric-value flag) 0))) 407 408 ;; Apply it to default-frame-alist. 409 (let ((parameter (assq 'menu-bar-lines default-frame-alist))) 410 (if (consp parameter) 411 (setcdr parameter (if menu-bar-mode 1 0)) 412 (setq default-frame-alist 413 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0)) 414 default-frame-alist)))) 415 416 ;; Apply it to existing frames. 417 (let ((frames (frame-list))) 418 (while frames 419 (let ((height (cdr (assq 'height (frame-parameters (car frames)))))) 420 (modify-frame-parameters (car frames) 421 (list (cons 'menu-bar-lines 422 (if menu-bar-mode 1 0)))) 423 (modify-frame-parameters (car frames) 424 (list (cons 'height height)))) 425 (setq frames (cdr frames)))))) 426 427(provide 'menu-bar) 428 429;;; menu-bar.el ends here 430