1 ;;; muse-project.el --- Handle Muse projects.
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (require 'muse-publish
)
37 (defgroup muse-project nil
38 "Options controlling the behavior of Muse project handling."
41 (defcustom muse-before-project-publish-hook nil
42 "A hook run before a project is published.
43 Each function is passed the project object, a cons with the format
44 (PROJNAME . SETTINGS)"
48 (defcustom muse-after-project-publish-hook nil
49 "A hook run after a project is published.
50 Each function is passed the project object, a cons with the format
51 (PROJNAME . SETTINGS)"
55 (defun muse-project-alist-get (sym)
56 "Turn `muse-project-alist' into something we can customize easily."
58 (let* ((val (copy-alist (symbol-value sym
)))
61 (let ((head (car (cdar val
)))
63 ;; Turn settings of first part into cons cells, symbol->string
65 (cond ((stringp (car head
))
66 (add-to-list 'res
(car head
) t
)
67 (setq head
(cdr head
)))
69 (add-to-list 'res
(list (symbol-name (car head
))
71 (setq head
(cddr head
)))
73 (setq head
(cdr head
)))))
74 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
75 (let ((styles (cdar val
)))
76 ;; Symbol->string in every style
78 (let ((head (cadr styles
))
81 (setq res
(plist-put res
(symbol-name (car head
))
83 (setq head
(cddr head
)))
84 (setcdr styles
(cons res
(cddr styles
))))
85 (setq styles
(cdr styles
))))
89 (defun muse-project-alist-set (sym val
)
90 "Turn customized version of `muse-project-alist' into something
91 Muse can make use of."
94 (let ((head (car (cdar val
)))
96 ;; Turn cons cells into flat list, string->symbol
98 (cond ((stringp (car head
))
99 (add-to-list 'res
(car head
) t
))
101 (add-to-list 'res
(intern (caar head
)) t
)
102 (add-to-list 'res
(car (cdar head
)) t
)))
103 (setq head
(cdr head
)))
104 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
105 (let ((styles (cdar val
)))
106 ;; String->symbol in every style
108 (let ((head (cadr styles
))
111 (setq res
(plist-put res
(intern (car head
))
113 (setq head
(cddr head
)))
114 (setcdr styles
(cons res
(cddr styles
))))
115 (setq styles
(cdr styles
))))
116 (setq val
(cdr val
))))
118 (define-widget 'muse-project
'default
119 "A widget that defines a Muse project."
121 :value-create
'muse-widget-type-value-create
122 :value-get
'muse-widget-child-value-get
123 :match
'muse-widget-type-match
124 :type
'(cons :format
" %v"
125 (repeat :tag
"Settings" :format
"%{%t%}:\n%v%i\n\n"
127 (string :tag
"Directory")
128 (list :tag
"Book function"
129 (const :tag
":book-funcall" ":book-funcall")
131 (sexp :tag
"Unknown")))
132 (list :tag
"Book part"
133 (const :tag
":book-part" ":book-part")
134 (string :tag
"Name"))
135 (list :tag
"Book style"
136 (const :tag
":book-style" ":book-style")
137 (string :tag
"Style"))
138 (list :tag
"Default file"
139 (const :tag
":default" ":default")
140 (string :tag
"File"))
141 (list :tag
"End of book"
142 (const :tag
":book-end" ":book-end")
144 (list :tag
"Force publishing"
145 (const :tag
":force-publish" ":force-publish")
146 (repeat (string :tag
"File")))
147 (list :tag
"Major mode"
148 (const :tag
":major-mode" ":major-mode")
149 (choice (function :tag
"Mode")
150 (sexp :tag
"Unknown")))
151 (list :tag
"New chapter"
152 (const :tag
":book-chapter" ":book-chapter")
153 (string :tag
"Name"))
154 (list :tag
"No chapters"
155 (const :tag
":nochapters" ":nochapters")
157 (list :tag
"Set variables"
158 (const :tag
":set" ":set")
159 (repeat (list (symbol :tag
"Variable")
160 (sexp :tag
"Setting"))))
161 (list :tag
"Visit links using"
162 (const :tag
":visit-link" ":visit-link")
164 (sexp :tag
"Unknown")))))
165 (repeat :tag
"Styles" :format
"%{%t%}:\n%v%i\n\n"
168 :tag
"Publishing style"
169 (const :tag
":base" ":base")
170 (string :tag
"Style"))
173 (const :tag
":base-url" ":base-url")
176 :tag
"Exclude matching"
177 (const :tag
":exclude" ":exclude")
180 :tag
"Include matching"
181 (const :tag
":include" ":include")
184 :tag
"Timestamps file"
185 (const :tag
":timestamps" ":timestamps")
189 (const :tag
":path" ":path")
190 (string :tag
"Path"))))))
192 (defcustom muse-project-alist nil
193 "An alist of Muse projects.
194 A project defines a fileset, and a list of custom attributes for use
195 when publishing files in that project."
196 :type
'(choice (const :tag
"No projects defined." nil
)
197 (repeat (cons :format
"%{%t%}:\n\n%v"
198 :tag
"Project" :indent
4
199 (string :tag
"Project name")
201 (sexp :tag
"Cannot parse expression"))
202 :get
'muse-project-alist-get
203 :set
'muse-project-alist-set
204 :group
'muse-project
)
206 ;; Make it easier to specify a muse-project-alist entry
208 (defcustom muse-project-ignore-regexp
209 (concat "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\|,.*\\)\\'\\|"
210 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\)\\(/\\|\\'\\)")
211 "A regexp matching files to be ignored in Wiki directories."
215 (defun muse-project-recurse-directory (base)
216 "Recusively retrieve all of the directories underneath BASE.
217 A list of these directories is returned.
218 Directories starting with \".\" will be ignored, as well as those
219 which match `muse-project-ignore-regexp'."
220 (when (and (file-directory-p base
)
221 (not (string-match muse-project-ignore-regexp base
)))
223 (dolist (file (directory-files base t
"^[^.]"))
224 (when (and (file-directory-p file
)
225 (not (string-match muse-project-ignore-regexp file
)))
226 (setq dir
(file-name-nondirectory file
))
228 (nconc list
(mapcar #'(lambda (item)
229 (concat dir
"/" item
))
230 (muse-project-recurse-directory file
)))))
233 (defun muse-project-alist-styles (entry-dir output-dir style
)
234 "Return a list of styles to use in a `muse-project-alist' entry.
235 ENTRY-DIR is the top-level directory of the project.
236 OUTPUT-DIR is where Muse files are published, keeping directory structure.
237 STYLE is the publishing style to use.
239 For an example of the use of this function, see
240 `examples/mwolson/muse-init.el' from the Muse distribution."
241 (cons `(:base
,style
:path
,(expand-file-name output-dir
)
242 :include
,(concat "/" (file-name-nondirectory entry-dir
)
244 (mapcar (lambda (dir);
246 :path
,(expand-file-name dir output-dir
)
247 :include
,(concat "/" dir
"/[^/]+$")))
248 (muse-project-recurse-directory entry-dir
))))
250 (defun muse-project-alist-dirs (entry-dir)
251 "Return a list of directories to use in a `muse-project-alist' entry.
252 ENTRY-DIR is the top-level directory of the project.
254 For an example of the use of this function, see
255 `examples/mwolson/muse-init.el' from the Muse distribution."
256 (cons (expand-file-name entry-dir
)
257 (mapcar (lambda (dir) (expand-file-name dir entry-dir
))
258 (muse-project-recurse-directory entry-dir
))))
260 ;; Constructing the file-alist
262 (defvar muse-project-file-alist nil
263 "This variable is automagically constructed as needed.")
265 (defvar muse-current-project nil
266 "Project we are currently visiting.")
267 (make-variable-buffer-local 'muse-current-project
)
269 (defsubst muse-project
(&optional project
)
270 "Resolve the given PROJECT into a full Muse project, if it is a string."
272 (or muse-current-project
273 (muse-project-of-file))
274 (if (stringp project
)
275 (assoc project muse-project-alist
)
276 (muse-assert (consp project
))
279 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
280 "Return a filename if PAGE exists within the given Muse PROJECT."
281 (setq project
(muse-project project
))
282 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
284 (defun muse-project-private-p (file)
285 "Return non-nil if NAME is a private page with PROJECT."
286 (unless muse-under-windows-p
287 (setq file
(file-truename file
))
288 (if (file-attributes file
) ; don't publish if no attributes exist
289 (or (when (eq ?-
(aref (nth 8 (file-attributes
290 (file-name-directory file
))) 7))
292 "The " (file-name-directory file
)
293 " directory must be readable by others"
294 " in order for its contents to be published.")))
295 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
298 (defun muse-project-file-entries (path)
299 (let* ((names (list t
))
302 ((file-directory-p path
)
303 (dolist (file (directory-files
304 path t
(when muse-file-extension
305 (concat "." muse-file-extension
"\\'"))))
306 (unless (or (string-match muse-project-ignore-regexp file
)
307 (file-directory-p file
))
309 (cons (cons (muse-page-name file
) file
) nil
))
310 (setq lnames
(cdr lnames
)))))
311 ((file-readable-p path
)
313 (cons (cons (muse-page-name path
) path
) nil
))
314 (setq lnames
(cdr lnames
)))
316 (muse-assert (file-name-directory path
))
317 (dolist (file (directory-files
318 (file-name-directory path
) t
319 (file-name-nondirectory path
)))
320 (unless (string-match muse-project-ignore-regexp file
)
322 (cons (cons (muse-page-name file
) file
) nil
))
323 (setq lnames
(cdr lnames
))))))
326 (defun muse-project-file-alist (&optional project no-check-p
)
327 "Return member filenames for the given Muse PROJECT.
328 On UNIX, this list is only updated if one of the directories'
329 contents have changed. On Windows, it is always reread from
331 (setq project
(muse-project project
))
332 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
334 ;; Determine the last modified of any directory mentioned in the
335 ;; project's pattern list
336 (unless (or muse-under-windows-p no-check-p
)
337 (let ((pats (cadr project
)))
339 (if (symbolp (car pats
))
340 (setq pats
(cddr pats
))
341 (let ((dir (or (and (file-directory-p (car pats
)) (car pats
))
342 (and (not (file-readable-p (car pats
)))
344 (file-name-directory (car pats
)))
345 (file-name-directory (car pats
))))))
347 (let ((mod-time (nth 5 (file-attributes dir
))))
348 (if (or (null last-mod
)
350 (muse-time-less-p last-mod mod-time
)))
351 (setq last-mod mod-time
)))))
352 (setq pats
(cdr pats
))))))
353 ;; Either return the currently known list, or read it again from
355 (if (or (and no-check-p
(cadr file-alist
))
356 (not (or muse-under-windows-p
357 (null (cddr file-alist
))
359 (muse-time-less-p (cddr file-alist
) last-mod
))))
362 (setcdr (cdr file-alist
) last-mod
)
363 (setq file-alist
(cons (car project
) (cons nil last-mod
))
364 muse-project-file-alist
365 (cons file-alist muse-project-file-alist
)))
366 ;; Read in all of the file entries
370 (let* ((names (list t
))
371 (pats (cadr project
)))
373 (if (symbolp (car pats
))
374 (setq pats
(cddr pats
))
375 (nconc names
(muse-project-file-entries (car pats
)))
376 (setq pats
(cdr pats
))))
379 (defun muse-project-of-file (&optional pathname
)
380 "Determine which project the given PATHNAME relates to.
381 If PATHNAME is nil, the current buffer's filename is used."
382 (if (and (null pathname
) muse-current-project
)
384 (unless pathname
(setq pathname
(muse-current-file)))
387 (not (string-match muse-project-ignore-regexp pathname
)))
388 (let* ((file (file-truename pathname
))
389 (dir (file-name-directory file
))
390 (project-entry muse-project-alist
)
392 (while (and project-entry
(not found
))
393 (let ((pats (car (cdar project-entry
))))
394 (while (and pats
(not found
))
395 (if (symbolp (car pats
))
396 (setq pats
(cddr pats
))
397 (let ((truename (file-truename (car pats
))))
398 (if (or (string= truename file
)
399 (string= truename dir
)
400 (string-match truename file
))
401 (setq found
(car project-entry
))))
402 (setq pats
(cdr pats
))))
403 (setq project-entry
(cdr project-entry
))))
406 (defun muse-read-project (prompt &optional no-check-p no-assume
)
407 "Read a project name from the minibuffer, if it can't be figured
409 (if (null muse-project-alist
)
410 (error "There are no Muse projects defined; see `muse-project-alist'.")
411 (or (unless no-check-p
412 (muse-project-of-file))
413 (if (and (not no-assume
)
414 (= 1 (length muse-project-alist
)))
415 (car muse-project-alist
)
416 (assoc (completing-read prompt muse-project-alist
)
417 muse-project-alist
)))))
419 (defvar muse-project-page-history nil
)
421 (defun muse-read-project-file (project prompt
&optional default
)
422 (let ((name (completing-read prompt
(muse-project-file-alist project
)
423 nil nil nil
'muse-project-page-history
425 (cons name
(muse-project-page-file name project
))))
427 (defun muse-project-find-file (name project
&optional command directory
)
428 "Open the Muse page given by NAME in PROJECT.
429 If COMMAND is non-nil, it is the function used to visit the file.
430 If DIRECTORY is non-nil, it is the directory in which the page
431 will be created if it does not already exist. Otherwise, the
432 first directory within the project's fileset is used."
434 (let* ((project (muse-read-project "Find in project: "
436 (default (muse-get-keyword :default
(cadr project
)))
437 (entry (muse-read-project-file
439 (format "Find page: (default: %s) "
443 (list entry project
)))
444 (setq project
(muse-project project
))
445 (let ((project-name (car project
)))
446 (unless (interactive-p)
447 (setq project
(muse-project project
)
448 name
(cons name
(muse-project-page-file name project
))))
449 ;; If we're given a relative name, open it as-is
451 (save-match-data (string-match "\\.\\./" (car name
))))
452 (setcdr name
(if muse-file-extension
453 (concat (car name
) "." muse-file-extension
)
455 ;; At this point, name is (PAGE . FILE).
457 (let ((pats (cadr project
)))
458 (while (and pats
(null directory
))
459 (if (symbolp (car pats
))
460 (setq pats
(cddr pats
))
461 (if (file-directory-p (car pats
))
462 (setq directory
(car pats
) pats nil
)
463 (setq pats
(cdr pats
))))))
465 (let ((filename (expand-file-name
466 (if muse-file-extension
467 (concat (car name
) "." muse-file-extension
)
470 (unless (file-exists-p directory
)
471 (make-directory directory t
))
472 (setcdr name filename
)))))
475 (funcall (or command
'find-file
) (cdr name
))
476 (error "There is no page %s in project %s."
477 (car name
) project-name
))))
479 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
480 "Given STYLES, return a list of the ones that are considered for FILE.
481 The name of a project may be used for STYLES."
482 (when (stringp styles
)
483 (setq styles
(cddr (muse-project styles
))))
484 (when (and file styles
)
486 (dolist (style styles
)
487 (let ((include-regexp (muse-style-element :include style
))
488 (exclude-regexp (muse-style-element :exclude style
)))
489 (when (and (or ignore-regexp
490 (and (null include-regexp
)
491 (null exclude-regexp
))
493 (string-match include-regexp file
)
494 (not (string-match exclude-regexp file
))))
495 (or (not (file-exists-p file
))
496 (not (muse-project-private-p file
))))
497 (add-to-list 'used-styles style
))))
500 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
501 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
503 (dolist (style styles
)
504 (let ((output-dir (muse-style-element :path style
)))
505 ;; ensure the publishing location is available
506 (unless (file-exists-p output-dir
)
507 (message "Creating publishing directory %s" output-dir
)
508 (make-directory output-dir
))
509 ;; publish the member file!
510 (if (muse-publish-file file style output-dir force
)
511 (setq published t
))))
514 (defun muse-project-save-buffers (&optional project
)
515 (setq project
(muse-project project
))
519 (and (buffer-modified-p buffer
)
520 (not (buffer-base-buffer buffer
))
521 (or (buffer-file-name buffer
)
524 (and buffer-offer-save
525 (> (buffer-size) 0))))
526 (with-current-buffer buffer
527 (let ((proj (muse-project-of-file)))
528 (and proj
(string= (car proj
)
530 (if (buffer-file-name buffer
)
531 (format "Save file %s? "
532 (buffer-file-name buffer
))
533 (format "Save buffer %s? "
534 (buffer-name buffer
))))))
540 '("buffer" "buffers" "save")
541 (if (boundp 'save-some-buffers-action-alist
)
542 save-some-buffers-action-alist
)))
544 (defun muse-project-publish (project &optional force
)
545 "Publish the pages of PROJECT that need publishing."
546 (interactive (list (muse-read-project "Publish project: " nil t
)
548 (setq project
(muse-project project
))
549 (let ((styles (cddr project
))
550 (muse-current-project project
)
552 ;; determine the style from the project, or else ask
554 (setq styles
(list (muse-publish-get-style))))
555 ;; prompt to save any buffers related to this project
556 (muse-project-save-buffers project
)
557 ;; run hook before publishing begins
558 (run-hook-with-args 'muse-before-project-publish-hook project
)
559 ;; publish all files in the project, for each style; the actual
560 ;; publishing will only happen if the files are newer than the
561 ;; last published output, or if the file is listed in
562 ;; :force-publish. Files in :force-publish will not trigger the
563 ;; "All pages need to be published" message.
564 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
)))
565 (file-alist (muse-project-file-alist project
)))
566 (dolist (pair file-alist
)
567 (when (muse-project-publish-file (cdr pair
) styles force
)
568 (setq forced-files
(delete (car pair
) forced-files
))
570 (dolist (file forced-files
)
571 (muse-project-publish-file (cdr (assoc file file-alist
)) styles t
)))
572 ;; run hook after publishing ends
573 (run-hook-with-args 'muse-after-project-publish-hook project
)
574 ;; notify the user that everything is now done
576 (message "All pages in %s have been published." (car project
))
577 (message "No pages in %s need publishing at this time."
580 (defun muse-project-batch-publish ()
581 "Publish Muse files in batch mode."
582 (let ((muse-batch-publishing-p t
)
584 (if (string= "--force" (or (car command-line-args-left
) ""))
586 command-line-args-left
(cdr command-line-args-left
)))
587 (if command-line-args-left
588 (dolist (project command-line-args-left
)
589 (message "Publishing project %s ..." project
)
590 (muse-project-publish project force
))
591 (message "No projects specified."))))
594 (put 'make-local-hook
'byte-compile nil
))
596 (defun muse-project-set-variables ()
597 "Load project-specific variables."
598 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
601 (setq sym
(car vars
))
602 (setq custom-set
(or (get sym
'custom-set
) 'set
))
603 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
604 (make-local-hook sym
)
605 (make-local-variable sym
)))
606 (funcall custom-set var
(car (cdr vars
)))
607 (setq vars
(cdr (cdr vars
))))))
609 (defun muse-project-delete-output-files (project)
611 (list (muse-read-project "Remove all output files for project: " nil t
)))
612 (setq project
(muse-project project
))
613 (let ((file-alist (muse-project-file-alist project
))
614 (styles (cddr project
))
616 (dolist (entry file-alist
)
617 (dolist (style styles
)
619 (and (setq path
(muse-style-element :path style
))
621 (concat (muse-style-element :prefix style
)
623 (or (muse-style-element :osuffix style
)
624 (muse-style-element :suffix style
)))
627 (muse-delete-file-if-exists output-file
))))))
629 (provide 'muse-project
)
631 ;;; muse-project.el ends here