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