New function elinstall-insert-section-header, replacing autoload-insert-section-header
[elinstall.git] / elinstall.el
blob9454e08ac825db65ce8effe7a8b24f99b2c6f847
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))
150 ;;;_ . elinstall-insert-section-header
151 (defun elinstall-insert-section-header (outbuf form)
152 "Insert the section-header line,
153 which lists the file name and which functions are in it, etc."
154 (insert generate-autoload-section-header)
155 (prin1 form outbuf)
156 (terpri outbuf)
157 ;; Break that line at spaces, to avoid very long lines.
158 ;; Make each sub-line into a comment.
159 (with-current-buffer outbuf
160 (save-excursion
161 (forward-line -1)
162 (while (not (eolp))
163 (move-to-column 64)
164 (skip-chars-forward "^ \n")
165 (or (eolp)
166 (insert "\n" generate-autoload-section-continuation))))))
168 ;;;_ . Making autoloads
169 ;;;_ , elinstall-generate-file-autoloads
170 ;;override to allow slashed load-paths
171 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
172 ;;a new arg.
173 ;;`relative-to' can be:
174 ;; * nil: act as at present. Assume that FILE's immediate directory
175 ;;is in load-path.
176 ;; * t :: use default-directory
177 ;; * a string :: relative to it, as a filename
179 (defun elinstall-generate-file-autoloads (relative-name full-name)
180 "Insert at point a loaddefs autoload section for FILE.
181 Autoloads are generated for defuns and defmacros in FILE
182 marked by `generate-autoload-cookie' (which see).
183 If FILE is being visited in a buffer, the contents of the buffer
184 are used.
185 Return non-nil in the case where no autoloads were added at point.
187 FULL-NAME is the absolute name of the file.
188 RELATIVE-NAME is its name respective to some component of load-path."
189 (let* ((outbuf (current-buffer))
190 (autoloads-done '())
191 (print-length nil)
192 (print-readably t) ; This does something in Lucid Emacs.
193 (float-output-format nil)
194 (done-any nil)
195 (visited (get-file-buffer full-name))
196 (source-buf
197 (or visited
198 ;; It is faster to avoid visiting the file.
199 (ignore-errors (autoload-find-file full-name))))
200 output-start)
201 (if source-buf
202 (with-current-buffer source-buf
203 ;;$$MOVE ME - this should be checked in action-finding.
204 ;; Obey the no-update-autoloads file local variable.
205 (unless no-update-autoloads
206 (message "Generating autoloads for %s..." relative-name)
207 (setq output-start (with-current-buffer outbuf (point)))
208 (save-excursion
209 (save-restriction
210 (widen)
211 (goto-char (point-min))
212 (while (not (eobp))
213 (skip-chars-forward " \t\n\f")
214 (cond
215 ((looking-at (regexp-quote generate-autoload-cookie))
216 (search-forward generate-autoload-cookie)
217 (skip-chars-forward " \t")
218 (setq done-any t)
219 (if (eolp)
220 ;; Read the next form and make an autoload.
221 (let* ((form (prog1 (read (current-buffer))
222 (or (bolp) (forward-line 1))))
223 (autoload
224 (make-autoload form relative-name)))
225 (if autoload
226 (push (nth 1 form) autoloads-done)
227 (setq autoload form))
228 (let ((autoload-print-form-outbuf outbuf))
229 (autoload-print-form autoload)))
231 ;; Copy the rest of the line to the output.
232 (princ (buffer-substring
233 (progn
234 ;; Back up over whitespace, to preserve it.
235 (skip-chars-backward " \f\t")
236 (if (= (char-after (1+ (point))) ? )
237 ;; Eat one space.
238 (forward-char 1))
239 (point))
240 (progn (forward-line 1) (point)))
241 outbuf)))
242 ((looking-at ";")
243 ;; Don't read the comment.
244 (forward-line 1))
246 (forward-sexp 1)
247 (forward-line 1))))))
249 (when done-any
250 (with-current-buffer outbuf
251 (save-excursion
252 ;; Insert the section-header line which lists the file name
253 ;; and which functions are in it, etc.
254 (goto-char output-start)
255 (elinstall-insert-section-header
256 outbuf
257 (list 'autoloads
258 autoloads-done
259 relative-name
260 (autoload-trim-file-name full-name)
261 (nth 5 (file-attributes full-name))))
263 (insert ";;; Generated autoloads from "
264 (autoload-trim-file-name full-name) "\n"))
265 (insert generate-autoload-section-trailer)))
266 (message "Generating autoloads for %s...done" relative-name))
268 (unless visited
269 ;; We created this buffer, so we should kill it.
270 (kill-buffer (current-buffer))))
271 (message "Could not load %s" relative-name))
273 (not done-any)))
276 ;;;_ , elinstall-deffile-insert-autoloads
277 (defun elinstall-deffile-insert-autoloads (file load-name)
278 "Update the autoloads for FILE in current buffer.
279 Return FILE if there was no autoload cookie in it, else nil.
281 Current buffer must be a loaddef-style file.
283 LOAD-NAME is the absolute name of the file.
284 RELATIVE-NAME is its name respective to some component of load-path."
285 (let (
286 (found nil)
287 (no-autoloads nil))
289 (save-excursion
290 (save-restriction
291 (widen)
292 (goto-char (point-min))
293 ;; Look for the section for FILE
294 (while (and (not found)
295 (search-forward generate-autoload-section-header nil t))
296 (let ((form (autoload-read-section-header)))
297 (cond
298 ((equal (nth 2 form) file)
299 ;; We found the section for this file.
300 (let ((begin (match-beginning 0)))
301 (progn
302 (search-forward generate-autoload-section-trailer)
303 (delete-region begin (point))
304 (setq found t))))
305 ((string< file (nth 2 form))
306 ;; We've come to a section alphabetically later than
307 ;; FILE. We assume the file is in order and so
308 ;; there must be no section for FILE. We will
309 ;; insert one before the section here.
310 (goto-char (match-beginning 0))
311 (setq found 'new)))))
312 (unless found
313 (progn
314 (setq found 'new)
315 ;; No later sections in the file. Put before the last page.
316 (goto-char (point-max))
317 (search-backward "\f" nil t)))
318 (setq no-autoloads
319 (elinstall-generate-file-autoloads file load-name))))
321 (if no-autoloads file nil)))
322 ;;;_ . Arranging to add to info-path and load-path
323 ;;;_ , elinstall-generate-add-to-path
324 (defun elinstall-generate-add-to-path (path-element type)
325 "Insert code at point to add PATH-ELEMENT to a path.
326 If TYPE is:
327 * `add-to-load-path', add to load-path
328 * `add-to-info-path', add to Info-default-directory-list
330 Current buffer must be a loaddef-style file."
331 (let ( (path-symbol
332 (case type
333 (add-to-load-path 'load-path)
334 (add-to-info-path 'Info-default-directory-list)
335 (t (error "Type not recognized"))))
336 (description
337 (case type
338 (add-to-load-path "load-path")
339 (add-to-info-path "info-path")))
340 (autoloads-done '())
341 (print-length nil)
342 (print-readably t) ; This does something in Lucid Emacs.
343 (float-output-format nil))
345 (message "Generating %s additions..." description)
348 (elinstall-insert-section-header
349 (current-buffer)
350 (list type (list path-element) nil nil nil))
352 (insert ";;; Generated path addition\n")
354 `(add-to-list ',path-symbol
355 (expand-file-name
356 ,(file-relative-name path-element)
357 (if load-file-name
358 (file-name-directory
359 (file-truename load-file-name)))))
360 (current-buffer))
362 (insert generate-autoload-section-trailer)
363 (message "Generating %s additions...done" description)))
366 ;;;_ , elinstall-deffile-insert-add-to-path
367 (defun elinstall-deffile-insert-add-to-path (path-element type)
368 "Insert code in current buffer to add PATH-ELEMENT to a path.
369 If TYPE is:
370 * `add-to-load-path', add to load-path
371 * `add-to-info-path', add to Info-default-directory-list
373 Current buffer must be a loaddef-style file."
374 (let (
375 (found nil)
376 (no-autoloads nil))
378 (save-excursion
379 (save-restriction
380 (widen)
381 (goto-char (point-min))
382 ;; Look for the section for PATH-ELEMENT
383 (while (and (not found)
384 (search-forward generate-autoload-section-header nil t))
385 (let ((form (autoload-read-section-header)))
386 (cond
387 ((and
388 (equal (nth 0 form) type)
389 (member (nth 1 form) path-element))
391 ;; We found the section for this add.
392 (let ((begin (match-beginning 0)))
393 (progn
394 (search-forward generate-autoload-section-trailer)
395 (delete-region begin (point))
396 (setq found t)))))))
398 (unless found
399 (progn
400 (setq found 'new)
401 ;; No later sections in the file. Put before the last page.
402 (goto-char (point-max))
403 (search-backward "\f" nil t)))
405 (elinstall-generate-add-to-path path-element type)))
407 ;;This never belongs in the no-autoloads section.
408 nil))
409 ;;;_ . elinstall-deffile-insert
411 (defun elinstall-deffile-insert (action)
412 "Insert autoloads etc into current file according to ACTION.
413 The format of ACTION is described in the design docs.
415 Return filename if this action belongs in the no-autoload section."
417 (when action
418 (case (car action)
419 (add-file-autoloads
420 (elinstall-deffile-insert-autoloads
421 (third action)
422 (fifth action)))
424 (add-to-load-path
425 (elinstall-deffile-insert-add-to-path
426 (third action)
427 'add-to-load-path)
428 nil)
430 (add-to-info-path
431 (elinstall-deffile-insert-add-to-path
432 (third action)
433 'add-to-info-path)
434 nil)
436 ((preload-file run-tests byte-compile)
437 (error "This case should not come here.")))))
439 ;;;_ . elinstall-prepare-deffile
440 (defun elinstall-prepare-deffile (deffile)
441 "Try to ensure that DEFFILE is available for receiving autoloads"
443 (autoload-ensure-default-file deffile)
444 (with-current-buffer (find-file-noselect deffile)
447 ;; We must read/write the file without any code conversion,
448 ;; but still decode EOLs.
449 (let ((coding-system-for-read 'raw-text))
451 ;; This is to make generated-autoload-file have Unix EOLs, so
452 ;; that it is portable to all platforms.
453 (setq buffer-file-coding-system 'raw-text-unix))
454 (or (> (buffer-size) 0)
455 (error "Autoloads file %s does not exist" buffer-file-name))
456 (or (file-writable-p buffer-file-name)
457 (error "Autoloads file %s is not writable"
458 buffer-file-name))))
460 ;;;_ . elinstall-update-deffile
462 ;;Adapted from autoload.el `update-directory-autoloads'.
464 (defun elinstall-update-deffile (target actions &optional
465 use-load-path force)
467 Update file TARGET with current autoloads as specified by ACTIONS.
468 Also remove any old definitions pointing to libraries that can no
469 longer be found.
471 ACTIONS must be a list of actions (See the format doc). Each one's
472 filename must be relative to some element of load-path.
474 USE-LOAD-PATH is a list to use as load-path. It should include
475 any new load-path that we are arranging to create. If it's not given,
476 load-path itself is used.
478 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
479 Other non-nil cases of FORCE are reserved for future development.
481 This uses `update-file-autoloads' (which see) to do its work.
482 In an interactive call, you must give one argument, the name
483 of a single directory."
484 (let
486 (use-load-path (or use-load-path load-path))
487 (this-time (current-time))
488 ;;files with no autoload cookies.
489 (no-autoloads nil))
491 (elinstall-prepare-deffile target)
492 (with-current-buffer
493 (find-file-noselect target)
494 (save-excursion
495 (setq actions
496 (elinstall-remove-autogen-action
497 (autoload-trim-file-name target)
498 actions))
500 (goto-char (point-min))
501 (while (search-forward generate-autoload-section-header nil t)
502 (let* ((form (autoload-read-section-header))
503 (file (nth 3 form)))
504 (cond ((and (consp file) (stringp (car file)))
505 ;; This is a list of files that have no
506 ;; autoload cookies.
507 ;; There shouldn't be more than one such entry.
508 ;; Remove the obsolete section.
509 (autoload-remove-section (match-beginning 0))
510 (let ((last-time (nth 4 form)))
511 (dolist (file file)
512 (let ((file-time (nth 5 (file-attributes file))))
513 (when (and file-time
514 (not (time-less-p last-time file-time)))
515 ;; file unchanged
516 (push file no-autoloads)
517 (setq actions
518 (elinstall-remove-autogen-action
519 file actions)))))))
520 ((not (stringp file)))
522 (let
523 ((file-path
524 (locate-library file nil use-load-path)))
525 (cond
526 ;;File doesn't exist, so remove its
527 ;;section.
528 ((not file-path)
529 (autoload-remove-section
530 (match-beginning 0)))
532 ;; File hasn't changed, so do nothing.
533 ((equal
534 (nth 4 form)
535 (nth 5 (file-attributes file-path)))
536 nil)
538 (elinstall-deffile-insert
539 (elinstall-get-autogen-action
540 file actions))))
542 (setq actions
543 (elinstall-remove-autogen-action
544 file actions))))))))
546 ;; Remaining actions have no existing autoload sections yet.
547 (setq no-autoloads
548 (append no-autoloads
549 (delq nil (mapcar #'elinstall-deffile-insert actions))))
550 (when no-autoloads
551 ;; Sort them for better readability.
552 (setq no-autoloads (sort no-autoloads 'string<))
553 ;; Add the `no-autoloads' section.
554 (goto-char (point-max))
555 (search-backward "\f" nil t)
557 (elinstall-insert-section-header
558 (current-buffer)
559 (list 'autoloads nil nil no-autoloads this-time))
560 (insert generate-autoload-section-trailer))
561 (save-buffer))))
563 ;;;_ . elinstall-stage-update-deffiles
564 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
565 "Update any deffiles mentioned in SEGMENT-LIST.
566 FORCE and USE-LOAD-PATH have the same meaning as in
567 `elinstall-update-deffile'.
569 (mapcar
570 #'(lambda (segment)
571 (let*
572 ((deffile (car segment)))
573 (if (stringp deffile)
574 (elinstall-update-deffile deffile (cdr segment) force
575 use-load-path))))
576 segment-list))
578 ;;;_ , Doing actions to arrange preloads
579 ;;;_ . elinstall-symlink-on-emacs-start
580 (defun elinstall-symlink-on-emacs-start
581 (filename target-basename target-dir &optional priority force)
582 "Symlink to TARGET-BASENAME.el in TARGET-DIR
584 If PRIORITY is given, it will be used as the priority prefix,
585 otherwise elinstall-default-priority will be.
586 PRIORITY must be an integer or nil.
587 If FORCE is `t', do it regardless of timestamps etc.
588 Other non-nil cases of FORCE are reserved for future development."
589 (let*
591 (priority (or priority elinstall-default-priority))
592 (target-name-nodir
593 (format
594 "%d%s.el"
595 priority
596 target-basename))
597 (target
598 (expand-file-name target-name-nodir target-dir)))
601 (cond
602 ;;Path should already exist.
603 ((not
604 (file-exists-p target-dir))
605 (message "The target directory doesn't exist."))
606 ;;Target shouldn't already exist, but if force is given, let
607 ;;user override.
608 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
609 ;;do nothing even on force.
610 ((and
611 (file-exists-p target)
613 (not force)
614 (not
615 (yes-or-no-p
616 (format "Really overwrite %s? " target))))
617 (message "File %s already exists" target)))
620 (make-symbolic-link
621 filename
622 target
623 nil)))))
625 ;;;_ . elinstall-add-to-dot-emacs
626 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
627 "Add code to load FILENAME to .emacs.
628 FILENAME should not have an extension"
630 ;;Visit .emacs
631 (with-current-buffer (find-file-noselect dot-emacs-name)
632 (save-excursion
633 ;;add at end of file
634 (goto-char (point-max))
635 (insert "\n;;Added by elinstall")
636 (insert "\n;;Consider using my-site-start to manage .emacs\n")
637 (pp `(load ,filename) (current-buffer))
638 (save-buffer))))
641 ;;;_ . elinstall-arrange-preload
642 ;;;###autoload
643 (defun elinstall-arrange-preload (force filename basename &optional priority)
644 "Arrange for FILENAME to be loaded on emacs start.
645 FORCE has its usual meaning.
646 BASENAME and PRIORITY are used as arguments to
647 `elinstall-symlink-on-emacs-start'.
650 (let
651 ((preload-target elinstall-default-preload-target))
653 ;;Dispatch the possibilities.
654 (cond
655 ((eq preload-target 'dot-emacs)
656 (elinstall-add-to-dot-emacs "~/.emacs" filename))
657 ((stringp preload-target)
658 (elinstall-symlink-on-emacs-start
659 filename basename preload-target priority force))
660 (null preload-target
661 (message "Not arranging for preloads"))
663 (message "I don't recognize that")))))
664 ;;;_ . elinstall-stage-arrange-preloads
665 (defun elinstall-stage-arrange-preloads (actions deffiles-used)
666 "Arrange any preloads mentioned in ACTIONS."
668 (mapcar
669 #'(lambda (act)
670 (case (car act)
671 (preload-file
672 (let*
673 ( (filename
674 (caddr act))
675 (proceed-p
676 (case (second act)
677 ((t) t)
678 ((nil) nil)
679 (if-used
680 (member filename deffiles-used)))))
682 (when proceed-p
683 (apply
684 #'elinstall-arrange-preload
685 force
686 (cddr act)))))
688 (error
689 "elinstall-stage-arrange-preloads: Action not
690 recognized."))) )
691 actions))
694 ;;;_ , Run tests
695 ;;;_ . elinstall-stage-run-tests
696 (defun elinstall-stage-run-tests (actions)
697 "Run any tests mentioned in ACTIONS."
699 (mapcar
700 #'(lambda (act)
701 (case (car act)
702 (run-tests
703 ;;$$WRITE ME - not a high priority right now.
704 nil)
706 (error
707 "elinstall-stage-run-tests: Action not
708 recognized."))) )
709 actions))
712 ;;;_ , Byte compile
713 ;;;_ . elinstall-stage-byte-compile
714 (defun elinstall-stage-byte-compile (actions)
715 "Do any byte-compilation mentioned in ACTIONS."
717 (mapcar
718 #'(lambda (act)
719 (case (car act)
720 ;;$$IMPROVE ME Understand flags to control second
721 ;;argument (whether to load file after
722 ;;compilation)
723 (byte-compile
724 (byte-compile-file (second act)))
726 (error
727 "elinstall-stage-byte-compile: Action not
728 recognized."))) )
729 actions))
731 ;;;_ . Segregating actions
732 ;;;_ , elinstall-remove-empty-segs
733 (defun elinstall-remove-empty-segs (segment-list)
734 "Return SEGMENT-LIST minus any segments that have no actions.
735 Intended only for the deffile stage data."
736 (delq nil
737 (mapcar
738 #'(lambda (segment)
739 (if (cdr segment)
740 segment
741 nil))
742 segment-list)))
744 ;;;_ , elinstall-segregate-actions
745 (defun elinstall-segregate-actions (actions)
746 "Return actions segregated by deffile.
748 Returns a list whose elements are each a cons of:
749 * deffile filename or nil
750 * A list of actions to be done for that deffile."
752 (let
754 (build-deffiles '())
755 (run-tests '())
756 (byte-compile '())
757 (arrange-preloads '()))
759 (dolist (act actions)
760 (when act
761 (case (car act)
762 ((add-file-autoloads
763 add-to-info-path
764 add-to-load-path)
765 (let*
766 ((deffile-name (second act))
767 (cell-already
768 (assoc deffile-name build-deffiles)))
769 (if cell-already
770 ;;There are already actions on this deffile.
771 ;;Splice this action in.
772 (setcdr cell-already
773 (cons act (cdr cell-already)))
774 ;;There are no actions on this deffile. Add a
775 ;;place for them and include this action.
776 (push (list deffile-name act) build-deffiles))))
777 (preload-file
778 (push act arrange-preloads))
779 (run-tests
780 (push act run-tests))
781 (byte-compile
782 (push act byte-compile)))))
784 (elinstall-make-stages
785 :build-deffiles
786 (elinstall-remove-empty-segs build-deffiles)
787 :run-tests
788 run-tests
789 :byte-compile
790 byte-compile
791 :arrange-preloads
792 arrange-preloads)))
798 ;;;_ . Finding actions
799 ;;;_ , Treating the parameter list
800 ;;;_ . elinstall-add-parameter
801 (defun elinstall-add-parameter (alist key new-value)
802 "Add a new value for KEY to ALIST"
804 (cons
805 (cons key new-value)
806 (assq-delete-all key (copy-list alist))))
808 ;;;_ . elinstall-get-parameter
809 (defun elinstall-get-parameter (alist key)
810 "Get the value of KEY from ALIST"
812 (cdr (assq key alist)))
813 ;;;_ . elinstall-expand-file-name
814 ;;$$OBSOLETE
816 (defun elinstall-expand-file-name (filename alist)
817 "Expand FILENAME by the value of `path' in ALIST"
818 (expand-file-name
819 filename
820 (elinstall-get-parameter alist 'path)))
821 ;;;_ , Finding deffiles
822 ;;;_ . elinstall-expand-deffile-name
823 (defun elinstall-expand-deffile-name (deffile)
824 "Expand DEFFILE autoload.el's way."
826 (expand-file-name (or deffile "loaddefs.el")
827 (expand-file-name "lisp"
828 source-directory)))
830 ;;;_ . elinstall-maybe-get-deffile
831 (defun elinstall-maybe-get-deffile (file)
832 "If FILE defined `generated-autoload-file', return it.
833 Otherwise return nil.
834 Return it as an absolute filename."
836 (save-excursion
837 ;;$$FIXME load buffer if it's not already loaded
838 (let*
839 ((existing-buffer (get-file-buffer file)))
841 ;; We want to get a value for generated-autoload-file from
842 ;; the local variables section if it's there.
843 ;;But if it's not loaded, we don't? Maybe should use
844 ;; `autoload-find-file' and load it.
845 (if existing-buffer
846 (set-buffer existing-buffer))
847 (if (local-variable-p 'generated-autoload-file)
848 (elinstall-expand-deffile-name
849 generated-autoload-file)
850 nil))))
853 ;;;_ , Workers
854 ;;;_ . elinstall-find-actions-for-file
855 (defun elinstall-find-actions-for-file
856 (filename load-path-element dir parameters)
857 "Return a list of actions to do for FILENAME.
858 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
859 `elinstall-find-actions-by-spec' "
861 (let
862 ((full-path
863 (expand-file-name filename dir)))
864 (list
865 `(add-file-autoloads
866 ,(elinstall-get-parameter
867 parameters 'def-file)
868 ;;load-name relative to a member of load-path
869 ,(file-name-sans-extension
870 (file-relative-name
871 full-path
872 load-path-element))
873 ,load-path-element ;;Is this still used?
874 ,full-path))))
876 ;;;_ . elinstall-find-actions-by-spec
878 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
879 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
881 LOAD-PATH-ELEMENT is the conceptual element of load-path that
882 surrounds DIR. It may not yet have been added to load-path."
883 (if (consp spec)
884 ;;$$IMPROVE ME by adding the other cases in the design.
885 (case (car spec)
887 (let
888 ((new-path
889 (expand-file-name
890 (second spec)
891 dir)))
893 (elinstall-find-actions-by-spec
894 (third spec)
895 load-path-element
896 new-path
897 parameters)))
899 (all
900 (apply #'nconc
901 (mapcar
902 #'(lambda (sub-spec)
903 (elinstall-find-actions-by-spec
904 sub-spec
905 load-path-element
907 parameters))
908 (cdr spec))))
910 (file
911 (elinstall-find-actions-for-file
912 filename load-path-element dir parameters))
914 (dir
915 (let*
916 ((dirname
917 (expand-file-name
918 (second spec)
919 dir))
920 (load-path-here
921 (not
922 (elinstall-get-parameter
923 parameters 'block-add-to-load-path)))
924 (load-path-element
925 (if load-path-here
926 dirname
927 load-path-element)))
929 (cons
930 ;;$$IMPROVE ME
931 ;;Do this only if there are loadable files.
932 (if load-path-here
933 `(add-to-load-path
934 ,(elinstall-get-parameter
935 parameters 'def-file)
936 ,load-path-element)
937 '())
938 ;;$$IMPROVE ME
939 ;;Do add-to-info-path too. But test if there are
940 ;;any info files present.
942 ;;$$IMPROVE ME
943 ;; We want to get a value for generated-autoload-file
944 ;; from the local variables section if it's there.
945 ;;Use `elinstall-maybe-get-deffile'
946 ;; Otherwise we'll use `def-file' in parameters.
948 ;;$$FIXME This isn't quite right. If directory
949 ;;itself is not in load-path, this will be wrong.
950 ;;Gotta know where our encompassing part of
951 ;;load-path is.
953 ;;$$ENCAP ME This should be shared with the
954 ;;treatment of (file FN)
956 ;;$$FIXME Don't do directories, but maybe recurse on
957 ;;them, if a flag is set. And since we definitely
958 ;;have a load-path element here,
959 ;;'block-add-to-load-path according to a parameter.
960 ;;Maybe could follow/not symlinks similarly.
961 (apply #'nconc
962 (mapcar
963 #'(lambda (filename)
964 (elinstall-find-actions-for-file
965 filename
966 load-path-element
967 dirname
968 parameters))
970 (directory-files
971 dirname
972 nil ;;Relative filenames
973 elinstall-elisp-regexp))))))
975 (def-file
976 (let
977 ((new-def-file
978 (expand-file-name
979 (second spec)
980 dir))
981 (for-preload (third spec)))
982 (assert (listp for-preload))
983 (append
984 (list
986 (and for-preload (car for-preload))
987 `(preload-file
988 ,(car for-preload)
989 ,new-def-file
990 ,@(cdr for-preload))
991 '()))
993 (elinstall-find-actions-by-spec
994 (fourth spec)
995 load-path-element
997 (elinstall-add-parameter parameters
998 'def-file new-def-file))))))
1000 ;;$$IMPROVE ME by adding the other cases in the design.
1001 (case spec
1002 (t))))
1003 ;;;_ . high-level work
1004 ;;;_ , elinstall-get-relevant-load-path
1005 (defun elinstall-get-relevant-load-path (actions)
1007 (delq nil
1008 (mapcar
1009 #'(lambda (act)
1010 (case (car act)
1011 (add-to-load-path
1012 (second act))
1013 (t nil)))
1014 actions)))
1015 ;;;_ , elinstall-get-deffile-list
1016 (defun elinstall-get-deffile-list (stages)
1017 "Get a list of deffile names"
1019 (mapcar
1020 #'car
1021 (elinstall-stages->build-deffiles stages)))
1023 ;;;_ , elinstall-x
1024 (defun elinstall-x (dir spec &optional force)
1026 (let*
1028 ;;This is just the default deffile, spec can override it.
1029 (def-file
1030 (elinstall-expand-deffile-name nil))
1031 (actions
1032 (elinstall-find-actions-by-spec
1033 spec
1037 (def-file . ,def-file ))))
1038 (stages (elinstall-segregate-actions actions))
1039 (use-load-path
1040 (elinstall-get-relevant-load-path
1041 actions)))
1043 (elinstall-stage-update-deffiles
1044 (elinstall-stages->build-deffiles stages)
1045 force
1046 use-load-path)
1047 (elinstall-stage-arrange-preloads
1048 (elinstall-stages->arrange-preloads stages)
1049 (elinstall-get-deffile-list stages))
1052 ;;;_ . Entry points
1053 ;;;_ , elinstall
1054 ;;;###autoload
1055 (defun elinstall (project-name path spec &optional force)
1056 "Install elisp files.
1057 They need not be a formal package.
1059 Parameters:
1061 PROJECT-NAME - the name of the project
1063 PATH - Path to the project.
1064 Suggestion: (elinstall-directory-true-name)
1066 SPEC - a spec for the autoloads etc to make. It can be as simple as
1067 \(dir \"\.\") for installing one directory.
1069 If FORCE is t, install a package even if it has already been
1070 installed. Other non-nil cases of FORCE are reserved for future
1071 development."
1073 (when
1074 (and
1075 (or
1076 force
1077 (not (elinstall-already-installed project-name)))
1078 (yes-or-no-p (format "Re-install %s? " project-name)))
1079 (elinstall-x
1080 path
1081 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1082 force)
1083 (elinstall-record-installed project-name)))
1087 ;;;_ , elinstall-update-directory-autoloads
1088 ;;$$TEST ME
1089 ;;;###autoload
1090 (defun elinstall-update-directory-autoloads (dir)
1093 (interactive "DInstall all elisp files from directory: ")
1096 (let
1097 ((def-file-name
1098 (elinstall-expand-deffile-name
1099 generated-autoload-file)))
1101 (elinstall-x
1103 `(def-file ,def-file-name (nil) (dir ".")))))
1107 ;;;_ , elinstall-update-file-autoloads
1108 ;;$$TEST ME
1109 ;;;###autoload
1110 (defun elinstall-update-file-autoloads (file)
1113 (interactive "fInstall elisp file: ")
1114 (let
1115 ((def-file-name
1117 (elinstall-maybe-get-deffile file)
1118 (elinstall-expand-deffile-name
1119 generated-autoload-file))))
1120 (elinstall
1121 file
1122 `(def-file ,def-file-name (nil) (file ,file)))))
1124 ;;;_. Footers
1125 ;;;_ , Provides
1127 (provide 'elinstall)
1129 ;;;_ * Local emacs vars.
1130 ;;;_ + Local variables:
1131 ;;;_ + mode: allout
1132 ;;;_ + End:
1134 ;;;_ , End
1135 ;;; elinstall.el ends here