Changed the logic of installedness checking
[elinstall.git] / elinstall.el
blob49fd244cc16dd22a720671c821ffebb22b752e9c
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.
24 ;;;_ , Version
25 ;;Version: 1.0
27 ;;;_ , Commentary:
29 ;; Entry points:
30 ;; elinstall Use this for overall loading
32 ;; elinstall-arrange-preload - Use this for non-autogenerated
33 ;; files that need to be linked in.
35 ;; elinstall-update-directory-autoloads
36 ;; elinstall-update-file-autoloads
38 ;;;_ , Requires
40 (require 'autoload)
41 (require 'pp)
42 (require 'cus-edit) ;;Because we save "installedness" manually
43 (require 'byte-compile nil t) ;;
46 ;;;_. Body
47 ;;;_ , Customizations
48 ;;;_ . Group
49 (defgroup elinstall
50 '()
51 "Customizations for elinstall"
52 :group 'development)
53 ;;;_ . elinstall-default-priority
54 (defcustom elinstall-default-priority
56 "Default priority for site-start"
57 :group 'elinstall
58 :type 'integer)
59 ;;;_ . elinstall-default-preload-target
60 (defcustom elinstall-default-preload-target
61 "~/.emacs.d/site-start.d/"
62 "Default preload-target for registering autoloads"
63 :group 'elinstall
64 :type
65 '(choice
66 (const "~/.emacs.d/site-start.d/")
67 (const "/etc/emacs/site-start.d/")
68 (directory "" )
69 (const nil)
70 (const 'dot-emacs)))
71 ;;;_ . elinstall-already-installed
72 (with-no-warnings
73 (defcustom elinstall-already-installed
74 '()
75 "(AUTOMATIC) Things that have already been installed.
76 This exists for recording what has been installed.
78 Though it's saved as customizable, user interaction is not
79 contemplated." ))
80 ;;;_ , Types
81 ;;;_ . elinstall-stages
82 (defstruct (elinstall-stages
83 (:constructor elinstall-make-stages)
84 (:conc-name elinstall-stages->)
85 (:copier nil))
86 "The elinstall stages"
87 build-deffiles
88 run-tests
89 byte-compile
90 arrange-preloads)
91 ;;;_ , Data
92 ;;;_ . Regular expressions
93 ;;;_ , elinstall-elisp-regexp
94 (defconst elinstall-elisp-regexp
95 (let ((tmp nil))
96 (dolist
97 (suf (get-load-suffixes))
98 (unless (string-match "\\.elc" suf) (push suf tmp)))
99 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
100 "Regular expression that matches elisp files" )
101 ;;;_ , Utilities
102 ;;;_ . elinstall-directory-true-name
103 (defun elinstall-directory-true-name ()
104 "Get the true name of the directory the calling code lives in.
105 CAUTION: This is sensitive to where it's called. That's the point of it."
106 (file-name-directory
107 (if load-file-name
108 (file-truename load-file-name)
109 (file-truename buffer-file-name))))
110 ;;;_ . Checking installedness
111 ;;;_ , elinstall-get-installation-record
112 (defun elinstall-get-installation-record (project-name)
113 "Return the installation record for PROJECT-NAME."
115 (assoc project-name elinstall-already-installed))
117 ;;;_ , elinstall-already-installed
118 (defun elinstall-already-installed (project-name)
119 "Return non-nil if PROJECT-NAME has been installed."
120 (elinstall-get-installation-record project-name))
122 ;;;_ , elinstall-record-installed
123 (defun elinstall-record-installed (project-name &optional version)
124 "Record that PROJECT-NAME has been installed."
125 (let
126 ((new-item
127 (list
128 project-name
129 (or version "0")
130 (current-time)
131 'installed))
132 (old-item
133 (elinstall-get-installation-record project-name)))
134 (when old-item
135 (setq elinstall-already-installed
136 (delete old-item elinstall-already-installed)))
137 (push new-item elinstall-already-installed)
138 (customize-save-variable
139 'elinstall-already-installed
140 elinstall-already-installed
141 "Set by elinstall-record-installed")))
142 ;;;_ . Finding deffiles
143 ;;;_ , elinstall-expand-deffile-name
144 (defun elinstall-expand-deffile-name (deffile)
145 "Expand DEFFILE autoload.el's way."
147 (expand-file-name (or deffile "loaddefs.el")
148 (expand-file-name "lisp"
149 source-directory)))
151 ;;;_ , Work
152 ;;;_ . Doing actions
154 ;;;_ , Doing autoload actions (adapted from autoload.el)
155 ;;;_ . Utilities about the action list
156 ;;;_ , elinstall-remove-autogen-action
157 (defun elinstall-remove-autogen-action (file actions)
158 "Return ACTIONS minus any add-file-autoloads on FILE removed."
160 (delq nil
161 (mapcar
162 #'(lambda (act)
163 (case (car act)
164 (add-file-autoloads
165 (if (equal file (third act))
167 act))
168 (t act)))
169 actions)))
170 ;;;_ , elinstall-get-autogen-action
171 (defun elinstall-get-autogen-action (file actions)
173 (let
174 ((the-act))
175 (dolist (act actions)
176 (case (car act)
177 (add-file-autoloads
178 (when (equal file (third act))
179 (setq the-act act)))))
180 the-act))
181 ;;;_ . About printing to autoload file
182 ;;;_ , elinstall-insert-section-header
183 (defun elinstall-insert-section-header (outbuf form)
184 "Insert the section-header line,
185 which lists the file name and which functions are in it, etc."
186 (insert generate-autoload-section-header)
187 (prin1 form outbuf)
188 (terpri outbuf)
189 ;; Break that line at spaces, to avoid very long lines.
190 ;; Make each sub-line into a comment.
191 (with-current-buffer outbuf
192 (save-excursion
193 (forward-line -1)
194 (while (not (eolp))
195 (move-to-column 64)
196 (skip-chars-forward "^ \n")
197 (or (eolp)
198 (insert "\n" generate-autoload-section-continuation))))))
200 ;;;_ , elinstall-insert-autoload-section
201 (defun elinstall-insert-autoload-section (text form &optional comment-string)
202 "Insert TEXT into current buffer as an autoload section"
204 (let* ( ;; This does something in Lucid Emacs.
205 (print-length nil)
206 (print-readably t)
207 (float-output-format nil))
209 (elinstall-insert-section-header (current-buffer) form)
210 (when comment-string
211 (insert ";;; " comment-string "\n"))
212 (insert text)
213 (insert generate-autoload-section-trailer)))
215 ;;;_ . Making autoloads
216 ;;;_ , elinstall-make-autoload-action
217 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
218 "Return the autoloads for current buffer as a string"
220 ;;We put all the text into a temp buffer, then get that buffer's
221 ;;string.
222 (let
223 ((outbuf
224 (generate-new-buffer " *temp*"))
225 (autoloads-done '())
226 (short-name
227 (file-name-nondirectory full-path))
229 ;; Apparently this does something in Lucid Emacs.
230 (print-length nil)
231 (print-readably t)
232 (float-output-format nil)
234 ;;load-name relative to a member of load-path.
235 (relative-name
236 (file-name-sans-extension
237 (file-relative-name
238 full-path
239 load-path-element))))
241 (with-current-buffer buf
242 (unwind-protect
243 (save-excursion
244 (save-restriction
245 (widen)
246 (goto-char (point-min))
247 (message "Finding autoloads for %s..." short-name)
248 (while (not (eobp))
249 (skip-chars-forward " \t\n\f")
250 (cond
251 ((looking-at (regexp-quote generate-autoload-cookie))
252 (search-forward generate-autoload-cookie)
253 (skip-chars-forward " \t")
254 (setq done-any t)
255 (if (eolp)
256 ;; Read the next form and make an autoload.
257 (let* ((form (prog1 (read (current-buffer))
258 (or (bolp) (forward-line 1))))
259 (autoload
260 (make-autoload form relative-name)))
261 (if autoload
262 (push (nth 1 form) autoloads-done)
263 (setq autoload form))
264 (let ((autoload-print-form-outbuf outbuf))
265 (autoload-print-form autoload)))
267 ;; Copy the rest of the line to the output.
268 (princ (buffer-substring
269 (progn
270 ;; Back up over whitespace,
271 ;; to preserve it.
272 (skip-chars-backward " \f\t")
273 (if (= (char-after (1+ (point))) ? )
274 ;; Eat one space.
275 (forward-char 1))
276 (point))
277 (progn (forward-line 1) (point)))
278 outbuf)))
279 ((looking-at ";")
280 ;; Don't read the comment.
281 (forward-line 1))
283 (forward-sexp 1)
284 (forward-line 1))))
285 (message "Finding autoloads for %s...done" short-name))
287 ;;Return this action. The temp buffer's contents is
288 ;;our final string.
289 `(add-file-autoloads
290 ,def-file
291 ,relative-name
292 ,full-path
293 ,(with-current-buffer outbuf (buffer-string))
294 ,autoloads-done))
296 ;;This in unwind-protected
297 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
300 ;;;_ , elinstall-generate-file-autoloads
302 (defun elinstall-generate-file-autoloads
303 (relative-name full-name text autoloads-done)
304 "Insert at point a loaddefs autoload section for FILE.
305 Autoloads are generated for defuns and defmacros in FILE
306 marked by `generate-autoload-cookie' (which see).
307 If FILE is being visited in a buffer, the contents of the buffer
308 are used.
309 Return non-nil in the case where no autoloads were added at point.
311 FULL-NAME is the absolute name of the file.
312 RELATIVE-NAME is its name respective to some component of load-path."
313 (if (not (equal text ""))
314 ;; Insert the section-header line which lists the file name and
315 ;; which functions are in it, etc.
316 (elinstall-insert-autoload-section
317 text
318 (list 'autoloads
319 autoloads-done
320 relative-name
321 (autoload-trim-file-name full-name)
322 (nth 5 (file-attributes full-name)))
323 (concat
324 "Generated autoloads from "
325 (autoload-trim-file-name full-name)))
328 ;;;_ , elinstall-deffile-insert-autoloads
329 (defun elinstall-deffile-insert-autoloads (file args)
330 "Update the autoloads for FILE in current buffer.
331 Return FILE if there was no autoload cookie in it, else nil.
333 Current buffer must be a loaddef-style file.
335 LOAD-NAME is the absolute name of the file.
336 RELATIVE-NAME is its name respective to some component of load-path."
337 (let (
338 (found nil)
339 (no-autoloads nil))
341 (save-excursion
342 (save-restriction
343 (widen)
344 (goto-char (point-min))
345 ;; Look for the section for FILE
346 (while (and (not found)
347 (search-forward generate-autoload-section-header nil t))
348 (let ((form (autoload-read-section-header)))
349 (cond
350 ((equal (nth 2 form) file)
351 ;; We found the section for this file.
352 (let ((begin (match-beginning 0)))
353 (progn
354 (search-forward generate-autoload-section-trailer)
355 (delete-region begin (point))
356 (setq found t))))
357 ((string< file (nth 2 form))
358 ;; We've come to a section alphabetically later than
359 ;; FILE. We assume the file is in order and so
360 ;; there must be no section for FILE. We will
361 ;; insert one before the section here.
362 (goto-char (match-beginning 0))
363 (setq found 'new)))))
364 (unless found
365 (progn
366 (setq found 'new)
367 ;; No later sections in the file. Put before the last page.
368 (goto-char (point-max))
369 (search-backward "\f" nil t)))
370 (setq no-autoloads
371 (apply #'elinstall-generate-file-autoloads
372 file args))))
374 (if no-autoloads file nil)))
375 ;;;_ . Arranging to add to info-path and load-path
376 ;;;_ , elinstall-generate-add-to-path
377 (defun elinstall-generate-add-to-path (path-element type)
378 "Insert code at point to add PATH-ELEMENT to a path.
379 If TYPE is:
380 * `add-to-load-path', add to load-path
381 * `add-to-info-path', add to Info-additional-directory-list
383 Current buffer must be a loaddef-style file."
384 (let ( (path-symbol
385 (case type
386 (add-to-load-path 'load-path)
387 (add-to-info-path 'Info-additional-directory-list)
388 (t (error "Type not recognized"))))
389 (description
390 (case type
391 (add-to-load-path "load-path")
392 (add-to-info-path "info-path")))
393 (autoloads-done '())
394 (print-length nil)
395 (print-readably t) ; This does something in Lucid Emacs.
396 (float-output-format nil))
398 (message "Generating %s additions..." description)
400 (elinstall-insert-autoload-section
401 (pp-to-string
402 `(add-to-list ',path-symbol
403 (expand-file-name
404 ,(file-relative-name path-element)
405 (if load-file-name
406 (file-name-directory
407 (file-truename load-file-name))))))
408 (list type (list path-element) nil nil nil)
409 nil)
410 (message "Generating %s additions...done" description)))
413 ;;;_ , elinstall-deffile-insert-add-to-path
414 (defun elinstall-deffile-insert-add-to-path (path-element type)
415 "Insert code in current buffer to add PATH-ELEMENT to a path.
416 If TYPE is:
417 * `add-to-load-path', add to load-path
418 * `add-to-info-path', add to Info-default-directory-list
420 Current buffer must be a loaddef-style file."
421 (let (
422 (found nil)
423 (no-autoloads nil))
425 (save-excursion
426 (save-restriction
427 (widen)
428 (goto-char (point-min))
429 ;; Look for the section for PATH-ELEMENT
430 (while (and (not found)
431 (search-forward generate-autoload-section-header nil t))
432 (let ((form (autoload-read-section-header)))
433 (cond
434 ((and
435 (equal (nth 0 form) type)
436 (member path-element (nth 1 form)))
438 ;; We found the section for this add.
439 (let ((begin (match-beginning 0)))
440 (progn
441 (search-forward generate-autoload-section-trailer)
442 (delete-region begin (point))
443 (setq found t)))))))
445 (unless found
446 (progn
447 (setq found 'new)
448 ;; No later sections in the file. Put before the last page.
449 (goto-char (point-max))
450 (search-backward "\f" nil t)))
452 (elinstall-generate-add-to-path path-element type)))
454 ;;This never belongs in the no-autoloads section.
455 nil))
456 ;;;_ . elinstall-deffile-insert
458 (defun elinstall-deffile-insert (action)
459 "Insert autoloads etc into current file according to ACTION.
460 The format of ACTION is described in the design docs.
462 Return filename if this action belongs in the no-autoload section."
464 (when action
465 (case (car action)
466 (add-file-autoloads
467 (elinstall-deffile-insert-autoloads
468 (third action)
469 (nthcdr 3 action)))
471 (add-to-load-path
472 (elinstall-deffile-insert-add-to-path
473 (third action)
474 'add-to-load-path)
475 nil)
477 (add-to-info-path
478 (elinstall-deffile-insert-add-to-path
479 (third action)
480 'add-to-info-path)
481 nil)
483 ((preload-file run-tests byte-compile)
484 (error "This case should not come here.")))))
486 ;;;_ . elinstall-prepare-deffile
487 (defun elinstall-prepare-deffile (deffile)
488 "Try to ensure that DEFFILE is available for receiving autoloads"
490 (autoload-ensure-default-file deffile)
491 (with-current-buffer (find-file-noselect deffile)
494 ;; We must read/write the file without any code conversion,
495 ;; but still decode EOLs.
496 (let ((coding-system-for-read 'raw-text))
498 ;; This is to make generated-autoload-file have Unix EOLs, so
499 ;; that it is portable to all platforms.
500 (setq buffer-file-coding-system 'raw-text-unix))
501 (or (> (buffer-size) 0)
502 (error "Autoloads file %s does not exist" buffer-file-name))
503 (or (file-writable-p buffer-file-name)
504 (error "Autoloads file %s is not writable"
505 buffer-file-name))))
507 ;;;_ . elinstall-update-deffile
509 ;;Adapted from autoload.el `update-directory-autoloads'.
511 (defun elinstall-update-deffile (target actions &optional
512 use-load-path force)
514 Update file TARGET with current autoloads as specified by ACTIONS.
515 Also remove any old definitions pointing to libraries that can no
516 longer be found.
518 ACTIONS must be a list of actions (See the format doc). Each one's
519 filename must be relative to some element of load-path.
521 USE-LOAD-PATH is a list to use as load-path. It should include
522 any new load-path that we are arranging to create. If it's not given,
523 load-path itself is used.
525 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
526 Other non-nil cases of FORCE are reserved for future development.
528 This uses `update-file-autoloads' (which see) to do its work.
529 In an interactive call, you must give one argument, the name
530 of a single directory."
531 (let
533 (use-load-path (or use-load-path load-path))
534 (this-time (current-time))
535 ;;files with no autoload cookies.
536 (no-autoloads nil))
538 (elinstall-prepare-deffile target)
539 (with-current-buffer
540 (find-file-noselect target)
541 (save-excursion
542 (setq actions
543 (elinstall-remove-autogen-action
544 (autoload-trim-file-name target)
545 actions))
547 (goto-char (point-min))
548 (while (search-forward generate-autoload-section-header nil t)
549 (let* ((form (autoload-read-section-header))
550 (file (nth 3 form)))
551 (cond ((and (consp file) (stringp (car file)))
552 ;; This is a list of files that have no
553 ;; autoload cookies.
554 ;; There shouldn't be more than one such entry.
555 ;; Remove the obsolete section.
556 (autoload-remove-section (match-beginning 0))
557 (let ((last-time (nth 4 form)))
558 (dolist (file file)
559 (let ((file-time (nth 5 (file-attributes file))))
560 (when (and file-time
561 (not (time-less-p last-time file-time)))
562 ;; file unchanged
563 (push file no-autoloads)
564 (setq actions
565 (elinstall-remove-autogen-action
566 file actions)))))))
567 ((not (stringp file)))
569 (let
570 ((file-path
571 (locate-library file nil use-load-path)))
572 (cond
573 ;;File doesn't exist, so remove its
574 ;;section.
575 ((not file-path)
576 (autoload-remove-section
577 (match-beginning 0)))
579 ;; File hasn't changed, so do nothing.
580 ((equal
581 (nth 4 form)
582 (nth 5 (file-attributes file-path)))
583 nil)
585 (elinstall-deffile-insert
586 (elinstall-get-autogen-action
587 file actions))))
589 (setq actions
590 (elinstall-remove-autogen-action
591 file actions))))))))
593 ;; Remaining actions have no existing autoload sections yet.
594 (setq no-autoloads
595 (append no-autoloads
596 (delq nil (mapcar #'elinstall-deffile-insert actions))))
597 (when no-autoloads
598 ;; Sort them for better readability.
599 (setq no-autoloads (sort no-autoloads 'string<))
600 ;; Add the `no-autoloads' section.
601 (goto-char (point-max))
602 (search-backward "\f" nil t)
603 (elinstall-insert-autoload-section
605 (list 'autoloads nil nil no-autoloads this-time)))
606 (save-buffer))))
608 ;;;_ . elinstall-stage-update-deffiles
609 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
610 "Update any deffiles mentioned in SEGMENT-LIST.
611 FORCE and USE-LOAD-PATH have the same meaning as in
612 `elinstall-update-deffile'.
614 (mapcar
615 #'(lambda (segment)
616 (let*
617 ((deffile (car segment)))
618 (if (stringp deffile)
619 (elinstall-update-deffile deffile (cdr segment) force
620 use-load-path))))
621 segment-list))
623 ;;;_ , Doing actions to arrange preloads
624 ;;;_ . elinstall-symlink-on-emacs-start
625 (defun elinstall-symlink-on-emacs-start
626 (filename target-basename target-dir &optional priority force)
627 "Symlink to TARGET-BASENAME.el in TARGET-DIR
629 If PRIORITY is given, it will be used as the priority prefix,
630 otherwise elinstall-default-priority will be.
631 PRIORITY must be an integer or nil.
632 If FORCE is `t', do it regardless of timestamps etc.
633 Other non-nil cases of FORCE are reserved for future development."
634 (let*
636 (priority (or priority elinstall-default-priority))
637 (target-name-nodir
638 (format
639 "%d%s.el"
640 priority
641 target-basename))
642 (target
643 (expand-file-name target-name-nodir target-dir)))
646 (cond
647 ;;Path should already exist.
648 ((not
649 (file-exists-p target-dir))
650 (message "The target directory doesn't exist."))
651 ;;Target shouldn't already exist, but if force is given, let
652 ;;user override.
653 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
654 ;;do nothing even on force.
655 ((and
656 (file-exists-p target)
658 (not force)
659 (not
660 (yes-or-no-p
661 (format "Really overwrite %s? " target))))
662 (message "File %s already exists" target)))
665 (make-symbolic-link
666 filename
667 target
668 nil)))))
670 ;;;_ . elinstall-add-to-dot-emacs
671 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
672 "Add code to load FILENAME to .emacs.
673 FILENAME should not have an extension"
675 ;;Visit .emacs
676 (with-current-buffer (find-file-noselect dot-emacs-name)
677 (save-excursion
678 ;;add at end of file
679 (goto-char (point-max))
680 (insert "\n;;Added by elinstall")
681 (insert "\n;;Consider using my-site-start to manage .emacs\n")
682 (pp `(load ,filename) (current-buffer))
683 (save-buffer))))
686 ;;;_ . elinstall-arrange-preload
687 ;;;###autoload
688 (defun elinstall-arrange-preload (force filename basename &optional priority)
689 "Arrange for FILENAME to be loaded on emacs start.
690 FORCE has its usual meaning.
691 BASENAME and PRIORITY are used as arguments to
692 `elinstall-symlink-on-emacs-start'.
695 (let
696 ((preload-target elinstall-default-preload-target))
698 ;;Dispatch the possibilities.
699 (cond
700 ((eq preload-target 'dot-emacs)
701 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
702 ((stringp preload-target)
703 (elinstall-symlink-on-emacs-start
704 filename basename preload-target priority force))
705 ((null preload-target)
706 (message "Not arranging for preloads"))
708 (message "I don't recognize that")))))
709 ;;;_ . elinstall-stage-arrange-preloads
710 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
711 "Arrange any preloads mentioned in ACTIONS."
713 (mapcar
714 #'(lambda (act)
715 (case (car act)
716 (preload-file
717 (let*
718 ( (filename
719 (caddr act))
720 (proceed-p
721 (case (second act)
722 ((t) t)
723 ((nil) nil)
724 (if-used
725 (member filename deffiles-used)))))
727 (when proceed-p
728 (apply
729 #'elinstall-arrange-preload
730 force
731 (cddr act)))))
733 (error
734 "elinstall-stage-arrange-preloads: Action not
735 recognized."))) )
736 actions))
739 ;;;_ , Run tests
740 ;;;_ . elinstall-stage-run-tests
741 (defun elinstall-stage-run-tests (actions)
742 "Run any tests mentioned in ACTIONS."
744 (mapcar
745 #'(lambda (act)
746 (case (car act)
747 (run-tests
748 ;;$$WRITE ME - not a high priority right now.
749 nil)
751 (error
752 "elinstall-stage-run-tests: Action not
753 recognized."))) )
754 actions))
757 ;;;_ , Byte compile
758 ;;;_ . elinstall-stage-byte-compile
759 (defun elinstall-stage-byte-compile (actions)
760 "Do any byte-compilation mentioned in ACTIONS."
762 (mapcar
763 #'(lambda (act)
764 (case (car act)
765 ;;$$IMPROVE ME Understand flags to control second
766 ;;argument (whether to load file after
767 ;;compilation)
768 (byte-compile
769 (byte-compile-file (second act)))
771 (error
772 "elinstall-stage-byte-compile: Action not
773 recognized."))) )
774 actions))
775 ;;;_ . Segregating actions
776 ;;;_ , elinstall-remove-empty-segs
777 (defun elinstall-remove-empty-segs (segment-list)
778 "Return SEGMENT-LIST minus any segments that have no actions.
779 Intended only for the deffile stage data."
780 (delq nil
781 (mapcar
782 #'(lambda (segment)
783 (if (cdr segment)
784 segment
785 nil))
786 segment-list)))
788 ;;;_ , elinstall-segregate-actions
789 (defun elinstall-segregate-actions (actions)
790 "Return actions segregated by deffile.
792 Returns a list whose elements are each a cons of:
793 * deffile filename or nil
794 * A list of actions to be done for that deffile."
796 (let
798 (build-deffiles '())
799 (run-tests '())
800 (byte-compile '())
801 (arrange-preloads '()))
803 (dolist (act actions)
804 (when act
805 (case (car act)
806 ((add-file-autoloads
807 add-to-info-path
808 add-to-load-path)
809 (let*
810 ((deffile-name (second act))
811 (cell-already
812 (assoc deffile-name build-deffiles)))
813 (if cell-already
814 ;;There are already actions on this deffile.
815 ;;Splice this action in.
816 (setcdr cell-already
817 (cons act (cdr cell-already)))
818 ;;There are no actions on this deffile. Add a
819 ;;place for them and include this action.
820 (push (list deffile-name act) build-deffiles))))
821 (preload-file
822 (push act arrange-preloads))
823 (run-tests
824 (push act run-tests))
825 (byte-compile
826 (push act byte-compile)))))
828 (elinstall-make-stages
829 :build-deffiles
830 (elinstall-remove-empty-segs build-deffiles)
831 :run-tests
832 run-tests
833 :byte-compile
834 byte-compile
835 :arrange-preloads
836 arrange-preloads)))
837 ;;;_ . Finding actions
838 ;;;_ , Informational
839 ;;;_ . elinstall-dir-has-info
841 ;;$$IMPROVE ME - Can this test be made more precise?
842 (defun elinstall-dir-has-info (dir)
843 "Return non-nil if DIR has info files in it.
844 DIR should be an absolute path."
846 (string-match "/info/" dir)
847 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
849 ;;;_ , Workers
850 ;;;_ . List of special variables used in this section
851 ;;load-path-element - The relevant element of load-path
852 ;;def-file - The file the autoload definitions etc will go into.
853 ;;add-to-load-path-p - Controls whether to add to load-path.
854 ;;recurse-dirs-p - Whether to recurse into subdirectories.
855 (defconst elinstall-find-actions-control-vars
856 '(add-to-load-path-p recurse-dirs-p compile-p force-recompile-p)
857 "Control special variables that the find-actions tree recognizes" )
858 ;;;_ . elinstall-actions-for-source-file
859 (defun elinstall-actions-for-source-file (filename dir)
860 "Return a list of actions to do for FILENAME in DIR.
861 Special variables are as noted in \"List of special variables\"."
862 (declare (special
863 load-path-element def-file compile-p force-recompile-p))
864 (let
865 ((full-path
866 (expand-file-name filename dir)))
867 (when
868 (and
869 (file-readable-p full-path)
870 (not (auto-save-file-name-p full-path)))
871 ;;$$IMPROVE ME create and use relevant control variables.
872 (let*
874 (visited (get-file-buffer full-path))
875 (buf
876 (or
877 visited
878 ;;Visit the file cheaply.
879 ;;hack-local-variables can give errors.
880 (ignore-errors (autoload-find-file full-path))))
881 (def-file
883 (ignore-errors
884 (with-current-buffer buf
885 (if (local-variable-p 'generated-autoload-file)
886 (elinstall-expand-deffile-name
887 generated-autoload-file)
888 nil)))
889 def-file))
890 ;;Figure out whether to run some actions, by file local vars.
891 (autoloads-p
892 (ignore-errors
893 (with-current-buffer buf
894 (not no-update-autoloads))))
895 (do-compile-p
896 (and
897 (featurep 'byte-compile)
898 (string-match emacs-lisp-file-regexp filename)
899 (ignore-errors
900 (with-current-buffer buf
901 (not no-byte-compile)))
902 (let
903 ((dest (byte-compile-dest-file full-path)))
904 (if (file-exists-p dest)
905 ;; File was already compiled.
906 (or force-recompile-p
907 (file-newer-than-file-p full-path dest))
908 (or compile-p
909 (y-or-n-p (concat "Compile " filename "? "))))))))
911 (prog1
912 (list
913 (if do-compile-p
914 `(byte-compile ,full-path)
915 nil)
916 (if autoloads-p
917 (elinstall-make-autoload-action
918 buf def-file load-path-element full-path)
919 nil))
920 (unless visited (kill-buffer-if-not-modified buf)))))))
921 ;;;_ . elinstall-actions-for-dir
922 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
923 "Make actions for DIR.
924 Recurse just if RECURSE-DIRS-P"
925 (declare (special
926 load-path-element def-file add-to-load-path-p))
927 ;;This does not treat symlinks specially. $$IMPROVE ME it could
928 ;;treat/not treat them conditional on control variables.
929 (let*
931 ;;Relative filenames of the source files. We know our
932 ;;loaddefs.el isn't really source so remove it. We'd have
933 ;;removed it anyways after seeing file local vars.
935 (elisp-source-files
936 (remove def-file
937 (directory-files
938 dirname
939 nil
940 elinstall-elisp-regexp)))
941 ;;Absolute filenames of subdirectories.
942 ;;Don't accept any directories beginning with dot. If user
943 ;;really wants to explore one he can use `(dir ".NAME")'.
944 (sub-dirs
945 (if recurse-dirs-p
946 (delq nil
947 (mapcar
948 #'(lambda (filename)
950 (file-directory-p filename)
951 filename
952 nil))
953 (directory-files
954 dirname t
955 "[^\\.]")))
956 '()))
958 (load-path-here
959 (and
960 elisp-source-files ;;If list is not empty.
961 add-to-load-path-p))
962 (load-path-element
963 (if load-path-here
964 dirname
965 load-path-element)))
967 (append
968 ;;Sometimes arrange to add this directory to load-path.
969 (if load-path-here
970 `((add-to-load-path
971 ,def-file
972 ,load-path-element))
973 '())
975 ;;$$IMPROVE ME - be controlled by a control variable.
976 ;;Sometimes add this directory to info path.
978 (elinstall-dir-has-info dirname)
979 `((add-to-info-path
980 ,def-file
981 "."))
982 '())
984 (apply #'nconc
985 (mapcar
986 #'(lambda (filename)
987 (elinstall-actions-for-source-file
988 filename
989 dirname))
990 elisp-source-files))
992 (if recurse-dirs-p
993 (apply #'nconc
994 (mapcar
995 #'(lambda (filename)
996 (elinstall-find-actions-by-spec-x
998 (expand-file-name
999 filename
1000 dirname)))
1001 sub-dirs))
1002 '()))))
1004 ;;;_ . elinstall-find-actions-by-spec-x
1006 (defun elinstall-find-actions-by-spec-x (spec dir)
1007 "Return a list of actions to do, controlled by SPEC."
1008 (declare (special
1009 load-path-element def-file add-to-load-path-p
1010 recurse-dirs-p))
1012 (if (consp spec)
1013 (case (car spec)
1015 ;;(in FN SPEC)
1016 (let
1017 ((new-dir
1018 (expand-file-name
1019 (second spec)
1020 dir)))
1022 (elinstall-find-actions-by-spec-x
1023 (third spec)
1024 new-dir)))
1026 (all
1027 ;;(all . SPEC*)
1028 (apply #'nconc
1029 (mapcar
1030 #'(lambda (sub-spec)
1031 (elinstall-find-actions-by-spec-x
1032 sub-spec
1033 dir))
1034 (cdr spec))))
1035 (matching
1036 ;;(matching PATTERN SPEC)
1037 (apply #'nconc
1038 (mapcar
1039 #'(lambda (dir)
1040 (elinstall-find-actions-by-spec-x
1041 (third spec)
1042 dir))
1043 (directory-files
1044 dir t (second spec))))
1046 (file
1047 ;;(file FN)
1048 (elinstall-actions-for-source-file
1049 (second spec) dir))
1051 ;;Rather than trying to bind all control variables each time
1052 ;;thru, we use `set' and `unwind-protect'.
1053 (control
1054 ;;control TYPE DISPOSITION SPEC
1055 (let
1056 ((key (second spec))
1057 old-value)
1058 (if (memq key elinstall-find-actions-control-vars)
1059 (unwind-protect
1060 (progn
1061 (set old-value (symbol-value key))
1062 (set key (third spec))
1063 (elinstall-find-actions-by-spec-x
1064 (second spec)
1065 dir))
1066 (set key old-value))
1067 (error "Unrecognized control variable %s" key))))
1070 (dir
1071 ;;(dir FN)
1072 (elinstall-actions-for-dir
1073 (expand-file-name
1074 (second spec)
1075 dir)
1076 nil))
1079 (load-path
1080 ;;(load-path SPEC)
1081 (append
1082 `((add-to-load-path ,def-file ,dir))
1083 (let
1084 ((load-path-element dir))
1085 (elinstall-find-actions-by-spec-x
1086 (second spec)
1087 dir))))
1090 (def-file
1091 ;;(def-file FN ARGS SPEC)
1092 (let
1093 ((def-file
1094 (expand-file-name
1095 (second spec)
1096 dir))
1097 (for-preload (third spec)))
1098 (assert (listp for-preload))
1099 (append
1100 (list
1102 (and for-preload (car for-preload))
1103 `(preload-file
1104 ,(car for-preload)
1105 ,def-file
1106 ,@(cdr for-preload))
1107 '()))
1109 (elinstall-find-actions-by-spec-x
1110 (fourth spec) dir)))))
1113 ;;Single symbols
1114 (case spec
1115 (dir
1116 (elinstall-actions-for-dir dir nil))
1117 ((t)
1118 (elinstall-actions-for-dir dir t)))))
1120 ;;;_ . elinstall-find-actions-by-spec
1121 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1124 (let
1125 ((load-path-element load-path-element)
1126 (def-file def-file)
1127 (add-to-load-path-p t)
1128 (recurse-dirs-p t)
1129 (force-recompile-p nil)
1130 (compile-p t))
1131 (declare (special
1132 load-path-element def-file add-to-load-path-p
1133 recurse-dirs-p force-recompile-p compile-p))
1135 (elinstall-find-actions-by-spec-x
1136 spec dir)))
1138 ;;;_ . high-level work
1139 ;;;_ , elinstall-get-relevant-load-path
1140 (defun elinstall-get-relevant-load-path (actions)
1142 (delq nil
1143 (mapcar
1144 #'(lambda (act)
1145 (case (car act)
1146 (add-to-load-path
1147 (second act))
1148 (t nil)))
1149 actions)))
1150 ;;;_ , elinstall-get-deffile-list
1151 (defun elinstall-get-deffile-list (stages)
1152 "Get a list of deffile names"
1154 (mapcar
1155 #'car
1156 (elinstall-stages->build-deffiles stages)))
1157 ;;;_ , elinstall-x
1158 (defun elinstall-x (dir spec &optional force)
1159 "High-level worker function to install elisp files."
1160 (let*
1162 ;;This is just the default deffile, spec can override it.
1163 (def-file
1164 (elinstall-expand-deffile-name nil))
1165 (actions
1166 (elinstall-find-actions-by-spec
1167 spec
1170 def-file))
1171 (stages (elinstall-segregate-actions actions))
1172 (use-load-path
1173 (elinstall-get-relevant-load-path
1174 actions)))
1176 (elinstall-stage-update-deffiles
1177 (elinstall-stages->build-deffiles stages)
1178 force
1179 use-load-path)
1180 (elinstall-stage-arrange-preloads
1181 (elinstall-stages->arrange-preloads stages)
1182 (elinstall-get-deffile-list stages)
1183 force)
1184 (elinstall-stage-byte-compile
1185 (elinstall-stages->byte-compile stages))
1187 ;;;_ , Entry points
1188 ;;;_ . elinstall
1189 ;;;###autoload
1190 (defun elinstall (project-name path spec &optional force version-string)
1191 "Install elisp files.
1192 They need not be a formal package.
1194 Parameters:
1196 PROJECT-NAME - the name of the project
1198 PATH - Path to the project.
1199 Suggestion: (elinstall-directory-true-name)
1201 SPEC - a spec for the autoloads etc to make. It can be as simple as
1202 \(dir \"\.\") for installing one directory.
1204 If FORCE is t, install a package even if it has already been
1205 installed. Other non-nil cases of FORCE are reserved for future
1206 development."
1208 (when
1210 force
1211 (not (elinstall-already-installed project-name))
1212 (yes-or-no-p (format "Re-install %s? " project-name)))
1213 (elinstall-x
1214 path
1215 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1216 force)
1217 (elinstall-record-installed project-name version-string)))
1221 ;;;_ . elinstall-update-directory-autoloads
1223 ;;The control variables and values of `force' that would stop other
1224 ;;actions don't exist yet. Similarly for
1225 ;;`elinstall-update-file-autoloads'.
1226 ;;;###autoload
1227 (defun elinstall-update-directory-autoloads (dir)
1228 "Update autoloads for directory DIR"
1230 (interactive "DUpdate autoloads for all elisp files from directory: ")
1231 (elinstall-x
1233 `(control compile-p nil
1234 (dir "."))))
1236 ;;;_ . elinstall-update-directory
1237 ;;;###autoload
1238 (defun elinstall-update-directory (dir)
1239 "Update autoloads for directory DIR"
1241 (interactive "DInstall all elisp files from directory: ")
1242 (elinstall-x
1244 '(dir ".")))
1246 ;;;_ . elinstall-update-file-autoloads
1247 ;;;###autoload
1248 (defun elinstall-update-file-autoloads (file)
1249 "Update autoloads for elisp file FILE"
1251 (interactive "fUpdate autoloads for elisp file: ")
1252 (elinstall-x
1253 file
1254 `(control compile-p nil
1255 (file ,file))))
1257 ;;;_ . elinstall-update-file
1258 ;;;###autoload
1259 (defun elinstall-update-file (file)
1260 "Install elisp file FILE"
1262 (interactive "fInstall elisp file: ")
1263 (elinstall-x
1264 file
1265 `(file ,file)))
1267 ;;;_. Footers
1268 ;;;_ , Provides
1270 (provide 'elinstall)
1272 ;;;_ * Local emacs vars.
1273 ;;;_ + Local variables:
1274 ;;;_ + mode: allout
1275 ;;;_ + End:
1277 ;;;_ , End
1278 ;;; elinstall.el ends here