3

I have a bash script which Sweaves .Rnw files into pdf's.

sweave_to_pdf ~/Foo.Rnw

I am wanting to write a emacs function to invoke this script on the current buffer. So far I have this:

(defun sweave_to_pdf ()
  "sweave_to_pdf function"
(interactive)
(shell-command
"~/Scripts/Shell/sweave_to_pdf.sh" (buffer-file-name (window-buffer (minibuffer-selected-window))))
)

It seems the file name and path of the buffer is not being passed to the shell script as a line argument. Any ideas how I could achieve this?

2 Answers2

2

The invocation needs to be a single string as the second and third argument refer to the output and error buffer:

(defun sweave-to-pdf ()
  "sweave_to_pdf function"
  (interactive)
  (shell-command (format "%s %s" "~/Scripts/Shell/sweave_to_pdf.sh"
                         buffer-file-name)))
wasamasa
  • 22,178
  • 1
  • 66
  • 99
2
  1. As YoungFrog mentions, you must quote the file name. Otherwise, the code fails if, for instance, the file name contains whitespaces.

  2. You might explicitely check that the current buffer is visiting a file.

(defun sweave-to-pdf ()
  "Export a sweave file to pdf."
  (interactive)
  (let ((file buffer-file-name)
        (script "~/Scripts/Shell/sweave_to_pdf.sh"))
    (unless file (user-error "Buffer must be visiting a file"))
    (shell-command (format "%s %s" script (shell-quote-argument file)))))
Tino
  • 420
  • 5
  • 9