diff options
| -rw-r--r-- | lisp/ChangeLog | 9 | ||||
| -rw-r--r-- | lisp/ansi-color.el | 90 |
2 files changed, 63 insertions, 36 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index d16067c5378..ee97e4f6af1 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,12 @@ | |||
| 1 | 2010-05-29 Chong Yidong <cyd@stupidchicken.com> | ||
| 2 | |||
| 3 | * ansi-color.el: Delete unused escape sequences (Bug#6085). | ||
| 4 | (ansi-color-drop-regexp): New constant. | ||
| 5 | (ansi-color-apply, ansi-color-filter-region) | ||
| 6 | (ansi-color-apply-on-region): Delete unrecognized control | ||
| 7 | sequences. | ||
| 8 | (ansi-color-apply): Build string list before calling concat. | ||
| 9 | |||
| 1 | 2010-05-29 Eli Zaretskii <eliz@gnu.org> | 10 | 2010-05-29 Eli Zaretskii <eliz@gnu.org> |
| 2 | 11 | ||
| 3 | Bidi-sensitive word movement with arrow keys. | 12 | Bidi-sensitive word movement with arrow keys. |
diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el index 58eed04f147..00162c99219 100644 --- a/lisp/ansi-color.el +++ b/lisp/ansi-color.el | |||
| @@ -142,6 +142,10 @@ map. This color map is stored in the variable `ansi-color-map'." | |||
| 142 | (defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)" | 142 | (defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)" |
| 143 | "Regexp that matches SGR control sequences.") | 143 | "Regexp that matches SGR control sequences.") |
| 144 | 144 | ||
| 145 | (defconst ansi-color-drop-regexp | ||
| 146 | "\033\\[\\([ABCDsuK]\\|2J\\|=[0-9]+[hI]\\|[0-9;]*[Hf]\\)" | ||
| 147 | "Regexp that matches ANSI control sequences to silently drop.") | ||
| 148 | |||
| 145 | (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]" | 149 | (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]" |
| 146 | "Regexp that matches SGR control sequence parameters.") | 150 | "Regexp that matches SGR control sequence parameters.") |
| 147 | 151 | ||
| @@ -154,7 +158,7 @@ map. This color map is stored in the variable `ansi-color-map'." | |||
| 154 | If nil, do nothing. | 158 | If nil, do nothing. |
| 155 | If the symbol `filter', then filter all SGR control sequences. | 159 | If the symbol `filter', then filter all SGR control sequences. |
| 156 | If anything else (such as t), then translate SGR control sequences | 160 | If anything else (such as t), then translate SGR control sequences |
| 157 | into text-properties. | 161 | into text properties. |
| 158 | 162 | ||
| 159 | In order for this to have any effect, `ansi-color-process-output' must | 163 | In order for this to have any effect, `ansi-color-process-output' must |
| 160 | be in `comint-output-filter-functions'. | 164 | be in `comint-output-filter-functions'. |
| @@ -188,12 +192,12 @@ in shell buffers. You set this variable by calling one of: | |||
| 188 | 192 | ||
| 189 | ;;;###autoload | 193 | ;;;###autoload |
| 190 | (defun ansi-color-process-output (ignored) | 194 | (defun ansi-color-process-output (ignored) |
| 191 | "Maybe translate SGR control sequences of comint output into text-properties. | 195 | "Maybe translate SGR control sequences of comint output into text properties. |
| 192 | 196 | ||
| 193 | Depending on variable `ansi-color-for-comint-mode' the comint output is | 197 | Depending on variable `ansi-color-for-comint-mode' the comint output is |
| 194 | either not processed, SGR control sequences are filtered using | 198 | either not processed, SGR control sequences are filtered using |
| 195 | `ansi-color-filter-region', or SGR control sequences are translated into | 199 | `ansi-color-filter-region', or SGR control sequences are translated into |
| 196 | text-properties using `ansi-color-apply-on-region'. | 200 | text properties using `ansi-color-apply-on-region'. |
| 197 | 201 | ||
| 198 | The comint output is assumed to lie between the marker | 202 | The comint output is assumed to lie between the marker |
| 199 | `comint-last-output-start' and the process-mark. | 203 | `comint-last-output-start' and the process-mark. |
| @@ -217,15 +221,15 @@ This is a good function to put in `comint-output-filter-functions'." | |||
| 217 | (defun ansi-color-unfontify-region (beg end &rest xemacs-stuff) | 221 | (defun ansi-color-unfontify-region (beg end &rest xemacs-stuff) |
| 218 | "Replacement function for `font-lock-default-unfontify-region'. | 222 | "Replacement function for `font-lock-default-unfontify-region'. |
| 219 | 223 | ||
| 220 | As text-properties are implemented using extents in XEmacs, this | 224 | As text properties are implemented using extents in XEmacs, this |
| 221 | function is probably not needed. In Emacs, however, things are a bit | 225 | function is probably not needed. In Emacs, however, things are a bit |
| 222 | different: When font-lock is active in a buffer, you cannot simply add | 226 | different: When font-lock is active in a buffer, you cannot simply add |
| 223 | face text-properties to the buffer. Font-lock will remove the face | 227 | face text properties to the buffer. Font-lock will remove the face |
| 224 | text-property using `font-lock-unfontify-region-function'. If you want | 228 | text property using `font-lock-unfontify-region-function'. If you want |
| 225 | to insert the strings returned by `ansi-color-apply' into such buffers, | 229 | to insert the strings returned by `ansi-color-apply' into such buffers, |
| 226 | you must set `font-lock-unfontify-region-function' to | 230 | you must set `font-lock-unfontify-region-function' to |
| 227 | `ansi-color-unfontify-region'. This function will not remove all face | 231 | `ansi-color-unfontify-region'. This function will not remove all face |
| 228 | text-properties unconditionally. It will keep the face text-properties | 232 | text properties unconditionally. It will keep the face text properties |
| 229 | if the property `ansi-color' is set. | 233 | if the property `ansi-color' is set. |
| 230 | 234 | ||
| 231 | The region from BEG to END is unfontified. XEMACS-STUFF is ignored. | 235 | The region from BEG to END is unfontified. XEMACS-STUFF is ignored. |
| @@ -262,7 +266,7 @@ escape sequence.") | |||
| 262 | (make-variable-buffer-local 'ansi-color-context) | 266 | (make-variable-buffer-local 'ansi-color-context) |
| 263 | 267 | ||
| 264 | (defun ansi-color-filter-apply (string) | 268 | (defun ansi-color-filter-apply (string) |
| 265 | "Filter out all SGR control sequences from STRING. | 269 | "Filter out all ANSI control sequences from STRING. |
| 266 | 270 | ||
| 267 | Every call to this function will set and use the buffer-local variable | 271 | Every call to this function will set and use the buffer-local variable |
| 268 | `ansi-color-context' to save partial escape sequences. This information | 272 | `ansi-color-context' to save partial escape sequences. This information |
| @@ -292,10 +296,11 @@ This function can be added to `comint-preoutput-filter-functions'." | |||
| 292 | result)) | 296 | result)) |
| 293 | 297 | ||
| 294 | (defun ansi-color-apply (string) | 298 | (defun ansi-color-apply (string) |
| 295 | "Translates SGR control sequences into text-properties. | 299 | "Translates SGR control sequences into text properties. |
| 300 | Delete all other control sequences without processing them. | ||
| 296 | 301 | ||
| 297 | Applies SGR control sequences setting foreground and background colors | 302 | Applies SGR control sequences setting foreground and background colors |
| 298 | to STRING using text-properties and returns the result. The colors used | 303 | to STRING using text properties and returns the result. The colors used |
| 299 | are given in `ansi-color-faces-vector' and `ansi-color-names-vector'. | 304 | are given in `ansi-color-faces-vector' and `ansi-color-names-vector'. |
| 300 | See function `ansi-color-apply-sequence' for details. | 305 | See function `ansi-color-apply-sequence' for details. |
| 301 | 306 | ||
| @@ -309,23 +314,27 @@ This function can be added to `comint-preoutput-filter-functions'. | |||
| 309 | You cannot insert the strings returned into buffers using font-lock. | 314 | You cannot insert the strings returned into buffers using font-lock. |
| 310 | See `ansi-color-unfontify-region' for a way around this." | 315 | See `ansi-color-unfontify-region' for a way around this." |
| 311 | (let ((face (car ansi-color-context)) | 316 | (let ((face (car ansi-color-context)) |
| 312 | (start 0) end escape-sequence result) | 317 | (start 0) end escape-sequence result |
| 313 | ;; if context was saved and is a string, prepend it | 318 | colorized-substring) |
| 319 | ;; If context was saved and is a string, prepend it. | ||
| 314 | (if (cadr ansi-color-context) | 320 | (if (cadr ansi-color-context) |
| 315 | (setq string (concat (cadr ansi-color-context) string) | 321 | (setq string (concat (cadr ansi-color-context) string) |
| 316 | ansi-color-context nil)) | 322 | ansi-color-context nil)) |
| 317 | ;; find the next escape sequence | 323 | ;; Find the next escape sequence. |
| 318 | (while (setq end (string-match ansi-color-regexp string start)) | 324 | (while (setq end (string-match ansi-color-regexp string start)) |
| 319 | ;; store escape sequence | ||
| 320 | (setq escape-sequence (match-string 1 string)) | 325 | (setq escape-sequence (match-string 1 string)) |
| 321 | ;; colorize the old block from start to end using old face | 326 | ;; Colorize the old block from start to end using old face. |
| 322 | (when face | 327 | (when face |
| 323 | (put-text-property start end 'ansi-color t string) | 328 | (put-text-property start end 'ansi-color t string) |
| 324 | (put-text-property start end 'face face string)) | 329 | (put-text-property start end 'face face string)) |
| 325 | (setq result (concat result (substring string start end)) | 330 | (setq colorized-substring (substring string start end) |
| 326 | start (match-end 0)) | 331 | start (match-end 0)) |
| 327 | ;; create new face by applying all the parameters in the escape | 332 | ;; Eliminate unrecognized ANSI sequences. |
| 328 | ;; sequence | 333 | (while (string-match ansi-color-drop-regexp colorized-substring) |
| 334 | (setq colorized-substring | ||
| 335 | (replace-match "" nil nil colorized-substring))) | ||
| 336 | (push colorized-substring result) | ||
| 337 | ;; Create new face, by applying escape sequence parameters. | ||
| 329 | (setq face (ansi-color-apply-sequence escape-sequence face))) | 338 | (setq face (ansi-color-apply-sequence escape-sequence face))) |
| 330 | ;; if the rest of the string should have a face, put it there | 339 | ;; if the rest of the string should have a face, put it there |
| 331 | (when face | 340 | (when face |
| @@ -335,13 +344,13 @@ See `ansi-color-unfontify-region' for a way around this." | |||
| 335 | (let (fragment) | 344 | (let (fragment) |
| 336 | (if (string-match "\033" string start) | 345 | (if (string-match "\033" string start) |
| 337 | (let ((pos (match-beginning 0))) | 346 | (let ((pos (match-beginning 0))) |
| 338 | (setq fragment (substring string pos) | 347 | (setq fragment (substring string pos)) |
| 339 | result (concat result (substring string start pos)))) | 348 | (push (substring string start pos) result)) |
| 340 | (setq result (concat result (substring string start)))) | 349 | (push (substring string start) result)) |
| 341 | (if (or face fragment) | 350 | (if (or face fragment) |
| 342 | (setq ansi-color-context (list face fragment)) | 351 | (setq ansi-color-context (list face fragment)) |
| 343 | (setq ansi-color-context nil))) | 352 | (setq ansi-color-context nil))) |
| 344 | result)) | 353 | (apply 'concat (nreverse result)))) |
| 345 | 354 | ||
| 346 | ;; Working with regions | 355 | ;; Working with regions |
| 347 | 356 | ||
| @@ -354,7 +363,7 @@ position processed.") | |||
| 354 | (make-variable-buffer-local 'ansi-color-context-region) | 363 | (make-variable-buffer-local 'ansi-color-context-region) |
| 355 | 364 | ||
| 356 | (defun ansi-color-filter-region (begin end) | 365 | (defun ansi-color-filter-region (begin end) |
| 357 | "Filter out all SGR control sequences from region BEGIN to END. | 366 | "Filter out all ANSI control sequences from region BEGIN to END. |
| 358 | 367 | ||
| 359 | Every call to this function will set and use the buffer-local variable | 368 | Every call to this function will set and use the buffer-local variable |
| 360 | `ansi-color-context-region' to save position. This information will be | 369 | `ansi-color-context-region' to save position. This information will be |
| @@ -365,23 +374,27 @@ it will override BEGIN, the start of the region. Set | |||
| 365 | (start (or (cadr ansi-color-context-region) begin))) | 374 | (start (or (cadr ansi-color-context-region) begin))) |
| 366 | (save-excursion | 375 | (save-excursion |
| 367 | (goto-char start) | 376 | (goto-char start) |
| 368 | ;; find the next escape sequence | 377 | ;; Delete unrecognized escape sequences. |
| 378 | (while (re-search-forward ansi-color-drop-regexp end-marker t) | ||
| 379 | (replace-match "")) | ||
| 380 | (goto-char start) | ||
| 381 | ;; Delete SGR escape sequences. | ||
| 369 | (while (re-search-forward ansi-color-regexp end-marker t) | 382 | (while (re-search-forward ansi-color-regexp end-marker t) |
| 370 | ;; delete the escape sequence | ||
| 371 | (replace-match "")) | 383 | (replace-match "")) |
| 372 | ;; save context, add the remainder of the string to the result | 384 | ;; save context, add the remainder of the string to the result |
| 373 | (if (re-search-forward "\033" end-marker t) | 385 | (if (re-search-forward "\033" end-marker t) |
| 374 | (setq ansi-color-context-region (list nil (match-beginning 0))) | 386 | (setq ansi-color-context-region (list nil (match-beginning 0))) |
| 375 | (setq ansi-color-context-region nil))))) | 387 | (setq ansi-color-context-region nil))))) |
| 376 | 388 | ||
| 377 | (defun ansi-color-apply-on-region (begin end) | 389 | (defun ansi-color-apply-on-region (begin end) |
| 378 | "Translates SGR control sequences into overlays or extents. | 390 | "Translates SGR control sequences into overlays or extents. |
| 391 | Delete all other control sequences without processing them. | ||
| 379 | 392 | ||
| 380 | Applies SGR control sequences setting foreground and background colors | 393 | SGR control sequences are applied by setting foreground and |
| 381 | to text in region between BEGIN and END using extents or overlays. | 394 | background colors to the text between BEGIN and END using |
| 382 | Emacs will use overlays, XEmacs will use extents. The colors used are | 395 | overlays. The colors used are given in `ansi-color-faces-vector' |
| 383 | given in `ansi-color-faces-vector' and `ansi-color-names-vector'. See | 396 | and `ansi-color-names-vector'. See `ansi-color-apply-sequence' |
| 384 | function `ansi-color-apply-sequence' for details. | 397 | for details. |
| 385 | 398 | ||
| 386 | Every call to this function will set and use the buffer-local variable | 399 | Every call to this function will set and use the buffer-local variable |
| 387 | `ansi-color-context-region' to save position and current face. This | 400 | `ansi-color-context-region' to save position and current face. This |
| @@ -394,11 +407,16 @@ start of the region and set the face with which to start. Set | |||
| 394 | (copy-marker begin))) | 407 | (copy-marker begin))) |
| 395 | (end-marker (copy-marker end)) | 408 | (end-marker (copy-marker end)) |
| 396 | escape-sequence) | 409 | escape-sequence) |
| 410 | ;; First, eliminate unrecognized ANSI control sequences. | ||
| 411 | (save-excursion | ||
| 412 | (goto-char start-marker) | ||
| 413 | (while (re-search-forward ansi-color-drop-regexp end-marker t) | ||
| 414 | (replace-match ""))) | ||
| 397 | (save-excursion | 415 | (save-excursion |
| 398 | (goto-char start-marker) | 416 | (goto-char start-marker) |
| 399 | ;; find the next escape sequence | 417 | ;; Find the next SGR sequence. |
| 400 | (while (re-search-forward ansi-color-regexp end-marker t) | 418 | (while (re-search-forward ansi-color-regexp end-marker t) |
| 401 | ;; colorize the old block from start to end using old face | 419 | ;; Colorize the old block from start to end using old face. |
| 402 | (when face | 420 | (when face |
| 403 | (ansi-color-set-extent-face | 421 | (ansi-color-set-extent-face |
| 404 | (ansi-color-make-extent start-marker (match-beginning 0)) | 422 | (ansi-color-make-extent start-marker (match-beginning 0)) |