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