1 ;;; modeline.el --- modeline hackery.
3 ;; Copyright (C) 1988, 1992-1994, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: extensions, dumped
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Synched up with: Not in FSF.
30 ;; This file is dumped with XEmacs.
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;;; General mouse modeline stuff ;;;
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (defgroup modeline nil
39 "Modeline customizations."
42 (defcustom drag-divider-event-lag 150
43 "*The pause (in msecs) between divider drag events before redisplaying.
44 If this value is too small, dragging will be choppy because redisplay cannot
45 keep up. If it is too large, dragging will be choppy because of the explicit
46 redisplay delay specified."
51 (define-obsolete-variable-alias
52 'drag-modeline-event-lag
53 'drag-divider-event-lag)
55 (defcustom modeline-click-swaps-buffers nil
56 "*If non-nil, clicking on the modeline changes the current buffer.
57 Click on the left half of the modeline cycles forward through the
58 buffer list and clicking on the right half cycles backward."
62 (defcustom modeline-scrolling-method nil
63 "*If non-nil, dragging the modeline with the mouse may also scroll its
64 text horizontally (vertical motion controls window resizing and horizontal
65 motion controls modeline scrolling).
67 With a value of t, the modeline text is scrolled in the same direction as
68 the mouse motion. With a value of 'scrollbar, the modeline is considered as
69 a scrollbar for its own text, which then moves in the opposite direction."
70 :type '(choice (const :tag "none" nil)
72 (const :tag "scrollbar" scrollbar))
73 :set (lambda (sym val)
77 (set-glyph-image modeline-pointer-glyph "hand2" 'global 'x))
79 (set-glyph-image modeline-pointer-glyph "fleur" 'global 'x))
81 (set-glyph-image modeline-pointer-glyph "sb_v_double_arrow"
85 (defun mouse-drag-modeline (event)
86 "Resize a window by dragging its modeline.
87 This command should be bound to a button-press event in modeline-map.
88 Holding down a mouse button and moving the mouse up and down will
89 make the clicked-on window taller or shorter.
91 See also the variable `modeline-scrolling-method'."
93 (or (button-press-event-p event)
94 (error "%s must be invoked by a mouse-press" this-command))
95 (or (event-over-modeline-p event)
96 (error "not over a modeline"))
97 ;; Give the modeline a "pressed" look. --hniksic
98 (let-specifier ((modeline-shadow-thickness
99 (- (specifier-instance modeline-shadow-thickness
100 (event-window event)))
101 (event-window event)))
103 (depress-line (event-y event))
104 (start-event-frame (event-frame event))
105 (start-event-window (event-window event))
106 (start-nwindows (count-windows t))
107 (hscroll-delta (face-width 'modeline))
108 (start-hscroll (modeline-hscroll (event-window event)))
109 (start-x-pixel (event-x-pixel event))
113 should-enlarge-minibuffer
114 event min-height minibuffer y top bot edges wconfig growth)
115 (setq minibuffer (minibuffer-window start-event-frame)
116 default-line-height (face-height 'default start-event-window)
117 min-height (+ (* window-min-height default-line-height)
118 ;; Don't let the window shrink by a
119 ;; non-multiple of the default line
120 ;; height. (enlarge-window -1) will do
121 ;; this if the difference between the
122 ;; current window height and the minimum
123 ;; window height is less than the height
124 ;; of the default font. These extra
125 ;; lost pixels of height don't come back
126 ;; if you grow the window again. This
127 ;; can make it impossible to drag back
128 ;; to the exact original size, which is
130 (% (window-pixel-height start-event-window)
131 default-line-height))
133 (if (specifier-instance has-modeline-p start-event-window)
134 (+ (face-height 'modeline start-event-window)
135 (* 2 (specifier-instance modeline-shadow-thickness
136 start-event-window)))
137 (* 2 (specifier-instance modeline-shadow-thickness
138 start-event-window))))
139 (if (not (eq (window-frame minibuffer) start-event-frame))
140 (setq minibuffer nil))
141 (if (and (null minibuffer) (one-window-p t))
142 (error "Attempt to resize sole window"))
143 ;; if this is the bottommost ordinary window, then to
144 ;; move its modeline the minibuffer must be enlarged.
145 (setq should-enlarge-minibuffer
146 (and minibuffer (window-lowest-p start-event-window)))
147 ;; loop reading events
149 (setq event (next-event event))
150 ;; requeue event and quit if this is a misc-user, eval or
152 ;; quit if this is a button press or release event, or if the event
153 ;; occurred in some other frame.
154 ;; drag if this is a mouse motion event and the time
155 ;; between this event and the last event is greater than
156 ;; drag-divider-event-lag.
157 ;; do nothing if this is any other kind of event.
158 (cond ((or (misc-user-event-p event)
159 (key-press-event-p event))
160 (setq unread-command-events (nconc unread-command-events
163 ((button-release-event-p event)
165 ;; Consider we have a mouse click neither X pos (modeline
166 ;; scroll) nore Y pos (modeline drag) have changed.
167 (and modeline-click-swaps-buffers
168 (= depress-line (event-y event))
169 (or (not modeline-scrolling-method)
171 (modeline-hscroll start-event-window)))
172 (modeline-swap-buffers event)))
173 ((button-event-p event)
175 ((not (motion-event-p event))
176 (dispatch-event event))
177 ((not (eq start-event-frame (event-frame event)))
179 ((< (abs (- (event-timestamp event) last-timestamp))
180 drag-divider-event-lag)
183 (when modeline-scrolling-method
184 (let ((delta (/ (- (event-x-pixel event) start-x-pixel)
186 (set-modeline-hscroll start-event-window
187 (if (eq modeline-scrolling-method t)
188 (- start-hscroll delta)
189 (+ start-hscroll delta)))
191 (setq last-timestamp (event-timestamp event)
192 y (event-y-pixel event)
193 edges (window-pixel-edges start-event-window)
196 ;; scale back a move that would make the
198 (cond ((< (- y top (- modeline-height)) min-height)
199 (setq y (+ top min-height (- modeline-height)))))
200 ;; compute size change needed
201 (setq growth (- y bot (/ (- modeline-height) 2))
202 wconfig (current-window-configuration))
203 ;; grow/shrink minibuffer?
204 (if should-enlarge-minibuffer
206 ;; yes. scale back shrinkage if it
207 ;; would make the minibuffer less than 1
210 ;; also flip the sign of the computed growth,
211 ;; since if we want to grow the window with the
212 ;; modeline we need to shrink the minibuffer
214 (if (and (> growth 0)
215 (< (- (window-pixel-height minibuffer)
217 default-line-height))
219 (- (window-pixel-height minibuffer)
220 default-line-height)))
221 (setq growth (- growth))))
222 ;; window grow and shrink by lines not pixels, so
223 ;; divide the pixel height by the height of the
225 (setq growth (/ growth default-line-height))
226 ;; grow/shrink the window
227 (enlarge-window growth nil (if should-enlarge-minibuffer
230 ;; if this window's growth caused another
231 ;; window to be deleted because it was too
232 ;; short, rescind the change.
234 ;; if size change caused space to be stolen
235 ;; from a window above this one, rescind the
236 ;; change, but only if we didn't grow/shrink
237 ;; the minibuffer. minibuffer size changes
238 ;; can cause all windows to shrink... no way
240 (if (or (/= start-nwindows (count-windows t))
241 (and (not should-enlarge-minibuffer)
242 (/= top (nth 1 (window-pixel-edges
243 start-event-window)))))
244 (set-window-configuration wconfig))))))))
246 ;; from Bob Weiner (bob_weiner@pts.mot.com)
247 ;; Whether this function should be called is now decided in
248 ;; mouse-drag-modeline - dverna feb. 98
249 (defun modeline-swap-buffers (event)
250 "Handle mouse clicks on modeline by switching buffers.
251 If click on left half of a frame's modeline, bury current buffer.
252 If click on right half of a frame's modeline, raise bottommost buffer.
253 Arg EVENT is the button release event that occurred on the modeline."
254 (or (event-over-modeline-p event)
255 (error "not over a modeline"))
256 (or (button-release-event-p event)
257 (error "not a button release event"))
258 (if (< (event-x event) (/ (window-width (event-window event)) 2))
259 ;; On left half of modeline, bury current buffer,
260 ;; displaying second buffer on list.
261 (mouse-bury-buffer event)
262 ;; On right half of modeline, raise and display bottommost
263 ;; buffer in buffer list.
264 (mouse-unbury-buffer event)))
266 (defconst modeline-menu
268 ["Delete Window Above" delete-window t]
269 ["Delete Other Windows" delete-other-windows t]
270 ["Split Window Above" split-window-vertically t]
271 ["Split Window Horizontally" split-window-horizontally t]
272 ["Balance Windows" balance-windows t]
275 (defun modeline-menu (event)
277 (popup-menu-and-execute-in-window
278 (cons (format "Window Commands for %S:"
279 (buffer-name (event-buffer event)))
283 (defvar modeline-map (make-sparse-keymap 'modeline-map)
284 "Keymap consulted for mouse-clicks on the modeline of a window.
285 This variable may be buffer-local; its value will be looked up in
286 the buffer of the window whose modeline was clicked upon.")
288 (define-key modeline-map 'button1 'mouse-drag-modeline)
289 ;; button2 selects the window without setting point
290 (define-key modeline-map 'button2 (lambda () (interactive "@")))
291 (define-key modeline-map 'button3 'modeline-menu)
293 (make-face 'modeline-mousable "Face for mousable portions of the modeline.")
294 (set-face-parent 'modeline-mousable 'modeline nil '(default))
295 (when (featurep 'window-system)
296 (set-face-foreground 'modeline-mousable "firebrick" nil '(default color win))
297 (set-face-font 'modeline-mousable [bold] nil '(default mono win))
298 (set-face-font 'modeline-mousable [bold] nil '(default grayscale win)))
300 (defmacro make-modeline-command-wrapper (command)
303 (save-selected-window
304 (select-window (event-window event))
305 (call-interactively ',(eval command)))))
307 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 (defvar minor-mode-alist nil
312 "Alist saying how to show minor modes in the modeline.
313 Each element looks like (VARIABLE STRING);
314 STRING is included in the modeline iff VARIABLE's value is non-nil.
316 Actually, STRING need not be a string; any possible modeline element
317 is okay. See `modeline-format'.")
319 ;; Used by C code (lookup-key and friends) but defined here.
320 (defvar minor-mode-map-alist nil
321 "Alist of keymaps to use for minor modes.
322 Each element looks like (VARIABLE . KEYMAP); KEYMAP is used to read
323 key sequences and look up bindings iff VARIABLE's value is non-nil.
324 If two active keymaps bind the same key, the keymap appearing earlier
325 in the list takes precedence.")
327 (make-face 'modeline-mousable-minor-mode
328 "Face for mousable minor-mode strings in the modeline.")
329 (set-face-parent 'modeline-mousable-minor-mode 'modeline-mousable nil
331 (when (featurep 'window-system)
332 (set-face-foreground 'modeline-mousable-minor-mode '("green4" "forestgreen")
333 nil '(default color win)))
335 (defvar modeline-mousable-minor-mode-extent (make-extent nil nil)
336 ;; alliteration at its finest.
337 "Extent managing the mousable minor mode modeline strings.")
338 (set-extent-face modeline-mousable-minor-mode-extent
339 'modeline-mousable-minor-mode)
341 ;; This replaces the idiom
343 ;; (or (assq 'isearch-mode minor-mode-alist)
344 ;; (setq minor-mode-alist
346 ;; (append minor-mode-alist
347 ;; '((isearch-mode isearch-mode))))))
349 (defun add-minor-mode (toggle name &optional keymap after toggle-fun)
350 "Add a minor mode to `minor-mode-alist' and `minor-mode-map-alist'.
352 TOGGLE is a symbol whose value as a variable specifies whether the
353 minor mode is active.
355 NAME is the name that should appear in the modeline. It should either
356 be a string beginning with a space, or a symbol with a similar string
359 KEYMAP is a keymap to make active when the minor mode is active.
361 AFTER is the toggling symbol used for another minor mode. If AFTER is
362 non-nil, then it is used to position the new mode in the minor-mode
365 TOGGLE-FUN specifies an interactive function that is called to toggle
366 the mode on and off; this affects what happens when button2 is pressed
367 on the mode, and when button3 is pressed somewhere in the list of
368 modes. If TOGGLE-FUN is nil and TOGGLE names an interactive function,
369 TOGGLE is used as the toggle function.
371 Example: (add-minor-mode 'view-minor-mode \" View\" view-mode-map)"
372 (let* ((add-elt #'(lambda (elt sym)
374 (cond ((null after) ; add to front
375 (push elt (symbol-value sym)))
376 ((and (not (eq after t))
377 (setq place (memq (assq after
379 (symbol-value sym))))
380 (push elt (cdr place)))
382 (set sym (append (symbol-value sym)
387 (check-argument-type 'commandp toggle-fun)
388 (when (commandp toggle)
389 (setq toggle-fun toggle)))
390 (when (and toggle-fun name)
391 (setq toggle-keymap (make-sparse-keymap
392 (intern (concat "modeline-minor-"
395 (define-key toggle-keymap 'button2
396 ;; defeat the DUMB-ASS byte-compiler, which tries to
397 ;; expand the macro at compile time and fucks up.
398 (eval '(make-modeline-command-wrapper toggle-fun)))
399 (put toggle 'modeline-toggle-function toggle-fun))
403 (cons (let ((extent (make-extent nil nil)))
404 (set-extent-keymap extent toggle-keymap)
407 (concat "button2 turns off "
408 (if (symbolp toggle-fun)
409 (symbol-name toggle-fun)
410 (symbol-name toggle))))
412 (cons modeline-mousable-minor-mode-extent name))
414 (if (setq el (assq toggle minor-mode-alist))
415 (setcdr el (list hacked-name))
417 (list toggle hacked-name)
418 'minor-mode-alist))))
420 (if (setq el (assq toggle minor-mode-map-alist))
424 'minor-mode-map-alist)))))
426 ;; #### TODO: Add `:menu-tag' keyword to add-minor-mode. Or create a
427 ;; separate function to manage the minor mode menu.
429 ;(put 'abbrev-mode :menu-tag "Abbreviation Expansion")
430 (add-minor-mode 'abbrev-mode " Abbrev")
431 ;; only when visiting a file...
432 (add-minor-mode 'overwrite-mode 'overwrite-mode)
433 ;(put 'auto-fill-function :menu-tag "Auto Fill")
434 (add-minor-mode 'auto-fill-function " Fill" nil nil 'auto-fill-mode)
436 ;(put 'defining-kbd-macro :menu-tag "Keyboard Macro")
437 (add-minor-mode 'defining-kbd-macro " Def" nil nil
440 (if defining-kbd-macro
442 ;; #### This means to disregard the last event.
443 ;; It is needed because the last recorded
444 ;; event is usually the mouse event that
445 ;; invoked the menu item (and this function),
446 ;; and having it in the macro causes problems.
447 (zap-last-kbd-macro-event)
449 (start-kbd-macro nil))))
451 (defun modeline-minor-mode-menu (event)
452 "The menu that pops up when you press `button3' inside the
453 parentheses on the modeline."
456 (set-buffer (event-buffer event))
457 (popup-menu-and-execute-in-window
463 (let* ((toggle-sym (car x))
464 (toggle-fun (or (get toggle-sym
465 'modeline-toggle-function)
466 (and (commandp toggle-sym)
468 (menu-tag (symbol-name (if (symbolp toggle-fun)
471 ;; Here a function should
472 ;; maybe be invoked to
473 ;; beautify the symbol's
479 ;; The following two are wrong
480 ;; because of possible name
482 ;:active (get toggle-sym :active t)
483 ;:included (get toggle-sym :included t)
485 :selected (and (boundp toggle-sym)
489 (string< (aref e1 0) (aref e2 0)))))
492 (defvar modeline-minor-mode-map (make-sparse-keymap 'modeline-minor-mode-map)
493 "Keymap consulted for mouse-clicks on the minor-mode modeline list.")
494 (define-key modeline-minor-mode-map 'button3 'modeline-minor-mode-menu)
496 (defvar modeline-minor-mode-extent (make-extent nil nil)
497 "Extent covering the minor mode modeline strings.")
498 (set-extent-face modeline-minor-mode-extent 'modeline-mousable)
499 (set-extent-keymap modeline-minor-mode-extent modeline-minor-mode-map)
502 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
506 (defun modeline-buffers-menu (event)
508 (popup-menu-and-execute-in-window
509 '("Buffers Popup Menu"
510 :filter buffers-menu-filter
511 ["List All Buffers" list-buffers t]
516 (defvar modeline-buffer-id-left-map
517 (make-sparse-keymap 'modeline-buffer-id-left-map)
518 "Keymap consulted for mouse-clicks on the left half of the buffer-id string.")
520 (defvar modeline-buffer-id-right-map
521 (make-sparse-keymap 'modeline-buffer-id-right-map)
522 "Keymap consulted for mouse-clicks on the right half of the buffer-id string.")
524 (define-key modeline-buffer-id-left-map 'button2 'mouse-unbury-buffer)
525 (define-key modeline-buffer-id-right-map 'button2 'mouse-bury-buffer)
526 (define-key modeline-buffer-id-left-map 'button3 'modeline-buffers-menu)
527 (define-key modeline-buffer-id-right-map 'button3 'modeline-buffers-menu)
529 (make-face 'modeline-buffer-id
530 "Face for the buffer ID string in the modeline.")
531 (set-face-parent 'modeline-buffer-id 'modeline nil '(default))
532 (when (featurep 'window-system)
533 (set-face-foreground 'modeline-buffer-id "blue4" nil '(default color win))
534 (set-face-font 'modeline-buffer-id [bold-italic] nil '(default mono win))
535 (set-face-font 'modeline-buffer-id [bold-italic] nil '(default grayscale win)))
536 (when (featurep 'tty)
537 (set-face-font 'modeline-buffer-id [bold-italic] nil '(default tty)))
539 (defvar modeline-buffer-id-extent (make-extent nil nil)
540 "Extent covering the whole of the buffer-id string.")
541 (set-extent-face modeline-buffer-id-extent 'modeline-buffer-id)
543 (defvar modeline-buffer-id-left-extent (make-extent nil nil)
544 "Extent covering the left half of the buffer-id string.")
545 (set-extent-keymap modeline-buffer-id-left-extent
546 modeline-buffer-id-left-map)
547 (set-extent-property modeline-buffer-id-left-extent 'help-echo
548 "button2 cycles to the previous buffer")
550 (defvar modeline-buffer-id-right-extent (make-extent nil nil)
551 "Extent covering the right half of the buffer-id string.")
552 (set-extent-keymap modeline-buffer-id-right-extent
553 modeline-buffer-id-right-map)
554 (set-extent-property modeline-buffer-id-right-extent 'help-echo
555 "button2 cycles to the next buffer")
557 (defconst modeline-buffer-identification
558 (list (cons modeline-buffer-id-left-extent (purecopy "XEmacs%N:"))
559 ; this used to be "XEmacs:"
560 (cons modeline-buffer-id-right-extent (purecopy " %17b")))
561 "Modeline control for identifying the buffer being displayed.
562 Its default value is \"XEmacs: %17b\" (NOT!). Major modes that edit things
563 other than ordinary files may change this (e.g. Info, Dired,...)")
564 (make-variable-buffer-local 'modeline-buffer-identification)
566 ;; These are for the sake of minor mode menu. #### All of this is
567 ;; kind of dirty. `add-minor-mode' started out as a simple substitute
568 ;; for (or (assq ...) ...) FSF stuff, but now is used for all kind of
569 ;; stuff. There should perhaps be a separate function to add toggles
570 ;; to the minor-mode-menu.
571 (add-minor-mode 'line-number-mode "")
572 (add-minor-mode 'column-number-mode "")
574 (defconst modeline-process nil
575 "Modeline control for displaying info on process status.
576 Normally nil in most modes, since there is no process to display.")
577 (make-variable-buffer-local 'modeline-process)
579 (defvar modeline-modified-map (make-sparse-keymap 'modeline-modified-map)
580 "Keymap consulted for mouse-clicks on the modeline-modified string.")
581 (define-key modeline-modified-map 'button2
582 (make-modeline-command-wrapper 'modeline-toggle-read-only))
584 (defvar modeline-modified-extent (make-extent nil nil)
585 "Extent covering the modeline-modified string.")
586 (set-extent-face modeline-modified-extent 'modeline-mousable)
587 (set-extent-keymap modeline-modified-extent modeline-modified-map)
588 (set-extent-property modeline-modified-extent 'help-echo
589 "button2 toggles the buffer's read-only status")
591 (defconst modeline-modified (purecopy '("--%1*%1+-"))
592 "Modeline control for displaying whether current buffer is modified.")
593 (make-variable-buffer-local 'modeline-modified)
595 (defvar modeline-narrowed-map (make-sparse-keymap 'modeline-narrowed-map)
596 "Keymap consulted for mouse-clicks on the modeline-narrowed string.")
597 (define-key modeline-narrowed-map 'button2
598 (make-modeline-command-wrapper 'widen))
600 (defvar modeline-narrowed-extent (make-extent nil nil)
601 "Extent covering the modeline-narrowed string.")
602 (set-extent-face modeline-narrowed-extent 'modeline-mousable)
603 (set-extent-keymap modeline-narrowed-extent modeline-narrowed-map)
604 (set-extent-property modeline-narrowed-extent 'help-echo
605 "button2 widens the buffer")
611 (cons modeline-modified-extent 'modeline-modified)
612 (cons modeline-buffer-id-extent 'modeline-buffer-identification)
616 (cons modeline-minor-mode-extent
617 (list (purecopy "") 'mode-name 'minor-mode-alist))
618 (cons modeline-narrowed-extent (purecopy "%n"))
621 (list 'line-number-mode (purecopy "L%l--"))
622 (list 'column-number-mode (purecopy "C%c--"))
623 (cons -3 (purecopy "%p"))
626 ;;; Added for XEmacs 20.3. Provide wrapper for vc since it may not always be
627 ;;; present, and its symbols are not visible this early in the dump if it
630 (defun modeline-toggle-read-only ()
631 "Change whether this buffer is visiting its file read-only.
632 With arg, set read-only iff arg is positive.
633 This function is designed to be called when the read-only indicator on the
634 modeline is clicked. It will call `vc-toggle-read-only' if available,
635 otherwise it will call the usual `toggle-read-only'."
637 (if (fboundp 'vc-toggle-read-only)
638 (vc-toggle-read-only)
641 ;;; modeline.el ends here