Document reserved keys
[emacs.git] / lisp / dframe.el
blob12cedaf5201cb1ad0e2290c92cab41c5e0b06587
1 ;;; dframe --- dedicate frame support modes -*- lexical-binding:t -*-
3 ;; Copyright (C) 1996-2018 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: file, tags, tools
8 (defvar dframe-version "1.3"
9 "The current version of the dedicated frame library.")
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This code was developed and maintained as a part of speedbar since 1996.
29 ;; It became its own support utility in Aug 2000.
31 ;; Dedicated frame mode is an Emacs independent library for supporting
32 ;; a program/buffer combination that resides in a dedicated frame.
33 ;; Support of this nature requires several complex interactions with the
34 ;; user which this library will provide, including:
36 ;; * Creation of a frame. Positioned relatively.
37 ;; Includes a frame cache for User position caching.
38 ;; * Switching between frames.
39 ;; * Timed activities using idle-timers
40 ;; * Frame/buffer killing hooks
41 ;; * Mouse-3 position relative menu
42 ;; * Mouse motion, help-echo hacks
43 ;; * Mouse clicking, double clicking, & XEmacs image clicking hack
44 ;; * Mode line hacking
45 ;; * Utilities for use in a program covering:
46 ;; o keymap massage for some actions
47 ;; o working with an associated buffer
48 ;; o shift-click
49 ;; o detaching a frame
50 ;; o focus-shifting & optional frame jumping
51 ;; o currently active frame.
52 ;; o message/y-or-n-p
53 ;; o mouse set point
55 ;; To Use:
56 ;; 1) (require 'dframe)
57 ;; 2) Variable Setup:
58 ;; -frame-parameters -- Frame parameters for Emacs.
59 ;; -frame-plist -- Frame parameters for XEmacs.
60 ;; -- Not on parameter lists: They can optionally include width
61 ;; and height. If width or height is not included, then it will
62 ;; be provided to match the originating frame. In general,
63 ;; turning off the menu bar, mode line, and minibuffer can
64 ;; provide a smaller window, or more display area.
65 ;; -track-mouse-flag -- mouse tracking on/off specific to your tool.
66 ;; -update-flag -- app toggle for timer use. Init from
67 ;; `dframe-have-timer-flag'. This is nil for terminals, since
68 ;; updating a frame in a terminal is not useful to the user.
69 ;; -key-map -- Your keymap. Call `dframe-update-keymap' on it.
70 ;; -buffer, -frame, -cached-frame -- Variables used to track your
71 ;; applications buffer, frame, or frame cache (when hidden). See
72 ;; `dframe-frame-mode' for details.
73 ;; -before-delete-hook, -before-popup-hook, -after-create-hook --
74 ;; Hooks to have called. The `-after-create-hook' probably wants
75 ;; to call a function which calls `dframe-reposition-frame' in an
76 ;; appropriate manner.
77 ;; 3) Function Setup:
78 ;; your-frame-mode -- function to toggle your app frame on and off.
79 ;; its tasks are:
80 ;; a) create a buffer
81 ;; b) Call `dframe-frame-mode'. (See its doc)
82 ;; c) If successful (your -frame variable has a value), call
83 ;; timer setup if applicable.
84 ;; your-frame-reposition- -- Function to call from after-create-hook to
85 ;; reposition your frame with `dframe-reposition-frame'.
86 ;; your-mode -- Set up the major mode of the buffer for your app.
87 ;; Set these variables: dframe-track-mouse-function,
88 ;; dframe-help-echo-function,
89 ;; dframe-mouse-click-function,
90 ;; dframe-mouse-position-function.
91 ;; See speedbar's implementation of these functions.
92 ;; `speedbar-current-frame', `speedbar-get-focus', `speedbar-message',
93 ;; `speedbar-y-or-n-p', `speedbar-set-timer', `speedbar-click',
94 ;; `speedbar-position-cursor-on-line'
95 ;; 4) Handling mouse clicks, and help text:
96 ;; dframe-track-mouse, dframe-help-echo-function --
97 ;; These variables need to be set to functions that display info
98 ;; based on the mouse's position.
99 ;; Text property 'help-echo, set to `dframe-help-echo', which will
100 ;; call `dframe-help-echo-function'.
101 ;; Have a `-click' function, it can call `dframe-quick-mouse' for
102 ;; positioning. If the variable `dframe-power-click' is non-nil,
103 ;; then `shift' was held down during the click.
105 ;;; Bugs
107 ;; * The timer managers doesn't handle multiple different timeouts.
108 ;; * You can't specify continuous timeouts (as opposed to just idle timers.)
110 (defvar x-pointer-hand2)
111 (defvar x-pointer-top-left-arrow)
113 ;;; Code:
115 ;;; Compatibility functions
117 (defalias 'dframe-frame-parameter
118 (if (fboundp 'frame-parameter) 'frame-parameter
119 (lambda (frame parameter)
120 "Return FRAME's PARAMETER value."
121 (cdr (assoc parameter (frame-parameters frame))))))
124 ;;; Variables
126 (defgroup dframe nil
127 "Faces used in dframe."
128 :prefix "dframe-"
129 :group 'dframe)
131 (defvar dframe-have-timer-flag (if (fboundp 'display-graphic-p)
132 (display-graphic-p)
133 window-system)
134 "Non-nil means that timers are available for this Emacs.
135 This is nil for terminals, since updating a frame in a terminal
136 is not useful to the user.")
138 (defcustom dframe-update-speed
139 (if (featurep 'xemacs) 2 ; 1 is too obtrusive in XEmacs
141 "Idle time in seconds needed before dframe will update itself.
142 Updates occur to allow dframe to display directory information
143 relevant to the buffer you are currently editing."
144 :group 'dframe
145 :type 'integer)
147 (defcustom dframe-activity-change-focus-flag nil
148 "Non-nil means the selected frame will change based on activity.
149 Thus, if a file is selected for edit, the buffer will appear in the
150 selected frame and the focus will change to that frame."
151 :group 'dframe
152 :type 'boolean)
154 (defcustom dframe-after-select-attached-frame-hook nil
155 "Hook run after dframe has selected the attached frame."
156 :group 'dframe
157 :type 'hook)
159 (defvar dframe-track-mouse-function nil
160 "A function to call when the mouse is moved in the given frame.
161 Typically used to display info about the line under the mouse.")
162 (make-variable-buffer-local 'dframe-track-mouse-function)
164 (defvar dframe-help-echo-function nil
165 "A function to call when help-echo is used in newer versions of Emacs.
166 Typically used to display info about the line under the mouse.")
167 (make-variable-buffer-local 'dframe-help-echo-function)
169 (defvar dframe-mouse-click-function nil
170 "A function to call when the mouse is clicked.
171 Valid clicks are mouse 2, our double mouse 1.")
172 (make-variable-buffer-local 'dframe-mouse-click-function)
174 (defvar dframe-mouse-position-function nil
175 "A function to call to position the cursor for a mouse click.")
176 (make-variable-buffer-local 'dframe-mouse-position-function)
178 (defvar dframe-power-click nil
179 "Never set this by hand. Value is t when S-mouse activity occurs.")
181 (defvar dframe-timer nil
182 "The dframe timer used for updating the buffer.")
183 (make-variable-buffer-local 'dframe-timer)
185 (defvar dframe-attached-frame nil
186 "The frame which started a frame mode.
187 This is the frame from which all interesting activities will go
188 for the mode using dframe.")
189 (make-variable-buffer-local 'dframe-attached-frame)
191 (defvar dframe-controlled nil
192 "Is this buffer controlled by a dedicated frame.
193 Local to those buffers, as a function called that created it.")
194 (make-variable-buffer-local 'dframe-controlled)
196 (defun dframe-update-keymap (map)
197 "Update the keymap MAP for dframe default bindings."
198 ;; Frame control
199 (define-key map "q" 'dframe-close-frame)
200 (define-key map "Q" 'delete-frame)
202 ;; Override switch to buffer to never hack our frame.
203 (substitute-key-definition 'switch-to-buffer
204 'dframe-switch-buffer-attached-frame
205 map global-map)
207 (if (featurep 'xemacs)
208 (progn
209 ;; mouse bindings so we can manipulate the items on each line
210 (define-key map 'button2 'dframe-click)
211 (define-key map '(shift button2) 'dframe-power-click)
212 ;; Info doc fix from Bob Weiner
213 (if (featurep 'infodoc)
215 (define-key map 'button3 'dframe-popup-kludge))
218 ;; mouse bindings so we can manipulate the items on each line
219 ;; (define-key map [down-mouse-1] 'dframe-double-click)
220 (define-key map [follow-link] 'mouse-face)
221 (define-key map [mouse-2] 'dframe-click)
222 ;; This is the power click for new frames, or refreshing a cache
223 (define-key map [S-mouse-2] 'dframe-power-click)
224 ;; This adds a small unnecessary visual effect
225 ;;(define-key map [down-mouse-2] 'dframe-quick-mouse)
227 (define-key map [down-mouse-3] 'dframe-popup-kludge)
229 ;; This lets the user scroll as if we had a scrollbar... well maybe not
230 (define-key map [mode-line mouse-2] 'dframe-mouse-hscroll)
231 ;; another handy place users might click to get our menu.
232 (define-key map [mode-line down-mouse-1]
233 'dframe-popup-kludge)
235 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
236 (define-key map [C-down-mouse-1] 'dframe-hack-buffer-menu)
238 ;; Lastly, we want to track the mouse. Play here
239 (define-key map [mouse-movement] 'dframe-track-mouse)
242 (defun dframe-live-p (frame)
243 "Return non-nil if FRAME is currently available."
244 (and frame (frame-live-p frame) (frame-visible-p frame)))
246 (defvar x-sensitive-text-pointer-shape)
247 (defvar x-pointer-shape)
249 (defun dframe-frame-mode (arg frame-var cache-var buffer-var frame-name
250 local-mode-fn
251 &optional
252 parameters
253 delete-hook popup-hook create-hook
255 "Manage a frame for an application, enabling it when ARG is positive.
256 FRAME-VAR is a variable used to cache the frame being used.
257 This frame is either resurrected, hidden, killed, etc based on
258 the value.
259 CACHE-VAR is a variable used to cache a cached frame.
260 BUFFER-VAR is a variable used to cache the buffer being used in dframe.
261 This buffer will have `dframe-frame-mode' run on it.
262 FRAME-NAME is the name of the frame to create.
263 LOCAL-MODE-FN is the function used to call this one.
264 PARAMETERS are frame parameters to apply to this dframe.
265 DELETE-HOOK is a hook to run when deleting a frame.
266 POPUP-HOOK is a hook to run before showing a frame.
267 CREATE-HOOK is a hook to run after creating a frame."
268 (let ((conv-hook (lambda (val)
269 (let ((sym (make-symbol "hook")))
270 (set sym val) sym))))
271 (if (consp delete-hook) (setq delete-hook (funcall conv-hook delete-hook)))
272 (if (consp create-hook) (setq create-hook (funcall conv-hook create-hook)))
273 (if (consp popup-hook) (setq popup-hook (funcall conv-hook popup-hook))))
274 ;; toggle frame on and off.
275 (if (not arg) (if (dframe-live-p (symbol-value frame-var))
276 (setq arg -1) (setq arg 1)))
277 ;; Make sure the current buffer is set.
278 (set-buffer (symbol-value buffer-var))
279 ;; turn the frame off on neg number
280 (if (and (numberp arg) (< arg 0))
281 (progn
282 (run-hooks delete-hook)
283 (if (and (symbol-value frame-var)
284 (frame-live-p (symbol-value frame-var)))
285 (progn
286 (set cache-var (symbol-value frame-var))
287 (make-frame-invisible (symbol-value frame-var))))
288 (set frame-var nil))
289 ;; Set this as our currently attached frame
290 (setq dframe-attached-frame (selected-frame))
291 (run-hooks 'dframe-setup-hook)
292 (run-hooks popup-hook)
293 ;; Updated the buffer passed in to contain all the hacks needed
294 ;; to make it work well in a dedicated window.
295 (with-current-buffer (symbol-value buffer-var)
296 ;; Declare this buffer a dedicated frame
297 (setq dframe-controlled local-mode-fn)
299 (if (featurep 'xemacs)
300 (progn
301 ;; Hack the XEmacs mouse-motion handler
302 (set (make-local-variable 'mouse-motion-handler)
303 'dframe-track-mouse-xemacs)
304 ;; Hack the double click handler
305 (make-local-variable 'mouse-track-click-hook)
306 (add-hook 'mouse-track-click-hook
307 (lambda (event count)
308 (if (/= (event-button event) 1)
309 nil ; Do normal operations.
310 (cond ((eq count 1)
311 (dframe-quick-mouse event))
312 ((or (eq count 2)
313 (eq count 3))
314 (dframe-click event)
315 (dframe-quick-mouse event)))
316 ;; Don't do normal operations.
317 t))))
318 ;; Enable mouse tracking in emacs
319 (if dframe-track-mouse-function
320 (set (make-local-variable 'track-mouse) t))) ;this could be messy.
321 ;;;; DISABLED: This causes problems for users with multiple frames.
322 ;;;; ;; Set this up special just for the passed in buffer
323 ;;;; ;; Terminal minibuffer stuff does not require this.
324 ;;;; (if (and (or (assoc 'minibuffer parameters)
325 ;;;; ;; XEmacs plist is not an association list
326 ;;;; (member 'minibuffer parameters))
327 ;;;; window-system (not (eq window-system 'pc))
328 ;;;; (null default-minibuffer-frame))
329 ;;;; (progn
330 ;;;; (make-local-variable 'default-minibuffer-frame)
331 ;;;; (setq default-minibuffer-frame dframe-attached-frame))
332 ;;;; )
333 ;; Override `temp-buffer-show-hook' so that help and such
334 ;; put their stuff into a frame other than our own.
335 ;; Correct use of `temp-buffer-show-function': Bob Weiner
336 (if (and (boundp 'temp-buffer-show-hook)
337 (boundp 'temp-buffer-show-function))
338 (progn (make-local-variable 'temp-buffer-show-hook)
339 (setq temp-buffer-show-hook temp-buffer-show-function)))
340 (make-local-variable 'temp-buffer-show-function)
341 (setq temp-buffer-show-function 'dframe-temp-buffer-show-function)
342 ;; If this buffer is killed, we must make sure that we destroy
343 ;; the frame the dedicated window is in.
344 (add-hook 'kill-buffer-hook (lambda ()
345 (let ((skilling (boundp 'skilling)))
346 (if skilling
348 (if dframe-controlled
349 (progn
350 (funcall dframe-controlled -1)
351 (set buffer-var nil)
352 )))))
353 t t)
355 ;; Get the frame to work in
356 (if (frame-live-p (symbol-value cache-var))
357 (progn
358 (set frame-var (symbol-value cache-var))
359 (make-frame-visible (symbol-value frame-var))
360 (select-frame (symbol-value frame-var))
361 (set-window-dedicated-p (selected-window) nil)
362 (if (not (eq (current-buffer) (symbol-value buffer-var)))
363 (switch-to-buffer (symbol-value buffer-var)))
364 (set-window-dedicated-p (selected-window) t)
365 (raise-frame (symbol-value frame-var))
367 (if (frame-live-p (symbol-value frame-var))
368 (raise-frame (symbol-value frame-var))
369 (set frame-var
370 (if (featurep 'xemacs)
371 ;; Only guess height if it is not specified.
372 (if (member 'height parameters)
373 (make-frame parameters)
374 (make-frame (nconc (list 'height
375 (dframe-needed-height))
376 parameters)))
377 (let* ((mh (dframe-frame-parameter dframe-attached-frame
378 'menu-bar-lines))
379 (paramsa
380 ;; Only add a guessed height if one is not specified
381 ;; in the input parameters.
382 (if (assoc 'height parameters)
383 parameters
384 (append
385 parameters
386 (list (cons 'height (+ (or mh 0) (frame-height)))))))
387 (params
388 ;; Only add a guessed width if one is not specified
389 ;; in the input parameters.
390 (if (assoc 'width parameters)
391 paramsa
392 (append
393 paramsa
394 (list (cons 'width (frame-width))))))
395 (frame
396 (if (not (eq window-system 'x))
397 (make-frame params)
398 (let ((x-pointer-shape x-pointer-top-left-arrow)
399 (x-sensitive-text-pointer-shape
400 x-pointer-hand2))
401 (make-frame params)))))
402 frame)))
403 ;; Put the buffer into the frame
404 (save-excursion
405 (select-frame (symbol-value frame-var))
406 (switch-to-buffer (symbol-value buffer-var))
407 (set-window-dedicated-p (selected-window) t))
408 ;; Run hooks (like reposition)
409 (run-hooks create-hook)
410 ;; Frame name
411 (if (and (or (null window-system) (eq window-system 'pc))
412 (fboundp 'set-frame-name))
413 (save-window-excursion
414 (select-frame (symbol-value frame-var))
415 (set-frame-name frame-name)))
416 ;; On a terminal, raise the frame or the user will
417 ;; be confused.
418 (if (not window-system)
419 (select-frame (symbol-value frame-var)))
420 ))) )
422 (defun dframe-reposition-frame (new-frame parent-frame location)
423 "Move NEW-FRAME to be relative to PARENT-FRAME.
424 LOCATION can be one of `random', `left', `right', `left-right', or `top-bottom'."
425 (if (featurep 'xemacs)
426 (dframe-reposition-frame-xemacs new-frame parent-frame location)
427 (dframe-reposition-frame-emacs new-frame parent-frame location)))
429 ;; Not defined in builds without X, but behind window-system test.
430 (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
431 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
433 (defun dframe-reposition-frame-emacs (new-frame parent-frame location)
434 "Move NEW-FRAME to be relative to PARENT-FRAME.
435 LOCATION can be one of `random', `left-right', `top-bottom', or
436 a cons cell indicating a position of the form (LEFT . TOP)."
437 ;; Position dframe.
438 ;; Do no positioning if not on a windowing system,
439 (unless (or (not window-system) (eq window-system 'pc))
440 (let* ((pfx (dframe-frame-parameter parent-frame 'left))
441 (pfy (dframe-frame-parameter parent-frame 'top))
442 (pfw (+ (tool-bar-pixel-width parent-frame)
443 (frame-pixel-width parent-frame)))
444 (pfh (frame-pixel-height parent-frame))
445 (nfw (frame-pixel-width new-frame))
446 (nfh (frame-pixel-height new-frame))
447 newleft newtop)
448 ;; Rebuild pfx,pfy to be absolute positions.
449 (setq pfx (if (not (consp pfx))
451 ;; If pfx is a list, that means we grow
452 ;; from a specific edge of the display.
453 ;; Convert that to the distance from the
454 ;; left side of the display.
455 (if (eq (car pfx) '-)
456 ;; A - means distance from the right edge
457 ;; of the display, or DW - pfx - framewidth
458 (- (x-display-pixel-width) (car (cdr pfx)) pfw)
459 (car (cdr pfx))))
460 pfy (if (not (consp pfy))
462 ;; If pfy is a list, that means we grow
463 ;; from a specific edge of the display.
464 ;; Convert that to the distance from the
465 ;; left side of the display.
466 (if (eq (car pfy) '-)
467 ;; A - means distance from the right edge
468 ;; of the display, or DW - pfx - framewidth
469 (- (x-display-pixel-height) (car (cdr pfy)) pfh)
470 (car (cdr pfy)))))
471 (cond ((eq location 'right)
472 (setq newleft (+ pfx pfw 10)
473 newtop pfy))
474 ((eq location 'left)
475 (setq newleft (- pfx 10 nfw)
476 newtop pfy))
477 ((eq location 'left-right)
478 (setq newleft
479 ;; Decide which side to put it on. 200 is just a
480 ;; buffer for the left edge of the screen. The
481 ;; extra 10 is just dressings for window
482 ;; decorations.
483 (let* ((left-guess (- pfx 10 nfw))
484 (right-guess (+ pfx pfw 10))
485 (left-margin left-guess)
486 (right-margin (- (x-display-pixel-width)
487 right-guess 5 nfw)))
488 (cond ((>= left-margin 0) left-guess)
489 ((>= right-margin 0) right-guess)
490 ;; otherwise choose side we overlap less
491 ((> left-margin right-margin) 0)
492 (t (- (x-display-pixel-width) nfw 5))))
493 newtop pfy))
494 ((eq location 'top-bottom)
495 (setq newleft pfx
496 newtop
497 ;; Try and guess if we should be on the top or bottom.
498 (let* ((top-guess (- pfy 15 nfh))
499 (bottom-guess (+ pfy 5 pfh))
500 (top-margin top-guess)
501 (bottom-margin (- (x-display-pixel-height)
502 bottom-guess 5 nfh)))
503 (cond ((>= top-margin 0) top-guess)
504 ((>= bottom-margin 0) bottom-guess)
505 ;; Choose a side to overlap the least.
506 ((> top-margin bottom-margin) 0)
507 (t (- (x-display-pixel-height) nfh 5))))))
508 ((consp location)
509 (setq newleft (or (car location) 0)
510 newtop (or (cdr location) 0)))
511 (t nil))
512 (modify-frame-parameters new-frame
513 (list (cons 'left newleft)
514 (cons 'top newtop))))))
516 (defun dframe-reposition-frame-xemacs (_new-frame _parent-frame _location)
517 "Move NEW-FRAME to be relative to PARENT-FRAME.
518 LOCATION can be one of `random', `left-right', or `top-bottom'."
519 ;; Not yet implemented
522 ;; XEmacs function only.
523 (defun dframe-needed-height (&optional frame)
524 "The needed height for the tool bar FRAME (in characters)."
525 (or frame (setq frame (selected-frame)))
526 ;; The 1 is the missing mode line or minibuffer
527 (+ 1 (/ (frame-pixel-height frame)
528 ;; This obscure code avoids a byte compiler warning in Emacs.
529 (let ((f 'face-height))
530 (funcall f 'default frame)))))
532 (defun dframe-detach (frame-var cache-var buffer-var)
533 "Detach the frame in symbol FRAME-VAR.
534 CACHE-VAR and BUFFER-VAR are symbols as in `dframe-frame-mode'."
535 (with-current-buffer (symbol-value buffer-var)
536 (rename-buffer (buffer-name) t)
537 (let ((oldframe (symbol-value frame-var)))
538 (set buffer-var nil)
539 (set frame-var nil)
540 (set cache-var nil)
541 ;; FIXME: Looks very suspicious. Luckily this function is unused.
542 (make-variable-buffer-local frame-var)
543 (set frame-var oldframe)
546 ;;; Special frame event proxies
547 (defvar dframe-setup-hook nil
548 "Used for setting frame special event bindings.")
550 (defun dframe-set-special-events ()
551 (define-key special-event-map [make-frame-visible]
552 'dframe-handle-make-frame-visible)
553 (define-key special-event-map [iconify-frame]
554 'dframe-handle-iconify-frame)
555 (define-key special-event-map [delete-frame]
556 'dframe-handle-delete-frame)
557 ;; Only need to run once.
558 (remove-hook 'dframe-setup-hook #'dframe-set-special-events))
560 (when (boundp 'special-event-map)
561 (add-hook 'dframe-setup-hook #'dframe-set-special-events))
563 (defvar dframe-make-frame-visible-function nil
564 "Function used when a dframe controlled frame is de-iconified.
565 The function must take an EVENT.")
566 (defvar dframe-iconify-frame-function nil
567 "Function used when a dframe controlled frame is iconified.
568 The function must take an EVENT.")
569 (defvar dframe-delete-frame-function nil
570 "Function used when a frame attached to a dframe frame is deleted.
571 The function must take an EVENT.")
573 (defun dframe-handle-make-frame-visible (e)
574 "Handle a `make-frame-visible' event.
575 Should enable auto-updating if the last state was also enabled.
576 Argument E is the event making the frame visible."
577 (interactive "e")
578 (let ((f last-event-frame))
579 (if (and (dframe-attached-frame f)
580 dframe-make-frame-visible-function)
581 (funcall dframe-make-frame-visible-function e)
584 (defun dframe-handle-iconify-frame (e)
585 "Handle a `iconify-frame' event.
586 Should disable auto-updating if the last state was also enabled.
587 Argument E is the event iconifying the frame."
588 (interactive "e")
589 (let ((f last-event-frame))
590 (if (and (dframe-attached-frame f)
591 dframe-iconify-frame-function e)
592 (funcall dframe-iconify-frame-function)
595 (defun dframe-handle-delete-frame (e)
596 "Handle `delete-frame' event.
597 Argument E is the event deleting the frame."
598 (interactive "e")
599 (let ((fl (frame-list))
600 (sf (selected-frame)))
601 ;; Loop over all frames. If dframe-delete-frame-function is
602 ;; non-nil, call it.
603 (while fl
604 (select-frame (car fl))
605 (if dframe-delete-frame-function
606 (funcall dframe-delete-frame-function e))
607 (setq fl (cdr fl)))
608 (if (frame-live-p sf)
609 (select-frame sf))
610 (handle-delete-frame e)))
613 ;;; Utilities
615 (defun dframe-get-focus (frame-var activator)
616 "Change frame focus to or from a dedicated frame.
617 If the selected frame is not in the symbol FRAME-VAR, then FRAME-VAR
618 frame is selected. If the FRAME-VAR is active, then select the
619 attached frame. If FRAME-VAR is nil, ACTIVATOR is called to
620 created it."
621 (interactive)
622 (if (eq (selected-frame) (symbol-value frame-var))
623 (if (frame-live-p dframe-attached-frame)
624 (dframe-select-attached-frame))
625 ;; make sure we have a frame
626 (if (not (frame-live-p (symbol-value frame-var)))
627 (funcall activator 1))
628 ;; go there
629 (select-frame (symbol-value frame-var))
631 (other-frame 0))
634 (defun dframe-close-frame ()
635 "Close the current frame if it is dedicated."
636 (interactive)
637 (if dframe-controlled
638 (let ((b (current-buffer)))
639 (funcall dframe-controlled -1)
640 (kill-buffer b))))
642 (defun dframe-current-frame (frame-var desired-major-mode)
643 "Return the existing dedicated frame to use.
644 FRAME-VAR is the variable storing the currently active dedicated frame.
645 If the current frame's buffer uses DESIRED-MAJOR-MODE, then use that frame."
646 (if (not (eq (selected-frame) (symbol-value frame-var)))
647 (if (and (eq major-mode desired-major-mode)
648 (get-buffer-window (current-buffer))
649 (window-frame (get-buffer-window (current-buffer))))
650 (window-frame (get-buffer-window (current-buffer)))
651 (symbol-value frame-var))
652 (symbol-value frame-var)))
654 (defun dframe-attached-frame (&optional frame)
655 "Return the attached frame belonging to the dframe controlled frame FRAME.
656 If optional arg FRAME is nil just return `dframe-attached-frame'."
657 (save-excursion
658 (if frame (select-frame frame))
659 dframe-attached-frame))
661 (defun dframe-select-attached-frame (&optional frame)
662 "Switch to the frame the dframe controlled frame FRAME was started from.
663 If optional arg FRAME is nil assume the attached frame is already selected
664 and just run the hooks `dframe-after-select-attached-frame-hook'. Return
665 the attached frame."
666 (let ((frame (dframe-attached-frame frame)))
667 (if frame (select-frame frame))
668 (prog1 frame
669 (run-hooks 'dframe-after-select-attached-frame-hook))))
671 (defmacro dframe-with-attached-buffer (&rest forms)
672 "Execute FORMS in the attached frame's special buffer.
673 Optionally select that frame if necessary."
674 `(save-selected-window
675 ;;(speedbar-set-timer speedbar-update-speed)
676 (dframe-select-attached-frame)
677 ,@forms
678 (dframe-maybee-jump-to-attached-frame)))
680 (defun dframe-maybee-jump-to-attached-frame ()
681 "Jump to the attached frame ONLY if this was not a mouse event."
682 (when (or (not (dframe-mouse-event-p last-input-event))
683 dframe-activity-change-focus-flag)
684 (dframe-select-attached-frame)
685 ;; KB: For what is this - raising the frame??
686 (other-frame 0)))
689 (defvar dframe-suppress-message-flag nil
690 "Non-nil means that `dframe-message' should just return a string.")
692 (defun dframe-message (fmt &rest args)
693 "Like `message', but for use in a dedicated frame.
694 Argument FMT is the format string, and ARGS are the arguments for message."
695 (save-selected-window
696 (if dframe-suppress-message-flag
697 (apply #'format-message fmt args)
698 (if dframe-attached-frame
699 ;; KB: Here we do not need calling `dframe-select-attached-frame'
700 (select-frame dframe-attached-frame))
701 (apply #'message fmt args))))
703 (defun dframe-y-or-n-p (prompt)
704 "Like `y-or-n-p', but for use in a dedicated frame.
705 Argument PROMPT is the prompt to use."
706 (save-selected-window
707 (if (and ;;default-minibuffer-frame
708 dframe-attached-frame
709 ;;(not (eq default-minibuffer-frame dframe-attached-frame))
711 ;; KB: Here we do not need calling `dframe-select-attached-frame'
712 (select-frame dframe-attached-frame))
713 (y-or-n-p prompt)))
715 ;;; timer management
717 ;; Unlike speedbar with a dedicated set of routines, dframe has one master
718 ;; timer, and all dframe users will use it. At least until I figure out a way
719 ;; around that problem.
721 ;; Advantage 1: Two apps with timer/frames can munge the master list
722 ;; to make sure they occur in order.
723 ;; Advantage 2: If a user hits a key between timer functions, we can
724 ;; interrupt them safely.
725 (defvar dframe-client-functions nil
726 "List of client functions using the dframe timer.")
728 (defun dframe-set-timer (timeout fn &optional _null-on-error)
729 "Apply a timer with TIMEOUT, to call FN, or remove a timer if TIMEOUT is nil.
730 TIMEOUT is the number of seconds until the dframe controlled program
731 timer is called again. When TIMEOUT is nil, turn off all timeouts.
732 This function must be called from the buffer belonging to the program
733 who requested the timer. NULL-ON-ERROR is ignored."
734 ;; First, fix up our list of client functions
735 (if timeout
736 (add-to-list 'dframe-client-functions fn)
737 (setq dframe-client-functions (delete fn dframe-client-functions)))
738 ;; Now decided what to do about the timeout.
739 (if (or
740 ;; We have a timer, restart the timer with the new time.
741 timeout
742 ;; We have a timer, an off is requested, and no client
743 ;; functions are left, shut er down.
744 (and dframe-timer (not timeout) dframe-client-functions))
745 ;; Only call the low level function if we are changing the state.
746 (dframe-set-timer-internal timeout)))
748 (defun dframe-set-timer-internal (timeout &optional _null-on-error)
749 "Apply a timer with TIMEOUT to call the dframe timer manager."
750 (when dframe-timer
751 (if (featurep 'xemacs)
752 (delete-itimer dframe-timer)
753 (cancel-timer dframe-timer))
754 (setq dframe-timer nil))
755 (when timeout
756 (setq dframe-timer
757 (if (featurep 'xemacs)
758 (start-itimer "dframe" 'dframe-timer-fn
759 timeout timeout t)
760 (run-with-idle-timer timeout t 'dframe-timer-fn)))))
762 (defun dframe-timer-fn ()
763 "Called due to the dframe timer.
764 Evaluates all cached timer functions in sequence."
765 (let ((l dframe-client-functions))
766 (while (and l (sit-for 0))
767 (with-demoted-errors "DFRAME TIMER ERROR: %S"
768 (funcall (car l)))
769 (setq l (cdr l)))))
771 ;;; Menu hacking for mouse-3
773 (defconst dframe-pass-event-to-popup-mode-menu
774 (let (max-args)
775 (and (fboundp 'popup-mode-menu)
776 (fboundp 'function-max-args)
777 (setq max-args (function-max-args 'popup-mode-menu))
778 (not (zerop max-args))))
779 "The EVENT arg to `popup-mode-menu' was introduced in XEmacs 21.4.0.")
781 ;; In XEmacs, we make popup menus work on the item over mouse (as
782 ;; opposed to where the point happens to be.) We attain this by
783 ;; temporarily moving the point to that place.
784 ;; Hrvoje Niksic <hniksic@srce.hr>
785 (defalias 'dframe-popup-kludge
786 (if (featurep 'xemacs)
787 (lambda (event) ; XEmacs.
788 "Pop up a menu related to the clicked on item.
789 Must be bound to EVENT."
790 (interactive "e")
791 (save-excursion
792 (if dframe-pass-event-to-popup-mode-menu
793 (popup-mode-menu event)
794 (goto-char (event-closest-point event))
795 (beginning-of-line)
796 (forward-char (min 5 (- (line-end-position)
797 (line-beginning-position))))
798 (popup-mode-menu))
799 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
800 ;; menu functions) return immediately.
801 (let (new)
802 (while (not (misc-user-event-p (setq new (next-event))))
803 (dispatch-event new))
804 (dispatch-event new))))
806 (lambda (e) ; Emacs.
807 "Pop up a menu related to the clicked on item.
808 Must be bound to event E."
809 (interactive "e")
810 (save-excursion
811 (mouse-set-point e)
812 ;; This gets the cursor where the user can see it.
813 (if (not (bolp)) (forward-char -1))
814 (sit-for 0)
815 (if (fboundp 'mouse-menu-major-mode-map)
816 (popup-menu (mouse-menu-major-mode-map) e)
817 (with-no-warnings ; don't warn about obsolete fallback
818 (mouse-major-mode-menu e nil)))))))
820 ;;; Interactive user functions for the mouse
822 (defalias 'dframe-mouse-event-p
823 (if (featurep 'xemacs)
824 'button-press-event-p
825 (lambda (event)
826 "Return t if the event is a mouse related event."
827 (if (and (listp event)
828 (member (event-basic-type event)
829 '(mouse-1 mouse-2 mouse-3)))
831 nil))))
833 (defun dframe-track-mouse (event)
834 "For motion EVENT, display info about the current line."
835 (interactive "e")
836 (when (and dframe-track-mouse-function
837 (or (featurep 'xemacs) ;; XEmacs always safe?
838 (windowp (posn-window (event-end event))) ; Sometimes
839 ; there is no window to jump into.
842 (funcall dframe-track-mouse-function event)))
844 (defun dframe-track-mouse-xemacs (event)
845 "For motion EVENT, display info about the current line."
846 (if (functionp (default-value 'mouse-motion-handler))
847 (funcall (default-value 'mouse-motion-handler) event))
848 (if dframe-track-mouse-function
849 (funcall dframe-track-mouse-function event)))
851 (defun dframe-help-echo (_window &optional buffer position)
852 "Display help based context.
853 The context is in WINDOW, viewing BUFFER, at POSITION.
854 BUFFER and POSITION are optional because XEmacs doesn't use them."
855 (when (and (not dframe-track-mouse-function)
856 (bufferp buffer)
857 dframe-help-echo-function)
858 (let ((dframe-suppress-message-flag t))
859 (with-current-buffer buffer
860 (save-excursion
861 (if position (goto-char position))
862 (funcall dframe-help-echo-function))))))
864 (defun dframe-mouse-set-point (e)
865 "Set point based on event E.
866 Handles clicking on images in XEmacs."
867 (if (and (featurep 'xemacs)
868 (save-excursion
869 (save-window-excursion
870 (mouse-set-point e)
871 (event-over-glyph-p e))))
872 ;; We are in XEmacs, and clicked on a picture
873 (let ((ext (event-glyph-extent e)))
874 ;; This position is back inside the extent where the
875 ;; junk we pushed into the property list lives.
876 (if (extent-end-position ext)
877 (goto-char (1- (extent-end-position ext)))
878 (mouse-set-point e)))
879 ;; We are not in XEmacs, OR we didn't click on a picture.
880 (mouse-set-point e)))
882 (defun dframe-quick-mouse (e)
883 "Since mouse events are strange, this will keep the mouse nicely positioned.
884 This should be bound to mouse event E."
885 (interactive "e")
886 (dframe-mouse-set-point e)
887 (if dframe-mouse-position-function
888 (funcall dframe-mouse-position-function)))
890 (defun dframe-power-click (e)
891 "Activate any dframe mouse click as a power click.
892 A power click will dispose of cached data (if available) or bring a buffer
893 up into a different window.
894 This should be bound to mouse event E."
895 (interactive "e")
896 (let ((dframe-power-click t))
897 (select-frame last-event-frame)
898 (dframe-click e)))
900 (defun dframe-click (e)
901 "Call our clients click function on a user click.
902 E is the event causing the click."
903 (interactive "e")
904 (dframe-mouse-set-point e)
905 (when dframe-mouse-click-function
906 ;; On the off chance of buffer switch, or something incorrectly
907 ;; configured.
908 (funcall dframe-mouse-click-function e)))
910 (defun dframe-double-click (e)
911 "Activate the registered click function on a double click.
912 This must be bound to a mouse event.
913 This should be bound to mouse event E."
914 (interactive "e")
915 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
916 (cond ((eq (car e) 'down-mouse-1)
917 (dframe-mouse-set-point e))
918 ((eq (car e) 'mouse-1)
919 (dframe-quick-mouse e))
920 ((or (eq (car e) 'double-down-mouse-1)
921 (eq (car e) 'triple-down-mouse-1))
922 (dframe-click e))))
924 ;;; Hacks of normal things.
926 ;; Some normal things that happen in one of these dedicated frames
927 ;; must be handled specially, so that our dedicated frame isn't
928 ;; messed up.
929 (defun dframe-temp-buffer-show-function (buffer)
930 "Placed in the variable `temp-buffer-show-function' in dedicated frames.
931 If a user requests help using \\[help-command] <Key> the temp BUFFER will be
932 redirected into a window on the attached frame."
933 (if dframe-attached-frame (dframe-select-attached-frame))
934 (pop-to-buffer buffer nil)
935 (other-window -1)
936 ;; Fix for using this hook on some platforms: Bob Weiner
937 (cond ((not (featurep 'xemacs))
938 (run-hooks 'temp-buffer-show-hook))
939 ((fboundp 'run-hook-with-args)
940 (run-hook-with-args 'temp-buffer-show-hook buffer))
941 ((and (boundp 'temp-buffer-show-hook)
942 (listp temp-buffer-show-hook))
943 (mapcar (function (lambda (hook) (funcall hook buffer)))
944 temp-buffer-show-hook))))
946 (defun dframe-hack-buffer-menu (_e)
947 "Control mouse 1 is buffer menu.
948 This hack overrides it so that the right thing happens in the main
949 Emacs frame, not in the dedicated frame.
950 Argument E is the event causing this activity."
951 (interactive "e")
952 (let ((fn (lookup-key global-map (if (featurep 'xemacs)
953 '(control button1)
954 [C-down-mouse-1])))
955 (oldbuff (current-buffer))
956 (newbuff nil))
957 (unwind-protect
958 (save-excursion
959 (set-window-dedicated-p (selected-window) nil)
960 (call-interactively fn)
961 (setq newbuff (current-buffer)))
962 (switch-to-buffer oldbuff)
963 (set-window-dedicated-p (selected-window) t))
964 (if (not (eq newbuff oldbuff))
965 (dframe-with-attached-buffer
966 (switch-to-buffer newbuff)))))
968 (defun dframe-switch-buffer-attached-frame (&optional buffer)
969 "Switch to BUFFER in the attached frame, and raise that frame.
970 This overrides the default behavior of `switch-to-buffer' which is
971 broken because of the dedicated frame."
972 (interactive)
973 ;; Assume we are in the dedicated frame.
974 (other-frame 1)
975 ;; Now switch buffers
976 (if buffer
977 (switch-to-buffer buffer)
978 (call-interactively 'switch-to-buffer nil nil)))
980 ;; XEmacs: this can be implemented using mode line keymaps, but there
981 ;; is no use, as we have horizontal scrollbar (as the docstring
982 ;; hints.)
983 (defun dframe-mouse-hscroll (e)
984 "Read a mouse event E from the mode line, and horizontally scroll.
985 If the mouse is being clicked on the far left, or far right of the
986 mode-line. This is only useful for non-XEmacs."
987 (interactive "e")
988 (let* ((x-point (car (nth 2 (car (cdr e)))))
989 (pixels-per-10-col (/ (* 10 (frame-pixel-width))
990 (frame-width)))
991 (click-col (1+ (/ (* 10 x-point) pixels-per-10-col)))
993 (cond ((< click-col 3)
994 (scroll-left 2))
995 ((> click-col (- (window-width) 5))
996 (scroll-right 2))
997 (t (dframe-message
998 "Click on the edge of the mode line to scroll left/right")))))
1000 (provide 'dframe)
1002 ;;; dframe.el ends here