Copied code into elinstall-generate-add-to-path. Changed docstrings.
[elinstall.git] / elinstall.el
blob603201220ab83e5bc87646a0a6b635cbf3adec5e
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-link-on-emacs-start - 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 'dot-emacs)))
68 (defcustom elinstall-already-installed
69 '()
70 "Things that have already been installed.
71 This exists for recording what has been installed. User interaction is not
72 contemplated at this time." )
74 ;;;_ , Data
75 ;;;_ . Regular expressions
76 ;;;_ , elinstall-elisp-regexp
77 (defconst elinstall-elisp-regexp
78 (let ((tmp nil))
79 (dolist
80 (suf (get-load-suffixes))
81 (unless (string-match "\\.elc" suf) (push suf tmp)))
82 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
83 "Regular expression that matches elisp files" )
85 ;;;_ , Utilities
86 ;;;_ . elinstall-directory-true-name
87 (defun elinstall-directory-true-name ()
88 "Get the true name of the directory the calling code lives in.
89 CAUTION: This is sensitive to where it's called. That's the point of it."
90 (file-name-directory
91 (if load-file-name
92 (file-truename load-file-name)
93 (file-truename buffer-file-name))))
94 ;;;_ . Checking installedness
95 ;;;_ , elinstall-already-installed
96 (defun elinstall-already-installed (project-name)
97 "Return non-nil if PROJECT-NAME has been installed."
98 (member project-name elinstall-already-installed))
100 ;;;_ , elinstall-record-installed
101 (defun elinstall-record-installed (project-name)
102 "Record that PROJECT-NAME has been installed."
104 (add-to-list 'elinstall-already-installed project-name)
105 (customize-save-variable
106 'elinstall-already-installed
107 elinstall-already-installed
108 "Set by elinstall-record-installed"))
109 ;;;_ , Work
110 ;;;_ . Doing actions
112 ;;;_ , Doing autoload actions (All adapted from autoload.el)
113 ;;;_ . Utilities about the action list
114 ;;;_ , elinstall-remove-autogen-action
115 (defun elinstall-remove-autogen-action (file actions)
116 "Return ACTIONS minus any add-file-autoloads on FILE removed."
118 (delq nil
119 (mapcar
120 #'(lambda (act)
121 (case (car act)
122 (add-file-autoloads
123 (if (equal file (third act))
125 act))))
126 actions)))
127 ;;;_ , elinstall-get-autogen-action
128 (defun elinstall-get-autogen-action (file actions)
130 (let
131 ((the-act))
132 (dolist (act actions)
133 (case (car act)
134 (add-file-autoloads
135 (when (equal file (third act))
136 (setq the-act act)))))
137 the-act))
139 ;;;_ . Making autoloads
140 ;;;_ , elinstall-generate-file-autoloads
141 ;;override to allow slashed load-paths
142 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
143 ;;a new arg.
144 ;;`relative-to' can be:
145 ;; * nil: act as at present. Assume that FILE's immediate directory
146 ;;is in load-path.
147 ;; * t :: use default-directory
148 ;; * a string :: relative to it, as a filename
150 (defun elinstall-generate-file-autoloads (relative-name full-name)
151 "Insert at point a loaddefs autoload section for FILE.
152 Autoloads are generated for defuns and defmacros in FILE
153 marked by `generate-autoload-cookie' (which see).
154 If FILE is being visited in a buffer, the contents of the buffer
155 are used.
156 Return non-nil in the case where no autoloads were added at point.
158 FULL-NAME is the absolute name of the file.
159 RELATIVE-NAME is its name respective to some component of load-path."
160 (let ((outbuf (current-buffer))
161 (autoloads-done '())
162 (print-length nil)
163 (print-readably t) ; This does something in Lucid Emacs.
164 (float-output-format nil)
165 (done-any nil)
166 (visited (get-file-buffer full-name))
167 output-start)
169 ;;Older code massaged `file' but we take both `relative-name' and
170 ;;`full-name', so we don't.
172 (with-current-buffer (or visited
173 ;; It is faster to avoid visiting the file.
174 (autoload-find-file full-name))
175 ;;$$MOVE ME - this should be checked in action-finding.
176 ;; Obey the no-update-autoloads file local variable.
177 (unless no-update-autoloads
178 (message "Generating autoloads for %s..." relative-name)
179 (setq output-start (with-current-buffer outbuf (point)))
180 (save-excursion
181 (save-restriction
182 (widen)
183 (goto-char (point-min))
184 (while (not (eobp))
185 (skip-chars-forward " \t\n\f")
186 (cond
187 ((looking-at (regexp-quote generate-autoload-cookie))
188 (search-forward generate-autoload-cookie)
189 (skip-chars-forward " \t")
190 (setq done-any t)
191 (if (eolp)
192 ;; Read the next form and make an autoload.
193 (let* ((form (prog1 (read (current-buffer))
194 (or (bolp) (forward-line 1))))
195 (autoload (make-autoload form load-name)))
196 (if autoload
197 (push (nth 1 form) autoloads-done)
198 (setq autoload form))
199 (let ((autoload-print-form-outbuf outbuf))
200 (autoload-print-form autoload)))
202 ;; Copy the rest of the line to the output.
203 (princ (buffer-substring
204 (progn
205 ;; Back up over whitespace, to preserve it.
206 (skip-chars-backward " \f\t")
207 (if (= (char-after (1+ (point))) ? )
208 ;; Eat one space.
209 (forward-char 1))
210 (point))
211 (progn (forward-line 1) (point)))
212 outbuf)))
213 ((looking-at ";")
214 ;; Don't read the comment.
215 (forward-line 1))
217 (forward-sexp 1)
218 (forward-line 1))))))
220 (when done-any
221 (with-current-buffer outbuf
222 (save-excursion
223 ;; Insert the section-header line which lists the file name
224 ;; and which functions are in it, etc.
225 (goto-char output-start)
226 (autoload-insert-section-header
227 outbuf autoloads-done relative-name full-name
228 (nth 5 (file-attributes full-name)))
229 (insert ";;; Generated autoloads from "
230 (autoload-trim-file-name full-name) "\n"))
231 (insert generate-autoload-section-trailer)))
232 (message "Generating autoloads for %s...done" relative-name))
233 (or visited
234 ;; We created this buffer, so we should kill it.
235 (kill-buffer (current-buffer))))
236 (not done-any)))
239 ;;;_ , elinstall-deffile-insert-autoloads
240 (defun elinstall-deffile-insert-autoloads (file load-name)
241 "Update the autoloads for FILE in current buffer.
242 Return FILE if there was no autoload cookie in it, else nil.
244 Current buffer must be a loaddef-style file.
246 LOAD-NAME is the absolute name of the file.
247 RELATIVE-NAME is its name respective to some component of load-path."
248 (let (
249 (found nil)
250 (no-autoloads nil))
252 (save-excursion
253 (save-restriction
254 (widen)
255 (goto-char (point-min))
256 ;; Look for the section for FILE
257 (while (and (not found)
258 (search-forward generate-autoload-section-header nil t))
259 (let ((form (autoload-read-section-header)))
260 (cond
261 ((equal (nth 2 form) file)
262 ;; We found the section for this file.
263 (let ((begin (match-beginning 0)))
264 (progn
265 (search-forward generate-autoload-section-trailer)
266 (delete-region begin (point))
267 (setq found t))))
268 ((string< file (nth 2 form))
269 ;; We've come to a section alphabetically later than
270 ;; FILE. We assume the file is in order and so
271 ;; there must be no section for FILE. We will
272 ;; insert one before the section here.
273 (goto-char (match-beginning 0))
274 (setq found 'new)))))
275 (unless found
276 (progn
277 (setq found 'new)
278 ;; No later sections in the file. Put before the last page.
279 (goto-char (point-max))
280 (search-backward "\f" nil t)))
281 (setq no-autoloads
282 (elinstall-generate-file-autoloads file load-name))))
284 (if no-autoloads file nil)))
285 ;;;_ . Arranging to add to info-path and load-path
286 ;;;_ , elinstall-generate-add-to-path
287 (defun elinstall-generate-add-to-path (path-element type)
288 "Insert code at point to add PATH-ELEMENT to a path.
289 If TYPE is:
290 * `add-to-load-path', add to load-path
291 * `add-to-info-path', add to Info-default-directory-list
293 Current buffer must be a loaddef-style file."
294 (let ((outbuf (current-buffer))
295 (autoloads-done '())
296 (print-length nil)
297 (print-readably t) ; This does something in Lucid Emacs.
298 (float-output-format nil)
299 (done-any nil)
300 (visited (get-file-buffer full-name))
301 output-start)
303 ;;Older code massaged `file' but we take both `relative-name' and
304 ;;`full-name', so we don't.
306 (with-current-buffer (or visited
307 ;; It is faster to avoid visiting the file.
308 (autoload-find-file full-name))
309 ;;$$MOVE ME - this should be checked in action-finding.
310 ;; Obey the no-update-autoloads file local variable.
311 (unless no-update-autoloads
312 (message "Generating autoloads for %s..." relative-name)
313 (setq output-start (with-current-buffer outbuf (point)))
314 (save-excursion
315 (save-restriction
316 (widen)
317 (goto-char (point-min))
318 (while (not (eobp))
319 (skip-chars-forward " \t\n\f")
320 (cond
321 ((looking-at (regexp-quote generate-autoload-cookie))
322 (search-forward generate-autoload-cookie)
323 (skip-chars-forward " \t")
324 (setq done-any t)
325 (if (eolp)
326 ;; Read the next form and make an autoload.
327 (let* ((form (prog1 (read (current-buffer))
328 (or (bolp) (forward-line 1))))
329 (autoload (make-autoload form load-name)))
330 (if autoload
331 (push (nth 1 form) autoloads-done)
332 (setq autoload form))
333 (let ((autoload-print-form-outbuf outbuf))
334 (autoload-print-form autoload)))
336 ;; Copy the rest of the line to the output.
337 (princ (buffer-substring
338 (progn
339 ;; Back up over whitespace, to preserve it.
340 (skip-chars-backward " \f\t")
341 (if (= (char-after (1+ (point))) ? )
342 ;; Eat one space.
343 (forward-char 1))
344 (point))
345 (progn (forward-line 1) (point)))
346 outbuf)))
347 ((looking-at ";")
348 ;; Don't read the comment.
349 (forward-line 1))
351 (forward-sexp 1)
352 (forward-line 1))))))
354 (when done-any
355 (with-current-buffer outbuf
356 (save-excursion
357 ;; Insert the section-header line which lists the file name
358 ;; and which functions are in it, etc.
359 (goto-char output-start)
360 (autoload-insert-section-header
361 outbuf autoloads-done relative-name full-name
362 (nth 5 (file-attributes full-name)))
363 (insert ";;; Generated autoloads from "
364 (autoload-trim-file-name full-name) "\n"))
365 (insert generate-autoload-section-trailer)))
366 (message "Generating autoloads for %s...done" relative-name))
367 (or visited
368 ;; We created this buffer, so we should kill it.
369 (kill-buffer (current-buffer))))
370 (not done-any)))
372 ;;;_ , elinstall-deffile-insert-add-to-path
373 (defun elinstall-deffile-insert-add-to-path (path-element type)
374 "Insert code in current buffer to add PATH-ELEMENT to a path.
375 If TYPE is:
376 * `add-to-load-path', add to load-path
377 * `add-to-info-path', add to Info-default-directory-list
379 Current buffer must be a loaddef-style file.
381 (let (
382 (found nil)
383 (no-autoloads nil))
385 (save-excursion
386 (save-restriction
387 (widen)
388 (goto-char (point-min))
389 ;; Look for the section for PATH-ELEMENT
390 (while (and (not found)
391 (search-forward generate-autoload-section-header nil t))
392 (let ((form (autoload-read-section-header)))
393 (cond
394 ((and
395 (equal (nth 0 form) type)
396 (member (nth 1 form) path-element))
398 ;; We found the section for this add.
399 (let ((begin (match-beginning 0)))
400 (progn
401 (search-forward generate-autoload-section-trailer)
402 (delete-region begin (point))
403 (setq found t)))))))
405 (unless found
406 (progn
407 (setq found 'new)
408 ;; No later sections in the file. Put before the last page.
409 (goto-char (point-max))
410 (search-backward "\f" nil t)))
412 (elinstall-generate-add-to-path file load-name)))
414 ;;This never belongs in the no-autoloads section.
415 nil))
417 ;;;_ . elinstall-deffile-insert
419 (defun elinstall-deffile-insert (action)
420 "Insert autoloads etc into current file according to ACTION.
421 The format of ACTION is described in the design docs.
423 Return filename if this action belongs in the no-autoload section."
425 (when action
426 (case (car action)
427 (add-file-autoloads
428 (elinstall-deffile-insert-autoloads
429 (third action)
430 (fifth action)))
432 (add-to-load-path
433 (elinstall-deffile-insert-add-to-path
434 (second action)
435 'add-to-load-path)
436 nil)
438 ;;Similar, but for info-path.
439 (add-to-info-path
440 (elinstall-deffile-insert-add-to-path
441 (second action)
442 'add-to-info-path)
443 nil)
445 (preload-file
446 (error "This case should not come here.")))))
448 ;;;_ . elinstall-prepare-deffile
449 (defun elinstall-prepare-deffile (deffile)
450 "Try to ensure that DEFFILE is available for receiving autoloads"
452 (autoload-ensure-default-file deffile)
453 (with-current-buffer (find-file-noselect deffile)
456 ;; We must read/write the file without any code conversion,
457 ;; but still decode EOLs.
458 (let ((coding-system-for-read 'raw-text))
460 ;; This is to make generated-autoload-file have Unix EOLs, so
461 ;; that it is portable to all platforms.
462 (setq buffer-file-coding-system 'raw-text-unix))
463 (or (> (buffer-size) 0)
464 (error "Autoloads file %s does not exist" buffer-file-name))
465 (or (file-writable-p buffer-file-name)
466 (error "Autoloads file %s is not writable"
467 buffer-file-name))))
469 ;;;_ . elinstall-update-deffile
471 ;;Adapted from autoload.el `update-directory-autoloads'.
472 ;;Still being adapted:
474 ;; * Still need to treat add-to-info-path and
475 ;;add-to-load-path. Both recognize them and insert them.
476 ;; * Adapt `elinstall-update-file-autoloads' to understand actions.
478 ;; * Finding "file" among actions is rickety. Maybe knowing the
479 ;; respective load-path element would help.
481 (defun elinstall-update-deffile (target actions &optional
482 use-load-path force)
484 Update file TARGET with current autoloads as specified by ACTIONS.
485 Also remove any old definitions pointing to libraries that can no
486 longer be found.
488 ACTIONS must be a list of actions (See the format doc). Each one's
489 filename must be relative to some element of load-path.
491 USE-LOAD-PATH is a list to use as load-path. It should include
492 any new load-path that we are arranging to create. If it's not given,
493 load-path itself is used.
495 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
496 Other non-nil cases of FORCE are reserved for future development.
498 This uses `update-file-autoloads' (which see) to do its work.
499 In an interactive call, you must give one argument, the name
500 of a single directory."
501 (let
503 (use-load-path (or use-load-path load-path))
504 (this-time (current-time))
505 ;;files with no autoload cookies.
506 (no-autoloads nil))
508 (elinstall-prepare-deffile target)
509 (with-current-buffer
510 (find-file-noselect target)
511 (save-excursion
512 (setq actions
513 (elinstall-remove-autogen-action
514 (autoload-trim-file-name target)
515 actions))
517 (goto-char (point-min))
518 (while (search-forward generate-autoload-section-header nil t)
519 (let* ((form (autoload-read-section-header))
520 (file (nth 3 form)))
521 (cond ((and (consp file) (stringp (car file)))
522 ;; This is a list of files that have no
523 ;; autoload cookies.
524 ;; There shouldn't be more than one such entry.
525 ;; Remove the obsolete section.
526 (autoload-remove-section (match-beginning 0))
527 (let ((last-time (nth 4 form)))
528 (dolist (file file)
529 (let ((file-time (nth 5 (file-attributes file))))
530 (when (and file-time
531 (not (time-less-p last-time file-time)))
532 ;; file unchanged
533 (push file no-autoloads)
534 (setq actions
535 (elinstall-remove-autogen-action
536 file actions)))))))
537 ((not (stringp file)))
539 (let
540 ((file-path
541 (locate-library file nil use-load-path)))
542 (cond
543 ;;File doesn't exist, so remove its
544 ;;section.
545 ((not file-path)
546 (autoload-remove-section
547 (match-beginning 0)))
549 ;; File hasn't changed, so do nothing.
550 ((equal
551 (nth 4 form)
552 (nth 5 (file-attributes file-path)))
553 nil)
555 (elinstall-deffile-insert
556 (elinstall-get-autogen-action
557 file actions))))
559 (setq actions
560 (elinstall-remove-autogen-action
561 file actions))))))))
563 ;; Remaining actions have no existing autoload sections yet.
564 (setq no-autoloads
565 (append no-autoloads
566 (delq nil (mapcar #'elinstall-deffile-insert actions))))
567 (when no-autoloads
568 ;; Sort them for better readability.
569 (setq no-autoloads (sort no-autoloads 'string<))
570 ;; Add the `no-autoloads' section.
571 (goto-char (point-max))
572 (search-backward "\f" nil t)
573 (autoload-insert-section-header
574 (current-buffer) nil nil no-autoloads this-time)
575 (insert generate-autoload-section-trailer))
576 (save-buffer))))
579 ;;;_ , Doing actions to arrange preloads
580 ;;;_ . elinstall-insert-add-to-path
581 (defun elinstall-insert-add-to-path (new path-sym)
582 "Insert code to add NEW to PATH-SYM.
583 Insert this at point in current buffer."
584 (insert "\n")
586 `(add-to-list ',path-sym
587 (expand-file-name ,new
588 (if load-file-name
589 (file-name-directory
590 (file-truename load-file-name)))))
591 (current-buffer)))
593 ;;;_ . elinstall-insert-add-to-load-path
594 (defun elinstall-insert-add-to-load-path (new)
595 "Insert code to add NEW to load-path.
596 Insert this at point in current buffer."
597 (elinstall-insert-add-to-path new 'load-path))
599 ;;;_ . elinstall-insert-add-to-info-path
600 (defun elinstall-insert-add-to-info-path (new)
601 "Insert code to add NEW to info-path.
602 Insert this at point in current buffer."
603 (elinstall-insert-add-to-path new 'Info-default-directory-list))
605 ;;;_ . elinstall-symlink-on-emacs-start
606 (defun elinstall-symlink-on-emacs-start
607 (filename target-basename target-dir &optional priority force)
608 "Symlink to TARGET-BASENAME.el in TARGET-DIR
610 If PRIORITY is given, it will be used as the priority prefix,
611 otherwise elinstall-default-priority will be.
612 PRIORITY must be an integer or nil.
613 If FORCE is `t', do it regardless of timestamps etc.
614 Other non-nil cases of FORCE are reserved for future development."
615 (let*
617 (priority (or priority elinstall-default-priority))
618 (target-name-nodir
619 (format
620 "%d%s.el"
621 priority
622 target-basename))
623 (target
624 (expand-file-name target-name-nodir target-dir)))
627 (cond
628 ;;Path should already exist.
629 ((not
630 (file-exists-p target-dir))
631 (message "The target directory doesn't exist."))
632 ;;Target shouldn't already exist, but if force is given, let
633 ;;user override.
634 ((and
635 (file-exists-p target)
637 (not force)
638 (not
639 (yes-or-no-p
640 (format "Really overwrite %s? " project-name))))
641 (message "No symlink made to %s" target)))
644 (make-symbolic-link
645 filename
646 target
647 nil)))))
649 ;;;_ . elinstall-add-to-dot-emacs
650 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
651 "Add code to load FILENAME to .emacs.
652 FILENAME should not have an extension"
654 ;;Visit .emacs
655 (with-current-buffer (find-file-noselect dot-emacs-name)
656 (save-excursion
657 ;;add at end of file
658 (goto-char (point-max))
659 (insert "\n;;Added by elinstall")
660 (insert "\n;;Consider using my-site-start to manage .emacs\n")
661 (pp `(load ,filename) (current-buffer))
662 (save-buffer))))
665 ;;;_ . elinstall-link-on-emacs-start
666 ;;;###autoload
667 (defun elinstall-link-on-emacs-start (filename force basename priority)
670 ;;Figure out parameters, using defaults when needed.
671 (let*
672 ( (preload-target elinstall-default-preload-target))
674 ;;Dispatch the possibilities.
675 (cond
676 ((eq preload-target 'dot-emacs)
677 (elinstall-add-to-dot-emacs "~/.emacs" filename))
678 ((stringp preload-target)
679 (elinstall-symlink-on-emacs-start
680 filename basename preload-target priority force))
683 (message "I don't recognize that")))))
685 ;;;_ , Cleanup actions
686 ;;Nothing yet. This will be another type of action.
688 ;;;_ . Segregating actions
689 ;;;_ , elinstall-segregate-actions
690 (defun elinstall-segregate-actions (actions)
691 "Return actions segregated by deffile.
693 Returns a list whose elements are each a cons of:
694 * deffile filename or nil
695 * A list of actions to be done for that deffile."
697 (let
698 ((segment-list '()))
699 (dolist (act actions)
700 (when act
701 (let*
702 ( (deffile-name
703 (case (car act)
704 ((add-file-autoloads
705 add-to-info-path
706 add-to-load-path)
707 (second act))
708 (preload-file nil)))
710 (cell (assoc deffile-name segment-list)))
711 (if cell
712 (setcdr cell (cons act (cdr cell)))
713 (setq segment-list
714 (cons
715 (cons
716 deffile-name
717 (list act))
718 segment-list))))))
719 segment-list))
723 ;;;_ . Finding actions
724 ;;;_ , Treating the parameter list
725 ;;;_ . elinstall-add-parameter
726 (defun elinstall-add-parameter (alist key new-value)
727 "Add a new value for KEY to ALIST"
729 (cons
730 (cons key new-value)
731 (assq-delete-all key (copy-list alist))))
733 ;;;_ . elinstall-get-parameter
734 (defun elinstall-get-parameter (alist key)
735 "Get the value of KEY from ALIST"
737 (cdr (assq key alist)))
738 ;;;_ . elinstall-expand-file-name
739 ;;$$OBSOLETE
741 (defun elinstall-expand-file-name (filename alist)
742 "Expand FILENAME by the value of `path' in ALIST"
743 (expand-file-name
744 filename
745 (elinstall-get-parameter alist 'path)))
746 ;;;_ , Finding deffiles
747 ;;;_ . elinstall-expand-deffile-name
748 (defun elinstall-expand-deffile-name (deffile)
749 "Expand DEFFILE autoload.el's way."
751 (expand-file-name (or deffile "loaddefs.el")
752 (expand-file-name "lisp"
753 source-directory)))
755 ;;;_ . elinstall-maybe-get-deffile
756 (defun elinstall-maybe-get-deffile (file)
757 "If FILE defined `generated-autoload-file', return it.
758 Otherwise return nil.
759 Return it as an absolute filename."
761 (save-excursion
762 ;;$$FIXME load buffer if it's not already loaded
763 (let*
764 ((existing-buffer (get-file-buffer file)))
766 ;; We want to get a value for generated-autoload-file from
767 ;; the local variables section if it's there.
768 ;;But if it's not loaded, we don't? Maybe should use
769 ;; `autoload-find-file' and load it.
770 (if existing-buffer
771 (set-buffer existing-buffer))
772 (if (local-variable-p 'generated-autoload-file)
773 (elinstall-expand-deffile-name
774 generated-autoload-file)
775 nil))))
779 ;;;_ , elinstall-find-actions-by-spec
781 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
782 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
784 LOAD-PATH-ELEMENT is the conceptual element of load-path that
785 surrounds PATH. It may not yet have been added to load-path."
786 (if (consp spec)
787 ;;$$IMPROVE ME by adding the other cases in the design.
788 (case (car spec)
790 (let
791 ((new-path
792 (expand-file-name
793 (second spec)
794 path)))
796 (elinstall-find-actions-by-spec
797 (third spec)
798 load-path-element
799 new-path
800 parameters)))
802 (all
803 (apply #'append
804 (mapcar
805 #'(lambda (sub-spec)
806 (elinstall-find-actions-by-spec
807 sub-spec
808 load-path-element
809 path
810 parameters))
811 (cdr spec))))
812 ;;$$WRITEME
813 (file
816 (dir
817 (let
818 ((dirname
819 (expand-file-name
820 (second spec)
821 path))
822 (load-path-here
823 (not
824 (elinstall-get-parameter
825 parameters 'block-add-to-load-path))))
826 (cons
827 ;;$$IMPROVE ME
828 ;;Do this only if there are loadable files.
829 (if load-path-here
830 `(add-to-load-path
831 ,(elinstall-get-parameter
832 parameters 'def-file)
833 ,dirname)
834 '())
835 ;;$$IMPROVE ME
836 ;;Do add-to-info-path too. But test if there are
837 ;;any info files present.
839 ;;$$IMPROVE ME
840 ;; We want to get a value for generated-autoload-file
841 ;; from the local variables section if it's there.
842 ;;Use `elinstall-maybe-get-deffile'
843 ;; Otherwise we'll use `def-file' in parameters.
845 ;;$$FIXME This isn't quite right. If directory
846 ;;itself is not in load-path, this will be wrong.
847 ;;Gotta know where our encompassing part of
848 ;;load-path is.
850 ;;$$ENCAP ME This should be shared with the
851 ;;treatment of (file FN)
853 ;;$$FIXME Don't do directories, but maybe recurse on
854 ;;them, if a flag is set. And since we definitely
855 ;;have a load-path element here,
856 ;;'block-add-to-load-path according to a parameter.
857 ;;Maybe could follow/not symlinks similarly.
858 (mapcar
859 #'(lambda (filename)
860 (let
861 ((full-path
862 (expand-file-name filename dirname)))
863 `(add-file-autoloads
864 ,(elinstall-get-parameter
865 parameters 'def-file)
866 ,(file-name-sans-extension
867 (file-relative-name
868 full-path
869 load-path-element))
870 ,load-path-element ;;Is this still used?
871 ,full-path)))
873 (directory-files
874 dirname
875 nil ;;Relative filenames
876 elinstall-elisp-regexp)))))
878 (def-file
879 (let
880 ((new-def-file
881 (expand-file-name
882 (second spec)
883 path)))
884 (elinstall-find-actions-by-spec
885 (third spec)
886 load-path-element
887 path
888 (elinstall-add-parameter parameters
889 'def-file new-def-file)))))
891 ;;$$IMPROVE ME by adding the other cases in the design.
892 (case spec
893 (t))))
894 ;;;_ . high-level work
895 ;;;_ , elinstall-get-relevant-load-path
896 (defun elinstall-get-relevant-load-path (actions)
898 (delq nil
899 (mapcar
900 #'(lambda (act)
901 (case (car act)
902 (add-to-load-path
903 (second act))
904 (t nil)))
905 actions)))
907 ;;;_ , elinstall-do-segment
908 (defun elinstall-do-segment (segment force use-load-path)
909 "Do all the actions in SEGMENT.
910 FORCE has its usual meaning.
911 USE-LOAD-PATH is the effective load-path."
913 ;;$$IMPROVE ME - this will have to know the additions to load-path.
914 (let*
915 ((deffile (car segment)))
916 (if (stringp deffile)
917 (elinstall-update-deffile deffile (cdr segment) force
918 use-load-path)
919 ;;Do other actions: link-in actions and cleanups.
920 (mapcar
921 #'(lambda (act)
922 ;;$$FIXME Distinguish link-in and cleanup actions.
923 (apply
924 #'elinstall-link-on-emacs-start
925 force
926 (cdr act)))
927 (cdr segment)))))
930 ;;;_ . Overall work
931 ;;;_ , elinstall-x
932 (defun elinstall-x (dir spec &optional preload-target force)
934 (let*
935 ((actions
936 (elinstall-find-actions-by-spec
937 spec
941 ;;$$RETHINK ME - maybe hand this work off to autoload?
942 ;;This is just the default loaddefs file, spec actually
943 ;;controls it.
944 (def-file . ,(elinstall-expand-deffile-name nil) ))))
945 (segment-list (elinstall-segregate-actions actions))
946 (use-load-path
947 (elinstall-get-relevant-load-path
948 actions)))
950 (mapcar
951 #'(lambda (segment)
952 (elinstall-do-segment segment force use-load-path))
953 segment-list)))
955 ;;;_ . Entry points
956 ;;;_ , elinstall
957 (defun elinstall (project-name dir spec &optional preload-target force)
958 "Install elisp files.
960 They need not be presented as a package.
962 Parameters:
963 PROJECT-NAME - the name of the project
964 DIR - the root directory of the project.
965 Suggestion: (elinstall-directory-true-name)
967 SPEC - a spec for the autoloads etc to make. It can be as simple as
968 `(in DIRECTORY t).
969 Suggestion: `(in ,(elinstall-directory-true-name) t)
970 PRELOAD-TARGET is where the autoload files are to be symlinked in. If
971 `nil' `elinstall-default-preload-target' is used instead.
973 If FORCE is t, install a package even if it has already been
974 installed.
975 Other non-nil cases of FORCE are reserved for future development."
977 (when
978 (and
979 (or
980 force
981 (not (elinstall-already-installed project-name)))
982 ;;$$RE-ADD ME - I commented it out just for development.
983 '(yes-or-no-p (format "Install %s " project-name)))
984 (elinstall-x dir spec preload-target force)
986 ;;$$RE-ADD ME - I commented it out just for development.
987 '(elinstall-record-installed project-name)))
991 ;;;_ , elinstall-update-directory-autoloads
992 ;;$$TEST ME
993 (defun elinstall-update-directory-autoloads (dir)
996 (interactive "DInstall all elisp files from directory: ")
998 (elinstall-x
1000 '(dir ".")
1001 (elinstall-expand-deffile-name
1002 generated-autoload-file)))
1005 ;;;_ , elinstall-update-file-autoloads
1006 ;;$$TEST ME
1007 (defun elinstall-update-file-autoloads (file)
1010 (interactive "fInstall elisp file: ")
1011 (elinstall
1012 file
1013 `(file ,file)
1015 (elinstall-maybe-get-deffile file)
1016 (elinstall-expand-deffile-name
1017 generated-autoload-file))))
1024 ;;;_. Footers
1025 ;;;_ , Provides
1027 (provide 'elinstall)
1029 ;;;_ * Local emacs vars.
1030 ;;;_ + Local variables:
1031 ;;;_ + mode: allout
1032 ;;;_ + End:
1034 ;;;_ , End
1035 ;;; elinstall.el ends here