1 ;; Copyright (C) 2005 David Whiting, A.J. Rossini, Rich M. Heiberger, Martin
2 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
4 ;; Original Author: David Whiting <david.whiting@ncl.ac.uk>
5 ;; Created: 15 April 2005
6 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
8 ;; Keywords: Noweb, Literate Statistical Practice, Sweave
10 ;; This file is part of ESS.
12 ;; This file is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This file is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; Some simple functions for ESS and Sweave
30 ;; david.whiting at ncl.ac.uk
31 ;; Wed Sep 1 14:55:52 CEST 2004
33 ;; I have written some very simple elisp functions that I have found
34 ;; useful and thought others might like to see. I dabble with elisp
35 ;; occasionally at best so there are probably better ways to do what I
36 ;; have done, but so far this seems to work for me. There are several
37 ;; things that are hard-coded, I use Linux and I think that this would
38 ;; probably not work in Windows (not as it is now anyway).
40 ;; With these functions and key bindings all I need to do is open a .Rnw
41 ;; file and, assuming R is running, I press:
43 ;; M-n s to Sweave the file, then
44 ;; M-n l to run latex on the results of Sweave, then
45 ;; M-n p to make and display a postscript file , or
46 ;; M-n P to make and display a PDF version.
48 ;; David Whiting to Anthony Rossini, Mar 30
49 ;; On Wed, Mar 30, 2005 at 11:51:26AM +0200, A.J. Rossini wrote:
50 ;; > I'm going to go ahead and add this stuff to the distribution tonight
51 ;; > if you don't mind. I'd forgotten about it!
52 ;; It would make me very happy indeed.
53 ;; > (however, need permission to do so).
54 ;; Permission granted!
59 ;; School of Clinical Medical Sciences, The Medical School
60 ;; University of Newcastle upon Tyne, NE2 4HH, UK.
65 ;;; 1. I want to be able to send ess-swv-latex a parameter to tell it
66 ;;; the number of times to run LaTeX (to get references updated
69 ;;; 2. Also need to add ess-swv-Bibtex.
71 ;;; 3. Might be good to have a way to chain commands.
73 ;; 4. ADD to the ../doc/ess.texi !!
75 ;; 5. add a sub-menu [Sweave] to the [Noweb] menu to teach users about
78 ;;; Autoloads and Requires
85 ;; MM: I think we should *not* require 'cl, but it's needed for
86 ;; (search .) below ... -> ``please'' replace the (search ...) parts
87 ;; (string-match "\\.Rnw$" ....) |--> (nil | index) should suffice
88 ;; ==> replaced ' (search ".Rnw" buf) '
89 ;; by ' (string-match "\\.Rnw$" buf '
92 (defun ess-swv-run-in-R (cmd &optional choose-process
)
93 "Run \\[cmd] on the current .Rnw file. Utility function not called by user."
94 (let* ((rnw-buf (current-buffer)))
95 (if choose-process
;; previous behavior
96 (ess-force-buffer-current "R process to load into: ")
98 (update-ess-process-name-list)
99 (cond ((= 0 (length ess-process-name-list
))
100 (message "no ESS processes running; starting R")
101 (sit-for 1); so the user notices before the next msgs/prompt
105 ((not (string= "R" (ess-make-buffer-current))); e.g. Splus, need R
106 (ess-force-buffer-current "R process to load into: "))
110 (ess-execute (format "require(tools)")) ;; Make sure tools is loaded.
111 (basic-save-buffer); do not Sweave/Stangle old version of file !
112 (let* ((sprocess (get-ess-process ess-current-process-name
))
113 (sbuffer (process-buffer sprocess
))
114 (rnw-file (buffer-file-name))
115 (Rnw-dir (file-name-directory rnw-file
))
118 "local({..od <- getwd(); setwd(%S); %s(%S); setwd(..od) })"
119 Rnw-dir cmd rnw-file
))
121 (message "%s()ing %S" cmd rnw-file
)
122 (ess-execute Sw-cmd
'buffer nil nil
)
123 (switch-to-buffer rnw-buf
)
124 (ess-show-buffer (buffer-name sbuffer
) nil
)))))
126 (defun ess-swv-tangle ()
127 "Run Stangle on the current .Rnw file."
129 (ess-swv-run-in-R "Stangle"))
131 (defun ess-swv-weave ()
132 "Run Sweave on the current .Rnw file."
134 (ess-swv-run-in-R "Sweave"))
136 (defun ess-swv-latex ()
137 "Run LaTeX on the product of Sweave()ing the current file."
140 (let* ((namestem (file-name-sans-extension (buffer-file-name)))
141 (latex-filename (concat namestem
".tex"))
142 (tex-buf (get-buffer-create " *ESS-tex-output*")))
143 (message "Running LaTeX on '%s' ..." latex-filename
)
144 (switch-to-buffer tex-buf
)
145 (call-process "latex" nil tex-buf
1 latex-filename
)
146 (switch-to-buffer (buffer-name))
147 (display-buffer tex-buf
)
148 (message "Finished running LaTeX" ))))
152 "Create a postscript file from a dvi file (name based on the current
153 Sweave file buffer name) and display it."
155 (let* ((buf (buffer-name))
156 (namestem (file-name-sans-extension (buffer-file-name)))
157 (dvi-filename (concat namestem
".dvi"))
158 (psviewer (ess-get-ps-viewer)))
159 (shell-command (concat "dvips -o temp.ps " dvi-filename
))
160 (shell-command (concat psviewer
" temp.ps & "))
161 (switch-to-buffer buf
)
164 (defun ess-swv-PDF ()
165 "Create a PDF file ('pdflatex') and display it."
167 (let* ((buf (buffer-name))
168 (namestem (file-name-sans-extension (buffer-file-name)))
169 (latex-filename (concat namestem
".tex"))
170 (tex-buf (get-buffer-create " *ESS-tex-output*"))
171 (pdfviewer (ess-get-pdf-viewer))
173 ;;(shell-command (concat "pdflatex " latex-filename))
174 (message "Running pdfLaTeX on '%s' ..." latex-filename
)
175 (switch-to-buffer tex-buf
)
177 (call-process "pdflatex" nil tex-buf
1 latex-filename
))
178 (if (not (= 0 pdf-status
))
179 (message "** OOPS: error in 'pdflatex' (%d)!" pdf-status
)
180 ;; else: pdflatex probably ok
181 (shell-command (concat pdfviewer
" " namestem
".pdf &")))
182 (switch-to-buffer buf
)
183 (display-buffer tex-buf
)))
186 (defun ess-insert-Sexpr ()
187 "Insert Sexpr{} into the buffer at point."
193 ;;; back-compatible wrappers:
194 (defun ess-makeSweave () "old *DEPRECATED* version of \\[ess-swv-weave]."
197 "** warning: ess-makeSweave is deprecated. Do use (ess-swv-weave) instead!")
200 (defun ess-makeLatex () "old *DEPRECATED* version of \\[ess-swv-latex]."
203 "** warning: ess-makeLatex is deprecated. Do use (ess-swv-latex) instead!")
206 (defun ess-makePS () "old *DEPRECATED* version of \\[ess-swv-PS]."
209 "** warning: ess-makePS is deprecated. Do use (ess-swv-PS) instead!")
212 (defun ess-makePDF () "old *DEPRECATED* version of \\[ess-swv-PDF]."
215 "** warning: ess-makePDF is deprecated. Do use (ess-swv-PDF) instead!")
219 ;;; Now bind some keys.
220 (define-key noweb-minor-mode-map
"\M-ns" 'ess-swv-weave
)
221 (define-key noweb-minor-mode-map
"\M-nT" 'ess-swv-tangle
)
222 (define-key noweb-minor-mode-map
"\M-nl" 'ess-swv-latex
)
223 (define-key noweb-minor-mode-map
"\M-np" 'ess-swv-PS
)
224 (define-key noweb-minor-mode-map
"\M-nP" 'ess-swv-PDF
)
226 (define-key noweb-minor-mode-map
"\M-nx" 'ess-insert-Sexpr
)
234 \f ; Local variables section
236 ;;; This file is automatically placed in Outline minor mode.
237 ;;; The file is structured as follows:
240 ;;; Subsections: ;;;*;;;
241 ;;; Components: defuns, defvars, defconsts
242 ;;; Random code beginning with a ;;;;* comment
246 ;;; outline-minor-mode: nil
247 ;;; mode: outline-minor
248 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
251 ;;; ess-swv.el ends here