-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-comint.el
123 lines (106 loc) · 4.23 KB
/
pjb-comint.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: pjb-comint.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Add output filters to process more emacs-048 codes.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;; 2010-07-27 <PJB> Extracted from ~/.emacs
;;;;BUGS
;;;;
;;;; This doesn't work. When there are errors signaled from the
;;;; filter functions, this may hang emacs. So we need to add
;;;; error handler or to debug better.
;;;;
;;;;LEGAL
;;;; GPL
;;;;
;;;; Copyright Pascal J. Bourguignon 2010 - 2011
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License
;;;; as published by the Free Software Foundation; either version
;;;; 2 of the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be
;;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;;;; PURPOSE. See the GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public
;;;; License along with this program; if not, write to the Free
;;;; Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;**************************************************************************
(when nil
(defun pjb-comint-filter-meat/erase-screen (string)
(when string
(let ((pos (search "c" string :from-end t)))
(if pos
(progn
(erase-buffer)
(subseq string (+ 2 pos)))
string))))
(defun pjb-comint-filter-meat/erase-in-line (string)
(when string
(let* ((pos (search "\\[\\([0-9]*\\)K" string :from-end t))
(ps (match-string 1 string)))
(if pos
(progn
(erase-buffer)
(if (or (string= "" ps) (= 0 (parse-number ps)))
(subseq string 0 pos)
""))
string))))
(defun ecma-048-cuu ()
(backward-line 1))
(defun ecma-048-cuf (offset)
(let ((new-column (+ (point) offset)))
(if (< (save-excursion (end-of-line) (point)) new-column)
(progn (end-of-line)
;; (insert (make-string (- new-column (point)) 32))
)
(forward-char offset))))
(defun ecma-048-crlf ()
(insert (make-string (forward-line 1) 10)))
(defun pjb-comint-filter-meat/position (string)
(when string
(let ((commands '(("\nA" beginning-of-line)
("\\([0-9]+\\)C" ecma-048-cuf 1)
("\\([0-9;]*\\)H" ignore)))
(start 0))
(while (let ((cmd (find-if (lambda (cmd) (eql start (string-match (first cmd) string start))) commands)))
(when cmd
(setf start (match-end 0))
(apply (second cmd) (mapcar (lambda (i) (nth-value 0 (cl:parse-integer (match-string i string)))) (cddr cmd)))
t)))
(if (zerop start)
string
(subseq string start)))))
(defun pjb-comint-filter-meat/color (string)
"Remove color ansi codes."
(when string
(with-temp-buffer
(insert string)
(goto-char 0)
(let ((changed nil))
(while (re-search-forward "\\[\\([0-9;]*\\)m" (point-max) t)
(setf changed t)
(delete-region (match-beginning 0) (match-end 0)))
(if changed
(buffer-substring-no-properties (point-min) (point-max))
string)))))
;; (add-hook 'comint-preoutput-filter-functions 'pjb-comint-filter-meat/position)
(add-hook 'comint-preoutput-filter-functions 'pjb-comint-filter-meat/erase-screen)
(add-hook 'comint-preoutput-filter-functions 'pjb-comint-filter-meat/erase-in-line)
(add-hook 'comint-preoutput-filter-functions 'pjb-comint-filter-meat/color)
;; comint-preoutput-filter-functions
(setf comint-preoutput-filter-functions nil)
) ;; when nil
;;;; THE END ;;;;