(ess-find-newest-R): Add message whilst searching.
[ess.git] / lisp / essd-r.el
blob50c981f8e51f67e9357c85d22e45aa1be623ca48
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 ;; FIXME: below we have a "copy-paste" version of this,
174 ;; ess-rterm-versions-create {which should really be merged into here}
175 (defun ess-r-versions-create ()
176 "Generate the `M-x R-X.Y' functions for starting other versions of R.
177 See `ess-r-versions' for strings that determine which functions are created.
179 The local variable `ess-r-versions-created' is used to return list of
180 the new R defuns, if any, that were created. The defuns will normally
181 be placed on the menubar upon ESS initialisation."
183 (if (not ess-r-versions)
184 nil ;nothing to return
185 ;; else, if ess-r-versions is non-nil, let's try to find those R versions.
186 ;; This works by creating a temp buffer where the template function is
187 ;; edited so that X.Y is replaced by the version name
188 (let ((template "")
189 (beg)
190 (versions)
191 (version)
192 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
193 (ess-r-versions-created)
195 ;; This is the template function used for creating M-x R-X.Y.
196 (setq template "(defun R-X.Y (&optional start-args)
197 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
198 This function was generated by `ess-r-versions-create'.\"
199 (interactive \"P\")
200 (let ((inferior-R-program-name \"R-X.Y\"))
201 (R start-args)))
204 (save-excursion
205 (set-buffer eval-buf)
206 ;; clear the buffer.
207 (delete-region (point-min) (point-max))
209 ;; Find which versions of R we want. Remove the pathname, leaving just
210 ;; the name of the executable.
211 (setq versions
212 (ess-uniq-list
213 (mapcar 'file-name-nondirectory
214 (apply 'nconc
215 (mapcar 'ess-find-exec-completions
216 ess-r-versions)))))
217 (ess-write-to-dribble-buffer
218 (format "(R): ess-r-versions-create making M-x defuns for %s"
219 (mapconcat 'identity versions " ")))
220 (setq ess-r-versions-created versions) ;keep copy for returning at end.
221 ;; Iterate over each string in VERSIONS, creating a new defun each time.
222 (while versions
223 (setq version (car versions)
224 versions (cdr versions))
225 (setq beg (point))
226 (insert template)
227 (goto-char beg)
228 (while (search-forward "R-X.Y" nil t)
229 (replace-match version t t))
230 (goto-char (point-max))
232 ;; buffer has now been created with defuns, so eval them!
233 (eval-buffer)
234 (kill-buffer eval-buf)
236 ess-r-versions-created)))
238 (defvar ess-newest-R nil
239 "Stores the newest version of R that has been found. Used as a cache,
240 within ess-find-newest-R. Do not use this value directly, but
241 instead call the defun `ess-find-newest-R'.")
243 (defun ess-find-newest-R ()
244 "Find the newest version of R on the system. Once the value is found,
245 cache it in the variable `ess-newest-R' for future use as finding the
246 newest version of R can be potentially time-consuming."
247 (or ess-newest-R
248 (progn (message "Finding all versions of R on your system...")
249 ;;(sleep-for 3)
250 nil)
251 (setq ess-newest-R (ess-newest-r
252 (if ess-microsoft-p
253 ess-rterm-versions
254 (add-to-list 'ess-r-versions-created
255 inferior-R-program-name))))))
256 (defun ess-check-R-name ()
257 "Check `inferior-R-program-name' points to an executable version of R.
258 If not, try to find the newest version of R elsewhere on the system, and
259 update `inferior-R-program-name' accordingly."
260 (unless (executable-find inferior-R-program-name)
261 ;; need to check if we can find another name.
262 (let ((newest (ess-find-newest-R)))
263 (if newest
264 (setq inferior-R-program-name newest)
265 (message "Sorry, no version of R could be found on your system.")))))
267 (defun R-newest (&optional start-args)
268 "Find the newest version of R available, and run it.
269 Subsequent calls to R-newest will run that version, rather than searching
270 again for the newest version. Providing an optional prefix arg (C-u) will
271 prompt for command line arguments."
272 (interactive "P")
273 (let ((rnewest (ess-find-newest-R)))
274 (if (not rnewest)
275 (error "No version of R could be found.")
276 ;; Else: we have a working version of R.
277 ;; Have to be careful to avoid recursion...
278 (message (concat "Newest version of R is " rnewest))
279 (fset 'R-newest
280 (intern
281 (if ess-microsoft-p
282 (file-name-nondirectory
283 (substring (file-name-directory
284 (substring (file-name-directory rnewest) 0 -1))
285 0 -1))
286 rnewest)))
287 ;;(fset 'R-newest (intern rnewest))
288 (R-newest start-args))))
290 ;; (ess-r-version-date "R-2.5.1") (ess-r-version-date "R-patched")
291 ;; (ess-r-version-date "R-1.2.1") (ess-r-version-date "R-1.8.1")
292 ;; Note that for R-devel, ver-string is something like
293 ;; R version 2.6.0 Under development (unstable) (2007-07-14 r42234)
294 ;; Antique examples are 'R 1.0.1 (April 14, 2000)' or 'R 1.5.1 (2002-06-17).'
295 (defun ess-r-version-date (rver)
296 "Return the date of the version of R named RVER.
297 The date is returned as a date string. If the version of R could
298 not be found from the output of the RVER program, \"-1\" is
299 returned."
300 (let (ver-string
301 (date "-1"))
302 (setq ver-string (shell-command-to-string
303 (concat rver " --version")))
304 (when (string-match
305 "R \\(version \\)?[1-9][^\n]+ (\\(2[0-9-]+\\)\\( r[0-9]+\\)?)"
306 ver-string)
307 (setq date (match-string 2 ver-string)))
308 (cons date rver)))
310 (defun ess-newest-r (rvers)
311 "Check all the versions of RVERS to see which is the newest.
312 Return the name of the newest version of R."
313 (let (rnewest
314 (rtimes (mapcar 'ess-r-version-date rvers)))
315 ;; SJE: 2007-07-13 -- following line is a temp var to check that
316 ;; the newest version of R is found correctly.
317 (setq ess-temp-newest rtimes)
318 (setq rnewest (ess-find-newest-date rtimes))
319 rnewest))
321 ;; Test case for following defun:
322 ;; (setq a '( ("2003-10-04" . "R-1.7")
323 ;; ("2006-11-19" . "R-2.2")
324 ;; ("2007-07-01" . "R-dev")
325 ;; ("-1" . "R-broken")
326 ;; ("2005-12-30" . "R-2.0")))
327 ;; (ess-find-newest-date a)
328 (defun ess-find-newest-date (rvers)
329 "Find the newest version of R given in the a-list RVERS.
330 Each element of RVERS is a dotted pair (date . R-version), where
331 date is given as e.g.\"2007-11-30\" so that we can compare dates
332 as strings. If a date is listed as \"-1\", that version of R
333 could not be found.
335 If the value returned is nil, no valid newest version of R could be found."
336 (let (new-r this-r
337 (new-time "0"))
338 (while rvers
339 (setq this-r (car rvers)
340 rvers (cdr rvers))
341 (when (string< new-time (car this-r))
342 (setq new-time (car this-r)
343 new-r (cdr this-r))))
344 new-r))
347 (defun ess-find-rterm (&optional ess-R-root-dir)
348 "Find the full path of all occurences of Rterm.exe under the ESS-R-ROOT-DIR.
349 If ESS-R-ROOT-DIR is nil, construct it by looking for an occurence of Rterm.exe
350 in the exec-path. If there are no occurences of Rterm.exe in the exec-path,
351 then use \"c:/progra~1/R/\" which is the default location for the R distribution."
352 (let* ((Rpath)
353 (rwxxyy)
354 (rw)
355 (Rterm nil))
356 (if (not ess-R-root-dir)
357 (progn
358 (setq Rpath (executable-find "Rterm"))
359 (setq ess-R-root-dir
360 (if Rpath
361 (expand-file-name
362 (concat
363 (file-name-directory Rpath)
364 "../../"))
365 "c:/progra~1/R/"))))
366 (setq rwxxyy (append (file-name-all-completions "rw" ess-R-root-dir)
367 (file-name-all-completions "R-1" ess-R-root-dir)
368 (file-name-all-completions "R-2" ess-R-root-dir)
369 (file-name-all-completions "R-devel" ess-R-root-dir)
370 (file-name-all-completions "R-patched" ess-R-root-dir)
371 )) ;; this needs to rewritten to use (append '("rw") ess-r-versions)
372 (while rwxxyy
373 (setq rw (car rwxxyy))
374 (setq rwxxyy (cdr rwxxyy))
375 (setq Rterm (cons (ess-replace-regexp-in-string "[\\]" "/"
376 (concat ess-R-root-dir rw "bin/Rterm.exe"))
377 Rterm)))
378 Rterm))
380 (defun ess-rterm-versions-create ()
381 "Generate the `M-x rwxxyy' functions for starting other versions of R.
382 See `ess-rterm-versions' for strings that determine which functions
383 are created.
385 The result `ess-rterm-versions-created' will store a list of the new
386 Rterm defuns, if any, that were created. The defuns will normally be
387 placed on the menubar upon ESS initialisation."
389 ;; This works by creating a temp buffer where the template function is
390 ;; edited so that R-X.Y is replaced by the version name
391 (let ((template "")
392 (beg)
393 (versions)
394 (version)
395 (version-root)
396 (eval-buf (get-buffer-create "*ess-temp-r-evals*"))
397 (ess-rterm-versions-created)
400 ;; This is the template function used for creating M-x R-X.Y.
401 (setq template "(defun R-X.Y (&optional start-args)
402 \"Call R-X.Y, i.e., the R version 'R-X.Y' using ESS.
403 This function was generated by `ess-rterm-versions-create'.\"
404 (interactive \"P\")
405 (let ((use-dialog-box) ;; MS dialog box won't return a directory
406 (inferior-R-program-name \"Rterm-X.Y\"))
407 (R start-args)))
410 (save-excursion
411 (set-buffer eval-buf)
412 ;; clear the buffer.
413 (delete-region (point-min) (point-max))
415 ;; Find which versions of R we want. Remove the pathname, leaving just
416 ;; the name of the executable.
417 (setq versions ess-rterm-versions)
418 (ess-write-to-dribble-buffer
419 (format "(R): ess-rterm-versions-create making M-x defuns for %s"
420 (mapconcat 'identity versions " ")))
422 ;; Iterate over each string in VERSIONS, creating a new defun each time.
423 (while versions
424 (setq version (car versions)
425 versions (cdr versions)
426 version-root (file-name-nondirectory
427 (substring (file-name-directory
428 (substring
429 (file-name-directory version)
430 0 -1))
431 0 -1)))
432 (setq beg (point))
433 (insert template)
434 (goto-char beg)
435 (while (search-forward "R-X.Y" nil t)
436 (replace-match version-root t t))
437 (goto-char beg)
438 (while (search-forward "Rterm-X.Y" nil t)
439 (replace-match version t t))
440 (goto-char (point-max))
441 (setq ess-rterm-versions-created
442 (cons version-root ess-rterm-versions-created))
444 ;; buffer has now been created with defuns, so eval them!
445 (eval-buffer)
446 (kill-buffer eval-buf)
448 ess-rterm-versions-created))
450 ;;;### autoload
451 (defun Rnw-mode ()
452 "Major mode for editing Sweave(R) source.
453 See `noweb-mode' and `R-mode' for more help."
454 (interactive)
455 (require 'ess-noweb);; << probably someplace else
456 (noweb-mode 1); turn it on
457 (noweb-set-doc-mode 'latex-mode)
458 (noweb-set-code-mode 'R-mode)
459 (run-hooks 'Rnw-mode-hook))
461 (fset 'Snw-mode 'Rnw-mode); just a synonym (for now or ever)
464 (autoload 'ess-transcript-mode "ess-trns"
465 "Major mode for editing S transcript files." t)
467 (defun R-transcript-mode ()
468 "Does the right thing."
469 (interactive)
470 (ess-transcript-mode R-customize-alist))
472 (fset 'r-transcript-mode 'R-transcript-mode)
474 (defun R-fix-T-F (&optional from quietly)
475 "Fix T/F into TRUE and FALSE *cautiously*, i.e. not in comments and strings;
476 starting from the current position (point)."
477 (interactive "d\nP"); point and prefix (C-u)
478 (save-excursion
479 (goto-char from)
480 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
481 'fixcase nil (not quietly))
482 (goto-char from)
483 (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
484 'fixcase nil (not quietly))))
486 ;; From: Sebastian Luque <spluque@gmail.com>
487 ;; To: ess-help@stat.math.ethz.ch
488 ;; Date: Mon, 01 May 2006 19:17:49 -0500
490 ;; Without knowing how to tell R to use w3m from within Emacs, and after
491 ;; switching to Konqueror's window for the millionth time, I wrote the
492 ;; following function:
494 ;; This emulates some of the functionality of RSiteSearch() and tests ok in
495 ;; my system GNU Emacs 22.0.50.1 (i486-pc-linux-gnu, X toolkit, Xaw3d scroll
496 ;; bars) of 2006-04-27 on pacem, modified by Debian. This has the benefit of
497 ;; displaying results with whatever you've told browse-url to use; in my
498 ;; case, w3m with the emacs-w3m package.
500 ;; My elisp skills are rather poor, so comments and suggestions for
501 ;; improvement are welcome.
502 ;; --
503 ;; Seb
506 ;; MM _FIXME_: This only works correctly for Emacs 22.0.50 (alpha)
507 ;; for 21.x it has problems in the (completing-read-multiple .)
508 ;; at the end
509 (defun R-site-search (string)
510 "Search the R archives for STRING, using default criteria. If
511 called with a prefix, options are available for
512 1) matches per page,
513 2) sections of the archives to search (separated by value of `crm-default-separator'),
514 3) for displaying results in long or short formats, and
515 4) for sorting by any given field.
516 Completion is available for supplying options."
517 (interactive "sSearch string: ")
518 (let ((site "http://search.r-project.org/cgi-bin/namazu.cgi?query=")
519 (okstring (replace-regexp-in-string " +" "+" string)))
520 (if current-prefix-arg
521 (let ((mpp (concat
522 "&max="
523 (completing-read
524 "Matches per page: "
525 '(("20" 1) ("30" 2) ("40" 3) ("50" 4) ("100" 5)))))
526 (format (concat
527 "&result="
528 (completing-read
529 "Format: " '("normal" "short")
530 nil t "normal" nil "normal")))
531 (sortby (concat
532 "&sort="
533 (completing-read
534 "Sort by: "
535 '(("score" 1) ("date:late" 2) ("date:early" 3)
536 ("field:subject:ascending" 4)
537 ("field:subject:decending" 5)
538 ("field:from:ascending" 6) ("field:from:decending" 7)
539 ("field:size:ascending" 8) ("field:size:decending" 9))
540 nil t "score" nil "score")))
541 (restrict (concat
542 "&idxname="
543 (mapconcat
544 'identity
545 (completing-read-multiple
546 "Limit search to: "
547 '(("Rhelp02a" 1) ("functions" 2) ("docs" 3)
548 ("Rhelp01" 4))
549 nil t "Rhelp02a,functions,docs" nil
550 "Rhelp02a,functions,docs") "&idxname="))))
551 (browse-url (concat site okstring mpp format sortby restrict)))
552 ;; else: without prefix use defaults:
553 (browse-url (concat site okstring "&max=20&result=normal&sort=score"
554 "&idxname=Rhelp02a&idxname=functions&idxname=docs")))))
557 \f ; provides
559 (provide 'essd-r)
561 \f ; Local variables section
563 ;;; This file is automatically placed in Outline minor mode.
564 ;;; The file is structured as follows:
565 ;;; Chapters: ^L ;
566 ;;; Sections: ;;*;;
567 ;;; Subsections: ;;;*;;;
568 ;;; Components: defuns, defvars, defconsts
569 ;;; Random code beginning with a ;;;;* comment
571 ;;; Local variables:
572 ;;; mode: emacs-lisp
573 ;;; outline-minor-mode: nil
574 ;;; mode: outline-minor
575 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
576 ;;; End:
578 ;;; essd-r.el ends here