2

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?

1 Answer 1

2

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.

4
  • Can "..." remain or should I replace it with some other valid string?
    – alper
    Commented Dec 30, 2021 at 0: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
    Commented Dec 30, 2021 at 5:03
  • would it be possible to change themes for the ansi-color-mode, such as can I apply github.com/dracula/emacs ? I can ask different question since it is not related
    – alper
    Commented Dec 30, 2021 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
    Commented Dec 30, 2021 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.