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