*** empty log message ***
[ess.git] / lisp / essd-r.el
blobb9fe7f97bf250f34a1636056509b7d0423359746
1 ;;; essd-r.el --- R customization
3 ;; Copyright (C) 1997--2005 A.J. Rossini, Rich M. Heiberger, Martin
4 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
6 ;; Original Author: A.J. Rossini
7 ;; Created: 12 Jun 1997
8 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
10 ;; Keywords: start up, configuration.
12 ;; This file is part of ESS.
14 ;; This file is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; This file is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 ;;; Commentary:
29 ;;; This file defines all the R customizations for ESS. See essl-s.el
30 ;;; for general S language customizations.
32 ;;; Autoloads and Requires
34 (ess-message "[essd-r:] (require 'essl-s)")
35 (require 'essl-s)
37 ;; modify S Syntax table:
38 (setq R-syntax-table S-syntax-table)
39 ;; R >= 1.8: back tick `string` -- unfortunately no *pair* checking:
40 ;; breaks when things like `..' are used:
41 ;; (modify-syntax-entry ?` "\"" R-syntax-table)
42 (modify-syntax-entry ?_ "_" R-syntax-table) ; foo_bar is symbol in R >=1.9
44 (ess-message "[essd-r:] (autoload ..) & (def** ..)")
46 (autoload 'inferior-ess "ess-inf" "Run an ESS process.")
47 (autoload 'ess-mode "ess-mode" "Edit an ESS process.")
49 ;;; Code:
51 (defvar R-customize-alist
52 (append
53 '((ess-local-customize-alist . 'R-customize-alist)
54 (ess-dialect . "R")
55 (ess-suffix . "R")
56 (ess-dump-filename-template . (ess-replace-regexp-in-string
57 "S$" ess-suffix ; in the one from custom:
58 ess-dump-filename-template-proto))
59 (ess-mode-syntax-table . R-syntax-table)
60 (ess-mode-editing-alist . R-editing-alist)
61 (ess-change-sp-regexp . ess-R-change-sp-regexp)
62 (ess-help-sec-regex . ess-help-R-sec-regex)
63 (ess-help-sec-keys-alist . ess-help-R-sec-keys-alist)
64 (ess-loop-timeout . ess-S-loop-timeout);fixme: dialect spec.
65 (ess-cmd-delay . ess-R-cmd-delay)
66 (ess-function-pattern . ess-R-function-pattern)
67 (ess-object-name-db-file . "ess-r-namedb.el" )
68 (ess-retr-lastvalue-command . "assign(\".Last.value\", .ess.lvsave, envir=NULL)\n") ; package:base
69 (ess-save-lastvalue-command . "assign(\".ess.lvsave\",.Last.value,inherits=TRUE)\n") ;envir=1
70 (ess-imenu-mode-function . 'ess-imenu-R)
71 (inferior-ess-program . inferior-R-program-name)
72 (inferior-ess-objects-command . inferior-R-objects-command)
73 (inferior-ess-font-lock-keywords . inferior-ess-R-font-lock-keywords)
74 (inferior-ess-search-list-command . "search()\n")
75 (inferior-ess-help-command . "help(\"%s\", htmlhelp=FALSE)\n")
76 (inferior-ess-exit-command . "q()")
77 (inferior-ess-exit-prompt . "Save workspace image? [y/n/c]: ")
78 (inferior-ess-primary-prompt . "\\([A-Z][][A-Za-z0-9.]*\\)*> ")
79 (inferior-ess-secondary-prompt . "+ ?")
80 ;;harmful for shell-mode's C-a: -- but "necessary" for ESS-help?
81 (inferior-ess-start-file . nil) ;; "~/.ess-R"
82 (inferior-ess-start-args . "")
83 (ess-STERM . "iESS")
84 (ess-editor . R-editor)
85 (ess-pager . R-pager)
87 S-common-cust-alist)
88 "Variables to customize for R")
90 ;;;### autoload
91 (defun R (&optional start-args)
92 "Call 'R', the 'GNU S' system from the R Foundation.
93 Optional prefix (C-u) allows to set command line arguments, such as
94 --vsize. This should be OS agnostic.
95 If you have certain command line arguments that should always be passed
96 to R, put them in the variable `inferior-R-args'."
97 (interactive "P")
98 (setq ess-customize-alist R-customize-alist)
99 (ess-write-to-dribble-buffer ;; for debugging only
100 (format
101 "\n(R): ess-dialect=%s, buf=%s, start-arg=%s\n current-prefix-arg=%s\n"
102 ess-dialect (current-buffer) start-args current-prefix-arg))
103 (let* ((r-always-arg
104 (if ess-microsoft-p
105 "--ess "
106 "--no-readline "))
107 (r-start-args
108 (concat r-always-arg
109 inferior-R-args
110 (if start-args
111 (read-string
112 (concat "Starting Args [other than `"
113 r-always-arg
114 "'] ? "))
115 nil)))
116 ;;Micro$ ?: default-process-coding-system ;-breaks UTF locales on Unix:
118 (if ess-microsoft-p
119 (setq default-process-coding-system '(undecided-dos . undecided-dos)))
120 (inferior-ess r-start-args) ;; (R)
121 (ess-write-to-dribble-buffer
122 (format "(R): inferior-ess-language-start=%s\n"
123 inferior-ess-language-start))
124 (if inferior-ess-language-start
125 (ess-eval-linewise inferior-ess-language-start))))
127 ;;;### autoload
128 (defun R-mode (&optional proc-name)
129 "Major mode for editing R source. See `ess-mode' for more help."
130 (interactive)
131 (setq ess-customize-alist R-customize-alist)
132 ;;(setq imenu-generic-expression R-imenu-generic-expression)
133 (ess-mode R-customize-alist proc-name)
134 (if (fboundp 'ess-add-toolbar) (ess-add-toolbar))
135 ;; ECB needs seminatic stuff.
136 ;; (if (featurep 'semantic)
137 ;; (setq semantic-toplevel-bovine-table r-toplevel-bovine-table))
138 (if ess-imenu-use-S
139 (progn (require 'ess-menu)
140 (ess-imenu-R)))
141 ;; MM: ^^^^^^^^^^^ should really use ess-imenu-mode-function from the
142 ;; alist above!
146 (fset 'r-mode 'R-mode)
148 (defun ess-r-versions-create ()
149 "Generate the `M-x R-X.Y' functions for starting other versions of R.
150 See `ess-r-versions' for strings that determine which functions are created.
152 The local variable `ess-r-versions-created' is used to return list of
153 the new R defuns, if any, that were created. The defuns will normally
154 be placed on the menubar upon ESS initialisation."
156 ;; This works by creating a temp buffer where the template function is
157 ;; edited so that X.Y is replaced by the version name
158 (let ((template "")
159 (beg)
160 (versions)
161 (version)
162 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
163 (ess-r-versions-created)
166 ;; This is the template function used for creating M-x R-X.Y.
167 (setq template "(defun R-X.Y (&optional start-args)
168 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
169 This function was generated by `ess-r-versions-create'.\"
170 (interactive \"P\")
171 (let ((inferior-R-program-name \"R-X.Y\"))
172 (R start-args)))
175 (save-excursion
176 (set-buffer eval-buf)
177 ;; clear the buffer.
178 (delete-region (point-min) (point-max))
180 ;; Find which versions of R we want. Remove the pathname, leaving just
181 ;; the name of the executable.
182 (setq versions
183 (mapcar 'file-name-nondirectory
184 (ess-uniq-list
185 (apply 'nconc
186 (mapcar 'ess-find-exec-completions
187 ess-r-versions)))))
188 (ess-write-to-dribble-buffer
189 (format "(R): ess-r-versions-create making M-x defuns for %s"
190 (mapconcat 'identity versions " ")))
191 (setq ess-r-versions-created versions) ;keep copy for returning at end.
192 ;; Iterate over each string in VERSIONS, creating a new defun each time.
193 (while versions
194 (setq version (car versions)
195 versions (cdr versions))
196 (setq beg (point))
197 (insert template)
198 (goto-char beg)
199 (while (search-forward "R-X.Y" nil t)
200 (replace-match version t t))
201 (goto-char (point-max))
203 ;; buffer has now been created with defuns, so eval them!
204 (eval-buffer)
205 (kill-buffer eval-buf)
207 ess-r-versions-created))
210 (defun ess-find-rterm (&optional ess-R-root-dir)
211 "Find the full path of all occurences of Rterm.exe under the ESS-R-ROOT-DIR.
212 If ESS-R-ROOT-DIR is nil, construct it by looking for an occurence of Rterm.exe
213 in the exec-path."
214 (let* ((Rpath)
215 (rwxxyy)
216 (rw)
217 (Rterm nil))
218 (if (not ess-R-root-dir)
219 (progn
220 (setq Rpath (executable-find "Rterm"))
221 (setq ess-R-root-dir
222 (if Rpath
223 (expand-file-name
224 (concat
225 (file-name-directory Rpath)
226 "../../"))
227 ""))))
228 (setq rwxxyy (file-name-all-completions "rw" ess-R-root-dir))
229 (while rwxxyy
230 (setq rw (car rwxxyy))
231 (setq rwxxyy (cdr rwxxyy))
232 (setq Rterm (cons (concat ess-R-root-dir rw "bin/Rterm.exe") Rterm)))
233 Rterm))
235 (defun ess-rterm-versions-create ()
236 "Generate the `M-x rwxxyy' functions for starting other versions of R.
237 See `ess-rterm-versions' for strings that determine which functions
238 are created.
240 The result `ess-rterm-versions-created' will store a list of the new
241 Rterm defuns, if any, that were created. The defuns will normally be
242 placed on the menubar upon ESS initialisation."
244 ;; This works by creating a temp buffer where the template function is
245 ;; edited so that R-X.Y is replaced by the version name
246 (let ((template "")
247 (beg)
248 (versions)
249 (version)
250 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
251 (ess-rterm-versions-created)
254 ;; This is the template function used for creating M-x R-X.Y.
255 (setq template "(defun R-X.Y (&optional start-args)
256 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
257 This function was generated by `ess-rterm-versions-create'.\"
258 (interactive \"P\")
259 (let ((inferior-R-program-name \"Rterm-X.Y\"))
260 (R start-args)))
263 (save-excursion
264 (set-buffer eval-buf)
265 ;; clear the buffer.
266 (delete-region (point-min) (point-max))
268 ;; Find which versions of R we want. Remove the pathname, leaving just
269 ;; the name of the executable.
270 (setq versions ess-rterm-versions)
271 (ess-write-to-dribble-buffer
272 (format "(R): ess-rterm-versions-create making M-x defuns for %s"
273 (mapconcat 'identity versions " ")))
275 ;; Iterate over each string in VERSIONS, creating a new defun each time.
276 (while versions
277 (setq version (car versions)
278 versions (cdr versions)
279 version-root (file-name-nondirectory
280 (substring (file-name-directory
281 (substring
282 (file-name-directory version)
283 0 -1))
284 0 -1)))
285 (setq beg (point))
286 (insert template)
287 (goto-char beg)
288 (while (search-forward "R-X.Y" nil t)
289 (replace-match version-root t t))
290 (goto-char beg)
291 (while (search-forward "Rterm-X.Y" nil t)
292 (replace-match version t t))
293 (goto-char (point-max))
294 (setq ess-rterm-versions-created
295 (cons version-root ess-rterm-versions-created))
297 ;; buffer has now been created with defuns, so eval them!
298 (eval-buffer)
299 (kill-buffer eval-buf)
301 ess-rterm-versions-created))
303 ;;;### autoload
304 (defun Rnw-mode ()
305 "Major mode for editing Sweave(R) source.
306 See `noweb-mode' and `R-mode' for more help."
307 (interactive)
308 (require 'ess-noweb);; << probably someplace else
309 (noweb-mode 1); turn it on
310 (noweb-set-doc-mode 'latex-mode)
311 (noweb-set-code-mode 'R-mode))
314 (autoload 'ess-transcript-mode "ess-trns"
315 "Major mode for editing S transcript files." t)
317 (defun R-transcript-mode ()
318 "Does the right thing."
319 (interactive)
320 (ess-transcript-mode R-customize-alist))
322 (fset 'r-transcript-mode 'R-transcript-mode)
324 (defun R-fix-T-F (&optional from quietly)
325 "Fix T/F into TRUE and FALSE *cautiously*, i.e. not in comments and strings;
326 starting from the current position (point)."
327 (interactive "d\nP"); point and prefix (C-u)
328 (save-excursion
329 (goto-char from)
330 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
331 'fixcase nil (not quietly))
332 (goto-char from)
333 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
334 'fixcase nil (not quietly))))
336 \f ; provides
338 (provide 'essd-r)
340 \f ; Local variables section
342 ;;; This file is automatically placed in Outline minor mode.
343 ;;; The file is structured as follows:
344 ;;; Chapters: ^L ;
345 ;;; Sections: ;;*;;
346 ;;; Subsections: ;;;*;;;
347 ;;; Components: defuns, defvars, defconsts
348 ;;; Random code beginning with a ;;;;* comment
350 ;;; Local variables:
351 ;;; mode: emacs-lisp
352 ;;; outline-minor-mode: nil
353 ;;; mode: outline-minor
354 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
355 ;;; End:
357 ;;; ess-site.el ends here