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