(ess-r-version-date): New defun, replacing
[ess.git] / lisp / essd-r.el
blob666c583c686e8340916ef542df06fc7f087d49c3
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 (require 'essd-r-args); for now
39 ;; modify S Syntax table:
40 (setq R-syntax-table S-syntax-table)
42 ;; In R 2.x, back tick now is a quote character, so lets tell Emacs
43 ;; that it is; the problem below for older R should no longer be a
44 ;; serious issue.
45 ;;R >= 1.8: back tick `string` -- unfortunately no *pair* checking:
46 ;; breaks when things like `..' are used:
47 (modify-syntax-entry ?` "\"" R-syntax-table)
48 (modify-syntax-entry ?_ "_" R-syntax-table) ; foo_bar is symbol in R >=1.9
50 (ess-message "[essd-r:] (autoload ..) & (def** ..)")
52 (autoload 'inferior-ess "ess-inf" "Run an ESS process.")
53 (autoload 'ess-mode "ess-mode" "Edit an ESS process.")
55 ;;; Code:
57 (defvar R-customize-alist
58 (append
59 '((ess-local-customize-alist . 'R-customize-alist)
60 (ess-dialect . "R")
61 (ess-suffix . "R")
62 (ess-dump-filename-template . (ess-replace-regexp-in-string
63 "S$" ess-suffix ; in the one from custom:
64 ess-dump-filename-template-proto))
65 (ess-mode-syntax-table . R-syntax-table)
66 (ess-mode-editing-alist . R-editing-alist)
67 (ess-change-sp-regexp . ess-R-change-sp-regexp)
68 (ess-help-sec-regex . ess-help-R-sec-regex)
69 (ess-help-sec-keys-alist . ess-help-R-sec-keys-alist)
70 (ess-loop-timeout . ess-S-loop-timeout);fixme: dialect spec.
71 (ess-cmd-delay . ess-R-cmd-delay)
72 (ess-function-pattern . ess-R-function-pattern)
73 (ess-object-name-db-file . "ess-r-namedb.el" )
74 (ess-imenu-mode-function . 'ess-imenu-R)
75 (inferior-ess-program . inferior-R-program-name)
76 (inferior-ess-objects-command . inferior-R-objects-command)
77 (inferior-ess-font-lock-keywords . inferior-ess-R-font-lock-keywords)
78 (inferior-ess-search-list-command . "search()\n")
79 (inferior-ess-help-command . "help(\"%s\", htmlhelp=FALSE)\n")
80 (inferior-ess-help-filetype . nil)
81 (inferior-ess-exit-command . "q()")
82 (inferior-ess-exit-prompt . "Save workspace image? [y/n/c]: ")
83 (inferior-ess-primary-prompt . "\\([A-Z][][A-Za-z0-9.]*\\)*> ")
84 (inferior-ess-secondary-prompt . "+ ?")
85 ;;harmful for shell-mode's C-a: -- but "necessary" for ESS-help?
86 (inferior-ess-start-file . nil) ;; "~/.ess-R"
87 (inferior-ess-start-args . "")
88 (ess-STERM . "iESS")
89 (ess-editor . R-editor)
90 (ess-pager . R-pager)
92 S-common-cust-alist)
93 "Variables to customize for R -- set up later than emacs initialization.")
95 (defvar ess-r-versions '("R-1" "R-2" "R-devel" "R-patched")
96 "List of partial strings for versions of R to access within ESS.
97 Each string specifies the start of a filename. If a filename
98 beginning with one of these strings is found on `exec-path', a M-x
99 command for that version of R is made available. For example, if the
100 file \"R-1.8.1\" is found and this variable includes the string
101 \"R-1\", a function called `M-x R-1.8.1' will be available to run that
102 version of R.
103 If duplicate versions of the same program are found (which happens if
104 the same path is listed on `exec-path' more than once), they are
105 ignored by calling `ess-uniq-list'.
106 Set this variable to nil to disable searching for other versions of R.
107 If you set this variable, you need to restart Emacs (and set this variable
108 before ess-site is loaded) for it to take effect.")
110 ;;;### autoload
111 (defun R (&optional start-args)
112 "Call 'R', the 'GNU S' system from the R Foundation.
113 Optional prefix (C-u) allows to set command line arguments, such as
114 --vsize. This should be OS agnostic.
115 If you have certain command line arguments that should always be passed
116 to R, put them in the variable `inferior-R-args'."
117 (interactive "P")
118 (setq ess-customize-alist R-customize-alist)
119 (ess-write-to-dribble-buffer ;; for debugging only
120 (format
121 "\n(R): ess-dialect=%s, buf=%s, start-arg=%s\n current-prefix-arg=%s\n"
122 ess-dialect (current-buffer) start-args current-prefix-arg))
123 (let* ((r-always-arg
124 (if (or ess-microsoft-p (eq system-type 'cygwin))
125 "--ess "
126 "--no-readline "))
127 (r-start-args
128 (concat r-always-arg
129 inferior-R-args " " ; add space just in case
130 (if start-args
131 (read-string
132 (concat "Starting Args [other than `"
133 r-always-arg
134 "'] ? "))
135 nil)))
136 ;;Micro$ ?: default-process-coding-system ;-breaks UTF locales on Unix:
138 (if ess-microsoft-p
139 (setq default-process-coding-system '(undecided-dos . undecided-dos)))
140 (inferior-ess r-start-args) ;; (R)
141 (ess-write-to-dribble-buffer
142 (format "(R): inferior-ess-language-start=%s\n"
143 inferior-ess-language-start))
144 ;; currently rely on baseenv() which is in R only since version 2.2:
145 (ess-eval-linewise
146 "if(!exists(\"baseenv\", mode=\"function\")) baseenv <- function() NULL"
147 nil nil nil 'wait-prompt);; solving "lines running together"
148 (if inferior-ess-language-start
149 (ess-eval-linewise inferior-ess-language-start
150 nil nil nil 'wait-prompt))))
152 ;;;### autoload
153 (defun R-mode (&optional proc-name)
154 "Major mode for editing R source. See `ess-mode' for more help."
155 (interactive)
156 (setq ess-customize-alist R-customize-alist)
157 ;;(setq imenu-generic-expression R-imenu-generic-expression)
158 (ess-mode R-customize-alist proc-name)
159 (if (fboundp 'ess-add-toolbar) (ess-add-toolbar))
160 ;; ECB needs seminatic stuff.
161 ;; (if (featurep 'semantic)
162 ;; (setq semantic-toplevel-bovine-table r-toplevel-bovine-table))
163 (if ess-imenu-use-S
164 (progn (require 'ess-menu)
165 (ess-imenu-R)))
166 ;; MM: ^^^^^^^^^^^ should really use ess-imenu-mode-function from the
167 ;; alist above!
171 (fset 'r-mode 'R-mode)
173 (defun ess-r-versions-create ()
174 "Generate the `M-x R-X.Y' functions for starting other versions of R.
175 See `ess-r-versions' for strings that determine which functions are created.
177 The local variable `ess-r-versions-created' is used to return list of
178 the new R defuns, if any, that were created. The defuns will normally
179 be placed on the menubar upon ESS initialisation."
182 (if (not ess-r-versions)
183 nil ;nothing to return
184 ;; else, if ess-r-versions is non-nil, let's try to find those R versions.
185 ;; This works by creating a temp buffer where the template function is
186 ;; edited so that X.Y is replaced by the version name
187 (let ((template "")
188 (beg)
189 (versions)
190 (version)
191 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
192 (ess-r-versions-created)
194 ;; This is the template function used for creating M-x R-X.Y.
195 (setq template "(defun R-X.Y (&optional start-args)
196 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
197 This function was generated by `ess-r-versions-create'.\"
198 (interactive \"P\")
199 (let ((inferior-R-program-name \"R-X.Y\"))
200 (R start-args)))
203 (save-excursion
204 (set-buffer eval-buf)
205 ;; clear the buffer.
206 (delete-region (point-min) (point-max))
208 ;; Find which versions of R we want. Remove the pathname, leaving just
209 ;; the name of the executable.
210 (setq versions
211 (ess-uniq-list
212 (mapcar 'file-name-nondirectory
213 (apply 'nconc
214 (mapcar 'ess-find-exec-completions
215 ess-r-versions)))))
216 (ess-write-to-dribble-buffer
217 (format "(R): ess-r-versions-create making M-x defuns for %s"
218 (mapconcat 'identity versions " ")))
219 (setq ess-r-versions-created versions) ;keep copy for returning at end.
220 ;; Iterate over each string in VERSIONS, creating a new defun each time.
221 (while versions
222 (setq version (car versions)
223 versions (cdr versions))
224 (setq beg (point))
225 (insert template)
226 (goto-char beg)
227 (while (search-forward "R-X.Y" nil t)
228 (replace-match version t t))
229 (goto-char (point-max))
231 ;; buffer has now been created with defuns, so eval them!
232 (eval-buffer)
233 (kill-buffer eval-buf)
235 ess-r-versions-created)))
240 ;; (ess-r-version-date "R-1.9")
241 (defun ess-r-version-date (rver)
242 "Return the date of the version of R named RVER.
243 The date is returned as a date string. If the version of R could
244 not be found from the output of the RVER program, \"-1\" is
245 returned."
246 (let (ver-string
247 (date "-1"))
248 (setq ver-string (shell-command-to-string
249 (concat rver " --version")))
250 (when (string-match "R version [^\n\(]+ (\\([0-9-]+\\))" ver-string)
251 (setq date (match-string 1 ver-string)))
252 (cons date rver)))
254 (defun ess-newest-r (rvers)
255 "Check all the versions of RVERS to see which is the newest.
256 Create an alias for that version of R, so that M-x R-newest will run it."
257 (let ( rnewest
258 (rtimes (mapcar 'ess-r-version-date rvers)))
259 (setq rnewest (ess-find-newest-date rtimes))
260 (fset 'R-newest (intern rnewest))
261 ;; SJE: 2007-07-13 -- following line is a temp var to check that
262 ;; the newest version of R is found correctly.
263 (setq temp-ess-newest rtimes)
264 (message (concat "newest is " rnewest))))
267 ;; Test case for following defun:
268 ;; (setq a '( ("2003-10-04" . "R-1.7")
269 ;; ("2006-11-19" . "R-2.2")
270 ;; ("2007-07-01" . "R-dev")
271 ;; ("-1" . "R-broken")
272 ;; ("2005-12-30" . "R-2.0")))
273 ;; (ess-find-newest-date a)
274 (defun ess-find-newest-date (rvers)
275 "Find the newest version of R given in the a-list RVERS.
276 Each element of RVERS is a dotted pair (date . R-version), where
277 date is given as e.g.\"2007-11-30\" so that we can compare dates
278 as strings. If a date is listed as \"-1\", that version of R
279 could not be found.
281 If the value returned is nil, no valid newest version of R could be found."
282 (let (new-r
283 (new-time "0"))
284 (while rvers
285 (setq this-r (car rvers)
286 rvers (cdr rvers))
287 (when (string< new-time (car this-r))
288 (setq new-time (car this-r)
289 new-r (cdr this-r))))
290 new-r))
293 (defun ess-find-rterm (&optional ess-R-root-dir)
294 "Find the full path of all occurences of Rterm.exe under the ESS-R-ROOT-DIR.
295 If ESS-R-ROOT-DIR is nil, construct it by looking for an occurence of Rterm.exe
296 in the exec-path. If there are no occurences of Rterm.exe in the exec-path,
297 then use \"c:/progra~1/R/\" which is the default location for the R distribution."
298 (let* ((Rpath)
299 (rwxxyy)
300 (rw)
301 (Rterm nil))
302 (if (not ess-R-root-dir)
303 (progn
304 (setq Rpath (executable-find "Rterm"))
305 (setq ess-R-root-dir
306 (if Rpath
307 (expand-file-name
308 (concat
309 (file-name-directory Rpath)
310 "../../"))
311 "c:/progra~1/R/"))))
312 (setq rwxxyy (append (file-name-all-completions "rw" ess-R-root-dir)
313 (file-name-all-completions "R-1" ess-R-root-dir)
314 (file-name-all-completions "R-2" ess-R-root-dir)
315 (file-name-all-completions "R-devel" ess-R-root-dir)
316 (file-name-all-completions "R-patched" ess-R-root-dir)
317 )) ;; this needs to rewritten to use (append '("rw") ess-r-versions)
318 (while rwxxyy
319 (setq rw (car rwxxyy))
320 (setq rwxxyy (cdr rwxxyy))
321 (setq Rterm (cons (ess-replace-regexp-in-string "[\\]" "/"
322 (concat ess-R-root-dir rw "bin/Rterm.exe"))
323 Rterm)))
324 Rterm))
326 (defun ess-rterm-versions-create ()
327 "Generate the `M-x rwxxyy' functions for starting other versions of R.
328 See `ess-rterm-versions' for strings that determine which functions
329 are created.
331 The result `ess-rterm-versions-created' will store a list of the new
332 Rterm defuns, if any, that were created. The defuns will normally be
333 placed on the menubar upon ESS initialisation."
335 ;; This works by creating a temp buffer where the template function is
336 ;; edited so that R-X.Y is replaced by the version name
337 (let ((template "")
338 (beg)
339 (versions)
340 (version)
341 (version-root)
342 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
343 (ess-rterm-versions-created)
346 ;; This is the template function used for creating M-x R-X.Y.
347 (setq template "(defun R-X.Y (&optional start-args)
348 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
349 This function was generated by `ess-rterm-versions-create'.\"
350 (interactive \"P\")
351 (let ((inferior-R-program-name \"Rterm-X.Y\"))
352 (R start-args)))
355 (save-excursion
356 (set-buffer eval-buf)
357 ;; clear the buffer.
358 (delete-region (point-min) (point-max))
360 ;; Find which versions of R we want. Remove the pathname, leaving just
361 ;; the name of the executable.
362 (setq versions ess-rterm-versions)
363 (ess-write-to-dribble-buffer
364 (format "(R): ess-rterm-versions-create making M-x defuns for %s"
365 (mapconcat 'identity versions " ")))
367 ;; Iterate over each string in VERSIONS, creating a new defun each time.
368 (while versions
369 (setq version (car versions)
370 versions (cdr versions)
371 version-root (file-name-nondirectory
372 (substring (file-name-directory
373 (substring
374 (file-name-directory version)
375 0 -1))
376 0 -1)))
377 (setq beg (point))
378 (insert template)
379 (goto-char beg)
380 (while (search-forward "R-X.Y" nil t)
381 (replace-match version-root t t))
382 (goto-char beg)
383 (while (search-forward "Rterm-X.Y" nil t)
384 (replace-match version t t))
385 (goto-char (point-max))
386 (setq ess-rterm-versions-created
387 (cons version-root ess-rterm-versions-created))
389 ;; buffer has now been created with defuns, so eval them!
390 (eval-buffer)
391 (kill-buffer eval-buf)
393 ess-rterm-versions-created))
395 ;;;### autoload
396 (defun Rnw-mode ()
397 "Major mode for editing Sweave(R) source.
398 See `noweb-mode' and `R-mode' for more help."
399 (interactive)
400 (require 'ess-noweb);; << probably someplace else
401 (noweb-mode 1); turn it on
402 (noweb-set-doc-mode 'latex-mode)
403 (noweb-set-code-mode 'R-mode)
404 (run-hooks 'Rnw-mode-hook))
406 (fset 'Snw-mode 'Rnw-mode); just a synonym (for now or ever)
409 (autoload 'ess-transcript-mode "ess-trns"
410 "Major mode for editing S transcript files." t)
412 (defun R-transcript-mode ()
413 "Does the right thing."
414 (interactive)
415 (ess-transcript-mode R-customize-alist))
417 (fset 'r-transcript-mode 'R-transcript-mode)
419 (defun R-fix-T-F (&optional from quietly)
420 "Fix T/F into TRUE and FALSE *cautiously*, i.e. not in comments and strings;
421 starting from the current position (point)."
422 (interactive "d\nP"); point and prefix (C-u)
423 (save-excursion
424 (goto-char from)
425 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
426 'fixcase nil (not quietly))
427 (goto-char from)
428 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
429 'fixcase nil (not quietly))))
431 ;; From: Sebastian Luque <spluque@gmail.com>
432 ;; To: ess-help@stat.math.ethz.ch
433 ;; Date: Mon, 01 May 2006 19:17:49 -0500
435 ;; Without knowing how to tell R to use w3m from within Emacs, and after
436 ;; switching to Konqueror's window for the millionth time, I wrote the
437 ;; following function:
439 ;; This emulates some of the functionality of RSiteSearch() and tests ok in
440 ;; my system GNU Emacs 22.0.50.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll
441 ;; bars) of 2006-04-27 on pacem, modified by Debian. This has the benefit of
442 ;; displaying results with whatever you've told browse-url to use; in my
443 ;; case, w3m with the emacs-w3m package.
445 ;; My elisp skills are rather poor, so comments and suggestions for
446 ;; improvement are welcome.
447 ;; --
448 ;; Seb
451 ;; MM _FIXME_: This only works correctly for Emacs 22.0.50 (alpha)
452 ;; for 21.x it has problems in the (completing-read-multiple .)
453 ;; at the end
454 (defun R-site-search (string)
455 "Search the R archives for STRING, using default criteria. If
456 called with a prefix, options are available for
457 1) matches per page,
458 2) sections of the archives to search (separated by value of `crm-default-separator'),
459 3) for displaying results in long or short formats, and
460 4) for sorting by any given field.
461 Completion is available for supplying options."
462 (interactive "sSearch string: ")
463 (let ((site "http://search.r-project.org/cgi-bin/namazu.cgi?query=")
464 (okstring (replace-regexp-in-string " +" "+" string)))
465 (if current-prefix-arg
466 (let ((mpp (concat
467 "&max="
468 (completing-read
469 "Matches per page: "
470 '(("20" 1) ("30" 2) ("40" 3) ("50" 4) ("100" 5)))))
471 (format (concat
472 "&result="
473 (completing-read
474 "Format: " '("normal" "short")
475 nil t "normal" nil "normal")))
476 (sortby (concat
477 "&sort="
478 (completing-read
479 "Sort by: "
480 '(("score" 1) ("date:late" 2) ("date:early" 3)
481 ("field:subject:ascending" 4)
482 ("field:subject:decending" 5)
483 ("field:from:ascending" 6) ("field:from:decending" 7)
484 ("field:size:ascending" 8) ("field:size:decending" 9))
485 nil t "score" nil "score")))
486 (restrict (concat
487 "&idxname="
488 (mapconcat
489 'identity
490 (completing-read-multiple
491 "Limit search to: "
492 '(("Rhelp02a" 1) ("functions" 2) ("docs" 3)
493 ("Rhelp01" 4))
494 nil t "Rhelp02a,functions,docs" nil
495 "Rhelp02a,functions,docs") "&idxname="))))
496 (browse-url (concat site okstring mpp format sortby restrict)))
497 ;; else: without prefix use defaults:
498 (browse-url (concat site okstring "&max=20&result=normal&sort=score"
499 "&idxname=Rhelp02a&idxname=functions&idxname=docs")))))
502 \f ; provides
504 (provide 'essd-r)
506 \f ; Local variables section
508 ;;; This file is automatically placed in Outline minor mode.
509 ;;; The file is structured as follows:
510 ;;; Chapters: ^L ;
511 ;;; Sections: ;;*;;
512 ;;; Subsections: ;;;*;;;
513 ;;; Components: defuns, defvars, defconsts
514 ;;; Random code beginning with a ;;;;* comment
516 ;;; Local variables:
517 ;;; mode: emacs-lisp
518 ;;; outline-minor-mode: nil
519 ;;; mode: outline-minor
520 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
521 ;;; End:
523 ;;; essd-r.el ends here