1 ;;; terminal.el --- terminal emulator for GNU Emacs
3 ;; Copyright (C) 1986-1989, 1993-1994, 2001-2018 Free Software
6 ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Obsolete-since: 24.4
9 ;; Keywords: comm, terminals
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
28 ;; This file has been censored by the Communications Decency Act.
29 ;; That law was passed under the guise of a ban on pornography, but
30 ;; it bans far more than that. This file did not contain pornography,
31 ;; but it was censored nonetheless.
33 ;; For information on US government censorship of the Internet, and
34 ;; what you can do to bring back freedom of the press, see the web
35 ;; site http://www.vtw.org/
40 ;;>> ** Nothing can be done about emacs' meta-lossage **
41 ;;>> (without redoing keymaps `sanely' -- ask Mly for details)
43 ;;>> One probably wants to do setenv MORE -c when running with
44 ;;>> more-processing enabled.
48 (defgroup terminal nil
49 "Terminal emulator for Emacs."
53 (defcustom terminal-escape-char ?\C-^
54 "All characters except for this are passed verbatim through the
55 terminal-emulator. This character acts as a prefix for commands
56 to the emulator program itself. Type this character twice to send
57 it through the emulator. Type ? after typing it for a list of
59 This variable is local to each terminal-emulator buffer."
63 (defcustom terminal-scrolling t
;;>> Setting this to t sort-of defeats my whole aim in writing this package...
64 "If non-nil, the terminal-emulator will losingly `scroll' when output occurs
65 past the bottom of the screen. If nil, output will win and `wrap' to the top
67 This variable is local to each terminal-emulator buffer."
71 (defcustom terminal-more-processing t
72 "If non-nil, do more-processing.
73 This variable is local to each terminal-emulator buffer."
77 ;; If you are the sort of loser who uses scrolling without more breaks
78 ;; and expects to actually see anything, you should probably set this to
80 (defcustom terminal-redisplay-interval
5000
81 "Maximum number of characters which will be processed by the
82 terminal-emulator before a screen redisplay is forced.
83 Set this to a large value for greater throughput,
84 set it smaller for more frequent updates but overall slower
89 (defvar terminal-more-break-insertion
90 "*** More break -- Press space to continue ***")
92 (defvar terminal-meta-map nil
)
95 (let ((map (make-sparse-keymap)))
96 (define-key map
[t] 'te-pass-through)
97 (setq terminal-meta-map map)))
99 (defvar terminal-map nil)
102 (let ((map (make-sparse-keymap)))
103 ;; Prevent defining [menu-bar] as te-pass-through
104 ;; so we allow the global menu bar to be visible.
105 (define-key map [menu-bar] (make-sparse-keymap))
106 (define-key map [t] 'te-pass-through
)
107 (define-key map
[switch-frame
] 'handle-switch-frame
)
108 (define-key map
"\e" terminal-meta-map
)
109 ;;(define-key map "\C-l"
110 ;; (lambda () (interactive) (te-pass-through) (redraw-display)))
111 (setq terminal-map map
)))
113 (defvar terminal-escape-map nil
)
114 (if terminal-escape-map
116 (let ((map (make-sparse-keymap)))
117 (define-key map
[t] 'undefined)
119 (while (<= (aref s 0) ?9)
120 (define-key map s 'digit-argument)
121 (aset s 0 (1+ (aref s 0)))))
122 (define-key map "b" 'switch-to-buffer)
123 (define-key map "o" 'other-window)
124 (define-key map "e" 'te-set-escape-char)
125 (define-key map "\C-l" 'redraw-display)
126 (define-key map "\C-o" 'te-flush-pending-output)
127 (define-key map "m" 'te-toggle-more-processing)
128 (define-key map "x" 'te-escape-extended-command)
129 ;;>> What use is this? Why is it in the default terminal-emulator map?
130 (define-key map "w" 'te-edit)
131 (define-key map "?" 'te-escape-help)
132 (define-key map (char-to-string help-char) 'te-escape-help)
133 (setq terminal-escape-map map)))
135 (defvar te-escape-command-alist nil)
136 (if te-escape-command-alist
138 (setq te-escape-command-alist
139 '(("Set Escape Character" . te-set-escape-char)
140 ;;>> What use is this? Why is it in the default terminal-emulator map?
142 ("Refresh" . redraw-display)
143 ("Record Output" . te-set-output-log)
144 ("Photo" . te-set-output-log)
145 ("Tofu" . te-tofu) ;; confuse the uninitiated
146 ("Stuff Input" . te-stuff-string)
147 ("Flush Pending Output" . te-flush-pending-output)
148 ("Enable More Processing" . te-enable-more-processing)
149 ("Disable More Processing" . te-disable-more-processing)
150 ("Scroll at end of page" . te-do-scrolling)
151 ("Wrap at end of page" . te-do-wrapping)
152 ("Switch To Buffer" . switch-to-buffer)
153 ("Other Window" . other-window)
154 ("Kill Buffer" . kill-buffer)
155 ("Help" . te-escape-help)
156 ("Set Redisplay Interval" . te-set-redisplay-interval)
159 (defvar terminal-more-break-map nil)
160 (if terminal-more-break-map
162 (let ((map (make-sparse-keymap)))
163 (define-key map [t] 'te-more-break-unread
)
164 (define-key map
(char-to-string help-char
) 'te-more-break-help
)
165 (define-key map
" " 'te-more-break-resume
)
166 (define-key map
"\C-l" 'redraw-display
)
167 (define-key map
"\C-o" 'te-more-break-flush-pending-output
)
168 ;;>>> this isn't right
169 ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
170 (define-key map
"\r" 'te-more-break-advance-one-line
)
172 (setq terminal-more-break-map map
)))
175 ;;; Pacify the byte compiler
176 (defvar te-process nil
)
177 (defvar te-log-buffer nil
)
178 (defvar te-height nil
)
179 (defvar te-width nil
)
180 (defvar te-more-count nil
)
181 (defvar te-redisplay-count nil
)
182 (defvar te-pending-output nil
)
183 (defvar te-saved-point
)
184 (defvar te-more-old-point nil
)
185 (defvar te-more-old-local-map nil
)
186 (defvar te-more-old-filter nil
)
187 (defvar te-more-old-mode-line-format nil
)
188 (defvar te-pending-output-info nil
)
190 ;; Required to support terminfo systems
191 (defconst te-terminal-name-prefix
"emacs-em"
192 "Prefix used for terminal type names for Terminfo.")
193 (defconst te-terminfo-directory
194 (file-name-as-directory
195 (expand-file-name "emacs-terminfo" temporary-file-directory
))
196 "Directory used for run-time terminal definition files for Terminfo.")
197 (defvar te-terminal-name nil
)
204 (local (current-local-map))
205 (global (current-global-map)))
208 (use-global-map terminal-escape-map
)
209 (use-local-map terminal-escape-map
)
210 (setq s
(read-key-sequence
211 (if current-prefix-arg
212 (format "Emacs Terminal escape[%s for help]> %d "
213 (substitute-command-keys
214 "\\<terminal-escape-map>\\[te-escape-help]")
215 (prefix-numeric-value current-prefix-arg
))
216 (format "Emacs Terminal escape[%s for help]> "
217 (substitute-command-keys
218 "\\<terminal-escape-map>\\[te-escape-help]"))))))
219 (use-global-map global
)
220 (use-local-map local
))
225 ;; Certain keys give vector notation, like [escape] when
226 ;; you hit esc key...
228 (string= s
(make-string 1 terminal-escape-char
)))
229 (setq last-command-event terminal-escape-char
)
230 (let ((terminal-escape-char -
259))
233 ((setq s
(lookup-key terminal-escape-map s
))
234 (call-interactively s
)))
239 (defun te-escape-help ()
240 "Provide help on commands available after terminal-escape-char is typed."
242 (message "Terminal emulator escape help...")
243 (let ((char (single-key-description terminal-escape-char
)))
246 (princ (format "Terminal-emulator escape, invoked by \"%s\"
247 Type \"%s\" twice to send a single \"%s\" through.
249 Other chars following \"%s\" are interpreted as follows:\n"
250 char char char char
))
252 (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
253 (princ (format "\nSubcommands of \"%s\" (%s)\n"
254 (where-is-internal 'te-escape-extended-command
255 terminal-escape-map t
)
256 'te-escape-extended-command
))
257 (let ((l (sort (copy-sequence te-escape-command-alist
)
258 (function (lambda (a b
)
259 (string< (car a
) (car b
)))))))
261 (let ((doc (or (documentation (cdr (car l
)))
263 (if (string-match "\n" doc
)
264 ;; just use first line of documentation
265 (setq doc
(substring doc
0 (match-beginning 0))))
267 (princ (car (car l
)))
276 (defun te-escape-extended-command ()
278 (let ((c (let ((completion-ignore-case t
))
279 (completing-read "terminal command: "
280 te-escape-command-alist
284 (setq c
(downcase c
))
285 (let ((l te-escape-command-alist
))
287 (if (string= c
(downcase (car (car l
))))
288 (throw 'foo
(call-interactively (cdr (car l
))))
289 (setq l
(cdr l
)))))))))
292 (defun te-escape-extended-command-unread ()
294 (setq unread-command-events
295 (nconc (listify-key-sequence (this-command-keys))
296 unread-command-events
))
297 (te-escape-extended-command))
299 (defun te-set-escape-char (c)
300 "Change the terminal-emulator escape character."
301 (interactive "cSet escape character to: ")
302 (let ((o terminal-escape-char
))
304 "\"%s\" is the escape char"
305 "\"%s\" is now the escape; \"%s\" passes through")
306 (single-key-description c
)
307 (single-key-description o
))
308 (setq terminal-escape-char c
)))
311 (defun te-stuff-string (string)
312 "Read a string to send to through the terminal emulator
313 as though that string had been typed on the keyboard.
315 Very poor man's file transfer protocol."
316 (interactive "sStuff string: ")
317 (process-send-string te-process string
))
319 (defun te-set-output-log (name)
320 "Record output from the terminal emulator in a buffer."
321 (interactive (list (if te-log-buffer
323 (read-buffer "Record output in buffer: "
324 (format "%s output-log"
325 (buffer-name (current-buffer)))
327 (if (or (null name
) (equal name
""))
328 (progn (setq te-log-buffer nil
)
329 (message "Output logging off."))
330 (if (get-buffer name
)
332 (with-current-buffer (get-buffer-create name
)
334 (buffer-disable-undo (current-buffer))
336 (setq te-log-buffer
(get-buffer name
))
337 (message "Recording terminal emulator output into buffer \"%s\""
338 (buffer-name te-log-buffer
))))
341 "Discontinue output log."
343 (te-set-output-log nil
))
346 (defun te-toggle (sym arg
)
347 (set sym
(cond ((not (numberp arg
)) arg
)
348 ((= arg
1) (not (symbol-value sym
)))
352 (defun te-toggle-more-processing (arg)
354 (message (if (te-toggle 'terminal-more-processing arg
)
355 "More processing on" "More processing off"))
356 (if terminal-more-processing
(setq te-more-count -
1)))
358 (defun te-toggle-scrolling (arg)
360 (message (if (te-toggle 'terminal-scrolling arg
)
361 "Scroll at end of page" "Wrap at end of page")))
363 (defun te-enable-more-processing ()
364 "Enable ** MORE ** processing"
366 (te-toggle-more-processing t
))
368 (defun te-disable-more-processing ()
369 "Disable ** MORE ** processing"
371 (te-toggle-more-processing nil
))
373 (defun te-do-scrolling ()
374 "Scroll at end of page (yuck)"
376 (te-toggle-scrolling t
))
378 (defun te-do-wrapping ()
379 "Wrap to top of window at end of page"
381 (te-toggle-scrolling nil
))
384 (defun te-set-redisplay-interval (arg)
385 "Set the maximum interval (in output characters) between screen updates.
386 Set this number to large value for greater throughput,
387 set it smaller for more frequent updates (but overall slower performance."
388 (interactive "NMax number of output chars between redisplay updates: ")
389 (setq arg
(max arg
1))
390 (setq terminal-redisplay-interval arg
391 te-redisplay-count
0))
395 ;; every command -must- call te-more-break-unwind
396 ;; or grave lossage will result
398 (put 'te-more-break-unread
'suppress-keymap t
)
399 (defun te-more-break-unread ()
401 (if (eq last-input-event terminal-escape-char
)
402 (call-interactively 'te-escape
)
403 (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
404 (single-key-description last-input-event
)
405 (te-pending-output-length))
406 (setq te-more-count
259259)
407 (te-more-break-unwind)
408 (let ((terminal-more-processing nil
))
411 (defun te-more-break-resume ()
412 "Proceed past the **MORE** break,
413 allowing the next page of output to appear"
415 (message "Continuing from more break")
416 (te-more-break-unwind))
418 (defun te-more-break-help ()
419 "Provide help on commands available in a terminal-emulator **MORE** break"
421 (message "Terminal-emulator more break help...")
425 (princ "Terminal-emulator more break.\n\n")
426 (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
427 (where-is-internal 'te-more-break-resume
428 terminal-more-break-map t
)
429 (documentation 'te-more-break-resume
)))
430 (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
431 (princ "Any other key is passed through to the program
432 running under the terminal emulator and disables more processing until
433 all pending output has been dealt with.")
437 (defun te-more-break-advance-one-line ()
438 "Allow one more line of text to be output before doing another more break."
440 (setq te-more-count
1)
441 (te-more-break-unwind))
443 (defun te-more-break-flush-pending-output ()
444 "Discard any output which has been received by the terminal emulator but
445 not yet processed and then proceed from the more break."
447 (te-more-break-unwind)
448 (te-flush-pending-output))
450 (defun te-flush-pending-output ()
451 "Discard any as-yet-unprocessed output which has been received by
452 the terminal emulator."
454 ;; this could conceivably be confusing in the presence of
455 ;; escape-sequences spanning process-output chunks
456 (if (null (cdr te-pending-output
))
457 (message "(There is no output pending)")
458 (let ((length (te-pending-output-length)))
459 (message "Flushing %d chars of pending output" length
)
460 (setq te-pending-output
461 (list 0 (format "\n*** %d chars of pending output flushed ***\n"
463 (te-update-pending-output-display)
464 (te-process-output nil
)
468 (defun te-pass-through ()
469 "Character is passed to the program running under the terminal emulator.
470 One characters is treated specially:
471 the terminal escape character (normally C-^)
472 lets you type a terminal emulator command."
474 (cond ((eq last-input-event terminal-escape-char
)
475 (call-interactively 'te-escape
))
477 ;; Convert `return' to C-m, etc.
478 (if (and (symbolp last-input-event
)
479 (get last-input-event
'ascii-character
))
480 (setq last-input-event
(get last-input-event
'ascii-character
)))
481 ;; Convert meta characters to 8-bit form for transmission.
482 (if (and (integerp last-input-event
)
483 (not (zerop (logand last-input-event ?\M-\^
@))))
484 (setq last-input-event
(+ 128 (logand last-input-event
127))))
485 ;; Now ignore all but actual characters.
486 ;; (It ought to be possible to send through function
487 ;; keys as character sequences if we add a description
488 ;; to our termcap entry of what they should look like.)
489 (if (integerp last-input-event
)
491 (and terminal-more-processing
(null (cdr te-pending-output
))
492 (te-set-more-count nil
))
493 (process-send-string te-process
(make-string 1 last-input-event
))
494 (te-process-output t
))
495 (message "Function key `%s' ignored"
496 (single-key-description last-input-event
))))))
499 (defun te-set-window-start ()
500 (let* ((w (get-buffer-window (current-buffer)))
501 (h (if w
(window-height w
))))
502 (cond ((not w
)) ; buffer not displayed
503 ((>= h
(/ (- (point) (point-min)) (1+ te-width
)))
504 ;; this is the normal case
505 (set-window-start w
(point-min)))
506 ;; this happens if some vandal shrinks our window.
507 ((>= h
(/ (- (point-max) (point)) (1+ te-width
)))
508 (set-window-start w
(- (point-max) (* h
(1+ te-width
)) -
1)))
512 (defun te-pending-output-length ()
513 (let ((length (car te-pending-output
))
514 (tem (cdr te-pending-output
)))
516 (setq length
(+ length
(length (car tem
))) tem
(cdr tem
)))
519 ;;>> What use is this terminal-edit stuff anyway?
520 ;;>> If nothing else, it was written by somebody who didn't
521 ;;>> competently understand the terminal-emulator...
523 (defvar terminal-edit-map nil
)
524 (if terminal-edit-map
526 (setq terminal-edit-map
(make-sparse-keymap))
527 (define-key terminal-edit-map
"\C-c\C-c" 'terminal-cease-edit
))
529 ;; Terminal Edit mode is suitable only for specially formatted data.
530 (put 'terminal-edit-mode
'mode-class
'special
)
532 (defun terminal-edit-mode ()
533 "Major mode for editing the contents of a terminal-emulator buffer.
534 The editing commands are the same as in Fundamental mode,
535 together with a command \\<terminal-edit-map>to return to terminal emulation: \\[terminal-cease-edit]."
536 (use-local-map terminal-edit-map
)
537 (setq major-mode
'terminal-edit-mode
)
538 (setq mode-name
"Terminal Edit")
539 (setq mode-line-modified
(default-value 'mode-line-modified
))
540 (setq mode-line-process nil
)
541 (run-mode-hooks 'terminal-edit-mode-hook
))
544 "Start editing the terminal emulator buffer with ordinary Emacs commands."
547 (force-mode-line-update)
548 ;; Make mode line update.
549 (if (eq (key-binding "\C-c\C-c") 'terminal-cease-edit
)
550 (message "Editing: Type C-c C-c to return to Terminal")
552 (substitute-command-keys
553 "Editing: Type \\[terminal-cease-edit] to return to Terminal"))))
555 (defun terminal-cease-edit ()
556 "Finish editing message; switch back to Terminal proper."
559 ;;>> emulator will blow out if buffer isn't exactly te-width x te-height
560 (let ((buffer-read-only nil
))
562 (let ((opoint (point-marker))
565 (goto-char (point-min))
568 (cond ((search-forward "\n" (+ p width
) 'move
)
570 (insert-char ?\s
(- width
(- (point) p
)))
573 (insert-char ?\s
(- width
(- (point) p
))))
574 ((= (following-char) ?
\n)
578 (if (search-forward "\n" nil t
)
579 (delete-region p
(1- (point)))
580 (delete-region p
(point-max))))))
582 (if (not (eobp)) (delete-region (point) (point-max)))
583 (if (eobp) (insert ?
\n)))
586 (set-marker opoint nil nil
)
587 (setq te-saved-point
(point))
588 (setq te-redisplay-count
0)
589 (setq te-more-count -
1)))
591 (setq mode-line-modified
(default-value 'mode-line-modified
))
592 (use-local-map terminal-map
)
593 (setq major-mode
'terminal-mode
)
594 (setq mode-name
"terminal")
595 (setq mode-line-process
'(":%s")))
599 (defun te-more-break ()
600 (te-set-more-count t
)
601 (make-local-variable 'te-more-old-point
)
602 (setq te-more-old-point
(point))
603 (make-local-variable 'te-more-old-local-map
)
604 (setq te-more-old-local-map
(current-local-map))
605 (use-local-map terminal-more-break-map
)
606 (make-local-variable 'te-more-old-filter
)
607 (setq te-more-old-filter
(process-filter te-process
))
608 (make-local-variable 'te-more-old-mode-line-format
)
609 (setq te-more-old-mode-line-format mode-line-format
610 mode-line-format
(list "-- **MORE** "
611 mode-line-buffer-identification
613 (set-process-filter te-process
614 (function (lambda (process string
)
615 (with-current-buffer (process-buffer process
)
616 (setq te-pending-output
(nconc te-pending-output
618 (te-update-pending-output-display))))
619 (te-update-pending-output-display)
620 (if (eq (window-buffer (selected-window)) (current-buffer))
621 (message "More break "))
623 (null terminal-more-break-insertion
)
626 (delete-region (point) (+ (point) te-width
))
627 (insert terminal-more-break-insertion
)))
628 (run-hooks 'terminal-more-break-hook
)
629 (sit-for 0) ;get display to update
630 (throw 'te-process-output t
))
632 (defun te-more-break-unwind ()
633 (use-local-map te-more-old-local-map
)
634 (set-process-filter te-process te-more-old-filter
)
635 (goto-char te-more-old-point
)
636 (setq mode-line-format te-more-old-mode-line-format
)
637 (force-mode-line-update)
638 (let ((buffer-read-only nil
))
640 (terminal-more-break-insertion
642 (delete-region (point)
643 (+ (point) (length terminal-more-break-insertion
)))
644 (insert-char ?\s te-width
)
645 (goto-char te-more-old-point
)))
646 (setq te-more-old-point nil
)
647 (let ((te-more-count 259259))
650 (te-process-output t
))
652 (defun te-set-more-count (newline)
653 (let ((line (/ (- (point) (point-min)) (1+ te-width
))))
654 (if newline
(setq line
(1+ line
)))
655 (cond ((= line te-height
)
656 (setq te-more-count te-height
))
657 ;>>>> something is strange. Investigate this!
658 ((= line
(1- te-height
))
659 (setq te-more-count te-height
))
660 ((or (< line
(/ te-height
2))
661 (> (- te-height line
) 10))
662 ;; break at end of this page
663 (setq te-more-count
(- te-height line
)))
665 ;; migrate back towards top (ie bottom) of screen.
666 (setq te-more-count
(- te-height
667 (if (> te-height
10) 2 1)))))))
670 ;;;; More or less straight-forward terminal escapes
672 ;; ^j, meaning `newline' to non-display programs.
673 ;; (Who would think of ever writing a system which doesn't understand
674 ;; display terminals natively? Un*x: The Operating System of the Future.)
676 "Move down a line, optionally do more processing, perhaps wrap/scroll,
677 move to start of new line, clear to end of line."
679 (cond ((not terminal-more-processing
))
680 ((< (setq te-more-count
(1- te-more-count
)) 0)
681 (te-set-more-count t
))
682 ((eq te-more-count
0)
683 ;; this doesn't return
687 (delete-region (point-min) (+ (point-min) te-width
))
688 (goto-char (point-min))
689 (if terminal-scrolling
690 (progn (delete-char 1)
691 (goto-char (point-max))
694 (delete-region (point) (+ (point) te-width
)))
695 (insert-char ?\s te-width
)
697 (te-set-window-start))
700 (defun te-move-to-position ()
701 ;; must offset by #o40 since cretinous unix won't send a 004 char through
702 (let ((y (- (te-get-char) 32))
703 (x (- (te-get-char) 32)))
704 (if (or (> x te-width
)
707 (goto-char (+ (point-min) x
(* y
(1+ te-width
))))
708 ;(te-set-window-start?)
710 (setq te-more-count -
1))
715 (defun te-clear-rest-of-line ()
717 (let ((n (- (point) (progn (end-of-line) (point)))))
718 (delete-region (point) (+ (point) n
))
719 (insert-char ?\s
(- n
)))))
723 (defun te-clear-rest-of-screen ()
725 (te-clear-rest-of-line)
726 (while (progn (end-of-line) (not (eobp)))
727 (forward-char 1) (end-of-line)
728 (delete-region (- (point) te-width
) (point))
729 (insert-char ?\s te-width
))))
733 (defun te-clear-screen ()
734 ;; regenerate buffer to compensate for (nonexistent!!) bugs.
737 (while (< i te-height
)
739 (insert-char ?\s te-width
)
741 (delete-region (1- (point-max)) (point-max))
742 (goto-char (point-min))
743 (setq te-more-count -
1))
747 (defun te-insert-lines ()
751 (let* ((line (- te-height
(/ (- (point) (point-min)) (1+ te-width
)) -
1))
752 (n (min (- (te-get-char) ?\s
) line
))
754 (delete-region (- (point-max) (* n
(1+ te-width
))) (point-max))
755 (if (eq (point) (point-max)) (insert ?
\n))
758 (insert-char ?\s te-width
)
759 (or (eq i line
) (insert ?
\n))))))
760 (setq te-more-count -
1))
764 (defun te-delete-lines ()
767 (let* ((line (- te-height
(/ (- (point) (point-min)) (1+ te-width
)) -
1))
768 (n (min (- (te-get-char) ?\s
) line
))
770 (delete-region (point)
771 (min (+ (point) (* n
(1+ te-width
))) (point-max)))
773 (goto-char (point-max))
776 (insert-char ?\s te-width
)
777 (or (eq i line
) (insert ?
\n))))))
778 (setq te-more-count -
1))
781 (defun te-beginning-of-line ()
785 (defun te-backward-char ()
790 (defun te-forward-char ()
799 (delete-region (1- (point)) (point))
809 (defun te-insert-spaces ()
811 (n (min (- (te-get-char) 32)
812 (- (progn (end-of-line) (point)) p
))))
820 ;; ^p d count+32 (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
821 (defun te-delete-char ()
823 (n (min (- (te-get-char) 32)
824 (- (progn (end-of-line) (point)) p
))))
834 ;; disgusting unix-required excrement
835 ;; Are we living twenty years in the past yet?
837 (defun te-losing-unix ()
841 (defun te-output-tab ()
843 (x (- p
(progn (beginning-of-line) (point))))
844 (l (min (- 8 (logand x
7))
845 (progn (end-of-line) (- (point) p
)))))
846 (goto-char (+ p l
))))
849 ;; Handle the `do' or `nl' termcap capability.
850 ;;>> I am not sure why this broken, obsolete, capability is here.
851 ;;>> Perhaps it is for VIle. No comment was made about why it
852 ;;>> was added (in "Sun Dec 6 01:22:27 1987 Richard Stallman")
853 (defun te-down-vertically-or-scroll ()
854 "Move down a line vertically, or scroll at bottom."
855 (let ((column (current-column)))
859 (delete-region (point-min) (+ (point-min) te-width
))
860 (goto-char (point-min))
862 (goto-char (point-max))
864 (insert-char ?\s te-width
)
867 (move-to-column column
))
868 (te-set-window-start))
871 ;; ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
872 ;; ^g => te-beep (for which it should use ^p ^g)
873 ;; ^h => te-backward-char (for which it should use ^p ^b)
877 (defun te-filter (process string
)
878 (with-current-buffer (process-buffer process
)
879 (goto-char te-saved-point
)
880 (and (bufferp te-log-buffer
)
881 (if (null (buffer-name te-log-buffer
))
883 (setq te-log-buffer nil
)
884 (set-buffer te-log-buffer
)
885 (goto-char (point-max))
886 (insert-before-markers string
)
887 (set-buffer (process-buffer process
))))
888 (setq te-pending-output
(nconc te-pending-output
(list string
)))
889 (te-update-pending-output-display)
890 (te-process-output (eq (current-buffer)
891 (window-buffer (selected-window))))
892 (set-buffer (process-buffer process
))
893 (setq te-saved-point
(point))))
895 ;; (A version of the following comment which might be distractingly offensive
896 ;; to some readers has been moved to term-nasty.el.)
897 ;; unix lacks ITS-style tty control...
898 (defun te-process-output (preemptible)
899 ;;>> There seems no good reason to ever disallow preemption
901 (catch 'te-process-output
902 (let ((buffer-read-only nil
)
903 (string nil
) ostring start char
(matchpos nil
))
904 (while (cdr te-pending-output
)
906 start
(car te-pending-output
)
907 string
(car (cdr te-pending-output
))
908 char
(aref string start
))
909 (if (eq (setq start
(1+ start
)) (length string
))
910 (progn (setq te-pending-output
911 (cons 0 (cdr (cdr te-pending-output
)))
913 string
(car (cdr te-pending-output
)))
914 (te-update-pending-output-display))
915 (setcar te-pending-output start
))
916 (if (and (> char ?
\037) (< char ?
\377))
920 (setq te-pending-output
921 (cons 0 (cons (make-string 1 char
)
922 (cdr te-pending-output
))))
923 (setcar te-pending-output
(1- start
)))
926 (delete-char 1) (insert char
)
927 (te-redisplay-if-necessary 1))
929 (let ((end (or (and (eq ostring string
) matchpos
)
930 (setq matchpos
(string-match
931 "[\000-\037\177-\377]"
934 (delete-char 1) (insert char
)
935 (setq char
(point)) (end-of-line)
936 (setq end
(min end
(+ start
(- (point) char
))))
938 (if (eq end matchpos
) (setq matchpos nil
))
939 (delete-region (point) (+ (point) (- end start
)))
940 (insert (if (and (eq start
0)
941 (eq end
(length string
)))
943 (substring string start end
)))
944 (if (eq end
(length string
))
945 (setq te-pending-output
946 (cons 0 (cdr (cdr te-pending-output
))))
947 (setcar te-pending-output end
))
948 (te-redisplay-if-necessary (1+ (- end start
))))))
949 ;; I suppose if I split the guts of this out into a separate
950 ;; function we could trivially emulate different terminals
951 ;; Who cares in any case? (Apart from stupid losers using rlogin)
954 (or (cdr (assq (te-get-char)
955 '((?
= . te-move-to-position
)
956 (?c . te-clear-rest-of-line
)
957 (?C . te-clear-rest-of-screen
)
958 (?\C-o . te-insert-lines
)
959 (?\C-k . te-delete-lines
)
960 ;; not necessary, but help sometimes.
961 (?\C-a . te-beginning-of-line
)
962 (?\C-b . te-backward-char
)
963 ;; should be C-d, but un*x
964 ;; pty's won't send \004 through!
965 ;; Can you believe this?
966 (?d . te-delete-char
)
967 (?_ . te-insert-spaces
)
969 (?\C-f . te-forward-char
)
971 (?\C-j . te-down-vertically-or-scroll
)
972 (?\C-l . te-clear-screen
)
976 '((?\C-j . te-newline
)
978 ;; Did I ask to be sent these characters?
979 ;; I don't remember doing so, either.
980 ;; (Perhaps some operating system or
981 ;; other is completely incompetent...)
982 (?\C-m . te-beginning-of-line
)
984 (?\C-h . te-backward-char
)
985 (?\C-i . te-output-tab
))))
987 (te-redisplay-if-necessary 1))
990 ;; preemptible output! Oh my!!
991 (throw 'te-process-output t
)))))
992 ;; We must update window-point in every window displaying our buffer
993 (walk-windows (lambda (w)
994 (when (and (not (eq w
(selected-window)))
995 (eq (window-buffer w
) (current-buffer)))
996 (set-window-point w
(point))))))
998 (defun te-get-char ()
999 (if (cdr te-pending-output
)
1000 (let ((start (car te-pending-output
))
1001 (string (car (cdr te-pending-output
))))
1002 (prog1 (aref string start
)
1003 (if (eq (setq start
(1+ start
)) (length string
))
1004 (setq te-pending-output
(cons 0 (cdr (cdr te-pending-output
))))
1005 (setcar te-pending-output start
))))
1007 (let ((filter (process-filter te-process
)))
1010 (set-process-filter te-process
1011 (function (lambda (_p s
)
1012 (or (eq (length s
) 1)
1013 (setq te-pending-output
(list 1 s
)))
1014 (throw 'char
(aref s
0)))))
1015 (accept-process-output te-process
))
1016 (set-process-filter te-process filter
))))))
1019 (defun te-redisplay-if-necessary (length)
1020 (and (<= (setq te-redisplay-count
(- te-redisplay-count length
)) 0)
1021 (eq (current-buffer) (window-buffer (selected-window)))
1022 (waiting-for-user-input-p)
1023 (progn (te-update-pending-output-display)
1025 (setq te-redisplay-count terminal-redisplay-interval
))))
1027 (defun te-update-pending-output-display ()
1028 (if (null (cdr te-pending-output
))
1029 (setq te-pending-output-info
"")
1030 (let ((length (te-pending-output-length)))
1032 (setq te-pending-output-info
"")
1033 (setq te-pending-output-info
(format "(%dK chars output pending) "
1034 (/ (+ length
512) 1024))))))
1035 (force-mode-line-update))
1038 (defun te-sentinel (process message
)
1039 (cond ((eq (process-status process
) 'run
))
1040 ((null (buffer-name (process-buffer process
)))) ;deleted
1041 (t (let ((b (current-buffer)))
1042 (with-current-buffer (process-buffer process
)
1043 (setq buffer-read-only nil
)
1045 (goto-char (point-max))
1046 (delete-blank-lines)
1047 (delete-horizontal-space)
1048 (insert "\n*******\n" message
"*******\n"))
1049 (if (and (eq b
(process-buffer process
))
1050 (waiting-for-user-input-p))
1051 (progn (goto-char (point-max))
1054 (defvar te-stty-string
"stty -nl erase '^?' kill '^u' intr '^c' echo pass8"
1055 "Shell command to set terminal modes for terminal emulator.")
1056 ;; This used to have `new' in it, but that loses outside BSD
1057 ;; and it's apparently not needed in BSD.
1059 (defcustom explicit-shell-file-name nil
1060 "If non-nil, is file name to use for explicitly requested inferior shell."
1061 :type
'(choice (const :tag
"None" nil
)
1066 (defun terminal-emulator (buffer program args
&optional width height
)
1067 "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
1068 ARGS is a list of argument-strings. Remaining arguments are WIDTH and HEIGHT.
1069 BUFFER's contents are made an image of the display generated by that program,
1070 and any input typed when BUFFER is the current Emacs buffer is sent to that
1071 program as keyboard input.
1073 Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
1074 are parsed from an input-string using your usual shell.
1075 WIDTH and HEIGHT are determined from the size of the current window
1076 -- WIDTH will be one less than the window's width, HEIGHT will be its height.
1078 To switch buffers and leave the emulator, or to give commands
1079 to the emulator itself (as opposed to the program running under it),
1080 type Control-^. The following character is an emulator command.
1081 Type Control-^ twice to send it to the subprogram.
1082 This escape character may be changed using the variable `terminal-escape-char'.
1084 `Meta' characters may not currently be sent through the terminal emulator.
1086 Here is a list of some of the variables which control the behavior
1087 of the emulator -- see their documentation for more information:
1088 terminal-escape-char, terminal-scrolling, terminal-more-processing,
1089 terminal-redisplay-interval.
1091 This function calls the value of terminal-mode-hook if that exists
1092 and is non-nil after the terminal buffer has been set up and the
1093 subprocess started."
1095 (cons (with-current-buffer (get-buffer-create "*terminal*")
1096 (buffer-name (if (or (not (boundp 'te-process
))
1098 (not (eq (process-status te-process
)
1101 (generate-new-buffer "*terminal*"))))
1104 ;; Default shell is same thing M-x shell uses.
1105 (or explicit-shell-file-name
1110 (format "Run program in emulator (default %s): "
1113 (list default-s
'())
1114 (te-parse-program-and-args s
))))))
1115 (switch-to-buffer buffer
)
1116 (if (null width
) (setq width
(- (window-width (selected-window)) 1)))
1117 (if (null height
) (setq height
(- (window-height (selected-window)) 1)))
1119 (setq te-width width te-height height
)
1120 (setq te-terminal-name
(concat te-terminal-name-prefix
1121 (number-to-string te-width
)
1122 (number-to-string te-height
)))
1123 (setq mode-line-buffer-identification
1124 (list (format "Emacs terminal %dx%d: %%b " te-width te-height
)
1125 'te-pending-output-info
))
1126 (let ((buffer-read-only nil
))
1129 (while (setq process
(get-buffer-process (current-buffer)))
1130 (if (y-or-n-p (format "Kill process %s? " (process-name process
)))
1131 (delete-process process
)
1132 (error "Process %s not killed" (process-name process
)))))
1134 (let ((process-environment
1135 (cons (concat "TERM=" te-terminal-name
)
1136 (cons (concat "TERMCAP=" (te-create-termcap))
1137 (cons (concat "TERMINFO=" (te-create-terminfo))
1138 process-environment
)))))
1140 (start-process "terminal-emulator" (current-buffer)
1142 ;; Yuck!!! Start a shell to set some terminal
1143 ;; control characteristics. Then start the
1144 ;; "env" program to setup the terminal type
1145 ;; Then finally start the program we wanted.
1146 (format "%s; exec %s"
1148 (mapconcat 'te-quote-arg-for-sh
1149 (cons program args
) " "))))
1150 (set-process-filter te-process
'te-filter
)
1151 (set-process-sentinel te-process
'te-sentinel
))
1152 (error (fundamental-mode)
1153 (signal (car err
) (cdr err
))))
1154 (setq inhibit-quit t
) ;sport death
1155 (use-local-map terminal-map
)
1156 (run-hooks 'terminal-mode-hook
)
1157 (message "Entering Emacs terminal-emulator... Type %s %s for help"
1158 (single-key-description terminal-escape-char
)
1159 (mapconcat 'single-key-description
1160 (where-is-internal 'te-escape-help terminal-escape-map t
)
1164 (defun te-parse-program-and-args (s)
1165 (cond ((string-match "\\`\\([-a-zA-Z0-9+=_.@/:]+[ \t]*\\)+\\'" s
)
1168 (setq l
(cons (if (string-match
1169 "\\([-a-zA-Z0-9+=_.@/:]+\\)\\([ \t]+\\)*"
1171 (prog1 (substring s p
(match-end 1))
1172 (setq p
(match-end 0))
1173 (if (eq p
(length s
)) (setq p nil
)))
1174 (prog1 (substring s p
)
1177 (setq l
(nreverse l
))
1178 (list (car l
) (cdr l
))))
1179 ((and (string-match "[ \t]" s
) (not (file-exists-p s
)))
1180 (list shell-file-name
(list "-c" (concat "exec " s
))))
1183 (put 'terminal-mode
'mode-class
'special
)
1184 ;; This is only separated out from function terminal-emulator
1185 ;; to keep the latter a little more manageable.
1186 (defun terminal-mode ()
1187 "Set up variables for use with the terminal-emulator.
1188 One should not call this -- it is an internal function
1189 of the terminal-emulator"
1190 (kill-all-local-variables)
1191 (buffer-disable-undo (current-buffer))
1192 (setq major-mode
'terminal-mode
)
1193 (setq mode-name
"terminal")
1194 ; (make-local-variable 'Helper-return-blurb)
1195 ; (setq Helper-return-blurb "return to terminal simulator")
1196 (setq mode-line-process
'(":%s"))
1197 (setq buffer-read-only t
)
1198 (setq truncate-lines t
)
1199 (make-local-variable 'terminal-escape-char
)
1200 (setq terminal-escape-char
(default-value 'terminal-escape-char
))
1201 (make-local-variable 'terminal-scrolling
)
1202 (setq terminal-scrolling
(default-value 'terminal-scrolling
))
1203 (make-local-variable 'terminal-more-processing
)
1204 (setq terminal-more-processing
(default-value 'terminal-more-processing
))
1205 (make-local-variable 'terminal-redisplay-interval
)
1206 (setq terminal-redisplay-interval
(default-value 'terminal-redisplay-interval
))
1207 (make-local-variable 'te-width
)
1208 (make-local-variable 'te-height
)
1209 (make-local-variable 'te-process
)
1210 (make-local-variable 'te-pending-output
)
1211 (setq te-pending-output
(list 0))
1212 (make-local-variable 'te-saved-point
)
1213 (setq te-saved-point
(point-min))
1214 (make-local-variable 'te-pending-output-info
) ;for the mode line
1215 (setq te-pending-output-info
"")
1216 (make-local-variable 'inhibit-quit
)
1217 ;(setq inhibit-quit t)
1218 (make-local-variable 'te-log-buffer
)
1219 (setq te-log-buffer nil
)
1220 (make-local-variable 'te-more-count
)
1221 (setq te-more-count -
1)
1222 (make-local-variable 'te-redisplay-count
)
1223 (setq te-redisplay-count terminal-redisplay-interval
)
1224 ;(use-local-map terminal-mode-map)
1225 ;; terminal-mode-hook is called above in function terminal-emulator
1228 ;;;; what a complete loss
1230 (defun te-quote-arg-for-sh (string)
1231 (cond ((string-match "\\`[-a-zA-Z0-9+=_.@/:]+\\'"
1234 ((not (string-match "[$]" string
))
1235 ;; "[\"\\]" are special to sh and the lisp reader in the same way
1236 (prin1-to-string string
))
1241 (while (cond ((>= start
(length string
))
1243 ;; this is the set of chars magic with "..." in `sh'
1244 ((setq end
(string-match "[\"\\$]"
1247 (t (setq harder
(concat harder
1248 (substring string start
)))
1250 (setq harder
(concat harder
(substring string start end
)
1251 ;; Can't use ?\\ since `concat'
1252 ;; unfortunately does prin1-to-string
1253 ;; on fixna. Amazing.
1259 (concat "\"" harder
"\"")))))
1261 (defun te-create-terminfo ()
1262 "Create and compile a terminfo entry for the virtual terminal. This is kept
1263 in the directory specified by `te-terminfo-directory'."
1264 (when (and system-uses-terminfo
1265 (not (file-exists-p (concat te-terminfo-directory
1266 (substring te-terminal-name-prefix
0 1)
1267 "/" te-terminal-name
))))
1270 ;; The first newline avoids trouble with ncurses.
1271 (format "%s,\n\tmir, xon,cols#%d, lines#%d,"
1272 te-terminal-name te-width te-height
)
1273 "bel=^P^G, clear=^P\\f, cr=^P^A, cub1=^P^B, cud1=^P\\n,"
1274 "cuf1=^P^F, cup=^P=%p1%'\\s'%+%c%p2%'\\s'%+%c,"
1275 "dch=^Pd%p1%'\\s'%+%c, dch1=^Pd!, dl=^P^K%p1%'\\s'%+%c,"
1276 "dl1=^P^K!, ed=^PC, el=^Pc, home=^P=\\s\\s,"
1277 "ich=^P_%p1%'\\s'%+%c, ich1=^P_!, il=^P^O%p1%'\\s'%+%c,"
1278 ;; The last newline avoids trouble with ncurses.
1279 "il1=^P^O!, ind=^P\\n, nel=\\n,\n"))
1280 ;; This is the desired name for the source file.
1281 (file-name (concat te-terminfo-directory te-terminal-name
".tif")) )
1282 (make-directory te-terminfo-directory t
)
1284 (make-temp-file (expand-file-name "tif" te-terminfo-directory
))))
1285 ;; Store the source file under a random temp name.
1286 (with-temp-file temp-file
1288 ;; Rename it to the desired name.
1289 ;; We use this roundabout approach
1290 ;; to avoid any risk of writing a name that
1291 ;; was mischievously set up as a symlink.
1292 (rename-file temp-file file-name
))
1293 ;; Now compile that source to make the binary that the
1294 ;; programs actually use.
1295 (let ((process-environment
1296 (cons (concat "TERMINFO="
1297 (directory-file-name te-terminfo-directory
))
1298 process-environment
)))
1299 (set-process-sentinel (start-process "tic" nil
"tic" file-name
)
1300 'te-tic-sentinel
))))
1301 (directory-file-name te-terminfo-directory
))
1303 (defun te-create-termcap ()
1304 "Create a termcap entry for the virtual terminal"
1305 ;; Because of Unix Brain Death(tm), we can't change
1306 ;; the terminal type of a running process, and so
1307 ;; terminal size and scrollability are wired-down
1308 ;; at this point. ("Detach? What's that?")
1309 (concat (format "%s:co#%d:li#%d:%s"
1310 ;; Sigh. These can't be dynamically changed.
1311 te-terminal-name te-width te-height
(if terminal-scrolling
1314 ;; cursor-motion, bol, forward/backward char
1315 "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:"
1316 ;; newline, clear eof/eof, audible bell
1317 "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:"
1318 ;; insert/delete char/line
1319 "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :"
1320 ;;-- Not-widely-known (ie nonstandard) flags, which mean
1321 ;; o writing in the last column of the last line
1322 ;; doesn't cause idiotic scrolling, and
1323 ;; o don't use idiotische c-s/c-q sogenannte
1324 ;; ``flow control'' auf keinen Fall.
1326 ;;-- For stupid or obsolete programs
1327 "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p= :"
1328 ;;-- For disgusting programs.
1329 ;; (VI? What losers need these, I wonder?)
1330 "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:")
1333 (defun te-tic-sentinel (_proc state-change
)
1334 "If tic has finished, delete the .tif file"
1335 (if (equal state-change
"finished
1337 (delete-file (concat te-terminfo-directory te-terminal-name
".tif"))))
1341 ;;; terminal.el ends here