I'd like to read a serial link from Emacs using serial-term. The output looks wrong, like if the new line characters did not trigger carriage return:

First line
          Second line
                     Third line

Reading from this serial line using ansi-term shows the expected output:

A first line
A second line
Third line

I tried to use set-buffer-file-coding-system to unix in the serial-term buffer, but this did not change anything.

Is it possible to get serial-term to interpret new line characters as carriage return and a line feed?


Here's my solution, that doesn't involve modifying the Emacs source. I copy the definition of serial-term from term.el and provide a process filter that inserts \r before any \n before passing the output to the term-mode process filter.

(defun replace-all (string to-find to-replace)
  (let ((index  (cl-search to-find string))
        (pos    0)
        (result ""))
    (while index
      (setq result (concat result
                           (substring string pos index)
            pos    (+ index (length to-find))
            index  (cl-search to-find string :start2 pos)))
    (concat result (substring string pos))))

(defun rephone-serial-process-filter (process output)
  "Replace LF in output string with CR+LF."
  (term-emulate-terminal process
                         (replace-all output
                                      (byte-to-string ?\n)
                                      (string ?\r ?\n))))

(defun rephone-serial-term (port)
  "Basically duplicate SERIAL-TERM from term.el but with process
  filtering to translate LF to CR+LF."
  (interactive (list (serial-read-name)))
  (let* ((process (make-serial-process
                   :port port
                   :speed 115200
                   :bytesize 8
                   :parity nil
                   :stopbits 1
                   :flowcontrol nil
                   :coding 'raw-text-unix
                   :noquery t
                   :name (format "Lua:%s" port)
                   :filter 'rephone-serial-process-filter
                   :sentinel 'term-sentinel))
         (buffer (process-buffer process)))
    (with-current-buffer buffer
      (goto-char (point-max))
      (set-marker (process-mark process) (point)))
    (switch-to-buffer buffer)

Patch to ${emacs_root}/lisp/term.el:

--- term.el 2016-10-06 01:34:52.000000000 +0800
+++ fix-term.el 2016-10-06 01:35:25.000000000 +0800
@@ -2857,6 +2857,8 @@ See `term-prompt-regexp'."
          (term-vertical-motion 0)
          (setq term-current-column term-start-line-column))
             ((eq char ?\n)  ;; (terminfo: cud1, ind)
+             (term-vertical-motion 0)
+             (setq term-current-column term-start-line-column)
          (unless (and term-kill-echo-list
            (term-down 1 t)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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