* dframe.el: New file.
[bpt/emacs.git] / lisp / dframe.el
CommitLineData
7cfc18c4
CY
1;;; dframe --- dedicate frame support modes
2
3;;; Copyright (C) 1996, 97, 98, 99, 2000, 01, 02, 03, 04 Free Software Foundation
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
6;; Keywords: file, tags, tools
7;; X-RCS: $Id: dframe.el,v 1.27 2005/02/06 17:02:45 berndl Exp $
8
9(defvar dframe-version "1.3"
10 "The current version of the dedicated frame library.")
11
12;; This file is part of GNU Emacs.
13
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 2, or (at your option)
17;; any later version.
18
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.
23
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., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
28
29;;; Commentary:
30;;
31;; This code was developed and maintained as a part of speedbar since 1996.
32;; It became its own support utility in Aug 2000.
33;;
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:
38;;
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
57;;
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.
107
108;;; Bugs
109;;
110;; * The timer managers doesn't handle multiple different timeouts.
111;; * You can't specify continuous timouts (as opposed to just lidle timers.)
112
113;;; Code:
114(defvar dframe-xemacsp (string-match "XEmacs" emacs-version)
115 "Non-nil if we are running in the XEmacs environment.")
116(defvar dframe-xemacs20p (and dframe-xemacsp
117 (>= emacs-major-version 20)))
118
119;; From custom web page for compatibility between versions of custom
120;; with help from ptype@dera.gov.uk (Proto Type)
121(eval-and-compile
122 (condition-case ()
123 (require 'custom)
124 (error nil))
125 (if (and (featurep 'custom) (fboundp 'custom-declare-variable)
126 ;; Some XEmacsen w/ custom don't have :set keyword.
127 ;; This protects them against custom.
128 (fboundp 'custom-initialize-set))
129 nil ;; We've got what we needed
130 ;; We have the old custom-library, hack around it!
131 (if (boundp 'defgroup)
132 nil
133 (defmacro defgroup (&rest args)
134 nil))
135 (if (boundp 'defface)
136 nil
137 (defmacro defface (var values doc &rest args)
138 (` (progn
139 (defvar (, var) (quote (, var)))
140 ;; To make colors for your faces you need to set your .Xdefaults
141 ;; or set them up ahead of time in your .emacs file.
142 (make-face (, var))
143 ))))
144 (if (boundp 'defcustom)
145 nil
146 (defmacro defcustom (var value doc &rest args)
147 (` (defvar (, var) (, value) (, doc)))))))
148
149\f
150;;; Compatibility functions
151;;
152(if (fboundp 'frame-parameter)
153
154 (defalias 'dframe-frame-parameter 'frame-parameter)
155
156 (defun dframe-frame-parameter (frame parameter)
157 "Return FRAME's PARAMETER value."
158 (cdr (assoc parameter (frame-parameters frame)))))
159
160\f
161;;; Variables
162;;
163(defgroup dframe nil
164 "Faces used in dframe."
165 :prefix "dframe-"
166 :group 'dframe)
167
168(defvar dframe-have-timer-flag
169 (and (or (fboundp 'run-with-idle-timer)
170 (fboundp 'start-itimer)
171 (boundp 'post-command-idle-hook))
172 (if (fboundp 'display-graphic-p)
173 (display-graphic-p)
174 window-system))
175 "Non-nil means that timers are available for this Emacs.")
176
177(defcustom dframe-update-speed
178 (if dframe-xemacsp
179 (if dframe-xemacs20p
180 2 ; 1 is too obrusive in XEmacs
181 5) ; when no idleness, need long delay
182 1)
183 "*Idle time in seconds needed before dframe will update itself.
184Updates occur to allow dframe to display directory information
185relevant to the buffer you are currently editing."
186 :group 'dframe
187 :type 'integer)
188
189(defcustom dframe-activity-change-focus-flag nil
190 "*Non-nil means the selected frame will change based on activity.
191Thus, if a file is selected for edit, the buffer will appear in the
192selected frame and the focus will change to that frame."
193 :group 'dframe
194 :type 'boolean)
195
196(defcustom dframe-after-select-attached-frame-hook nil
197 "*Hook run after dframe has selected the attached frame."
198 :group 'dframe
199 :type 'hook)
200
201(defvar dframe-track-mouse-function nil
202 "*A function to call when the mouse is moved in the given frame.
203Typically used to display info about the line under the mouse.")
204(make-variable-buffer-local 'dframe-track-mouse-function)
205
206(defvar dframe-help-echo-function nil
207 "*A function to call when help-echo is used in newer versions of Emacs.
208Typically used to display info about the line under the mouse.")
209(make-variable-buffer-local 'dframe-help-echo-function)
210
211(defvar dframe-mouse-click-function nil
212 "*A function to call when the mouse is clicked.
213Valid clicks are mouse 2, our double mouse 1.")
214(make-variable-buffer-local 'dframe-mouse-click-function)
215
216(defvar dframe-mouse-position-function nil
217 "*A function to called to position the cursor for a mouse click.")
218(make-variable-buffer-local 'dframe-mouse-position-function)
219
220(defvar dframe-power-click nil
221 "Never set this by hand. Value is t when S-mouse activity occurs.")
222
223(defvar dframe-timer nil
224 "The dframe timer used for updating the buffer.")
225(make-variable-buffer-local 'dframe-timer)
226
227(defvar dframe-attached-frame nil
228 "The frame which started a frame mode.
229This is the frame from which all interesting activities will go
230for the mode using dframe.")
231(make-variable-buffer-local 'dframe-attached-frame)
232
233(defvar dframe-controlled nil
234 "Is this buffer controlled by a dedicated frame.
235Local to those buffers, as a function called that created it.")
236(make-variable-buffer-local 'dframe-controlled)
237
238(defun dframe-update-keymap (map)
239 "Update the keymap MAP for dframe default bindings."
240 ;; Frame control
241 (define-key map "q" 'dframe-close-frame)
242 (define-key map "Q" 'delete-frame)
243
244 ;; Override switch to buffer to never hack our frame.
245 (substitute-key-definition 'switch-to-buffer
246 'dframe-switch-buffer-attached-frame
247 map global-map)
248
249 (if dframe-xemacsp
250 (progn
251 ;; mouse bindings so we can manipulate the items on each line
252 (define-key map 'button2 'dframe-click)
253 (define-key map '(shift button2) 'dframe-power-click)
254 ;; Info doc fix from Bob Weiner
255 (if (featurep 'infodoc)
256 nil
257 (define-key map 'button3 'dframe-xemacs-popup-kludge))
258 )
259
260 ;; mouse bindings so we can manipulate the items on each line
261 (define-key map [down-mouse-1] 'dframe-double-click)
262 (define-key map [mouse-2] 'dframe-click)
263 ;; This is the power click for new frames, or refreshing a cache
264 (define-key map [S-mouse-2] 'dframe-power-click)
265 ;; This adds a small unecessary visual effect
266 ;;(define-key map [down-mouse-2] 'dframe-quick-mouse)
267
268 (define-key map [down-mouse-3] 'dframe-emacs-popup-kludge)
269
270 ;; This lets the user scroll as if we had a scrollbar... well maybe not
271 (define-key map [mode-line mouse-2] 'dframe-mouse-hscroll)
272 ;; another handy place users might click to get our menu.
273 (define-key map [mode-line down-mouse-1]
274 'dframe-emacs-popup-kludge)
275
276 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
277 (define-key map [C-down-mouse-1] 'dframe-hack-buffer-menu)
278
279 ;; Lastly, we want to track the mouse. Play here
280 (define-key map [mouse-movement] 'dframe-track-mouse)
281 ))
282
283(defun dframe-live-p (frame)
284 "Return non-nil if FRAME is currently available."
285 (and frame (frame-live-p frame) (frame-visible-p frame)))
286
287(defun dframe-frame-mode (arg frame-var cache-var buffer-var frame-name
288 local-mode-fn
289 &optional
290 parameters
291 delete-hook popup-hook create-hook
292 )
293 "Manage a frame for an application, enabling it when ARG is positive.
294FRAME-VAR is a variable used to cache the frame being used.
295This frame is either resurrected, hidden, killed, etc based on
296the value.
297CACHE-VAR is a variable used to cache a cached frame.
298BUFFER-VAR is a variable used to cache the buffer being used in dframe.
299This buffer will have `dframe-mode' run on it.
300FRAME-NAME is the name of the frame to create.
301LOCAL-MODE-FN is the function used to call this one.
302PARAMETERS are frame parameters to apply to this dframe.
303DELETE-HOOK are hooks to run when deleting a frame.
304POPUP-HOOK are hooks to run before showing a frame.
305CREATE-HOOK are hooks to run after creating a frame."
306 ;; toggle frame on and off.
307 (if (not arg) (if (dframe-live-p (symbol-value frame-var))
308 (setq arg -1) (setq arg 1)))
309 ;; Make sure the current buffer is set.
310 (set-buffer (symbol-value buffer-var))
311 ;; turn the frame off on neg number
312 (if (and (numberp arg) (< arg 0))
313 (progn
314 (run-hooks 'delete-hook)
315 (if (and (symbol-value frame-var)
316 (frame-live-p (symbol-value frame-var)))
317 (progn
318 (set cache-var (symbol-value frame-var))
319 (make-frame-invisible (symbol-value frame-var))))
320 (set frame-var nil))
321 ;; Set this as our currently attached frame
322 (setq dframe-attached-frame (selected-frame))
323 (run-hooks 'popup-hook)
324 ;; Updated the buffer passed in to contain all the hacks needed
325 ;; to make it work well in a dedicated window.
326 (save-excursion
327 (set-buffer (symbol-value buffer-var))
328 ;; Declare this buffer a dedicated frame
329 (setq dframe-controlled local-mode-fn)
330
331 (if dframe-xemacsp
332 ;; Hack the XEmacs mouse-motion handler
333 (with-no-warnings
334 ;; Hack the XEmacs mouse-motion handler
335 (set (make-local-variable 'mouse-motion-handler)
336 'dframe-track-mouse-xemacs)
337 ;; Hack the double click handler
338 (make-local-variable 'mouse-track-click-hook)
339 (add-hook 'mouse-track-click-hook
340 (lambda (event count)
341 (if (/= (event-button event) 1)
342 nil ; Do normal operations.
343 (cond ((eq count 1)
344 (dframe-quick-mouse event))
345 ((or (eq count 2)
346 (eq count 3))
347 (dframe-click event)
348 (dframe-quick-mouse event)))
349 ;; Don't do normal operations.
350 t))))
351 ;; Enable mouse tracking in emacs
352 (if dframe-track-mouse-function
353 (set (make-local-variable 'track-mouse) t)) ;this could be messy.
354 ;; disable auto-show-mode for Emacs
355 (setq auto-show-mode nil))
356;;;; DISABLED: This causes problems for users with multiple frames.
357;;;; ;; Set this up special just for the passed in buffer
358;;;; ;; Terminal minibuffer stuff does not require this.
359;;;; (if (and (or (assoc 'minibuffer parameters)
360;;;; ;; XEmacs plist is not an association list
361;;;; (member 'minibuffer parameters))
362;;;; window-system (not (eq window-system 'pc))
363;;;; (null default-minibuffer-frame))
364;;;; (progn
365;;;; (make-local-variable 'default-minibuffer-frame)
366;;;; (setq default-minibuffer-frame dframe-attached-frame))
367;;;; )
368 ;; Override `temp-buffer-show-hook' so that help and such
369 ;; put their stuff into a frame other than our own.
370 ;; Correct use of `temp-buffer-show-function': Bob Weiner
371 (if (and (boundp 'temp-buffer-show-hook)
372 (boundp 'temp-buffer-show-function))
373 (progn (make-local-variable 'temp-buffer-show-hook)
374 (setq temp-buffer-show-hook temp-buffer-show-function)))
375 (make-local-variable 'temp-buffer-show-function)
376 (setq temp-buffer-show-function 'dframe-temp-buffer-show-function)
377 ;; If this buffer is killed, we must make sure that we destroy
378 ;; the frame the dedicated window is in.
379 (add-hook 'kill-buffer-hook `(lambda ()
380 (let ((skilling (boundp 'skilling)))
381 (if skilling
382 nil
383 (if dframe-controlled
384 (progn
385 (funcall dframe-controlled -1)
386 (setq ,buffer-var nil)
387 )))))
388 t t)
389 )
390 ;; Get the frame to work in
391 (if (frame-live-p (symbol-value cache-var))
392 (progn
393 (set frame-var (symbol-value cache-var))
394 (make-frame-visible (symbol-value frame-var))
395 (select-frame (symbol-value frame-var))
396 (set-window-dedicated-p (selected-window) nil)
397 (if (not (eq (current-buffer) (symbol-value buffer-var)))
398 (switch-to-buffer (symbol-value buffer-var)))
399 (set-window-dedicated-p (selected-window) t)
400 (raise-frame (symbol-value frame-var))
401 )
402 (if (frame-live-p (symbol-value frame-var))
403 (raise-frame (symbol-value frame-var))
404 (set frame-var
405 (if dframe-xemacsp
406 ;; Only guess height if it is not specified.
407 (if (member 'height parameters)
408 (make-frame parameters)
409 (make-frame (nconc (list 'height
410 (dframe-needed-height))
411 parameters)))
412 (let* ((mh (dframe-frame-parameter dframe-attached-frame
413 'menu-bar-lines))
414 (paramsa
415 ;; Only add a guessed height if one is not specified
416 ;; in the input parameters.
417 (if (assoc 'height parameters)
418 parameters
419 (append
420 parameters
421 (list (cons 'height (+ mh (frame-height)))))))
422 (params
423 ;; Only add a guessed width if one is not specified
424 ;; in the input parameters.
425 (if (assoc 'width parameters)
426 paramsa
427 (append
428 paramsa
429 (list (cons 'width (frame-width))))))
430 (frame
431 (if (or (< emacs-major-version 20)
432 (not (eq window-system 'x)))
433 (make-frame params)
434 (let ((x-pointer-shape x-pointer-top-left-arrow)
435 (x-sensitive-text-pointer-shape
436 x-pointer-hand2))
437 (make-frame params)))))
438 frame)))
439 ;; Put the buffer into the frame
440 (save-excursion
441 (select-frame (symbol-value frame-var))
442 (switch-to-buffer (symbol-value buffer-var))
443 (set-window-dedicated-p (selected-window) t))
444 ;; Run hooks (like reposition)
445 (run-hooks 'create-hook)
446 ;; Frame name
447 (if (and (or (null window-system) (eq window-system 'pc))
448 (fboundp 'set-frame-name))
449 (save-window-excursion
450 (select-frame (symbol-value frame-var))
451 (set-frame-name frame-name)))
452 ;; On a terminal, raise the frame or the user will
453 ;; be confused.
454 (if (not window-system)
455 (select-frame (symbol-value frame-var)))
456 ))) )
457
458(defun dframe-reposition-frame (new-frame parent-frame location)
459 "Move NEW-FRAME to be relative to PARENT-FRAME.
460LOCATION can be one of 'random, 'left, 'right, 'left-right, or 'top-bottom."
461 (if dframe-xemacsp
462 (dframe-reposition-frame-xemacs new-frame parent-frame location)
463 (dframe-reposition-frame-emacs new-frame parent-frame location)))
464
465(defun dframe-reposition-frame-emacs (new-frame parent-frame location)
466 "Move NEW-FRAME to be relative to PARENT-FRAME.
467LOCATION can be one of 'random, 'left-right, 'top-bottom, or
468a cons cell indicationg a position of the form (LEFT . TOP)."
469 (let* ((pfx (dframe-frame-parameter parent-frame 'left))
470 (pfy (dframe-frame-parameter parent-frame 'top))
471 (pfw (frame-pixel-width parent-frame))
472 (pfh (frame-pixel-height parent-frame))
473 (nfw (frame-pixel-width new-frame))
474 (nfh (frame-pixel-height new-frame))
475 newleft newtop
476 )
477 ;; Position dframe.
478 (if (or (not window-system) (eq window-system 'pc))
479 ;; Do no positioning if not on a windowing system,
480 nil
481 ;; Rebuild pfx,pfy to be absolute positions.
482 (setq pfx (if (not (consp pfx))
483 pfx
484 ;; If pfx is a list, that means we grow
485 ;; from a specific edge of the display.
486 ;; Convert that to the distance from the
487 ;; left side of the display.
488 (if (eq (car pfx) '-)
489 ;; A - means distance from the right edge
490 ;; of the display, or DW - pfx - framewidth
491 (- (x-display-pixel-width) (car (cdr pfx)) pfw)
492 (car (cdr pfx))))
493 pfy (if (not (consp pfy))
494 pfy
495 ;; If pfy is a list, that means we grow
496 ;; from a specific edge of the display.
497 ;; Convert that to the distance from the
498 ;; left side of the display.
499 (if (eq (car pfy) '-)
500 ;; A - means distance from the right edge
501 ;; of the display, or DW - pfx - framewidth
502 (- (x-display-pixel-height) (car (cdr pfy)) pfh)
503 (car (cdr pfy))))
504 )
505 (cond ((eq location 'right)
506 (setq newleft (+ pfx pfw 5)
507 newtop pfy))
508 ((eq location 'left)
509 (setq newleft (+ pfx 10 nfw)
510 newtop pfy))
511 ((eq location 'left-right)
512 (setq newleft
513 ;; Decide which side to put it on. 200 is just a
514 ;; buffer for the left edge of the screen. The
515 ;; extra 10 is just dressings for window
516 ;; decorations.
517 (let* ((left-guess (- pfx 10 nfw))
518 (right-guess (+ pfx pfw 5))
519 (left-margin left-guess)
520 (right-margin (- (x-display-pixel-width)
521 right-guess 5 nfw)))
522 (cond ((>= left-margin 0) left-guess)
523 ((>= right-margin 0) right-guess)
524 ;; otherwise choose side we overlap less
525 ((> left-margin right-margin) 0)
526 (t (- (x-display-pixel-width) nfw 5))))
527 newtop pfy
528 ))
529 ((eq location 'top-bottom)
530 (setq newleft pfx
531 newtop
532 ;; Try and guess if we should be on the top or bottom.
533 (let* ((top-guess (- pfy 15 nfh))
534 (bottom-guess (+ pfy 5 pfh))
535 (top-margin top-guess)
536 (bottom-margin (- (x-display-pixel-height)
537 bottom-guess 5 nfh)))
538 (cond ((>= top-margin 0) top-guess)
539 ((>= bottom-margin 0) bottom-guess)
540 ;; Choose a side to overlap the least.
541 ((> top-margin bottom-margin) 0)
542 (t (- (x-display-pixel-height) nfh 5)))))
543 )
544 ((consp location)
545 (setq newleft (or (car location) 0)
546 newtop (or (cdr location) 0)))
547 (t nil))
548 (modify-frame-parameters new-frame
549 (list (cons 'left newleft)
550 (cons 'top newtop))))))
551
552(defun dframe-reposition-frame-xemacs (new-frame parent-frame location)
553 "Move NEW-FRAME to be relative to PARENT-FRAME.
554LOCATION can be one of 'random, 'left-right, or 'top-bottom."
555 ;; Not yet implemented
556 )
557
558;; XEmacs function only.
559(defun dframe-needed-height (&optional frame)
560 "The needed height for the tool bar FRAME (in characters)."
561 (or frame (setq frame (selected-frame)))
562 ;; The 1 is the missing modeline/minibuffer
563 (+ 1 (/ (frame-pixel-height frame)
564 ;; This obscure code avoids a byte compiler warning in Emacs.
565 (let ((f 'face-height))
566 (funcall f 'default frame)))))
567
568(defun dframe-detach (frame-var cache-var buffer-var)
569 "Detatch the frame in symbol FRAME-VAR.
570CACHE-VAR and BUFFER-VAR are symbols as in `dframe-frame-mode'"
571 (save-excursion
572 (set-buffer (symbol-value buffer-var))
573 (rename-buffer (buffer-name) t)
574 (let ((oldframe (symbol-value frame-var)))
575 (set buffer-var nil)
576 (set frame-var nil)
577 (set cache-var nil)
578 (make-variable-buffer-local frame-var)
579 (set frame-var oldframe)
580 )))
581
582;;; Special frame event proxies
583;;
584(if (boundp 'special-event-map)
585 (progn
586 (define-key special-event-map [make-frame-visible]
587 'dframe-handle-make-frame-visible)
588 (define-key special-event-map [iconify-frame]
589 'dframe-handle-iconify-frame)
590 (define-key special-event-map [delete-frame]
591 'dframe-handle-delete-frame))
592 )
593
594(defvar dframe-make-frame-visible-function nil
595 "Function used when a dframe controlled frame is de-iconified.
596The function must take an EVENT.")
597(defvar dframe-iconify-frame-function nil
598 "Function used when a dframe controlled frame is iconified.
599The function must take an EVENT.")
600(defvar dframe-delete-frame-function nil
601 "Function used when a frame attached to a dframe frame is deleted.
602The function must take an EVENT.")
603
604(defun dframe-handle-make-frame-visible (e)
605 "Handle a `make-frame-visible' event.
606Should enables auto-updating if the last state was also enabled.
607Argument E is the event making the frame visible."
608 (interactive "e")
609 (let ((f last-event-frame))
610 (if (and (dframe-attached-frame f)
611 dframe-make-frame-visible-function)
612 (funcall dframe-make-frame-visible-function e)
613 )))
614
615(defun dframe-handle-iconify-frame (e)
616 "Handle a `iconify-frame' event.
617Should disables auto-updating if the last state was also enabled.
618Argument E is the event iconifying the frame."
619 (interactive "e")
620 (let ((f last-event-frame))
621 (if (and (dframe-attached-frame f)
622 dframe-iconify-frame-function e)
623 (funcall dframe-iconify-frame-function)
624 )))
625
626(defun dframe-handle-delete-frame (e)
627 "Handle `delete-frame' event.
628Argument E is the event deleting the frame."
629 (interactive "e")
630 (let ((fl (frame-list))
631 (sf (selected-frame)))
632 ;; Loop over all frames. If dframe-delete-frame-function is
633 ;; non-nil, call it.
634 (while fl
635 (select-frame (car fl))
636 (if dframe-delete-frame-function
637 (funcall dframe-delete-frame-function e))
638 (setq fl (cdr fl)))
639 (if (frame-live-p sf)
640 (select-frame sf))
641 (handle-delete-frame e)))
642
643
644;;; Utilities
645;;
646(defun dframe-get-focus (frame-var activator &optional hook)
647 "Change frame focus to or from a dedicated frame.
648If the selected frame is not in the symbol FRAME-VAR, then FRAME-VAR
649frame is selected. If the FRAME-VAR is active, then select the
650attached frame. If FRAME-VAR is nil, ACTIVATOR is called to
651created it. HOOK is an optional argument of hooks to run when
652selecting FRAME."
653 (interactive)
654 (if (eq (selected-frame) (symbol-value frame-var))
655 (if (frame-live-p dframe-attached-frame)
656 (dframe-select-attached-frame))
657 ;; make sure we have a frame
658 (if (not (frame-live-p (symbol-value frame-var)))
659 (funcall activator 1))
660 ;; go there
661 (select-frame (symbol-value frame-var))
662 )
663 (other-frame 0)
664 ;; If updates are off, then refresh the frame (they want it now...)
665 (run-hooks 'hook))
666
667
668(defun dframe-close-frame ()
669 "Close the current frame if it is dedicated."
670 (interactive)
671 (if dframe-controlled
672 (let ((b (current-buffer)))
673 (funcall dframe-controlled -1)
674 (kill-buffer b))))
675
676(defun dframe-current-frame (frame-var desired-major-mode)
677 "Return the existing dedicated frame to use.
678FRAME-VAR is the variable storing the currently active dedicated frame.
679If the current frame's buffer uses DESIRED-MAJOR-MODE, then use that frame."
680 (if (not (eq (selected-frame) (symbol-value frame-var)))
681 (if (and (eq major-mode 'desired-major-mode)
682 (get-buffer-window (current-buffer))
683 (window-frame (get-buffer-window (current-buffer))))
684 (window-frame (get-buffer-window (current-buffer)))
685 (symbol-value frame-var))
686 (symbol-value frame-var)))
687
688(defun dframe-attached-frame (&optional frame)
689 "Return the attached frame belonging to the dframe controlled frame FRAME.
690If optional arg FRAME is nil just return `dframe-attached-frame'."
691 (save-excursion
692 (if frame (select-frame frame))
693 dframe-attached-frame))
694
695(defun dframe-select-attached-frame (&optional frame)
696 "Switch to the frame the dframe controlled frame FRAME was started from. If
697optional arg FRAME is nil assume the attached frame is already selected and
698just run the hooks `dframe-after-select-attached-frame-hook'. Return the
699attached frame."
700 (let ((frame (dframe-attached-frame frame)))
701 (if frame (select-frame frame))
702 (prog1 frame
703 (run-hooks 'dframe-after-select-attached-frame-hook))))
704
705(defmacro dframe-with-attached-buffer (&rest forms)
706 "Execute FORMS in the attached frame's special buffer.
707Optionally select that frame if necessary."
708 `(save-selected-window
709 ;;(speedbar-set-timer speedbar-update-speed)
710 (dframe-select-attached-frame)
711 ,@forms
712 (dframe-maybee-jump-to-attached-frame)))
713
714(defun dframe-maybee-jump-to-attached-frame ()
715 "Jump to the attached frame ONLY if this was not a mouse event."
716 (when (or (not (dframe-mouse-event-p last-input-event))
717 dframe-activity-change-focus-flag)
718 (dframe-select-attached-frame)
719 ;; KB: For what is this - raising the frame??
720 (other-frame 0)))
721
722
723(defvar dframe-suppress-message-flag nil
724 "Non-nil means that `dframe-message' should just return a string.")
725
726(defun dframe-message (fmt &rest args)
727 "Like message, but for use in a dedicated frame.
728Argument FMT is the format string, and ARGS are the arguments for message."
729 (save-selected-window
730 (if dframe-suppress-message-flag
731 (apply 'format fmt args)
732 (if dframe-attached-frame
733 ;; KB: Here we do not need calling `dframe-select-attached-frame'
734 (select-frame dframe-attached-frame))
735 (apply 'message fmt args))))
736
737(defun dframe-y-or-n-p (prompt)
738 "Like `y-or-n-p', but for use in a dedicated frame.
739Argument PROMPT is the prompt to use."
740 (save-selected-window
741 (if (and ;;default-minibuffer-frame
742 dframe-attached-frame
743 ;;(not (eq default-minibuffer-frame dframe-attached-frame))
744 )
745 ;; KB: Here we do not need calling `dframe-select-attached-frame'
746 (select-frame dframe-attached-frame))
747 (y-or-n-p prompt)))
748\f
749;;; timer management
750;;
751;; Unlike speedbar with a dedicated set of routines, dframe has one master
752;; timer, and all dframe users will use it. At least until I figure out a way
753;; around that problem.
754;;
755;; Advantage 1: Two apps with timer/frames can munge the master list
756;; to make sure they occur in order.
757;; Advantage 2: If a user hits a key between timer functions, we can
758;; interrupt them safely.
759(defvar dframe-client-functions nil
760 "List of client functions using the dframe timer.")
761
762(defun dframe-set-timer (timeout fn &optional null-on-error)
763 "Apply a timer with TIMEOUT, to call FN, or remove a timer if TIMEOUT is nil.
764TIMEOUT is the number of seconds until the dframe controled program
765timer is called again. When TIMEOUT is nil, turn off all timeouts.
766This function must be called from the buffer belonging to the program
767who requested the timer.
768If NULL-ON-ERROR is a symbol, set it to nil if we cannot create a timer."
769 ;; First, fix up our list of client functions
770 (if timeout
771 (add-to-list 'dframe-client-functions fn)
772 (setq dframe-client-functions (delete fn dframe-client-functions)))
773 ;; Now decided what to do about the timout.
774 (if (or
775 ;; We have a timer, restart the timer with the new time.
776 timeout
777 ;; We have a timer, an off is requested, and no client
778 ;; functions are left, shut er down.
779 (and dframe-timer (not timeout) dframe-client-functions))
780 ;; Only call the low level function if we are changing the state.
781 (dframe-set-timer-internal timeout null-on-error)))
782
783(defun dframe-set-timer-internal (timeout &optional null-on-error)
784 "Apply a timer with TIMEOUT to call the dframe timer manager.
785If NULL-ON-ERROR is a symbol, set it to nil if we cannot create a timer."
786 (cond
787 ;; XEmacs
788 (dframe-xemacsp
789 (with-no-warnings
790 (if dframe-timer
791 (progn (delete-itimer dframe-timer)
792 (setq dframe-timer nil)))
793 (if timeout
794 (if (and dframe-xemacsp
795 (or (>= emacs-major-version 21)
796 (and (= emacs-major-version 20)
797 (> emacs-minor-version 0))
798 (and (= emacs-major-version 19)
799 (>= emacs-minor-version 15))))
800 (setq dframe-timer (start-itimer "dframe"
801 'dframe-timer-fn
802 timeout
803 timeout
804 t))
805 (setq dframe-timer (start-itimer "dframe"
806 'dframe-timer-fn
807 timeout
808 nil))))))
809 ;; Post 19.31 Emacs
810 ((fboundp 'run-with-idle-timer)
811 (if dframe-timer
812 (progn (cancel-timer dframe-timer)
813 (setq dframe-timer nil)))
814 (if timeout
815 (setq dframe-timer
816 (run-with-idle-timer timeout t 'dframe-timer-fn))))
817 ;; Emacs 19.30 (Thanks twice: ptype@dra.hmg.gb)
818 ((fboundp 'post-command-idle-hook)
819 (if timeout
820 (add-hook 'post-command-idle-hook 'dframe-timer-fn)
821 (remove-hook 'post-command-idle-hook 'dframe-timer-fn)))
822 ;; Older or other Emacsen with no timers. Set up so that its
823 ;; obvious this emacs can't handle the updates
824 ((symbolp null-on-error)
825 (set null-on-error nil)))
826 )
827
828(defun dframe-timer-fn ()
829 "Called due to the dframe timer.
830Evaluates all cached timer functions in sequence."
831 (let ((l dframe-client-functions))
832 (while (and l (sit-for 0))
833 (condition-case er
834 (funcall (car l))
835 (error (message "DFRAME TIMER ERROR: %S" er)))
836 (setq l (cdr l)))))
837
838;;; Menu hacking for mouse-3
839;;
840(defconst dframe-pass-event-to-popup-mode-menu
841 (let (max-args)
842 (and (fboundp 'popup-mode-menu)
843 (fboundp 'function-max-args)
844 (setq max-args (function-max-args 'popup-mode-menu))
845 (not (zerop max-args))))
846 "The EVENT arg to 'popup-mode-menu' was introduced in XEmacs 21.4.0.")
847
848;; In XEmacs, we make popup menus work on the item over mouse (as
849;; opposed to where the point happens to be.) We attain this by
850;; temporarily moving the point to that place.
851;; Hrvoje Niksic <hniksic@srce.hr>
852(with-no-warnings
853(defun dframe-xemacs-popup-kludge (event)
854 "Pop up a menu related to the clicked on item.
855Must be bound to EVENT."
856 (interactive "e")
857 (save-excursion
858 (if dframe-pass-event-to-popup-mode-menu
859 (popup-mode-menu event)
860 (goto-char (event-closest-point event))
861 (beginning-of-line)
862 (forward-char (min 5 (- (save-excursion (end-of-line) (point))
863 (save-excursion (beginning-of-line) (point)))))
864 (popup-mode-menu))
865 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
866 ;; menu functions) return immediately.
867 (let (new)
868 (while (not (misc-user-event-p (setq new (next-event))))
869 (dispatch-event new))
870 (dispatch-event new))))
871);with-no-warnings
872
873(defun dframe-emacs-popup-kludge (e)
874 "Pop up a menu related to the clicked on item.
875Must be bound to event E."
876 (interactive "e")
877 (save-excursion
878 (mouse-set-point e)
879 ;; This gets the cursor where the user can see it.
880 (if (not (bolp)) (forward-char -1))
881 (sit-for 0)
882 (if (< emacs-major-version 20)
883 (mouse-major-mode-menu e)
884 (mouse-major-mode-menu e nil))))
885
886;;; Interactive user functions for the mouse
887;;
888(if dframe-xemacsp
889 (defalias 'dframe-mouse-event-p 'button-press-event-p)
890 (defun dframe-mouse-event-p (event)
891 "Return t if the event is a mouse related event."
892 (if (and (listp event)
893 (member (event-basic-type event)
894 '(mouse-1 mouse-2 mouse-3)))
895 t
896 nil)))
897
898(defun dframe-track-mouse (event)
899 "For motion EVENT, display info about the current line."
900 (interactive "e")
901 (when (and dframe-track-mouse-function
902 (or dframe-xemacsp ;; XEmacs always safe?
903 (windowp (posn-window (event-end event))) ; Sometimes
904 ; there is no window to jump into.
905 ))
906
907 (funcall dframe-track-mouse-function event)))
908
909(defun dframe-track-mouse-xemacs (event)
910 "For motion EVENT, display info about the current line."
911 (if (functionp (default-value 'mouse-motion-handler))
912 (funcall (default-value 'mouse-motion-handler) event))
913 (if dframe-track-mouse-function
914 (funcall dframe-track-mouse-function event)))
915
916(defun dframe-help-echo (window &optional buffer position)
917 "Display help based context.
918The context is in WINDOW, viewing BUFFER, at POSITION.
919BUFFER and POSITION are optional because XEmacs doesn't use them."
920 (when (and (not dframe-track-mouse-function)
921 (bufferp buffer)
922 dframe-help-echo-function)
923 (let ((dframe-suppress-message-flag t))
924 (with-current-buffer buffer
925 (if position (goto-char position))
926 (funcall dframe-help-echo-function)))))
927
928(defun dframe-mouse-set-point (e)
929 "Set POINT based on event E.
930Handles clicking on images in XEmacs."
931 (if (save-excursion
932 (save-window-excursion
933 (mouse-set-point e)
934 (and (fboundp 'event-over-glyph-p) (event-over-glyph-p e))))
935 ;; We are in XEmacs, and clicked on a picture
936 (with-no-warnings
937 (let ((ext (event-glyph-extent e)))
938 ;; This position is back inside the extent where the
939 ;; junk we pushed into the property list lives.
940 (if (extent-end-position ext)
941 (goto-char (1- (extent-end-position ext)))
942 (mouse-set-point e)))
943 );with-no-warnings
944 ;; We are not in XEmacs, OR we didn't click on a picture.
945 (mouse-set-point e)))
946
947(defun dframe-quick-mouse (e)
948 "Since mouse events are strange, this will keep the mouse nicely positioned.
949This should be bound to mouse event E."
950 (interactive "e")
951 (dframe-mouse-set-point e)
952 (if dframe-mouse-position-function
953 (funcall dframe-mouse-position-function)))
954
955(defun dframe-power-click (e)
956 "Activate any `dframe' mouse click as a power click.
957A power click will dispose of cached data (if available) or bring a buffer
958up into a different window.
959This should be bound to mouse event E."
960 (interactive "e")
961 (let ((dframe-power-click t))
962 (select-frame last-event-frame)
963 (dframe-click e)))
964
965(defun dframe-click (e)
966 "Call our clients click function on a user click.
967E is the event causing the click."
968 (interactive "e")
969 (dframe-mouse-set-point e)
970 (when dframe-mouse-click-function
971 ;; On the off chance of buffer switch, or something incorrectly
972 ;; configured.
973 (funcall dframe-mouse-click-function e)))
974
975(defun dframe-double-click (e)
976 "Activate the registered click function on a double click.
977This must be bound to a mouse event.
978This should be bound to mouse event E."
979 (interactive "e")
980 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
981 (cond ((eq (car e) 'down-mouse-1)
982 (dframe-mouse-set-point e))
983 ((eq (car e) 'mouse-1)
984 (dframe-quick-mouse e))
985 ((or (eq (car e) 'double-down-mouse-1)
986 (eq (car e) 'triple-down-mouse-1))
987 (dframe-click e))))
988
989;;; Hacks of normal things.
990;;
991;; Some normal things that happen in one of these dedicated frames
992;; must be handled specially, so that our dedicated frame isn't
993;; messed up.
994(defun dframe-temp-buffer-show-function (buffer)
995 "Placed in the variable `temp-buffer-show-function' in dedicated frames.
996If a user requests help using \\[help-command] <Key> the temp BUFFER will be
997redirected into a window on the attached frame."
998 (if dframe-attached-frame (dframe-select-attached-frame))
999 (pop-to-buffer buffer nil)
1000 (other-window -1)
1001 ;; Fix for using this hook on some platforms: Bob Weiner
1002 (cond ((not dframe-xemacsp)
1003 (run-hooks 'temp-buffer-show-hook))
1004 ((fboundp 'run-hook-with-args)
1005 (run-hook-with-args 'temp-buffer-show-hook buffer))
1006 ((and (boundp 'temp-buffer-show-hook)
1007 (listp temp-buffer-show-hook))
1008 (mapcar (function (lambda (hook) (funcall hook buffer)))
1009 temp-buffer-show-hook))))
1010
1011(defun dframe-hack-buffer-menu (e)
1012 "Control mouse 1 is buffer menu.
1013This hack overrides it so that the right thing happens in the main
1014Emacs frame, not in the dedicated frame.
1015Argument E is the event causing this activity."
1016 (interactive "e")
1017 (let ((fn (lookup-key global-map (if dframe-xemacsp
1018 '(control button1)
1019 [C-down-mouse-1])))
1020 (oldbuff (current-buffer))
1021 (newbuff nil))
1022 (unwind-protect
1023 (save-excursion
1024 (set-window-dedicated-p (selected-window) nil)
1025 (call-interactively fn)
1026 (setq newbuff (current-buffer)))
1027 (switch-to-buffer oldbuff)
1028 (set-window-dedicated-p (selected-window) t))
1029 (if (not (eq newbuff oldbuff))
1030 (dframe-with-attached-buffer
1031 (switch-to-buffer newbuff)))))
1032
1033(defun dframe-switch-buffer-attached-frame (&optional buffer)
1034 "Switch to BUFFER in the attached frame, and raise that frame.
1035This overrides the default behavior of `switch-to-buffer' which is
1036broken because of the dedicated frame."
1037 (interactive)
1038 ;; Assume we are in the dedicated frame.
1039 (other-frame 1)
1040 ;; Now switch buffers
1041 (if buffer
1042 (switch-to-buffer buffer)
1043 (call-interactively 'switch-to-buffer nil nil)))
1044
1045;; XEmacs: this can be implemented using modeline keymaps, but there
1046;; is no use, as we have horizontal scrollbar (as the docstring
1047;; hints.)
1048(defun dframe-mouse-hscroll (e)
1049 "Read a mouse event E from the mode line, and horizontally scroll.
1050If the mouse is being clicked on the far left, or far right of the
1051mode-line. This is only useful for non-XEmacs."
1052 (interactive "e")
1053 (let* ((x-point (car (nth 2 (car (cdr e)))))
1054 (pixels-per-10-col (/ (* 10 (frame-pixel-width))
1055 (frame-width)))
1056 (click-col (1+ (/ (* 10 x-point) pixels-per-10-col)))
1057 )
1058 (cond ((< click-col 3)
1059 (scroll-left 2))
1060 ((> click-col (- (window-width) 5))
1061 (scroll-right 2))
1062 (t (dframe-message
1063 "Click on the edge of the modeline to scroll left/right")))
1064 ))
1065
1066(provide 'dframe)
1067
1068;;; dframe.el ends here