Small rmail fixes.
[bpt/emacs.git] / lisp / dframe.el
CommitLineData
7cfc18c4
CY
1;;; dframe --- dedicate frame support modes
2
95df8112 3;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
7cfc18c4
CY
4
5;; Author: Eric M. Ludlam <zappo@gnu.org>
6;; Keywords: file, tags, tools
7cfc18c4
CY
7
8(defvar dframe-version "1.3"
9 "The current version of the dedicated frame library.")
10
11;; This file is part of GNU Emacs.
12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
7cfc18c4 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
7cfc18c4
CY
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7cfc18c4
CY
25
26;;; Commentary:
27;;
28;; This code was developed and maintained as a part of speedbar since 1996.
29;; It became its own support utility in Aug 2000.
30;;
31;; Dedicated frame mode is an Emacs independent library for supporting
32;; a program/buffer combination that resides in a dedicated frame.
33;; Support of this nature requires several complex interactions with the
34;; user which this library will provide, including:
35;;
36;; * Creation of a frame. Positioned relatively.
37;; Includes a frame cache for User position caching.
38;; * Switching between frames.
39;; * Timed activities using idle-timers
40;; * Frame/buffer killing hooks
41;; * Mouse-3 position relative menu
42;; * Mouse motion, help-echo hacks
5b86d3e4 43;; * Mouse clicking, double clicking, & XEmacs image clicking hack
7cfc18c4
CY
44;; * Mode line hacking
45;; * Utilities for use in a program covering:
46;; o keymap massage for some actions
47;; o working with an associated buffer
48;; o shift-click
49;; o detaching a frame
50;; o focus-shifting & optional frame jumping
51;; o currently active frame.
52;; o message/y-or-n-p
53;; o mouse set point
54;;
55;; To Use:
56;; 1) (require 'dframe)
57;; 2) Variable Setup:
58;; -frame-parameters -- Frame parameters for Emacs.
59;; -frame-plist -- Frame parameters for XEmacs.
60;; -- Not on parameter lists: They can optionally include width
61;; and height. If width or height is not included, then it will
62;; be provided to match the originating frame. In general,
63;; turning off the menu bar, mode line, and minibuffer can
64;; provide a smaller window, or more display area.
65;; -track-mouse-flag -- mouse tracking on/off specific to your tool.
66;; -update-flag -- app toggle for timer use. Init from
67;; `dframe-have-timer-flag'. This is nil for terminals, since
68;; updating a frame in a terminal is not useful to the user.
69;; -key-map -- Your keymap. Call `dframe-update-keymap' on it.
70;; -buffer, -frame, -cached-frame -- Variables used to track your
71;; applications buffer, frame, or frame cache (when hidden). See
72;; `dframe-frame-mode' for details.
73;; -before-delete-hook, -before-popup-hook, -after-create-hook --
74;; Hooks to have called. The `-after-create-hook' probably wants
75;; to call a function which calls `dframe-reposition-frame' in an
76;; appropriate manner.
77;; 3) Function Setup:
78;; your-frame-mode -- function to toggle your app frame on and off.
79;; its tasks are:
80;; a) create a buffer
81;; b) Call `dframe-frame-mode'. (See its doc)
82;; c) If successful (your -frame variable has a value), call
83;; timer setup if applicable.
84;; your-frame-reposition- -- Function to call from after-create-hook to
85;; reposition your frame with `dframe-repsoition-frame'.
86;; your-mode -- Set up the major mode of the buffer for your app.
87;; Set these variables: dframe-track-mouse-function,
88;; dframe-help-echo-function,
89;; dframe-mouse-click-function,
90;; dframe-mouse-position-function.
91;; See speedbar's implementation of these functions.
92;; `speedbar-current-frame', `speedbar-get-focus', `speedbar-message',
93;; `speedbar-y-or-n-p', `speedbar-set-timer', `speedbar-click',
94;; `speedbar-position-cursor-on-line'
95;; 4) Handling mouse clicks, and help text:
96;; dframe-track-mouse, dframe-help-echo-function --
97;; These variables need to be set to functions that display info
98;; based on the mouse's position.
99;; Text propert 'help-echo, set to `dframe-help-echo', which will
100;; call `dframe-help-echo-function'.
101;; Have a `-click' function, it can call `dframe-quick-mouse' for
102;; positioning. If the variable `dframe-power-click' is non-nil,
103;; then `shift' was held down during the click.
104
105;;; Bugs
106;;
107;; * The timer managers doesn't handle multiple different timeouts.
5b86d3e4 108;; * You can't specify continuous timeouts (as opposed to just idle timers.)
7cfc18c4 109
58eb29e5
JB
110(defvar x-pointer-hand2)
111(defvar x-pointer-top-left-arrow)
112
7cfc18c4 113;;; Code:
7cfc18c4
CY
114\f
115;;; Compatibility functions
116;;
78b35906
SM
117(defalias 'dframe-frame-parameter
118 (if (fboundp 'frame-parameter) 'frame-parameter
119 (lambda (frame parameter)
120 "Return FRAME's PARAMETER value."
121 (cdr (assoc parameter (frame-parameters frame))))))
7cfc18c4
CY
122
123\f
124;;; Variables
125;;
126(defgroup dframe nil
127 "Faces used in dframe."
128 :prefix "dframe-"
129 :group 'dframe)
130
5b86d3e4
GM
131(defvar dframe-have-timer-flag (if (fboundp 'display-graphic-p)
132 (display-graphic-p)
133 window-system)
134 "Non-nil means that timers are available for this Emacs.
135This is nil for terminals, since updating a frame in a terminal
136is not useful to the user.")
7cfc18c4
CY
137
138(defcustom dframe-update-speed
5b86d3e4 139 (if (featurep 'xemacs) 2 ; 1 is too obrusive in XEmacs
7cfc18c4 140 1)
78b35906 141 "Idle time in seconds needed before dframe will update itself.
7cfc18c4
CY
142Updates occur to allow dframe to display directory information
143relevant to the buffer you are currently editing."
144 :group 'dframe
145 :type 'integer)
146
147(defcustom dframe-activity-change-focus-flag nil
78b35906 148 "Non-nil means the selected frame will change based on activity.
7cfc18c4
CY
149Thus, if a file is selected for edit, the buffer will appear in the
150selected frame and the focus will change to that frame."
151 :group 'dframe
152 :type 'boolean)
153
154(defcustom dframe-after-select-attached-frame-hook nil
78b35906 155 "Hook run after dframe has selected the attached frame."
7cfc18c4
CY
156 :group 'dframe
157 :type 'hook)
158
159(defvar dframe-track-mouse-function nil
160 "*A function to call when the mouse is moved in the given frame.
161Typically used to display info about the line under the mouse.")
162(make-variable-buffer-local 'dframe-track-mouse-function)
163
164(defvar dframe-help-echo-function nil
165 "*A function to call when help-echo is used in newer versions of Emacs.
166Typically used to display info about the line under the mouse.")
167(make-variable-buffer-local 'dframe-help-echo-function)
168
169(defvar dframe-mouse-click-function nil
170 "*A function to call when the mouse is clicked.
171Valid clicks are mouse 2, our double mouse 1.")
172(make-variable-buffer-local 'dframe-mouse-click-function)
173
174(defvar dframe-mouse-position-function nil
ca68aad8 175 "*A function to call to position the cursor for a mouse click.")
7cfc18c4
CY
176(make-variable-buffer-local 'dframe-mouse-position-function)
177
178(defvar dframe-power-click nil
179 "Never set this by hand. Value is t when S-mouse activity occurs.")
180
181(defvar dframe-timer nil
182 "The dframe timer used for updating the buffer.")
183(make-variable-buffer-local 'dframe-timer)
184
185(defvar dframe-attached-frame nil
186 "The frame which started a frame mode.
187This is the frame from which all interesting activities will go
188for the mode using dframe.")
189(make-variable-buffer-local 'dframe-attached-frame)
190
191(defvar dframe-controlled nil
192 "Is this buffer controlled by a dedicated frame.
193Local to those buffers, as a function called that created it.")
194(make-variable-buffer-local 'dframe-controlled)
195
196(defun dframe-update-keymap (map)
197 "Update the keymap MAP for dframe default bindings."
198 ;; Frame control
199 (define-key map "q" 'dframe-close-frame)
200 (define-key map "Q" 'delete-frame)
201
202 ;; Override switch to buffer to never hack our frame.
203 (substitute-key-definition 'switch-to-buffer
204 'dframe-switch-buffer-attached-frame
205 map global-map)
206
78b35906 207 (if (featurep 'xemacs)
7cfc18c4
CY
208 (progn
209 ;; mouse bindings so we can manipulate the items on each line
210 (define-key map 'button2 'dframe-click)
211 (define-key map '(shift button2) 'dframe-power-click)
212 ;; Info doc fix from Bob Weiner
213 (if (featurep 'infodoc)
214 nil
78b35906 215 (define-key map 'button3 'dframe-popup-kludge))
7cfc18c4
CY
216 )
217
218 ;; mouse bindings so we can manipulate the items on each line
222a6c9b
CY
219 ;; (define-key map [down-mouse-1] 'dframe-double-click)
220 (define-key map [follow-link] 'mouse-face)
7cfc18c4
CY
221 (define-key map [mouse-2] 'dframe-click)
222 ;; This is the power click for new frames, or refreshing a cache
223 (define-key map [S-mouse-2] 'dframe-power-click)
224 ;; This adds a small unecessary visual effect
225 ;;(define-key map [down-mouse-2] 'dframe-quick-mouse)
226
78b35906 227 (define-key map [down-mouse-3] 'dframe-popup-kludge)
7cfc18c4
CY
228
229 ;; This lets the user scroll as if we had a scrollbar... well maybe not
230 (define-key map [mode-line mouse-2] 'dframe-mouse-hscroll)
231 ;; another handy place users might click to get our menu.
232 (define-key map [mode-line down-mouse-1]
78b35906 233 'dframe-popup-kludge)
7cfc18c4
CY
234
235 ;; We can't switch buffers with the buffer mouse menu. Lets hack it.
236 (define-key map [C-down-mouse-1] 'dframe-hack-buffer-menu)
237
238 ;; Lastly, we want to track the mouse. Play here
239 (define-key map [mouse-movement] 'dframe-track-mouse)
240 ))
241
242(defun dframe-live-p (frame)
243 "Return non-nil if FRAME is currently available."
244 (and frame (frame-live-p frame) (frame-visible-p frame)))
245
06b60517
JB
246(defvar x-sensitive-text-pointer-shape)
247(defvar x-pointer-shape)
248
7cfc18c4
CY
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
254 )
255 "Manage a frame for an application, enabling it when ARG is positive.
256FRAME-VAR is a variable used to cache the frame being used.
257This frame is either resurrected, hidden, killed, etc based on
258the value.
259CACHE-VAR is a variable used to cache a cached frame.
260BUFFER-VAR is a variable used to cache the buffer being used in dframe.
26e98da9 261This buffer will have `dframe-frame-mode' run on it.
7cfc18c4
CY
262FRAME-NAME is the name of the frame to create.
263LOCAL-MODE-FN is the function used to call this one.
264PARAMETERS are frame parameters to apply to this dframe.
265DELETE-HOOK are hooks to run when deleting a frame.
266POPUP-HOOK are hooks to run before showing a frame.
267CREATE-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.
78b35906 288 (with-current-buffer (symbol-value buffer-var)
7cfc18c4
CY
289 ;; Declare this buffer a dedicated frame
290 (setq dframe-controlled local-mode-fn)
291
78b35906
SM
292 (if (featurep 'xemacs)
293 (progn
7cfc18c4
CY
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
e8dc1f8c 313 (set (make-local-variable 'track-mouse) t))) ;this could be messy.
78b35906 314;;;; DISABLED: This causes problems for users with multiple frames.
7cfc18c4
CY
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
340 nil
341 (if dframe-controlled
342 (progn
343 (funcall dframe-controlled -1)
344 (setq ,buffer-var nil)
345 )))))
346 t t)
347 )
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))
359 )
360 (if (frame-live-p (symbol-value frame-var))
361 (raise-frame (symbol-value frame-var))
362 (set frame-var
78b35906 363 (if (featurep 'xemacs)
7cfc18c4
CY
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
d7dbc017 379 (list (cons 'height (+ (or mh 0) (frame-height)))))))
7cfc18c4
CY
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
5b86d3e4 389 (if (not (eq window-system 'x))
7cfc18c4
CY
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 ))) )
414
415(defun dframe-reposition-frame (new-frame parent-frame location)
416 "Move NEW-FRAME to be relative to PARENT-FRAME.
417LOCATION can be one of 'random, 'left, 'right, 'left-right, or 'top-bottom."
78b35906 418 (if (featurep 'xemacs)
7cfc18c4
CY
419 (dframe-reposition-frame-xemacs new-frame parent-frame location)
420 (dframe-reposition-frame-emacs new-frame parent-frame location)))
421
2a41ed35
GM
422;; Not defined in builds without X, but behind window-system test.
423(declare-function x-display-pixel-width "xfns.c" (&optional terminal))
424(declare-function x-display-pixel-height "xfns.c" (&optional terminal))
425
7cfc18c4
CY
426(defun dframe-reposition-frame-emacs (new-frame parent-frame location)
427 "Move NEW-FRAME to be relative to PARENT-FRAME.
428LOCATION can be one of 'random, 'left-right, 'top-bottom, or
ca68aad8 429a cons cell indicating a position of the form (LEFT . TOP)."
2a41ed35
GM
430 ;; Position dframe.
431 ;; Do no positioning if not on a windowing system,
432 (unless (or (not window-system) (eq window-system 'pc))
433 (let* ((pfx (dframe-frame-parameter parent-frame 'left))
434 (pfy (dframe-frame-parameter parent-frame 'top))
fb0cf781
J
435 (pfw (+ (tool-bar-pixel-width parent-frame)
436 (frame-pixel-width parent-frame)))
2a41ed35
GM
437 (pfh (frame-pixel-height parent-frame))
438 (nfw (frame-pixel-width new-frame))
439 (nfh (frame-pixel-height new-frame))
440 newleft newtop)
7cfc18c4
CY
441 ;; Rebuild pfx,pfy to be absolute positions.
442 (setq pfx (if (not (consp pfx))
443 pfx
444 ;; If pfx is a list, that means we grow
445 ;; from a specific edge of the display.
446 ;; Convert that to the distance from the
447 ;; left side of the display.
448 (if (eq (car pfx) '-)
449 ;; A - means distance from the right edge
450 ;; of the display, or DW - pfx - framewidth
451 (- (x-display-pixel-width) (car (cdr pfx)) pfw)
452 (car (cdr pfx))))
453 pfy (if (not (consp pfy))
454 pfy
455 ;; If pfy is a list, that means we grow
456 ;; from a specific edge of the display.
457 ;; Convert that to the distance from the
458 ;; left side of the display.
459 (if (eq (car pfy) '-)
460 ;; A - means distance from the right edge
461 ;; of the display, or DW - pfx - framewidth
462 (- (x-display-pixel-height) (car (cdr pfy)) pfh)
2a41ed35 463 (car (cdr pfy)))))
7cfc18c4 464 (cond ((eq location 'right)
fb0cf781 465 (setq newleft (+ pfx pfw 10)
7cfc18c4
CY
466 newtop pfy))
467 ((eq location 'left)
8a2c27b9 468 (setq newleft (- pfx 10 nfw)
7cfc18c4
CY
469 newtop pfy))
470 ((eq location 'left-right)
471 (setq newleft
472 ;; Decide which side to put it on. 200 is just a
473 ;; buffer for the left edge of the screen. The
474 ;; extra 10 is just dressings for window
475 ;; decorations.
476 (let* ((left-guess (- pfx 10 nfw))
fb0cf781 477 (right-guess (+ pfx pfw 10))
7cfc18c4
CY
478 (left-margin left-guess)
479 (right-margin (- (x-display-pixel-width)
480 right-guess 5 nfw)))
481 (cond ((>= left-margin 0) left-guess)
482 ((>= right-margin 0) right-guess)
483 ;; otherwise choose side we overlap less
484 ((> left-margin right-margin) 0)
485 (t (- (x-display-pixel-width) nfw 5))))
2a41ed35 486 newtop pfy))
7cfc18c4
CY
487 ((eq location 'top-bottom)
488 (setq newleft pfx
489 newtop
490 ;; Try and guess if we should be on the top or bottom.
491 (let* ((top-guess (- pfy 15 nfh))
492 (bottom-guess (+ pfy 5 pfh))
493 (top-margin top-guess)
494 (bottom-margin (- (x-display-pixel-height)
495 bottom-guess 5 nfh)))
496 (cond ((>= top-margin 0) top-guess)
497 ((>= bottom-margin 0) bottom-guess)
498 ;; Choose a side to overlap the least.
499 ((> top-margin bottom-margin) 0)
2a41ed35 500 (t (- (x-display-pixel-height) nfh 5))))))
7cfc18c4
CY
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
2a41ed35
GM
506 (list (cons 'left newleft)
507 (cons 'top newtop))))))
7cfc18c4 508
06b60517 509(defun dframe-reposition-frame-xemacs (_new-frame _parent-frame _location)
7cfc18c4
CY
510 "Move NEW-FRAME to be relative to PARENT-FRAME.
511LOCATION can be one of 'random, 'left-right, or 'top-bottom."
512 ;; Not yet implemented
513 )
514
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)))))
524
525(defun dframe-detach (frame-var cache-var buffer-var)
dbdb7031 526 "Detach the frame in symbol FRAME-VAR.
7cfc18c4 527CACHE-VAR and BUFFER-VAR are symbols as in `dframe-frame-mode'"
78b35906 528 (with-current-buffer (symbol-value buffer-var)
7cfc18c4
CY
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)
78b35906 534 ;; FIXME: Looks very suspicious. Luckily this function is unused.
7cfc18c4
CY
535 (make-variable-buffer-local frame-var)
536 (set frame-var oldframe)
537 )))
538
539;;; Special frame event proxies
540;;
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))
549 )
550
551(defvar dframe-make-frame-visible-function nil
552 "Function used when a dframe controlled frame is de-iconified.
553The function must take an EVENT.")
554(defvar dframe-iconify-frame-function nil
555 "Function used when a dframe controlled frame is iconified.
556The 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.
559The function must take an EVENT.")
560
561(defun dframe-handle-make-frame-visible (e)
562 "Handle a `make-frame-visible' event.
26e98da9 563Should enable auto-updating if the last state was also enabled.
7cfc18c4
CY
564Argument 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)
570 )))
571
572(defun dframe-handle-iconify-frame (e)
573 "Handle a `iconify-frame' event.
26e98da9 574Should disable auto-updating if the last state was also enabled.
7cfc18c4
CY
575Argument 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)
581 )))
582
583(defun dframe-handle-delete-frame (e)
584 "Handle `delete-frame' event.
585Argument 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)))
599
600
601;;; Utilities
602;;
603(defun dframe-get-focus (frame-var activator &optional hook)
604 "Change frame focus to or from a dedicated frame.
605If the selected frame is not in the symbol FRAME-VAR, then FRAME-VAR
606frame is selected. If the FRAME-VAR is active, then select the
607attached frame. If FRAME-VAR is nil, ACTIVATOR is called to
608created it. HOOK is an optional argument of hooks to run when
26e98da9 609selecting FRAME-VAR."
7cfc18c4
CY
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))
619 )
620 (other-frame 0)
621 ;; If updates are off, then refresh the frame (they want it now...)
622 (run-hooks 'hook))
623
624
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))))
632
633(defun dframe-current-frame (frame-var desired-major-mode)
634 "Return the existing dedicated frame to use.
635FRAME-VAR is the variable storing the currently active dedicated frame.
636If the current frame's buffer uses DESIRED-MAJOR-MODE, then use that frame."
637 (if (not (eq (selected-frame) (symbol-value frame-var)))
2d6af8dd 638 (if (and (eq major-mode desired-major-mode)
7cfc18c4
CY
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)))
644
645(defun dframe-attached-frame (&optional frame)
646 "Return the attached frame belonging to the dframe controlled frame FRAME.
647If optional arg FRAME is nil just return `dframe-attached-frame'."
648 (save-excursion
649 (if frame (select-frame frame))
650 dframe-attached-frame))
651
652(defun dframe-select-attached-frame (&optional frame)
26e98da9
JB
653 "Switch to the frame the dframe controlled frame FRAME was started from.
654If optional arg FRAME is nil assume the attached frame is already selected
655and just run the hooks `dframe-after-select-attached-frame-hook'. Return
656the attached frame."
7cfc18c4
CY
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))))
661
662(defmacro dframe-with-attached-buffer (&rest forms)
663 "Execute FORMS in the attached frame's special buffer.
664Optionally 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)))
670
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)
58eb29e5 676 ;; KB: For what is this - raising the frame??
7cfc18c4
CY
677 (other-frame 0)))
678
679
680(defvar dframe-suppress-message-flag nil
681 "Non-nil means that `dframe-message' should just return a string.")
682
683(defun dframe-message (fmt &rest args)
684 "Like message, but for use in a dedicated frame.
685Argument 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))))
693
694(defun dframe-y-or-n-p (prompt)
695 "Like `y-or-n-p', but for use in a dedicated frame.
696Argument 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))
701 )
702 ;; KB: Here we do not need calling `dframe-select-attached-frame'
703 (select-frame dframe-attached-frame))
704 (y-or-n-p prompt)))
705\f
706;;; timer management
707;;
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.
711;;
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.")
718
06b60517 719(defun dframe-set-timer (timeout fn &optional _null-on-error)
7cfc18c4
CY
720 "Apply a timer with TIMEOUT, to call FN, or remove a timer if TIMEOUT is nil.
721TIMEOUT is the number of seconds until the dframe controled program
722timer is called again. When TIMEOUT is nil, turn off all timeouts.
723This function must be called from the buffer belonging to the program
06b60517 724who requested the timer. NULL-ON-ERROR is ignored."
7cfc18c4
CY
725 ;; First, fix up our list of client functions
726 (if timeout
727 (add-to-list 'dframe-client-functions fn)
728 (setq dframe-client-functions (delete fn dframe-client-functions)))
729 ;; Now decided what to do about the timout.
730 (if (or
731 ;; We have a timer, restart the timer with the new time.
732 timeout
733 ;; We have a timer, an off is requested, and no client
734 ;; functions are left, shut er down.
735 (and dframe-timer (not timeout) dframe-client-functions))
736 ;; Only call the low level function if we are changing the state.
06b60517 737 (dframe-set-timer-internal timeout)))
7cfc18c4 738
06b60517 739(defun dframe-set-timer-internal (timeout &optional _null-on-error)
5b86d3e4
GM
740 "Apply a timer with TIMEOUT to call the dframe timer manager."
741 (when dframe-timer
742 (if (featurep 'xemacs)
743 (delete-itimer dframe-timer)
744 (cancel-timer dframe-timer))
745 (setq dframe-timer nil))
746 (when timeout
747 (setq dframe-timer
748 (if (featurep 'xemacs)
749 (start-itimer "dframe" 'dframe-timer-fn
750 timeout timeout t)
751 (run-with-idle-timer timeout t 'dframe-timer-fn)))))
7cfc18c4
CY
752
753(defun dframe-timer-fn ()
754 "Called due to the dframe timer.
755Evaluates all cached timer functions in sequence."
756 (let ((l dframe-client-functions))
757 (while (and l (sit-for 0))
758 (condition-case er
759 (funcall (car l))
760 (error (message "DFRAME TIMER ERROR: %S" er)))
761 (setq l (cdr l)))))
762
763;;; Menu hacking for mouse-3
764;;
765(defconst dframe-pass-event-to-popup-mode-menu
766 (let (max-args)
767 (and (fboundp 'popup-mode-menu)
768 (fboundp 'function-max-args)
769 (setq max-args (function-max-args 'popup-mode-menu))
770 (not (zerop max-args))))
ca68aad8 771 "The EVENT arg to `popup-mode-menu' was introduced in XEmacs 21.4.0.")
7cfc18c4
CY
772
773;; In XEmacs, we make popup menus work on the item over mouse (as
774;; opposed to where the point happens to be.) We attain this by
775;; temporarily moving the point to that place.
776;; Hrvoje Niksic <hniksic@srce.hr>
78b35906
SM
777(defalias 'dframe-popup-kludge
778 (if (featurep 'xemacs)
779 (lambda (event) ; XEmacs.
780 "Pop up a menu related to the clicked on item.
7cfc18c4 781Must be bound to EVENT."
78b35906
SM
782 (interactive "e")
783 (save-excursion
784 (if dframe-pass-event-to-popup-mode-menu
785 (popup-mode-menu event)
786 (goto-char (event-closest-point event))
787 (beginning-of-line)
5ed619e0
GM
788 (forward-char (min 5 (- (line-end-position)
789 (line-beginning-position))))
78b35906
SM
790 (popup-mode-menu))
791 ;; Wait for menu to bail out. `popup-mode-menu' (and other popup
792 ;; menu functions) return immediately.
793 (let (new)
794 (while (not (misc-user-event-p (setq new (next-event))))
795 (dispatch-event new))
796 (dispatch-event new))))
797
798 (lambda (e) ; Emacs.
799 "Pop up a menu related to the clicked on item.
7cfc18c4 800Must be bound to event E."
78b35906
SM
801 (interactive "e")
802 (save-excursion
803 (mouse-set-point e)
804 ;; This gets the cursor where the user can see it.
805 (if (not (bolp)) (forward-char -1))
806 (sit-for 0)
5b86d3e4
GM
807 (if (fboundp 'mouse-menu-major-mode-map)
808 (popup-menu (mouse-menu-major-mode-map) e)
809 (with-no-warnings ; don't warn about obsolete fallback
810 (mouse-major-mode-menu e nil)))))))
7cfc18c4
CY
811
812;;; Interactive user functions for the mouse
813;;
78b35906
SM
814(defalias 'dframe-mouse-event-p
815 (if (featurep 'xemacs)
816 'button-press-event-p
817 (lambda (event)
818 "Return t if the event is a mouse related event."
819 (if (and (listp event)
820 (member (event-basic-type event)
821 '(mouse-1 mouse-2 mouse-3)))
822 t
823 nil))))
7cfc18c4
CY
824
825(defun dframe-track-mouse (event)
826 "For motion EVENT, display info about the current line."
827 (interactive "e")
828 (when (and dframe-track-mouse-function
78b35906 829 (or (featurep 'xemacs) ;; XEmacs always safe?
7cfc18c4
CY
830 (windowp (posn-window (event-end event))) ; Sometimes
831 ; there is no window to jump into.
832 ))
58eb29e5 833
7cfc18c4
CY
834 (funcall dframe-track-mouse-function event)))
835
836(defun dframe-track-mouse-xemacs (event)
837 "For motion EVENT, display info about the current line."
838 (if (functionp (default-value 'mouse-motion-handler))
839 (funcall (default-value 'mouse-motion-handler) event))
840 (if dframe-track-mouse-function
841 (funcall dframe-track-mouse-function event)))
842
06b60517 843(defun dframe-help-echo (_window &optional buffer position)
7cfc18c4
CY
844 "Display help based context.
845The context is in WINDOW, viewing BUFFER, at POSITION.
846BUFFER and POSITION are optional because XEmacs doesn't use them."
847 (when (and (not dframe-track-mouse-function)
848 (bufferp buffer)
849 dframe-help-echo-function)
850 (let ((dframe-suppress-message-flag t))
851 (with-current-buffer buffer
222a6c9b
CY
852 (save-excursion
853 (if position (goto-char position))
854 (funcall dframe-help-echo-function))))))
7cfc18c4
CY
855
856(defun dframe-mouse-set-point (e)
ca68aad8 857 "Set point based on event E.
7cfc18c4 858Handles clicking on images in XEmacs."
78b35906
SM
859 (if (and (featurep 'xemacs)
860 (save-excursion
861 (save-window-excursion
862 (mouse-set-point e)
863 (event-over-glyph-p e))))
7cfc18c4 864 ;; We are in XEmacs, and clicked on a picture
7cfc18c4
CY
865 (let ((ext (event-glyph-extent e)))
866 ;; This position is back inside the extent where the
867 ;; junk we pushed into the property list lives.
868 (if (extent-end-position ext)
869 (goto-char (1- (extent-end-position ext)))
870 (mouse-set-point e)))
7cfc18c4
CY
871 ;; We are not in XEmacs, OR we didn't click on a picture.
872 (mouse-set-point e)))
873
874(defun dframe-quick-mouse (e)
875 "Since mouse events are strange, this will keep the mouse nicely positioned.
876This should be bound to mouse event E."
877 (interactive "e")
878 (dframe-mouse-set-point e)
879 (if dframe-mouse-position-function
880 (funcall dframe-mouse-position-function)))
881
882(defun dframe-power-click (e)
26e98da9 883 "Activate any dframe mouse click as a power click.
7cfc18c4
CY
884A power click will dispose of cached data (if available) or bring a buffer
885up into a different window.
886This should be bound to mouse event E."
887 (interactive "e")
888 (let ((dframe-power-click t))
889 (select-frame last-event-frame)
890 (dframe-click e)))
891
892(defun dframe-click (e)
893 "Call our clients click function on a user click.
894E is the event causing the click."
895 (interactive "e")
896 (dframe-mouse-set-point e)
897 (when dframe-mouse-click-function
898 ;; On the off chance of buffer switch, or something incorrectly
899 ;; configured.
900 (funcall dframe-mouse-click-function e)))
901
902(defun dframe-double-click (e)
903 "Activate the registered click function on a double click.
904This must be bound to a mouse event.
905This should be bound to mouse event E."
906 (interactive "e")
907 ;; Emacs only. XEmacs handles this via `mouse-track-click-hook'.
908 (cond ((eq (car e) 'down-mouse-1)
909 (dframe-mouse-set-point e))
910 ((eq (car e) 'mouse-1)
911 (dframe-quick-mouse e))
912 ((or (eq (car e) 'double-down-mouse-1)
913 (eq (car e) 'triple-down-mouse-1))
914 (dframe-click e))))
915
916;;; Hacks of normal things.
917;;
918;; Some normal things that happen in one of these dedicated frames
919;; must be handled specially, so that our dedicated frame isn't
920;; messed up.
921(defun dframe-temp-buffer-show-function (buffer)
922 "Placed in the variable `temp-buffer-show-function' in dedicated frames.
923If a user requests help using \\[help-command] <Key> the temp BUFFER will be
924redirected into a window on the attached frame."
925 (if dframe-attached-frame (dframe-select-attached-frame))
926 (pop-to-buffer buffer nil)
927 (other-window -1)
928 ;; Fix for using this hook on some platforms: Bob Weiner
78b35906 929 (cond ((not (featurep 'xemacs))
7cfc18c4
CY
930 (run-hooks 'temp-buffer-show-hook))
931 ((fboundp 'run-hook-with-args)
932 (run-hook-with-args 'temp-buffer-show-hook buffer))
933 ((and (boundp 'temp-buffer-show-hook)
934 (listp temp-buffer-show-hook))
935 (mapcar (function (lambda (hook) (funcall hook buffer)))
936 temp-buffer-show-hook))))
937
06b60517 938(defun dframe-hack-buffer-menu (_e)
7cfc18c4
CY
939 "Control mouse 1 is buffer menu.
940This hack overrides it so that the right thing happens in the main
941Emacs frame, not in the dedicated frame.
942Argument E is the event causing this activity."
943 (interactive "e")
78b35906
SM
944 (let ((fn (lookup-key global-map (if (featurep 'xemacs)
945 '(control button1)
7cfc18c4
CY
946 [C-down-mouse-1])))
947 (oldbuff (current-buffer))
948 (newbuff nil))
949 (unwind-protect
950 (save-excursion
951 (set-window-dedicated-p (selected-window) nil)
952 (call-interactively fn)
953 (setq newbuff (current-buffer)))
954 (switch-to-buffer oldbuff)
955 (set-window-dedicated-p (selected-window) t))
956 (if (not (eq newbuff oldbuff))
957 (dframe-with-attached-buffer
958 (switch-to-buffer newbuff)))))
959
960(defun dframe-switch-buffer-attached-frame (&optional buffer)
961 "Switch to BUFFER in the attached frame, and raise that frame.
962This overrides the default behavior of `switch-to-buffer' which is
963broken because of the dedicated frame."
964 (interactive)
965 ;; Assume we are in the dedicated frame.
966 (other-frame 1)
967 ;; Now switch buffers
968 (if buffer
969 (switch-to-buffer buffer)
970 (call-interactively 'switch-to-buffer nil nil)))
971
972;; XEmacs: this can be implemented using modeline keymaps, but there
973;; is no use, as we have horizontal scrollbar (as the docstring
974;; hints.)
975(defun dframe-mouse-hscroll (e)
976 "Read a mouse event E from the mode line, and horizontally scroll.
977If the mouse is being clicked on the far left, or far right of the
978mode-line. This is only useful for non-XEmacs."
979 (interactive "e")
980 (let* ((x-point (car (nth 2 (car (cdr e)))))
981 (pixels-per-10-col (/ (* 10 (frame-pixel-width))
982 (frame-width)))
983 (click-col (1+ (/ (* 10 x-point) pixels-per-10-col)))
984 )
985 (cond ((< click-col 3)
986 (scroll-left 2))
987 ((> click-col (- (window-width) 5))
988 (scroll-right 2))
989 (t (dframe-message
990 "Click on the edge of the modeline to scroll left/right")))
991 ))
992
993(provide 'dframe)
994
995;;; dframe.el ends here