comparison lisp/packages/shell-font.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 0293115a14e9
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;; Decorate a shell buffer with fonts.
2 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 ;; This file is part of XEmacs.
5
6 ;; XEmacs is free software; you can redistribute it and/or modify it
7 ;; under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2, or (at your option)
9 ;; any later version.
10
11 ;; XEmacs is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with XEmacs; see the file COPYING. If not, write to the Free
18 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 ;;; Synched up with: Not in FSF.
21
22 ;; Do this: (add-hook 'shell-mode-hook 'install-shell-fonts)
23 ;; and the prompt in your shell-buffers will appear bold-italic, process
24 ;; output will appear in normal face, and typein will appear in bold.
25 ;;
26 ;; The faces shell-prompt, shell-input and shell-output can be modified
27 ;; as desired, for example, (copy-face 'italic 'shell-prompt).
28
29 ;; Written by Jamie Zawinski, overhauled by Eric Benson.
30
31 ;; TODO:
32 ;; =====
33 ;; Parse ANSI/VT100 escape sequences to turn on underlining/boldface/etc.
34 ;; Automatically run nuke-nroff-bs?
35
36
37 (require 'text-props) ; for put-nonduplicable-text-property
38
39 (make-face 'shell-prompt)
40 (if (not (face-differs-from-default-p 'shell-prompt))
41 (copy-face 'bold-italic 'shell-prompt))
42
43 (make-face 'shell-input)
44 (if (not (face-differs-from-default-p 'shell-input))
45 (copy-face 'bold 'shell-input))
46
47 (make-face 'shell-output)
48 (if (not (face-differs-from-default-p 'shell-output))
49 (progn (make-face-unbold 'shell-output)
50 (make-face-unitalic 'shell-output)
51 (set-face-underline-p 'shell-output nil)))
52
53 (defvar shell-font-read-only-prompt nil
54 "*Set all shell prompts to be read-only")
55
56 (defvar shell-font-current-face 'shell-input)
57
58 (defun shell-font-fontify-region (start end delete-count)
59 ;; for use as an element of after-change-functions; fontifies the inserted text.
60 (if (= start end)
61 nil
62 ; ;; This creates lots of extents (one per user-typed character)
63 ; ;; which is wasteful of memory.
64 ; (let ((e (make-extent start end)))
65 ; (set-extent-face e shell-font-current-face)
66 ; (set-extent-property e 'shell-font t))
67
68 ;; This efficiently merges extents
69 (put-nonduplicable-text-property start end 'face shell-font-current-face)
70 (and shell-font-read-only-prompt
71 (eq shell-font-current-face 'shell-prompt)
72 (put-nonduplicable-text-property start end 'read-only t))
73 ))
74
75 (defun shell-font-hack-prompt (limit)
76 "Search backward from point-max for text matching the comint-prompt-regexp,
77 and put it in the `shell-prompt' face. LIMIT is the left bound of the search."
78 (save-excursion
79 (goto-char (point-max))
80 (save-match-data
81 (cond ((re-search-backward comint-prompt-regexp limit t)
82 (goto-char (match-end 0))
83 (cond ((= (point) (point-max))
84 (skip-chars-backward " \t")
85 (let ((shell-font-current-face 'shell-prompt))
86 (shell-font-fontify-region
87 (match-beginning 0) (point) 0)))))))))
88
89
90 (defvar shell-font-process-filter nil
91 "In an interaction buffer with shell-font, this is the original proc filter.
92 shell-font encapsulates this.")
93
94 (defun shell-font-process-filter (proc string)
95 "Invoke the original process filter, then set fonts on the output.
96 The original filter is in the buffer-local variable shell-font-process-filter."
97 (let ((cb (current-buffer))
98 (pb (process-buffer proc)))
99 (if (null pb)
100 ;; If the proc has no buffer, leave it alone.
101 (funcall shell-font-process-filter proc string)
102 ;; Don't do save excursion because some proc filters want to change
103 ;; the buffer's point.
104 (set-buffer pb)
105 (let ((p (marker-position (process-mark proc))))
106 (prog1
107 ;; this let must not be around the `set-buffer' call.
108 (let ((shell-font-current-face 'shell-output))
109 (funcall shell-font-process-filter proc string))
110 (shell-font-hack-prompt p)
111 (set-buffer cb))))))
112
113 ;;;###autoload
114 (defun install-shell-fonts ()
115 "Decorate the current interaction buffer with fonts.
116 This uses the faces called `shell-prompt', `shell-input' and `shell-output';
117 you can alter the graphical attributes of those with the normal
118 face-manipulation functions."
119 (let* ((proc (or (get-buffer-process (current-buffer))
120 (error "no process in %S" (current-buffer))))
121 (old (or (process-filter proc)
122 (error "no process filter on %S" proc))))
123 (make-local-variable 'after-change-functions)
124 (add-hook 'after-change-functions 'shell-font-fontify-region)
125 (make-local-variable 'shell-font-current-face)
126 (setq shell-font-current-face 'shell-input)
127 (make-local-variable 'shell-font-process-filter)
128 (or (eq old 'shell-font-process-filter) ; already set
129 (setq shell-font-process-filter old))
130 (set-process-filter proc 'shell-font-process-filter))
131 nil)
132
133 (add-hook 'shell-mode-hook 'install-shell-fonts)
134 (add-hook 'telnet-mode-hook 'install-shell-fonts)
135 (add-hook 'gdb-mode-hook 'install-shell-fonts)
136
137 ;; for compatibility with the 19.8 version
138 ;(fset 'install-shell-font-prompt 'install-shell-fonts)
139 (make-obsolete 'install-shell-font-prompt 'install-shell-fonts)
140
141 (provide 'shell-font)