1

I'm trying to get ansi colors from my bash process, but I can't get this to work.

(setq shell-process-name "shell-process-name")
(setq shell-process-buffer "*shell-process-buffer*")

(defun open-shell-process ()
  (let ((buf shell-process-buffer)
        (coding-system-for-read 'binary)
        (default-directory "~/"))
    (start-process shell-process-name
                   buf
                   "/bin/bash")
    (let ((proc (get-buffer-process buf)))
      (set-process-filter proc 'shell-process-filter)
      (accept-process-output proc 0.1))
    (with-current-buffer buf
      (erase-buffer))))

(defun shell-process-filter (proc output)
  (let ((buf (process-buffer proc)))
    (with-current-buffer buf
      (goto-char (point-max))
      (insert (ansi-color-apply output)))))

Do I need to change process-environment or what am I missing here ?

bertfred
  • 1,699
  • 1
  • 11
  • 23
  • 1
    Yup, I'd certainly try putting something other than `TERM=dumb` into `process-environment` when starting this process. – wvxvw Dec 05 '17 at 11:01
  • 1
    I have TERM=xterm-256color and that works with emacs -Q; M-x shell. Reviewing this comment, you really need to provide basics like emacs version, OS, and the commands you are running (M-x shell? M-x term?). – Realraptor Dec 05 '17 at 11:45
  • @wvxvw Thanks. It works, but `ansi-color-apply` seems to be broken. @Realraptor I don't want to change the emacs shells. – bertfred Dec 05 '17 at 13:08
  • 1
    `ansi-color-apply` doesn't work here because it applies text properties to the string. This is correct for strings, but when the string is inserted into the buffer, font-locking overrides the face properties. – npostavs Sep 08 '18 at 01:54
  • This seems to be a good alternative https://github.com/atomontage/xterm-color – bertfred Sep 08 '18 at 10:54

1 Answers1

2

Extending Realraptor's comment with the answer here: .bash_profile or .bashrc for shell in emacs?

Stick TERM=xterm-256color into your .bashrc, and for good measure, this in .emacs:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
(add-to-list 'comint-output-filter-functions 'ansi-color-process-output)
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
Joe Corneli
  • 1,786
  • 1
  • 14
  • 27