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.

5
  • You may wish to consider using a process-filter to output or suppress whatever you want: gnu.org/software/emacs/manual/html_node/elisp/…
    – lawlist
    Mar 15, 2017 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, 2017 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. emacs.stackexchange.com/a/5844/2287 You could also search and replace in the start-process buffer.
    – lawlist
    Mar 15, 2017 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 ...: stackoverflow.com/a/23178396/2112489
    – lawlist
    Mar 15, 2017 at 16:29
  • @lawlist thanks for the advice and thr links.
    – Seong
    Mar 20, 2017 at 11:17

2 Answers 2

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.

3
  • 1
    This is a lifesaver. Note, you can also pass :sentinel #'ignore directly to make-process. Jul 27, 2017 at 5:38
  • 1
    @RadonRosborough: very good point, indeed, thanks. I added it to my answer.
    – Stefan
    Jul 27, 2017 at 13:08
  • 1
    If you use make-process with :stderr and you want to remove the message from there too, use this: (set-process-sentinel (get-buffer-process your-stderr-buffer) #'ignore) Aug 17, 2017 at 18:03
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))))
1
  • your code perfectly works for my case. Thanks a lot.
    – Seong
    Mar 20, 2017 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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