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