1

我制作了一个小的 elisp 代码来处理环境模块(http://modules.sourceforge.net/),如下所示:

(defun Modules-module (command)
  "foo"

  (interactive (list (read-string "Module cmd: " nil nil)))
    ;; clear cmd buffer, log buffer is replaced below

  (setq cmd-buffer (get-buffer-create "*modules-cmd*"))
  (setq log-buffer (get-buffer-create "*modules-log*"))

  (set-buffer cmd-buffer)
  (delete-region (point-min) (point-max))

  (set-buffer log-buffer)
  (delete-region (point-min) (point-max))

  ;; (start-process "Module" cmd-buffer "/usr/bin/tclsh" "/usr/share/Modules/default/libexec/modulecmd.tcl" "lisp" command)
  (make-process :name "Module" :buffer cmd-buffer :command (list "/usr/bin/tclsh" "/usr/share/Modules/default/libexec/modulecmd.tcl" "lisp" command) :stderr log-buffer)

  (set-buffer cmd-buffer)
  (beginning-of-buffer)
  (delete-matching-lines "Process")

  (set-buffer cmd-buffer)
  (if (> (buffer-size) 0)
      (eval-buffer)
    )
  )

结果已成功插入 cmd-buffer,但缓冲区在缓冲区末尾有消息“处理模块完成”,如下所示:

(setenv "LOADEDMODULES" "gcc/6.2.1")
(setenv "CXX" "g++")
(setenv "COMPILER_TYPE" "gcc")
(setenv "COMPILER_OF_TYPE" "Gcc")
(setenv "FC" "gfortran")
(setenv "COMPILER" "gnu")
(setenv "LOADEDMODULES_modshare" "gcc/6.2.1:1")
(setenv "FORTRAN_TYPE" "gfortran")

Process Module finished

由于“处理模块完成”,(eval-buffer)不起作用。

我添加了 (set-buffer cmd-buffer) (beginning-of-buffer) (delete-matching-lines "Process")

在 (eval-buffer) 语句之前但失败了。

我的想法是 1. 通过使用 (eval-region) 从缓冲区的开头到 Process 完成行之前的行。或 2. 删除 Process finished 行并使用 (eval-buffer)

任何建议都将不胜感激。

4

2 回答 2

3

make-process是异步的,所以在调用eval-buffer. 但是对于您的直接问题,此字符串只是由默认进程哨兵插入。

所以就

(let ((proc (make-process ...)))
  (set-process-sentinel proc #'ignore)
  ...)

甚至只是

(make-process ... :sentinel #'ignore)

应该摆脱它。

于 2017-03-16T21:44:27.650 回答
1

也许是这样的:

(defun Modules-module (command)
  (with-temp-buffer
    (shell-command
     (concat "/usr/bin/tclsh /usr/share/Modules/default/libexec/modulecmd.tcl lisp "
             (shell-quote-argument command))
     (current-buffer))
    (eval-buffer)))

此外,虽然这不是您实际问题的一部分,但请let用于局部变量。

(let ((cmd-buffer (get-buffer-create "*modules-cmd*")))
  (with-current-buffer cmd-buffer
    (eq cmd-buffer (current-buffer))))
于 2017-03-15T17:11:59.187 回答