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