comparison lisp/term.el @ 57299:6ca70f15d948

(term-adjust-current-row-cache): Don\'t allow the current row to become negative. (term-emulate-terminal): Fix insert mode. Handle tab insertion at the end of the line. Fix scroll down. Add comments. (term-handle-ansi-escape): Don\'t exceed terminal width when moving right. (term-scroll-region): Move the cursor after setting the scroll region.
author Dan Nicolaescu <dann@ics.uci.edu>
date Sat, 02 Oct 2004 18:23:27 +0000
parents d838e748405a
children 74ab35181199 58db929d96c6
comparison
equal deleted inserted replaced
57298:c6197a2f56ed 57299:6ca70f15d948
2586 (save-excursion 2586 (save-excursion
2587 (narrow-to-region term-home-marker (point-max)) 2587 (narrow-to-region term-home-marker (point-max))
2588 (- (term-vertical-motion -9999)))))))) 2588 (- (term-vertical-motion -9999))))))))
2589 2589
2590 (defun term-adjust-current-row-cache (delta) 2590 (defun term-adjust-current-row-cache (delta)
2591 (if term-current-row 2591 (when term-current-row
2592 (setq term-current-row (+ term-current-row delta)))) 2592 (setq term-current-row
2593 (max 0 (+ term-current-row delta)))))
2593 2594
2594 (defun term-terminal-pos () 2595 (defun term-terminal-pos ()
2595 (save-excursion ; save-restriction 2596 (save-excursion ; save-restriction
2596 (let ((save-col (term-current-column)) 2597 (let ((save-col (term-current-column))
2597 x y) 2598 x y)
2762 (setq term-current-column (current-column) 2763 (setq term-current-column (current-column)
2763 columns (- term-current-column old-column)) 2764 columns (- term-current-column old-column))
2764 (when (not (or (eobp) term-insert-mode)) 2765 (when (not (or (eobp) term-insert-mode))
2765 (setq pos (point)) 2766 (setq pos (point))
2766 (term-move-columns columns) 2767 (term-move-columns columns)
2767 (delete-region pos (point)))) 2768 (delete-region pos (point)))
2769 ;; In insert if the if the current line
2770 ;; has become too long it needs to be
2771 ;; chopped off.
2772 (when term-insert-mode
2773 (setq pos (point))
2774 (end-of-line)
2775 (when (> (current-column) term-width)
2776 (delete-region (- (point) (- (current-column) term-width))
2777 (point)))
2778 (goto-char pos)))
2768 (setq term-current-column nil) 2779 (setq term-current-column nil)
2769 2780
2770 (put-text-property old-point (point) 2781 (put-text-property old-point (point)
2771 'face term-current-face) 2782 'face term-current-face)
2772 ;; If the last char was written in last column, 2783 ;; If the last char was written in last column,
2776 (term-move-columns -1) 2787 (term-move-columns -1)
2777 (setq term-terminal-state 1))) 2788 (setq term-terminal-state 1)))
2778 (setq i (1- funny))) 2789 (setq i (1- funny)))
2779 ((and (setq term-terminal-state 0) 2790 ((and (setq term-terminal-state 0)
2780 (eq char ?\^I)) ; TAB (terminfo: ht) 2791 (eq char ?\^I)) ; TAB (terminfo: ht)
2781 ;; FIXME: Does not handle line wrap!
2782 (setq count (term-current-column)) 2792 (setq count (term-current-column))
2783 (setq count (+ count 8 (- (mod count 8)))) 2793 ;; The line cannot exceed term-width. TAB at
2784 (if (< (move-to-column count nil) count) 2794 ;; the end of a line should not cause wrapping.
2785 (term-insert-char char 1)) 2795 (setq count (min term-width
2786 (setq term-current-column count)) 2796 (+ count 8 (- (mod count 8)))))
2797 (if (> term-width count)
2798 (progn
2799 (term-move-columns
2800 (- count (term-current-column)))
2801 (setq term-current-column count))
2802 (when (> term-width (term-current-column))
2803 (term-move-columns
2804 (1- (- term-width (term-current-column)))))
2805 (when (= term-width (term-current-column))
2806 (term-move-columns -1))))
2787 ((eq char ?\r) 2807 ((eq char ?\r)
2788 ;; Optimize CRLF at end of buffer: 2808 ;; Optimize CRLF at end of buffer:
2789 (cond ((and (< (setq temp (1+ i)) str-length) 2809 (cond ((and (< (setq temp (1+ i)) str-length)
2790 (eq (aref str temp) ?\n) 2810 (eq (aref str temp) ?\n)
2791 (= (point) (point-max)) 2811 (= (point) (point-max))
2849 (setq term-terminal-state 3)) 2869 (setq term-terminal-state 3))
2850 ((eq char ?D) ;; scroll forward 2870 ((eq char ?D) ;; scroll forward
2851 (term-handle-deferred-scroll) 2871 (term-handle-deferred-scroll)
2852 (term-down 1 t) 2872 (term-down 1 t)
2853 (setq term-terminal-state 0)) 2873 (setq term-terminal-state 0))
2854 ((eq char ?M) ;; scroll reversed 2874 ;; ((eq char ?E) ;; (terminfo: nw), not used for
2855 (term-insert-lines 1) 2875 ;; ;; now, but this is a working
2876 ;; ;; implementation
2877 ;; (term-down 1)
2878 ;; (term-goto term-current-row 0)
2879 ;; (setq term-terminal-state 0))
2880 ((eq char ?M) ;; scroll reversed (terminfo: ri)
2881 (term-down -1)
2856 (setq term-terminal-state 0)) 2882 (setq term-terminal-state 0))
2857 ((eq char ?7) ;; Save cursor (terminfo: sc) 2883 ((eq char ?7) ;; Save cursor (terminfo: sc)
2858 (term-handle-deferred-scroll) 2884 (term-handle-deferred-scroll)
2859 (setq term-saved-cursor 2885 (setq term-saved-cursor
2860 (cons (term-current-row) 2886 (cons (term-current-row)
2863 ((eq char ?8) ;; Restore cursor (terminfo: rc) 2889 ((eq char ?8) ;; Restore cursor (terminfo: rc)
2864 (if term-saved-cursor 2890 (if term-saved-cursor
2865 (term-goto (car term-saved-cursor) 2891 (term-goto (car term-saved-cursor)
2866 (cdr term-saved-cursor))) 2892 (cdr term-saved-cursor)))
2867 (setq term-terminal-state 0)) 2893 (setq term-terminal-state 0))
2894 ;; The \E#8 reset sequence for xterm. We
2895 ;; probably don't need to handle it, but this
2896 ;; is the code to parse it.
2897 ;; ((eq char ?#)
2898 ;; (when (eq (aref str (1+ i)) ?8)
2899 ;; (setq i (1+ i))
2900 ;; (setq term-terminal-state 0)))
2868 ((setq term-terminal-state 0)))) 2901 ((setq term-terminal-state 0))))
2869 ((eq term-terminal-state 3) ; Seen Esc [ 2902 ((eq term-terminal-state 3) ; Seen Esc [
2870 (cond ((and (>= char ?0) (<= char ?9)) 2903 (cond ((and (>= char ?0) (<= char ?9))
2871 (setq term-terminal-parameter 2904 (setq term-terminal-parameter
2872 (+ (* 10 term-terminal-parameter) (- char ?0)))) 2905 (+ (* 10 term-terminal-parameter) (- char ?0))))
3119 ;;; Handle a character assuming (eq terminal-state 2) - 3152 ;;; Handle a character assuming (eq terminal-state 2) -
3120 ;;; i.e. we have previously seen Escape followed by ?[. 3153 ;;; i.e. we have previously seen Escape followed by ?[.
3121 3154
3122 (defun term-handle-ansi-escape (proc char) 3155 (defun term-handle-ansi-escape (proc char)
3123 (cond 3156 (cond
3124 ((eq char ?H) ; cursor motion 3157 ((or (eq char ?H) ; cursor motion (terminfo: cup)
3158 ;; (eq char ?f) ; xterm seems to handle this sequence too, not
3159 ;; needed for now
3160 )
3125 (if (<= term-terminal-parameter 0) 3161 (if (<= term-terminal-parameter 0)
3126 (setq term-terminal-parameter 1)) 3162 (setq term-terminal-parameter 1))
3127 (if (<= term-terminal-previous-parameter 0) 3163 (if (<= term-terminal-previous-parameter 0)
3128 (setq term-terminal-previous-parameter 1)) 3164 (setq term-terminal-previous-parameter 1))
3129 (if (> term-terminal-previous-parameter term-height) 3165 (if (> term-terminal-previous-parameter term-height)
3131 (if (> term-terminal-parameter term-width) 3167 (if (> term-terminal-parameter term-width)
3132 (setq term-terminal-parameter term-width)) 3168 (setq term-terminal-parameter term-width))
3133 (term-goto 3169 (term-goto
3134 (1- term-terminal-previous-parameter) 3170 (1- term-terminal-previous-parameter)
3135 (1- term-terminal-parameter))) 3171 (1- term-terminal-parameter)))
3136 ;; \E[A - cursor up (terminfo: cuu1) 3172 ;; \E[A - cursor up (terminfo: cuu, cuu1)
3137 ((eq char ?A) 3173 ((eq char ?A)
3138 (term-handle-deferred-scroll) 3174 (term-handle-deferred-scroll)
3139 (term-down (- (max 1 term-terminal-parameter)) t)) 3175 (term-down (- (max 1 term-terminal-parameter)) t))
3140 ;; \E[B - cursor down 3176 ;; \E[B - cursor down (terminfo: cud)
3141 ((eq char ?B) 3177 ((eq char ?B)
3142 (term-down (max 1 term-terminal-parameter) t)) 3178 (term-down (max 1 term-terminal-parameter) t))
3143 ;; \E[C - cursor right 3179 ;; \E[C - cursor right (terminfo: cuf)
3144 ((eq char ?C) 3180 ((eq char ?C)
3145 (term-move-columns (max 1 term-terminal-parameter))) 3181 (term-move-columns
3146 ;; \E[D - cursor left 3182 (max 1
3183 (if (>= (+ term-terminal-parameter (term-current-column)) term-width)
3184 (- term-width (term-current-column) 1)
3185 term-terminal-parameter))))
3186 ;; \E[D - cursor left (terminfo: cub)
3147 ((eq char ?D) 3187 ((eq char ?D)
3148 (term-move-columns (- (max 1 term-terminal-parameter)))) 3188 (term-move-columns (- (max 1 term-terminal-parameter))))
3149 ;; \E[J - clear to end of screen (terminfo: ed, clear) 3189 ;; \E[J - clear to end of screen (terminfo: ed, clear)
3150 ((eq char ?J) 3190 ((eq char ?J)
3151 (term-erase-in-display term-terminal-parameter)) 3191 (term-erase-in-display term-terminal-parameter))
3199 (process-send-string proc 3239 (process-send-string proc
3200 (format "\e[%s;%sR" 3240 (format "\e[%s;%sR"
3201 (1+ (term-current-row)) 3241 (1+ (term-current-row))
3202 (1+ (term-horizontal-column))))) 3242 (1+ (term-horizontal-column)))))
3203 ;; \E[r - Set scrolling region 3243 ;; \E[r - Set scrolling region
3204 ((eq char ?r) 3244 ((eq char ?r) ;; (terminfo: csr)
3205 (term-scroll-region 3245 (term-scroll-region
3206 (1- term-terminal-previous-parameter) 3246 (1- term-terminal-previous-parameter)
3207 term-terminal-parameter)) 3247 term-terminal-parameter))
3208 (t))) 3248 (t)))
3209 3249
3221 term-height 3261 term-height
3222 bottom)) 3262 bottom))
3223 (setq term-scroll-with-delete 3263 (setq term-scroll-with-delete
3224 (or (term-using-alternate-sub-buffer) 3264 (or (term-using-alternate-sub-buffer)
3225 (not (and (= term-scroll-start 0) 3265 (not (and (= term-scroll-start 0)
3226 (= term-scroll-end term-height)))))) 3266 (= term-scroll-end term-height)))))
3267 (term-move-columns (- (term-current-column)))
3268 (term-goto
3269 term-scroll-start (term-current-column)))
3227 3270
3228 ;; (defun term-switch-to-alternate-sub-buffer (set) 3271 ;; (defun term-switch-to-alternate-sub-buffer (set)
3229 ;; ;; If asked to switch to (from) the alternate sub-buffer, and already (not) 3272 ;; ;; If asked to switch to (from) the alternate sub-buffer, and already (not)
3230 ;; ;; using it, do nothing. This test is needed for some programs (including 3273 ;; ;; using it, do nothing. This test is needed for some programs (including
3231 ;; ;; Emacs) that emit the ti termcap string twice, for unknown reason. 3274 ;; ;; Emacs) that emit the ti termcap string twice, for unknown reason.
3599 (col (term-horizontal-column)) 3642 (col (term-horizontal-column))
3600 (start-region term-home-marker) 3643 (start-region term-home-marker)
3601 (end-region (if (eq kind 1) (point) (point-max)))) 3644 (end-region (if (eq kind 1) (point) (point-max))))
3602 (delete-region start-region end-region) 3645 (delete-region start-region end-region)
3603 (term-unwrap-line) 3646 (term-unwrap-line)
3604 (if (eq kind 1) 3647 (when (eq kind 1)
3605 (term-insert-char ?\n row)) 3648 (term-insert-char ?\n row))
3606 (setq term-current-column nil) 3649 (setq term-current-column nil)
3607 (setq term-current-row nil) 3650 (setq term-current-row nil)
3608 (term-goto row col))))) 3651 (term-goto row col)))))
3609 3652
3610 (defun term-delete-chars (count) 3653 (defun term-delete-chars (count)