(ess-r-versions-create): Apply unique to list of list of R
[ess.git] / lisp / essd-r.el
bloba08b376668ff1e789caf9e6527c39ea1e7239ff5
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 (defvar ess-r-versions '("R-1" "R-2" "R-devel" "R-patched")
91 "List of partial strings for versions of R to access within ESS.
92 Each string specifies the start of a filename. If a filename
93 beginning with one of these strings is found on `exec-path', a M-x
94 command for that version of R is made available. For example, if the
95 file \"R-1.8.1\" is found and this variable includes the string
96 \"R-1\", a function called `M-x R-1.8.1' will be available to run that
97 version of R.
98 If duplicate versions of the same program are found (which happens if
99 the same path is listed on `exec-path' more than once), they are
100 ignored by calling `ess-uniq-list'.
101 If you set this variable, you need to restart Emacs (and set this variable
102 before ess-site is loaded) for it to take effect.")
104 ;;;### autoload
105 (defun R (&optional start-args)
106 "Call 'R', the 'GNU S' system from the R Foundation.
107 Optional prefix (C-u) allows to set command line arguments, such as
108 --vsize. This should be OS agnostic.
109 If you have certain command line arguments that should always be passed
110 to R, put them in the variable `inferior-R-args'."
111 (interactive "P")
112 (setq ess-customize-alist R-customize-alist)
113 (ess-write-to-dribble-buffer ;; for debugging only
114 (format
115 "\n(R): ess-dialect=%s, buf=%s, start-arg=%s\n current-prefix-arg=%s\n"
116 ess-dialect (current-buffer) start-args current-prefix-arg))
117 (let* ((r-always-arg
118 (if ess-microsoft-p
119 "--ess "
120 "--no-readline "))
121 (r-start-args
122 (concat r-always-arg
123 inferior-R-args
124 (if start-args
125 (read-string
126 (concat "Starting Args [other than `"
127 r-always-arg
128 "'] ? "))
129 nil)))
130 ;;Micro$ ?: default-process-coding-system ;-breaks UTF locales on Unix:
132 (if ess-microsoft-p
133 (setq default-process-coding-system '(undecided-dos . undecided-dos)))
134 (inferior-ess r-start-args) ;; (R)
135 (ess-write-to-dribble-buffer
136 (format "(R): inferior-ess-language-start=%s\n"
137 inferior-ess-language-start))
138 (if inferior-ess-language-start
139 (ess-eval-linewise inferior-ess-language-start))))
141 ;;;### autoload
142 (defun R-mode (&optional proc-name)
143 "Major mode for editing R source. See `ess-mode' for more help."
144 (interactive)
145 (setq ess-customize-alist R-customize-alist)
146 ;;(setq imenu-generic-expression R-imenu-generic-expression)
147 (ess-mode R-customize-alist proc-name)
148 (if (fboundp 'ess-add-toolbar) (ess-add-toolbar))
149 ;; ECB needs seminatic stuff.
150 ;; (if (featurep 'semantic)
151 ;; (setq semantic-toplevel-bovine-table r-toplevel-bovine-table))
152 (if ess-imenu-use-S
153 (progn (require 'ess-menu)
154 (ess-imenu-R)))
155 ;; MM: ^^^^^^^^^^^ should really use ess-imenu-mode-function from the
156 ;; alist above!
160 (fset 'r-mode 'R-mode)
162 (defun ess-r-versions-create ()
163 "Generate the `M-x R-X.Y' functions for starting other versions of R.
164 See `ess-r-versions' for strings that determine which functions are created.
166 The local variable `ess-r-versions-created' is used to return list of
167 the new R defuns, if any, that were created. The defuns will normally
168 be placed on the menubar upon ESS initialisation."
170 ;; This works by creating a temp buffer where the template function is
171 ;; edited so that X.Y is replaced by the version name
172 (let ((template "")
173 (beg)
174 (versions)
175 (version)
176 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
177 (ess-r-versions-created)
180 ;; This is the template function used for creating M-x R-X.Y.
181 (setq template "(defun R-X.Y (&optional start-args)
182 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
183 This function was generated by `ess-r-versions-create'.\"
184 (interactive \"P\")
185 (let ((inferior-R-program-name \"R-X.Y\"))
186 (R start-args)))
189 (save-excursion
190 (set-buffer eval-buf)
191 ;; clear the buffer.
192 (delete-region (point-min) (point-max))
194 ;; Find which versions of R we want. Remove the pathname, leaving just
195 ;; the name of the executable.
196 (setq versions
197 (ess-uniq-list
198 (mapcar 'file-name-nondirectory
199 (apply 'nconc
200 (mapcar 'ess-find-exec-completions
201 ess-r-versions)))))
202 (ess-write-to-dribble-buffer
203 (format "(R): ess-r-versions-create making M-x defuns for %s"
204 (mapconcat 'identity versions " ")))
205 (setq ess-r-versions-created versions) ;keep copy for returning at end.
206 ;; Iterate over each string in VERSIONS, creating a new defun each time.
207 (while versions
208 (setq version (car versions)
209 versions (cdr versions))
210 (setq beg (point))
211 (insert template)
212 (goto-char beg)
213 (while (search-forward "R-X.Y" nil t)
214 (replace-match version t t))
215 (goto-char (point-max))
217 ;; buffer has now been created with defuns, so eval them!
218 (eval-buffer)
219 (kill-buffer eval-buf)
221 ess-r-versions-created))
224 (defun ess-find-rterm (&optional ess-R-root-dir)
225 "Find the full path of all occurences of Rterm.exe under the ESS-R-ROOT-DIR.
226 If ESS-R-ROOT-DIR is nil, construct it by looking for an occurence of Rterm.exe
227 in the exec-path."
228 (let* ((Rpath)
229 (rwxxyy)
230 (rw)
231 (Rterm nil))
232 (if (not ess-R-root-dir)
233 (progn
234 (setq Rpath (executable-find "Rterm"))
235 (setq ess-R-root-dir
236 (if Rpath
237 (expand-file-name
238 (concat
239 (file-name-directory Rpath)
240 "../../"))
241 ""))))
242 (setq rwxxyy (file-name-all-completions "rw" ess-R-root-dir))
243 (while rwxxyy
244 (setq rw (car rwxxyy))
245 (setq rwxxyy (cdr rwxxyy))
246 (setq Rterm (cons (concat ess-R-root-dir rw "bin/Rterm.exe") Rterm)))
247 Rterm))
249 (defun ess-rterm-versions-create ()
250 "Generate the `M-x rwxxyy' functions for starting other versions of R.
251 See `ess-rterm-versions' for strings that determine which functions
252 are created.
254 The result `ess-rterm-versions-created' will store a list of the new
255 Rterm defuns, if any, that were created. The defuns will normally be
256 placed on the menubar upon ESS initialisation."
258 ;; This works by creating a temp buffer where the template function is
259 ;; edited so that R-X.Y is replaced by the version name
260 (let ((template "")
261 (beg)
262 (versions)
263 (version)
264 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
265 (ess-rterm-versions-created)
268 ;; This is the template function used for creating M-x R-X.Y.
269 (setq template "(defun R-X.Y (&optional start-args)
270 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
271 This function was generated by `ess-rterm-versions-create'.\"
272 (interactive \"P\")
273 (let ((inferior-R-program-name \"Rterm-X.Y\"))
274 (R start-args)))
277 (save-excursion
278 (set-buffer eval-buf)
279 ;; clear the buffer.
280 (delete-region (point-min) (point-max))
282 ;; Find which versions of R we want. Remove the pathname, leaving just
283 ;; the name of the executable.
284 (setq versions ess-rterm-versions)
285 (ess-write-to-dribble-buffer
286 (format "(R): ess-rterm-versions-create making M-x defuns for %s"
287 (mapconcat 'identity versions " ")))
289 ;; Iterate over each string in VERSIONS, creating a new defun each time.
290 (while versions
291 (setq version (car versions)
292 versions (cdr versions)
293 version-root (file-name-nondirectory
294 (substring (file-name-directory
295 (substring
296 (file-name-directory version)
297 0 -1))
298 0 -1)))
299 (setq beg (point))
300 (insert template)
301 (goto-char beg)
302 (while (search-forward "R-X.Y" nil t)
303 (replace-match version-root t t))
304 (goto-char beg)
305 (while (search-forward "Rterm-X.Y" nil t)
306 (replace-match version t t))
307 (goto-char (point-max))
308 (setq ess-rterm-versions-created
309 (cons version-root ess-rterm-versions-created))
311 ;; buffer has now been created with defuns, so eval them!
312 (eval-buffer)
313 (kill-buffer eval-buf)
315 ess-rterm-versions-created))
317 ;;;### autoload
318 (defun Rnw-mode ()
319 "Major mode for editing Sweave(R) source.
320 See `noweb-mode' and `R-mode' for more help."
321 (interactive)
322 (require 'ess-noweb);; << probably someplace else
323 (noweb-mode 1); turn it on
324 (noweb-set-doc-mode 'latex-mode)
325 (noweb-set-code-mode 'R-mode))
328 (autoload 'ess-transcript-mode "ess-trns"
329 "Major mode for editing S transcript files." t)
331 (defun R-transcript-mode ()
332 "Does the right thing."
333 (interactive)
334 (ess-transcript-mode R-customize-alist))
336 (fset 'r-transcript-mode 'R-transcript-mode)
338 (defun R-fix-T-F (&optional from quietly)
339 "Fix T/F into TRUE and FALSE *cautiously*, i.e. not in comments and strings;
340 starting from the current position (point)."
341 (interactive "d\nP"); point and prefix (C-u)
342 (save-excursion
343 (goto-char from)
344 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
345 'fixcase nil (not quietly))
346 (goto-char from)
347 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
348 'fixcase nil (not quietly))))
350 \f ; provides
352 (provide 'essd-r)
354 \f ; Local variables section
356 ;;; This file is automatically placed in Outline minor mode.
357 ;;; The file is structured as follows:
358 ;;; Chapters: ^L ;
359 ;;; Sections: ;;*;;
360 ;;; Subsections: ;;;*;;;
361 ;;; Components: defuns, defvars, defconsts
362 ;;; Random code beginning with a ;;;;* comment
364 ;;; Local variables:
365 ;;; mode: emacs-lisp
366 ;;; outline-minor-mode: nil
367 ;;; mode: outline-minor
368 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
369 ;;; End:
371 ;;; essd-r.el ends here