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")
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'."
98 (setq ess-customize-alist R-customize-alist
)
99 (ess-write-to-dribble-buffer ;; for debugging only
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
))
112 (concat "Starting Args [other than `"
116 ;;Micro$ ?: default-process-coding-system ;-breaks UTF locales on Unix:
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
))))
128 (defun R-mode (&optional proc-name
)
129 "Major mode for editing R source. See `ess-mode' for more help."
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))
139 (progn (require 'ess-menu
)
141 ;; MM: ^^^^^^^^^^^ should really use ess-imenu-mode-function from the
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
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'.\"
171 (let ((inferior-R-program-name \"R-X.Y\"))
176 (set-buffer eval-buf
)
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.
183 (mapcar 'file-name-nondirectory
186 (mapcar 'ess-find-exec-completions
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.
194 (setq version
(car versions
)
195 versions
(cdr versions
))
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!
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
218 (if (not ess-R-root-dir
)
220 (setq Rpath
(executable-find "Rterm"))
225 (file-name-directory Rpath
)
228 (setq rwxxyy
(file-name-all-completions "rw" ess-R-root-dir
))
230 (setq rw
(car rwxxyy
))
231 (setq rwxxyy
(cdr rwxxyy
))
232 (setq Rterm
(cons (concat ess-R-root-dir rw
"bin/Rterm.exe") 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
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
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'.\"
259 (let ((inferior-R-program-name \"Rterm-X.Y\"))
264 (set-buffer eval-buf
)
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.
277 (setq version
(car versions
)
278 versions
(cdr versions
)
279 version-root
(file-name-nondirectory
280 (substring (file-name-directory
282 (file-name-directory version
)
288 (while (search-forward "R-X.Y" nil t
)
289 (replace-match version-root t t
))
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!
299 (kill-buffer eval-buf
)
301 ess-rterm-versions-created
))
305 "Major mode for editing Sweave(R) source.
306 See `noweb-mode' and `R-mode' for more help."
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."
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)
330 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
331 'fixcase nil
(not quietly
))
333 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
334 'fixcase nil
(not quietly
))))
340 \f ; Local variables section
342 ;;; This file is automatically placed in Outline minor mode.
343 ;;; The file is structured as follows:
346 ;;; Subsections: ;;;*;;;
347 ;;; Components: defuns, defvars, defconsts
348 ;;; Random code beginning with a ;;;;* comment
352 ;;; outline-minor-mode: nil
353 ;;; mode: outline-minor
354 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
357 ;;; ess-site.el ends here