Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs overwrite with carriage return

Tags:

emacs

I like using start-process-shell-command to launch off subprocesses, such as compilations, renderings, or unit tests, from within emacs. I know that I can have the output redirected into a buffer by giving a buffer name.

(start-process-shell-command "proc-name" "output-buffer-name" command)

Many processes will use carriage returns for real-time progress bars, so that in the terminal the progress bar takes up only a single line in the final output. However, when this progress bar is redirected into an emacs buffer, the carriage return characters are kept, and so the buffer shows all of the status updates, making it be a pain to read through the output.

Is there a way to have emacs treat carriage returns in the output buffers in the same way that the terminal treats carriage returns? That is, return the pointer to the beginning of the line and overwrite the existing text.

like image 672
Eldritch Cheese Avatar asked Sep 04 '25 03:09

Eldritch Cheese


1 Answers

You can do this with a filter function.

It's a bit of work, but you just need to find the line terminated by the \r in the output, then find the old line in the buffer, delete the line, and replace it with the new line. Here's a toy version:

// foo.c
#include <stdio.h>
main() {
  int i;
  for (i = 0; i < 10; i++) {
    printf("  count: %d\r", i);
    fflush(stdout);
    sleep(1);
  }
  printf("\n");
}

Then you can have each count line overwrite the previous line (in this case by wiping the entire buffer.)

(defun filt (proc string)
  (with-current-buffer "foo"
    (delete-region (point-min) (point-max))
    (insert string)))

(progn 
  (setq proc
        (start-process "foo" "foo" "path/to/foo"))
  (set-process-filter proc 'filt))
like image 181
seanmcl Avatar answered Sep 06 '25 01:09

seanmcl