Re-enabled checks for re-install
[elinstall.git] / elinstall.el
blob22a5b3f4854581ad71392709407cd4c410b43ad0
1 ;;;_ elinstall.el --- Automatically and flexibly install elisp files
3 ;;;_. Headers
4 ;;;_ , License
5 ;; Copyright (C) 2010 Tom Breton (Tehom)
7 ;; Author: Tom Breton (Tehom) <tehom@panix.com>
8 ;; Keywords: maint, tools, internal
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;;_ , Commentary:
27 ;; Entry points:
28 ;; elinstall Use this for overall loading
30 ;; elinstall-arrange-preload - Use this for non-autogenerated
31 ;; files that need to be linked in.
33 ;; elinstall-update-directory-autoloads
34 ;; elinstall-update-file-autoloads
36 ;;;_ , Requires
38 (require 'autoload)
39 (require 'pp)
40 (require 'cus-edit) ;;Because we save "installedness" manually
43 ;;;_. Body
44 ;;;_ , Customizations
45 (defgroup elinstall
46 '()
47 "Customizations for elinstall"
48 :group 'elinstall)
50 (defcustom elinstall-default-priority
52 "Default priority for site-start"
53 :group 'elinstall
54 :type 'integer)
56 (defcustom elinstall-default-preload-target
57 "~/.emacs.d/site-start.d/"
58 "Default preload-target for registering autoloads"
59 :group 'elinstall
60 :type
61 '(choice
62 (const "~/.emacs.d/site-start.d/")
63 (const "/etc/emacs/site-start.d/")
64 (directory "" )
65 (const nil)
66 (const 'dot-emacs)))
69 (defcustom elinstall-already-installed
70 '()
71 "Things that have already been installed.
72 This exists for recording what has been installed. User interaction is not
73 contemplated at this time." )
75 ;;;_ , Data
76 ;;;_ . Regular expressions
77 ;;;_ , elinstall-elisp-regexp
78 (defconst elinstall-elisp-regexp
79 (let ((tmp nil))
80 (dolist
81 (suf (get-load-suffixes))
82 (unless (string-match "\\.elc" suf) (push suf tmp)))
83 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
84 "Regular expression that matches elisp files" )
86 ;;;_ , Utilities
87 ;;;_ . elinstall-directory-true-name
88 (defun elinstall-directory-true-name ()
89 "Get the true name of the directory the calling code lives in.
90 CAUTION: This is sensitive to where it's called. That's the point of it."
91 (file-name-directory
92 (if load-file-name
93 (file-truename load-file-name)
94 (file-truename buffer-file-name))))
95 ;;;_ . Checking installedness
96 ;;;_ , elinstall-already-installed
97 (defun elinstall-already-installed (project-name)
98 "Return non-nil if PROJECT-NAME has been installed."
99 (member project-name elinstall-already-installed))
101 ;;;_ , elinstall-record-installed
102 (defun elinstall-record-installed (project-name)
103 "Record that PROJECT-NAME has been installed."
105 (add-to-list 'elinstall-already-installed project-name)
106 (customize-save-variable
107 'elinstall-already-installed
108 elinstall-already-installed
109 "Set by elinstall-record-installed"))
110 ;;;_ , Work
111 ;;;_ . Doing actions
113 ;;;_ , Doing autoload actions (All adapted from autoload.el)
114 ;;;_ . Utilities about the action list
115 ;;;_ , elinstall-remove-autogen-action
116 (defun elinstall-remove-autogen-action (file actions)
117 "Return ACTIONS minus any add-file-autoloads on FILE removed."
119 (delq nil
120 (mapcar
121 #'(lambda (act)
122 (case (car act)
123 (add-file-autoloads
124 (if (equal file (third act))
126 act))
127 (t act)))
128 actions)))
129 ;;;_ , elinstall-get-autogen-action
130 (defun elinstall-get-autogen-action (file actions)
132 (let
133 ((the-act))
134 (dolist (act actions)
135 (case (car act)
136 (add-file-autoloads
137 (when (equal file (third act))
138 (setq the-act act)))))
139 the-act))
141 ;;;_ . Making autoloads
142 ;;;_ , elinstall-generate-file-autoloads
143 ;;override to allow slashed load-paths
144 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
145 ;;a new arg.
146 ;;`relative-to' can be:
147 ;; * nil: act as at present. Assume that FILE's immediate directory
148 ;;is in load-path.
149 ;; * t :: use default-directory
150 ;; * a string :: relative to it, as a filename
152 (defun elinstall-generate-file-autoloads (relative-name full-name)
153 "Insert at point a loaddefs autoload section for FILE.
154 Autoloads are generated for defuns and defmacros in FILE
155 marked by `generate-autoload-cookie' (which see).
156 If FILE is being visited in a buffer, the contents of the buffer
157 are used.
158 Return non-nil in the case where no autoloads were added at point.
160 FULL-NAME is the absolute name of the file.
161 RELATIVE-NAME is its name respective to some component of load-path."
162 (let* ((outbuf (current-buffer))
163 (autoloads-done '())
164 (print-length nil)
165 (print-readably t) ; This does something in Lucid Emacs.
166 (float-output-format nil)
167 (done-any nil)
168 (visited (get-file-buffer full-name))
169 (source-buf
170 (or visited
171 ;; It is faster to avoid visiting the file.
172 (ignore-errors (autoload-find-file full-name))))
173 output-start)
174 (if source-buf
175 (with-current-buffer source-buf
176 ;;$$MOVE ME - this should be checked in action-finding.
177 ;; Obey the no-update-autoloads file local variable.
178 (unless no-update-autoloads
179 (message "Generating autoloads for %s..." relative-name)
180 (setq output-start (with-current-buffer outbuf (point)))
181 (save-excursion
182 (save-restriction
183 (widen)
184 (goto-char (point-min))
185 (while (not (eobp))
186 (skip-chars-forward " \t\n\f")
187 (cond
188 ((looking-at (regexp-quote generate-autoload-cookie))
189 (search-forward generate-autoload-cookie)
190 (skip-chars-forward " \t")
191 (setq done-any t)
192 (if (eolp)
193 ;; Read the next form and make an autoload.
194 (let* ((form (prog1 (read (current-buffer))
195 (or (bolp) (forward-line 1))))
196 (autoload (make-autoload form load-name)))
197 (if autoload
198 (push (nth 1 form) autoloads-done)
199 (setq autoload form))
200 (let ((autoload-print-form-outbuf outbuf))
201 (autoload-print-form autoload)))
203 ;; Copy the rest of the line to the output.
204 (princ (buffer-substring
205 (progn
206 ;; Back up over whitespace, to preserve it.
207 (skip-chars-backward " \f\t")
208 (if (= (char-after (1+ (point))) ? )
209 ;; Eat one space.
210 (forward-char 1))
211 (point))
212 (progn (forward-line 1) (point)))
213 outbuf)))
214 ((looking-at ";")
215 ;; Don't read the comment.
216 (forward-line 1))
218 (forward-sexp 1)
219 (forward-line 1))))))
221 (when done-any
222 (with-current-buffer outbuf
223 (save-excursion
224 ;; Insert the section-header line which lists the file name
225 ;; and which functions are in it, etc.
226 (goto-char output-start)
227 (autoload-insert-section-header
228 outbuf autoloads-done relative-name full-name
229 (nth 5 (file-attributes full-name)))
230 (insert ";;; Generated autoloads from "
231 (autoload-trim-file-name full-name) "\n"))
232 (insert generate-autoload-section-trailer)))
233 (message "Generating autoloads for %s...done" relative-name))
235 (unless visited
236 ;; We created this buffer, so we should kill it.
237 (kill-buffer (current-buffer))))
238 (message "Could not load %s" relative-name))
240 (not done-any)))
243 ;;;_ , elinstall-deffile-insert-autoloads
244 (defun elinstall-deffile-insert-autoloads (file load-name)
245 "Update the autoloads for FILE in current buffer.
246 Return FILE if there was no autoload cookie in it, else nil.
248 Current buffer must be a loaddef-style file.
250 LOAD-NAME is the absolute name of the file.
251 RELATIVE-NAME is its name respective to some component of load-path."
252 (let (
253 (found nil)
254 (no-autoloads nil))
256 (save-excursion
257 (save-restriction
258 (widen)
259 (goto-char (point-min))
260 ;; Look for the section for FILE
261 (while (and (not found)
262 (search-forward generate-autoload-section-header nil t))
263 (let ((form (autoload-read-section-header)))
264 (cond
265 ((equal (nth 2 form) file)
266 ;; We found the section for this file.
267 (let ((begin (match-beginning 0)))
268 (progn
269 (search-forward generate-autoload-section-trailer)
270 (delete-region begin (point))
271 (setq found t))))
272 ((string< file (nth 2 form))
273 ;; We've come to a section alphabetically later than
274 ;; FILE. We assume the file is in order and so
275 ;; there must be no section for FILE. We will
276 ;; insert one before the section here.
277 (goto-char (match-beginning 0))
278 (setq found 'new)))))
279 (unless found
280 (progn
281 (setq found 'new)
282 ;; No later sections in the file. Put before the last page.
283 (goto-char (point-max))
284 (search-backward "\f" nil t)))
285 (setq no-autoloads
286 (elinstall-generate-file-autoloads file load-name))))
288 (if no-autoloads file nil)))
289 ;;;_ . Arranging to add to info-path and load-path
290 ;;;_ , elinstall-generate-add-to-path
291 (defun elinstall-generate-add-to-path (path-element type)
292 "Insert code at point to add PATH-ELEMENT to a path.
293 If TYPE is:
294 * `add-to-load-path', add to load-path
295 * `add-to-info-path', add to Info-default-directory-list
297 Current buffer must be a loaddef-style file."
298 (let ( (path-symbol
299 (case type
300 (add-to-load-path 'load-path)
301 (add-to-info-path 'Info-default-directory-list)))
302 (description
303 (case type
304 (add-to-load-path "load-path")
305 (add-to-info-path "info-path")))
306 (autoloads-done '())
307 (print-length nil)
308 (print-readably t) ; This does something in Lucid Emacs.
309 (float-output-format nil))
311 (message "Generating %s additions..." description)
313 (autoload-insert-section-header
314 (current-buffer) (list path-element) nil nil
315 nil)
316 (insert ";;; Generated path addition\n")
318 `(add-to-list ',path-symbol
319 (expand-file-name
320 ,(file-relative-name path-element)
321 (if load-file-name
322 (file-name-directory
323 (file-truename load-file-name)))))
324 (current-buffer))
326 (insert generate-autoload-section-trailer)
327 (message "Generating %s additions...done" description)))
330 ;;;_ , elinstall-deffile-insert-add-to-path
331 (defun elinstall-deffile-insert-add-to-path (path-element type)
332 "Insert code in current buffer to add PATH-ELEMENT to a path.
333 If TYPE is:
334 * `add-to-load-path', add to load-path
335 * `add-to-info-path', add to Info-default-directory-list
337 Current buffer must be a loaddef-style file."
338 (let (
339 (found nil)
340 (no-autoloads nil))
342 (save-excursion
343 (save-restriction
344 (widen)
345 (goto-char (point-min))
346 ;; Look for the section for PATH-ELEMENT
347 (while (and (not found)
348 (search-forward generate-autoload-section-header nil t))
349 (let ((form (autoload-read-section-header)))
350 (cond
351 ((and
352 (equal (nth 0 form) type)
353 (member (nth 1 form) path-element))
355 ;; We found the section for this add.
356 (let ((begin (match-beginning 0)))
357 (progn
358 (search-forward generate-autoload-section-trailer)
359 (delete-region begin (point))
360 (setq found t)))))))
362 (unless found
363 (progn
364 (setq found 'new)
365 ;; No later sections in the file. Put before the last page.
366 (goto-char (point-max))
367 (search-backward "\f" nil t)))
369 (elinstall-generate-add-to-path path-element type)))
371 ;;This never belongs in the no-autoloads section.
372 nil))
374 ;;;_ . elinstall-deffile-insert
376 (defun elinstall-deffile-insert (action)
377 "Insert autoloads etc into current file according to ACTION.
378 The format of ACTION is described in the design docs.
380 Return filename if this action belongs in the no-autoload section."
382 (when action
383 (case (car action)
384 (add-file-autoloads
385 (elinstall-deffile-insert-autoloads
386 (third action)
387 (fifth action)))
389 (add-to-load-path
390 (elinstall-deffile-insert-add-to-path
391 (third action)
392 'add-to-load-path)
393 nil)
395 (add-to-info-path
396 (elinstall-deffile-insert-add-to-path
397 (third action)
398 'add-to-info-path)
399 nil)
401 ((preload-file run-tests byte-compile)
402 (error "This case should not come here.")))))
404 ;;;_ . elinstall-prepare-deffile
405 (defun elinstall-prepare-deffile (deffile)
406 "Try to ensure that DEFFILE is available for receiving autoloads"
408 (autoload-ensure-default-file deffile)
409 (with-current-buffer (find-file-noselect deffile)
412 ;; We must read/write the file without any code conversion,
413 ;; but still decode EOLs.
414 (let ((coding-system-for-read 'raw-text))
416 ;; This is to make generated-autoload-file have Unix EOLs, so
417 ;; that it is portable to all platforms.
418 (setq buffer-file-coding-system 'raw-text-unix))
419 (or (> (buffer-size) 0)
420 (error "Autoloads file %s does not exist" buffer-file-name))
421 (or (file-writable-p buffer-file-name)
422 (error "Autoloads file %s is not writable"
423 buffer-file-name))))
425 ;;;_ . elinstall-update-deffile
427 ;;Adapted from autoload.el `update-directory-autoloads'.
428 ;;Still being adapted:
430 ;; * Still need to treat add-to-info-path and
431 ;;add-to-load-path. Both recognize them and insert them.
432 ;; * Adapt `elinstall-update-file-autoloads' to understand actions.
434 ;; * Finding "file" among actions is rickety. Maybe knowing the
435 ;; respective load-path element would help.
437 (defun elinstall-update-deffile (target actions &optional
438 use-load-path force)
440 Update file TARGET with current autoloads as specified by ACTIONS.
441 Also remove any old definitions pointing to libraries that can no
442 longer be found.
444 ACTIONS must be a list of actions (See the format doc). Each one's
445 filename must be relative to some element of load-path.
447 USE-LOAD-PATH is a list to use as load-path. It should include
448 any new load-path that we are arranging to create. If it's not given,
449 load-path itself is used.
451 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
452 Other non-nil cases of FORCE are reserved for future development.
454 This uses `update-file-autoloads' (which see) to do its work.
455 In an interactive call, you must give one argument, the name
456 of a single directory."
457 (let
459 (use-load-path (or use-load-path load-path))
460 (this-time (current-time))
461 ;;files with no autoload cookies.
462 (no-autoloads nil))
464 (elinstall-prepare-deffile target)
465 (with-current-buffer
466 (find-file-noselect target)
467 (save-excursion
468 (setq actions
469 (elinstall-remove-autogen-action
470 (autoload-trim-file-name target)
471 actions))
473 (goto-char (point-min))
474 (while (search-forward generate-autoload-section-header nil t)
475 (let* ((form (autoload-read-section-header))
476 (file (nth 3 form)))
477 (cond ((and (consp file) (stringp (car file)))
478 ;; This is a list of files that have no
479 ;; autoload cookies.
480 ;; There shouldn't be more than one such entry.
481 ;; Remove the obsolete section.
482 (autoload-remove-section (match-beginning 0))
483 (let ((last-time (nth 4 form)))
484 (dolist (file file)
485 (let ((file-time (nth 5 (file-attributes file))))
486 (when (and file-time
487 (not (time-less-p last-time file-time)))
488 ;; file unchanged
489 (push file no-autoloads)
490 (setq actions
491 (elinstall-remove-autogen-action
492 file actions)))))))
493 ((not (stringp file)))
495 (let
496 ((file-path
497 (locate-library file nil use-load-path)))
498 (cond
499 ;;File doesn't exist, so remove its
500 ;;section.
501 ((not file-path)
502 (autoload-remove-section
503 (match-beginning 0)))
505 ;; File hasn't changed, so do nothing.
506 ((equal
507 (nth 4 form)
508 (nth 5 (file-attributes file-path)))
509 nil)
511 (elinstall-deffile-insert
512 (elinstall-get-autogen-action
513 file actions))))
515 (setq actions
516 (elinstall-remove-autogen-action
517 file actions))))))))
519 ;; Remaining actions have no existing autoload sections yet.
520 (setq no-autoloads
521 (append no-autoloads
522 (delq nil (mapcar #'elinstall-deffile-insert actions))))
523 (when no-autoloads
524 ;; Sort them for better readability.
525 (setq no-autoloads (sort no-autoloads 'string<))
526 ;; Add the `no-autoloads' section.
527 (goto-char (point-max))
528 (search-backward "\f" nil t)
529 (autoload-insert-section-header
530 (current-buffer) nil nil no-autoloads this-time)
531 (insert generate-autoload-section-trailer))
532 (save-buffer))))
535 ;;;_ , Doing actions to arrange preloads
536 ;;;_ . elinstall-insert-add-to-path
537 (defun elinstall-insert-add-to-path (new path-sym)
538 "Insert code to add NEW to PATH-SYM.
539 Insert this at point in current buffer."
540 (insert "\n")
542 `(add-to-list ',path-sym
543 (expand-file-name ,new
544 (if load-file-name
545 (file-name-directory
546 (file-truename load-file-name)))))
547 (current-buffer)))
549 ;;;_ . elinstall-insert-add-to-load-path
550 (defun elinstall-insert-add-to-load-path (new)
551 "Insert code to add NEW to load-path.
552 Insert this at point in current buffer."
553 (elinstall-insert-add-to-path new 'load-path))
555 ;;;_ . elinstall-insert-add-to-info-path
556 (defun elinstall-insert-add-to-info-path (new)
557 "Insert code to add NEW to info-path.
558 Insert this at point in current buffer."
559 (elinstall-insert-add-to-path new 'Info-default-directory-list))
561 ;;;_ . elinstall-symlink-on-emacs-start
562 (defun elinstall-symlink-on-emacs-start
563 (filename target-basename target-dir &optional priority force)
564 "Symlink to TARGET-BASENAME.el in TARGET-DIR
566 If PRIORITY is given, it will be used as the priority prefix,
567 otherwise elinstall-default-priority will be.
568 PRIORITY must be an integer or nil.
569 If FORCE is `t', do it regardless of timestamps etc.
570 Other non-nil cases of FORCE are reserved for future development."
571 (let*
573 (priority (or priority elinstall-default-priority))
574 (target-name-nodir
575 (format
576 "%d%s.el"
577 priority
578 target-basename))
579 (target
580 (expand-file-name target-name-nodir target-dir)))
583 (cond
584 ;;Path should already exist.
585 ((not
586 (file-exists-p target-dir))
587 (message "The target directory doesn't exist."))
588 ;;Target shouldn't already exist, but if force is given, let
589 ;;user override.
590 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
591 ;;do nothing.
592 ((and
593 (file-exists-p target)
595 (not force)
596 (not
597 (yes-or-no-p
598 (format "Really overwrite %s? " project-name))))
599 (message "File %s already exists" target)))
602 (make-symbolic-link
603 filename
604 target
605 nil)))))
607 ;;;_ . elinstall-add-to-dot-emacs
608 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
609 "Add code to load FILENAME to .emacs.
610 FILENAME should not have an extension"
612 ;;Visit .emacs
613 (with-current-buffer (find-file-noselect dot-emacs-name)
614 (save-excursion
615 ;;add at end of file
616 (goto-char (point-max))
617 (insert "\n;;Added by elinstall")
618 (insert "\n;;Consider using my-site-start to manage .emacs\n")
619 (pp `(load ,filename) (current-buffer))
620 (save-buffer))))
623 ;;;_ . elinstall-arrange-preload
624 ;;;###autoload
625 (defun elinstall-arrange-preload (force filename basename &optional priority)
626 "Arrange for FILENAME to be loaded on emacs start.
627 FORCE has its usual meaning.
628 BASENAME and PRIORITY are used as arguments to
629 `elinstall-symlink-on-emacs-start'.
632 (let
633 ((preload-target elinstall-default-preload-target))
635 ;;Dispatch the possibilities.
636 (cond
637 ((eq preload-target 'dot-emacs)
638 (elinstall-add-to-dot-emacs "~/.emacs" filename))
639 ((stringp preload-target)
640 (elinstall-symlink-on-emacs-start
641 filename basename preload-target priority force))
642 (null preload-target
643 (message "Not arranging for preloads"))
645 (message "I don't recognize that")))))
647 ;;;_ , Cleanup actions
648 ;;Nothing yet. This will be another type of action.
650 ;;;_ . Segregating actions
651 ;;;_ , elinstall-remove-empty-segs
652 (defun elinstall-remove-empty-segs (segment-list)
653 "Return SEGMENT-LIST minus any segments that have no actions"
654 (delq nil
655 (mapcar
656 #'(lambda (segment)
657 (if (cdr segment)
658 segment
659 nil))
660 segment-list)))
662 ;;;_ , elinstall-segregate-actions
663 (defun elinstall-segregate-actions (actions)
664 "Return actions segregated by deffile.
666 Returns a list whose elements are each a cons of:
667 * deffile filename or nil
668 * A list of actions to be done for that deffile."
670 ;;$$IMPROVE ME - put tests in a separate segment, and byte-compile
671 ;;in yet another one.
672 (let
673 ((segment-list '())
674 (last-segment (list nil)))
676 (dolist (act actions)
677 (when act
678 (let*
679 ( (deffile-name
680 (case (car act)
681 ((add-file-autoloads
682 add-to-info-path
683 add-to-load-path)
684 (second act))
685 ((preload-file run-tests byte-compile)
686 nil)))
688 (cell
689 (if deffile-name
690 (assoc deffile-name segment-list)
691 last-segment)))
692 (if cell
693 (setcdr cell (cons act (cdr cell)))
694 (setq segment-list
695 (cons
696 (cons
697 deffile-name
698 (list act))
699 segment-list))))))
700 (elinstall-remove-empty-segs
701 (append
702 segment-list
703 (if (cdr last-segment)
704 (list last-segment)
705 '())))))
710 ;;;_ . Finding actions
711 ;;;_ , Treating the parameter list
712 ;;;_ . elinstall-add-parameter
713 (defun elinstall-add-parameter (alist key new-value)
714 "Add a new value for KEY to ALIST"
716 (cons
717 (cons key new-value)
718 (assq-delete-all key (copy-list alist))))
720 ;;;_ . elinstall-get-parameter
721 (defun elinstall-get-parameter (alist key)
722 "Get the value of KEY from ALIST"
724 (cdr (assq key alist)))
725 ;;;_ . elinstall-expand-file-name
726 ;;$$OBSOLETE
728 (defun elinstall-expand-file-name (filename alist)
729 "Expand FILENAME by the value of `path' in ALIST"
730 (expand-file-name
731 filename
732 (elinstall-get-parameter alist 'path)))
733 ;;;_ , Finding deffiles
734 ;;;_ . elinstall-expand-deffile-name
735 (defun elinstall-expand-deffile-name (deffile)
736 "Expand DEFFILE autoload.el's way."
738 (expand-file-name (or deffile "loaddefs.el")
739 (expand-file-name "lisp"
740 source-directory)))
742 ;;;_ . elinstall-maybe-get-deffile
743 (defun elinstall-maybe-get-deffile (file)
744 "If FILE defined `generated-autoload-file', return it.
745 Otherwise return nil.
746 Return it as an absolute filename."
748 (save-excursion
749 ;;$$FIXME load buffer if it's not already loaded
750 (let*
751 ((existing-buffer (get-file-buffer file)))
753 ;; We want to get a value for generated-autoload-file from
754 ;; the local variables section if it's there.
755 ;;But if it's not loaded, we don't? Maybe should use
756 ;; `autoload-find-file' and load it.
757 (if existing-buffer
758 (set-buffer existing-buffer))
759 (if (local-variable-p 'generated-autoload-file)
760 (elinstall-expand-deffile-name
761 generated-autoload-file)
762 nil))))
766 ;;;_ , elinstall-find-actions-by-spec
768 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
769 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
771 LOAD-PATH-ELEMENT is the conceptual element of load-path that
772 surrounds PATH. It may not yet have been added to load-path."
773 (if (consp spec)
774 ;;$$IMPROVE ME by adding the other cases in the design.
775 (case (car spec)
777 (let
778 ((new-path
779 (expand-file-name
780 (second spec)
781 path)))
783 (elinstall-find-actions-by-spec
784 (third spec)
785 load-path-element
786 new-path
787 parameters)))
789 (all
790 (apply #'append
791 (mapcar
792 #'(lambda (sub-spec)
793 (elinstall-find-actions-by-spec
794 sub-spec
795 load-path-element
796 path
797 parameters))
798 (cdr spec))))
799 ;;$$WRITEME
800 (file
803 (dir
804 (let
805 ((dirname
806 (expand-file-name
807 (second spec)
808 path))
809 (load-path-here
810 (not
811 (elinstall-get-parameter
812 parameters 'block-add-to-load-path))))
813 (cons
814 ;;$$IMPROVE ME
815 ;;Do this only if there are loadable files.
816 (if load-path-here
817 `(add-to-load-path
818 ,(elinstall-get-parameter
819 parameters 'def-file)
820 ,dirname)
821 '())
822 ;;$$IMPROVE ME
823 ;;Do add-to-info-path too. But test if there are
824 ;;any info files present.
826 ;;$$IMPROVE ME
827 ;; We want to get a value for generated-autoload-file
828 ;; from the local variables section if it's there.
829 ;;Use `elinstall-maybe-get-deffile'
830 ;; Otherwise we'll use `def-file' in parameters.
832 ;;$$FIXME This isn't quite right. If directory
833 ;;itself is not in load-path, this will be wrong.
834 ;;Gotta know where our encompassing part of
835 ;;load-path is.
837 ;;$$ENCAP ME This should be shared with the
838 ;;treatment of (file FN)
840 ;;$$FIXME Don't do directories, but maybe recurse on
841 ;;them, if a flag is set. And since we definitely
842 ;;have a load-path element here,
843 ;;'block-add-to-load-path according to a parameter.
844 ;;Maybe could follow/not symlinks similarly.
845 (mapcar
846 #'(lambda (filename)
847 (let
848 ((full-path
849 (expand-file-name filename dirname)))
850 `(add-file-autoloads
851 ,(elinstall-get-parameter
852 parameters 'def-file)
853 ,(file-name-sans-extension
854 (file-relative-name
855 full-path
856 load-path-element))
857 ,load-path-element ;;Is this still used?
858 ,full-path)))
860 (directory-files
861 dirname
862 nil ;;Relative filenames
863 elinstall-elisp-regexp)))))
865 (def-file
866 (let
867 ((new-def-file
868 (expand-file-name
869 (second spec)
870 path)))
871 (cons
872 ;;$$IMPROVE ME see a priority argument. See
873 ;;parameters of whether to give this action at all.
874 ;;See a parameter for basename
875 `(preload-file
876 ,new-def-file
877 ,(file-name-sans-extension
878 (second spec)))
880 (elinstall-find-actions-by-spec
881 (third spec)
882 load-path-element
883 path
884 (elinstall-add-parameter parameters
885 'def-file new-def-file))))))
887 ;;$$IMPROVE ME by adding the other cases in the design.
888 (case spec
889 (t))))
890 ;;;_ . high-level work
891 ;;;_ , elinstall-get-relevant-load-path
892 (defun elinstall-get-relevant-load-path (actions)
894 (delq nil
895 (mapcar
896 #'(lambda (act)
897 (case (car act)
898 (add-to-load-path
899 (second act))
900 (t nil)))
901 actions)))
903 ;;;_ , elinstall-do-segment
904 (defun elinstall-do-segment (segment force use-load-path)
905 "Do all the actions in SEGMENT.
906 FORCE has its usual meaning.
907 USE-LOAD-PATH is the effective load-path."
909 ;;$$IMPROVE ME - this will have to know the additions to load-path.
910 (let*
911 ((deffile (car segment)))
912 (if (stringp deffile)
913 (elinstall-update-deffile deffile (cdr segment) force
914 use-load-path)
915 ;;Do other actions: link-in actions and cleanups.
916 (mapcar
917 #'(lambda (act)
919 (case (car act)
920 (preload-file
921 (apply
922 #'elinstall-arrange-preload
923 force
924 (cdr act)))
925 (run-tests
926 ;;$$WRITE ME - not a high priority right now.
927 nil)
929 ;;$$IMPROVE ME Understand flags to control second
930 ;;argument (whether to load file after
931 ;;compilation)
932 (byte-compile
933 (byte-compile-file (second act)))))
935 (cdr segment)))))
938 ;;;_ . Overall work
939 ;;;_ , elinstall-x
940 (defun elinstall-x (dir spec &optional force)
942 (let*
944 ;;This is just the default deffile, spec can override it.
945 (def-file
946 (elinstall-expand-deffile-name nil))
947 (actions
948 (elinstall-find-actions-by-spec
949 spec
953 (def-file . ,def-file ))))
954 (segment-list (elinstall-segregate-actions actions))
955 (use-load-path
956 (elinstall-get-relevant-load-path
957 actions)))
959 (mapcar
960 #'(lambda (segment)
961 (elinstall-do-segment segment force use-load-path))
962 segment-list)))
964 ;;;_ . Entry points
965 ;;;_ , elinstall
966 (defun elinstall (project-name path spec &optional force)
967 "Install elisp files.
968 They need not be a formal package.
970 Parameters:
972 PROJECT-NAME - the name of the project
974 PATH - Path to the project.
975 Suggestion: (elinstall-directory-true-name)
977 SPEC - a spec for the autoloads etc to make. It can be as simple as
978 \(dir \"\.\") for installing one directory.
980 If FORCE is t, install a package even if it has already been
981 installed. Other non-nil cases of FORCE are reserved for future
982 development."
984 (when
985 (and
986 (or
987 force
988 (not (elinstall-already-installed project-name)))
989 (yes-or-no-p (format "Re-install %s? " project-name)))
990 (elinstall-x path spec force)
991 (elinstall-record-installed project-name)))
995 ;;;_ , elinstall-update-directory-autoloads
996 ;;$$TEST ME
997 (defun elinstall-update-directory-autoloads (dir)
1000 (interactive "DInstall all elisp files from directory: ")
1002 (elinstall-x
1004 '(dir ".")
1005 (elinstall-expand-deffile-name
1006 generated-autoload-file)))
1009 ;;;_ , elinstall-update-file-autoloads
1010 ;;$$TEST ME
1011 (defun elinstall-update-file-autoloads (file)
1014 (interactive "fInstall elisp file: ")
1015 (elinstall
1016 file
1017 `(file ,file)
1019 (elinstall-maybe-get-deffile file)
1020 (elinstall-expand-deffile-name
1021 generated-autoload-file))))
1028 ;;;_. Footers
1029 ;;;_ , Provides
1031 (provide 'elinstall)
1033 ;;;_ * Local emacs vars.
1034 ;;;_ + Local variables:
1035 ;;;_ + mode: allout
1036 ;;;_ + End:
1038 ;;;_ , End
1039 ;;; elinstall.el ends here