*** empty log message ***
[ess.git] / lisp / ess-install.el
blobd57b8a11c2aa41f3104a170b841edfdb0105c340
1 ;;; ess-install.el --- Automatic installation of ESS.
2 ;; Auto-install procedure. EXPERIMENTAL!
4 ;; Copyright (C) 2006 A.J. Rossini, Rich M. Heiberger, Martin
5 ;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
7 ;; Original Author: Stephen Eglen
9 ;; This file is part of ESS
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; In short: you may use this code any way you like, as long as you
26 ;; don't charge more than a distribution fee for it, do distribute the
27 ;; source with any binaries, remove this notice, or hold anyone liable
28 ;; for its results.
31 ;;; Commentary:
33 ;; Although installing ESS is relatively simple, sometimes people get
34 ;; confused as to what to add to their init files, or even where their
35 ;; init files are located. The following procedure should be a bit
36 ;; simpler, as Emacs will add the necessary start-up lines itself.
37 ;; (The instructions below assume you have downloaded ess as a zip
38 ;; package, but it will work also for the .tar.gz version of ESS as
39 ;; long as you know how to unpack a .tar.gz in step 3.)
40 ;;
41 ;; Installing ESS for the first time.
42 ;;
43 ;; 1. Create a folder (e.g C:/emacs) where you will store ESS. We will
44 ;; assume that you are installing ESS into C:/emacs (unix users can use
45 ;; ~/emacs).
46 ;;
47 ;; 2. Download ess-5.2.12.zip and store it in the folder you created.
48 ;;
49 ;; 3. Unpack the files from the zip archive, e.g. by right-clicking on it
50 ;; within Windows explorer and selecting "Extract all". On unix, use
51 ;; "unzip ess-5.2.12.zip".
52 ;;
53 ;; 4. Start a new emacs (or xemacs).
54 ;;
55 ;; 5. In the new emacs, you need to open the file "ess-install.el" which
56 ;; is part of ESS. To do this, type:
57 ;;
58 ;; C-x C-f c:/emacs/ess-5.2.12/lisp/ess-install.el RET
59 ;;
60 ;; You should now see a lisp file with the top line:
61 ;; ;;; ess-install.el --- Automatic installation of ESS.
62 ;;
63 ;; 6. Type M-x eval-buffer RET
64 ;;
65 ;; What does this do? This will find your emacs initialisation file, and
66 ;; it will add the following two lines to the end of the file:
67 ;;
68 ;; ;;; ESS setup for version 5.2.12
69 ;; (load "c:/emacs/ess-5.2.12/lisp/ess-site")
70 ;;
71 ;; Do not edit those two lines! They are useful if later you come to
72 ;; upgrade ESS.
73 ;;
74 ;; 7. Start a new Emacs and you should find then that ESS is loaded. For
75 ;; example, create a new file called "foo.R" and check that it opens
76 ;; in R mode by looking at the mode line and menubar.
77 ;;
78 ;; Upgrading your version of ESS.
79 ;;
80 ;; If (and only if) you use the above instructions for installing ESS,
81 ;; when a new version of ESS is released, you can use the same method to
82 ;; install the new version. Repeat steps 2-7 for the new release of ESS,
83 ;; and this time in step 6, if emacs finds that you already have the
84 ;; special line ";;; ESS setup for version 5.2.12", it will highlight
85 ;; those lines, and query whether you want to replace those two lines
86 ;; with the new setup code.
87 ;;
88 ;; If you do upgrade ESS this way, bear in mind that your old version
89 ;; will not be deleted from your filespace -- you will have to delete it
90 ;; yourself.
92 ;; TODO: possibly add a call to (byte-recompile-directory ess-lisp-dir
93 ;; 0) so that lisp files are byte compiled.
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96 ;; Location where the new lisp files are stored.
97 (defvar ess-lisp-dir (file-name-directory
98 (abbreviate-file-name buffer-file-name))
99 "Location where the new lisp files are stored.")
101 (defvar ess-site-file (concat ess-lisp-dir "ess-site")
102 "Full path to the new ess-site file.
103 Do not include .el extension in case there is also a .elc around.")
105 (defvar ess-new-version nil
106 "Version number of new ESS to be installed.")
108 (defvar ess-installed nil)
111 (defun ess-install-byte-compile ()
112 "Byte compile the ESS files.
113 This will probably generate warnings, but they can hopefully be
114 ignored."
115 ;; To do byte compilation, XEmacs seems to want the files on its
116 ;; load-path so that it can do the (require 'xyz) statements.
117 (add-to-list 'load-path ess-lisp-dir)
118 (byte-recompile-directory ess-lisp-dir 0))
120 ;; Check that ess-site-file is written using unix directory separators.
121 ;; i.e. need to change c:\\some\\dir\\ess-site.el to
122 ;; c:/some/dir/ess-site.el
123 ;; To do this, we have to load in ess-replace-in-string, from
124 ;; the file ess-inf.el
126 (save-window-excursion
127 (find-file (concat ess-lisp-dir "ess-inf.el"))
128 (goto-char (point-min))
129 (search-forward-regexp "^(defun ess-replace-in-string " nil t)
130 (eval-defun nil)
131 (setq ess-site-file
132 (ess-replace-in-string ess-site-file "\\\\" "/" t))
136 ;; Get the version number of the new software. Open the file
137 ;; ess-cust.el and then find the definition of the variable
138 ;; ess-version.
139 (save-window-excursion
140 (let ((beg))
141 (find-file (concat ess-lisp-dir "ess-cust.el"))
142 ;; go back to start, just in case file was previously open.
143 (goto-char (point-min))
144 (search-forward "defvar ess-version \"")
145 (setq beg (point))
146 (search-forward "\"")
147 (setq ess-new-version (buffer-substring beg (1- (point))))))
149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 ;;; Highlighting (copied from reftex.el -- cheers Carsten!)
151 ;; Only one highlight region is needed, whereas two are provided here,
152 ;; so this code could be simplified. But we may want it again later.
154 ;; Highlighting uses overlays. If this is for XEmacs, we need to load
155 ;; the overlay library, available in version 19.15
156 (and (not (fboundp 'make-overlay))
157 (condition-case nil
158 (require 'overlay)
159 ('error
160 (error "Fm needs overlay emulation (available in XEmacs 19.15)"))))
162 ;; We keep a vector with several different overlays to do our highlighting.
163 (defvar ess-highlight-overlays [nil nil])
165 ;; Initialize the overlays (here we provide two overlays)
166 (aset ess-highlight-overlays 0 (make-overlay 1 1))
167 (overlay-put (aref ess-highlight-overlays 0) 'face 'highlight)
168 (aset ess-highlight-overlays 1 (make-overlay 1 1))
169 (overlay-put (aref ess-highlight-overlays 1) 'face 'highlight)
171 ;; Two functions for activating and deactivation highlight overlays
172 (defun ess-highlight (index begin end &optional buffer)
173 "Highlight a region with overlay INDEX."
174 (move-overlay (aref ess-highlight-overlays index)
175 begin end (or buffer (current-buffer))))
176 (defun ess-unhighlight (index)
177 "Detatch overlay INDEX."
178 (delete-overlay (aref ess-highlight-overlays index)))
180 ;;; End of highlighting code.
181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
183 ;; Try to find the .emacs init file and edit it.
184 (save-window-excursion
186 ;; Try to find the init file if one already exists,
187 ;; or create a new one if we can't find any.
188 (if (stringp user-init-file)
189 (find-file user-init-file)
190 ;; else, let's guess that the init file should be called ".emacs"
191 ;; and the tilde will be resolved okay.
192 (find-file "~/.emacs"))
193 (goto-char (point-min))
195 (let ((ess-commands
196 (concat "\n;;; ESS setup for version " ess-new-version "\n"
197 "(load \"" ess-site-file "\")\n"))
198 (new-install)
199 (beg))
200 (if (search-forward ";;; ESS setup for version " nil t)
201 (progn
202 (message "You already have ESS installed.")
203 (setq ess-installed
204 (buffer-substring (point)
205 (save-excursion (end-of-line) (point))))
207 (beginning-of-line)
208 (setq beg (point))
209 ;; We assume the next line contains a sexp that loads the
210 ;; the ess-site; this sexp can be multiple lines.
211 (forward-line 1)
212 (forward-list 1)
213 (ess-highlight 0 beg (point))
215 (setq new-install
216 (yes-or-no-p
217 (concat "Replace ESS version " ess-installed
218 " with version "
219 ess-new-version "? ")))
220 (when new-install
221 (kill-region beg (point))
222 (insert ess-commands)
223 (save-buffer)
224 (ess-install-byte-compile)
225 (message (concat "ESS updated to version " ess-new-version))
227 ;; else, just insert commands at end.
228 (goto-char (point-max))
229 (insert ess-commands)
230 (save-buffer)
231 (ess-install-byte-compile)
232 (message (concat "ESS version "ess-new-version" installed."))