Bugfixes: Byte-compile setup and fixed misc compile errors
[elinstall.git] / elinstall.el
blobd152c93c6c592fb241570e092bfb142459452cc0
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
41 (require 'byte-compile nil t) ;;
44 ;;;_. Body
45 ;;;_ , Customizations
46 (defgroup elinstall
47 '()
48 "Customizations for elinstall"
49 :group 'elinstall)
51 (defcustom elinstall-default-priority
53 "Default priority for site-start"
54 :group 'elinstall
55 :type 'integer)
57 (defcustom elinstall-default-preload-target
58 "~/.emacs.d/site-start.d/"
59 "Default preload-target for registering autoloads"
60 :group 'elinstall
61 :type
62 '(choice
63 (const "~/.emacs.d/site-start.d/")
64 (const "/etc/emacs/site-start.d/")
65 (directory "" )
66 (const nil)
67 (const 'dot-emacs)))
70 (defcustom elinstall-already-installed
71 '()
72 "Things that have already been installed.
73 This exists for recording what has been installed. User interaction is not
74 contemplated at this time." )
75 ;;;_ , Types
76 ;;;_ . elinstall-stages
77 (defstruct (elinstall-stages
78 (:constructor elinstall-make-stages)
79 (:conc-name elinstall-stages->)
80 (:copier nil))
81 "The elinstall stages"
82 build-deffiles
83 run-tests
84 byte-compile
85 arrange-preloads)
86 ;;;_ , Data
87 ;;;_ . Regular expressions
88 ;;;_ , elinstall-elisp-regexp
89 (defconst elinstall-elisp-regexp
90 (let ((tmp nil))
91 (dolist
92 (suf (get-load-suffixes))
93 (unless (string-match "\\.elc" suf) (push suf tmp)))
94 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
95 "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 path-element (nth 1 form)))
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 force))
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))
730 ;;;_ . Segregating actions
731 ;;;_ , elinstall-remove-empty-segs
732 (defun elinstall-remove-empty-segs (segment-list)
733 "Return SEGMENT-LIST minus any segments that have no actions.
734 Intended only for the deffile stage data."
735 (delq nil
736 (mapcar
737 #'(lambda (segment)
738 (if (cdr segment)
739 segment
740 nil))
741 segment-list)))
743 ;;;_ , elinstall-segregate-actions
744 (defun elinstall-segregate-actions (actions)
745 "Return actions segregated by deffile.
747 Returns a list whose elements are each a cons of:
748 * deffile filename or nil
749 * A list of actions to be done for that deffile."
751 (let
753 (build-deffiles '())
754 (run-tests '())
755 (byte-compile '())
756 (arrange-preloads '()))
758 (dolist (act actions)
759 (when act
760 (case (car act)
761 ((add-file-autoloads
762 add-to-info-path
763 add-to-load-path)
764 (let*
765 ((deffile-name (second act))
766 (cell-already
767 (assoc deffile-name build-deffiles)))
768 (if cell-already
769 ;;There are already actions on this deffile.
770 ;;Splice this action in.
771 (setcdr cell-already
772 (cons act (cdr cell-already)))
773 ;;There are no actions on this deffile. Add a
774 ;;place for them and include this action.
775 (push (list deffile-name act) build-deffiles))))
776 (preload-file
777 (push act arrange-preloads))
778 (run-tests
779 (push act run-tests))
780 (byte-compile
781 (push act byte-compile)))))
783 (elinstall-make-stages
784 :build-deffiles
785 (elinstall-remove-empty-segs build-deffiles)
786 :run-tests
787 run-tests
788 :byte-compile
789 byte-compile
790 :arrange-preloads
791 arrange-preloads)))
792 ;;;_ . Finding actions
793 ;;;_ , Treating the parameter list
794 ;;;_ . elinstall-add-parameter
795 (defun elinstall-add-parameter (alist key new-value)
796 "Add a new value for KEY to ALIST"
798 (cons
799 (cons key new-value)
800 (assq-delete-all key (copy-list alist))))
802 ;;;_ . elinstall-get-parameter
803 (defun elinstall-get-parameter (alist key)
804 "Get the value of KEY from ALIST"
806 (cdr (assq key alist)))
807 ;;;_ . elinstall-expand-file-name
808 ;;$$OBSOLETE
810 (defun elinstall-expand-file-name (filename alist)
811 "Expand FILENAME by the value of `path' in ALIST"
812 (expand-file-name
813 filename
814 (elinstall-get-parameter alist 'path)))
815 ;;;_ , Finding deffiles
816 ;;;_ . elinstall-expand-deffile-name
817 (defun elinstall-expand-deffile-name (deffile)
818 "Expand DEFFILE autoload.el's way."
820 (expand-file-name (or deffile "loaddefs.el")
821 (expand-file-name "lisp"
822 source-directory)))
824 ;;;_ . elinstall-maybe-get-deffile
825 (defun elinstall-maybe-get-deffile (file)
826 "If FILE defined `generated-autoload-file', return it.
827 Otherwise return nil.
828 Return it as an absolute filename."
830 (save-excursion
831 ;;$$FIXME load buffer if it's not already loaded
832 (let*
833 ((existing-buffer (get-file-buffer file)))
835 ;; We want to get a value for generated-autoload-file from
836 ;; the local variables section if it's there.
837 ;;But if it's not loaded, we don't? Maybe should use
838 ;; `autoload-find-file' and load it.
839 (if existing-buffer
840 (set-buffer existing-buffer))
841 (if (local-variable-p 'generated-autoload-file)
842 (elinstall-expand-deffile-name
843 generated-autoload-file)
844 nil))))
846 ;;;_ . Workers
847 ;;;_ , elinstall-find-actions-for-file
848 (defun elinstall-find-actions-for-file
849 (filename load-path-element dir parameters)
850 "Return a list of actions to do for FILENAME.
851 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
852 `elinstall-find-actions-by-spec' "
854 ;;$$IMPROVE ME take and treat a "force" argument.
855 (let*
856 ((full-path
857 (expand-file-name filename dir))
858 ;;$$IMPROVE ME - take into account local no-byte-compile flag.
859 (compile-p
860 (and
861 (featurep 'byte-compile)
862 (string-match emacs-lisp-file-regexp filename)
863 (file-readable-p full-path)
864 (not (auto-save-file-name-p full-path))
865 (let*
866 ((dest (byte-compile-dest-file full-path))
867 ;;$$PUNT - currently, we wouldn't have gotten
868 ;;here if we weren't intending to do everything.
869 (recompile-p nil)
870 (compile-new t))
871 (if (file-exists-p dest)
872 ;; File was already compiled.
873 (or recompile-p (file-newer-than-file-p full-path dest))
874 (or compile-new
875 (y-or-n-p (concat "Compile " filename "? "))))))))
878 (list
879 (if compile-p
880 `(byte-compile ,full-path)
881 '())
882 `(add-file-autoloads
883 ,(elinstall-get-parameter
884 parameters 'def-file)
885 ;;load-name relative to a member of load-path
886 ,(file-name-sans-extension
887 (file-relative-name
888 full-path
889 load-path-element))
890 ,load-path-element ;;Is this still used?
891 ,full-path))))
893 ;;;_ , elinstall-find-actions-by-spec
895 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
896 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
898 LOAD-PATH-ELEMENT is the conceptual element of load-path that
899 surrounds DIR. It may not yet have been added to load-path."
900 (if (consp spec)
901 ;;$$IMPROVE ME by adding the other cases in the design.
902 (case (car spec)
904 (let
905 ((new-path
906 (expand-file-name
907 (second spec)
908 dir)))
910 (elinstall-find-actions-by-spec
911 (third spec)
912 load-path-element
913 new-path
914 parameters)))
916 (all
917 (apply #'nconc
918 (mapcar
919 #'(lambda (sub-spec)
920 (elinstall-find-actions-by-spec
921 sub-spec
922 load-path-element
924 parameters))
925 (cdr spec))))
927 (file
928 (elinstall-find-actions-for-file
929 filename load-path-element dir parameters))
931 (dir
932 (let*
933 ((dirname
934 (expand-file-name
935 (second spec)
936 dir))
937 (load-path-here
938 (not
939 (elinstall-get-parameter
940 parameters 'block-add-to-load-path)))
941 (load-path-element
942 (if load-path-here
943 dirname
944 load-path-element)))
946 (cons
947 ;;$$IMPROVE ME
948 ;;Do this only if there are loadable files.
949 (if load-path-here
950 `(add-to-load-path
951 ,(elinstall-get-parameter
952 parameters 'def-file)
953 ,load-path-element)
954 '())
955 ;;$$IMPROVE ME
956 ;;Do add-to-info-path too. But test if there are
957 ;;any info files present.
959 ;;$$IMPROVE ME
960 ;; We want to get a value for generated-autoload-file
961 ;; from the local variables section if it's there.
962 ;;Use `elinstall-maybe-get-deffile'
963 ;; Otherwise we'll use `def-file' in parameters.
965 ;;$$FIXME This isn't quite right. If directory
966 ;;itself is not in load-path, this will be wrong.
967 ;;Gotta know where our encompassing part of
968 ;;load-path is.
970 ;;$$ENCAP ME This should be shared with the
971 ;;treatment of (file FN)
973 ;;$$FIXME Don't do directories, but maybe recurse on
974 ;;them, if a flag is set. And since we definitely
975 ;;have a load-path element here,
976 ;;'block-add-to-load-path according to a parameter.
977 ;;Maybe could follow/not symlinks similarly.
978 (apply #'nconc
979 (mapcar
980 #'(lambda (filename)
981 (elinstall-find-actions-for-file
982 filename
983 load-path-element
984 dirname
985 parameters))
987 (directory-files
988 dirname
989 nil ;;Relative filenames
990 elinstall-elisp-regexp))))))
992 (def-file
993 (let
994 ((new-def-file
995 (expand-file-name
996 (second spec)
997 dir))
998 (for-preload (third spec)))
999 (assert (listp for-preload))
1000 (append
1001 (list
1003 (and for-preload (car for-preload))
1004 `(preload-file
1005 ,(car for-preload)
1006 ,new-def-file
1007 ,@(cdr for-preload))
1008 '()))
1010 (elinstall-find-actions-by-spec
1011 (fourth spec)
1012 load-path-element
1014 (elinstall-add-parameter parameters
1015 'def-file new-def-file))))))
1017 ;;$$IMPROVE ME by adding the other cases in the design.
1018 (case spec
1019 (t))))
1020 ;;;_ . high-level work
1021 ;;;_ , elinstall-get-relevant-load-path
1022 (defun elinstall-get-relevant-load-path (actions)
1024 (delq nil
1025 (mapcar
1026 #'(lambda (act)
1027 (case (car act)
1028 (add-to-load-path
1029 (second act))
1030 (t nil)))
1031 actions)))
1032 ;;;_ , elinstall-get-deffile-list
1033 (defun elinstall-get-deffile-list (stages)
1034 "Get a list of deffile names"
1036 (mapcar
1037 #'car
1038 (elinstall-stages->build-deffiles stages)))
1040 ;;;_ , elinstall-x
1041 (defun elinstall-x (dir spec &optional force)
1043 (let*
1045 ;;This is just the default deffile, spec can override it.
1046 (def-file
1047 (elinstall-expand-deffile-name nil))
1048 (actions
1049 (elinstall-find-actions-by-spec
1050 spec
1054 (def-file . ,def-file ))))
1055 (stages (elinstall-segregate-actions actions))
1056 (use-load-path
1057 (elinstall-get-relevant-load-path
1058 actions)))
1060 (elinstall-stage-update-deffiles
1061 (elinstall-stages->build-deffiles stages)
1062 force
1063 use-load-path)
1064 (elinstall-stage-arrange-preloads
1065 (elinstall-stages->arrange-preloads stages)
1066 (elinstall-get-deffile-list stages))
1067 (elinstall-stage-byte-compile
1068 (elinstall-stages->byte-compile stages))
1070 ;;;_ , Entry points
1071 ;;;_ . elinstall
1072 ;;;###autoload
1073 (defun elinstall (project-name path spec &optional force)
1074 "Install elisp files.
1075 They need not be a formal package.
1077 Parameters:
1079 PROJECT-NAME - the name of the project
1081 PATH - Path to the project.
1082 Suggestion: (elinstall-directory-true-name)
1084 SPEC - a spec for the autoloads etc to make. It can be as simple as
1085 \(dir \"\.\") for installing one directory.
1087 If FORCE is t, install a package even if it has already been
1088 installed. Other non-nil cases of FORCE are reserved for future
1089 development."
1091 (when
1092 (and
1093 (or
1094 force
1095 (not (elinstall-already-installed project-name)))
1096 (yes-or-no-p (format "Re-install %s? " project-name)))
1097 (elinstall-x
1098 path
1099 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1100 force)
1101 (elinstall-record-installed project-name)))
1105 ;;;_ . elinstall-update-directory-autoloads
1106 ;;$$SPLIT ME - one to just classically update autoloads, one to
1107 ;;install a file.
1108 ;;$$TEST ME
1109 ;;;###autoload
1110 (defun elinstall-update-directory-autoloads (dir)
1113 (interactive "DInstall all elisp files from directory: ")
1116 (let
1117 ((def-file-name
1118 (elinstall-expand-deffile-name
1119 generated-autoload-file)))
1121 (elinstall-x
1123 `(def-file ,def-file-name (nil) (dir ".")))))
1127 ;;;_ . elinstall-update-file-autoloads
1128 ;;$$SPLIT ME - one to just classically update autoloads, one to
1129 ;;install a file.
1130 ;;$$TEST ME
1131 ;;;###autoload
1132 (defun elinstall-update-file-autoloads (file)
1135 (interactive "fInstall elisp file: ")
1136 (let
1137 ((def-file-name
1139 (elinstall-maybe-get-deffile file)
1140 (elinstall-expand-deffile-name
1141 generated-autoload-file))))
1142 (elinstall-x
1143 file
1144 `(def-file ,def-file-name (nil) (file ,file)))))
1145 ;;;_. Footers
1146 ;;;_ , Provides
1148 (provide 'elinstall)
1150 ;;;_ * Local emacs vars.
1151 ;;;_ + Local variables:
1152 ;;;_ + mode: allout
1153 ;;;_ + End:
1155 ;;;_ , End
1156 ;;; elinstall.el ends here