【问题标题】:How to handle make-process output including control character如何处理包含控制字符的制作过程输出
【发布时间】:2020-05-21 04:06:45
【问题描述】:

我想处理包含控制字符的make-process 输出。

(setq proc
      (make-process
       :name "sh"
       :buffer (get-buffer-create "*proc*")
       :command '("hub" "clone" "emacs-mirror/emacs")
       :filter (lambda (proc string)
                 (when (buffer-live-p (process-buffer proc))
                   (with-current-buffer (process-buffer proc)
                     (let ((moving (= (point) (process-mark proc))))
                       (save-excursion
                         (goto-char (process-mark proc))
                         (insert string)
                         (set-marker (process-mark proc) (point)))
                       (if moving (goto-char (process-mark proc)))))))))

但是,控制字符按原样插入。如何像shell一样处理?

注意:可能是相关的手册页

【问题讨论】:

  • 当终端看到控制字符\r时,会将光标移动到当前行的开头,你可以将\r替换为\n,它应该会提供更好的输出,虽然并不完美。跨度>
  • 是的,将 \r 替换为 \n 会产生更好的结果,但 Elisp shell 模拟器(M-x eshell, shell)效果很好。我觉得 Emacs 应该有这个句柄函数或者方法...?
  • 似乎Eshell根据eshell-handle-control-codes看到\r时只是删除了整个当前行
  • 谢谢您的指点。 Eshell 是解决这个问题的好老师。

标签: shell emacs control-characters


【解决方案1】:

使用 Eshell 内部输出过滤器会产生很好的效果。

不需要整个Eshell(不要输入eshell-mode),设置一些标记让我们可以使用eshell-output-filter函数。

(with-current-buffer (get-buffer-create "*proc*")
  (set (make-local-variable 'eshell-last-input-start) (point-marker))
  (set (make-local-variable 'eshell-last-input-end) (point-marker))
  (set (make-local-variable 'eshell-last-output-start) (point-marker))
  (set (make-local-variable 'eshell-last-output-end) (point-marker))
  (set (make-local-variable 'eshell-last-output-block-begin) (point)))
;;=> 1

(setq proc
      (make-process
       :name "sh"
       :buffer (get-buffer-create "*proc*")
       :command '("hub" "clone" "emacs-mirror/emacs")
       :filter (lambda (proc string)
                 (when (buffer-live-p (process-buffer proc))
                   (with-current-buffer (process-buffer proc)
                     (let ((moving (= (point) (process-mark proc))))
                       (save-excursion
                         (goto-char (process-mark proc))
                         (let ((inhibit-read-only t))
                           (eshell-output-filter proc string))
                         (set-marker (process-mark proc) (point)))
                       (if moving (goto-char (process-mark proc)))))))))
;;=> #<process sh>

(delete-process proc)    ; kill the process when you want
;;=> nil

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2015-05-20
    • 2018-04-15
    • 1970-01-01
    相关资源
    最近更新 更多