Changed from homogeneous segments to non-homogeneous ones
[elinstall.git] / elinstall.el
blob1adaf293df5949b0e6bf7ae6e50cf966f3165fa5
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
197 (make-autoload form relative-name)))
198 (if autoload
199 (push (nth 1 form) autoloads-done)
200 (setq autoload form))
201 (let ((autoload-print-form-outbuf outbuf))
202 (autoload-print-form autoload)))
204 ;; Copy the rest of the line to the output.
205 (princ (buffer-substring
206 (progn
207 ;; Back up over whitespace, to preserve it.
208 (skip-chars-backward " \f\t")
209 (if (= (char-after (1+ (point))) ? )
210 ;; Eat one space.
211 (forward-char 1))
212 (point))
213 (progn (forward-line 1) (point)))
214 outbuf)))
215 ((looking-at ";")
216 ;; Don't read the comment.
217 (forward-line 1))
219 (forward-sexp 1)
220 (forward-line 1))))))
222 (when done-any
223 (with-current-buffer outbuf
224 (save-excursion
225 ;; Insert the section-header line which lists the file name
226 ;; and which functions are in it, etc.
227 (goto-char output-start)
228 (autoload-insert-section-header
229 outbuf autoloads-done relative-name full-name
230 (nth 5 (file-attributes full-name)))
231 (insert ";;; Generated autoloads from "
232 (autoload-trim-file-name full-name) "\n"))
233 (insert generate-autoload-section-trailer)))
234 (message "Generating autoloads for %s...done" relative-name))
236 (unless visited
237 ;; We created this buffer, so we should kill it.
238 (kill-buffer (current-buffer))))
239 (message "Could not load %s" relative-name))
241 (not done-any)))
244 ;;;_ , elinstall-deffile-insert-autoloads
245 (defun elinstall-deffile-insert-autoloads (file load-name)
246 "Update the autoloads for FILE in current buffer.
247 Return FILE if there was no autoload cookie in it, else nil.
249 Current buffer must be a loaddef-style file.
251 LOAD-NAME is the absolute name of the file.
252 RELATIVE-NAME is its name respective to some component of load-path."
253 (let (
254 (found nil)
255 (no-autoloads nil))
257 (save-excursion
258 (save-restriction
259 (widen)
260 (goto-char (point-min))
261 ;; Look for the section for FILE
262 (while (and (not found)
263 (search-forward generate-autoload-section-header nil t))
264 (let ((form (autoload-read-section-header)))
265 (cond
266 ((equal (nth 2 form) file)
267 ;; We found the section for this file.
268 (let ((begin (match-beginning 0)))
269 (progn
270 (search-forward generate-autoload-section-trailer)
271 (delete-region begin (point))
272 (setq found t))))
273 ((string< file (nth 2 form))
274 ;; We've come to a section alphabetically later than
275 ;; FILE. We assume the file is in order and so
276 ;; there must be no section for FILE. We will
277 ;; insert one before the section here.
278 (goto-char (match-beginning 0))
279 (setq found 'new)))))
280 (unless found
281 (progn
282 (setq found 'new)
283 ;; No later sections in the file. Put before the last page.
284 (goto-char (point-max))
285 (search-backward "\f" nil t)))
286 (setq no-autoloads
287 (elinstall-generate-file-autoloads file load-name))))
289 (if no-autoloads file nil)))
290 ;;;_ . Arranging to add to info-path and load-path
291 ;;;_ , elinstall-generate-add-to-path
292 (defun elinstall-generate-add-to-path (path-element type)
293 "Insert code at point to add PATH-ELEMENT to a path.
294 If TYPE is:
295 * `add-to-load-path', add to load-path
296 * `add-to-info-path', add to Info-default-directory-list
298 Current buffer must be a loaddef-style file."
299 (let ( (path-symbol
300 (case type
301 (add-to-load-path 'load-path)
302 (add-to-info-path 'Info-default-directory-list)))
303 (description
304 (case type
305 (add-to-load-path "load-path")
306 (add-to-info-path "info-path")))
307 (autoloads-done '())
308 (print-length nil)
309 (print-readably t) ; This does something in Lucid Emacs.
310 (float-output-format nil))
312 (message "Generating %s additions..." description)
314 (autoload-insert-section-header
315 (current-buffer) (list path-element) nil nil
316 nil)
317 (insert ";;; Generated path addition\n")
319 `(add-to-list ',path-symbol
320 (expand-file-name
321 ,(file-relative-name path-element)
322 (if load-file-name
323 (file-name-directory
324 (file-truename load-file-name)))))
325 (current-buffer))
327 (insert generate-autoload-section-trailer)
328 (message "Generating %s additions...done" description)))
331 ;;;_ , elinstall-deffile-insert-add-to-path
332 (defun elinstall-deffile-insert-add-to-path (path-element type)
333 "Insert code in current buffer to add PATH-ELEMENT to a path.
334 If TYPE is:
335 * `add-to-load-path', add to load-path
336 * `add-to-info-path', add to Info-default-directory-list
338 Current buffer must be a loaddef-style file."
339 (let (
340 (found nil)
341 (no-autoloads nil))
343 (save-excursion
344 (save-restriction
345 (widen)
346 (goto-char (point-min))
347 ;; Look for the section for PATH-ELEMENT
348 (while (and (not found)
349 (search-forward generate-autoload-section-header nil t))
350 (let ((form (autoload-read-section-header)))
351 (cond
352 ((and
353 (equal (nth 0 form) type)
354 (member (nth 1 form) path-element))
356 ;; We found the section for this add.
357 (let ((begin (match-beginning 0)))
358 (progn
359 (search-forward generate-autoload-section-trailer)
360 (delete-region begin (point))
361 (setq found t)))))))
363 (unless found
364 (progn
365 (setq found 'new)
366 ;; No later sections in the file. Put before the last page.
367 (goto-char (point-max))
368 (search-backward "\f" nil t)))
370 (elinstall-generate-add-to-path path-element type)))
372 ;;This never belongs in the no-autoloads section.
373 nil))
375 ;;;_ . elinstall-deffile-insert
377 (defun elinstall-deffile-insert (action)
378 "Insert autoloads etc into current file according to ACTION.
379 The format of ACTION is described in the design docs.
381 Return filename if this action belongs in the no-autoload section."
383 (when action
384 (case (car action)
385 (add-file-autoloads
386 (elinstall-deffile-insert-autoloads
387 (third action)
388 (fifth action)))
390 (add-to-load-path
391 (elinstall-deffile-insert-add-to-path
392 (third action)
393 'add-to-load-path)
394 nil)
396 (add-to-info-path
397 (elinstall-deffile-insert-add-to-path
398 (third action)
399 'add-to-info-path)
400 nil)
402 ((preload-file run-tests byte-compile)
403 (error "This case should not come here.")))))
405 ;;;_ . elinstall-prepare-deffile
406 (defun elinstall-prepare-deffile (deffile)
407 "Try to ensure that DEFFILE is available for receiving autoloads"
409 (autoload-ensure-default-file deffile)
410 (with-current-buffer (find-file-noselect deffile)
413 ;; We must read/write the file without any code conversion,
414 ;; but still decode EOLs.
415 (let ((coding-system-for-read 'raw-text))
417 ;; This is to make generated-autoload-file have Unix EOLs, so
418 ;; that it is portable to all platforms.
419 (setq buffer-file-coding-system 'raw-text-unix))
420 (or (> (buffer-size) 0)
421 (error "Autoloads file %s does not exist" buffer-file-name))
422 (or (file-writable-p buffer-file-name)
423 (error "Autoloads file %s is not writable"
424 buffer-file-name))))
426 ;;;_ . elinstall-update-deffile
428 ;;Adapted from autoload.el `update-directory-autoloads'.
430 (defun elinstall-update-deffile (target actions &optional
431 use-load-path force)
433 Update file TARGET with current autoloads as specified by ACTIONS.
434 Also remove any old definitions pointing to libraries that can no
435 longer be found.
437 ACTIONS must be a list of actions (See the format doc). Each one's
438 filename must be relative to some element of load-path.
440 USE-LOAD-PATH is a list to use as load-path. It should include
441 any new load-path that we are arranging to create. If it's not given,
442 load-path itself is used.
444 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
445 Other non-nil cases of FORCE are reserved for future development.
447 This uses `update-file-autoloads' (which see) to do its work.
448 In an interactive call, you must give one argument, the name
449 of a single directory."
450 (let
452 (use-load-path (or use-load-path load-path))
453 (this-time (current-time))
454 ;;files with no autoload cookies.
455 (no-autoloads nil))
457 (elinstall-prepare-deffile target)
458 (with-current-buffer
459 (find-file-noselect target)
460 (save-excursion
461 (setq actions
462 (elinstall-remove-autogen-action
463 (autoload-trim-file-name target)
464 actions))
466 (goto-char (point-min))
467 (while (search-forward generate-autoload-section-header nil t)
468 (let* ((form (autoload-read-section-header))
469 (file (nth 3 form)))
470 (cond ((and (consp file) (stringp (car file)))
471 ;; This is a list of files that have no
472 ;; autoload cookies.
473 ;; There shouldn't be more than one such entry.
474 ;; Remove the obsolete section.
475 (autoload-remove-section (match-beginning 0))
476 (let ((last-time (nth 4 form)))
477 (dolist (file file)
478 (let ((file-time (nth 5 (file-attributes file))))
479 (when (and file-time
480 (not (time-less-p last-time file-time)))
481 ;; file unchanged
482 (push file no-autoloads)
483 (setq actions
484 (elinstall-remove-autogen-action
485 file actions)))))))
486 ((not (stringp file)))
488 (let
489 ((file-path
490 (locate-library file nil use-load-path)))
491 (cond
492 ;;File doesn't exist, so remove its
493 ;;section.
494 ((not file-path)
495 (autoload-remove-section
496 (match-beginning 0)))
498 ;; File hasn't changed, so do nothing.
499 ((equal
500 (nth 4 form)
501 (nth 5 (file-attributes file-path)))
502 nil)
504 (elinstall-deffile-insert
505 (elinstall-get-autogen-action
506 file actions))))
508 (setq actions
509 (elinstall-remove-autogen-action
510 file actions))))))))
512 ;; Remaining actions have no existing autoload sections yet.
513 (setq no-autoloads
514 (append no-autoloads
515 (delq nil (mapcar #'elinstall-deffile-insert actions))))
516 (when no-autoloads
517 ;; Sort them for better readability.
518 (setq no-autoloads (sort no-autoloads 'string<))
519 ;; Add the `no-autoloads' section.
520 (goto-char (point-max))
521 (search-backward "\f" nil t)
522 (autoload-insert-section-header
523 (current-buffer) nil nil no-autoloads this-time)
524 (insert generate-autoload-section-trailer))
525 (save-buffer))))
528 ;;;_ , Doing actions to arrange preloads
529 ;;;_ . elinstall-insert-add-to-path
530 (defun elinstall-insert-add-to-path (new path-sym)
531 "Insert code to add NEW to PATH-SYM.
532 Insert this at point in current buffer."
533 (insert "\n")
535 `(add-to-list ',path-sym
536 (expand-file-name ,new
537 (if load-file-name
538 (file-name-directory
539 (file-truename load-file-name)))))
540 (current-buffer)))
542 ;;;_ . elinstall-insert-add-to-load-path
543 (defun elinstall-insert-add-to-load-path (new)
544 "Insert code to add NEW to load-path.
545 Insert this at point in current buffer."
546 (elinstall-insert-add-to-path new 'load-path))
548 ;;;_ . elinstall-insert-add-to-info-path
549 (defun elinstall-insert-add-to-info-path (new)
550 "Insert code to add NEW to info-path.
551 Insert this at point in current buffer."
552 (elinstall-insert-add-to-path new 'Info-default-directory-list))
554 ;;;_ . elinstall-symlink-on-emacs-start
555 (defun elinstall-symlink-on-emacs-start
556 (filename target-basename target-dir &optional priority force)
557 "Symlink to TARGET-BASENAME.el in TARGET-DIR
559 If PRIORITY is given, it will be used as the priority prefix,
560 otherwise elinstall-default-priority will be.
561 PRIORITY must be an integer or nil.
562 If FORCE is `t', do it regardless of timestamps etc.
563 Other non-nil cases of FORCE are reserved for future development."
564 (let*
566 (priority (or priority elinstall-default-priority))
567 (target-name-nodir
568 (format
569 "%d%s.el"
570 priority
571 target-basename))
572 (target
573 (expand-file-name target-name-nodir target-dir)))
576 (cond
577 ;;Path should already exist.
578 ((not
579 (file-exists-p target-dir))
580 (message "The target directory doesn't exist."))
581 ;;Target shouldn't already exist, but if force is given, let
582 ;;user override.
583 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
584 ;;do nothing even on force.
585 ((and
586 (file-exists-p target)
588 (not force)
589 (not
590 (yes-or-no-p
591 (format "Really overwrite %s? " project-name))))
592 (message "File %s already exists" target)))
595 (make-symbolic-link
596 filename
597 target
598 nil)))))
600 ;;;_ . elinstall-add-to-dot-emacs
601 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
602 "Add code to load FILENAME to .emacs.
603 FILENAME should not have an extension"
605 ;;Visit .emacs
606 (with-current-buffer (find-file-noselect dot-emacs-name)
607 (save-excursion
608 ;;add at end of file
609 (goto-char (point-max))
610 (insert "\n;;Added by elinstall")
611 (insert "\n;;Consider using my-site-start to manage .emacs\n")
612 (pp `(load ,filename) (current-buffer))
613 (save-buffer))))
616 ;;;_ . elinstall-arrange-preload
617 ;;;###autoload
618 (defun elinstall-arrange-preload (force filename basename &optional priority)
619 "Arrange for FILENAME to be loaded on emacs start.
620 FORCE has its usual meaning.
621 BASENAME and PRIORITY are used as arguments to
622 `elinstall-symlink-on-emacs-start'.
625 (let
626 ((preload-target elinstall-default-preload-target))
628 ;;Dispatch the possibilities.
629 (cond
630 ((eq preload-target 'dot-emacs)
631 (elinstall-add-to-dot-emacs "~/.emacs" filename))
632 ((stringp preload-target)
633 (elinstall-symlink-on-emacs-start
634 filename basename preload-target priority force))
635 (null preload-target
636 (message "Not arranging for preloads"))
638 (message "I don't recognize that")))))
640 ;;;_ , Cleanup actions
641 ;;Nothing yet. This will be another type of action.
643 ;;;_ . Segregating actions
644 ;;;_ , elinstall-remove-empty-segs
645 (defun elinstall-remove-empty-segs (segment-list)
646 "Return SEGMENT-LIST minus any segments that have no actions"
647 (delq nil
648 (mapcar
649 #'(lambda (segment)
650 (if (cdr segment)
651 segment
652 nil))
653 segment-list)))
655 ;;;_ , elinstall-segregate-actions
656 (defun elinstall-segregate-actions (actions)
657 "Return actions segregated by deffile.
659 Returns a list whose elements are each a cons of:
660 * deffile filename or nil
661 * A list of actions to be done for that deffile."
663 ;;$$IMPROVE ME - put tests in a separate segment, and byte-compile
664 ;;in yet another one.
665 (let
666 ((segment-list '())
667 (last-segment (list nil)))
669 (dolist (act actions)
670 (when act
671 (let
672 ((cell
673 (case (car act)
674 ((add-file-autoloads
675 add-to-info-path
676 add-to-load-path)
677 (let*
678 ((deffile-name (second act))
679 (cell-already
680 (assoc deffile-name segment-list)))
681 (if cell-already
682 cell-already
683 (let
684 ((new-cell (list deffile-name)))
685 (setq segment-list
686 (cons
687 new-cell
688 segment-list))
689 new-cell))))
690 ((preload-file run-tests byte-compile)
691 last-segment))))
693 (unless cell "Error: Couldn't find segment")
694 (setcdr cell (cons act (cdr cell))))))
696 '(elinstall-remove-empty-segs
697 (append
698 segment-list
699 (if (cdr last-segment)
700 (list last-segment)
701 '())))
702 (cons
703 (elinstall-remove-empty-segs
704 segment-list)
705 (if (cdr last-segment)
706 (list last-segment)
707 '())
714 ;;;_ . Finding actions
715 ;;;_ , Treating the parameter list
716 ;;;_ . elinstall-add-parameter
717 (defun elinstall-add-parameter (alist key new-value)
718 "Add a new value for KEY to ALIST"
720 (cons
721 (cons key new-value)
722 (assq-delete-all key (copy-list alist))))
724 ;;;_ . elinstall-get-parameter
725 (defun elinstall-get-parameter (alist key)
726 "Get the value of KEY from ALIST"
728 (cdr (assq key alist)))
729 ;;;_ . elinstall-expand-file-name
730 ;;$$OBSOLETE
732 (defun elinstall-expand-file-name (filename alist)
733 "Expand FILENAME by the value of `path' in ALIST"
734 (expand-file-name
735 filename
736 (elinstall-get-parameter alist 'path)))
737 ;;;_ , Finding deffiles
738 ;;;_ . elinstall-expand-deffile-name
739 (defun elinstall-expand-deffile-name (deffile)
740 "Expand DEFFILE autoload.el's way."
742 (expand-file-name (or deffile "loaddefs.el")
743 (expand-file-name "lisp"
744 source-directory)))
746 ;;;_ . elinstall-maybe-get-deffile
747 (defun elinstall-maybe-get-deffile (file)
748 "If FILE defined `generated-autoload-file', return it.
749 Otherwise return nil.
750 Return it as an absolute filename."
752 (save-excursion
753 ;;$$FIXME load buffer if it's not already loaded
754 (let*
755 ((existing-buffer (get-file-buffer file)))
757 ;; We want to get a value for generated-autoload-file from
758 ;; the local variables section if it's there.
759 ;;But if it's not loaded, we don't? Maybe should use
760 ;; `autoload-find-file' and load it.
761 (if existing-buffer
762 (set-buffer existing-buffer))
763 (if (local-variable-p 'generated-autoload-file)
764 (elinstall-expand-deffile-name
765 generated-autoload-file)
766 nil))))
769 ;;;_ , Workers
770 ;;;_ . elinstall-find-actions-for-file
771 (defun elinstall-find-actions-for-file
772 (filename load-path-element dir parameters)
773 "Return a list of actions to do for FILENAME.
774 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
775 `elinstall-find-actions-by-spec' "
777 (let
778 ((full-path
779 (expand-file-name filename dir)))
780 (list
781 `(add-file-autoloads
782 ,(elinstall-get-parameter
783 parameters 'def-file)
784 ;;load-name relative to a member of load-path
785 ,(file-name-sans-extension
786 (file-relative-name
787 full-path
788 load-path-element))
789 ,load-path-element ;;Is this still used?
790 ,full-path))))
792 ;;;_ . elinstall-find-actions-by-spec
794 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
795 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
797 LOAD-PATH-ELEMENT is the conceptual element of load-path that
798 surrounds DIR. It may not yet have been added to load-path."
799 (if (consp spec)
800 ;;$$IMPROVE ME by adding the other cases in the design.
801 (case (car spec)
803 (let
804 ((new-path
805 (expand-file-name
806 (second spec)
807 dir)))
809 (elinstall-find-actions-by-spec
810 (third spec)
811 load-path-element
812 new-path
813 parameters)))
815 (all
816 (apply #'nconc
817 (mapcar
818 #'(lambda (sub-spec)
819 (elinstall-find-actions-by-spec
820 sub-spec
821 load-path-element
823 parameters))
824 (cdr spec))))
826 (file
827 (elinstall-find-actions-for-file
828 filename load-path-element dir parameters))
830 (dir
831 (let*
832 ((dirname
833 (expand-file-name
834 (second spec)
835 dir))
836 (load-path-here
837 (not
838 (elinstall-get-parameter
839 parameters 'block-add-to-load-path)))
840 (load-path-element
841 (if load-path-here
842 dirname
843 load-path-element)))
845 (cons
846 ;;$$IMPROVE ME
847 ;;Do this only if there are loadable files.
848 (if load-path-here
849 `(add-to-load-path
850 ,(elinstall-get-parameter
851 parameters 'def-file)
852 ,load-path-element)
853 '())
854 ;;$$IMPROVE ME
855 ;;Do add-to-info-path too. But test if there are
856 ;;any info files present.
858 ;;$$IMPROVE ME
859 ;; We want to get a value for generated-autoload-file
860 ;; from the local variables section if it's there.
861 ;;Use `elinstall-maybe-get-deffile'
862 ;; Otherwise we'll use `def-file' in parameters.
864 ;;$$FIXME This isn't quite right. If directory
865 ;;itself is not in load-path, this will be wrong.
866 ;;Gotta know where our encompassing part of
867 ;;load-path is.
869 ;;$$ENCAP ME This should be shared with the
870 ;;treatment of (file FN)
872 ;;$$FIXME Don't do directories, but maybe recurse on
873 ;;them, if a flag is set. And since we definitely
874 ;;have a load-path element here,
875 ;;'block-add-to-load-path according to a parameter.
876 ;;Maybe could follow/not symlinks similarly.
877 (apply #'nconc
878 (mapcar
879 #'(lambda (filename)
880 (elinstall-find-actions-for-file
881 filename
882 load-path-element
883 dirname
884 parameters))
886 (directory-files
887 dirname
888 nil ;;Relative filenames
889 elinstall-elisp-regexp))))))
891 (def-file
892 (let
893 ((new-def-file
894 (expand-file-name
895 (second spec)
896 dir))
897 (for-preload (third spec)))
898 (assert (listp for-preload))
899 (append
900 (list
902 (and for-preload (car for-preload))
903 `(preload-file
904 ,(car for-preload)
905 ,new-def-file
906 ,@(cdr for-preload))
907 '()))
909 (elinstall-find-actions-by-spec
910 (fourth spec)
911 load-path-element
913 (elinstall-add-parameter parameters
914 'def-file new-def-file))))))
916 ;;$$IMPROVE ME by adding the other cases in the design.
917 (case spec
918 (t))))
919 ;;;_ . high-level work
920 ;;;_ , elinstall-get-relevant-load-path
921 (defun elinstall-get-relevant-load-path (actions)
923 (delq nil
924 (mapcar
925 #'(lambda (act)
926 (case (car act)
927 (add-to-load-path
928 (second act))
929 (t nil)))
930 actions)))
931 ;;;_ , elinstall-stage-update-deffiles
932 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
934 (mapcar
935 #'(lambda (segment)
936 (let*
937 ((deffile (car segment)))
938 (if (stringp deffile)
939 (elinstall-update-deffile deffile (cdr segment) force
940 use-load-path))))
941 segment-list))
943 ;;;_ , elinstall-do-segment
944 ;;$$OBSOLESCENT
945 (defun elinstall-do-segment (segment force use-load-path)
946 "Do all the actions in SEGMENT.
947 FORCE has its usual meaning.
948 USE-LOAD-PATH is the effective load-path."
950 (let*
951 ((deffile (car segment)))
952 (if (stringp deffile)
953 (elinstall-update-deffile deffile (cdr segment) force
954 use-load-path)
955 ;;Do other actions: link-in actions and cleanups.
956 (mapcar
957 #'(lambda (act)
959 (case (car act)
960 (preload-file
961 (let
962 ((proceed (car act)))
963 (apply
964 #'elinstall-arrange-preload
965 force
966 (cddr act))))
967 (run-tests
968 ;;$$WRITE ME - not a high priority right now.
969 nil)
971 ;;$$IMPROVE ME Understand flags to control second
972 ;;argument (whether to load file after
973 ;;compilation)
974 (byte-compile
975 (byte-compile-file (second act)))))
977 (cdr segment)))))
980 ;;;_ . Overall work
981 ;;;_ , elinstall-x
982 (defun elinstall-x (dir spec &optional force)
984 (let*
986 ;;This is just the default deffile, spec can override it.
987 (def-file
988 (elinstall-expand-deffile-name nil))
989 (actions
990 (elinstall-find-actions-by-spec
991 spec
995 (def-file . ,def-file ))))
996 (stages (elinstall-segregate-actions actions))
997 (use-load-path
998 (elinstall-get-relevant-load-path
999 actions)))
1001 '(elinstall-stage-update-deffiles
1002 (car stages)
1004 (mapcar
1005 #'(lambda (segment)
1006 (elinstall-do-segment segment force use-load-path))
1007 stages)
1010 ;;;_ . Entry points
1011 ;;;_ , elinstall
1012 ;;;###autoload
1013 (defun elinstall (project-name path spec &optional force)
1014 "Install elisp files.
1015 They need not be a formal package.
1017 Parameters:
1019 PROJECT-NAME - the name of the project
1021 PATH - Path to the project.
1022 Suggestion: (elinstall-directory-true-name)
1024 SPEC - a spec for the autoloads etc to make. It can be as simple as
1025 \(dir \"\.\") for installing one directory.
1027 If FORCE is t, install a package even if it has already been
1028 installed. Other non-nil cases of FORCE are reserved for future
1029 development."
1031 (when
1032 (and
1033 (or
1034 force
1035 (not (elinstall-already-installed project-name)))
1036 (yes-or-no-p (format "Re-install %s? " project-name)))
1037 (elinstall-x
1038 path
1039 `(def-file "loaddefs.el" (t ,project-name) ,spec)
1040 force)
1041 (elinstall-record-installed project-name)))
1045 ;;;_ , elinstall-update-directory-autoloads
1046 ;;$$TEST ME
1047 ;;;###autoload
1048 (defun elinstall-update-directory-autoloads (dir)
1051 (interactive "DInstall all elisp files from directory: ")
1054 (let
1055 ((def-file-name
1056 (elinstall-expand-deffile-name
1057 generated-autoload-file)))
1059 (elinstall-x
1061 `(def-file ,def-file-name (nil) (dir "."))
1066 ;;;_ , elinstall-update-file-autoloads
1067 ;;$$TEST ME
1068 ;;;###autoload
1069 (defun elinstall-update-file-autoloads (file)
1072 (interactive "fInstall elisp file: ")
1073 (let
1074 ((def-file-name
1076 (elinstall-maybe-get-deffile file)
1077 (elinstall-expand-deffile-name
1078 generated-autoload-file))))
1079 (elinstall
1080 file
1081 `(def-file ,def-file-name (nil) (file ,file)))))
1083 ;;;_. Footers
1084 ;;;_ , Provides
1086 (provide 'elinstall)
1088 ;;;_ * Local emacs vars.
1089 ;;;_ + Local variables:
1090 ;;;_ + mode: allout
1091 ;;;_ + End:
1093 ;;;_ , End
1094 ;;; elinstall.el ends here