Code towards getting autoload text during action-finding
[elinstall.git] / elinstall.el
blob4dfeca0dc0e0c31b36e53821220fcf5c5d56e2ed
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 ;;;_ . Group
47 (defgroup elinstall
48 '()
49 "Customizations for elinstall"
50 :group 'development)
51 ;;;_ . elinstall-default-priority
52 (defcustom elinstall-default-priority
54 "Default priority for site-start"
55 :group 'elinstall
56 :type 'integer)
57 ;;;_ . elinstall-default-preload-target
58 (defcustom elinstall-default-preload-target
59 "~/.emacs.d/site-start.d/"
60 "Default preload-target for registering autoloads"
61 :group 'elinstall
62 :type
63 '(choice
64 (const "~/.emacs.d/site-start.d/")
65 (const "/etc/emacs/site-start.d/")
66 (directory "" )
67 (const nil)
68 (const 'dot-emacs)))
69 ;;;_ . elinstall-already-installed
70 (defvar elinstall-already-installed
71 '()
72 "(AUTOMATIC) Things that have already been installed.
73 This exists for recording what has been installed.
75 Though it's saved as customizable, user interaction is not
76 contemplated." )
77 ;;;_ , Types
78 ;;;_ . elinstall-stages
79 (defstruct (elinstall-stages
80 (:constructor elinstall-make-stages)
81 (:conc-name elinstall-stages->)
82 (:copier nil))
83 "The elinstall stages"
84 build-deffiles
85 run-tests
86 byte-compile
87 arrange-preloads)
88 ;;;_ , Data
89 ;;;_ . Regular expressions
90 ;;;_ , elinstall-elisp-regexp
91 (defconst elinstall-elisp-regexp
92 (let ((tmp nil))
93 (dolist
94 (suf (get-load-suffixes))
95 (unless (string-match "\\.elc" suf) (push suf tmp)))
96 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
97 "Regular expression that matches elisp files" )
98 ;;;_ , Utilities
99 ;;;_ . elinstall-directory-true-name
100 (defun elinstall-directory-true-name ()
101 "Get the true name of the directory the calling code lives in.
102 CAUTION: This is sensitive to where it's called. That's the point of it."
103 (file-name-directory
104 (if load-file-name
105 (file-truename load-file-name)
106 (file-truename buffer-file-name))))
107 ;;;_ . Checking installedness
108 ;;;_ , elinstall-already-installed
109 (defun elinstall-already-installed (project-name)
110 "Return non-nil if PROJECT-NAME has been installed."
111 (member project-name elinstall-already-installed))
113 ;;;_ , elinstall-record-installed
114 (defun elinstall-record-installed (project-name)
115 "Record that PROJECT-NAME has been installed."
117 (add-to-list 'elinstall-already-installed project-name)
118 (customize-save-variable
119 'elinstall-already-installed
120 elinstall-already-installed
121 "Set by elinstall-record-installed"))
122 ;;;_ . Finding deffiles
123 ;;;_ , elinstall-expand-deffile-name
124 (defun elinstall-expand-deffile-name (deffile)
125 "Expand DEFFILE autoload.el's way."
127 (expand-file-name (or deffile "loaddefs.el")
128 (expand-file-name "lisp"
129 source-directory)))
131 ;;;_ , Work
132 ;;;_ . Doing actions
134 ;;;_ , Doing autoload actions (adapted from autoload.el)
135 ;;;_ . Utilities about the action list
136 ;;;_ , elinstall-remove-autogen-action
137 (defun elinstall-remove-autogen-action (file actions)
138 "Return ACTIONS minus any add-file-autoloads on FILE removed."
140 (delq nil
141 (mapcar
142 #'(lambda (act)
143 (case (car act)
144 (add-file-autoloads
145 (if (equal file (third act))
147 act))
148 (t act)))
149 actions)))
150 ;;;_ , elinstall-get-autogen-action
151 (defun elinstall-get-autogen-action (file actions)
153 (let
154 ((the-act))
155 (dolist (act actions)
156 (case (car act)
157 (add-file-autoloads
158 (when (equal file (third act))
159 (setq the-act act)))))
160 the-act))
161 ;;;_ . elinstall-insert-section-header
162 (defun elinstall-insert-section-header (outbuf form)
163 "Insert the section-header line,
164 which lists the file name and which functions are in it, etc."
165 (insert generate-autoload-section-header)
166 (prin1 form outbuf)
167 (terpri outbuf)
168 ;; Break that line at spaces, to avoid very long lines.
169 ;; Make each sub-line into a comment.
170 (with-current-buffer outbuf
171 (save-excursion
172 (forward-line -1)
173 (while (not (eolp))
174 (move-to-column 64)
175 (skip-chars-forward "^ \n")
176 (or (eolp)
177 (insert "\n" generate-autoload-section-continuation))))))
179 ;;;_ . Making autoloads
180 ;;;_ , elinstall-insert-autoload-section
181 '(elinstall-insert-autoload-section
182 text
183 (list 'autoloads
184 autoloads-done
185 relative-name
186 (autoload-trim-file-name full-name)
187 (nth 5 (file-attributes full-name)))
188 (concat
189 "Generated autoloads from "
190 (autoload-trim-file-name full-name)))
192 (defun elinstall-insert-autoload-section (text form &optional comment-string)
193 "Insert TEXT into current buffer as an autoload section"
195 ;; Insert the section-header line which lists the file name
196 ;; and which functions are in it, etc.
198 (elinstall-insert-section-header (current-buffer) form)
200 (when comment-string
201 (insert ";;; " comment-string "\n"))
202 (insert text)
203 (insert generate-autoload-section-trailer))
205 ;;;_ , elinstall-make-autoload-action
206 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
207 "Return the autoloads for current buffer as a string"
209 ;;We put all the text into a temp buffer, then get that buffer's
210 ;;string.
211 (let
212 ((outbuf
213 (generate-new-buffer " *temp*"))
214 (autoloads-done '())
215 (short-name
216 (buffer-name buf))
217 (relative-name
218 (file-name-sans-extension
219 (file-relative-name
220 full-path
221 load-path-element))))
223 (message "Finding autoloads for %s..." short-name)
224 (with-current-buffer buf
225 (unwind-protect
226 (save-excursion
227 (save-restriction
228 (widen)
229 (goto-char (point-min))
230 (while (not (eobp))
231 (skip-chars-forward " \t\n\f")
232 (cond
233 ((looking-at (regexp-quote generate-autoload-cookie))
234 (search-forward generate-autoload-cookie)
235 (skip-chars-forward " \t")
236 (setq done-any t)
237 (if (eolp)
238 ;; Read the next form and make an autoload.
239 (let* ((form (prog1 (read (current-buffer))
240 (or (bolp) (forward-line 1))))
241 (autoload
242 (make-autoload form relative-name)))
243 (if autoload
244 (push (nth 1 form) autoloads-done)
245 (setq autoload form))
246 (let ((autoload-print-form-outbuf outbuf))
247 (autoload-print-form autoload)))
249 ;; Copy the rest of the line to the output.
250 (princ (buffer-substring
251 (progn
252 ;; Back up over whitespace,
253 ;; to preserve it.
254 (skip-chars-backward " \f\t")
255 (if (= (char-after (1+ (point))) ? )
256 ;; Eat one space.
257 (forward-char 1))
258 (point))
259 (progn (forward-line 1) (point)))
260 outbuf)))
261 ((looking-at ";")
262 ;; Don't read the comment.
263 (forward-line 1))
265 (forward-sexp 1)
266 (forward-line 1)))))
269 (message "Finding autoloads for %s...done" short-name)
271 ;;Return this action. The temp buffer's contents is
272 ;;our final string.
273 `(add-file-autoloads
274 ,def-file
275 ;;load-name relative to a member of load-path.
276 ,relative-name
277 ,load-path-element
278 ,full-path
279 ,(with-current-buffer outbuf (buffer-string))))
281 ;;The unwind-protected part
282 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
285 ;;;_ , elinstall-generate-file-autoloads
286 ;;override to allow slashed load-paths
287 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
288 ;;a new arg.
289 ;;`relative-to' can be:
290 ;; * nil: act as at present. Assume that FILE's immediate directory
291 ;;is in load-path.
292 ;; * t :: use default-directory
293 ;; * a string :: relative to it, as a filename
295 (defun elinstall-generate-file-autoloads (relative-name full-name text)
296 "Insert at point a loaddefs autoload section for FILE.
297 Autoloads are generated for defuns and defmacros in FILE
298 marked by `generate-autoload-cookie' (which see).
299 If FILE is being visited in a buffer, the contents of the buffer
300 are used.
301 Return non-nil in the case where no autoloads were added at point.
303 FULL-NAME is the absolute name of the file.
304 RELATIVE-NAME is its name respective to some component of load-path."
305 (let* ((outbuf (current-buffer))
306 (autoloads-done '())
307 (print-length nil)
308 (print-readably t) ; This does something in Lucid Emacs.
309 (float-output-format nil)
310 (done-any nil)
311 (visited (get-file-buffer full-name))
312 (source-buf
313 (or visited
314 ;; It is faster to avoid visiting the file.
315 (ignore-errors (autoload-find-file full-name))))
316 output-start)
317 (if source-buf
318 (with-current-buffer source-buf
319 (message "Generating autoloads for %s..." relative-name)
320 (setq output-start (with-current-buffer outbuf (point)))
321 (save-excursion
322 (save-restriction
323 (widen)
324 (goto-char (point-min))
325 (while (not (eobp))
326 (skip-chars-forward " \t\n\f")
327 (cond
328 ((looking-at (regexp-quote generate-autoload-cookie))
329 (search-forward generate-autoload-cookie)
330 (skip-chars-forward " \t")
331 (setq done-any t)
332 (if (eolp)
333 ;; Read the next form and make an autoload.
334 (let* ((form (prog1 (read (current-buffer))
335 (or (bolp) (forward-line 1))))
336 (autoload
337 (make-autoload form relative-name)))
338 (if autoload
339 (push (nth 1 form) autoloads-done)
340 (setq autoload form))
341 (let ((autoload-print-form-outbuf outbuf))
342 (autoload-print-form autoload)))
344 ;; Copy the rest of the line to the output.
345 (princ (buffer-substring
346 (progn
347 ;; Back up over whitespace, to preserve it.
348 (skip-chars-backward " \f\t")
349 (if (= (char-after (1+ (point))) ? )
350 ;; Eat one space.
351 (forward-char 1))
352 (point))
353 (progn (forward-line 1) (point)))
354 outbuf)))
355 ((looking-at ";")
356 ;; Don't read the comment.
357 (forward-line 1))
359 (forward-sexp 1)
360 (forward-line 1))))))
362 (when done-any
363 (with-current-buffer outbuf
364 (save-excursion
365 ;; Insert the section-header line which lists the file name
366 ;; and which functions are in it, etc.
367 (goto-char output-start)
368 (elinstall-insert-section-header
369 outbuf
370 (list 'autoloads
371 autoloads-done
372 relative-name
373 (autoload-trim-file-name full-name)
374 (nth 5 (file-attributes full-name))))
376 (insert ";;; Generated autoloads from "
377 (autoload-trim-file-name full-name) "\n"))
378 (insert generate-autoload-section-trailer)))
379 (message "Generating autoloads for %s...done" relative-name))
381 (unless visited
382 ;; We created this buffer, so we should kill it.
383 (kill-buffer (current-buffer)))
384 (message "Could not load %s" relative-name))
386 (not done-any)))
389 ;;;_ , elinstall-deffile-insert-autoloads
390 (defun elinstall-deffile-insert-autoloads (file load-name text)
391 "Update the autoloads for FILE in current buffer.
392 Return FILE if there was no autoload cookie in it, else nil.
394 Current buffer must be a loaddef-style file.
396 LOAD-NAME is the absolute name of the file.
397 RELATIVE-NAME is its name respective to some component of load-path."
398 (let (
399 (found nil)
400 (no-autoloads nil))
402 (save-excursion
403 (save-restriction
404 (widen)
405 (goto-char (point-min))
406 ;; Look for the section for FILE
407 (while (and (not found)
408 (search-forward generate-autoload-section-header nil t))
409 (let ((form (autoload-read-section-header)))
410 (cond
411 ((equal (nth 2 form) file)
412 ;; We found the section for this file.
413 (let ((begin (match-beginning 0)))
414 (progn
415 (search-forward generate-autoload-section-trailer)
416 (delete-region begin (point))
417 (setq found t))))
418 ((string< file (nth 2 form))
419 ;; We've come to a section alphabetically later than
420 ;; FILE. We assume the file is in order and so
421 ;; there must be no section for FILE. We will
422 ;; insert one before the section here.
423 (goto-char (match-beginning 0))
424 (setq found 'new)))))
425 (unless found
426 (progn
427 (setq found 'new)
428 ;; No later sections in the file. Put before the last page.
429 (goto-char (point-max))
430 (search-backward "\f" nil t)))
431 (setq no-autoloads
432 (elinstall-generate-file-autoloads file load-name text))))
434 (if no-autoloads file nil)))
435 ;;;_ . Arranging to add to info-path and load-path
436 ;;;_ , elinstall-generate-add-to-path
437 (defun elinstall-generate-add-to-path (path-element type)
438 "Insert code at point to add PATH-ELEMENT to a path.
439 If TYPE is:
440 * `add-to-load-path', add to load-path
441 * `add-to-info-path', add to Info-default-directory-list
443 Current buffer must be a loaddef-style file."
444 (let ( (path-symbol
445 (case type
446 (add-to-load-path 'load-path)
447 (add-to-info-path 'Info-default-directory-list)
448 (t (error "Type not recognized"))))
449 (description
450 (case type
451 (add-to-load-path "load-path")
452 (add-to-info-path "info-path")))
453 (autoloads-done '())
454 (print-length nil)
455 (print-readably t) ; This does something in Lucid Emacs.
456 (float-output-format nil))
458 (message "Generating %s additions..." description)
461 (elinstall-insert-section-header
462 (current-buffer)
463 (list type (list path-element) nil nil nil))
465 (insert ";;; Generated path addition\n")
467 `(add-to-list ',path-symbol
468 (expand-file-name
469 ,(file-relative-name path-element)
470 (if load-file-name
471 (file-name-directory
472 (file-truename load-file-name)))))
473 (current-buffer))
475 (insert generate-autoload-section-trailer)
476 (message "Generating %s additions...done" description)))
479 ;;;_ , elinstall-deffile-insert-add-to-path
480 (defun elinstall-deffile-insert-add-to-path (path-element type)
481 "Insert code in current buffer to add PATH-ELEMENT to a path.
482 If TYPE is:
483 * `add-to-load-path', add to load-path
484 * `add-to-info-path', add to Info-default-directory-list
486 Current buffer must be a loaddef-style file."
487 (let (
488 (found nil)
489 (no-autoloads nil))
491 (save-excursion
492 (save-restriction
493 (widen)
494 (goto-char (point-min))
495 ;; Look for the section for PATH-ELEMENT
496 (while (and (not found)
497 (search-forward generate-autoload-section-header nil t))
498 (let ((form (autoload-read-section-header)))
499 (cond
500 ((and
501 (equal (nth 0 form) type)
502 (member path-element (nth 1 form)))
504 ;; We found the section for this add.
505 (let ((begin (match-beginning 0)))
506 (progn
507 (search-forward generate-autoload-section-trailer)
508 (delete-region begin (point))
509 (setq found t)))))))
511 (unless found
512 (progn
513 (setq found 'new)
514 ;; No later sections in the file. Put before the last page.
515 (goto-char (point-max))
516 (search-backward "\f" nil t)))
518 (elinstall-generate-add-to-path path-element type)))
520 ;;This never belongs in the no-autoloads section.
521 nil))
522 ;;;_ . elinstall-deffile-insert
524 (defun elinstall-deffile-insert (action)
525 "Insert autoloads etc into current file according to ACTION.
526 The format of ACTION is described in the design docs.
528 Return filename if this action belongs in the no-autoload section."
530 (when action
531 (case (car action)
532 (add-file-autoloads
533 (elinstall-deffile-insert-autoloads
534 (third action)
535 (fifth action)
536 (sixth action)))
538 (add-to-load-path
539 (elinstall-deffile-insert-add-to-path
540 (third action)
541 'add-to-load-path)
542 nil)
544 (add-to-info-path
545 (elinstall-deffile-insert-add-to-path
546 (third action)
547 'add-to-info-path)
548 nil)
550 ((preload-file run-tests byte-compile)
551 (error "This case should not come here.")))))
553 ;;;_ . elinstall-prepare-deffile
554 (defun elinstall-prepare-deffile (deffile)
555 "Try to ensure that DEFFILE is available for receiving autoloads"
557 (autoload-ensure-default-file deffile)
558 (with-current-buffer (find-file-noselect deffile)
561 ;; We must read/write the file without any code conversion,
562 ;; but still decode EOLs.
563 (let ((coding-system-for-read 'raw-text))
565 ;; This is to make generated-autoload-file have Unix EOLs, so
566 ;; that it is portable to all platforms.
567 (setq buffer-file-coding-system 'raw-text-unix))
568 (or (> (buffer-size) 0)
569 (error "Autoloads file %s does not exist" buffer-file-name))
570 (or (file-writable-p buffer-file-name)
571 (error "Autoloads file %s is not writable"
572 buffer-file-name))))
574 ;;;_ . elinstall-update-deffile
576 ;;Adapted from autoload.el `update-directory-autoloads'.
578 (defun elinstall-update-deffile (target actions &optional
579 use-load-path force)
581 Update file TARGET with current autoloads as specified by ACTIONS.
582 Also remove any old definitions pointing to libraries that can no
583 longer be found.
585 ACTIONS must be a list of actions (See the format doc). Each one's
586 filename must be relative to some element of load-path.
588 USE-LOAD-PATH is a list to use as load-path. It should include
589 any new load-path that we are arranging to create. If it's not given,
590 load-path itself is used.
592 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
593 Other non-nil cases of FORCE are reserved for future development.
595 This uses `update-file-autoloads' (which see) to do its work.
596 In an interactive call, you must give one argument, the name
597 of a single directory."
598 (let
600 (use-load-path (or use-load-path load-path))
601 (this-time (current-time))
602 ;;files with no autoload cookies.
603 (no-autoloads nil))
605 (elinstall-prepare-deffile target)
606 (with-current-buffer
607 (find-file-noselect target)
608 (save-excursion
609 (setq actions
610 (elinstall-remove-autogen-action
611 (autoload-trim-file-name target)
612 actions))
614 (goto-char (point-min))
615 (while (search-forward generate-autoload-section-header nil t)
616 (let* ((form (autoload-read-section-header))
617 (file (nth 3 form)))
618 (cond ((and (consp file) (stringp (car file)))
619 ;; This is a list of files that have no
620 ;; autoload cookies.
621 ;; There shouldn't be more than one such entry.
622 ;; Remove the obsolete section.
623 (autoload-remove-section (match-beginning 0))
624 (let ((last-time (nth 4 form)))
625 (dolist (file file)
626 (let ((file-time (nth 5 (file-attributes file))))
627 (when (and file-time
628 (not (time-less-p last-time file-time)))
629 ;; file unchanged
630 (push file no-autoloads)
631 (setq actions
632 (elinstall-remove-autogen-action
633 file actions)))))))
634 ((not (stringp file)))
636 (let
637 ((file-path
638 (locate-library file nil use-load-path)))
639 (cond
640 ;;File doesn't exist, so remove its
641 ;;section.
642 ((not file-path)
643 (autoload-remove-section
644 (match-beginning 0)))
646 ;; File hasn't changed, so do nothing.
647 ((equal
648 (nth 4 form)
649 (nth 5 (file-attributes file-path)))
650 nil)
652 (elinstall-deffile-insert
653 (elinstall-get-autogen-action
654 file actions))))
656 (setq actions
657 (elinstall-remove-autogen-action
658 file actions))))))))
660 ;; Remaining actions have no existing autoload sections yet.
661 (setq no-autoloads
662 (append no-autoloads
663 (delq nil (mapcar #'elinstall-deffile-insert actions))))
664 (when no-autoloads
665 ;; Sort them for better readability.
666 (setq no-autoloads (sort no-autoloads 'string<))
667 ;; Add the `no-autoloads' section.
668 (goto-char (point-max))
669 (search-backward "\f" nil t)
671 (elinstall-insert-section-header
672 (current-buffer)
673 (list 'autoloads nil nil no-autoloads this-time))
674 (insert generate-autoload-section-trailer))
675 (save-buffer))))
677 ;;;_ . elinstall-stage-update-deffiles
678 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
679 "Update any deffiles mentioned in SEGMENT-LIST.
680 FORCE and USE-LOAD-PATH have the same meaning as in
681 `elinstall-update-deffile'.
683 (mapcar
684 #'(lambda (segment)
685 (let*
686 ((deffile (car segment)))
687 (if (stringp deffile)
688 (elinstall-update-deffile deffile (cdr segment) force
689 use-load-path))))
690 segment-list))
692 ;;;_ , Doing actions to arrange preloads
693 ;;;_ . elinstall-symlink-on-emacs-start
694 (defun elinstall-symlink-on-emacs-start
695 (filename target-basename target-dir &optional priority force)
696 "Symlink to TARGET-BASENAME.el in TARGET-DIR
698 If PRIORITY is given, it will be used as the priority prefix,
699 otherwise elinstall-default-priority will be.
700 PRIORITY must be an integer or nil.
701 If FORCE is `t', do it regardless of timestamps etc.
702 Other non-nil cases of FORCE are reserved for future development."
703 (let*
705 (priority (or priority elinstall-default-priority))
706 (target-name-nodir
707 (format
708 "%d%s.el"
709 priority
710 target-basename))
711 (target
712 (expand-file-name target-name-nodir target-dir)))
715 (cond
716 ;;Path should already exist.
717 ((not
718 (file-exists-p target-dir))
719 (message "The target directory doesn't exist."))
720 ;;Target shouldn't already exist, but if force is given, let
721 ;;user override.
722 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
723 ;;do nothing even on force.
724 ((and
725 (file-exists-p target)
727 (not force)
728 (not
729 (yes-or-no-p
730 (format "Really overwrite %s? " target))))
731 (message "File %s already exists" target)))
734 (make-symbolic-link
735 filename
736 target
737 nil)))))
739 ;;;_ . elinstall-add-to-dot-emacs
740 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
741 "Add code to load FILENAME to .emacs.
742 FILENAME should not have an extension"
744 ;;Visit .emacs
745 (with-current-buffer (find-file-noselect dot-emacs-name)
746 (save-excursion
747 ;;add at end of file
748 (goto-char (point-max))
749 (insert "\n;;Added by elinstall")
750 (insert "\n;;Consider using my-site-start to manage .emacs\n")
751 (pp `(load ,filename) (current-buffer))
752 (save-buffer))))
755 ;;;_ . elinstall-arrange-preload
756 ;;;###autoload
757 (defun elinstall-arrange-preload (force filename basename &optional priority)
758 "Arrange for FILENAME to be loaded on emacs start.
759 FORCE has its usual meaning.
760 BASENAME and PRIORITY are used as arguments to
761 `elinstall-symlink-on-emacs-start'.
764 (let
765 ((preload-target elinstall-default-preload-target))
767 ;;Dispatch the possibilities.
768 (cond
769 ((eq preload-target 'dot-emacs)
770 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
771 ((stringp preload-target)
772 (elinstall-symlink-on-emacs-start
773 filename basename preload-target priority force))
774 ((null preload-target)
775 (message "Not arranging for preloads"))
777 (message "I don't recognize that")))))
778 ;;;_ . elinstall-stage-arrange-preloads
779 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
780 "Arrange any preloads mentioned in ACTIONS."
782 (mapcar
783 #'(lambda (act)
784 (case (car act)
785 (preload-file
786 (let*
787 ( (filename
788 (caddr act))
789 (proceed-p
790 (case (second act)
791 ((t) t)
792 ((nil) nil)
793 (if-used
794 (member filename deffiles-used)))))
796 (when proceed-p
797 (apply
798 #'elinstall-arrange-preload
799 force
800 (cddr act)))))
802 (error
803 "elinstall-stage-arrange-preloads: Action not
804 recognized."))) )
805 actions))
808 ;;;_ , Run tests
809 ;;;_ . elinstall-stage-run-tests
810 (defun elinstall-stage-run-tests (actions)
811 "Run any tests mentioned in ACTIONS."
813 (mapcar
814 #'(lambda (act)
815 (case (car act)
816 (run-tests
817 ;;$$WRITE ME - not a high priority right now.
818 nil)
820 (error
821 "elinstall-stage-run-tests: Action not
822 recognized."))) )
823 actions))
826 ;;;_ , Byte compile
827 ;;;_ . elinstall-stage-byte-compile
828 (defun elinstall-stage-byte-compile (actions)
829 "Do any byte-compilation mentioned in ACTIONS."
831 (mapcar
832 #'(lambda (act)
833 (case (car act)
834 ;;$$IMPROVE ME Understand flags to control second
835 ;;argument (whether to load file after
836 ;;compilation)
837 (byte-compile
838 (byte-compile-file (second act)))
840 (error
841 "elinstall-stage-byte-compile: Action not
842 recognized."))) )
843 actions))
844 ;;;_ . Segregating actions
845 ;;;_ , elinstall-remove-empty-segs
846 (defun elinstall-remove-empty-segs (segment-list)
847 "Return SEGMENT-LIST minus any segments that have no actions.
848 Intended only for the deffile stage data."
849 (delq nil
850 (mapcar
851 #'(lambda (segment)
852 (if (cdr segment)
853 segment
854 nil))
855 segment-list)))
857 ;;;_ , elinstall-segregate-actions
858 (defun elinstall-segregate-actions (actions)
859 "Return actions segregated by deffile.
861 Returns a list whose elements are each a cons of:
862 * deffile filename or nil
863 * A list of actions to be done for that deffile."
865 (let
867 (build-deffiles '())
868 (run-tests '())
869 (byte-compile '())
870 (arrange-preloads '()))
872 (dolist (act actions)
873 (when act
874 (case (car act)
875 ((add-file-autoloads
876 add-to-info-path
877 add-to-load-path)
878 (let*
879 ((deffile-name (second act))
880 (cell-already
881 (assoc deffile-name build-deffiles)))
882 (if cell-already
883 ;;There are already actions on this deffile.
884 ;;Splice this action in.
885 (setcdr cell-already
886 (cons act (cdr cell-already)))
887 ;;There are no actions on this deffile. Add a
888 ;;place for them and include this action.
889 (push (list deffile-name act) build-deffiles))))
890 (preload-file
891 (push act arrange-preloads))
892 (run-tests
893 (push act run-tests))
894 (byte-compile
895 (push act byte-compile)))))
897 (elinstall-make-stages
898 :build-deffiles
899 (elinstall-remove-empty-segs build-deffiles)
900 :run-tests
901 run-tests
902 :byte-compile
903 byte-compile
904 :arrange-preloads
905 arrange-preloads)))
906 ;;;_ . Finding actions
907 ;;;_ , Informational
908 ;;;_ . elinstall-dir-has-info
910 ;;$$IMPROVE ME - Can this test be made more precise?
911 (defun elinstall-dir-has-info (dir)
912 "Return non-nil if DIR has info files in it.
913 DIR should be an absolute path."
915 (string-match "/info/" dir)
916 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
918 ;;;_ , Workers
919 ;;;_ . List of special variables used in this section
920 ;;load-path-element - The relevant element of load-path
921 ;;def-file - The file the autoload definitions etc will go into.
922 ;;add-to-load-path-p - Controls whether to add to load-path.
924 ;;;_ . elinstall-actions-for-source-file
925 (defun elinstall-actions-for-source-file (filename dir)
926 "Return a list of actions to do for FILENAME in DIR.
927 Special variables are as noted in \"List of special variables\"."
928 (declare (special
929 load-path-element def-file))
930 (let
931 ((full-path
932 (expand-file-name filename dir)))
933 (when
934 (and
935 (file-readable-p full-path)
936 (not (auto-save-file-name-p full-path)))
937 ;;$$IMPROVE ME create and use relevant control variables.
938 (let*
940 (visited (get-file-buffer full-path))
941 (buf
942 (or
943 visited
944 ;;Visit the file cheaply.
945 ;;hack-local-variables can give errors.
946 (ignore-errors (autoload-find-file full-path))))
947 (def-file
949 (ignore-errors
950 (with-current-buffer buf
951 (if (local-variable-p 'generated-autoload-file)
952 (elinstall-expand-deffile-name
953 generated-autoload-file)
954 nil)))
955 def-file))
956 ;;Figure out whether to run some actions, by file local vars.
957 (autoloads-p
958 (ignore-errors
959 (with-current-buffer buf
960 (not no-update-autoloads))))
961 (compile-p
962 (and
963 (featurep 'byte-compile)
964 (string-match emacs-lisp-file-regexp filename)
965 (ignore-errors
966 (with-current-buffer buf
967 (not no-byte-compile)))
968 (let*
969 ((dest (byte-compile-dest-file full-path))
970 ;;$$PUNT - currently, we wouldn't have
971 ;;gotten here if we weren't intending to do
972 ;;everything so set force variables accordingly.
973 (recompile-p nil)
974 (compile-new t))
975 (if (file-exists-p dest)
976 ;; File was already compiled.
977 (or recompile-p (file-newer-than-file-p full-path dest))
978 (or compile-new
979 (y-or-n-p (concat "Compile " filename "? "))))))))
981 (prog1
982 (list
983 (if compile-p
984 `(byte-compile ,full-path)
985 nil)
986 (if autoloads-p
987 (elinstall-make-autoload-action
988 buf def-file load-path-element full-path)
989 nil))
990 (unless visited (kill-buffer-if-not-modified buf)))))))
992 ;;;_ . elinstall-find-actions-by-spec-x
994 (defun elinstall-find-actions-by-spec-x (spec dir)
995 "Return a list of actions to do, controlled by SPEC."
996 (declare (special
997 load-path-element def-file add-to-load-path-p))
999 (if (consp spec)
1000 ;;$$IMPROVE ME by adding the other cases in the design.
1001 (case (car spec)
1003 (let
1004 ((new-dir
1005 (expand-file-name
1006 (second spec)
1007 dir)))
1009 (elinstall-find-actions-by-spec-x
1010 (third spec)
1011 new-dir)))
1013 (all
1014 (apply #'nconc
1015 (mapcar
1016 #'(lambda (sub-spec)
1017 (elinstall-find-actions-by-spec-x
1018 sub-spec
1019 dir))
1020 (cdr spec))))
1022 (file
1023 (elinstall-actions-for-source-file
1024 filename dir))
1025 ;;$$ADD ME control, rather than trying to bind all control
1026 ;;variables so we can safely bind one, will use set and
1027 ;;unwind-protect.
1029 (dir
1030 (let*
1031 ((dirname
1032 (expand-file-name
1033 (second spec)
1034 dir))
1035 ;;Relative filenames
1036 (elisp-source-files
1037 (directory-files
1038 dirname
1039 nil
1040 elinstall-elisp-regexp))
1041 (load-path-here
1042 (and
1043 elisp-source-files ;;List not empty.
1044 add-to-load-path-p))
1045 (load-path-element
1046 (if load-path-here
1047 dirname
1048 load-path-element)))
1050 (append
1051 ;;$$IMPROVE ME - remove the current deffile from
1052 ;;this list.
1053 ;;Maybe arrange to add this directory to load-path.
1054 (if load-path-here
1055 `((add-to-load-path
1056 ,def-file
1057 ,load-path-element))
1058 '())
1060 ;;$$IMPROVE ME - be controlled by a control variable.
1061 ;;If any info files are present, do add-to-info-path
1062 ;;too.
1064 (elinstall-dir-has-info dirname)
1065 `((add-to-info-path
1066 ,def-file
1067 "."))
1068 '())
1072 ;;$$FIXME Don't do directories, but maybe recurse on
1073 ;;them, if a flag is set.
1074 ;;Maybe could follow/not symlinks similarly.
1075 (apply #'nconc
1076 (mapcar
1077 #'(lambda (filename)
1078 (elinstall-actions-for-source-file
1079 filename
1080 dirname))
1081 elisp-source-files)))))
1083 (load-path
1084 (append
1085 `((add-to-load-path ,def-file ,dir))
1086 (let
1087 ((load-path-element dir))
1088 (elinstall-find-actions-by-spec-x spec dir))))
1091 (def-file
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)))))
1112 ;;$$IMPROVE ME by adding the other cases in the design.
1113 (case spec
1114 (t))))
1115 ;;;_ . elinstall-find-actions-by-spec
1116 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1119 (let
1120 ((load-path-element load-path-element)
1121 (def-file def-file)
1122 (add-to-load-path-p t))
1123 (declare (special
1124 load-path-element def-file add-to-load-path-p))
1126 (elinstall-find-actions-by-spec-x
1127 spec dir)))
1129 ;;;_ . high-level work
1130 ;;;_ , elinstall-get-relevant-load-path
1131 (defun elinstall-get-relevant-load-path (actions)
1133 (delq nil
1134 (mapcar
1135 #'(lambda (act)
1136 (case (car act)
1137 (add-to-load-path
1138 (second act))
1139 (t nil)))
1140 actions)))
1141 ;;;_ , elinstall-get-deffile-list
1142 (defun elinstall-get-deffile-list (stages)
1143 "Get a list of deffile names"
1145 (mapcar
1146 #'car
1147 (elinstall-stages->build-deffiles stages)))
1149 ;;;_ , elinstall-x
1150 (defun elinstall-x (dir spec &optional force)
1152 (let*
1154 ;;This is just the default deffile, spec can override it.
1155 (def-file
1156 (elinstall-expand-deffile-name nil))
1157 (actions
1158 (elinstall-find-actions-by-spec
1159 spec
1162 def-file))
1163 (stages (elinstall-segregate-actions actions))
1164 (use-load-path
1165 (elinstall-get-relevant-load-path
1166 actions)))
1168 (elinstall-stage-update-deffiles
1169 (elinstall-stages->build-deffiles stages)
1170 force
1171 use-load-path)
1172 (elinstall-stage-arrange-preloads
1173 (elinstall-stages->arrange-preloads stages)
1174 (elinstall-get-deffile-list stages)
1175 force)
1176 (elinstall-stage-byte-compile
1177 (elinstall-stages->byte-compile stages))
1179 ;;;_ , Entry points
1180 ;;;_ . elinstall
1181 ;;;###autoload
1182 (defun elinstall (project-name path spec &optional force)
1183 "Install elisp files.
1184 They need not be a formal package.
1186 Parameters:
1188 PROJECT-NAME - the name of the project
1190 PATH - Path to the project.
1191 Suggestion: (elinstall-directory-true-name)
1193 SPEC - a spec for the autoloads etc to make. It can be as simple as
1194 \(dir \"\.\") for installing one directory.
1196 If FORCE is t, install a package even if it has already been
1197 installed. Other non-nil cases of FORCE are reserved for future
1198 development."
1200 (when
1201 (and
1202 (or
1203 force
1204 (not (elinstall-already-installed project-name)))
1205 (yes-or-no-p (format "Re-install %s? " project-name)))
1206 (elinstall-x
1207 path
1208 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1209 force)
1210 (elinstall-record-installed project-name)))
1214 ;;;_ . elinstall-update-directory-autoloads
1215 ;;$$SPLIT ME - one to just classically update autoloads, one to
1216 ;;install a file.
1217 ;;$$TEST ME
1218 ;;;###autoload
1219 (defun elinstall-update-directory-autoloads (dir)
1222 (interactive "DInstall all elisp files from directory: ")
1225 (let
1226 ((def-file-name
1227 (elinstall-expand-deffile-name
1228 generated-autoload-file)))
1230 (elinstall-x
1232 `(def-file ,def-file-name (nil) (dir ".")))))
1236 ;;;_ . elinstall-update-file-autoloads
1237 ;;$$SPLIT ME - one to just classically update autoloads, one to
1238 ;;install a file.
1239 ;;$$TEST ME
1240 ;;;###autoload
1241 (defun elinstall-update-file-autoloads (file)
1244 (interactive "fInstall elisp file: ")
1245 (let
1246 ((def-file-name
1247 ;;This is the default. File local vars can override it.
1248 (elinstall-expand-deffile-name
1249 generated-autoload-file)))
1250 (elinstall-x
1251 file
1252 `(def-file ,def-file-name (nil) (file ,file)))))
1253 ;;;_. Footers
1254 ;;;_ , Provides
1256 (provide 'elinstall)
1258 ;;;_ * Local emacs vars.
1259 ;;;_ + Local variables:
1260 ;;;_ + mode: allout
1261 ;;;_ + End:
1263 ;;;_ , End
1264 ;;; elinstall.el ends here