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