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