1

When I run the test feature of Rust in Emacs compilation mode, with ansi-color mode loaded, somehow the character ^[(B is always displayed like below:

enter image description here

It looks quite annoying.

Does anyone know how to remove these characters from the compilation buffer?

Trung Ta
  • 301
  • 1
  • 11
  • Don't put an answer in the question. Post it separately as an answer. If you want, you can accept your own answer, e.g., if it answers your question better or more completely than other answers. – Drew Dec 01 '22 at 16:26
  • @Drew: Thanks! I just posted an answer instead – Trung Ta Dec 02 '22 at 02:26

2 Answers2

1

Make sure that your shell init files (~/.bashrc or similar) are not setting the TERM variable to anything. TERM will then keep its default value (which is probably dumb while M-x compile is running things), and your compiler will know that it is not supposed to print any escape sequences. If it still does, file a bug report on the compiler. Or in this case perhaps your test suite.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • Thanks a lot for the tip! Previously, I set `TERM` to `xterm-256color`. Now, I configure it to `(setenv "TERM" "xterm-color")` and the compilation mode works just fine! – Trung Ta Dec 01 '22 at 03:20
  • 1
    You shouldn’t set it to anything at all. A program that runs other programs (such as Emacs running your compiler) should be free to set it to the appropriate value. – db48x Dec 01 '22 at 03:23
  • Thanks for the advice. Using the default dumb shell, the color will not be displayed. I want to see the color too, so I wrote a quick hack only the change the `TERM` variable during the compilation (I updated this part in my question too) – Trung Ta Dec 01 '22 at 04:01
1

Following @db48x's answer, I wrote a quick function to advise M-x compile to use TERM=xterm-color during its session.

  ;; Advise compile to set TERM variable
  (defun tddsg--advice-compile (func &rest args)
    (let ((saved-term-env (getenv "TERM")))
      ;; Use xterm-color so that the compilation mode can display color properly
      (setenv "TERM" "xterm-color")
      (apply func args)
      (setenv "TERM" saved-term-env)))
  (advice-add 'compile :around #'tddsg--advice-compile)

Here is the result:

enter image description here

Trung Ta
  • 301
  • 1
  • 11