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