Initial Commit
[packages] / xemacs-packages / ess / lisp / ess-swv.el
1 ;; Copyright (C) 2005 David Whiting, A.J. Rossini, Rich M. Heiberger, Martin
2 ;;      Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
3
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>
7
8 ;; Keywords: Noweb, Literate Statistical Practice, Sweave
9
10 ;; This file is part of ESS.
11
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)
15 ;; any later version.
16
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.
21
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.
25
26 ;;; Commentary:
27
28 ;;
29 ;; Some simple functions for ESS and Sweave
30 ;; david.whiting at ncl.ac.uk david.whiting at ncl.ac.uk
31 ;; Wed Sep 1 14:55:52 CEST 2004
32
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).
39
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:
42
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.
47
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!
55
56 ;; Dave
57 ;; --
58 ;; David Whiting
59 ;; School of Clinical Medical Sciences, The Medical School
60 ;; University of Newcastle upon Tyne, NE2 4HH, UK.
61
62
63 ;;; TODO:
64 ;;;
65 ;;; 1. I want to be able to send ess-makeLatex a parameter to tell it
66 ;;; the number of times to run LaTeX (to get references updated
67 ;;; correctly).
68 ;;;
69 ;;; 2. Also need to add ess-makeBibtex.
70 ;;;
71 ;;; 3. Might be good to have a way to chain commands.
72 ;;;
73
74 ;;; Autoloads and Requires
75
76 (require 'ess-noweb)
77
78 (defun ess-makeSweave ()
79    "Run Sweave on the current .Rnw file."
80    (interactive)
81    (save-excursion
82      ;; Make sure tools is loaded.
83      (setq ess-command (format "library(tools)"))
84      (ess-execute ess-command)
85      (message "Sweaving %S" (buffer-file-name))
86      (setq ess-command (format "Sweave(%S)" (buffer-file-name)))
87      (ess-execute ess-command 'buffer nil nil)))
88
89
90 (defun ess-makeLatex ()
91    "Run LaTeX on the product of Sweave()ing the current file."
92    (interactive)
93    (save-excursion
94      (let* ((thisbuffer (buffer-name))
95             (namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
96             (latex-filename (concat namestem ".tex")))
97        (message "Running LaTeX ..." )
98        (switch-to-buffer "*tex-output*")
99        (call-process "latex" nil "*tex-output*" 1 latex-filename)
100        (switch-to-buffer thisbuffer)
101        (message "Finished running LaTeX" ))))
102
103
104 (defun ess-makePS ()
105    "Create a postscript file from a dvi file (name based on the current
106 Sweave file buffer name) and display it with gv."
107    (interactive)
108    (let ((namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
109          (dvi-filename (concat namestem ".dvi")))
110      (shell-command (concat "dvips -o temp.ps " dvi-filename))
111      (shell-command "gv temp.ps & ")))
112
113
114 (defun ess-makePDF ()
115    "Create a PDF file and display it with acroread."
116    (interactive)
117    (let* ((namestem (substring (buffer-name) 0 (search ".Rnw" (buffer-name))))
118          (tex-filename (concat namestem ".tex")))
119      (shell-command (concat "pdflatex " tex-filename))
120      (shell-command (concat "acroread " namestem ".pdf &"))))
121
122 (defun ess-insert-Sexpr ()
123  "Insert Sexpr{} into the buffer at point."
124  (interactive)
125  (insert "\\Sexpr{}")
126  (backward-char))
127
128
129 ;;; Now bind some keys.
130 (define-key noweb-minor-mode-map "\M-ns" 'ess-makeSweave)
131 (define-key noweb-minor-mode-map "\M-nl" 'ess-makeLatex)
132 (define-key noweb-minor-mode-map "\M-np" 'ess-makePS)
133 (define-key noweb-minor-mode-map "\M-nP" 'ess-makePDF)
134 (define-key noweb-minor-mode-map "\M-nx" 'ess-insert-Sexpr)
135
136
137 \f ; provides
138
139 (provide 'ess-swv)
140
141 \f ; Local variables section
142
143 ;;; This file is automatically placed in Outline minor mode.
144 ;;; The file is structured as follows:
145 ;;; Chapters:     ^L ;
146 ;;; Sections:    ;;*;;
147 ;;; Subsections: ;;;*;;;
148 ;;; Components:  defuns, defvars, defconsts
149 ;;;              Random code beginning with a ;;;;* comment
150
151 ;;; Local variables:
152 ;;; mode: emacs-lisp
153 ;;; outline-minor-mode: nil
154 ;;; mode: outline-minor
155 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
156 ;;; End:
157
158 ;;; ess-swv.el ends here