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
)))
64 (cond ((stringp (car head
))
65 (add-to-list 'res
(car head
))
66 (setq head
(cdr head
)))
68 (add-to-list 'res
(list (car head
) (cadr head
)) t
)
69 (setq head
(cddr head
)))
71 (setq head
(cdr head
)))))
72 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
76 (defun muse-project-alist-set (sym val
)
77 "Turn customized version of `muse-project-alist' into something
78 Muse can make use of."
81 (let ((head (car (cdar val
)))
84 (cond ((stringp (car head
))
85 (add-to-list 'res
(car head
) t
))
87 (add-to-list 'res
(caar head
) t
)
88 (add-to-list 'res
(car (cdar head
)) t
)))
89 (setq head
(cdr head
)))
90 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
91 (setq val
(cdr val
))))
93 (define-widget 'muse-project
'lazy
94 "A widget that defines a Muse project."
96 :type
'(cons (repeat :tag
"Settings"
98 (string :tag
"Directory")
99 (list :tag
"Book function"
100 (const :book-funcall
)
102 (sexp :tag
"Unknown")))
103 (list :tag
"Book part"
105 (string :tag
"Name"))
106 (list :tag
"Book style"
108 (string :tag
"Style"))
109 (list :tag
"Default file"
111 (string :tag
"File"))
112 (list :tag
"End of book"
115 (list :tag
"Force publishing"
116 (const :force-publish
)
117 (repeat (string :tag
"File")))
118 (list :tag
"Major mode"
120 (choice (function :tag
"Mode")
121 (sexp :tag
"Unknown")))
122 (list :tag
"New chapter"
123 (const :book-chapter
)
124 (string :tag
"Name"))
125 (list :tag
"No chapters"
128 (list :tag
"Set variables"
130 (repeat (list (symbol :tag
"Variable")
131 (sexp :tag
"Setting"))))
132 (list :tag
"Visit links using"
135 (sexp :tag
"Unknown")))))
136 (repeat :tag
"Styles"
137 (repeat :tag
"Style" :inline t
140 :tag
"Publishing style"
142 (string :tag
"Style"))
148 :tag
"Exclude matching"
152 :tag
"Include matching"
158 (string :tag
"Path")))))))
160 (defcustom muse-project-alist nil
161 "An alist of Muse projects.
162 A project defines a fileset, and a list of custom attributes for use
163 when publishing files in that project."
165 :key-type
(string :tag
"Project name")
166 :value-type muse-project
)
167 :get
'muse-project-alist-get
168 :set
'muse-project-alist-set
169 :group
'muse-project
)
171 (defvar muse-project-file-alist nil
172 "This variable is automagically constructed as needed.")
174 (defcustom muse-project-ignore-regexp
175 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
176 "A regexp matching files to be ignored in Wiki directories."
180 (defvar muse-current-project nil
181 "Project we are currently visiting.")
182 (make-variable-buffer-local 'muse-current-project
)
184 (defsubst muse-project
(&optional project
)
185 "Resolve the given PROJECT into a full Muse project, if it is a string."
187 (or muse-current-project
188 (muse-project-of-file))
189 (if (stringp project
)
190 (assoc project muse-project-alist
)
191 (muse-assert (consp project
))
194 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
195 "Return a filename if PAGE exists within the given Muse PROJECT."
196 (setq project
(muse-project project
))
197 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
199 (defun muse-project-private-p (file)
200 "Return non-nil if NAME is a private page with PROJECT."
201 (unless muse-under-windows-p
202 (setq file
(file-truename file
))
203 (if (file-attributes file
) ; don't publish if no attributes exist
204 (or (when (eq ?-
(aref (nth 8 (file-attributes
205 (file-name-directory file
))) 7))
207 "The " (file-name-directory file
)
208 " directory must be readable by others"
209 " in order for its contents to be published.")))
210 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
213 (defun muse-project-file-entries (path)
214 (let* ((names (list t
))
217 ((file-directory-p path
)
218 (dolist (file (directory-files
219 path t
(when muse-file-extension
220 (concat "." muse-file-extension
"\\'"))))
221 (unless (or (string-match muse-project-ignore-regexp file
)
222 (file-directory-p file
))
224 (cons (cons (muse-page-name file
) file
) nil
))
225 (setq lnames
(cdr lnames
)))))
226 ((file-readable-p path
)
228 (cons (cons (muse-page-name path
) path
) nil
))
229 (setq lnames
(cdr lnames
)))
231 (muse-assert (file-name-directory path
))
232 (dolist (file (directory-files
233 (file-name-directory path
) t
234 (file-name-nondirectory path
)))
235 (unless (string-match muse-project-ignore-regexp file
)
237 (cons (cons (muse-page-name file
) file
) nil
))
238 (setq lnames
(cdr lnames
))))))
241 (defun muse-project-file-alist (&optional project no-check-p
)
242 "Return member filenames for the given Muse PROJECT.
243 On UNIX, this list is only updated if one of the directories'
244 contents have changed. On Windows, it is always reread from
246 (setq project
(muse-project project
))
247 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
249 ;; Determine the last modified of any directory mentioned in the
250 ;; project's pattern list
251 (unless (or muse-under-windows-p no-check-p
)
252 (let ((pats (cadr project
)))
254 (if (symbolp (car pats
))
255 (setq pats
(cddr pats
))
256 (let ((dir (or (and (file-directory-p (car pats
)) (car pats
))
257 (and (not (file-readable-p (car pats
)))
259 (file-name-directory (car pats
)))
260 (file-name-directory (car pats
))))))
262 (let ((mod-time (nth 5 (file-attributes dir
))))
263 (if (or (null last-mod
)
265 (muse-time-less-p last-mod mod-time
)))
266 (setq last-mod mod-time
)))))
267 (setq pats
(cdr pats
))))))
268 ;; Either return the currently known list, or read it again from
270 (if (or (and no-check-p
(cadr file-alist
))
271 (not (or muse-under-windows-p
272 (null (cddr file-alist
))
274 (muse-time-less-p (cddr file-alist
) last-mod
))))
277 (setcdr (cdr file-alist
) last-mod
)
278 (setq file-alist
(cons (car project
) (cons nil last-mod
))
279 muse-project-file-alist
280 (cons file-alist muse-project-file-alist
)))
281 ;; Read in all of the file entries
285 (let* ((names (list t
))
286 (pats (cadr project
)))
288 (if (symbolp (car pats
))
289 (setq pats
(cddr pats
))
290 (nconc names
(muse-project-file-entries (car pats
)))
291 (setq pats
(cdr pats
))))
294 (defun muse-project-of-file (&optional pathname
)
295 "Determine which project the given PATHNAME relates to.
296 If PATHNAME is nil, the current buffer's filename is used."
297 (if (and (null pathname
) muse-current-project
)
299 (setq pathname
(or pathname
300 (and (boundp 'muse-publishing-current-file
)
301 muse-publishing-current-file
)
304 (let* ((file (file-truename pathname
))
305 (dir (file-name-directory file
))
306 (project-entry muse-project-alist
)
309 (while (and project-entry
(not found
))
310 (let ((pats (car (cdar project-entry
))))
311 (while (and pats
(not found
))
312 (if (symbolp (car pats
))
313 (setq pats
(cddr pats
))
314 (let ((truename (file-truename (car pats
))))
315 (if (or (string= truename file
)
316 (string= truename dir
)
317 (string-match truename file
))
318 (setq found
(car project-entry
))))
319 (setq pats
(cdr pats
))))
320 (setq project-entry
(cdr project-entry
)))))
323 (defun muse-read-project (prompt &optional no-check-p no-assume
)
324 "Read a project name from the minibuffer, if it can't be figured
326 (if (null muse-project-alist
)
327 (error "There are no Muse projects defined; see `muse-project-alist'.")
328 (or (unless no-check-p
329 (muse-project-of-file))
330 (if (and (not no-assume
)
331 (= 1 (length muse-project-alist
)))
332 (car muse-project-alist
)
333 (assoc (completing-read prompt muse-project-alist
)
334 muse-project-alist
)))))
336 (defvar muse-project-page-history nil
)
338 (defun muse-read-project-file (project prompt
&optional default
)
339 (let ((name (completing-read prompt
(muse-project-file-alist project
)
340 nil nil nil
'muse-project-page-history
342 (cons name
(muse-project-page-file name project
))))
344 (defun muse-project-find-file (name project
&optional command directory
)
345 "Open the Muse page given by NAME in PROJECT.
346 If COMMAND is non-nil, it is the function used to visit the file.
347 If DIRECTORY is non-nil, it is the directory in which the page
348 will be created if it does not already exist. Otherwise, the
349 first directory within the project's fileset is used."
351 (let* ((project (muse-read-project "Find in project: "
353 (default (muse-get-keyword :default
(cadr project
)))
354 (entry (muse-read-project-file
356 (format "Find page: (default: %s) "
360 (list entry project
)))
361 (setq project
(muse-project project
))
362 (let ((project-name (car project
)))
363 (unless (interactive-p)
364 (setq project
(muse-project project
)
365 name
(cons name
(muse-project-page-file name project
))))
366 ;; If we're given a relative name, open it as-is
368 (save-match-data (string-match "\\.\\./" (car name
))))
369 (setcdr name
(if muse-file-extension
370 (concat (car name
) "." muse-file-extension
)
372 ;; At this point, name is (PAGE . FILE).
374 (let ((pats (cadr project
)))
375 (while (and pats
(null directory
))
376 (if (symbolp (car pats
))
377 (setq pats
(cddr pats
))
378 (if (file-directory-p (car pats
))
379 (setq directory
(car pats
) pats nil
)
380 (setq pats
(cdr pats
))))))
382 (let ((filename (expand-file-name
383 (if muse-file-extension
384 (concat (car name
) "." muse-file-extension
)
387 (unless (file-exists-p directory
)
388 (make-directory directory t
))
389 (setcdr name filename
)))))
392 (funcall (or command
'find-file
) (cdr name
))
393 (error "There is no page %s in project %s."
394 (car name
) project-name
))))
396 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
397 "Given STYLES, return a list of the ones that are considered for FILE.
398 The name of a project may be used for STYLES."
399 (when (stringp styles
)
400 (setq styles
(cddr (muse-project styles
))))
401 (muse-assert (and file styles
))
403 (dolist (style styles
)
404 (let ((include-regexp (muse-style-element :include style
))
405 (exclude-regexp (muse-style-element :exclude style
)))
406 (when (and (or ignore-regexp
407 (and (null include-regexp
)
408 (null exclude-regexp
))
410 (string-match include-regexp file
)
411 (not (string-match exclude-regexp file
))))
412 (or (not (file-exists-p file
))
413 (not (muse-project-private-p file
))))
414 (add-to-list 'used-styles style
))))
417 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
418 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
420 (dolist (style styles
)
421 (let ((output-dir (muse-style-element :path style
)))
422 ;; ensure the publishing location is available
423 (unless (file-exists-p output-dir
)
424 (message "Creating publishing directory %s" output-dir
)
425 (make-directory output-dir
))
426 ;; publish the member file!
427 (if (muse-publish-file file style output-dir force
)
428 (setq published t
))))
431 (defun muse-project-save-buffers (&optional project
)
432 (setq project
(muse-project project
))
436 (and (buffer-modified-p buffer
)
437 (not (buffer-base-buffer buffer
))
438 (or (buffer-file-name buffer
)
441 (and buffer-offer-save
442 (> (buffer-size) 0))))
443 (with-current-buffer buffer
444 (let ((proj (muse-project-of-file)))
445 (and proj
(string= (car proj
)
447 (if (buffer-file-name buffer
)
448 (format "Save file %s? "
449 (buffer-file-name buffer
))
450 (format "Save buffer %s? "
451 (buffer-name buffer
))))))
457 '("buffer" "buffers" "save")
458 (if (boundp 'save-some-buffers-action-alist
)
459 save-some-buffers-action-alist
)))
461 (defun muse-project-publish (project &optional force
)
462 "Publish the pages of PROJECT that need publishing."
463 (interactive (list (muse-read-project "Publish project: " nil t
)
465 (setq project
(muse-project project
))
466 (let ((styles (cddr project
))
467 (muse-current-project project
)
469 ;; determine the style from the project, or else ask
471 (setq styles
(list (muse-publish-get-style))))
472 ;; prompt to save any buffers related to this project
473 (muse-project-save-buffers project
)
474 ;; run hook before publishing begins
475 (run-hook-with-args 'muse-before-project-publish-hook project
)
476 ;; publish all files in the project, for each style; the actual
477 ;; publishing will only happen if the files are newer than the
478 ;; last published output
479 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
))))
480 (dolist (pair (muse-project-file-alist project
))
481 (when (muse-project-publish-file
482 (cdr pair
) styles
(or force
(member (car pair
) forced-files
)))
483 (setq published t
))))
484 ;; run hook after publishing ends
485 (run-hook-with-args 'muse-after-project-publish-hook project
)
486 ;; notify the user that everything is now done
488 (message "All pages in %s have been published." (car project
))
489 (message "No pages in %s need publishing at this time."
492 (defun muse-project-batch-publish ()
493 "Publish Muse files in batch mode."
494 (let ((muse-batch-publishing-p t
)
496 (if (string= "--force" (or (car command-line-args-left
) ""))
498 command-line-args-left
(cdr command-line-args-left
)))
499 (if command-line-args-left
500 (dolist (project command-line-args-left
)
501 (message "Publishing project %s ..." project
)
502 (muse-project-publish project force
))
503 (message "No projects specified."))))
506 (put 'make-local-hook
'byte-compile nil
))
508 (defun muse-project-set-variables ()
509 "Load project-specific variables."
510 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
513 (setq sym
(car vars
))
514 (setq custom-set
(or (get sym
'custom-set
) 'set
))
515 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
516 (make-local-hook sym
)
517 (make-local-variable sym
)))
518 (funcall custom-set var
(car (cdr vars
)))
519 (setq vars
(cdr (cdr vars
))))))
521 (defun muse-project-delete-output-files (project)
523 (list (muse-read-project "Remove all output files for project: " nil t
)))
524 (setq project
(muse-project project
))
525 (let ((file-alist (muse-project-file-alist project
))
526 (styles (cddr project
))
528 (dolist (entry file-alist
)
529 (dolist (style styles
)
531 (and (setq path
(muse-style-element :path style
))
533 (concat (muse-style-element :prefix style
)
535 (or (muse-style-element :osuffix style
)
536 (muse-style-element :suffix style
)))
539 (muse-delete-file-if-exists output-file
))))))
541 (provide 'muse-project
)
543 ;;; muse-project.el ends here