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