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