;;; ess-install.el --- Automatic installation of ESS.1;; Auto-install procedure. EXPERIMENTAL!23;; Copyright (C) 2006 A.J. Rossini, Richard M. Heiberger, Martin4;; Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.56;; Author: Stephen Eglen78;; This file is part of ESS910;; This file is free software; you can redistribute it and/or modify11;; it under the terms of the GNU General Public License as published by12;; the Free Software Foundation; either version 2, or (at your option)13;; any later version.14;;15;; This file is distributed in the hope that it will be useful,16;; but WITHOUT ANY WARRANTY; without even the implied warranty of17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18;; GNU General Public License for more details.19;;20;; A copy of the GNU General Public License is available at21;; http://www.r-project.org/Licenses/22;;23;;24;; In short: you may use this code any way you like, as long as you25;; don't charge more than a distribution fee for it, do distribute the26;; source with any binaries, remove this notice, or hold anyone liable27;; for its results.2829;;; Commentary:3031;; Although installing ESS is relatively simple, sometimes people get32;; confused as to what to add to their init files, or even where their33;; init files are located. The following procedure should be a bit34;; simpler, as Emacs will add the necessary start-up lines itself.35;; (The instructions below assume you have downloaded ess as a zip36;; package, but it will work also for the .tar.gz version of ESS as37;; long as you know how to unpack a .tar.gz in step 3.)38;;39;; Installing ESS for the first time.40;;41;; 1. Create a folder (e.g C:/emacs) where you will store ESS. We will42;; assume that you are installing ESS into C:/emacs (unix users can use43;; ~/emacs).44;;45;; 2. Download ess-5.2.12.zip and store it in the folder you created.46;;47;; 3. Unpack the files from the zip archive, e.g. by right-clicking on it48;; within Windows explorer and selecting "Extract all". On unix, use49;; "unzip ess-5.2.12.zip".50;;51;; 4. Start a new emacs (or xemacs).52;;53;; 5. In the new emacs, you need to open the file "ess-install.el" which54;; is part of ESS. To do this, type:55;;56;; C-x C-f c:/emacs/ess-5.2.12/lisp/ess-install.el RET57;;58;; You should now see a lisp file with the top line:59;; ;;; ess-install.el --- Automatic installation of ESS.60;;61;; 6. Type M-x eval-buffer RET62;;63;; What does this do? This will find your emacs initialisation file, and64;; it will add the following two lines to the end of the file:65;;66;; ;;; ESS setup for version 5.2.1267;; (load "c:/emacs/ess-5.2.12/lisp/ess-site")68;;69;; Do not edit those two lines! They are useful if later you come to70;; upgrade ESS.71;;72;; 7. Start a new Emacs and you should find then that ESS is loaded. For73;; example, create a new file called "foo.R" and check that it opens74;; in R mode by looking at the mode line and menubar.75;;76;; Upgrading your version of ESS.77;;78;; If (and only if) you use the above instructions for installing ESS,79;; when a new version of ESS is released, you can use the same method to80;; install the new version. Repeat steps 2-7 for the new release of ESS,81;; and this time in step 6, if emacs finds that you already have the82;; special line ";;; ESS setup for version 5.2.12", it will highlight83;; those lines, and query whether you want to replace those two lines84;; with the new setup code.85;;86;; If you do upgrade ESS this way, bear in mind that your old version87;; will not be deleted from your filespace -- you will have to delete it88;; yourself.8990;; TODO: possibly add a call to (byte-recompile-directory ess-lisp-dir91;; 0) so that lisp files are byte compiled.9293;;; Code:9495;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;96;; Location where the new lisp files are stored.97(defvar ess-lisp-dir (file-name-directory98(abbreviate-file-name buffer-file-name))99"Location where the new lisp files are stored.")100101(defvar ess-site-file (concat ess-lisp-dir "ess-site")102"Full path to the new ess-site file.103Do not include .el extension in case there is also a .elc around.")104105(defvar ess-new-version nil106"Version number of new ESS to be installed.")107108(defvar ess-installed nil)109110111(defun ess-install-byte-compile ()112"Byte compile the ESS files.113This will probably generate warnings, but they can hopefully be114ignored."115;; To do byte compilation, XEmacs seems to want the files on its116;; 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))119120;; Check that ess-site-file is written using unix directory separators.121;; i.e. need to change c:\\some\\dir\\ess-site.el to122;; c:/some/dir/ess-site.el123;; To do this, we have to load in ess-replace-in-string, from124;; the file ess-inf.el125126(save-window-excursion127(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-file132(ess-replace-in-string ess-site-file "\\\\" "/" t))133)134135136;; Get the version number of the new software. Open the file137;; ess-custom.el and then find the definition of the variable138;; ess-version.139(save-window-excursion140(let ((beg))141(find-file (concat ess-lisp-dir "ess-custom.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))))))148149;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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.153154;; Highlighting uses overlays. If this is for XEmacs, we need to load155;; the overlay library, available in version 19.15156(and (not (fboundp 'make-overlay))157(condition-case nil158(require 'overlay)159('error160(error "Fm needs overlay emulation (available in XEmacs 19.15)"))))161162;; We keep a vector with several different overlays to do our highlighting.163(defvar ess-highlight-overlays [nil nil])164165;; 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)170171;; Two functions for activating and deactivation highlight overlays172(defun ess-highlight (index begin end &optional buffer)173"Highlight a region with overlay INDEX."174(move-overlay (aref ess-highlight-overlays index)175begin end (or buffer (current-buffer))))176(defun ess-unhighlight (index)177"Detatch overlay INDEX."178(delete-overlay (aref ess-highlight-overlays index)))179180;;; End of highlighting code.181;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;182183;; Try to find the .emacs init file and edit it.184(save-window-excursion185186;; 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))194195(let ((ess-commands196(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(progn202(message "You already have ESS installed.")203(setq ess-installed204(buffer-substring (point)205(save-excursion (end-of-line) (point))))206207(beginning-of-line)208(setq beg (point))209;; We assume the next line contains a sexp that loads the210;; the ess-site; this sexp can be multiple lines.211(forward-line 1)212(forward-list 1)213(ess-highlight 0 beg (point))214215(setq new-install216(yes-or-no-p217(concat "Replace ESS version " ess-installed218" with version "219ess-new-version "? ")))220(when new-install221(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))226))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."))233)))234235;;; ess-install.el ends here236237238