ANSI Coloring in Compilation Mode

后端 未结 5 1671
别那么骄傲
别那么骄傲 2020-12-08 06:29

Have anyone added support for ansi-color in compilation-mode Emacs? If so what property/attribute does the color-writing program have to check for in order to make sure its

相关标签:
5条回答
  • 2020-12-08 07:11

    Riffing on @stribb's solution, which riffs on @gavenkoa's solution, this is how to set it up with the awesome use-package:

    (use-package ansi-color
      :config
      (defun my-colorize-compilation-buffer ()
        (when (eq major-mode 'compilation-mode)
          (ansi-color-apply-on-region compilation-filter-start (point-max))))
      :hook (compilation-filter . my-colorize-compilation-buffer))
    
    0 讨论(0)
  • 2020-12-08 07:19

    Riffing on @gavenkoa's solution:

    (when (require 'ansi-color nil t)
      (defun my-colorize-compilation-buffer ()
        (when (eq major-mode 'compilation-mode)
          (ansi-color-apply-on-region compilation-filter-start (point-max))))
      (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
    

    This will not block errors but will still not raise an error if ansi-color is unavailable. Personally, I find the wildcard catch semantics of ignore-error distasteful.

    0 讨论(0)
  • 2020-12-08 07:21

    My optimized solution which don't pollute M-x grep (only for M-x compile):

    (ignore-errors
      (require 'ansi-color)
      (defun my-colorize-compilation-buffer ()
        (when (eq major-mode 'compilation-mode)
          (ansi-color-apply-on-region compilation-filter-start (point-max))))
      (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
    
    0 讨论(0)
  • 2020-12-08 07:21

    As of 2020, the most modern way appears to be the xterm-color Emacs package.

    See my answer on the duplicate question for details.

    0 讨论(0)
  • 2020-12-08 07:25

    There's already a function for applying color to comint buffers. You simply need to enable it on compilation buffers:

    (require 'ansi-color)
    (defun colorize-compilation-buffer ()
      (toggle-read-only)
      (ansi-color-apply-on-region compilation-filter-start (point))
      (toggle-read-only))
    (add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
    

    Color writing programs should check the TERM environment variable and the terminfo database to check if the terminal supports color. In practice, a lot of programs ignore this and rely on a user setting. Emacs will set the compilation terminal type to dumb by default but this can be overriden by setting the compilation-environment variable.

    Update: Note that in Emacs 24.5 the two calls to (toggle-read-only) in the code above are not needed.

    0 讨论(0)
提交回复
热议问题