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)
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.
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)")
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.")
51 (defvar R-customize-alist
53 '((ess-local-customize-alist .
'R-customize-alist
)
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 .
"")
84 (ess-editor . R-editor
)
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
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.")
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'."
112 (setq ess-customize-alist R-customize-alist
)
113 (ess-write-to-dribble-buffer ;; for debugging only
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
))
126 (concat "Starting Args [other than `"
130 ;;Micro$ ?: default-process-coding-system ;-breaks UTF locales on Unix:
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
))))
142 (defun R-mode (&optional proc-name
)
143 "Major mode for editing R source. See `ess-mode' for more help."
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))
153 (progn (require 'ess-menu
)
155 ;; MM: ^^^^^^^^^^^ should really use ess-imenu-mode-function from the
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
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'.\"
185 (let ((inferior-R-program-name \"R-X.Y\"))
190 (set-buffer eval-buf
)
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.
198 (mapcar 'file-name-nondirectory
200 (mapcar 'ess-find-exec-completions
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.
208 (setq version
(car versions
)
209 versions
(cdr versions
))
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!
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
232 (if (not ess-R-root-dir
)
234 (setq Rpath
(executable-find "Rterm"))
239 (file-name-directory Rpath
)
242 (setq rwxxyy
(file-name-all-completions "rw" ess-R-root-dir
))
244 (setq rw
(car rwxxyy
))
245 (setq rwxxyy
(cdr rwxxyy
))
246 (setq Rterm
(cons (concat ess-R-root-dir rw
"bin/Rterm.exe") 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
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
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'.\"
273 (let ((inferior-R-program-name \"Rterm-X.Y\"))
278 (set-buffer eval-buf
)
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.
291 (setq version
(car versions
)
292 versions
(cdr versions
)
293 version-root
(file-name-nondirectory
294 (substring (file-name-directory
296 (file-name-directory version
)
302 (while (search-forward "R-X.Y" nil t
)
303 (replace-match version-root t t
))
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!
313 (kill-buffer eval-buf
)
315 ess-rterm-versions-created
))
319 "Major mode for editing Sweave(R) source.
320 See `noweb-mode' and `R-mode' for more help."
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."
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)
344 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
345 'fixcase nil
(not quietly
))
347 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
348 'fixcase nil
(not quietly
))))
354 \f ; Local variables section
356 ;;; This file is automatically placed in Outline minor mode.
357 ;;; The file is structured as follows:
360 ;;; Subsections: ;;;*;;;
361 ;;; Components: defuns, defvars, defconsts
362 ;;; Random code beginning with a ;;;;* comment
366 ;;; outline-minor-mode: nil
367 ;;; mode: outline-minor
368 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
371 ;;; essd-r.el ends here