1 ;;; terminal.el --- terminal emulator for GNU Emacs.
2 ;; Keywords: comm, terminals
4 ;; Copyright (C) 1986, 1987, 1988, 1989, 1993 Free Software Foundation, Inc.
5 ;; Written by Richard Mlynarik, November 1986.
6 ;; Face and attribute support added by Richard Mlynarik, April 1996.
8 ;; This file is part of XEmacs.
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Synched up with: Not synched with FSF.
30 ;;#### One probably wants to do setenv MORE -c when running with
31 ;;#### more-processing enabled.
36 (defvar terminal-escape-char ?\C-^
37 "*All characters except for this are passed verbatim through the
38 terminal-emulator. This character acts as a prefix for commands
39 to the emulator program itself. Type this character twice to send
40 it through the emulator. Type ? after typing it for a list of
42 This variable is local to each terminal-emulator buffer.")
44 (defvar terminal-scrolling t
45 "*If non-nil, the terminal-emulator will `scroll' when output occurs
46 past the bottom of the screen. If nil, output will `wrap' to the top
48 This variable is local to each terminal-emulator buffer.")
50 (defvar terminal-more-processing t
51 "*If non-nil, do more-processing.
52 This variable is local to each terminal-emulator buffer.")
54 ;; If you are the sort of loser who uses scrolling without more breaks
55 ;; and expects to actually see anything, you should probably set this to
57 (defvar terminal-redisplay-interval 5000
58 "*Maximum number of characters which will be processed by the
59 terminal-emulator before a screen redisplay is forced.
60 Set this to a large value for greater throughput,
61 set it smaller for more frequent updates but overall slower
64 (defvar terminal-more-break-insertion
65 "*** More break -- Press space to continue ***")
67 (defvar terminal-escape-map nil)
68 (defvar terminal-map nil)
69 (defvar terminal-more-break-map nil)
72 (let ((map (make-keymap)))
73 (set-keymap-name map 'terminal-map)
75 (let ((meta-prefix-char -1)
80 (define-key map s 'te-pass-through)
83 ;(define-key map "\C-l"
84 ; '(lambda () (interactive) (te-pass-through) (redraw-display)))
85 (setq terminal-map map)))
87 (if terminal-escape-map
89 (let ((map (make-keymap)))
90 (set-keymap-name map 'terminal-escape-map)
91 (let ((s (make-string 1 ?0)))
92 (while (<= (aref s 0) ?9)
93 (define-key map s 'digit-argument)
94 (aset s 0 (1+ (aref s 0)))))
95 (define-key map "b" 'switch-to-buffer)
96 (define-key map "o" 'other-window)
97 (define-key map "e" 'te-set-escape-char)
98 (define-key map "\C-l" 'redraw-display)
99 (define-key map "\C-o" 'te-flush-pending-output)
100 (define-key map "m" 'te-toggle-more-processing)
101 (define-key map "x" 'te-escape-extended-command)
102 (define-key map "?" 'te-escape-help)
103 (define-key map (vector help-char) 'te-escape-help)
104 (setq terminal-escape-map map)))
106 (defvar te-escape-command-alist '())
107 (if te-escape-command-alist
109 (setq te-escape-command-alist
110 '(("Set Escape Character" . te-set-escape-char)
111 ("Refresh" . redraw-display)
112 ("Record Output" . te-set-output-log)
113 ("Photo" . te-set-output-log)
114 ("Tofu" . te-tofu) ;; confuse the uninitiated
115 ("Stuff Input" . te-stuff-string)
116 ("Flush Pending Output" . te-flush-pending-output)
117 ("Enable More Processing" . te-enable-more-processing)
118 ("Disable More Processing" . te-disable-more-processing)
119 ("Scroll at end of page" . te-do-scrolling)
120 ("Wrap at end of page" . te-do-wrapping)
121 ("Switch To Buffer" . switch-to-buffer)
122 ("Other Window" . other-window)
123 ("Kill Buffer" . kill-buffer)
124 ("Help" . te-escape-help)
125 ("Set Redisplay Interval" . te-set-redisplay-interval)
128 ;(setq terminal-more-break-map nil)
129 (if terminal-more-break-map
131 (let ((map (make-keymap)))
132 (set-keymap-name map 'terminal-more-break-map)
134 (let ((meta-prefix-char -1)
135 (s (make-string 1 0))
139 (define-key map s 'te-more-break-unwind)
142 (define-key map (vector help-char) 'te-more-break-help)
143 (define-key map " " 'te-more-break-resume)
144 (define-key map "\C-l" 'redraw-display)
145 (define-key map "\C-o" 'te-more-break-flush-pending-output)
146 ;;#### this isn't right
147 ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
148 (define-key map "\r" 'te-more-break-advance-one-line)
150 (setq terminal-more-break-map map)))
155 (defvar te-pending-output)
156 (defvar te-saved-point)
157 (defvar te-pending-output-info)
158 (defvar te-log-buffer)
159 (defvar te-more-count)
160 (defvar te-redisplay-count)
161 (defvar te-current-face)
162 (defvar te-current-attributes)
164 (make-face 'terminal-default)
166 (make-face 'terminal-standout)
167 (if (not (face-differs-from-default-p 'terminal-standout))
168 (copy-face 'bold 'terminal-standout))
170 (make-face 'terminal-underline)
171 (cond ((face-differs-from-default-p 'terminal-underline))
172 ((find-face 'underline)
173 (copy-face 'underline 'terminal-underline))
175 (set-face-underline-p 'terminal-underline t)))
177 (make-face 'terminal-standout-underline)
178 (cond ((face-differs-from-default-p 'terminal-standout-underline))
180 (copy-face 'terminal-standout 'terminal-standout-underline)
181 (set-face-underline-p 'terminal-standout-underline t)))
183 (defun te-insert-blank (count)
185 (insert-char ?\ count)
186 (put-text-property p (point) 'face 'terminal-default)))
191 (defun te-escape-p (event)
192 (cond ((eventp terminal-escape-char)
193 (cond ((key-press-event-p event)
194 (and (key-press-event-p terminal-escape-char)
195 (= (event-modifier-bits event)
196 (event-modifier-bits terminal-escape-char))
197 (eq (event-key event)
198 (event-key terminal-escape-char))))
199 ((button-press-event-p event)
200 (and (button-press-event-p terminal-escape-char)
201 (= (event-modifier-bits event)
202 (event-modifier-bits terminal-escape-char))
203 (eq (event-button event)
204 (event-button terminal-escape-char))))
206 ((numberp terminal-escape-char)
207 (let ((c (event-to-character event nil t nil)))
208 (and c (= c terminal-escape-char))))
215 (let ((c (let ((cursor-in-echo-area t)
216 (prompt (if prefix-arg
217 (format "Emacs Terminal escape> %d "
218 (prefix-numeric-value prefix-arg))
219 "Emacs Terminal escape> ")))
220 (message "%s" prompt)
221 (let ((e (next-command-event)))
222 (while (button-release-event-p e)
223 (setq e (next-command-event e)))
227 (setq unread-command-event e)
228 (lookup-key terminal-escape-map
229 (read-key-sequence prompt))))))))
232 (copy-event c last-command-event)
233 (let ((terminal-escape-char -259))
236 (call-interactively c)))))
238 (defun te-escape-help ()
239 "Provide help on commands available after terminal-escape-char is typed."
241 (message "Terminal emulator escape help...")
242 (let ((char (single-key-description terminal-escape-char)))
245 (princ (format "Terminal-emulator escape, invoked by \"%s\"
246 Type \"%s\" twice to send a single \"%s\" through.
248 Other chars following \"%s\" are interpreted as follows:\n"
249 char char char char))
251 (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
252 (princ (format "\nSubcommands of \"%s\" (%s)\n"
253 (where-is-internal 'te-escape-extended-command
254 terminal-escape-map t)
255 'te-escape-extended-command))
256 (let ((l (if (fboundp 'sortcar)
257 (sortcar (copy-sequence te-escape-command-alist)
259 (sort (copy-sequence te-escape-command-alist)
260 (function (lambda (a b)
261 (string< (car a) (car b))))))))
263 (let ((doc (or (documentation (cdr (car l)))
265 (if (string-match "\n" doc)
266 ;; just use first line of documentation
267 (setq doc (substring doc 0 (match-beginning 0))))
269 (princ (car (car l)))
278 (defun te-escape-extended-command ()
280 (let ((c (let ((completion-ignore-case t))
281 (completing-read "terminal command: "
282 te-escape-command-alist
286 (setq c (downcase c))
287 (let ((l te-escape-command-alist))
289 (if (string= c (downcase (car (car l))))
290 (throw 'foo (call-interactively (cdr (car l))))
291 (setq l (cdr l)))))))))
294 (defun te-escape-extended-command-unread ()
296 (setq unread-command-event last-command-event)
297 (te-escape-extended-command))
299 (defun te-set-escape-char (c)
300 "Change the terminal-emulator escape character."
301 (interactive (list (let ((cursor-in-echo-area t))
302 (message "Set escape character to: ")
303 (let ((e (next-command-event)))
304 (while (button-release-event-p e)
305 (setq e (next-command-event e)))
307 (cond ((te-escape-p c)
308 (message "\"%s\" is escape char"))
309 ((and (eventp terminal-escape-char)
310 (event-to-character terminal-escape-char nil t nil))
311 (message "\"%s\" is now escape; \"%s\" passes though"
312 (single-key-description c)
313 (single-key-description terminal-escape-char)))
315 (message "\"%s\" is now escape"
316 (single-key-description c))
317 ;; Let mouse-events, for example, go back to looking at global map
318 (local-unset-key (vector terminal-escape-char))))
319 (local-set-key (vector c) 'te-escape) ;ensure it's defined
320 (setq terminal-escape-char c))
323 (defun te-stuff-string (string)
324 "Read a string to send to through the terminal emulator
325 as though that string had been typed on the keyboard.
327 Very poor man's file transfer protocol."
328 (interactive "sStuff string: ")
329 (process-send-string te-process string))
331 (defun te-set-output-log (name)
332 "Record output from the terminal emulator in a buffer."
333 (interactive (list (if te-log-buffer
335 (read-buffer "Record output in buffer: "
336 (format "%s output-log"
337 (buffer-name (current-buffer)))
339 (if (or (null name) (equal name ""))
340 (progn (setq te-log-buffer nil)
341 (message "Output logging off."))
342 (if (get-buffer name)
345 (set-buffer (get-buffer-create name))
347 (buffer-disable-undo (current-buffer))
349 (setq te-log-buffer (get-buffer name))
350 (message "Recording terminal emulator output into buffer \"%s\""
351 (buffer-name te-log-buffer))))
354 "Discontinue output log."
356 (te-set-output-log nil))
359 (defun te-toggle (sym arg)
360 (set sym (cond ((not (numberp arg)) arg)
361 ((= arg 1) (not (symbol-value sym)))
365 (defun te-toggle-more-processing (arg)
367 (message (if (te-toggle 'terminal-more-processing arg)
368 "More processing on" "More processing off"))
369 (if terminal-more-processing (setq te-more-count -1)))
371 (defun te-toggle-scrolling (arg)
373 (message (if (te-toggle 'terminal-scrolling arg)
374 "Scroll at end of page" "Wrap at end of page")))
376 (defun te-enable-more-processing ()
377 "Enable ** MORE ** processing"
379 (te-toggle-more-processing t))
381 (defun te-disable-more-processing ()
382 "Disable ** MORE ** processing"
384 (te-toggle-more-processing nil))
386 (defun te-do-scrolling ()
387 "Scroll at end of page (yuck)"
389 (te-toggle-scrolling t))
391 (defun te-do-wrapping ()
392 "Wrap to top of window at end of page"
394 (te-toggle-scrolling nil))
397 (defun te-set-redisplay-interval (arg)
398 "Set the maximum interval (in output characters) between screen updates.
399 Set this number to large value for greater throughput,
400 set it smaller for more frequent updates (but overall slower performance."
401 (interactive "NMax number of output chars between redisplay updates: ")
402 (setq arg (max arg 1))
403 (setq terminal-redisplay-interval arg
404 te-redisplay-count 0))
408 ;; every command -must- call te-more-break-unwind
409 ;; or grave lossage will result
411 (put 'te-more-break-unread 'suppress-keymap t)
412 (defun te-more-break-unread ()
414 (if (te-escape-p last-command-event)
415 (call-interactively 'te-escape)
416 (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
417 (single-key-description last-command-event)
418 (te-pending-output-length))
419 (setq te-more-count 259259)
420 (te-more-break-unwind)
421 (let ((terminal-more-processing nil))
424 (defun te-more-break-resume ()
425 "Proceed past the **MORE** break,
426 allowing the next page of output to appear"
428 (message "Continuing from more break")
429 (te-more-break-unwind))
431 (defun te-more-break-help ()
432 "Provide help on commands available in a terminal-emulator **MORE** break"
434 (message "Terminal-emulator more break help...")
438 (princ "Terminal-emulator more break.\n\n")
439 (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
440 (where-is-internal 'te-more-break-resume
441 terminal-more-break-map t)
442 (documentation 'te-more-break-resume)))
443 (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
444 (princ "Any other key is passed through to the program
445 running under the terminal emulator and disables more processing until
446 all pending output has been dealt with.")
450 (defun te-more-break-advance-one-line ()
451 "Allow one more line of text to be output before doing another more break."
453 (setq te-more-count 1)
454 (te-more-break-unwind))
456 (defun te-more-break-flush-pending-output ()
457 "Discard any output which has been received by the terminal emulator but
458 not yet proceesed and then proceed from the more break."
460 (te-more-break-unwind)
461 (te-flush-pending-output))
463 (defun te-flush-pending-output ()
464 "Discard any as-yet-unprocessed output which has been received by
465 the terminal emulator."
467 ;; this could conceivably be confusing in the presence of
468 ;; escape-sequences spanning process-output chunks
469 (if (null (cdr te-pending-output))
470 (message "(There is no output pending)")
471 (let ((length (te-pending-output-length)))
472 (message "Flushing %d chars of pending output" length)
473 (setq te-pending-output
474 (list 0 (format "\n*** %d chars of pending output flushed ***\n"
476 (te-update-pending-output-display)
477 (te-process-output nil)
481 (defun te-pass-through ()
482 "Send the last character typed through the terminal-emulator
483 without any interpretation"
485 (if (te-escape-p last-command-event)
486 (call-interactively 'te-escape)
487 (and terminal-more-processing
488 (null (cdr te-pending-output))
489 (te-set-more-count nil))
490 (let ((c (event-to-character last-command-event nil t nil)))
491 (if c (process-send-string te-process (make-string 1 c))))
492 (te-process-output t)))
494 (defun te-set-window-start ()
495 (let* ((w (get-buffer-window (current-buffer)))
496 (h (if w (window-height w))))
497 (cond ((not w)) ; buffer not displayed
498 ((>= h (/ (- (point) (point-min)) (1+ te-width)))
499 ;; this is the normal case
500 (set-window-start w (point-min)))
501 ;; this happens if some vandal shrinks our window.
502 ((>= h (/ (- (point-max) (point)) (1+ te-width)))
503 (set-window-start w (- (point-max) (* h (1+ te-width)) -1)))
507 (defun te-pending-output-length ()
508 (let ((length (car te-pending-output))
509 (tem (cdr te-pending-output)))
511 (setq length (+ length (length (car tem))) tem (cdr tem)))
516 (defun te-more-break ()
517 (te-set-more-count t)
518 (make-local-variable 'te-more-old-point)
519 (setq te-more-old-point (point))
520 (make-local-variable 'te-more-old-local-map)
521 (setq te-more-old-local-map (current-local-map))
522 (use-local-map terminal-more-break-map)
523 (make-local-variable 'te-more-old-filter)
524 (setq te-more-old-filter (process-filter te-process))
525 (make-local-variable 'te-more-old-mode-line-format)
526 (setq te-more-old-mode-line-format mode-line-format
527 mode-line-format (list "-- **MORE** "
528 mode-line-buffer-identification
530 (set-process-filter te-process
531 (function (lambda (process string)
533 (set-buffer (process-buffer process))
534 (setq te-pending-output (nconc te-pending-output
536 (te-update-pending-output-display))))
537 (te-update-pending-output-display)
538 (if (eq (window-buffer (selected-window)) (current-buffer))
539 (message "More break "))
541 (null terminal-more-break-insertion)
544 (delete-region (point) (+ (point) te-width))
545 (insert terminal-more-break-insertion)))
546 (run-hooks 'terminal-more-break-hook)
547 (sit-for 0) ;get display to update
548 (throw 'te-process-output t))
550 (defun te-more-break-unwind ()
552 (use-local-map te-more-old-local-map)
553 (set-process-filter te-process te-more-old-filter)
554 (goto-char te-more-old-point)
555 (setq mode-line-format te-more-old-mode-line-format)
556 (set-buffer-modified-p (buffer-modified-p))
557 (let ((buffer-read-only nil))
559 (terminal-more-break-insertion
561 (delete-region (point)
562 (+ (point) (length terminal-more-break-insertion)))
563 (te-insert-blank te-width)
564 (goto-char te-more-old-point)))
565 (setq te-more-old-point nil)
566 (let ((te-more-count 259259))
569 (te-process-output t))
571 (defun te-set-more-count (newline)
572 (let ((line (/ (- (point) (point-min)) (1+ te-width))))
573 (if newline (setq line (1+ line)))
574 (cond ((= line te-height)
575 (setq te-more-count te-height))
576 ;#### something is strange. Investigate this!
577 ((= line (1- te-height))
578 (setq te-more-count te-height))
579 ((or (< line (/ te-height 2))
580 (> (- te-height line) 10))
581 ;; break at end of this page
582 (setq te-more-count (- te-height line)))
584 ;; migrate back towards top (ie bottom) of screen.
585 (setq te-more-count (- te-height
586 (if (> te-height 10) 2 1)))))))
589 ;;;; More or less straight-forward terminal escapes
591 ;; ^j, meaning `newline' to non-display programs.
592 ;; (Who would think of ever writing a system which doesn't understand
593 ;; display terminals natively? Un*x: The Operating System of the Future.)
595 "Move down a line, optionally do more processing, perhaps wrap/scroll,
596 move to start of new line, clear to end of line."
598 (cond ((not terminal-more-processing))
599 ((< (setq te-more-count (1- te-more-count)) 0)
600 (te-set-more-count t))
601 ((eq te-more-count 0)
602 ;; this doesn't return
606 (delete-region (point-min) (+ (point-min) te-width))
607 (goto-char (point-min))
608 (if terminal-scrolling
609 (progn (delete-char 1)
610 (goto-char (point-max))
613 (delete-region (point) (+ (point) te-width)))
614 (te-insert-blank te-width)
616 (te-set-window-start))
619 ;; Handle the `do' or `nl' termcap capability.
620 ;;#### I am not sure why this broken, obsolete, capability is here.
621 ;;#### Perhaps it is for VIle. No comment was made about why it
622 ;;#### was added (in "Sun Dec 6 01:22:27 1987 Richard Stallman")
623 (defun te-down-vertically-or-scroll ()
624 "Move down a line vertically, or scroll at bottom."
625 (let ((column (current-column)))
629 (delete-region (point-min) (+ (point-min) te-width))
630 (goto-char (point-min))
632 (goto-char (point-max))
634 (te-insert-blank te-width)
637 (move-to-column column))
638 (te-set-window-start))
641 (defun te-move-to-position ()
642 ;; must offset by #o40 since cretinous unix won't send a 004 char through
643 (let ((y (- (te-get-char) 32))
644 (x (- (te-get-char) 32)))
645 (if (or (> x te-width)
647 () ;(error "fucked %d %d" x y)
648 (goto-char (+ (point-min) x (* y (1+ te-width))))
649 ;(te-set-window-start?)
651 (setq te-more-count -1))
656 (defun te-clear-rest-of-line ()
658 (let ((n (- (point) (progn (end-of-line) (point)))))
659 (delete-region (point) (+ (point) n))
660 (te-insert-blank (- n)))))
664 (defun te-clear-rest-of-screen ()
666 (te-clear-rest-of-line)
667 (while (progn (end-of-line) (not (eobp)))
668 (forward-char 1) (end-of-line)
669 (delete-region (- (point) te-width) (point))
670 (te-insert-blank te-width))))
674 (defun te-clear-screen ()
675 ;; regenerate buffer to compensate for (nonexistent!!) bugs.
678 (while (< i te-height)
680 (te-insert-blank te-width)
682 (delete-region (1- (point-max)) (point-max))
683 (goto-char (point-min))
684 (setq te-more-count -1))
688 (defun te-insert-lines ()
692 (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
693 (n (min (- (te-get-char) ?\ ) line))
695 (delete-region (- (point-max) (* n (1+ te-width))) (point-max))
696 (if (eq (point) (point-max)) (insert ?\n))
699 (te-insert-blank te-width)
700 (or (eq i line) (insert ?\n))))))
701 (setq te-more-count -1))
705 (defun te-delete-lines ()
708 (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
709 (n (min (- (te-get-char) ?\ ) line))
711 (delete-region (point)
712 (min (+ (point) (* n (1+ te-width))) (point-max)))
714 (goto-char (point-max))
717 (te-insert-blank te-width)
718 (or (eq i line) (insert ?\n))))))
719 (setq te-more-count -1))
722 (defun te-beginning-of-line ()
726 (defun te-backward-char ()
731 (defun te-forward-char ()
737 (defun te-change-attribute ()
738 (let* ((attribute (te-get-char))
739 (on (= (te-get-char) ?1))
740 (standout (assq 'standout te-current-attributes))
741 (underline (assq 'underline te-current-attributes))
742 (frob (function (lambda ()
743 ;; This would be even more of a combinatorial mess if I
744 ;; decided I wanted to support anything more than the two
745 ;; standout and underline attributes.
746 (setq te-current-face
747 (or (cdr (assoc te-current-attributes
748 '((((standout . t) (underline . nil))
750 (((standout . nil) (underline . t))
752 (((standout . t) (underline . nil))
753 . terminal-standout-underline))))
754 'terminal-default))))))
755 (cond ((= attribute ?+) ;standout on/off
758 ((= attribute ?_) ;underline on/off
759 (setcdr underline on)
761 ;; reverse, blink, half-bright, double-bright, blank, protect
764 (setcdr underline nil)
765 (setcdr standout nil)
766 (setq te-current-face 'terminal-default)))))
773 (delete-region (1- (point)) (point))
783 (defun te-insert-spaces ()
785 (n (min (- (te-get-char) 32)
786 (- (progn (end-of-line) (point)) p))))
794 ;; ^p d count+32 (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
795 (defun te-delete-char ()
797 (n (min (- (te-get-char) 32)
798 (- (progn (end-of-line) (point)) p))))
808 ;; disgusting unix-required shit
809 ;; Are we living twenty years in the past yet?
811 (defun te-losing-unix ()
813 ;(message "fucking-unix: %d" char)
817 (defun te-output-tab ()
819 (x (- p (progn (beginning-of-line) (point))))
820 (l (min (- 8 (logand x 7))
821 (progn (end-of-line) (- (point) p)))))
822 (goto-char (+ p l))))
825 ;; ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
826 ;; ^g => te-beep (for which it should use ^p ^g)
827 ;; ^h => te-backward-char (for which it should use ^p ^b)
831 (defun te-filter (process string)
832 (let* ((obuf (current-buffer)))
833 ;; can't use save-excursion, as that preserves point, which we don't want
836 (set-buffer (process-buffer process))
837 (goto-char te-saved-point)
838 (and (bufferp te-log-buffer)
839 (if (null (buffer-name te-log-buffer))
841 (setq te-log-buffer nil)
842 (set-buffer te-log-buffer)
843 (goto-char (point-max))
844 (insert-before-markers string)
845 (set-buffer (process-buffer process))))
846 (setq te-pending-output (nconc te-pending-output (list string)))
847 (te-update-pending-output-display)
848 (te-process-output (eq (current-buffer)
849 (window-buffer (selected-window))))
850 (set-buffer (process-buffer process))
851 (setq te-saved-point (point)))
854 ;; fucking unix has -such- braindamaged lack of tty control...
855 (defun te-process-output (preemptable)
856 ;;#### There seems no good reason to ever disallow preemption
858 (catch 'te-process-output
859 (let ((buffer-read-only nil)
860 (string nil) ostring start char (matchpos nil))
861 (while (cdr te-pending-output)
863 start (car te-pending-output)
864 string (car (cdr te-pending-output))
865 char (aref string start))
866 (if (eq (setq start (1+ start)) (length string))
867 (progn (setq te-pending-output
868 (cons 0 (cdr (cdr te-pending-output)))
870 string (car (cdr te-pending-output)))
871 (te-update-pending-output-display))
872 (setcar te-pending-output start))
873 (if (and (> char ?\037) (< char ?\377))
877 (setq te-pending-output
878 (cons 0 (cons (make-string 1 char)
879 (cdr te-pending-output))))
880 (setcar te-pending-output (1- start)))
883 (delete-char 1) (insert char)
884 (put-text-property (1- (point)) (point)
885 'face te-current-face)
886 (te-redisplay-if-necessary 1))
888 (let ((end (or (and (eq ostring string) matchpos)
889 (setq matchpos (string-match
890 "[\000-\037\177-\377]"
893 (delete-char 1) (insert char)
895 (put-text-property (1- char) char 'face te-current-face)
897 (setq end (min end (+ start (- (point) char))))
899 (if (eq end matchpos) (setq matchpos nil))
900 (delete-region (point) (+ (point) (- end start)))
902 (insert (if (and (eq start 0)
903 (eq end (length string)))
905 (substring string start end)))
906 (put-text-property char (point) 'face te-current-face)
907 (if (eq end (length string))
908 (setq te-pending-output
909 (cons 0 (cdr (cdr te-pending-output))))
910 (setcar te-pending-output end))
911 (te-redisplay-if-necessary (1+ (- end start))))))
912 ;; I suppose if I split the guts of this out into a separate
913 ;; function we could trivially emulate different terminals
914 ;; Who cares in any case? (Apart from stupid losers using rlogin)
917 (or (cdr (assq (te-get-char)
918 '((?= . te-move-to-position)
919 (?c . te-clear-rest-of-line)
920 (?C . te-clear-rest-of-screen)
921 (?\C-o . te-insert-lines)
922 (?\C-k . te-delete-lines)
923 (?* . te-change-attribute)
924 ;; not necessary, but help sometimes.
925 (?\C-a . te-beginning-of-line)
926 (?\C-b . te-backward-char)
927 ;; should be C-d, but un*x
928 ;; pty's won't send \004 through!
929 ;; Can you believe this?
930 (?d . te-delete-char)
931 (?_ . te-insert-spaces)
933 (?\C-f . te-forward-char)
935 (?\C-j . te-down-vertically-or-scroll)
936 (?\C-l . te-clear-screen)
940 '((?\C-j . te-newline)
942 ;; Did I ask to be sent these characters?
943 ;; I don't remember doing so, either.
944 ;; (Perhaps some operating system or
945 ;; other is completely incompetent...)
946 (?\C-m . te-beginning-of-line) ;fuck me harder
947 (?\C-g . te-beep) ;again and again!
948 (?\C-h . te-backward-char) ;wa12id!!
949 (?\C-i . te-output-tab)))) ;(spiked)
950 'te-losing-unix))) ;That feels better
951 (te-redisplay-if-necessary 1))
954 ;; preemptable output! Oh my!!
955 (throw 'te-process-output t)))))
956 ;; We must update window-point in every window displaying our buffer
957 (let* ((s (selected-window))
959 (while (not (eq s (setq w (next-window w))))
960 (if (eq (window-buffer w) (current-buffer))
961 (set-window-point w (point))))))
963 (defun te-get-char ()
964 (if (cdr te-pending-output)
965 (let ((start (car te-pending-output))
966 (string (car (cdr te-pending-output))))
967 (prog1 (aref string start)
968 (if (eq (setq start (1+ start)) (length string))
969 (setq te-pending-output (cons 0 (cdr (cdr te-pending-output))))
970 (setcar te-pending-output start))))
972 (let ((filter (process-filter te-process)))
975 (set-process-filter te-process
976 (function (lambda (p s)
977 (or (eq (length s) 1)
978 (setq te-pending-output (list 1 s)))
979 (throw 'char (aref s 0)))))
980 (accept-process-output te-process))
981 (set-process-filter te-process filter))))))
984 (defun te-redisplay-if-necessary (length)
985 (and (<= (setq te-redisplay-count (- te-redisplay-count length)) 0)
986 (eq (current-buffer) (window-buffer (selected-window)))
987 (waiting-for-user-input-p)
988 (progn (te-update-pending-output-display)
990 (setq te-redisplay-count terminal-redisplay-interval))))
992 (defun te-update-pending-output-display ()
993 (if (null (cdr te-pending-output))
994 (setq te-pending-output-info "")
995 (let ((length (te-pending-output-length)))
997 (setq te-pending-output-info "")
998 (setq te-pending-output-info (format "(%dK chars output pending) "
999 (/ (+ length 512) 1024))))))
1001 (set-buffer-modified-p (buffer-modified-p)))
1004 (defun te-sentinel (process message)
1005 (cond ((eq (process-status process) 'run))
1006 ((null (buffer-name (process-buffer process)))) ;deleted
1007 (t (let ((b (current-buffer)))
1009 (set-buffer (process-buffer process))
1010 (setq buffer-read-only nil)
1012 (goto-char (point-max))
1013 (delete-blank-lines)
1014 (delete-horizontal-space)
1015 (insert "\n*******\n" message "*******\n"))
1016 (if (and (eq b (process-buffer process))
1017 (waiting-for-user-input-p))
1018 (progn (goto-char (point-max))
1021 (defvar te-stty-string "stty -nl erase '^?' kill '^u' intr '^c' echo pass8"
1022 "Shell command to set terminal modes for terminal emulator.")
1023 ;; This used to have `new' in it, but that loses outside BSD
1024 ;; and it's apparently not needed in BSD.
1026 (defvar explicit-shell-file-name nil
1027 "*If non-nil, is file name to use for explicitly requested inferior shell.")
1030 (defun terminal-emulator (buffer program args &optional width height)
1031 "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
1032 ARGS is a list of argument-strings. Remaining arguments are WIDTH and HEIGHT.
1033 BUFFER's contents are made an image of the display generated by that program,
1034 and any input typed when BUFFER is the current Emacs buffer is sent to that
1035 program an keyboard input.
1037 Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
1038 are parsed from an input-string using your usual shell.
1039 WIDTH and HEIGHT are determined from the size of the current window
1040 -- WIDTH will be one less than the window's width, HEIGHT will be its height.
1042 To switch buffers and leave the emulator, or to give commands
1043 to the emulator itself (as opposed to the program running under it),
1044 type Control-^. The following character is an emulator command.
1045 Type Control-^ twice to send it to the subprogram.
1046 This escape character may be changed using the variable `terminal-escape-char'.
1048 `Meta' characters may not currently be sent through the terminal emulator.
1050 Here is a list of some of the variables which control the behaviour
1051 of the emulator -- see their documentation for more information:
1052 terminal-escape-char, terminal-scrolling, terminal-more-processing,
1053 terminal-redisplay-interval.
1055 This function calls the value of terminal-mode-hook if that exists
1056 and is non-nil after the terminal buffer has been set up and the
1059 Presently with `termcap' only; if somebody sends us code to make this
1060 work with `terminfo' we will try to use it."
1062 (cons (save-excursion
1063 (set-buffer (get-buffer-create "*terminal*"))
1064 (buffer-name (if (or (not (boundp 'te-process))
1066 (not (eq (process-status te-process)
1069 (generate-new-buffer "*terminal*"))))
1072 ;; Default shell is same thing M-x shell uses.
1073 (or explicit-shell-file-name
1077 (s (read-shell-command
1078 (format "Run program in emulator: (default %s) "
1081 (list default-s '())
1082 (te-parse-program-and-args s))))))
1083 (switch-to-buffer buffer)
1084 (if (null width) (setq width (- (window-width (selected-window)) 1)))
1085 (if (null height) (setq height (- (window-height (selected-window)) 1)))
1087 (setq te-width width te-height height)
1088 (setq mode-line-buffer-identification
1089 (list (format "Emacs terminal %dx%d: %%b " te-width te-height)
1090 'te-pending-output-info))
1091 (let ((buffer-read-only nil))
1094 (while (setq process (get-buffer-process (current-buffer)))
1095 (if (y-or-n-p (format "Kill process %s? " (process-name process)))
1096 (delete-process process)
1097 (error "Process %s not killed" (process-name process)))))
1100 ;; Because of Unix Brain Death(tm), we can't change
1101 ;; the terminal type of a running process, and so
1102 ;; terminal size and scrollability are wired-down
1103 ;; at this point. ("Detach? What's that?")
1104 (concat (format "emacs-virtual:co#%d:li#%d:%s:km:"
1105 ;; Sigh. These can't be dynamically changed.
1106 te-width te-height (if terminal-scrolling
1109 ;; cursor-motion, bol, forward/backward char
1110 "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:"
1111 ;; newline, clear eof/eof, audible bell
1112 "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:"
1113 ;; insert/delete char/line
1114 "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :"
1115 ;;-- Not-widely-known (ie nonstandard) flags, which mean
1116 ;; o writing in the last column of the last line
1117 ;; doesn't cause idiotic scrolling, and
1118 ;; o don't use idiotische c-s/c-q sogenannte
1119 ;; ``flow control'' auf keinen Fall.
1121 ;;-- For stupid or obsolete programs
1122 "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p= :"
1123 ;;-- For disgusting programs.
1124 ;; (VI? What losers need these, I wonder?)
1125 "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:"
1127 (if (face-equal 'terminal-default 'terminal-standout)
1128 "" "so=^p*+1:se=^p*+0")
1129 (if (face-equal 'terminal-default 'terminal-underline)
1130 "" "us=^p*_1:ue=^p*_0")
1132 (if (fboundp 'start-subprocess)
1133 ;; this winning function would do everything, except that
1134 ;; rms doesn't want it.
1135 (setq te-process (start-subprocess "terminal-emulator"
1137 'channel-type 'terminal
1139 'buffer (current-buffer)
1140 'sentinel 'te-sentinel
1142 (list (cons "TERM" "emacs-virtual")
1143 (cons "TERMCAP" termcap))))
1144 ;; so instead we resort to this...
1146 (let ((process-environment
1147 (cons "TERM=emacs-virtual"
1148 (cons (concat "TERMCAP=" termcap)
1149 process-environment))))
1150 (start-process "terminal-emulator"
1153 ;; Yuck!!! Start a shell to set some
1154 ;; terminal control characteristics.
1155 ;; Then exec the program we wanted.
1156 (format "%s; exec %s"
1158 (mapconcat 'te-quote-arg-for-sh
1161 (set-process-filter te-process 'te-filter)
1162 (set-process-sentinel te-process 'te-sentinel)))
1163 (error (fundamental-mode)
1164 (signal (car err) (cdr err))))
1166 (setq inhibit-quit t) ;sport death
1167 (use-local-map terminal-map)
1168 (run-hooks 'terminal-mode-hook)
1169 (message "Entering emacs terminal-emulator... Type %s %s for help"
1170 (single-key-description terminal-escape-char)
1171 (mapconcat 'single-key-description
1172 (where-is-internal 'te-escape-help
1178 (defun te-parse-program-and-args (s)
1179 (cond ((string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ \t]*\\)+\\'" s)
1182 (setq l (cons (if (string-match
1183 "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ \t]+\\)*"
1185 (prog1 (substring s p (match-end 1))
1186 (setq p (match-end 0))
1187 (if (eq p (length s)) (setq p nil)))
1188 (prog1 (substring s p)
1191 (setq l (nreverse l))
1192 (list (car l) (cdr l))))
1193 ((and (string-match "[ \t]" s) (not (file-exists-p s)))
1194 (list shell-file-name (list "-c" (concat "exec " s))))
1197 (put 'terminal-mode 'mode-class 'special)
1198 ;; This is only separated out from function terminal-emulator
1199 ;; to keep the latter a little more managable.
1200 (defun terminal-mode ()
1201 "Set up variables for use of the terminal-emulator.
1202 One should not call this -- it is an internal function
1203 of the terminal-emulator"
1204 (kill-all-local-variables)
1205 (buffer-disable-undo (current-buffer))
1206 (setq major-mode 'terminal-mode)
1207 (setq mode-name "terminal")
1208 ; (make-local-variable 'Helper-return-blurb)
1209 ; (setq Helper-return-blurb "return to terminal simulator")
1210 (setq mode-line-process '(": %s"))
1211 (setq buffer-read-only t)
1212 (setq truncate-lines t)
1213 (make-local-variable 'terminal-escape-char)
1214 (setq terminal-escape-char (default-value 'terminal-escape-char))
1215 (make-local-variable 'terminal-scrolling)
1216 (setq terminal-scrolling (default-value 'terminal-scrolling))
1217 (make-local-variable 'terminal-more-processing)
1218 (setq terminal-more-processing (default-value 'terminal-more-processing))
1219 (make-local-variable 'terminal-redisplay-interval)
1220 (setq terminal-redisplay-interval (default-value 'terminal-redisplay-interval))
1221 (make-local-variable 'te-width)
1222 (make-local-variable 'te-height)
1223 (make-local-variable 'te-process)
1224 (make-local-variable 'te-pending-output)
1225 (setq te-pending-output (list 0))
1226 (make-local-variable 'te-saved-point)
1227 (setq te-saved-point (point-min))
1228 (make-local-variable 'te-pending-output-info) ;for the mode line
1229 (setq te-pending-output-info "")
1230 (make-local-variable 'inhibit-quit)
1231 ;(setq inhibit-quit t)
1232 (make-local-variable 'te-log-buffer)
1233 (setq te-log-buffer nil)
1234 (make-local-variable 'te-more-count)
1235 (setq te-more-count -1)
1236 (make-local-variable 'te-redisplay-count)
1237 (setq te-redisplay-count terminal-redisplay-interval)
1238 (make-local-variable 'te-current-face)
1239 (setq te-current-face 'terminal-default)
1240 (make-local-variable 'te-current-attributes)
1241 (setq te-current-attributes (list (cons 'standout nil)
1242 (cons 'underline nil)))
1243 ;(use-local-map terminal-mode-map)
1244 ;; terminal-mode-hook is called above in function terminal-emulator
1245 (make-local-variable 'meta-prefix-char)
1246 (setq meta-prefix-char -1) ;death to ASCII lossage
1249 ;;;; what a complete loss
1251 (defun te-quote-arg-for-sh (fuckme)
1252 (cond ((string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'"
1255 ((not (string-match "[$]" fuckme))
1256 ;; "[\"\\]" are special to sh and the lisp reader in the same way
1257 (prin1-to-string fuckme))
1262 (while (cond ((>= cretin (length fuckme))
1264 ;; this is the set of chars magic with "..." in `sh'
1265 ((setq stupid (string-match "[\"\\$]"
1268 (t (setq harder (concat harder
1269 (substring fuckme cretin)))
1271 (setq harder (concat harder (substring fuckme cretin stupid)
1272 ;; Can't use ?\\ since `concat'
1273 ;; unfortunately does prin1-to-string
1274 ;; on fixna. Amazing.
1279 cretin (1+ stupid)))
1280 (concat "\"" harder "\"")))))