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
)))
67 ((and (symbolp (car head
))
68 (or (symbolp (cadr head
))
69 (stringp (cadr head
))))
70 (add-to-list 'res
(list (car head
) (cadr head
)) t
)
71 (setq head
(cddr head
)))
73 (setq head
(cdr head
)))))
74 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
78 (defun muse-project-alist-set (sym val
)
79 "Turn customized version of `muse-project-alist' into something
80 Muse can make use of."
83 (let ((head (car (cdar val
)))
86 (cond ((stringp (car head
))
87 (add-to-list 'res
(car head
) t
))
89 (add-to-list 'res
(caar head
) t
)
90 (add-to-list 'res
(car (cdar head
)) t
)))
91 (setq head
(cdr head
)))
92 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
93 (setq val
(cdr val
))))
95 (define-widget 'muse-project
'lazy
96 "A widget that defines a Muse project."
98 :type
'(cons (repeat :tag
"Contents"
100 (string :tag
"Directory")
102 (choice :tag
"Property"
103 (const :book-chapter
)
105 (const :book-funcall
)
114 (string) (symbol)))))
115 (repeat (repeat :tag
"Style"
124 (string :tag
"Value"))))))
126 (defcustom muse-project-alist nil
127 "An alist of Muse projects.
128 A project defines a fileset, and a list of custom attributes for use
129 when publishing files in that project."
131 :key-type
(string :tag
"Project name")
132 :value-type muse-project
)
133 :get
'muse-project-alist-get
134 :set
'muse-project-alist-set
135 :group
'muse-project
)
137 (defvar muse-project-file-alist nil
138 "This variable is automagically constructed as needed.")
140 (defcustom muse-project-ignore-regexp
141 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
142 "A regexp matching files to be ignored in Wiki directories."
146 (defvar muse-current-project nil
147 "Project we are currently visiting.")
148 (make-variable-buffer-local 'muse-current-project
)
150 (defsubst muse-project
(&optional project
)
151 "Resolve the given PROJECT into a full Muse project, if it is a string."
153 (or muse-current-project
154 (muse-project-of-file))
155 (if (stringp project
)
156 (assoc project muse-project-alist
)
157 (muse-assert (consp project
))
160 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
161 "Return a filename if PAGE exists within the given Muse PROJECT."
162 (setq project
(muse-project project
))
163 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
165 (defun muse-project-private-p (file)
166 "Return non-nil if NAME is a private page with PROJECT."
167 (unless muse-under-windows-p
168 (setq file
(file-truename file
))
169 (if (file-attributes file
) ; don't publish if no attributes exist
170 (or (when (eq ?-
(aref (nth 8 (file-attributes
171 (file-name-directory file
))) 7))
173 "The " (file-name-directory file
)
174 " directory must be readable by others"
175 " in order for its contents to be published.")))
176 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
179 (defun muse-project-file-entries (path)
180 (let* ((names (list t
))
183 ((file-directory-p path
)
184 (dolist (file (directory-files
185 path t
(when muse-file-extension
186 (concat "." muse-file-extension
"\\'"))))
187 (unless (or (string-match muse-project-ignore-regexp file
)
188 (file-directory-p file
))
190 (cons (cons (muse-page-name file
) file
) nil
))
191 (setq lnames
(cdr lnames
)))))
192 ((file-readable-p path
)
194 (cons (cons (muse-page-name path
) path
) nil
))
195 (setq lnames
(cdr lnames
)))
197 (muse-assert (file-name-directory path
))
198 (dolist (file (directory-files
199 (file-name-directory path
) t
200 (file-name-nondirectory path
)))
201 (unless (string-match muse-project-ignore-regexp file
)
203 (cons (cons (muse-page-name file
) file
) nil
))
204 (setq lnames
(cdr lnames
))))))
207 (defun muse-project-file-alist (&optional project no-check-p
)
208 "Return member filenames for the given Muse PROJECT.
209 On UNIX, this list is only updated if one of the directories'
210 contents have changed. On Windows, it is always reread from
212 (setq project
(muse-project project
))
213 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
215 ;; Determine the last modified of any directory mentioned in the
216 ;; project's pattern list
217 (unless (or muse-under-windows-p no-check-p
)
218 (let ((pats (cadr project
)))
220 (if (symbolp (car pats
))
221 (setq pats
(cddr pats
))
222 (let ((dir (or (and (file-directory-p (car pats
)) (car pats
))
223 (and (not (file-readable-p (car pats
)))
225 (file-name-directory (car pats
)))
226 (file-name-directory (car pats
))))))
228 (let ((mod-time (nth 5 (file-attributes dir
))))
229 (if (or (null last-mod
)
231 (muse-time-less-p last-mod mod-time
)))
232 (setq last-mod mod-time
)))))
233 (setq pats
(cdr pats
))))))
234 ;; Either return the currently known list, or read it again from
236 (if (or (and no-check-p
(cadr file-alist
))
237 (not (or muse-under-windows-p
238 (null (cddr file-alist
))
240 (muse-time-less-p (cddr file-alist
) last-mod
))))
243 (setcdr (cdr file-alist
) last-mod
)
244 (setq file-alist
(cons (car project
) (cons nil last-mod
))
245 muse-project-file-alist
246 (cons file-alist muse-project-file-alist
)))
247 ;; Read in all of the file entries
251 (let* ((names (list t
))
252 (pats (cadr project
)))
254 (if (symbolp (car pats
))
255 (setq pats
(cddr pats
))
256 (nconc names
(muse-project-file-entries (car pats
)))
257 (setq pats
(cdr pats
))))
260 (defun muse-project-of-file (&optional pathname
)
261 "Determine which project the given PATHNAME relates to.
262 If PATHNAME is nil, the current buffer's filename is used."
263 (if (and (null pathname
) muse-current-project
)
266 (and (boundp 'muse-publishing-current-file
)
267 muse-publishing-current-file
)
269 (let* ((file (file-truename (or pathname buffer-file-name
)))
270 (dir (file-name-directory file
))
271 (project-entry muse-project-alist
)
273 (while (and project-entry
(not found
))
274 (let ((pats (car (cdar project-entry
))))
275 (while (and pats
(not found
))
276 (if (symbolp (car pats
))
277 (setq pats
(cddr pats
))
278 (let ((truename (file-truename (car pats
))))
279 (if (or (string= truename file
)
280 (string= truename dir
)
281 (string-match truename file
))
282 (setq found
(car project-entry
))))
283 (setq pats
(cdr pats
))))
284 (setq project-entry
(cdr project-entry
))))
287 (defun muse-read-project (prompt &optional no-check-p no-assume
)
288 "Read a project name from the minibuffer, if it can't be figured
290 (if (null muse-project-alist
)
291 (error "There are no Muse projects defined; see `muse-project-alist'.")
292 (or (unless no-check-p
293 (muse-project-of-file))
294 (if (and (not no-assume
)
295 (= 1 (length muse-project-alist
)))
296 (car muse-project-alist
)
297 (assoc (completing-read prompt muse-project-alist
)
298 muse-project-alist
)))))
300 (defvar muse-project-page-history nil
)
302 (defun muse-read-project-file (project prompt
&optional default
)
303 (let ((name (completing-read prompt
(muse-project-file-alist project
)
304 nil nil nil
'muse-project-page-history
306 (cons name
(muse-project-page-file name project
))))
308 (defun muse-project-find-file (name project
&optional command directory
)
309 "Open the Muse page given by NAME in PROJECT.
310 If COMMAND is non-nil, it is the function used to visit the file.
311 If DIRECTORY is non-nil, it is the directory in which the page
312 will be created if it does not already exist. Otherwise, the
313 first directory within the project's fileset is used."
315 (let* ((project (muse-read-project "Find in project: "
317 (default (muse-get-keyword :default
(cadr project
)))
318 (entry (muse-read-project-file
320 (format "Find page: (default: %s) "
324 (list entry project
)))
325 (setq project
(muse-project project
))
326 (let ((project-name (car project
)))
327 (unless (interactive-p)
328 (setq project
(muse-project project
)
329 name
(cons name
(muse-project-page-file name project
))))
330 ;; If we're given a relative name, open it as-is
332 (save-match-data (string-match "\\.\\./" (car name
))))
333 (setcdr name
(if muse-file-extension
334 (concat (car name
) "." muse-file-extension
)
336 ;; At this point, name is (PAGE . FILE).
338 (let ((pats (cadr project
)))
339 (while (and pats
(null directory
))
340 (if (symbolp (car pats
))
341 (setq pats
(cddr pats
))
342 (if (file-directory-p (car pats
))
343 (setq directory
(car pats
) pats nil
)
344 (setq pats
(cdr pats
))))))
346 (let ((filename (expand-file-name
347 (if muse-file-extension
348 (concat (car name
) "." muse-file-extension
)
351 (unless (file-exists-p directory
)
352 (make-directory directory t
))
353 (setcdr name filename
)))))
356 (funcall (or command
'find-file
) (cdr name
))
357 (error "There is no page %s in project %s."
358 (car name
) project-name
))))
360 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
361 "Given STYLES, return a list of the ones that are considered for FILE.
362 The name of a project may be used for STYLES."
363 (when (stringp styles
)
364 (setq styles
(cddr (muse-project styles
))))
365 (muse-assert (and file styles
))
367 (dolist (style styles
)
368 (let ((include-regexp (muse-style-element :include style
))
369 (exclude-regexp (muse-style-element :exclude style
)))
370 (when (and (or ignore-regexp
371 (and (null include-regexp
)
372 (null exclude-regexp
))
374 (string-match include-regexp file
)
375 (not (string-match exclude-regexp file
))))
376 (or (not (file-exists-p file
))
377 (not (muse-project-private-p file
))))
378 (add-to-list 'used-styles style
))))
381 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
382 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
384 (dolist (style styles
)
385 (let ((output-dir (muse-style-element :path style
)))
386 ;; ensure the publishing location is available
387 (unless (file-exists-p output-dir
)
388 (message "Creating publishing directory %s" output-dir
)
389 (make-directory output-dir
))
390 ;; publish the member file!
391 (if (muse-publish-file file style output-dir force
)
392 (setq published t
))))
395 (defun muse-project-save-buffers (&optional project
)
396 (setq project
(muse-project project
))
400 (and (buffer-modified-p buffer
)
401 (not (buffer-base-buffer buffer
))
402 (or (buffer-file-name buffer
)
405 (and buffer-offer-save
406 (> (buffer-size) 0))))
407 (with-current-buffer buffer
408 (let ((proj (muse-project-of-file)))
409 (and proj
(string= (car proj
)
411 (if (buffer-file-name buffer
)
412 (format "Save file %s? "
413 (buffer-file-name buffer
))
414 (format "Save buffer %s? "
415 (buffer-name buffer
))))))
421 '("buffer" "buffers" "save")
422 (if (boundp 'save-some-buffers-action-alist
)
423 save-some-buffers-action-alist
)))
425 (defun muse-project-publish (project &optional force
)
426 "Publish the pages of PROJECT that need publishing."
427 (interactive (list (muse-read-project "Publish project: " nil t
)
429 (setq project
(muse-project project
))
430 (let ((styles (cddr project
))
431 (muse-current-project project
)
433 ;; determine the style from the project, or else ask
435 (setq styles
(list (muse-publish-get-style))))
436 ;; prompt to save any buffers related to this project
437 (muse-project-save-buffers project
)
438 ;; run hook before publishing begins
439 (run-hook-with-args 'muse-before-project-publish-hook project
)
440 ;; publish all files in the project, for each style; the actual
441 ;; publishing will only happen if the files are newer than the
442 ;; last published output
443 (dolist (pair (muse-project-file-alist project
))
444 (if (muse-project-publish-file (cdr pair
) styles force
)
446 ;; run hook after publishing ends
447 (run-hook-with-args 'muse-after-project-publish-hook project
)
448 ;; notify the user that everything is now done
450 (message "All pages in %s have been published." (car project
))
451 (message "No pages in %s need publishing at this time."
454 (defun muse-project-batch-publish ()
455 "Publish Muse files in batch mode."
456 (let ((muse-batch-publishing-p t
)
458 (if (string= "--force" (or (car command-line-args-left
) ""))
460 command-line-args-left
(cdr command-line-args-left
)))
461 (if command-line-args-left
462 (dolist (project command-line-args-left
)
463 (message "Publishing project %s ..." project
)
464 (muse-project-publish project force
))
465 (message "No projects specified."))))
468 (put 'make-local-hook
'byte-compile nil
))
470 (defun muse-project-set-variables ()
471 "Load project-specific variables."
472 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
475 (setq sym
(car vars
))
476 (setq custom-set
(or (get sym
'custom-set
) 'set
))
477 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
478 (make-local-hook sym
)
479 (make-local-variable sym
)))
480 (funcall custom-set var
(car (cdr vars
)))
481 (setq vars
(cdr (cdr vars
))))))
483 (defun muse-project-delete-output-files (project)
485 (list (muse-read-project "Remove all output files for project: " nil t
)))
486 (setq project
(muse-project project
))
487 (let ((file-alist (muse-project-file-alist project
))
488 (styles (cddr project
))
490 (dolist (entry file-alist
)
491 (dolist (style styles
)
493 (and (setq path
(muse-style-element :path style
))
495 (concat (muse-style-element :prefix style
)
497 (or (muse-style-element :osuffix style
)
498 (muse-style-element :suffix style
)))
501 (muse-delete-file-if-exists output-file
))))))
503 (provide 'muse-project
)
505 ;;; muse-project.el ends here