2

I made a small elisp code which handles environment module (http://modules.sourceforge.net/) as follows:

(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)
    )
  )

the results are inserted into cmd-buffer successfully but the buffer has message 'Process Module finished' at the end of buffer as follows:

(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

Due to the 'Process Module finished', (eval-buffer) does not work.

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

before the (eval-buffer) statement but failed.

My idea is 1. by using (eval-region) from the beginning of the buffer to the line before the Process finished line. or 2. remove the Process finished line and use (eval-buffer)

Any advice is grateful.

Seong
  • 556
  • 4
  • 18
  • You may wish to consider using a `process-filter` to output or suppress whatever you want: https://www.gnu.org/software/emacs/manual/html_node/elisp/Filter-Functions.html – lawlist Mar 15 '17 at 14:18
  • @lawlist could you show me a sample code how to remove the unwanted message from the stdout by using process-filter because I am not familiar with elisp coding. – Seong Mar 15 '17 at 15:54
  • Here is a link to an example where my `start-process` has `nil` for the process buffer (so that it doesn't create a buffer), and I use a `process-filter` to send just what I want to the `*Messages*` buffer. The `process-filter` has two arguments -- PROCESS and STRING. To determine what I wanted and did not want, I first looked at every string that was output -- e.g., `(message "my-output: %s" string)` and then I set up some rules with things like `string-match` or `string-equal`. http://emacs.stackexchange.com/a/5844/2287 You could also search and replace in the `start-process` buffer. – lawlist Mar 15 '17 at 16:25
  • Here is a link to another example using `start-process` to create a buffer (instead of using the `*Messages*` buffer in the previous example) -- it also has a search-and-replace ...: http://stackoverflow.com/a/23178396/2112489 – lawlist Mar 15 '17 at 16:29
  • @lawlist thanks for the advice and thr links. – Seong Mar 20 '17 at 11:17

2 Answers2

3

make-process is asynchronous, so you'll want to wait for it to finish before calling eval-buffer. But as for your immediate problem, this string is simply inserted by the default process sentinel.

So just

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

or even just

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

should get rid of it.

Stefan
  • 27,908
  • 4
  • 53
  • 82
1

Maybe something like:

(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)))

Also, though this isn't part of your actual question, use let for local variables.

(let ((cmd-buffer (get-buffer-create "*modules-cmd*")))
  (with-current-buffer cmd-buffer
    (eq cmd-buffer (current-buffer))))
jpkotta
  • 9,237
  • 3
  • 29
  • 34