* sun-mouse.el (suspend-emacstool): Run suspend-hook, not
[bpt/emacs.git] / lisp / terminal.el
1 ;;; terminal.el --- terminal emulator for GNU Emacs.
2
3 ;; Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
4
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: comm, terminals
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Code:
26
27 ;;>>TODO
28 ;;>> terminfo?
29 ;;>> ** Nothing can be done about emacs' meta-lossage **
30 ;;>> (without redoing keymaps `sanely' -- ask Mly for details)
31
32 ;;>> One probably wants to do setenv MORE -c when running with
33 ;;>> more-processing enabled.
34
35 (require 'ehelp)
36
37 (defvar terminal-escape-char ?\C-^
38 "*All characters except for this are passed verbatim through the
39 terminal-emulator. This character acts as a prefix for commands
40 to the emulator program itself. Type this character twice to send
41 it through the emulator. Type ? after typing it for a list of
42 possible commands.
43 This variable is local to each terminal-emulator buffer.")
44
45 (defvar terminal-scrolling t ;;>> Setting this to T sort-of defeats my whole aim in writing this package...
46 "*If non-nil, the terminal-emulator will losingly `scroll' when output occurs
47 past the bottom of the screen. If nil, output will win and `wrap' to the top
48 of the screen.
49 This variable is local to each terminal-emulator buffer.")
50
51 (defvar terminal-more-processing t
52 "*If non-nil, do more-processing.
53 This variable is local to each terminal-emulator buffer.")
54
55 ;; If you are the sort of loser who uses scrolling without more breaks
56 ;; and expects to actually see anything, you should probably set this to
57 ;; around 400
58 (defvar terminal-redisplay-interval 5000
59 "*Maximum number of characters which will be processed by the
60 terminal-emulator before a screen redisplay is forced.
61 Set this to a large value for greater throughput,
62 set it smaller for more frequent updates but overall slower
63 performance.")
64
65 (defvar terminal-more-break-insertion
66 "*** More break -- Press space to continue ***")
67
68 (defvar terminal-map nil)
69 (if terminal-map
70 nil
71 (let ((map (make-keymap)))
72 (fillarray (car (cdr map)) 'te-pass-through)
73 ;(define-key map "\C-l"
74 ; '(lambda () (interactive) (te-pass-through) (redraw-display)))
75 (setq terminal-map map)))
76
77 (defvar terminal-escape-map nil)
78 (if terminal-escape-map
79 nil
80 (let ((map (make-keymap)))
81 ;(fillarray map 'te-escape-extended-command-unread)
82 (fillarray (car (cdr map)) 'undefined)
83 (let ((s "0"))
84 (while (<= (aref s 0) ?9)
85 (define-key map s 'digit-argument)
86 (aset s 0 (1+ (aref s 0)))))
87 (define-key map "b" 'switch-to-buffer)
88 (define-key map "o" 'other-window)
89 (define-key map "e" 'te-set-escape-char)
90 (define-key map "\C-l" 'redraw-display)
91 (define-key map "\C-o" 'te-flush-pending-output)
92 (define-key map "m" 'te-toggle-more-processing)
93 (define-key map "x" 'te-escape-extended-command)
94 ;;>> What use is this? Why is it in the default terminal-emulator map?
95 (define-key map "w" 'te-edit)
96 (define-key map "?" 'te-escape-help)
97 (define-key map (char-to-string help-char) 'te-escape-help)
98 (setq terminal-escape-map map)))
99
100 (defvar te-escape-command-alist nil)
101 (if te-escape-command-alist
102 nil
103 (setq te-escape-command-alist
104 '(("Set Escape Character" . te-set-escape-char)
105 ;;>> What use is this? Why is it in the default terminal-emulator map?
106 ("Edit" . te-edit)
107 ("Refresh" . redraw-display)
108 ("Record Output" . te-set-output-log)
109 ("Photo" . te-set-output-log)
110 ("Tofu" . te-tofu) ;; confuse the uninitiated
111 ("Stuff Input" . te-stuff-string)
112 ("Flush Pending Output" . te-flush-pending-output)
113 ("Enable More Processing" . te-enable-more-processing)
114 ("Disable More Processing" . te-disable-more-processing)
115 ("Scroll at end of page" . te-do-scrolling)
116 ("Wrap at end of page" . te-do-wrapping)
117 ("Switch To Buffer" . switch-to-buffer)
118 ("Other Window" . other-window)
119 ("Kill Buffer" . kill-buffer)
120 ("Help" . te-escape-help)
121 ("Set Redisplay Interval" . te-set-redisplay-interval)
122 )))
123
124 (defvar terminal-more-break-map nil)
125 (if terminal-more-break-map
126 nil
127 (let ((map (make-keymap)))
128 (fillarray (car (cdr map)) 'te-more-break-unread)
129 (define-key map (char-to-string help-char) 'te-more-break-help)
130 (define-key map " " 'te-more-break-resume)
131 (define-key map "\C-l" 'redraw-display)
132 (define-key map "\C-o" 'te-more-break-flush-pending-output)
133 ;;>>> this isn't right
134 ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
135 (define-key map "\r" 'te-more-break-advance-one-line)
136
137 (setq terminal-more-break-map map)))
138
139 \f
140 ;;; Pacify the byte compiler
141 (defvar te-process nil)
142 (defvar te-log-buffer nil)
143 (defvar te-height nil)
144 (defvar te-width nil)
145 (defvar te-more-count nil)
146 (defvar te-redisplay-count nil)
147 (defvar te-pending-output nil)
148 (defvar te-saved-point)
149 (defvar te-more-old-point nil)
150 (defvar te-more-old-local-map nil)
151 (defvar te-more-old-filter nil)
152 (defvar te-more-old-mode-line-format nil)
153 (defvar te-pending-output-info nil)
154
155 \f
156 ;;;; escape map
157
158 (defun te-escape ()
159 (interactive)
160 (let (s
161 (local (current-local-map))
162 (global (current-global-map)))
163 (unwind-protect
164 (progn
165 (use-global-map terminal-escape-map)
166 (use-local-map terminal-escape-map)
167 (setq s (read-key-sequence
168 (if prefix-arg
169 (format "Emacs Terminal escape> %d "
170 (prefix-numeric-value prefix-arg))
171 "Emacs Terminal escape> "))))
172 (use-global-map global)
173 (use-local-map local))
174 (message "")
175 (cond ((string= s (make-string 1 terminal-escape-char))
176 (setq last-command-char terminal-escape-char)
177 (let ((terminal-escape-char -259))
178 (te-pass-through)))
179 ((setq s (lookup-key terminal-escape-map s))
180 (call-interactively s)))))
181
182 (defun te-escape-help ()
183 "Provide help on commands available after terminal-escape-char is typed."
184 (interactive)
185 (message "Terminal emulator escape help...")
186 (let ((char (single-key-description terminal-escape-char)))
187 (with-electric-help
188 (function (lambda ()
189 (princ (format "Terminal-emulator escape, invoked by \"%s\"
190 Type \"%s\" twice to send a single \"%s\" through.
191
192 Other chars following \"%s\" are interpreted as follows:\n"
193 char char char char))
194
195 (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
196 (princ (format "\nSubcommands of \"%s\" (%s)\n"
197 (where-is-internal 'te-escape-extended-command
198 terminal-escape-map nil t)
199 'te-escape-extended-command))
200 (let ((l (if (fboundp 'sortcar)
201 (sortcar (copy-sequence te-escape-command-alist)
202 'string<)
203 (sort (copy-sequence te-escape-command-alist)
204 (function (lambda (a b)
205 (string< (car a) (car b))))))))
206 (while l
207 (let ((doc (or (documentation (cdr (car l)))
208 "Not documented")))
209 (if (string-match "\n" doc)
210 ;; just use first line of documentation
211 (setq doc (substring doc 0 (match-beginning 0))))
212 (princ " \"")
213 (princ (car (car l)))
214 (princ "\":\n ")
215 (princ doc)
216 (write-char ?\n))
217 (setq l (cdr l))))
218 nil)))))
219
220
221
222 (defun te-escape-extended-command ()
223 (interactive)
224 (let ((c (let ((completion-ignore-case t))
225 (completing-read "terminal command: "
226 te-escape-command-alist
227 nil t))))
228 (if c
229 (catch 'foo
230 (setq c (downcase c))
231 (let ((l te-escape-command-alist))
232 (while l
233 (if (string= c (downcase (car (car l))))
234 (throw 'foo (call-interactively (cdr (car l))))
235 (setq l (cdr l)))))))))
236
237 ;; not used.
238 (defun te-escape-extended-command-unread ()
239 (interactive)
240 (setq unread-command-events (listify-key-sequence (this-command-keys)))
241 (te-escape-extended-command))
242
243 (defun te-set-escape-char (c)
244 "Change the terminal-emulator escape character."
245 (interactive "cSet escape character to: ")
246 (let ((o terminal-escape-char))
247 (message (if (= o c)
248 "\"%s\" is the escape char"
249 "\"%s\" is now the escape; \"%s\" passes through")
250 (single-key-description c)
251 (single-key-description o))
252 (setq terminal-escape-char c)))
253
254
255 (defun te-stuff-string (string)
256 "Read a string to send to through the terminal emulator
257 as though that string had been typed on the keyboard.
258
259 Very poor man's file transfer protocol."
260 (interactive "sStuff string: ")
261 (process-send-string te-process string))
262
263 (defun te-set-output-log (name)
264 "Record output from the terminal emulator in a buffer."
265 (interactive (list (if te-log-buffer
266 nil
267 (read-buffer "Record output in buffer: "
268 (format "%s output-log"
269 (buffer-name (current-buffer)))
270 nil))))
271 (if (or (null name) (equal name ""))
272 (progn (setq te-log-buffer nil)
273 (message "Output logging off."))
274 (if (get-buffer name)
275 nil
276 (save-excursion
277 (set-buffer (get-buffer-create name))
278 (fundamental-mode)
279 (buffer-disable-undo (current-buffer))
280 (erase-buffer)))
281 (setq te-log-buffer (get-buffer name))
282 (message "Recording terminal emulator output into buffer \"%s\""
283 (buffer-name te-log-buffer))))
284
285 (defun te-tofu ()
286 "Discontinue output log."
287 (interactive)
288 (te-set-output-log nil))
289
290
291 (defun te-toggle (sym arg)
292 (set sym (cond ((not (numberp arg)) arg)
293 ((= arg 1) (not (symbol-value sym)))
294 ((< arg 0) nil)
295 (t t))))
296
297 (defun te-toggle-more-processing (arg)
298 (interactive "p")
299 (message (if (te-toggle 'terminal-more-processing arg)
300 "More processing on" "More processing off"))
301 (if terminal-more-processing (setq te-more-count -1)))
302
303 (defun te-toggle-scrolling (arg)
304 (interactive "p")
305 (message (if (te-toggle 'terminal-scrolling arg)
306 "Scroll at end of page" "Wrap at end of page")))
307
308 (defun te-enable-more-processing ()
309 "Enable ** MORE ** processing"
310 (interactive)
311 (te-toggle-more-processing t))
312
313 (defun te-disable-more-processing ()
314 "Disable ** MORE ** processing"
315 (interactive)
316 (te-toggle-more-processing nil))
317
318 (defun te-do-scrolling ()
319 "Scroll at end of page (yuck)"
320 (interactive)
321 (te-toggle-scrolling t))
322
323 (defun te-do-wrapping ()
324 "Wrap to top of window at end of page"
325 (interactive)
326 (te-toggle-scrolling nil))
327
328
329 (defun te-set-redisplay-interval (arg)
330 "Set the maximum interval (in output characters) between screen updates.
331 Set this number to large value for greater throughput,
332 set it smaller for more frequent updates (but overall slower performance."
333 (interactive "NMax number of output chars between redisplay updates: ")
334 (setq arg (max arg 1))
335 (setq terminal-redisplay-interval arg
336 te-redisplay-count 0))
337 \f
338 ;;;; more map
339
340 ;; every command -must- call te-more-break-unwind
341 ;; or grave lossage will result
342
343 (put 'te-more-break-unread 'suppress-keymap t)
344 (defun te-more-break-unread ()
345 (interactive)
346 (if (= last-input-char terminal-escape-char)
347 (call-interactively 'te-escape)
348 (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
349 (single-key-description last-input-char)
350 (te-pending-output-length))
351 (setq te-more-count 259259)
352 (te-more-break-unwind)
353 (let ((terminal-more-processing nil))
354 (te-pass-through))))
355
356 (defun te-more-break-resume ()
357 "Proceed past the **MORE** break,
358 allowing the next page of output to appear"
359 (interactive)
360 (message "Continuing from more break")
361 (te-more-break-unwind))
362
363 (defun te-more-break-help ()
364 "Provide help on commands available in a terminal-emulator **MORE** break"
365 (interactive)
366 (message "Terminal-emulator more break help...")
367 (sit-for 0)
368 (with-electric-help
369 (function (lambda ()
370 (princ "Terminal-emulator more break.\n\n")
371 (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
372 (where-is-internal 'te-more-break-resume
373 terminal-more-break-map nil t)
374 (documentation 'te-more-break-resume)))
375 (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
376 (princ "Any other key is passed through to the program
377 running under the terminal emulator and disables more processing until
378 all pending output has been dealt with.")
379 nil))))
380
381
382 (defun te-more-break-advance-one-line ()
383 "Allow one more line of text to be output before doing another more break."
384 (interactive)
385 (setq te-more-count 1)
386 (te-more-break-unwind))
387
388 (defun te-more-break-flush-pending-output ()
389 "Discard any output which has been received by the terminal emulator but
390 not yet proceesed and then proceed from the more break."
391 (interactive)
392 (te-more-break-unwind)
393 (te-flush-pending-output))
394
395 (defun te-flush-pending-output ()
396 "Discard any as-yet-unprocessed output which has been received by
397 the terminal emulator."
398 (interactive)
399 ;; this could conceivably be confusing in the presence of
400 ;; escape-sequences spanning process-output chunks
401 (if (null (cdr te-pending-output))
402 (message "(There is no output pending)")
403 (let ((length (te-pending-output-length)))
404 (message "Flushing %d chars of pending output" length)
405 (setq te-pending-output
406 (list 0 (format "\n*** %d chars of pending output flushed ***\n"
407 length)))
408 (te-update-pending-output-display)
409 (te-process-output nil)
410 (sit-for 0))))
411
412 \f
413 (defun te-pass-through ()
414 "Character is passed to the program running under the terminal emulator.
415 One characters is treated specially:
416 the terminal escape character (normally C-^)
417 lets you type a terminal emulator command."
418 (interactive)
419 (cond ((= last-input-char terminal-escape-char)
420 (call-interactively 'te-escape))
421 (t
422 (and terminal-more-processing (null (cdr te-pending-output))
423 (te-set-more-count nil))
424 (send-string te-process (make-string 1 last-input-char))
425 (te-process-output t))))
426
427
428 (defun te-set-window-start ()
429 (let* ((w (get-buffer-window (current-buffer)))
430 (h (if w (window-height w))))
431 (cond ((not w)) ; buffer not displayed
432 ((>= h (/ (- (point) (point-min)) (1+ te-width)))
433 ;; this is the normal case
434 (set-window-start w (point-min)))
435 ;; this happens if some vandal shrinks our window.
436 ((>= h (/ (- (point-max) (point)) (1+ te-width)))
437 (set-window-start w (- (point-max) (* h (1+ te-width)) -1)))
438 ;; I give up.
439 (t nil))))
440
441 (defun te-pending-output-length ()
442 (let ((length (car te-pending-output))
443 (tem (cdr te-pending-output)))
444 (while tem
445 (setq length (+ length (length (car tem))) tem (cdr tem)))
446 length))
447 \f
448 ;;>> What use is this terminal-edit stuff anyway?
449 ;;>> If nothing else, it was written by somebody who didn't
450 ;;>> competently understand the terminal-emulator...
451
452 (defvar terminal-edit-map nil)
453 (if terminal-edit-map
454 nil
455 (setq terminal-edit-map (make-sparse-keymap))
456 (define-key terminal-edit-map "\C-c\C-c" 'terminal-cease-edit))
457
458 ;; Terminal Edit mode is suitable only for specially formatted data.
459 (put 'terminal-edit-mode 'mode-class 'special)
460
461 (defun terminal-edit-mode ()
462 "Major mode for editing the contents of a terminal-emulator buffer.
463 The editing commands are the same as in Fundamental mode,
464 together with a command \\<terminal-edit-mode-map>to return to terminal emulation: \\[terminal-cease-edit]."
465 (use-local-map terminal-edit-map)
466 (setq major-mode 'terminal-edit-mode)
467 (setq mode-name "Terminal Edit")
468 (setq mode-line-modified (default-value 'mode-line-modified))
469 (setq mode-line-process nil)
470 (run-hooks 'terminal-edit-mode-hook))
471
472 (defun te-edit ()
473 "Start editing the terminal emulator buffer with ordinary Emacs commands."
474 (interactive)
475 (terminal-edit-mode)
476 (set-buffer-modified-p (buffer-modified-p))
477 ;; Make mode line update.
478 (if (eq (key-binding "\C-c\C-c") 'terminal-cease-edit)
479 (message "Editing: Type C-c C-c to return to Terminal")
480 (message (substitute-command-keys
481 "Editing: Type \\[terminal-cease-edit] to return to Terminal"))))
482
483 (defun terminal-cease-edit ()
484 "Finish editing message; switch back to Terminal proper."
485 (interactive)
486
487 ;;>> emulator will blow out if buffer isn't exactly te-width x te-height
488 (let ((buffer-read-only nil))
489 (widen)
490 (let ((opoint (point-marker))
491 (width te-width)
492 (h (1- te-height)))
493 (goto-char (point-min))
494 (while (>= h 0)
495 (let ((p (point)))
496 (cond ((search-forward "\n" (+ p width) 'move)
497 (forward-char -1)
498 (insert-char ?\ (- width (- (point) p)))
499 (forward-char 1))
500 ((eobp)
501 (insert-char ?\ (- width (- (point) p))))
502 ((= (following-char) ?\n)
503 (forward-char 1))
504 (t
505 (setq p (point))
506 (if (search-forward "\n" nil t)
507 (delete-region p (1- (point)))
508 (delete-region p (point-max))))))
509 (if (= h 0)
510 (if (not (eobp)) (delete-region (point) (point-max)))
511 (if (eobp) (insert ?\n)))
512 (setq h (1- h)))
513 (goto-char opoint)
514 (set-marker opoint nil nil)
515 (setq te-saved-point (point))
516 (setq te-redisplay-count 0)
517 (setq te-more-count -1)))
518
519 (setq mode-line-modified (default-value 'mode-line-modified))
520 (setq major-mode 'terminal-mode)
521 (setq mode-name "terminal")
522 (setq mode-line-process '(": %s")))
523 \f
524 ;;;; more break hair
525
526 (defun te-more-break ()
527 (te-set-more-count t)
528 (make-local-variable 'te-more-old-point)
529 (setq te-more-old-point (point))
530 (make-local-variable 'te-more-old-local-map)
531 (setq te-more-old-local-map (current-local-map))
532 (use-local-map terminal-more-break-map)
533 (make-local-variable 'te-more-old-filter)
534 (setq te-more-old-filter (process-filter te-process))
535 (make-local-variable 'te-more-old-mode-line-format)
536 (setq te-more-old-mode-line-format mode-line-format
537 mode-line-format (list "-- **MORE** "
538 mode-line-buffer-identification
539 "%-"))
540 (set-process-filter te-process
541 (function (lambda (process string)
542 (save-excursion
543 (set-buffer (process-buffer process))
544 (setq te-pending-output (nconc te-pending-output
545 (list string))))
546 (te-update-pending-output-display))))
547 (te-update-pending-output-display)
548 (if (eq (window-buffer (selected-window)) (current-buffer))
549 (message "More break "))
550 (or (eobp)
551 (null terminal-more-break-insertion)
552 (save-excursion
553 (forward-char 1)
554 (delete-region (point) (+ (point) te-width))
555 (insert terminal-more-break-insertion)))
556 (run-hooks 'terminal-more-break-hook)
557 (sit-for 0) ;get display to update
558 (throw 'te-process-output t))
559
560 (defun te-more-break-unwind ()
561 (use-local-map te-more-old-local-map)
562 (set-process-filter te-process te-more-old-filter)
563 (goto-char te-more-old-point)
564 (setq mode-line-format te-more-old-mode-line-format)
565 (set-buffer-modified-p (buffer-modified-p))
566 (let ((buffer-read-only nil))
567 (cond ((eobp))
568 (terminal-more-break-insertion
569 (forward-char 1)
570 (delete-region (point)
571 (+ (point) (length terminal-more-break-insertion)))
572 (insert-char ?\ te-width)
573 (goto-char te-more-old-point)))
574 (setq te-more-old-point nil)
575 (let ((te-more-count 259259))
576 (te-newline)))
577 ;(sit-for 0)
578 (te-process-output t))
579
580 (defun te-set-more-count (newline)
581 (let ((line (/ (- (point) (point-min)) (1+ te-width))))
582 (if newline (setq line (1+ line)))
583 (cond ((= line te-height)
584 (setq te-more-count te-height))
585 ;>>>> something is strange. Investigate this!
586 ((= line (1- te-height))
587 (setq te-more-count te-height))
588 ((or (< line (/ te-height 2))
589 (> (- te-height line) 10))
590 ;; break at end of this page
591 (setq te-more-count (- te-height line)))
592 (t
593 ;; migrate back towards top (ie bottom) of screen.
594 (setq te-more-count (- te-height
595 (if (> te-height 10) 2 1)))))))
596
597 \f
598 ;;;; More or less straight-forward terminal escapes
599
600 ;; ^j, meaning `newline' to non-display programs.
601 ;; (Who would think of ever writing a system which doesn't understand
602 ;; display terminals natively? Un*x: The Operating System of the Future.)
603 (defun te-newline ()
604 "Move down a line, optionally do more processing, perhaps wrap/scroll,
605 move to start of new line, clear to end of line."
606 (end-of-line)
607 (cond ((not terminal-more-processing))
608 ((< (setq te-more-count (1- te-more-count)) 0)
609 (te-set-more-count t))
610 ((eql te-more-count 0)
611 ;; this doesn't return
612 (te-more-break)))
613 (if (eobp)
614 (progn
615 (delete-region (point-min) (+ (point-min) te-width))
616 (goto-char (point-min))
617 (if terminal-scrolling
618 (progn (delete-char 1)
619 (goto-char (point-max))
620 (insert ?\n))))
621 (forward-char 1)
622 (delete-region (point) (+ (point) te-width)))
623 (insert-char ?\ te-width)
624 (beginning-of-line)
625 (te-set-window-start))
626
627 ; ^p = x+32 y+32
628 (defun te-move-to-position ()
629 ;; must offset by #o40 since cretinous unix won't send a 004 char through
630 (let ((y (- (te-get-char) 32))
631 (x (- (te-get-char) 32)))
632 (if (or (> x te-width)
633 (> y te-height))
634 ()
635 (goto-char (+ (point-min) x (* y (1+ te-width))))
636 ;(te-set-window-start?)
637 ))
638 (setq te-more-count -1))
639
640
641
642 ;; ^p c
643 (defun te-clear-rest-of-line ()
644 (save-excursion
645 (let ((n (- (point) (progn (end-of-line) (point)))))
646 (delete-region (point) (+ (point) n))
647 (insert-char ?\ (- n)))))
648
649
650 ;; ^p C
651 (defun te-clear-rest-of-screen ()
652 (save-excursion
653 (te-clear-rest-of-line)
654 (while (progn (end-of-line) (not (eobp)))
655 (forward-char 1) (end-of-line)
656 (delete-region (- (point) te-width) (point))
657 (insert-char ?\ te-width))))
658
659
660 ;; ^p ^l
661 (defun te-clear-screen ()
662 ;; regenerate buffer to compensate for (nonexistent!!) bugs.
663 (erase-buffer)
664 (let ((i 0))
665 (while (< i te-height)
666 (setq i (1+ i))
667 (insert-char ?\ te-width)
668 (insert ?\n)))
669 (delete-region (1- (point-max)) (point-max))
670 (goto-char (point-min))
671 (setq te-more-count -1))
672
673
674 ;; ^p ^o count+32
675 (defun te-insert-lines ()
676 (if (not (bolp))
677 ();(error "fooI")
678 (save-excursion
679 (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
680 (n (min (- (te-get-char) ?\ ) line))
681 (i 0))
682 (delete-region (- (point-max) (* n (1+ te-width))) (point-max))
683 (if (eql (point) (point-max)) (insert ?\n))
684 (while (< i n)
685 (setq i (1+ i))
686 (insert-char ?\ te-width)
687 (or (eql i line) (insert ?\n))))))
688 (setq te-more-count -1))
689
690
691 ;; ^p ^k count+32
692 (defun te-delete-lines ()
693 (if (not (bolp))
694 ();(error "fooD")
695 (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
696 (n (min (- (te-get-char) ?\ ) line))
697 (i 0))
698 (delete-region (point)
699 (min (+ (point) (* n (1+ te-width))) (point-max)))
700 (save-excursion
701 (goto-char (point-max))
702 (while (< i n)
703 (setq i (1+ i))
704 (insert-char ?\ te-width)
705 (or (eql i line) (insert ?\n))))))
706 (setq te-more-count -1))
707
708 ;; ^p ^a
709 (defun te-beginning-of-line ()
710 (beginning-of-line))
711
712 ;; ^p ^b
713 (defun te-backward-char ()
714 (if (not (bolp))
715 (backward-char 1)))
716
717 ;; ^p ^f
718 (defun te-forward-char ()
719 (if (not (eolp))
720 (forward-char 1)))
721
722 \f
723 ;; 0177
724 (defun te-delete ()
725 (if (bolp)
726 ()
727 (delete-region (1- (point)) (point))
728 (insert ?\ )
729 (forward-char -1)))
730
731 ;; ^p ^g
732 (defun te-beep ()
733 (beep))
734
735
736 ;; ^p _ count+32
737 (defun te-insert-spaces ()
738 (let* ((p (point))
739 (n (min (- (te-get-char) 32)
740 (- (progn (end-of-line) (point)) p))))
741 (if (<= n 0)
742 nil
743 (delete-char (- n))
744 (goto-char p)
745 (insert-char ?\ n))
746 (goto-char p)))
747
748 ;; ^p d count+32 (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
749 (defun te-delete-char ()
750 (let* ((p (point))
751 (n (min (- (te-get-char) 32)
752 (- (progn (end-of-line) (point)) p))))
753 (if (<= n 0)
754 nil
755 (insert-char ?\ n)
756 (goto-char p)
757 (delete-char n))
758 (goto-char p)))
759
760
761 \f
762 ;; disgusting unix-required shit
763 ;; Are we living twenty years in the past yet?
764
765 (defun te-losing-unix ()
766 nil)
767
768 ;; ^i
769 (defun te-output-tab ()
770 (let* ((p (point))
771 (x (- p (progn (beginning-of-line) (point))))
772 (l (min (- 8 (logand x 7))
773 (progn (end-of-line) (- (point) p)))))
774 (goto-char (+ p l))))
775
776 ;; ^p ^j
777 ;; Handle the `do' or `nl' termcap capability.
778 ;;>> I am not sure why this broken, obsolete, capability is here.
779 ;;>> Perhaps it is for VIle. No comment was made about why it
780 ;;>> was added (in "Sun Dec 6 01:22:27 1987 Richard Stallman")
781 (defun te-down-vertically-or-scroll ()
782 "Move down a line vertically, or scroll at bottom."
783 (let ((column (current-column)))
784 (end-of-line)
785 (if (eobp)
786 (progn
787 (delete-region (point-min) (+ (point-min) te-width))
788 (goto-char (point-min))
789 (delete-char 1)
790 (goto-char (point-max))
791 (insert ?\n)
792 (insert-char ?\ te-width)
793 (beginning-of-line))
794 (forward-line 1))
795 (move-to-column column))
796 (te-set-window-start))
797
798 ;; Also:
799 ;; ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
800 ;; ^g => te-beep (for which it should use ^p ^g)
801 ;; ^h => te-backward-char (for which it should use ^p ^b)
802
803 \f
804
805 (defun te-filter (process string)
806 (let* ((obuf (current-buffer))
807 (m meta-flag))
808 ;; can't use save-excursion, as that preserves point, which we don't want
809 (unwind-protect
810 (progn
811 (set-buffer (process-buffer process))
812 (goto-char te-saved-point)
813 (and (bufferp te-log-buffer)
814 (if (null (buffer-name te-log-buffer))
815 ;; killed
816 (setq te-log-buffer nil)
817 (set-buffer te-log-buffer)
818 (goto-char (point-max))
819 (insert-before-markers string)
820 (set-buffer (process-buffer process))))
821 (setq te-pending-output (nconc te-pending-output (list string)))
822 (te-update-pending-output-display)
823 ;; this binding is needed because emacs looks at meta-flag when
824 ;; the keystroke is read from the keyboard, not when it is about
825 ;; to be fed into a keymap (or returned by read-char)
826 ;; There still could be some screws, though.
827 (let ((meta-flag m))
828 (te-process-output (eq (current-buffer)
829 (window-buffer (selected-window)))))
830 (set-buffer (process-buffer process))
831 (setq te-saved-point (point)))
832 (set-buffer obuf))))
833
834 ;; (A version of the following comment which might be distractingly offensive
835 ;; to some readers has been moved to term-nasty.el.)
836 ;; unix lacks ITS-style tty control...
837 (defun te-process-output (preemptable)
838 ;;>> There seems no good reason to ever disallow preemption
839 (setq preemptable t)
840 (catch 'te-process-output
841 (let ((buffer-read-only nil)
842 (string nil) ostring start char (matchpos nil))
843 (while (cdr te-pending-output)
844 (setq ostring string
845 start (car te-pending-output)
846 string (car (cdr te-pending-output))
847 char (aref string start))
848 (if (eql (setq start (1+ start)) (length string))
849 (progn (setq te-pending-output
850 (cons 0 (cdr (cdr te-pending-output)))
851 start 0
852 string (car (cdr te-pending-output)))
853 (te-update-pending-output-display))
854 (setcar te-pending-output start))
855 (if (and (> char ?\037) (< char ?\377))
856 (cond ((eolp)
857 ;; unread char
858 (if (eql start 0)
859 (setq te-pending-output
860 (cons 0 (cons (make-string 1 char)
861 (cdr te-pending-output))))
862 (setcar te-pending-output (1- start)))
863 (te-newline))
864 ((null string)
865 (delete-char 1) (insert char)
866 (te-redisplay-if-necessary 1))
867 (t
868 (let ((end (or (and (eq ostring string) matchpos)
869 (setq matchpos (string-match
870 "[\000-\037\177-\377]"
871 string start))
872 (length string))))
873 (delete-char 1) (insert char)
874 (setq char (point)) (end-of-line)
875 (setq end (min end (+ start (- (point) char))))
876 (goto-char char)
877 (if (eql end matchpos) (setq matchpos nil))
878 (delete-region (point) (+ (point) (- end start)))
879 (insert (if (and (eql start 0)
880 (eql end (length string)))
881 string
882 (substring string start end)))
883 (if (eql end (length string))
884 (setq te-pending-output
885 (cons 0 (cdr (cdr te-pending-output))))
886 (setcar te-pending-output end))
887 (te-redisplay-if-necessary (1+ (- end start))))))
888 ;; I suppose if I split the guts of this out into a separate
889 ;; function we could trivially emulate different terminals
890 ;; Who cares in any case? (Apart from stupid losers using rlogin)
891 (funcall
892 (if (eql char ?\^p)
893 (or (cdr (assq (te-get-char)
894 '((?= . te-move-to-position)
895 (?c . te-clear-rest-of-line)
896 (?C . te-clear-rest-of-screen)
897 (?\C-o . te-insert-lines)
898 (?\C-k . te-delete-lines)
899 ;; not necessary, but help sometimes.
900 (?\C-a . te-beginning-of-line)
901 (?\C-b . te-backward-char)
902 ;; should be C-d, but un*x
903 ;; pty's won't send \004 through!
904 ;; Can you believe this?
905 (?d . te-delete-char)
906 (?_ . te-insert-spaces)
907 ;; random
908 (?\C-f . te-forward-char)
909 (?\C-g . te-beep)
910 (?\C-j . te-down-vertically-or-scroll)
911 (?\C-l . te-clear-screen)
912 )))
913 'te-losing-unix)
914 (or (cdr (assq char
915 '((?\C-j . te-newline)
916 (?\177 . te-delete)
917 ;; Did I ask to be sent these characters?
918 ;; I don't remember doing so, either.
919 ;; (Perhaps some operating system or
920 ;; other is completely incompetent...)
921 (?\C-m . te-beginning-of-line)
922 (?\C-g . te-beep)
923 (?\C-h . te-backward-char)
924 (?\C-i . te-output-tab))))
925 'te-losing-unix)))
926 (te-redisplay-if-necessary 1))
927 (and preemptable
928 (input-pending-p)
929 ;; preemptable output! Oh my!!
930 (throw 'te-process-output t)))))
931 ;; We must update window-point in every window displaying our buffer
932 (let* ((s (selected-window))
933 (w s))
934 (while (not (eq s (setq w (next-window w))))
935 (if (eq (window-buffer w) (current-buffer))
936 (set-window-point w (point))))))
937
938 (defun te-get-char ()
939 (if (cdr te-pending-output)
940 (let ((start (car te-pending-output))
941 (string (car (cdr te-pending-output))))
942 (prog1 (aref string start)
943 (if (eql (setq start (1+ start)) (length string))
944 (setq te-pending-output (cons 0 (cdr (cdr te-pending-output))))
945 (setcar te-pending-output start))))
946 (catch 'char
947 (let ((filter (process-filter te-process)))
948 (unwind-protect
949 (progn
950 (set-process-filter te-process
951 (function (lambda (p s)
952 (or (eql (length s) 1)
953 (setq te-pending-output (list 1 s)))
954 (throw 'char (aref s 0)))))
955 (accept-process-output te-process))
956 (set-process-filter te-process filter))))))
957
958
959 (defun te-redisplay-if-necessary (length)
960 (and (<= (setq te-redisplay-count (- te-redisplay-count length)) 0)
961 (eq (current-buffer) (window-buffer (selected-window)))
962 (waiting-for-user-input-p)
963 (progn (te-update-pending-output-display)
964 (sit-for 0)
965 (setq te-redisplay-count terminal-redisplay-interval))))
966
967 (defun te-update-pending-output-display ()
968 (if (null (cdr te-pending-output))
969 (setq te-pending-output-info "")
970 (let ((length (te-pending-output-length)))
971 (if (< length 1500)
972 (setq te-pending-output-info "")
973 (setq te-pending-output-info (format "(%dK chars output pending) "
974 (/ (+ length 512) 1024))))))
975 ;; update mode line
976 (set-buffer-modified-p (buffer-modified-p)))
977
978 \f
979 (defun te-sentinel (process message)
980 (cond ((eq (process-status process) 'run))
981 ((null (buffer-name (process-buffer process)))) ;deleted
982 (t (let ((b (current-buffer)))
983 (save-excursion
984 (set-buffer (process-buffer process))
985 (setq buffer-read-only nil)
986 (fundamental-mode)
987 (goto-char (point-max))
988 (delete-blank-lines)
989 (delete-horizontal-space)
990 (insert "\n*******\n" message "*******\n"))
991 (if (and (eq b (process-buffer process))
992 (waiting-for-user-input-p))
993 (progn (goto-char (point-max))
994 (recenter -1)))))))
995 \f
996 (defvar te-stty-string "stty -nl erase ^? kill ^u intr ^c echo"
997 "Shell command to set terminal modes for terminal emulator.")
998 ;; This used to have `new' in it, but that loses outside BSD
999 ;; and it's apparently not needed in BSD.
1000
1001 (defvar explicit-shell-file-name nil
1002 "*If non-nil, is file name to use for explicitly requested inferior shell.")
1003
1004 ;;;###autoload
1005 (defun terminal-emulator (buffer program args &optional width height)
1006 "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
1007 ARGS is a list of argument-strings. Remaining arguments are WIDTH and HEIGHT.
1008 BUFFER's contents are made an image of the display generated by that program,
1009 and any input typed when BUFFER is the current Emacs buffer is sent to that
1010 program an keyboard input.
1011
1012 Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
1013 are parsed from an input-string using your usual shell.
1014 WIDTH and HEIGHT are determined from the size of the current window
1015 -- WIDTH will be one less than the window's width, HEIGHT will be its height.
1016
1017 To switch buffers and leave the emulator, or to give commands
1018 to the emulator itself (as opposed to the program running under it),
1019 type Control-^. The following character is an emulator command.
1020 Type Control-^ twice to send it to the subprogram.
1021 This escape character may be changed using the variable `terminal-escape-char'.
1022
1023 `Meta' characters may not currently be sent through the terminal emulator.
1024
1025 Here is a list of some of the variables which control the behaviour
1026 of the emulator -- see their documentation for more information:
1027 terminal-escape-char, terminal-scrolling, terminal-more-processing,
1028 terminal-redisplay-interval.
1029
1030 This function calls the value of terminal-mode-hook if that exists
1031 and is non-nil after the terminal buffer has been set up and the
1032 subprocess started.
1033
1034 Presently with `termcap' only; if somebody sends us code to make this
1035 work with `terminfo' we will try to use it."
1036 (interactive
1037 (cons (save-excursion
1038 (set-buffer (get-buffer-create "*terminal*"))
1039 (buffer-name (if (or (not (boundp 'te-process))
1040 (null te-process)
1041 (not (eq (process-status te-process)
1042 'run)))
1043 (current-buffer)
1044 (generate-new-buffer "*terminal*"))))
1045 (append
1046 (let* ((default-s
1047 ;; Default shell is same thing M-x shell uses.
1048 (or explicit-shell-file-name
1049 (getenv "ESHELL")
1050 (getenv "SHELL")
1051 "/bin/sh"))
1052 (s (read-string
1053 (format "Run program in emulator: (default %s) "
1054 default-s))))
1055 (if (equal s "")
1056 (list default-s '())
1057 (te-parse-program-and-args s))))))
1058 (switch-to-buffer buffer)
1059 (if (null width) (setq width (- (window-width (selected-window)) 1)))
1060 (if (null height) (setq height (- (window-height (selected-window)) 1)))
1061 (terminal-mode)
1062 (setq te-width width te-height height)
1063 (setq mode-line-buffer-identification
1064 (list (format "Emacs terminal %dx%d: %%b " te-width te-height)
1065 'te-pending-output-info))
1066 (let ((buffer-read-only nil))
1067 (te-clear-screen))
1068 (let (process)
1069 (while (setq process (get-buffer-process (current-buffer)))
1070 (if (y-or-n-p (format "Kill process %s? " (process-name process)))
1071 (delete-process process)
1072 (error "Process %s not killed" (process-name process)))))
1073 (condition-case err
1074 (let ((termcap
1075 ;; Because of Unix Brain Death(tm), we can't change
1076 ;; the terminal type of a running process, and so
1077 ;; terminal size and scrollability are wired-down
1078 ;; at this point. ("Detach? What's that?")
1079 (concat (format "emacs-virtual:co#%d:li#%d:%s"
1080 ;; Sigh. These can't be dynamically changed.
1081 te-width te-height (if terminal-scrolling
1082 "" "ns:"))
1083 ;;-- Basic things
1084 ;; cursor-motion, bol, forward/backward char
1085 "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:"
1086 ;; newline, clear eof/eof, audible bell
1087 "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:"
1088 ;; insert/delete char/line
1089 "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :"
1090 ;;-- Not-widely-known (ie nonstandard) flags, which mean
1091 ;; o writing in the last column of the last line
1092 ;; doesn't cause idiotic scrolling, and
1093 ;; o don't use idiotische c-s/c-q sogenannte
1094 ;; ``flow control'' auf keinen Fall.
1095 "LP:NF:"
1096 ;;-- For stupid or obsolete programs
1097 "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p= :"
1098 ;;-- For disgusting programs.
1099 ;; (VI? What losers need these, I wonder?)
1100 "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:")))
1101 (let ((process-environment
1102 (cons "TERM=emacs-virtual"
1103 (cons (concat "TERMCAP=" termcap)
1104 process-environment))))
1105 (setq te-process
1106 (start-process "terminal-emulator" (current-buffer)
1107 "/bin/sh" "-c"
1108 ;; Yuck!!! Start a shell to set some terminal
1109 ;; control characteristics. Then start the
1110 ;; "env" program to setup the terminal type
1111 ;; Then finally start the program we wanted.
1112 (format "%s; exec %s"
1113 te-stty-string
1114 (mapconcat 'te-quote-arg-for-sh
1115 (cons program args) " ")))))
1116 (set-process-filter te-process 'te-filter)
1117 (set-process-sentinel te-process 'te-sentinel))
1118 (error (fundamental-mode)
1119 (signal (car err) (cdr err))))
1120 ;; sigh
1121 (if (default-value 'meta-flag)
1122 (progn (message
1123 "Note: Meta key disabled due to maybe-eventually-reparable braindamage")
1124 (sit-for 1)))
1125 (setq inhibit-quit t) ;sport death
1126 (use-local-map terminal-map)
1127 (run-hooks 'terminal-mode-hook)
1128 (message "Entering emacs terminal-emulator... Type %s %s for help"
1129 (single-key-description terminal-escape-char)
1130 (mapconcat 'single-key-description
1131 (where-is-internal 'te-escape-help
1132 terminal-escape-map
1133 nil t)
1134 " ")))
1135
1136
1137 (defun te-parse-program-and-args (s)
1138 (cond ((string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ \t]*\\)+\\'" s)
1139 (let ((l ()) (p 0))
1140 (while p
1141 (setq l (cons (if (string-match
1142 "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ \t]+\\)*"
1143 s p)
1144 (prog1 (substring s p (match-end 1))
1145 (setq p (match-end 0))
1146 (if (eql p (length s)) (setq p nil)))
1147 (prog1 (substring s p)
1148 (setq p nil)))
1149 l)))
1150 (setq l (nreverse l))
1151 (list (car l) (cdr l))))
1152 ((and (string-match "[ \t]" s) (not (file-exists-p s)))
1153 (list shell-file-name (list "-c" (concat "exec " s))))
1154 (t (list s ()))))
1155
1156 (put 'terminal-mode 'mode-class 'special)
1157 ;; This is only separated out from function terminal-emulator
1158 ;; to keep the latter a little more managable.
1159 (defun terminal-mode ()
1160 "Set up variables for use f the terminal-emualtor.
1161 One should not call this -- it is an internal function
1162 of the terminal-emulator"
1163 (kill-all-local-variables)
1164 (buffer-disable-undo (current-buffer))
1165 (setq major-mode 'terminal-mode)
1166 (setq mode-name "terminal")
1167 ; (make-local-variable 'Helper-return-blurb)
1168 ; (setq Helper-return-blurb "return to terminal simulator")
1169 (setq mode-line-process '(": %s"))
1170 (setq buffer-read-only t)
1171 (setq truncate-lines t)
1172 (make-local-variable 'terminal-escape-char)
1173 (setq terminal-escape-char (default-value 'terminal-escape-char))
1174 (make-local-variable 'terminal-scrolling)
1175 (setq terminal-scrolling (default-value 'terminal-scrolling))
1176 (make-local-variable 'terminal-more-processing)
1177 (setq terminal-more-processing (default-value 'terminal-more-processing))
1178 (make-local-variable 'terminal-redisplay-interval)
1179 (setq terminal-redisplay-interval (default-value 'terminal-redisplay-interval))
1180 (make-local-variable 'te-width)
1181 (make-local-variable 'te-height)
1182 (make-local-variable 'te-process)
1183 (make-local-variable 'te-pending-output)
1184 (setq te-pending-output (list 0))
1185 (make-local-variable 'te-saved-point)
1186 (setq te-saved-point (point-min))
1187 (make-local-variable 'te-pending-output-info) ;for the mode line
1188 (setq te-pending-output-info "")
1189 (make-local-variable 'inhibit-quit)
1190 ;(setq inhibit-quit t)
1191 (make-local-variable 'te-log-buffer)
1192 (setq te-log-buffer nil)
1193 (make-local-variable 'te-more-count)
1194 (setq te-more-count -1)
1195 (make-local-variable 'te-redisplay-count)
1196 (setq te-redisplay-count terminal-redisplay-interval)
1197 ;;>> Nothing can be done about this without decruftifying
1198 ;;>> emacs keymaps.
1199 (make-local-variable 'meta-flag) ;sigh
1200 (setq meta-flag nil)
1201 ;(use-local-map terminal-mode-map)
1202 ;; terminal-mode-hook is called above in function terminal-emulator
1203 )
1204 \f
1205 ;;;; what a complete loss
1206
1207 (defun te-quote-arg-for-sh (string)
1208 (cond ((string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'"
1209 string)
1210 string)
1211 ((not (string-match "[$]" string))
1212 ;; "[\"\\]" are special to sh and the lisp reader in the same way
1213 (prin1-to-string string))
1214 (t
1215 (let ((harder "")
1216 (start 0)
1217 (end 0))
1218 (while (cond ((>= start (length string))
1219 nil)
1220 ;; this is the set of chars magic with "..." in `sh'
1221 ((setq end (string-match "[\"\\$]"
1222 string start))
1223 t)
1224 (t (setq harder (concat harder
1225 (substring string start)))
1226 nil))
1227 (setq harder (concat harder (substring string start end)
1228 ;; Can't use ?\\ since `concat'
1229 ;; unfortunately does prin1-to-string
1230 ;; on fixna. Amazing.
1231 "\\"
1232 (substring string
1233 end
1234 (1+ end)))
1235 start (1+ end)))
1236 (concat "\"" harder "\"")))))
1237
1238 (provide 'terminal)
1239
1240 ;;; terminal.el ends here