0
|
1 ;;; -*- Mode: Emacs-Lisp -*-
|
|
2
|
|
3 ;;; ilisp-out.el --
|
|
4
|
|
5 ;;; This file is part of ILISP.
|
4
|
6 ;;; Version: 5.8
|
0
|
7 ;;;
|
|
8 ;;; Copyright (C) 1990, 1991, 1992, 1993 Chris McConnell
|
|
9 ;;; 1993, 1994 Ivan Vasquez
|
4
|
10 ;;; 1994, 1995, 1996 Marco Antoniotti and Rick Busdiecker
|
|
11 ;;; 1996 Marco Antoniotti and Rick Campbell
|
0
|
12 ;;;
|
|
13 ;;; Other authors' names for which this Copyright notice also holds
|
|
14 ;;; may appear later in this file.
|
|
15 ;;;
|
4
|
16 ;;; Send mail to 'ilisp-request@naggum.no' to be included in the
|
|
17 ;;; ILISP mailing list. 'ilisp@naggum.no' is the general ILISP
|
0
|
18 ;;; mailing list were bugs and improvements are discussed.
|
|
19 ;;;
|
|
20 ;;; ILISP is freely redistributable under the terms found in the file
|
|
21 ;;; COPYING.
|
|
22
|
|
23
|
|
24
|
|
25 ;;;
|
|
26 ;;; ILISP output, including a popper replacement.
|
|
27 ;;;
|
|
28
|
|
29 (defvar ilisp-output-buffer " *Output*")
|
|
30 (defvar ilisp-output-buffer-major-mode 'lisp-mode
|
|
31 "*The major mode for the ilisp typeout window.")
|
|
32 (defvar ilisp-output-min-height 2
|
|
33 "*The minimum height of the typeout window used to display ilisp output.")
|
|
34 (defvar ilisp-output-max-height 25
|
|
35 "*The maximum height of the typeout window used to display ilisp output.")
|
|
36 (defvar ilisp-display-output-function 'ilisp-display-output-default
|
|
37 "The name of a function to display all ilisp output. The function gets a
|
|
38 single argument, a string.")
|
|
39
|
|
40
|
|
41 ;; Minor mode (just to get a pretty mode line).
|
|
42 (defvar ilisp-output-mode-line nil)
|
|
43 (defvar ilisp-output-mode nil "If T, then we are in the ilisp-output minor mode.")
|
|
44 (make-variable-buffer-local 'ilisp-output-mode)
|
|
45
|
|
46 (or (assq 'ilisp-output-mode minor-mode-alist)
|
|
47 (setq minor-mode-alist
|
|
48 (cons '(ilisp-output-mode ilisp-output-mode-line) minor-mode-alist)))
|
|
49
|
|
50
|
|
51 (defun ilisp-output-buffer (&optional create-p)
|
|
52 (let ((buffer (if create-p
|
|
53 (get-buffer-create ilisp-output-buffer)
|
|
54 (get-buffer ilisp-output-buffer))))
|
|
55 (or ilisp-output-mode-line
|
|
56 (setq ilisp-output-mode-line
|
|
57 (list (format
|
|
58 " %s bury, %s scroll"
|
|
59 (ilisp-where-is 'ilisp-bury-output)
|
|
60 (ilisp-where-is 'ilisp-scroll-output)))))
|
|
61 buffer))
|
|
62
|
|
63 (defun ilisp-output-window ()
|
|
64 (let ((buffer (get-buffer ilisp-output-buffer)))
|
|
65 (if buffer
|
|
66 (get-buffer-window buffer))))
|
|
67
|
|
68
|
|
69 (defun lisp-display-output (output)
|
|
70 "Display OUTPUT in the appropriate place.
|
|
71 This calls the function given by the value of ilisp-display-output-function in
|
|
72 order to do the real work."
|
|
73 (cond ((null output))
|
|
74 (t
|
|
75 ;; Bugcheck
|
|
76 (if (not (stringp output))
|
|
77 (error "bug: not a string in lisp-display-output"))
|
|
78
|
|
79 (if (ilisp-value 'comint-errorp t)
|
|
80 (setq output (funcall (ilisp-value 'ilisp-error-filter)
|
|
81 output)))
|
|
82 (funcall ilisp-display-output-function output))))
|
|
83
|
|
84
|
|
85
|
|
86 ;;; Popper replacement
|
|
87
|
|
88
|
|
89 (defun ilisp-bury-output ()
|
|
90 "Delete the typeout window, if any"
|
|
91 (interactive)
|
|
92 (let* ((buffer (ilisp-output-buffer))
|
|
93 (window (and buffer (get-buffer-window buffer))))
|
|
94 (if buffer
|
|
95 (bury-buffer buffer))
|
|
96 (if window
|
|
97 (ilisp-delete-window window))))
|
|
98
|
|
99
|
|
100 (defun ilisp-show-output (&optional buffer)
|
|
101 "Make typeout visible, if it is not already."
|
|
102 (interactive)
|
|
103 (let ((buffer (or buffer (ilisp-output-buffer))))
|
|
104 (if buffer
|
|
105 (ilisp-display-buffer-in-typeout-window buffer))))
|
|
106
|
|
107
|
|
108 (defun ilisp-delete-window (window)
|
|
109 "Delete a window with minimal redisplay."
|
|
110 (let ((height (window-height window))
|
|
111 (lower-window (ilisp-find-lower-window window)))
|
|
112 (delete-window window)
|
|
113 (if (and lower-window
|
|
114 (not (eq lower-window window)))
|
|
115 (let ((old-window (selected-window)))
|
|
116 (save-excursion
|
|
117 (select-window lower-window)
|
|
118 (set-buffer (window-buffer))
|
|
119 (goto-char (window-start))
|
|
120 (vertical-motion (- height))
|
|
121 (set-window-start lower-window (point)))
|
|
122 (select-window old-window)))))
|
|
123
|
|
124
|
|
125 (defun ilisp-scroll-output (&optional lines)
|
|
126 "Scroll the typeout-window, if any."
|
|
127 (interactive "P")
|
|
128 (let* ((buffer (ilisp-output-buffer))
|
|
129 (window (and buffer (get-buffer-window buffer)))
|
|
130 (old-window (selected-window)))
|
|
131 (if window
|
|
132 (unwind-protect
|
|
133 (progn
|
|
134 (select-window window)
|
|
135 (set-buffer buffer)
|
|
136 (scroll-up lines))
|
|
137 (select-window old-window)))))
|
|
138
|
|
139
|
|
140 (defun ilisp-grow-output (&optional n)
|
|
141 "Grow the typeout window by ARG (default 1) lines."
|
|
142 (interactive "p")
|
|
143 (let* ((buffer (ilisp-output-buffer))
|
|
144 (window (and buffer (get-buffer-window buffer)))
|
|
145 (old-window (selected-window)))
|
|
146 (if window
|
|
147 (unwind-protect
|
|
148 (progn
|
|
149 (select-window window)
|
|
150 (enlarge-window n))
|
|
151 (if (ilisp-window-live-p old-window)
|
|
152 (select-window old-window))))))
|
|
153
|
|
154
|
|
155 (defun ilisp-trim-blank-lines ()
|
|
156 ;; Delete leading blank lines
|
|
157 (goto-char (point-min))
|
|
158 (if (looking-at "\n+")
|
|
159 (replace-match ""))
|
|
160 ;; Delete trailing blank lines
|
|
161 (goto-char (point-max))
|
|
162 (skip-chars-backward "\n")
|
|
163 (if (< (point) (point-max))
|
|
164 (delete-region (1+ (point)) (point-max))))
|
|
165
|
|
166
|
|
167 (defun ilisp-write-string-to-buffer (buffer string)
|
|
168 (save-excursion
|
|
169 (set-buffer buffer)
|
|
170 ;; Maybe an option to keep the old output?
|
|
171 (erase-buffer)
|
|
172 ;; New: select mode for the output buffer.
|
|
173 (if (not (eq major-mode ilisp-output-buffer-major-mode))
|
|
174 (funcall ilisp-output-buffer-major-mode))
|
|
175 (setq ilisp-output-mode t)
|
|
176 (princ string buffer)
|
|
177 (ilisp-trim-blank-lines)
|
|
178 (goto-char (point-min))))
|
|
179
|
|
180
|
|
181 (defun ilisp-desired-height (buffer-or-window)
|
|
182 (let ((height
|
|
183 (cond ((bufferp buffer-or-window)
|
|
184 (ilisp-needed-buffer-height buffer-or-window))
|
|
185 ((windowp buffer-or-window)
|
|
186 (ilisp-needed-window-height buffer-or-window)))))
|
|
187 (max window-min-height
|
|
188 (min ilisp-output-max-height
|
|
189 (max ilisp-output-min-height
|
|
190 height)))))
|
|
191
|
|
192
|
|
193 ;; A first guess at the height needed to display this buffer.
|
|
194 (defun ilisp-needed-buffer-height (buffer)
|
|
195 (save-excursion
|
|
196 (set-buffer buffer)
|
|
197 (1+ (count-lines (point-min) (point-max)))))
|
|
198
|
|
199
|
|
200 ;; The height this window must be to display its entire buffer.
|
|
201 (defun ilisp-needed-window-height (window)
|
|
202 (save-window-excursion
|
|
203 (select-window window)
|
|
204 (save-excursion
|
|
205 (set-buffer (window-buffer))
|
|
206 (+ 2 (save-excursion
|
|
207 (goto-char (point-min))
|
|
208 ;; Any upper bound on the height of an emacs window will
|
|
209 ;; do here. How about 1000.
|
|
210 (vertical-motion 1000))))))
|
|
211
|
|
212
|
|
213 (defun ilisp-shrink-wrap-window (window)
|
|
214 (let ((previously-selected-window (selected-window))
|
|
215 (buffer (window-buffer window)))
|
|
216
|
|
217 (select-window window)
|
|
218 (let* ((current-height (window-height window))
|
|
219 (desired-height (ilisp-desired-height window))
|
|
220 (delta (- desired-height current-height)))
|
|
221 (enlarge-window delta)
|
|
222 (set-buffer buffer)
|
|
223 (goto-char (point-min))
|
|
224
|
|
225 ;; Now repair damage to the window below us, if it still exists.
|
|
226 (let ((lower-window (ilisp-find-lower-window window)))
|
|
227 (if lower-window
|
|
228 (progn
|
|
229 (select-window lower-window)
|
|
230 (let ((old-point (point)))
|
|
231 (goto-char (window-start))
|
|
232 (vertical-motion delta)
|
|
233 (set-window-start lower-window (point))
|
|
234 (goto-char old-point)
|
|
235 (if (not (pos-visible-in-window-p old-point))
|
|
236 (recenter 0))))))
|
|
237 ;; If there was no lower window, then we ought to preserve
|
|
238 ;; the start of the window above us, if any.
|
|
239
|
|
240 (if (ilisp-window-live-p previously-selected-window)
|
|
241 (select-window previously-selected-window)))))
|
|
242
|
|
243
|
|
244
|
|
245 (defun ilisp-window-live-p (window)
|
|
246 (let* ((initial-window (selected-window))
|
|
247 (win initial-window)
|
|
248 (found nil))
|
|
249 (while win
|
|
250 (cond ((eq window win)
|
|
251 (setq found t
|
|
252 win nil))
|
|
253 (t
|
|
254 (setq win (next-window win 'no))
|
|
255 (if (eq win initial-window)
|
|
256 (setq win nil)))))
|
|
257 found))
|
|
258
|
|
259 ;; XEmacs change -- window-edges is gone in 19.12+ so use
|
|
260 ;; next-vertical-window instead.
|
|
261 (defun ilisp-find-lower-window (window)
|
|
262 "Find the window directly below us, if any. This is probably the
|
|
263 window from which enlarge-window would steal lines."
|
4
|
264 (if (or (not (string-match "XEmacs" emacs-version))
|
6
|
265 (and (= emacs-major-version 19)
|
|
266 (< emacs-minor-version 12)))
|
0
|
267 (let* ((bottom (nth 3 (window-edges window)))
|
|
268 (window* nil)
|
|
269 (win window))
|
|
270 (while (not (eq (setq win (next-window win 'no))
|
|
271 window))
|
|
272 (if (and (= (nth 1 (window-edges win))
|
|
273 bottom)
|
|
274 (null window*))
|
|
275 (setq window* win)))
|
|
276 window*)
|
|
277 (next-vertical-window window)))
|
|
278
|
|
279 ;; XEmacs change -- There is now a primitive to do this.
|
|
280 (defun ilisp-find-top-left-most-window ()
|
|
281 "Return the leftmost topmost window on the current screen."
|
4
|
282 (if (or (not (string-match "XEmacs" emacs-version))
|
6
|
283 (and (= emacs-major-version 19)
|
|
284 (< emacs-minor-version 12)))
|
0
|
285 (let* ((window* (selected-window))
|
|
286 (edges* (window-edges window*))
|
|
287 (win nil)
|
|
288 (edges nil)
|
|
289 (start-window window*))
|
|
290 (while (not (eq (setq win (next-window win 'no))
|
|
291 start-window))
|
|
292 (setq edges (window-edges win))
|
|
293 (if (or (< (car (cdr edges)) (car (cdr edges*))) ; top
|
|
294 (and (= (car (cdr edges)) (car (cdr edges*)))
|
|
295 (< (car edges) (car edges*)))) ; left
|
|
296 (setq window* win
|
|
297 edges* edges)))
|
|
298 window*)
|
|
299 (frame-highest-window (selected-frame) 0)))
|
|
300
|
|
301
|
|
302 ;; This causes the typeout window to be created by splitting or using the
|
|
303 ;; top-left-most window on the current screen. That is different behavior
|
|
304 ;; from the popper, which always split the current window.
|
|
305 (defun ilisp-window-to-use-for-typeout ()
|
|
306 (ilisp-find-top-left-most-window))
|
|
307
|
|
308
|
|
309 (defun ilisp-display-buffer-in-typeout-window (buffer)
|
|
310 "Display buffer in a window at the top of the screen."
|
|
311 (let ((window (get-buffer-window buffer)))
|
|
312
|
|
313 ;; If buffer already has a window, keep it.
|
|
314 (if (null window)
|
|
315 ;; Otherwise, find a window to split.
|
|
316 (let* ((top-window (ilisp-window-to-use-for-typeout))
|
|
317 (new-window nil)
|
|
318 (previously-selected-window (selected-window))
|
|
319 (desired-height (ilisp-desired-height buffer)))
|
|
320
|
|
321 ;; The new window is always the lower one.
|
|
322 (select-window top-window)
|
|
323
|
|
324 ;; Always minimize redisplay (except in emacs 18).
|
|
325 (let ((split-window-keep-point nil))
|
|
326 ;; If the top window is not big enough to split, commandeer it
|
|
327 ;; entirely.
|
|
328 (cond ((> desired-height (- (window-height) window-min-height))
|
|
329 (setq new-window top-window))
|
|
330 (t
|
|
331 (setq new-window (split-window-vertically desired-height)))))
|
|
332
|
|
333 (set-window-buffer top-window buffer)
|
|
334 ;; The height is already correct, unless there was line wrapping.
|
|
335 ;; Account for that here.
|
|
336 (ilisp-shrink-wrap-window top-window)
|
|
337
|
|
338 ;; Restore selected window.
|
|
339 (if (eq previously-selected-window top-window)
|
|
340 (select-window new-window)
|
|
341 (select-window previously-selected-window)))
|
|
342
|
|
343 ;; Simply shrink-wrap an existing window.
|
|
344 (ilisp-shrink-wrap-window window))))
|
|
345
|
|
346
|
|
347
|
|
348
|
|
349
|
|
350
|
|
351
|
|
352
|
|
353 ;;; Various functions to which to bind ilisp-display-output-function.
|
|
354
|
|
355 ;; This function does what ilisp used to do, except that we use the
|
|
356 ;; new "popper".
|
|
357
|
|
358 (defun ilisp-display-output-default (output)
|
|
359 "Dispatch on the value of lisp-no-popper:
|
|
360 lisp-no-popper = nil: display output in a typeout window.
|
|
361 lisp-no-popper = t: display output in the ilisp buffer
|
|
362 otherwise: display one-line output in the echo area, multiline output in the ilisp buffer."
|
|
363 (cond ((null lisp-no-popper)
|
|
364 (ilisp-display-output-in-typeout-window output))
|
|
365 ((eq lisp-no-popper t)
|
|
366 (ilisp-display-output-in-lisp-listener output))
|
|
367 (t
|
|
368 (ilisp-display-output-adaptively output))))
|
|
369
|
|
370
|
|
371 ;; This is the display function I like to use.
|
|
372
|
|
373 ;; Another trick which might be useful is to dispatch on the value
|
|
374 ;; this-command here, to make output from different ilisp commands
|
|
375 ;; go to different places.
|
|
376
|
|
377 (defun ilisp-display-output-adaptively (output)
|
|
378 "Display one-liners in the echo area, others in the typeout window"
|
|
379 (cond ((or (string-match "\n" output)
|
|
380 (> (length output) (window-width (minibuffer-window))))
|
|
381 (message "See above.")
|
|
382 (ilisp-display-output-in-typeout-window output))
|
|
383 (t
|
|
384 (ilisp-display-output-in-echo-area output))))
|
|
385
|
|
386
|
|
387 (defun ilisp-display-output-in-typeout-window (output)
|
|
388 "Display output in a shrink-wrapped window at the top of the screen."
|
|
389 (let ((buffer (ilisp-output-buffer t)))
|
|
390 (ilisp-write-string-to-buffer buffer output)
|
|
391 (ilisp-display-buffer-in-typeout-window buffer)))
|
|
392
|
|
393
|
|
394 (defun ilisp-display-output-in-echo-area (output)
|
|
395 "Display output as a message in the echo area."
|
|
396 ;; First clear any existing typeout so as to not confuse the user.
|
|
397 (or (eq (selected-window) (ilisp-output-window))
|
|
398 (ilisp-bury-output))
|
4
|
399
|
|
400 ;; v5.7: Patch suggested by hunter@work.nlm.nih.gov (Larry Hunter)
|
0
|
401 ;; If output contains '%', 'message' loses.
|
|
402 ;; (message (ilisp-quote-%s output))
|
|
403 ;; An alternative here could be '(princ output)', as suggested by
|
|
404 ;; Christopher Hoover <ch@lks.csi.com>
|
4
|
405 ;; (princ output)
|
|
406
|
|
407 ;; v5.7b: Patch suggested by fujieda@jaist.ac.jp (Kazuhiro Fujieda)
|
|
408 ;; Best one for FSF Emacs 19.2[89].
|
|
409 (message "%s" output)
|
0
|
410 )
|
|
411
|
|
412
|
|
413 ;;; ilisp-quote-%s --
|
|
414 ;;; Patch suggested by hunter@work.nlm.nih.gov (Larry Hunter)
|
|
415
|
|
416 (defun ilisp-quote-%s (string)
|
|
417 "Quote all the occurences of ?% in STRING in an ELisp fashion."
|
|
418 (mapconcat '(lambda (char)
|
|
419 (if (char-equal char ?%)
|
|
420 "%%"
|
|
421 (char-to-string char)))
|
|
422 string ""))
|
|
423
|
|
424
|
|
425 (defun ilisp-display-output-in-temp-buffer (output)
|
|
426 (with-output-to-temp-buffer ilisp-output-buffer
|
|
427 (princ output)))
|
|
428
|
|
429
|
|
430 (defun ilisp-display-output-in-lisp-listener (output)
|
|
431 "Display output in the ilisp buffer"
|
|
432 (let ((buffer (current-buffer))
|
|
433 (window (selected-window)))
|
|
434 (unwind-protect
|
|
435 (progn
|
|
436 (lisp-pop-to-buffer (ilisp-buffer))
|
|
437 (if (not (eq (current-buffer) buffer))
|
|
438 (setq ilisp-last-buffer buffer))
|
|
439 (comint-insert
|
|
440 (concat
|
|
441 (if ilisp-last-message
|
|
442 (concat ";;; " ilisp-last-message "\n"))
|
|
443 (comint-remove-whitespace output)
|
|
444 "\n"
|
|
445 ilisp-last-prompt))
|
|
446 (setq ilisp-last-message nil))
|
|
447 (if (window-point window)
|
|
448 (progn (select-window window)
|
|
449 (set-buffer buffer))))))
|
|
450
|
|
451
|
|
452
|
|
453 ;;; Changed according to suggestions by Robert P. Goldman
|
|
454 (defun lisp-pop-to-buffer (buffer)
|
|
455 "Like pop-to-buffer, but select a screen that buffer was shown in."
|
|
456 (let ((ilisp-window (if ilisp-epoch-running
|
|
457 (epoch::get-buffer-window buffer)
|
|
458 (get-buffer-window buffer))))
|
|
459 (if ilisp-window
|
|
460 (select-window ilisp-window)
|
|
461 ;; It is not currently displayed, so find some place to display
|
|
462 ;; it.
|
|
463 (progn
|
|
464 (cond (ilisp-epoch-running
|
|
465 ;; Select a screen that the buffer has been displayed in before
|
|
466 ;; or the current screen otherwise.
|
|
467 (epoch::select-screen
|
|
468 ;; allowed-screens in epoch 3.2, was called screens before that
|
|
469 (or (car (save-excursion
|
|
470 (set-buffer buffer)
|
|
471 (symbol-value 'allowed-screens)))
|
|
472 (epoch::current-screen))))
|
|
473
|
|
474 ;; Next clauses patterned after a suggestion by R. P. Goldman.
|
|
475 ((eq +ilisp-emacs-version-id+ 'fsf-19)
|
|
476 (let* ((window (get-buffer-window buffer t))
|
|
477 (frame (if window (window-frame window))))
|
|
478 (if (eq 'x (framep frame))
|
|
479 (progn
|
|
480 (raise-frame frame)
|
|
481 (select-frame frame)))))
|
|
482 (t nil)) ; fsf-18, but also lucid and
|
|
483 ; xemacs.
|
|
484 ; I do not know how to make
|
|
485 ; them work
|
|
486 ; Marco Antoniotti, Jan 4th 1995
|
|
487 (ilisp-bury-output)
|
|
488 (pop-to-buffer buffer))))
|
|
489 (set-buffer buffer))
|
|
490
|
|
491 ;(defun lisp-pop-to-buffer (buffer)
|
|
492 ; "Like pop-to-buffer, but select a screen that buffer was shown in.
|
|
493 ; Also, first bury any typeout-window."
|
|
494 ; (let ((ilisp-window (if ilisp-epoch-running
|
|
495 ; (epoch::get-buffer-window buffer)
|
|
496 ; (get-buffer-window buffer))))
|
|
497 ; (if ilisp-window
|
|
498 ; (select-window ilisp-window)
|
|
499 ; ;; It is not currently displayed, so find some place to display it.
|
|
500 ; (if ilisp-epoch-running
|
|
501 ; ;; Select a screen that the buffer has been displayed in before
|
|
502 ; ;; or the current screen otherwise.
|
|
503 ; (epoch::select-screen
|
|
504 ; ;; allowed-screens in epoch 3.2, was called screens before that
|
|
505 ; (or (car (save-excursion
|
|
506 ; (set-buffer buffer)
|
|
507 ; (symbol-value 'allowed-screens)))
|
|
508 ; (epoch::current-screen))))
|
|
509 ; ;; Do not pop to the output buffer.
|
|
510 ; (ilisp-bury-output)
|
|
511 ; (pop-to-buffer buffer)))
|
|
512 ; (set-buffer buffer))
|
|
513
|
|
514
|
|
515 ;;;
|
|
516 (defun switch-to-lisp (eob-p &optional ilisp-only)
|
|
517 "If in an ILISP buffer, switch to the buffer that last switched to
|
|
518 an ILISP otherwise, switch to the current ILISP buffer. With
|
|
519 argument, positions cursor at end of buffer. If you don't want to
|
|
520 split windows, set pop-up-windows to NIL."
|
|
521 (interactive "P")
|
|
522 (if (and (not ilisp-only) ilisp-last-buffer
|
|
523 (memq major-mode ilisp-modes))
|
|
524 (lisp-pop-to-buffer ilisp-last-buffer)
|
|
525 (if (not (memq major-mode ilisp-modes))
|
|
526 (setq ilisp-last-buffer (current-buffer)))
|
|
527 (lisp-pop-to-buffer (ilisp-buffer))
|
|
528 (cond (eob-p (goto-char (point-max))))))
|