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