Fix typo in previous.
[emacs.git] / lisp / dframe.el
blob0d320c214e1ec95234c3e843c5fa719b3181741f
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 timeouts (as opposed to just idle 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 (if (fboundp 'display-graphic-p)
135 (display-graphic-p)
136 window-system)
137 "Non-nil means that timers are available for this Emacs.
138 This is nil for terminals, since updating a frame in a terminal
139 is not useful to the user.")
141 (defcustom dframe-update-speed
142 (if (featurep 'xemacs) 2 ; 1 is too obrusive in XEmacs
144 "Idle time in seconds needed before dframe will update itself.
145 Updates occur to allow dframe to display directory information
146 relevant to the buffer you are currently editing."
147 :group 'dframe
148 :type 'integer)
150 (defcustom dframe-activity-change-focus-flag nil
151 "Non-nil means the selected frame will change based on activity.
152 Thus, if a file is selected for edit, the buffer will appear in the
153 selected frame and the focus will change to that frame."
154 :group 'dframe
155 :type 'boolean)
157 (defcustom dframe-after-select-attached-frame-hook nil
158 "Hook run after dframe has selected the attached frame."
159 :group 'dframe
160 :type 'hook)
162 (defvar dframe-track-mouse-function nil
163 "*A function to call when the mouse is moved in the given frame.
164 Typically used to display info about the line under the mouse.")
165 (make-variable-buffer-local 'dframe-track-mouse-function)
167 (defvar dframe-help-echo-function nil
168 "*A function to call when help-echo is used in newer versions of Emacs.
169 Typically used to display info about the line under the mouse.")
170 (make-variable-buffer-local 'dframe-help-echo-function)
172 (defvar dframe-mouse-click-function nil
173 "*A function to call when the mouse is clicked.
174 Valid clicks are mouse 2, our double mouse 1.")
175 (make-variable-buffer-local 'dframe-mouse-click-function)
177 (defvar dframe-mouse-position-function nil
178 "*A function to called to position the cursor for a mouse click.")
179 (make-variable-buffer-local 'dframe-mouse-position-function)
181 (defvar dframe-power-click nil
182 "Never set this by hand. Value is t when S-mouse activity occurs.")
184 (defvar dframe-timer nil
185 "The dframe timer used for updating the buffer.")
186 (make-variable-buffer-local 'dframe-timer)
188 (defvar dframe-attached-frame nil
189 "The frame which started a frame mode.
190 This is the frame from which all interesting activities will go
191 for the mode using dframe.")
192 (make-variable-buffer-local 'dframe-attached-frame)
194 (defvar dframe-controlled nil
195 "Is this buffer controlled by a dedicated frame.
196 Local to those buffers, as a function called that created it.")
197 (make-variable-buffer-local 'dframe-controlled)
199 (defun dframe-update-keymap (map)
200 "Update the keymap MAP for dframe default bindings."
201 ;; Frame control
202 (define-key map "q" 'dframe-close-frame)
203 (define-key map "Q" 'delete-frame)
205 ;; Override switch to buffer to never hack our frame.
206 (substitute-key-definition 'switch-to-buffer
207 'dframe-switch-buffer-attached-frame
208 map global-map)
210 (if (featurep 'xemacs)
211 (progn
212 ;; mouse bindings so we can manipulate the items on each line
213 (define-key map 'button2 'dframe-click)
214 (define-key map '(shift button2) 'dframe-power-click)
215 ;; Info doc fix from Bob Weiner
216 (if (featurep 'infodoc)
218 (define-key map 'button3 'dframe-popup-kludge))
221 ;; mouse bindings so we can manipulate the items on each line
222 ;; (define-key map [down-mouse-1] 'dframe-double-click)
223 (define-key map [follow-link] 'mouse-face)
224 (define-key map [mouse-2] 'dframe-click)
225 ;; This is the power click for new frames, or refreshing a cache
226 (define-key map [S-mouse-2] 'dframe-power-click)
227 ;; This adds a small unecessary visual effect
228 ;;(define-key map [down-mouse-2] 'dframe-quick-mouse)
230 (define-key map [down-mouse-3] 'dframe-popup-kludge)
232 ;; This lets the user scroll as if we had a scrollbar... well maybe not
233 (define-key map [mode-line mouse-2] 'dframe-mouse-hscroll)
234 ;; another handy place users might click to get our menu.
235 (define-key map [mode-line down-mouse-1]
236 'dframe-popup-kludge)
238 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
239 (define-key map [C-down-mouse-1] 'dframe-hack-buffer-menu)
241 ;; Lastly, we want to track the mouse. Play here
242 (define-key map [mouse-movement] 'dframe-track-mouse)
245 (defun dframe-live-p (frame)
246 "Return non-nil if FRAME is currently available."
247 (and frame (frame-live-p frame) (frame-visible-p frame)))
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 are hooks to run when deleting a frame.
266 POPUP-HOOK are hooks to run before showing a frame.
267 CREATE-HOOK are hooks to run after creating a frame."
268 ;; toggle frame on and off.
269 (if (not arg) (if (dframe-live-p (symbol-value frame-var))
270 (setq arg -1) (setq arg 1)))
271 ;; Make sure the current buffer is set.
272 (set-buffer (symbol-value buffer-var))
273 ;; turn the frame off on neg number
274 (if (and (numberp arg) (< arg 0))
275 (progn
276 (run-hooks 'delete-hook)
277 (if (and (symbol-value frame-var)
278 (frame-live-p (symbol-value frame-var)))
279 (progn
280 (set cache-var (symbol-value frame-var))
281 (make-frame-invisible (symbol-value frame-var))))
282 (set frame-var nil))
283 ;; Set this as our currently attached frame
284 (setq dframe-attached-frame (selected-frame))
285 (run-hooks 'popup-hook)
286 ;; Updated the buffer passed in to contain all the hacks needed
287 ;; to make it work well in a dedicated window.
288 (with-current-buffer (symbol-value buffer-var)
289 ;; Declare this buffer a dedicated frame
290 (setq dframe-controlled local-mode-fn)
292 (if (featurep 'xemacs)
293 (progn
294 ;; Hack the XEmacs mouse-motion handler
295 (set (make-local-variable 'mouse-motion-handler)
296 'dframe-track-mouse-xemacs)
297 ;; Hack the double click handler
298 (make-local-variable 'mouse-track-click-hook)
299 (add-hook 'mouse-track-click-hook
300 (lambda (event count)
301 (if (/= (event-button event) 1)
302 nil ; Do normal operations.
303 (cond ((eq count 1)
304 (dframe-quick-mouse event))
305 ((or (eq count 2)
306 (eq count 3))
307 (dframe-click event)
308 (dframe-quick-mouse event)))
309 ;; Don't do normal operations.
310 t))))
311 ;; Enable mouse tracking in emacs
312 (if dframe-track-mouse-function
313 (set (make-local-variable 'track-mouse) t))) ;this could be messy.
314 ;;;; DISABLED: This causes problems for users with multiple frames.
315 ;;;; ;; Set this up special just for the passed in buffer
316 ;;;; ;; Terminal minibuffer stuff does not require this.
317 ;;;; (if (and (or (assoc 'minibuffer parameters)
318 ;;;; ;; XEmacs plist is not an association list
319 ;;;; (member 'minibuffer parameters))
320 ;;;; window-system (not (eq window-system 'pc))
321 ;;;; (null default-minibuffer-frame))
322 ;;;; (progn
323 ;;;; (make-local-variable 'default-minibuffer-frame)
324 ;;;; (setq default-minibuffer-frame dframe-attached-frame))
325 ;;;; )
326 ;; Override `temp-buffer-show-hook' so that help and such
327 ;; put their stuff into a frame other than our own.
328 ;; Correct use of `temp-buffer-show-function': Bob Weiner
329 (if (and (boundp 'temp-buffer-show-hook)
330 (boundp 'temp-buffer-show-function))
331 (progn (make-local-variable 'temp-buffer-show-hook)
332 (setq temp-buffer-show-hook temp-buffer-show-function)))
333 (make-local-variable 'temp-buffer-show-function)
334 (setq temp-buffer-show-function 'dframe-temp-buffer-show-function)
335 ;; If this buffer is killed, we must make sure that we destroy
336 ;; the frame the dedicated window is in.
337 (add-hook 'kill-buffer-hook `(lambda ()
338 (let ((skilling (boundp 'skilling)))
339 (if skilling
341 (if dframe-controlled
342 (progn
343 (funcall dframe-controlled -1)
344 (setq ,buffer-var nil)
345 )))))
346 t t)
348 ;; Get the frame to work in
349 (if (frame-live-p (symbol-value cache-var))
350 (progn
351 (set frame-var (symbol-value cache-var))
352 (make-frame-visible (symbol-value frame-var))
353 (select-frame (symbol-value frame-var))
354 (set-window-dedicated-p (selected-window) nil)
355 (if (not (eq (current-buffer) (symbol-value buffer-var)))
356 (switch-to-buffer (symbol-value buffer-var)))
357 (set-window-dedicated-p (selected-window) t)
358 (raise-frame (symbol-value frame-var))
360 (if (frame-live-p (symbol-value frame-var))
361 (raise-frame (symbol-value frame-var))
362 (set frame-var
363 (if (featurep 'xemacs)
364 ;; Only guess height if it is not specified.
365 (if (member 'height parameters)
366 (make-frame parameters)
367 (make-frame (nconc (list 'height
368 (dframe-needed-height))
369 parameters)))
370 (let* ((mh (dframe-frame-parameter dframe-attached-frame
371 'menu-bar-lines))
372 (paramsa
373 ;; Only add a guessed height if one is not specified
374 ;; in the input parameters.
375 (if (assoc 'height parameters)
376 parameters
377 (append
378 parameters
379 (list (cons 'height (+ (or mh 0) (frame-height)))))))
380 (params
381 ;; Only add a guessed width if one is not specified
382 ;; in the input parameters.
383 (if (assoc 'width parameters)
384 paramsa
385 (append
386 paramsa
387 (list (cons 'width (frame-width))))))
388 (frame
389 (if (not (eq window-system 'x))
390 (make-frame params)
391 (let ((x-pointer-shape x-pointer-top-left-arrow)
392 (x-sensitive-text-pointer-shape
393 x-pointer-hand2))
394 (make-frame params)))))
395 frame)))
396 ;; Put the buffer into the frame
397 (save-excursion
398 (select-frame (symbol-value frame-var))
399 (switch-to-buffer (symbol-value buffer-var))
400 (set-window-dedicated-p (selected-window) t))
401 ;; Run hooks (like reposition)
402 (run-hooks 'create-hook)
403 ;; Frame name
404 (if (and (or (null window-system) (eq window-system 'pc))
405 (fboundp 'set-frame-name))
406 (save-window-excursion
407 (select-frame (symbol-value frame-var))
408 (set-frame-name frame-name)))
409 ;; On a terminal, raise the frame or the user will
410 ;; be confused.
411 (if (not window-system)
412 (select-frame (symbol-value frame-var)))
413 ))) )
415 (defun dframe-reposition-frame (new-frame parent-frame location)
416 "Move NEW-FRAME to be relative to PARENT-FRAME.
417 LOCATION can be one of 'random, 'left, 'right, 'left-right, or 'top-bottom."
418 (if (featurep 'xemacs)
419 (dframe-reposition-frame-xemacs new-frame parent-frame location)
420 (dframe-reposition-frame-emacs new-frame parent-frame location)))
422 (defun dframe-reposition-frame-emacs (new-frame parent-frame location)
423 "Move NEW-FRAME to be relative to PARENT-FRAME.
424 LOCATION can be one of 'random, 'left-right, 'top-bottom, or
425 a cons cell indicationg a position of the form (LEFT . TOP)."
426 (let* ((pfx (dframe-frame-parameter parent-frame 'left))
427 (pfy (dframe-frame-parameter parent-frame 'top))
428 (pfw (frame-pixel-width parent-frame))
429 (pfh (frame-pixel-height parent-frame))
430 (nfw (frame-pixel-width new-frame))
431 (nfh (frame-pixel-height new-frame))
432 newleft newtop
434 ;; Position dframe.
435 (if (or (not window-system) (eq window-system 'pc))
436 ;; Do no positioning if not on a windowing system,
438 ;; Rebuild pfx,pfy to be absolute positions.
439 (setq pfx (if (not (consp pfx))
441 ;; If pfx is a list, that means we grow
442 ;; from a specific edge of the display.
443 ;; Convert that to the distance from the
444 ;; left side of the display.
445 (if (eq (car pfx) '-)
446 ;; A - means distance from the right edge
447 ;; of the display, or DW - pfx - framewidth
448 (- (x-display-pixel-width) (car (cdr pfx)) pfw)
449 (car (cdr pfx))))
450 pfy (if (not (consp pfy))
452 ;; If pfy is a list, that means we grow
453 ;; from a specific edge of the display.
454 ;; Convert that to the distance from the
455 ;; left side of the display.
456 (if (eq (car pfy) '-)
457 ;; A - means distance from the right edge
458 ;; of the display, or DW - pfx - framewidth
459 (- (x-display-pixel-height) (car (cdr pfy)) pfh)
460 (car (cdr pfy))))
462 (cond ((eq location 'right)
463 (setq newleft (+ pfx pfw 5)
464 newtop pfy))
465 ((eq location 'left)
466 (setq newleft (- pfx 10 nfw)
467 newtop pfy))
468 ((eq location 'left-right)
469 (setq newleft
470 ;; Decide which side to put it on. 200 is just a
471 ;; buffer for the left edge of the screen. The
472 ;; extra 10 is just dressings for window
473 ;; decorations.
474 (let* ((left-guess (- pfx 10 nfw))
475 (right-guess (+ pfx pfw 5))
476 (left-margin left-guess)
477 (right-margin (- (x-display-pixel-width)
478 right-guess 5 nfw)))
479 (cond ((>= left-margin 0) left-guess)
480 ((>= right-margin 0) right-guess)
481 ;; otherwise choose side we overlap less
482 ((> left-margin right-margin) 0)
483 (t (- (x-display-pixel-width) nfw 5))))
484 newtop pfy
486 ((eq location 'top-bottom)
487 (setq newleft pfx
488 newtop
489 ;; Try and guess if we should be on the top or bottom.
490 (let* ((top-guess (- pfy 15 nfh))
491 (bottom-guess (+ pfy 5 pfh))
492 (top-margin top-guess)
493 (bottom-margin (- (x-display-pixel-height)
494 bottom-guess 5 nfh)))
495 (cond ((>= top-margin 0) top-guess)
496 ((>= bottom-margin 0) bottom-guess)
497 ;; Choose a side to overlap the least.
498 ((> top-margin bottom-margin) 0)
499 (t (- (x-display-pixel-height) nfh 5)))))
501 ((consp location)
502 (setq newleft (or (car location) 0)
503 newtop (or (cdr location) 0)))
504 (t nil))
505 (modify-frame-parameters new-frame
506 (list (cons 'left newleft)
507 (cons 'top newtop))))))
509 (defun dframe-reposition-frame-xemacs (new-frame parent-frame location)
510 "Move NEW-FRAME to be relative to PARENT-FRAME.
511 LOCATION can be one of 'random, 'left-right, or 'top-bottom."
512 ;; Not yet implemented
515 ;; XEmacs function only.
516 (defun dframe-needed-height (&optional frame)
517 "The needed height for the tool bar FRAME (in characters)."
518 (or frame (setq frame (selected-frame)))
519 ;; The 1 is the missing modeline/minibuffer
520 (+ 1 (/ (frame-pixel-height frame)
521 ;; This obscure code avoids a byte compiler warning in Emacs.
522 (let ((f 'face-height))
523 (funcall f 'default frame)))))
525 (defun dframe-detach (frame-var cache-var buffer-var)
526 "Detatch the frame in symbol FRAME-VAR.
527 CACHE-VAR and BUFFER-VAR are symbols as in `dframe-frame-mode'"
528 (with-current-buffer (symbol-value buffer-var)
529 (rename-buffer (buffer-name) t)
530 (let ((oldframe (symbol-value frame-var)))
531 (set buffer-var nil)
532 (set frame-var nil)
533 (set cache-var nil)
534 ;; FIXME: Looks very suspicious. Luckily this function is unused.
535 (make-variable-buffer-local frame-var)
536 (set frame-var oldframe)
539 ;;; Special frame event proxies
541 (if (boundp 'special-event-map)
542 (progn
543 (define-key special-event-map [make-frame-visible]
544 'dframe-handle-make-frame-visible)
545 (define-key special-event-map [iconify-frame]
546 'dframe-handle-iconify-frame)
547 (define-key special-event-map [delete-frame]
548 'dframe-handle-delete-frame))
551 (defvar dframe-make-frame-visible-function nil
552 "Function used when a dframe controlled frame is de-iconified.
553 The function must take an EVENT.")
554 (defvar dframe-iconify-frame-function nil
555 "Function used when a dframe controlled frame is iconified.
556 The function must take an EVENT.")
557 (defvar dframe-delete-frame-function nil
558 "Function used when a frame attached to a dframe frame is deleted.
559 The function must take an EVENT.")
561 (defun dframe-handle-make-frame-visible (e)
562 "Handle a `make-frame-visible' event.
563 Should enable auto-updating if the last state was also enabled.
564 Argument E is the event making the frame visible."
565 (interactive "e")
566 (let ((f last-event-frame))
567 (if (and (dframe-attached-frame f)
568 dframe-make-frame-visible-function)
569 (funcall dframe-make-frame-visible-function e)
572 (defun dframe-handle-iconify-frame (e)
573 "Handle a `iconify-frame' event.
574 Should disable auto-updating if the last state was also enabled.
575 Argument E is the event iconifying the frame."
576 (interactive "e")
577 (let ((f last-event-frame))
578 (if (and (dframe-attached-frame f)
579 dframe-iconify-frame-function e)
580 (funcall dframe-iconify-frame-function)
583 (defun dframe-handle-delete-frame (e)
584 "Handle `delete-frame' event.
585 Argument E is the event deleting the frame."
586 (interactive "e")
587 (let ((fl (frame-list))
588 (sf (selected-frame)))
589 ;; Loop over all frames. If dframe-delete-frame-function is
590 ;; non-nil, call it.
591 (while fl
592 (select-frame (car fl))
593 (if dframe-delete-frame-function
594 (funcall dframe-delete-frame-function e))
595 (setq fl (cdr fl)))
596 (if (frame-live-p sf)
597 (select-frame sf))
598 (handle-delete-frame e)))
601 ;;; Utilities
603 (defun dframe-get-focus (frame-var activator &optional hook)
604 "Change frame focus to or from a dedicated frame.
605 If the selected frame is not in the symbol FRAME-VAR, then FRAME-VAR
606 frame is selected. If the FRAME-VAR is active, then select the
607 attached frame. If FRAME-VAR is nil, ACTIVATOR is called to
608 created it. HOOK is an optional argument of hooks to run when
609 selecting FRAME-VAR."
610 (interactive)
611 (if (eq (selected-frame) (symbol-value frame-var))
612 (if (frame-live-p dframe-attached-frame)
613 (dframe-select-attached-frame))
614 ;; make sure we have a frame
615 (if (not (frame-live-p (symbol-value frame-var)))
616 (funcall activator 1))
617 ;; go there
618 (select-frame (symbol-value frame-var))
620 (other-frame 0)
621 ;; If updates are off, then refresh the frame (they want it now...)
622 (run-hooks 'hook))
625 (defun dframe-close-frame ()
626 "Close the current frame if it is dedicated."
627 (interactive)
628 (if dframe-controlled
629 (let ((b (current-buffer)))
630 (funcall dframe-controlled -1)
631 (kill-buffer b))))
633 (defun dframe-current-frame (frame-var desired-major-mode)
634 "Return the existing dedicated frame to use.
635 FRAME-VAR is the variable storing the currently active dedicated frame.
636 If the current frame's buffer uses DESIRED-MAJOR-MODE, then use that frame."
637 (if (not (eq (selected-frame) (symbol-value frame-var)))
638 (if (and (eq major-mode 'desired-major-mode)
639 (get-buffer-window (current-buffer))
640 (window-frame (get-buffer-window (current-buffer))))
641 (window-frame (get-buffer-window (current-buffer)))
642 (symbol-value frame-var))
643 (symbol-value frame-var)))
645 (defun dframe-attached-frame (&optional frame)
646 "Return the attached frame belonging to the dframe controlled frame FRAME.
647 If optional arg FRAME is nil just return `dframe-attached-frame'."
648 (save-excursion
649 (if frame (select-frame frame))
650 dframe-attached-frame))
652 (defun dframe-select-attached-frame (&optional frame)
653 "Switch to the frame the dframe controlled frame FRAME was started from.
654 If optional arg FRAME is nil assume the attached frame is already selected
655 and just run the hooks `dframe-after-select-attached-frame-hook'. Return
656 the attached frame."
657 (let ((frame (dframe-attached-frame frame)))
658 (if frame (select-frame frame))
659 (prog1 frame
660 (run-hooks 'dframe-after-select-attached-frame-hook))))
662 (defmacro dframe-with-attached-buffer (&rest forms)
663 "Execute FORMS in the attached frame's special buffer.
664 Optionally select that frame if necessary."
665 `(save-selected-window
666 ;;(speedbar-set-timer speedbar-update-speed)
667 (dframe-select-attached-frame)
668 ,@forms
669 (dframe-maybee-jump-to-attached-frame)))
671 (defun dframe-maybee-jump-to-attached-frame ()
672 "Jump to the attached frame ONLY if this was not a mouse event."
673 (when (or (not (dframe-mouse-event-p last-input-event))
674 dframe-activity-change-focus-flag)
675 (dframe-select-attached-frame)
676 ;; KB: For what is this - raising the frame??
677 (other-frame 0)))
680 (defvar dframe-suppress-message-flag nil
681 "Non-nil means that `dframe-message' should just return a string.")
683 (defun dframe-message (fmt &rest args)
684 "Like message, but for use in a dedicated frame.
685 Argument FMT is the format string, and ARGS are the arguments for message."
686 (save-selected-window
687 (if dframe-suppress-message-flag
688 (apply 'format fmt args)
689 (if dframe-attached-frame
690 ;; KB: Here we do not need calling `dframe-select-attached-frame'
691 (select-frame dframe-attached-frame))
692 (apply 'message fmt args))))
694 (defun dframe-y-or-n-p (prompt)
695 "Like `y-or-n-p', but for use in a dedicated frame.
696 Argument PROMPT is the prompt to use."
697 (save-selected-window
698 (if (and ;;default-minibuffer-frame
699 dframe-attached-frame
700 ;;(not (eq default-minibuffer-frame dframe-attached-frame))
702 ;; KB: Here we do not need calling `dframe-select-attached-frame'
703 (select-frame dframe-attached-frame))
704 (y-or-n-p prompt)))
706 ;;; timer management
708 ;; Unlike speedbar with a dedicated set of routines, dframe has one master
709 ;; timer, and all dframe users will use it. At least until I figure out a way
710 ;; around that problem.
712 ;; Advantage 1: Two apps with timer/frames can munge the master list
713 ;; to make sure they occur in order.
714 ;; Advantage 2: If a user hits a key between timer functions, we can
715 ;; interrupt them safely.
716 (defvar dframe-client-functions nil
717 "List of client functions using the dframe timer.")
719 (defun dframe-set-timer (timeout fn &optional null-on-error)
720 "Apply a timer with TIMEOUT, to call FN, or remove a timer if TIMEOUT is nil.
721 TIMEOUT is the number of seconds until the dframe controled program
722 timer is called again. When TIMEOUT is nil, turn off all timeouts.
723 This function must be called from the buffer belonging to the program
724 who requested the timer.
725 If NULL-ON-ERROR is a symbol, set it to nil if we cannot create a timer."
726 ;; First, fix up our list of client functions
727 (if timeout
728 (add-to-list 'dframe-client-functions fn)
729 (setq dframe-client-functions (delete fn dframe-client-functions)))
730 ;; Now decided what to do about the timout.
731 (if (or
732 ;; We have a timer, restart the timer with the new time.
733 timeout
734 ;; We have a timer, an off is requested, and no client
735 ;; functions are left, shut er down.
736 (and dframe-timer (not timeout) dframe-client-functions))
737 ;; Only call the low level function if we are changing the state.
738 (dframe-set-timer-internal timeout null-on-error)))
740 (defun dframe-set-timer-internal (timeout &optional null-on-error)
741 "Apply a timer with TIMEOUT to call the dframe timer manager."
742 (when dframe-timer
743 (if (featurep 'xemacs)
744 (delete-itimer dframe-timer)
745 (cancel-timer dframe-timer))
746 (setq dframe-timer nil))
747 (when timeout
748 (setq dframe-timer
749 (if (featurep 'xemacs)
750 (start-itimer "dframe" 'dframe-timer-fn
751 timeout timeout t)
752 (run-with-idle-timer timeout t 'dframe-timer-fn)))))
754 (defun dframe-timer-fn ()
755 "Called due to the dframe timer.
756 Evaluates all cached timer functions in sequence."
757 (let ((l dframe-client-functions))
758 (while (and l (sit-for 0))
759 (condition-case er
760 (funcall (car l))
761 (error (message "DFRAME TIMER ERROR: %S" er)))
762 (setq l (cdr l)))))
764 ;;; Menu hacking for mouse-3
766 (defconst dframe-pass-event-to-popup-mode-menu
767 (let (max-args)
768 (and (fboundp 'popup-mode-menu)
769 (fboundp 'function-max-args)
770 (setq max-args (function-max-args 'popup-mode-menu))
771 (not (zerop max-args))))
772 "The EVENT arg to 'popup-mode-menu' was introduced in XEmacs 21.4.0.")
774 ;; In XEmacs, we make popup menus work on the item over mouse (as
775 ;; opposed to where the point happens to be.) We attain this by
776 ;; temporarily moving the point to that place.
777 ;; Hrvoje Niksic <hniksic@srce.hr>
778 (defalias 'dframe-popup-kludge
779 (if (featurep 'xemacs)
780 (lambda (event) ; XEmacs.
781 "Pop up a menu related to the clicked on item.
782 Must be bound to EVENT."
783 (interactive "e")
784 (save-excursion
785 (if dframe-pass-event-to-popup-mode-menu
786 (popup-mode-menu event)
787 (goto-char (event-closest-point event))
788 (beginning-of-line)
789 (forward-char (min 5 (- (save-excursion (end-of-line) (point))
790 (save-excursion (beginning-of-line) (point)))))
791 (popup-mode-menu))
792 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
793 ;; menu functions) return immediately.
794 (let (new)
795 (while (not (misc-user-event-p (setq new (next-event))))
796 (dispatch-event new))
797 (dispatch-event new))))
799 (lambda (e) ; Emacs.
800 "Pop up a menu related to the clicked on item.
801 Must be bound to event E."
802 (interactive "e")
803 (save-excursion
804 (mouse-set-point e)
805 ;; This gets the cursor where the user can see it.
806 (if (not (bolp)) (forward-char -1))
807 (sit-for 0)
808 (if (fboundp 'mouse-menu-major-mode-map)
809 (popup-menu (mouse-menu-major-mode-map) e)
810 (with-no-warnings ; don't warn about obsolete fallback
811 (mouse-major-mode-menu e nil)))))))
813 ;;; Interactive user functions for the mouse
815 (defalias 'dframe-mouse-event-p
816 (if (featurep 'xemacs)
817 'button-press-event-p
818 (lambda (event)
819 "Return t if the event is a mouse related event."
820 (if (and (listp event)
821 (member (event-basic-type event)
822 '(mouse-1 mouse-2 mouse-3)))
824 nil))))
826 (defun dframe-track-mouse (event)
827 "For motion EVENT, display info about the current line."
828 (interactive "e")
829 (when (and dframe-track-mouse-function
830 (or (featurep 'xemacs) ;; XEmacs always safe?
831 (windowp (posn-window (event-end event))) ; Sometimes
832 ; there is no window to jump into.
835 (funcall dframe-track-mouse-function event)))
837 (defun dframe-track-mouse-xemacs (event)
838 "For motion EVENT, display info about the current line."
839 (if (functionp (default-value 'mouse-motion-handler))
840 (funcall (default-value 'mouse-motion-handler) event))
841 (if dframe-track-mouse-function
842 (funcall dframe-track-mouse-function event)))
844 (defun dframe-help-echo (window &optional buffer position)
845 "Display help based context.
846 The context is in WINDOW, viewing BUFFER, at POSITION.
847 BUFFER and POSITION are optional because XEmacs doesn't use them."
848 (when (and (not dframe-track-mouse-function)
849 (bufferp buffer)
850 dframe-help-echo-function)
851 (let ((dframe-suppress-message-flag t))
852 (with-current-buffer buffer
853 (save-excursion
854 (if position (goto-char position))
855 (funcall dframe-help-echo-function))))))
857 (defun dframe-mouse-set-point (e)
858 "Set POINT based on event E.
859 Handles clicking on images in XEmacs."
860 (if (and (featurep 'xemacs)
861 (save-excursion
862 (save-window-excursion
863 (mouse-set-point e)
864 (event-over-glyph-p e))))
865 ;; We are in XEmacs, and clicked on a picture
866 (let ((ext (event-glyph-extent e)))
867 ;; This position is back inside the extent where the
868 ;; junk we pushed into the property list lives.
869 (if (extent-end-position ext)
870 (goto-char (1- (extent-end-position ext)))
871 (mouse-set-point e)))
872 ;; We are not in XEmacs, OR we didn't click on a picture.
873 (mouse-set-point e)))
875 (defun dframe-quick-mouse (e)
876 "Since mouse events are strange, this will keep the mouse nicely positioned.
877 This should be bound to mouse event E."
878 (interactive "e")
879 (dframe-mouse-set-point e)
880 (if dframe-mouse-position-function
881 (funcall dframe-mouse-position-function)))
883 (defun dframe-power-click (e)
884 "Activate any dframe mouse click as a power click.
885 A power click will dispose of cached data (if available) or bring a buffer
886 up into a different window.
887 This should be bound to mouse event E."
888 (interactive "e")
889 (let ((dframe-power-click t))
890 (select-frame last-event-frame)
891 (dframe-click e)))
893 (defun dframe-click (e)
894 "Call our clients click function on a user click.
895 E is the event causing the click."
896 (interactive "e")
897 (dframe-mouse-set-point e)
898 (when dframe-mouse-click-function
899 ;; On the off chance of buffer switch, or something incorrectly
900 ;; configured.
901 (funcall dframe-mouse-click-function e)))
903 (defun dframe-double-click (e)
904 "Activate the registered click function on a double click.
905 This must be bound to a mouse event.
906 This should be bound to mouse event E."
907 (interactive "e")
908 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
909 (cond ((eq (car e) 'down-mouse-1)
910 (dframe-mouse-set-point e))
911 ((eq (car e) 'mouse-1)
912 (dframe-quick-mouse e))
913 ((or (eq (car e) 'double-down-mouse-1)
914 (eq (car e) 'triple-down-mouse-1))
915 (dframe-click e))))
917 ;;; Hacks of normal things.
919 ;; Some normal things that happen in one of these dedicated frames
920 ;; must be handled specially, so that our dedicated frame isn't
921 ;; messed up.
922 (defun dframe-temp-buffer-show-function (buffer)
923 "Placed in the variable `temp-buffer-show-function' in dedicated frames.
924 If a user requests help using \\[help-command] <Key> the temp BUFFER will be
925 redirected into a window on the attached frame."
926 (if dframe-attached-frame (dframe-select-attached-frame))
927 (pop-to-buffer buffer nil)
928 (other-window -1)
929 ;; Fix for using this hook on some platforms: Bob Weiner
930 (cond ((not (featurep 'xemacs))
931 (run-hooks 'temp-buffer-show-hook))
932 ((fboundp 'run-hook-with-args)
933 (run-hook-with-args 'temp-buffer-show-hook buffer))
934 ((and (boundp 'temp-buffer-show-hook)
935 (listp temp-buffer-show-hook))
936 (mapcar (function (lambda (hook) (funcall hook buffer)))
937 temp-buffer-show-hook))))
939 (defun dframe-hack-buffer-menu (e)
940 "Control mouse 1 is buffer menu.
941 This hack overrides it so that the right thing happens in the main
942 Emacs frame, not in the dedicated frame.
943 Argument E is the event causing this activity."
944 (interactive "e")
945 (let ((fn (lookup-key global-map (if (featurep 'xemacs)
946 '(control button1)
947 [C-down-mouse-1])))
948 (oldbuff (current-buffer))
949 (newbuff nil))
950 (unwind-protect
951 (save-excursion
952 (set-window-dedicated-p (selected-window) nil)
953 (call-interactively fn)
954 (setq newbuff (current-buffer)))
955 (switch-to-buffer oldbuff)
956 (set-window-dedicated-p (selected-window) t))
957 (if (not (eq newbuff oldbuff))
958 (dframe-with-attached-buffer
959 (switch-to-buffer newbuff)))))
961 (defun dframe-switch-buffer-attached-frame (&optional buffer)
962 "Switch to BUFFER in the attached frame, and raise that frame.
963 This overrides the default behavior of `switch-to-buffer' which is
964 broken because of the dedicated frame."
965 (interactive)
966 ;; Assume we are in the dedicated frame.
967 (other-frame 1)
968 ;; Now switch buffers
969 (if buffer
970 (switch-to-buffer buffer)
971 (call-interactively 'switch-to-buffer nil nil)))
973 ;; XEmacs: this can be implemented using modeline keymaps, but there
974 ;; is no use, as we have horizontal scrollbar (as the docstring
975 ;; hints.)
976 (defun dframe-mouse-hscroll (e)
977 "Read a mouse event E from the mode line, and horizontally scroll.
978 If the mouse is being clicked on the far left, or far right of the
979 mode-line. This is only useful for non-XEmacs."
980 (interactive "e")
981 (let* ((x-point (car (nth 2 (car (cdr e)))))
982 (pixels-per-10-col (/ (* 10 (frame-pixel-width))
983 (frame-width)))
984 (click-col (1+ (/ (* 10 x-point) pixels-per-10-col)))
986 (cond ((< click-col 3)
987 (scroll-left 2))
988 ((> click-col (- (window-width) 5))
989 (scroll-right 2))
990 (t (dframe-message
991 "Click on the edge of the modeline to scroll left/right")))
994 (provide 'dframe)
996 ;; arch-tag: df9b91b6-e85e-4a76-a02e-b3cb5b686bd4
997 ;;; dframe.el ends here