1

I am using following code to apply colorize text in emacs, such as for ^[ characters to apply color. In order to use this code I have to select text to apply it

reference: https://unix.stackexchange.com/a/19505/198423

(defun ansi-color-apply-on-region-int (beg end)
  "interactive version of func"
  (interactive "r")
  (ansi-color-apply-on-region beg end))

How can I automatically apply it for the complete buffer? Is there any alternative solution to use it as a mode something like ansi-color-mode?

Drew
  • 75,699
  • 9
  • 109
  • 225
alper
  • 1,238
  • 11
  • 30

1 Answers1

1

If that function does what you want on the region then this should do what you want on the entire buffer:

(ansi-color-apply-on-region-int (point-min) (point-max))

If you want to do it interactively then C-x h followed by M-x ansi-color-apply-on-region-int.

Or use this command:

(defun ansi-color-on-buffer ()
  "..."
  (interactive)
  (ansi-color-apply-on-region (point-min) (point-max)))

If the buffer is narrowed, that acts on the visible portion. If you instead want to act on the full buffer, even if it's narrowed, then change (point-min) to 1 and (point-max) to (buffer-size).

If you want a minor mode:

(define-minor-mode ansi-color-mode 
  "..."
  nil nil nil
  (ansi-color-apply-on-region 1 (buffer-size)))

Replace the "..." occurrences with doc strings that say what each function does. See the Elisp manual, node Function Documentation for info about writing doc strings for functions.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Can `"..."` remain or should I replace it with some other valid string? – alper Dec 30 '21 at 00:21
  • Yes, replace it with a doc string that describes what the minor mode does - what it's for. I updated the answer with some info about writing doc strings. – Drew Dec 30 '21 at 05:03
  • would it be possible to change themes for the `ansi-color-mode`, such as can I apply https://github.com/dracula/emacs ? I can ask different question since it is not related – alper Dec 30 '21 at 18:53
  • Please ask a different question for that. (And I have no idea about that. Hopefully someone else will be able to help.) – Drew Dec 30 '21 at 20:44