1 ;;; terminal.el --- terminal emulator for GNU Emacs.
3 ;; Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
7 ;; Keywords: comm, terminals
9 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;;>> ** Nothing can be done about emacs' meta-lossage **
30 ;;>> (without redoing keymaps `sanely' -- ask Mly for details)
32 ;;>> One probably wants to do setenv MORE -c when running with
33 ;;>> more-processing enabled.
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
43 This variable is local to each terminal-emulator buffer.")
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
49 This variable is local to each terminal-emulator buffer.")
51 (defvar terminal-more-processing t
52 "*If non-nil, do more-processing.
53 This variable is local to each terminal-emulator buffer.")
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
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
65 (defvar terminal-more-break-insertion
66 "*** More break -- Press space to continue ***")
68 (defvar terminal-map 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
)))
77 (defvar terminal-escape-map nil
)
78 (if terminal-escape-map
80 (let ((map (make-keymap)))
81 ;(fillarray map 'te-escape-extended-command-unread)
82 (fillarray (car (cdr map
)) 'undefined
)
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
)))
100 (defvar te-escape-command-alist nil
)
101 (if te-escape-command-alist
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?
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
)
124 (defvar terminal-more-break-map nil
)
125 (if terminal-more-break-map
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
)
137 (setq terminal-more-break-map map
)))
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
)
161 (local (current-local-map))
162 (global (current-global-map)))
165 (use-global-map terminal-escape-map
)
166 (use-local-map terminal-escape-map
)
167 (setq s
(read-key-sequence
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
))
175 (cond ((string= s
(make-string 1 terminal-escape-char
))
176 (setq last-command-char terminal-escape-char
)
177 (let ((terminal-escape-char -
259))
179 ((setq s
(lookup-key terminal-escape-map s
))
180 (call-interactively s
)))))
182 (defun te-escape-help ()
183 "Provide help on commands available after terminal-escape-char is typed."
185 (message "Terminal emulator escape help...")
186 (let ((char (single-key-description terminal-escape-char
)))
189 (princ (format "Terminal-emulator escape, invoked by \"%s\"
190 Type \"%s\" twice to send a single \"%s\" through.
192 Other chars following \"%s\" are interpreted as follows:\n"
193 char char char char
))
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
)
203 (sort (copy-sequence te-escape-command-alist
)
204 (function (lambda (a b
)
205 (string< (car a
) (car b
))))))))
207 (let ((doc (or (documentation (cdr (car l
)))
209 (if (string-match "\n" doc
)
210 ;; just use first line of documentation
211 (setq doc
(substring doc
0 (match-beginning 0))))
213 (princ (car (car l
)))
222 (defun te-escape-extended-command ()
224 (let ((c (let ((completion-ignore-case t
))
225 (completing-read "terminal command: "
226 te-escape-command-alist
230 (setq c
(downcase c
))
231 (let ((l te-escape-command-alist
))
233 (if (string= c
(downcase (car (car l
))))
234 (throw 'foo
(call-interactively (cdr (car l
))))
235 (setq l
(cdr l
)))))))))
238 (defun te-escape-extended-command-unread ()
240 (setq unread-command-events
(listify-key-sequence (this-command-keys)))
241 (te-escape-extended-command))
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
))
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
)))
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.
259 Very poor man's file transfer protocol."
260 (interactive "sStuff string: ")
261 (process-send-string te-process string
))
263 (defun te-set-output-log (name)
264 "Record output from the terminal emulator in a buffer."
265 (interactive (list (if te-log-buffer
267 (read-buffer "Record output in buffer: "
268 (format "%s output-log"
269 (buffer-name (current-buffer)))
271 (if (or (null name
) (equal name
""))
272 (progn (setq te-log-buffer nil
)
273 (message "Output logging off."))
274 (if (get-buffer name
)
277 (set-buffer (get-buffer-create name
))
279 (buffer-disable-undo (current-buffer))
281 (setq te-log-buffer
(get-buffer name
))
282 (message "Recording terminal emulator output into buffer \"%s\""
283 (buffer-name te-log-buffer
))))
286 "Discontinue output log."
288 (te-set-output-log nil
))
291 (defun te-toggle (sym arg
)
292 (set sym
(cond ((not (numberp arg
)) arg
)
293 ((= arg
1) (not (symbol-value sym
)))
297 (defun te-toggle-more-processing (arg)
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)))
303 (defun te-toggle-scrolling (arg)
305 (message (if (te-toggle 'terminal-scrolling arg
)
306 "Scroll at end of page" "Wrap at end of page")))
308 (defun te-enable-more-processing ()
309 "Enable ** MORE ** processing"
311 (te-toggle-more-processing t
))
313 (defun te-disable-more-processing ()
314 "Disable ** MORE ** processing"
316 (te-toggle-more-processing nil
))
318 (defun te-do-scrolling ()
319 "Scroll at end of page (yuck)"
321 (te-toggle-scrolling t
))
323 (defun te-do-wrapping ()
324 "Wrap to top of window at end of page"
326 (te-toggle-scrolling nil
))
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))
340 ;; every command -must- call te-more-break-unwind
341 ;; or grave lossage will result
343 (put 'te-more-break-unread
'suppress-keymap t
)
344 (defun te-more-break-unread ()
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
))
356 (defun te-more-break-resume ()
357 "Proceed past the **MORE** break,
358 allowing the next page of output to appear"
360 (message "Continuing from more break")
361 (te-more-break-unwind))
363 (defun te-more-break-help ()
364 "Provide help on commands available in a terminal-emulator **MORE** break"
366 (message "Terminal-emulator more break help...")
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.")
382 (defun te-more-break-advance-one-line ()
383 "Allow one more line of text to be output before doing another more break."
385 (setq te-more-count
1)
386 (te-more-break-unwind))
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."
392 (te-more-break-unwind)
393 (te-flush-pending-output))
395 (defun te-flush-pending-output ()
396 "Discard any as-yet-unprocessed output which has been received by
397 the terminal emulator."
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"
408 (te-update-pending-output-display)
409 (te-process-output nil
)
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."
419 (cond ((= last-input-char terminal-escape-char
)
420 (call-interactively 'te-escape
))
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
))))
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)))
441 (defun te-pending-output-length ()
442 (let ((length (car te-pending-output
))
443 (tem (cdr te-pending-output
)))
445 (setq length
(+ length
(length (car tem
))) tem
(cdr tem
)))
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...
452 (defvar terminal-edit-map nil
)
453 (if terminal-edit-map
455 (setq terminal-edit-map
(make-sparse-keymap))
456 (define-key terminal-edit-map
"\C-c\C-c" 'terminal-cease-edit
))
458 ;; Terminal Edit mode is suitable only for specially formatted data.
459 (put 'terminal-edit-mode
'mode-class
'special
)
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
))
473 "Start editing the terminal emulator buffer with ordinary Emacs commands."
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"))))
483 (defun terminal-cease-edit ()
484 "Finish editing message; switch back to Terminal proper."
487 ;;>> emulator will blow out if buffer isn't exactly te-width x te-height
488 (let ((buffer-read-only nil
))
490 (let ((opoint (point-marker))
493 (goto-char (point-min))
496 (cond ((search-forward "\n" (+ p width
) 'move
)
498 (insert-char ?\
(- width
(- (point) p
)))
501 (insert-char ?\
(- width
(- (point) p
))))
502 ((= (following-char) ?
\n)
506 (if (search-forward "\n" nil t
)
507 (delete-region p
(1- (point)))
508 (delete-region p
(point-max))))))
510 (if (not (eobp)) (delete-region (point) (point-max)))
511 (if (eobp) (insert ?
\n)))
514 (set-marker opoint nil nil
)
515 (setq te-saved-point
(point))
516 (setq te-redisplay-count
0)
517 (setq te-more-count -
1)))
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")))
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
540 (set-process-filter te-process
541 (function (lambda (process string
)
543 (set-buffer (process-buffer process
))
544 (setq te-pending-output
(nconc te-pending-output
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 "))
551 (null terminal-more-break-insertion
)
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
))
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
))
568 (terminal-more-break-insertion
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))
578 (te-process-output t
))
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
)))
593 ;; migrate back towards top (ie bottom) of screen.
594 (setq te-more-count
(- te-height
595 (if (> te-height
10) 2 1)))))))
598 ;;;; More or less straight-forward terminal escapes
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.)
604 "Move down a line, optionally do more processing, perhaps wrap/scroll,
605 move to start of new line, clear to 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
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))
622 (delete-region (point) (+ (point) te-width
)))
623 (insert-char ?\ te-width
)
625 (te-set-window-start))
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
)
635 (goto-char (+ (point-min) x
(* y
(1+ te-width
))))
636 ;(te-set-window-start?)
638 (setq te-more-count -
1))
643 (defun te-clear-rest-of-line ()
645 (let ((n (- (point) (progn (end-of-line) (point)))))
646 (delete-region (point) (+ (point) n
))
647 (insert-char ?\
(- n
)))))
651 (defun te-clear-rest-of-screen ()
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
))))
661 (defun te-clear-screen ()
662 ;; regenerate buffer to compensate for (nonexistent!!) bugs.
665 (while (< i te-height
)
667 (insert-char ?\ te-width
)
669 (delete-region (1- (point-max)) (point-max))
670 (goto-char (point-min))
671 (setq te-more-count -
1))
675 (defun te-insert-lines ()
679 (let* ((line (- te-height
(/ (- (point) (point-min)) (1+ te-width
)) -
1))
680 (n (min (- (te-get-char) ?\
) line
))
682 (delete-region (- (point-max) (* n
(1+ te-width
))) (point-max))
683 (if (eql (point) (point-max)) (insert ?
\n))
686 (insert-char ?\ te-width
)
687 (or (eql i line
) (insert ?
\n))))))
688 (setq te-more-count -
1))
692 (defun te-delete-lines ()
695 (let* ((line (- te-height
(/ (- (point) (point-min)) (1+ te-width
)) -
1))
696 (n (min (- (te-get-char) ?\
) line
))
698 (delete-region (point)
699 (min (+ (point) (* n
(1+ te-width
))) (point-max)))
701 (goto-char (point-max))
704 (insert-char ?\ te-width
)
705 (or (eql i line
) (insert ?
\n))))))
706 (setq te-more-count -
1))
709 (defun te-beginning-of-line ()
713 (defun te-backward-char ()
718 (defun te-forward-char ()
727 (delete-region (1- (point)) (point))
737 (defun te-insert-spaces ()
739 (n (min (- (te-get-char) 32)
740 (- (progn (end-of-line) (point)) p
))))
748 ;; ^p d count+32 (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
749 (defun te-delete-char ()
751 (n (min (- (te-get-char) 32)
752 (- (progn (end-of-line) (point)) p
))))
762 ;; disgusting unix-required shit
763 ;; Are we living twenty years in the past yet?
765 (defun te-losing-unix ()
769 (defun te-output-tab ()
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
))))
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)))
787 (delete-region (point-min) (+ (point-min) te-width
))
788 (goto-char (point-min))
790 (goto-char (point-max))
792 (insert-char ?\ te-width
)
795 (move-to-column column
))
796 (te-set-window-start))
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)
805 (defun te-filter (process string
)
806 (let* ((obuf (current-buffer))
808 ;; can't use save-excursion, as that preserves point, which we don't want
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
))
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.
828 (te-process-output (eq (current-buffer)
829 (window-buffer (selected-window)))))
830 (set-buffer (process-buffer process
))
831 (setq te-saved-point
(point)))
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
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
)
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
)))
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))
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
)))
865 (delete-char 1) (insert char
)
866 (te-redisplay-if-necessary 1))
868 (let ((end (or (and (eq ostring string
) matchpos
)
869 (setq matchpos
(string-match
870 "[\000-\037\177-\377]"
873 (delete-char 1) (insert char
)
874 (setq char
(point)) (end-of-line)
875 (setq end
(min end
(+ start
(- (point) 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
)))
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)
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
)
908 (?\C-f . te-forward-char
)
910 (?\C-j . te-down-vertically-or-scroll
)
911 (?\C-l . te-clear-screen
)
915 '((?\C-j . te-newline
)
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
)
923 (?\C-h . te-backward-char
)
924 (?\C-i . te-output-tab
))))
926 (te-redisplay-if-necessary 1))
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))
934 (while (not (eq s
(setq w
(next-window w
))))
935 (if (eq (window-buffer w
) (current-buffer))
936 (set-window-point w
(point))))))
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
))))
947 (let ((filter (process-filter te-process
)))
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
))))))
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)
965 (setq te-redisplay-count terminal-redisplay-interval
))))
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)))
972 (setq te-pending-output-info
"")
973 (setq te-pending-output-info
(format "(%dK chars output pending) "
974 (/ (+ length
512) 1024))))))
976 (set-buffer-modified-p (buffer-modified-p)))
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)))
984 (set-buffer (process-buffer process
))
985 (setq buffer-read-only nil
)
987 (goto-char (point-max))
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))
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.
1001 (defvar explicit-shell-file-name nil
1002 "*If non-nil, is file name to use for explicitly requested inferior shell.")
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.
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.
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'.
1023 `Meta' characters may not currently be sent through the terminal emulator.
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.
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
1034 Presently with `termcap' only; if somebody sends us code to make this
1035 work with `terminfo' we will try to use it."
1037 (cons (save-excursion
1038 (set-buffer (get-buffer-create "*terminal*"))
1039 (buffer-name (if (or (not (boundp 'te-process
))
1041 (not (eq (process-status te-process
)
1044 (generate-new-buffer "*terminal*"))))
1047 ;; Default shell is same thing M-x shell uses.
1048 (or explicit-shell-file-name
1053 (format "Run program in emulator: (default %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)))
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
))
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
)))))
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
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.
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
))))
1106 (start-process "terminal-emulator" (current-buffer)
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"
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
))))
1121 (if (default-value 'meta-flag
)
1123 "Note: Meta key disabled due to maybe-eventually-reparable braindamage")
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
1137 (defun te-parse-program-and-args (s)
1138 (cond ((string-match "\\`\\([a-zA-Z0-9-+=_.@/:]+[ \t]*\\)+\\'" s
)
1141 (setq l
(cons (if (string-match
1142 "\\([a-zA-Z0-9-+=_.@/:]+\\)\\([ \t]+\\)*"
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
)
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
))))
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
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
1205 ;;;; what a complete loss
1207 (defun te-quote-arg-for-sh (string)
1208 (cond ((string-match "\\`[a-zA-Z0-9-+=_.@/:]+\\'"
1211 ((not (string-match "[$]" string
))
1212 ;; "[\"\\]" are special to sh and the lisp reader in the same way
1213 (prin1-to-string string
))
1218 (while (cond ((>= start
(length string
))
1220 ;; this is the set of chars magic with "..." in `sh'
1221 ((setq end
(string-match "[\"\\$]"
1224 (t (setq harder
(concat harder
1225 (substring string start
)))
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.
1236 (concat "\"" harder
"\"")))))
1240 ;;; terminal.el ends here