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