Bugfix: t case needed parentheses
[elinstall.git] / elinstall.el
blob168da6d81a5eb5225cd1981a98756968389dbd6e
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." )
74 ;;;_ , Types
75 ;;;_ . elinstall-stages
76 (defstruct (elinstall-stages
77 (:constructor elinstall-make-stages)
78 (:conc-name elinstall-stages->)
79 (:copier nil))
80 "The elinstall stages"
81 build-deffiles
82 run-tests
83 byte-compile
84 arrange-preloads)
85 ;;;_ , Data
86 ;;;_ . Regular expressions
87 ;;;_ , elinstall-elisp-regexp
88 (defconst elinstall-elisp-regexp
89 (let ((tmp nil))
90 (dolist
91 (suf (get-load-suffixes))
92 (unless (string-match "\\.elc" suf) (push suf tmp)))
93 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
94 "Regular expression that matches elisp files" )
96 ;;;_ , Utilities
97 ;;;_ . elinstall-directory-true-name
98 (defun elinstall-directory-true-name ()
99 "Get the true name of the directory the calling code lives in.
100 CAUTION: This is sensitive to where it's called. That's the point of it."
101 (file-name-directory
102 (if load-file-name
103 (file-truename load-file-name)
104 (file-truename buffer-file-name))))
105 ;;;_ . Checking installedness
106 ;;;_ , elinstall-already-installed
107 (defun elinstall-already-installed (project-name)
108 "Return non-nil if PROJECT-NAME has been installed."
109 (member project-name elinstall-already-installed))
111 ;;;_ , elinstall-record-installed
112 (defun elinstall-record-installed (project-name)
113 "Record that PROJECT-NAME has been installed."
115 (add-to-list 'elinstall-already-installed project-name)
116 (customize-save-variable
117 'elinstall-already-installed
118 elinstall-already-installed
119 "Set by elinstall-record-installed"))
120 ;;;_ , Work
121 ;;;_ . Doing actions
123 ;;;_ , Doing autoload actions (adapted from autoload.el)
124 ;;;_ . Utilities about the action list
125 ;;;_ , elinstall-remove-autogen-action
126 (defun elinstall-remove-autogen-action (file actions)
127 "Return ACTIONS minus any add-file-autoloads on FILE removed."
129 (delq nil
130 (mapcar
131 #'(lambda (act)
132 (case (car act)
133 (add-file-autoloads
134 (if (equal file (third act))
136 act))
137 (t act)))
138 actions)))
139 ;;;_ , elinstall-get-autogen-action
140 (defun elinstall-get-autogen-action (file actions)
142 (let
143 ((the-act))
144 (dolist (act actions)
145 (case (car act)
146 (add-file-autoloads
147 (when (equal file (third act))
148 (setq the-act act)))))
149 the-act))
151 ;;;_ . Making autoloads
152 ;;;_ , elinstall-generate-file-autoloads
153 ;;override to allow slashed load-paths
154 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
155 ;;a new arg.
156 ;;`relative-to' can be:
157 ;; * nil: act as at present. Assume that FILE's immediate directory
158 ;;is in load-path.
159 ;; * t :: use default-directory
160 ;; * a string :: relative to it, as a filename
162 (defun elinstall-generate-file-autoloads (relative-name full-name)
163 "Insert at point a loaddefs autoload section for FILE.
164 Autoloads are generated for defuns and defmacros in FILE
165 marked by `generate-autoload-cookie' (which see).
166 If FILE is being visited in a buffer, the contents of the buffer
167 are used.
168 Return non-nil in the case where no autoloads were added at point.
170 FULL-NAME is the absolute name of the file.
171 RELATIVE-NAME is its name respective to some component of load-path."
172 (let* ((outbuf (current-buffer))
173 (autoloads-done '())
174 (print-length nil)
175 (print-readably t) ; This does something in Lucid Emacs.
176 (float-output-format nil)
177 (done-any nil)
178 (visited (get-file-buffer full-name))
179 (source-buf
180 (or visited
181 ;; It is faster to avoid visiting the file.
182 (ignore-errors (autoload-find-file full-name))))
183 output-start)
184 (if source-buf
185 (with-current-buffer source-buf
186 ;;$$MOVE ME - this should be checked in action-finding.
187 ;; Obey the no-update-autoloads file local variable.
188 (unless no-update-autoloads
189 (message "Generating autoloads for %s..." relative-name)
190 (setq output-start (with-current-buffer outbuf (point)))
191 (save-excursion
192 (save-restriction
193 (widen)
194 (goto-char (point-min))
195 (while (not (eobp))
196 (skip-chars-forward " \t\n\f")
197 (cond
198 ((looking-at (regexp-quote generate-autoload-cookie))
199 (search-forward generate-autoload-cookie)
200 (skip-chars-forward " \t")
201 (setq done-any t)
202 (if (eolp)
203 ;; Read the next form and make an autoload.
204 (let* ((form (prog1 (read (current-buffer))
205 (or (bolp) (forward-line 1))))
206 (autoload
207 (make-autoload form relative-name)))
208 (if autoload
209 (push (nth 1 form) autoloads-done)
210 (setq autoload form))
211 (let ((autoload-print-form-outbuf outbuf))
212 (autoload-print-form autoload)))
214 ;; Copy the rest of the line to the output.
215 (princ (buffer-substring
216 (progn
217 ;; Back up over whitespace, to preserve it.
218 (skip-chars-backward " \f\t")
219 (if (= (char-after (1+ (point))) ? )
220 ;; Eat one space.
221 (forward-char 1))
222 (point))
223 (progn (forward-line 1) (point)))
224 outbuf)))
225 ((looking-at ";")
226 ;; Don't read the comment.
227 (forward-line 1))
229 (forward-sexp 1)
230 (forward-line 1))))))
232 (when done-any
233 (with-current-buffer outbuf
234 (save-excursion
235 ;; Insert the section-header line which lists the file name
236 ;; and which functions are in it, etc.
237 (goto-char output-start)
238 (autoload-insert-section-header
239 outbuf autoloads-done relative-name full-name
240 (nth 5 (file-attributes full-name)))
241 (insert ";;; Generated autoloads from "
242 (autoload-trim-file-name full-name) "\n"))
243 (insert generate-autoload-section-trailer)))
244 (message "Generating autoloads for %s...done" relative-name))
246 (unless visited
247 ;; We created this buffer, so we should kill it.
248 (kill-buffer (current-buffer))))
249 (message "Could not load %s" relative-name))
251 (not done-any)))
254 ;;;_ , elinstall-deffile-insert-autoloads
255 (defun elinstall-deffile-insert-autoloads (file load-name)
256 "Update the autoloads for FILE in current buffer.
257 Return FILE if there was no autoload cookie in it, else nil.
259 Current buffer must be a loaddef-style file.
261 LOAD-NAME is the absolute name of the file.
262 RELATIVE-NAME is its name respective to some component of load-path."
263 (let (
264 (found nil)
265 (no-autoloads nil))
267 (save-excursion
268 (save-restriction
269 (widen)
270 (goto-char (point-min))
271 ;; Look for the section for FILE
272 (while (and (not found)
273 (search-forward generate-autoload-section-header nil t))
274 (let ((form (autoload-read-section-header)))
275 (cond
276 ((equal (nth 2 form) file)
277 ;; We found the section for this file.
278 (let ((begin (match-beginning 0)))
279 (progn
280 (search-forward generate-autoload-section-trailer)
281 (delete-region begin (point))
282 (setq found t))))
283 ((string< file (nth 2 form))
284 ;; We've come to a section alphabetically later than
285 ;; FILE. We assume the file is in order and so
286 ;; there must be no section for FILE. We will
287 ;; insert one before the section here.
288 (goto-char (match-beginning 0))
289 (setq found 'new)))))
290 (unless found
291 (progn
292 (setq found 'new)
293 ;; No later sections in the file. Put before the last page.
294 (goto-char (point-max))
295 (search-backward "\f" nil t)))
296 (setq no-autoloads
297 (elinstall-generate-file-autoloads file load-name))))
299 (if no-autoloads file nil)))
300 ;;;_ . Arranging to add to info-path and load-path
301 ;;;_ , elinstall-generate-add-to-path
302 (defun elinstall-generate-add-to-path (path-element type)
303 "Insert code at point to add PATH-ELEMENT to a path.
304 If TYPE is:
305 * `add-to-load-path', add to load-path
306 * `add-to-info-path', add to Info-default-directory-list
308 Current buffer must be a loaddef-style file."
309 (let ( (path-symbol
310 (case type
311 (add-to-load-path 'load-path)
312 (add-to-info-path 'Info-default-directory-list)))
313 (description
314 (case type
315 (add-to-load-path "load-path")
316 (add-to-info-path "info-path")))
317 (autoloads-done '())
318 (print-length nil)
319 (print-readably t) ; This does something in Lucid Emacs.
320 (float-output-format nil))
322 (message "Generating %s additions..." description)
324 (autoload-insert-section-header
325 (current-buffer) (list path-element) nil nil
326 nil)
327 (insert ";;; Generated path addition\n")
329 `(add-to-list ',path-symbol
330 (expand-file-name
331 ,(file-relative-name path-element)
332 (if load-file-name
333 (file-name-directory
334 (file-truename load-file-name)))))
335 (current-buffer))
337 (insert generate-autoload-section-trailer)
338 (message "Generating %s additions...done" description)))
341 ;;;_ , elinstall-deffile-insert-add-to-path
342 (defun elinstall-deffile-insert-add-to-path (path-element type)
343 "Insert code in current buffer to add PATH-ELEMENT to a path.
344 If TYPE is:
345 * `add-to-load-path', add to load-path
346 * `add-to-info-path', add to Info-default-directory-list
348 Current buffer must be a loaddef-style file."
349 (let (
350 (found nil)
351 (no-autoloads nil))
353 (save-excursion
354 (save-restriction
355 (widen)
356 (goto-char (point-min))
357 ;; Look for the section for PATH-ELEMENT
358 (while (and (not found)
359 (search-forward generate-autoload-section-header nil t))
360 (let ((form (autoload-read-section-header)))
361 (cond
362 ((and
363 (equal (nth 0 form) type)
364 (member (nth 1 form) path-element))
366 ;; We found the section for this add.
367 (let ((begin (match-beginning 0)))
368 (progn
369 (search-forward generate-autoload-section-trailer)
370 (delete-region begin (point))
371 (setq found t)))))))
373 (unless found
374 (progn
375 (setq found 'new)
376 ;; No later sections in the file. Put before the last page.
377 (goto-char (point-max))
378 (search-backward "\f" nil t)))
380 (elinstall-generate-add-to-path path-element type)))
382 ;;This never belongs in the no-autoloads section.
383 nil))
384 ;;;_ . elinstall-deffile-insert
386 (defun elinstall-deffile-insert (action)
387 "Insert autoloads etc into current file according to ACTION.
388 The format of ACTION is described in the design docs.
390 Return filename if this action belongs in the no-autoload section."
392 (when action
393 (case (car action)
394 (add-file-autoloads
395 (elinstall-deffile-insert-autoloads
396 (third action)
397 (fifth action)))
399 (add-to-load-path
400 (elinstall-deffile-insert-add-to-path
401 (third action)
402 'add-to-load-path)
403 nil)
405 (add-to-info-path
406 (elinstall-deffile-insert-add-to-path
407 (third action)
408 'add-to-info-path)
409 nil)
411 ((preload-file run-tests byte-compile)
412 (error "This case should not come here.")))))
414 ;;;_ . elinstall-prepare-deffile
415 (defun elinstall-prepare-deffile (deffile)
416 "Try to ensure that DEFFILE is available for receiving autoloads"
418 (autoload-ensure-default-file deffile)
419 (with-current-buffer (find-file-noselect deffile)
422 ;; We must read/write the file without any code conversion,
423 ;; but still decode EOLs.
424 (let ((coding-system-for-read 'raw-text))
426 ;; This is to make generated-autoload-file have Unix EOLs, so
427 ;; that it is portable to all platforms.
428 (setq buffer-file-coding-system 'raw-text-unix))
429 (or (> (buffer-size) 0)
430 (error "Autoloads file %s does not exist" buffer-file-name))
431 (or (file-writable-p buffer-file-name)
432 (error "Autoloads file %s is not writable"
433 buffer-file-name))))
435 ;;;_ . elinstall-update-deffile
437 ;;Adapted from autoload.el `update-directory-autoloads'.
439 (defun elinstall-update-deffile (target actions &optional
440 use-load-path force)
442 Update file TARGET with current autoloads as specified by ACTIONS.
443 Also remove any old definitions pointing to libraries that can no
444 longer be found.
446 ACTIONS must be a list of actions (See the format doc). Each one's
447 filename must be relative to some element of load-path.
449 USE-LOAD-PATH is a list to use as load-path. It should include
450 any new load-path that we are arranging to create. If it's not given,
451 load-path itself is used.
453 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
454 Other non-nil cases of FORCE are reserved for future development.
456 This uses `update-file-autoloads' (which see) to do its work.
457 In an interactive call, you must give one argument, the name
458 of a single directory."
459 (let
461 (use-load-path (or use-load-path load-path))
462 (this-time (current-time))
463 ;;files with no autoload cookies.
464 (no-autoloads nil))
466 (elinstall-prepare-deffile target)
467 (with-current-buffer
468 (find-file-noselect target)
469 (save-excursion
470 (setq actions
471 (elinstall-remove-autogen-action
472 (autoload-trim-file-name target)
473 actions))
475 (goto-char (point-min))
476 (while (search-forward generate-autoload-section-header nil t)
477 (let* ((form (autoload-read-section-header))
478 (file (nth 3 form)))
479 (cond ((and (consp file) (stringp (car file)))
480 ;; This is a list of files that have no
481 ;; autoload cookies.
482 ;; There shouldn't be more than one such entry.
483 ;; Remove the obsolete section.
484 (autoload-remove-section (match-beginning 0))
485 (let ((last-time (nth 4 form)))
486 (dolist (file file)
487 (let ((file-time (nth 5 (file-attributes file))))
488 (when (and file-time
489 (not (time-less-p last-time file-time)))
490 ;; file unchanged
491 (push file no-autoloads)
492 (setq actions
493 (elinstall-remove-autogen-action
494 file actions)))))))
495 ((not (stringp file)))
497 (let
498 ((file-path
499 (locate-library file nil use-load-path)))
500 (cond
501 ;;File doesn't exist, so remove its
502 ;;section.
503 ((not file-path)
504 (autoload-remove-section
505 (match-beginning 0)))
507 ;; File hasn't changed, so do nothing.
508 ((equal
509 (nth 4 form)
510 (nth 5 (file-attributes file-path)))
511 nil)
513 (elinstall-deffile-insert
514 (elinstall-get-autogen-action
515 file actions))))
517 (setq actions
518 (elinstall-remove-autogen-action
519 file actions))))))))
521 ;; Remaining actions have no existing autoload sections yet.
522 (setq no-autoloads
523 (append no-autoloads
524 (delq nil (mapcar #'elinstall-deffile-insert actions))))
525 (when no-autoloads
526 ;; Sort them for better readability.
527 (setq no-autoloads (sort no-autoloads 'string<))
528 ;; Add the `no-autoloads' section.
529 (goto-char (point-max))
530 (search-backward "\f" nil t)
531 (autoload-insert-section-header
532 (current-buffer) nil nil no-autoloads this-time)
533 (insert generate-autoload-section-trailer))
534 (save-buffer))))
536 ;;;_ . elinstall-stage-update-deffiles
537 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
538 "Update any deffiles mentioned in SEGMENT-LIST.
539 FORCE and USE-LOAD-PATH have the same meaning as in
540 `elinstall-update-deffile'.
542 (mapcar
543 #'(lambda (segment)
544 (let*
545 ((deffile (car segment)))
546 (if (stringp deffile)
547 (elinstall-update-deffile deffile (cdr segment) force
548 use-load-path))))
549 segment-list))
551 ;;;_ , Doing actions to arrange preloads
552 ;;;_ . elinstall-symlink-on-emacs-start
553 (defun elinstall-symlink-on-emacs-start
554 (filename target-basename target-dir &optional priority force)
555 "Symlink to TARGET-BASENAME.el in TARGET-DIR
557 If PRIORITY is given, it will be used as the priority prefix,
558 otherwise elinstall-default-priority will be.
559 PRIORITY must be an integer or nil.
560 If FORCE is `t', do it regardless of timestamps etc.
561 Other non-nil cases of FORCE are reserved for future development."
562 (let*
564 (priority (or priority elinstall-default-priority))
565 (target-name-nodir
566 (format
567 "%d%s.el"
568 priority
569 target-basename))
570 (target
571 (expand-file-name target-name-nodir target-dir)))
574 (cond
575 ;;Path should already exist.
576 ((not
577 (file-exists-p target-dir))
578 (message "The target directory doesn't exist."))
579 ;;Target shouldn't already exist, but if force is given, let
580 ;;user override.
581 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
582 ;;do nothing even on force.
583 ((and
584 (file-exists-p target)
586 (not force)
587 (not
588 (yes-or-no-p
589 (format "Really overwrite %s? " target))))
590 (message "File %s already exists" target)))
593 (make-symbolic-link
594 filename
595 target
596 nil)))))
598 ;;;_ . elinstall-add-to-dot-emacs
599 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
600 "Add code to load FILENAME to .emacs.
601 FILENAME should not have an extension"
603 ;;Visit .emacs
604 (with-current-buffer (find-file-noselect dot-emacs-name)
605 (save-excursion
606 ;;add at end of file
607 (goto-char (point-max))
608 (insert "\n;;Added by elinstall")
609 (insert "\n;;Consider using my-site-start to manage .emacs\n")
610 (pp `(load ,filename) (current-buffer))
611 (save-buffer))))
614 ;;;_ . elinstall-arrange-preload
615 ;;;###autoload
616 (defun elinstall-arrange-preload (force filename basename &optional priority)
617 "Arrange for FILENAME to be loaded on emacs start.
618 FORCE has its usual meaning.
619 BASENAME and PRIORITY are used as arguments to
620 `elinstall-symlink-on-emacs-start'.
623 (let
624 ((preload-target elinstall-default-preload-target))
626 ;;Dispatch the possibilities.
627 (cond
628 ((eq preload-target 'dot-emacs)
629 (elinstall-add-to-dot-emacs "~/.emacs" filename))
630 ((stringp preload-target)
631 (elinstall-symlink-on-emacs-start
632 filename basename preload-target priority force))
633 (null preload-target
634 (message "Not arranging for preloads"))
636 (message "I don't recognize that")))))
637 ;;;_ . elinstall-stage-arrange-preloads
638 (defun elinstall-stage-arrange-preloads (actions deffiles-used)
639 "Arrange any preloads mentioned in ACTIONS."
641 (mapcar
642 #'(lambda (act)
643 (case (car act)
644 (preload-file
645 (let*
646 ( (filename
647 (caddr act))
648 (proceed-p
649 (case (second act)
650 ((t) t)
651 ((nil) nil)
652 (if-used
653 (member filename deffiles-used)))))
655 (when proceed-p
656 (apply
657 #'elinstall-arrange-preload
658 force
659 (cddr act)))))
661 (error
662 "elinstall-stage-arrange-preloads: Action not
663 recognized."))) )
664 actions))
667 ;;;_ , Run tests
668 ;;;_ . elinstall-stage-run-tests
669 (defun elinstall-stage-run-tests (actions)
670 "Run any tests mentioned in ACTIONS."
672 (mapcar
673 #'(lambda (act)
674 (case (car act)
675 (run-tests
676 ;;$$WRITE ME - not a high priority right now.
677 nil)
679 (error
680 "elinstall-stage-run-tests: Action not
681 recognized."))) )
682 actions))
685 ;;;_ , Byte compile
686 ;;;_ . elinstall-stage-byte-compile
687 (defun elinstall-stage-byte-compile (actions)
688 "Do any byte-compilation mentioned in ACTIONS."
690 (mapcar
691 #'(lambda (act)
692 (case (car act)
693 ;;$$IMPROVE ME Understand flags to control second
694 ;;argument (whether to load file after
695 ;;compilation)
696 (byte-compile
697 (byte-compile-file (second act)))
699 (error
700 "elinstall-stage-byte-compile: Action not
701 recognized."))) )
702 actions))
704 ;;;_ . Segregating actions
705 ;;;_ , elinstall-remove-empty-segs
706 (defun elinstall-remove-empty-segs (segment-list)
707 "Return SEGMENT-LIST minus any segments that have no actions.
708 Intended only for the deffile stage data."
709 (delq nil
710 (mapcar
711 #'(lambda (segment)
712 (if (cdr segment)
713 segment
714 nil))
715 segment-list)))
717 ;;;_ , elinstall-segregate-actions
718 (defun elinstall-segregate-actions (actions)
719 "Return actions segregated by deffile.
721 Returns a list whose elements are each a cons of:
722 * deffile filename or nil
723 * A list of actions to be done for that deffile."
725 (let
727 (build-deffiles '())
728 (run-tests '())
729 (byte-compile '())
730 (arrange-preloads '()))
732 (dolist (act actions)
733 (when act
734 (case (car act)
735 ((add-file-autoloads
736 add-to-info-path
737 add-to-load-path)
738 (let*
739 ((deffile-name (second act))
740 (cell-already
741 (assoc deffile-name build-deffiles)))
742 (if cell-already
743 ;;There are already actions on this deffile.
744 ;;Splice this action in.
745 (setcdr cell-already
746 (cons act (cdr cell-already)))
747 ;;There are no actions on this deffile. Add a
748 ;;place for them and include this action.
749 (push (list deffile-name act) build-deffiles))))
750 (preload-file
751 (push act arrange-preloads))
752 (run-tests
753 (push act run-tests))
754 (byte-compile
755 (push act byte-compile)))))
757 (elinstall-make-stages
758 :build-deffiles
759 (elinstall-remove-empty-segs build-deffiles)
760 :run-tests
761 run-tests
762 :byte-compile
763 byte-compile
764 :arrange-preloads
765 arrange-preloads)))
771 ;;;_ . Finding actions
772 ;;;_ , Treating the parameter list
773 ;;;_ . elinstall-add-parameter
774 (defun elinstall-add-parameter (alist key new-value)
775 "Add a new value for KEY to ALIST"
777 (cons
778 (cons key new-value)
779 (assq-delete-all key (copy-list alist))))
781 ;;;_ . elinstall-get-parameter
782 (defun elinstall-get-parameter (alist key)
783 "Get the value of KEY from ALIST"
785 (cdr (assq key alist)))
786 ;;;_ . elinstall-expand-file-name
787 ;;$$OBSOLETE
789 (defun elinstall-expand-file-name (filename alist)
790 "Expand FILENAME by the value of `path' in ALIST"
791 (expand-file-name
792 filename
793 (elinstall-get-parameter alist 'path)))
794 ;;;_ , Finding deffiles
795 ;;;_ . elinstall-expand-deffile-name
796 (defun elinstall-expand-deffile-name (deffile)
797 "Expand DEFFILE autoload.el's way."
799 (expand-file-name (or deffile "loaddefs.el")
800 (expand-file-name "lisp"
801 source-directory)))
803 ;;;_ . elinstall-maybe-get-deffile
804 (defun elinstall-maybe-get-deffile (file)
805 "If FILE defined `generated-autoload-file', return it.
806 Otherwise return nil.
807 Return it as an absolute filename."
809 (save-excursion
810 ;;$$FIXME load buffer if it's not already loaded
811 (let*
812 ((existing-buffer (get-file-buffer file)))
814 ;; We want to get a value for generated-autoload-file from
815 ;; the local variables section if it's there.
816 ;;But if it's not loaded, we don't? Maybe should use
817 ;; `autoload-find-file' and load it.
818 (if existing-buffer
819 (set-buffer existing-buffer))
820 (if (local-variable-p 'generated-autoload-file)
821 (elinstall-expand-deffile-name
822 generated-autoload-file)
823 nil))))
826 ;;;_ , Workers
827 ;;;_ . elinstall-find-actions-for-file
828 (defun elinstall-find-actions-for-file
829 (filename load-path-element dir parameters)
830 "Return a list of actions to do for FILENAME.
831 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
832 `elinstall-find-actions-by-spec' "
834 (let
835 ((full-path
836 (expand-file-name filename dir)))
837 (list
838 `(add-file-autoloads
839 ,(elinstall-get-parameter
840 parameters 'def-file)
841 ;;load-name relative to a member of load-path
842 ,(file-name-sans-extension
843 (file-relative-name
844 full-path
845 load-path-element))
846 ,load-path-element ;;Is this still used?
847 ,full-path))))
849 ;;;_ . elinstall-find-actions-by-spec
851 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
852 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
854 LOAD-PATH-ELEMENT is the conceptual element of load-path that
855 surrounds DIR. It may not yet have been added to load-path."
856 (if (consp spec)
857 ;;$$IMPROVE ME by adding the other cases in the design.
858 (case (car spec)
860 (let
861 ((new-path
862 (expand-file-name
863 (second spec)
864 dir)))
866 (elinstall-find-actions-by-spec
867 (third spec)
868 load-path-element
869 new-path
870 parameters)))
872 (all
873 (apply #'nconc
874 (mapcar
875 #'(lambda (sub-spec)
876 (elinstall-find-actions-by-spec
877 sub-spec
878 load-path-element
880 parameters))
881 (cdr spec))))
883 (file
884 (elinstall-find-actions-for-file
885 filename load-path-element dir parameters))
887 (dir
888 (let*
889 ((dirname
890 (expand-file-name
891 (second spec)
892 dir))
893 (load-path-here
894 (not
895 (elinstall-get-parameter
896 parameters 'block-add-to-load-path)))
897 (load-path-element
898 (if load-path-here
899 dirname
900 load-path-element)))
902 (cons
903 ;;$$IMPROVE ME
904 ;;Do this only if there are loadable files.
905 (if load-path-here
906 `(add-to-load-path
907 ,(elinstall-get-parameter
908 parameters 'def-file)
909 ,load-path-element)
910 '())
911 ;;$$IMPROVE ME
912 ;;Do add-to-info-path too. But test if there are
913 ;;any info files present.
915 ;;$$IMPROVE ME
916 ;; We want to get a value for generated-autoload-file
917 ;; from the local variables section if it's there.
918 ;;Use `elinstall-maybe-get-deffile'
919 ;; Otherwise we'll use `def-file' in parameters.
921 ;;$$FIXME This isn't quite right. If directory
922 ;;itself is not in load-path, this will be wrong.
923 ;;Gotta know where our encompassing part of
924 ;;load-path is.
926 ;;$$ENCAP ME This should be shared with the
927 ;;treatment of (file FN)
929 ;;$$FIXME Don't do directories, but maybe recurse on
930 ;;them, if a flag is set. And since we definitely
931 ;;have a load-path element here,
932 ;;'block-add-to-load-path according to a parameter.
933 ;;Maybe could follow/not symlinks similarly.
934 (apply #'nconc
935 (mapcar
936 #'(lambda (filename)
937 (elinstall-find-actions-for-file
938 filename
939 load-path-element
940 dirname
941 parameters))
943 (directory-files
944 dirname
945 nil ;;Relative filenames
946 elinstall-elisp-regexp))))))
948 (def-file
949 (let
950 ((new-def-file
951 (expand-file-name
952 (second spec)
953 dir))
954 (for-preload (third spec)))
955 (assert (listp for-preload))
956 (append
957 (list
959 (and for-preload (car for-preload))
960 `(preload-file
961 ,(car for-preload)
962 ,new-def-file
963 ,@(cdr for-preload))
964 '()))
966 (elinstall-find-actions-by-spec
967 (fourth spec)
968 load-path-element
970 (elinstall-add-parameter parameters
971 'def-file new-def-file))))))
973 ;;$$IMPROVE ME by adding the other cases in the design.
974 (case spec
975 (t))))
976 ;;;_ . high-level work
977 ;;;_ , elinstall-get-relevant-load-path
978 (defun elinstall-get-relevant-load-path (actions)
980 (delq nil
981 (mapcar
982 #'(lambda (act)
983 (case (car act)
984 (add-to-load-path
985 (second act))
986 (t nil)))
987 actions)))
988 ;;;_ , elinstall-get-deffile-list
989 (defun elinstall-get-deffile-list (stages)
990 "Get a list of deffile names"
992 (mapcar
993 #'car
994 (elinstall-stages->build-deffiles stages)))
996 ;;;_ , elinstall-x
997 (defun elinstall-x (dir spec &optional force)
999 (let*
1001 ;;This is just the default deffile, spec can override it.
1002 (def-file
1003 (elinstall-expand-deffile-name nil))
1004 (actions
1005 (elinstall-find-actions-by-spec
1006 spec
1010 (def-file . ,def-file ))))
1011 (stages (elinstall-segregate-actions actions))
1012 (use-load-path
1013 (elinstall-get-relevant-load-path
1014 actions)))
1016 (elinstall-stage-update-deffiles
1017 (elinstall-stages->build-deffiles stages)
1018 force
1019 use-load-path)
1020 (elinstall-stage-arrange-preloads
1021 (elinstall-stages->arrange-preloads stages)
1022 (elinstall-get-deffile-list stages))
1025 ;;;_ . Entry points
1026 ;;;_ , elinstall
1027 ;;;###autoload
1028 (defun elinstall (project-name path spec &optional force)
1029 "Install elisp files.
1030 They need not be a formal package.
1032 Parameters:
1034 PROJECT-NAME - the name of the project
1036 PATH - Path to the project.
1037 Suggestion: (elinstall-directory-true-name)
1039 SPEC - a spec for the autoloads etc to make. It can be as simple as
1040 \(dir \"\.\") for installing one directory.
1042 If FORCE is t, install a package even if it has already been
1043 installed. Other non-nil cases of FORCE are reserved for future
1044 development."
1046 (when
1047 (and
1048 (or
1049 force
1050 (not (elinstall-already-installed project-name)))
1051 (yes-or-no-p (format "Re-install %s? " project-name)))
1052 (elinstall-x
1053 path
1054 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1055 force)
1056 (elinstall-record-installed project-name)))
1060 ;;;_ , elinstall-update-directory-autoloads
1061 ;;$$TEST ME
1062 ;;;###autoload
1063 (defun elinstall-update-directory-autoloads (dir)
1066 (interactive "DInstall all elisp files from directory: ")
1069 (let
1070 ((def-file-name
1071 (elinstall-expand-deffile-name
1072 generated-autoload-file)))
1074 (elinstall-x
1076 `(def-file ,def-file-name (nil) (dir ".")))))
1080 ;;;_ , elinstall-update-file-autoloads
1081 ;;$$TEST ME
1082 ;;;###autoload
1083 (defun elinstall-update-file-autoloads (file)
1086 (interactive "fInstall elisp file: ")
1087 (let
1088 ((def-file-name
1090 (elinstall-maybe-get-deffile file)
1091 (elinstall-expand-deffile-name
1092 generated-autoload-file))))
1093 (elinstall
1094 file
1095 `(def-file ,def-file-name (nil) (file ,file)))))
1097 ;;;_. Footers
1098 ;;;_ , Provides
1100 (provide 'elinstall)
1102 ;;;_ * Local emacs vars.
1103 ;;;_ + Local variables:
1104 ;;;_ + mode: allout
1105 ;;;_ + End:
1107 ;;;_ , End
1108 ;;; elinstall.el ends here