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 (defvar muse-project-file-alist nil
207 "This variable is automagically constructed as needed.")
209 (defcustom muse-project-ignore-regexp
210 (concat "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\|,.*\\)\\'\\|"
211 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\)\\(/\\|\\'\\)")
212 "A regexp matching files to be ignored in Wiki directories."
216 (defvar muse-current-project nil
217 "Project we are currently visiting.")
218 (make-variable-buffer-local 'muse-current-project
)
220 (defsubst muse-project
(&optional project
)
221 "Resolve the given PROJECT into a full Muse project, if it is a string."
223 (or muse-current-project
224 (muse-project-of-file))
225 (if (stringp project
)
226 (assoc project muse-project-alist
)
227 (muse-assert (consp project
))
230 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
231 "Return a filename if PAGE exists within the given Muse PROJECT."
232 (setq project
(muse-project project
))
233 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
235 (defun muse-project-private-p (file)
236 "Return non-nil if NAME is a private page with PROJECT."
237 (unless muse-under-windows-p
238 (setq file
(file-truename file
))
239 (if (file-attributes file
) ; don't publish if no attributes exist
240 (or (when (eq ?-
(aref (nth 8 (file-attributes
241 (file-name-directory file
))) 7))
243 "The " (file-name-directory file
)
244 " directory must be readable by others"
245 " in order for its contents to be published.")))
246 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
249 (defun muse-project-file-entries (path)
250 (let* ((names (list t
))
253 ((file-directory-p path
)
254 (dolist (file (directory-files
255 path t
(when muse-file-extension
256 (concat "." muse-file-extension
"\\'"))))
257 (unless (or (string-match muse-project-ignore-regexp file
)
258 (file-directory-p file
))
260 (cons (cons (muse-page-name file
) file
) nil
))
261 (setq lnames
(cdr lnames
)))))
262 ((file-readable-p path
)
264 (cons (cons (muse-page-name path
) path
) nil
))
265 (setq lnames
(cdr lnames
)))
267 (muse-assert (file-name-directory path
))
268 (dolist (file (directory-files
269 (file-name-directory path
) t
270 (file-name-nondirectory path
)))
271 (unless (string-match muse-project-ignore-regexp file
)
273 (cons (cons (muse-page-name file
) file
) nil
))
274 (setq lnames
(cdr lnames
))))))
277 (defun muse-project-file-alist (&optional project no-check-p
)
278 "Return member filenames for the given Muse PROJECT.
279 On UNIX, this list is only updated if one of the directories'
280 contents have changed. On Windows, it is always reread from
282 (setq project
(muse-project project
))
283 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
285 ;; Determine the last modified of any directory mentioned in the
286 ;; project's pattern list
287 (unless (or muse-under-windows-p no-check-p
)
288 (let ((pats (cadr project
)))
290 (if (symbolp (car pats
))
291 (setq pats
(cddr pats
))
292 (let ((dir (or (and (file-directory-p (car pats
)) (car pats
))
293 (and (not (file-readable-p (car pats
)))
295 (file-name-directory (car pats
)))
296 (file-name-directory (car pats
))))))
298 (let ((mod-time (nth 5 (file-attributes dir
))))
299 (if (or (null last-mod
)
301 (muse-time-less-p last-mod mod-time
)))
302 (setq last-mod mod-time
)))))
303 (setq pats
(cdr pats
))))))
304 ;; Either return the currently known list, or read it again from
306 (if (or (and no-check-p
(cadr file-alist
))
307 (not (or muse-under-windows-p
308 (null (cddr file-alist
))
310 (muse-time-less-p (cddr file-alist
) last-mod
))))
313 (setcdr (cdr file-alist
) last-mod
)
314 (setq file-alist
(cons (car project
) (cons nil last-mod
))
315 muse-project-file-alist
316 (cons file-alist muse-project-file-alist
)))
317 ;; Read in all of the file entries
321 (let* ((names (list t
))
322 (pats (cadr project
)))
324 (if (symbolp (car pats
))
325 (setq pats
(cddr pats
))
326 (nconc names
(muse-project-file-entries (car pats
)))
327 (setq pats
(cdr pats
))))
330 (defun muse-project-of-file (&optional pathname
)
331 "Determine which project the given PATHNAME relates to.
332 If PATHNAME is nil, the current buffer's filename is used."
333 (if (and (null pathname
) muse-current-project
)
335 (setq pathname
(or pathname
336 (and (boundp 'muse-publishing-current-file
)
337 muse-publishing-current-file
)
341 (not (string-match muse-project-ignore-regexp pathname
)))
342 (let* ((file (file-truename pathname
))
343 (dir (file-name-directory file
))
344 (project-entry muse-project-alist
)
346 (while (and project-entry
(not found
))
347 (let ((pats (car (cdar project-entry
))))
348 (while (and pats
(not found
))
349 (if (symbolp (car pats
))
350 (setq pats
(cddr pats
))
351 (let ((truename (file-truename (car pats
))))
352 (if (or (string= truename file
)
353 (string= truename dir
)
354 (string-match truename file
))
355 (setq found
(car project-entry
))))
356 (setq pats
(cdr pats
))))
357 (setq project-entry
(cdr project-entry
))))
360 (defun muse-read-project (prompt &optional no-check-p no-assume
)
361 "Read a project name from the minibuffer, if it can't be figured
363 (if (null muse-project-alist
)
364 (error "There are no Muse projects defined; see `muse-project-alist'.")
365 (or (unless no-check-p
366 (muse-project-of-file))
367 (if (and (not no-assume
)
368 (= 1 (length muse-project-alist
)))
369 (car muse-project-alist
)
370 (assoc (completing-read prompt muse-project-alist
)
371 muse-project-alist
)))))
373 (defvar muse-project-page-history nil
)
375 (defun muse-read-project-file (project prompt
&optional default
)
376 (let ((name (completing-read prompt
(muse-project-file-alist project
)
377 nil nil nil
'muse-project-page-history
379 (cons name
(muse-project-page-file name project
))))
381 (defun muse-project-find-file (name project
&optional command directory
)
382 "Open the Muse page given by NAME in PROJECT.
383 If COMMAND is non-nil, it is the function used to visit the file.
384 If DIRECTORY is non-nil, it is the directory in which the page
385 will be created if it does not already exist. Otherwise, the
386 first directory within the project's fileset is used."
388 (let* ((project (muse-read-project "Find in project: "
390 (default (muse-get-keyword :default
(cadr project
)))
391 (entry (muse-read-project-file
393 (format "Find page: (default: %s) "
397 (list entry project
)))
398 (setq project
(muse-project project
))
399 (let ((project-name (car project
)))
400 (unless (interactive-p)
401 (setq project
(muse-project project
)
402 name
(cons name
(muse-project-page-file name project
))))
403 ;; If we're given a relative name, open it as-is
405 (save-match-data (string-match "\\.\\./" (car name
))))
406 (setcdr name
(if muse-file-extension
407 (concat (car name
) "." muse-file-extension
)
409 ;; At this point, name is (PAGE . FILE).
411 (let ((pats (cadr project
)))
412 (while (and pats
(null directory
))
413 (if (symbolp (car pats
))
414 (setq pats
(cddr pats
))
415 (if (file-directory-p (car pats
))
416 (setq directory
(car pats
) pats nil
)
417 (setq pats
(cdr pats
))))))
419 (let ((filename (expand-file-name
420 (if muse-file-extension
421 (concat (car name
) "." muse-file-extension
)
424 (unless (file-exists-p directory
)
425 (make-directory directory t
))
426 (setcdr name filename
)))))
429 (funcall (or command
'find-file
) (cdr name
))
430 (error "There is no page %s in project %s."
431 (car name
) project-name
))))
433 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
434 "Given STYLES, return a list of the ones that are considered for FILE.
435 The name of a project may be used for STYLES."
436 (when (stringp styles
)
437 (setq styles
(cddr (muse-project styles
))))
438 (muse-assert (and file styles
))
440 (dolist (style styles
)
441 (let ((include-regexp (muse-style-element :include style
))
442 (exclude-regexp (muse-style-element :exclude style
)))
443 (when (and (or ignore-regexp
444 (and (null include-regexp
)
445 (null exclude-regexp
))
447 (string-match include-regexp file
)
448 (not (string-match exclude-regexp file
))))
449 (or (not (file-exists-p file
))
450 (not (muse-project-private-p file
))))
451 (add-to-list 'used-styles style
))))
454 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
455 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
457 (dolist (style styles
)
458 (let ((output-dir (muse-style-element :path style
)))
459 ;; ensure the publishing location is available
460 (unless (file-exists-p output-dir
)
461 (message "Creating publishing directory %s" output-dir
)
462 (make-directory output-dir
))
463 ;; publish the member file!
464 (if (muse-publish-file file style output-dir force
)
465 (setq published t
))))
468 (defun muse-project-save-buffers (&optional project
)
469 (setq project
(muse-project project
))
473 (and (buffer-modified-p buffer
)
474 (not (buffer-base-buffer buffer
))
475 (or (buffer-file-name buffer
)
478 (and buffer-offer-save
479 (> (buffer-size) 0))))
480 (with-current-buffer buffer
481 (let ((proj (muse-project-of-file)))
482 (and proj
(string= (car proj
)
484 (if (buffer-file-name buffer
)
485 (format "Save file %s? "
486 (buffer-file-name buffer
))
487 (format "Save buffer %s? "
488 (buffer-name buffer
))))))
494 '("buffer" "buffers" "save")
495 (if (boundp 'save-some-buffers-action-alist
)
496 save-some-buffers-action-alist
)))
498 (defun muse-project-publish (project &optional force
)
499 "Publish the pages of PROJECT that need publishing."
500 (interactive (list (muse-read-project "Publish project: " nil t
)
502 (setq project
(muse-project project
))
503 (let ((styles (cddr project
))
504 (muse-current-project project
)
506 ;; determine the style from the project, or else ask
508 (setq styles
(list (muse-publish-get-style))))
509 ;; prompt to save any buffers related to this project
510 (muse-project-save-buffers project
)
511 ;; run hook before publishing begins
512 (run-hook-with-args 'muse-before-project-publish-hook project
)
513 ;; publish all files in the project, for each style; the actual
514 ;; publishing will only happen if the files are newer than the
515 ;; last published output, or if the file is listed in
516 ;; :force-publish. Files in :force-publish will not trigger the
517 ;; "All pages need to be published" message.
518 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
)))
519 (file-alist (muse-project-file-alist project
)))
520 (dolist (pair file-alist
)
521 (when (muse-project-publish-file (cdr pair
) styles force
)
522 (setq forced-files
(delete (car pair
) forced-files
))
524 (dolist (file forced-files
)
525 (muse-project-publish-file (cdr (assoc file file-alist
)) styles t
)))
526 ;; run hook after publishing ends
527 (run-hook-with-args 'muse-after-project-publish-hook project
)
528 ;; notify the user that everything is now done
530 (message "All pages in %s have been published." (car project
))
531 (message "No pages in %s need publishing at this time."
534 (defun muse-project-batch-publish ()
535 "Publish Muse files in batch mode."
536 (let ((muse-batch-publishing-p t
)
538 (if (string= "--force" (or (car command-line-args-left
) ""))
540 command-line-args-left
(cdr command-line-args-left
)))
541 (if command-line-args-left
542 (dolist (project command-line-args-left
)
543 (message "Publishing project %s ..." project
)
544 (muse-project-publish project force
))
545 (message "No projects specified."))))
548 (put 'make-local-hook
'byte-compile nil
))
550 (defun muse-project-set-variables ()
551 "Load project-specific variables."
552 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
555 (setq sym
(car vars
))
556 (setq custom-set
(or (get sym
'custom-set
) 'set
))
557 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
558 (make-local-hook sym
)
559 (make-local-variable sym
)))
560 (funcall custom-set var
(car (cdr vars
)))
561 (setq vars
(cdr (cdr vars
))))))
563 (defun muse-project-delete-output-files (project)
565 (list (muse-read-project "Remove all output files for project: " nil t
)))
566 (setq project
(muse-project project
))
567 (let ((file-alist (muse-project-file-alist project
))
568 (styles (cddr project
))
570 (dolist (entry file-alist
)
571 (dolist (style styles
)
573 (and (setq path
(muse-style-element :path style
))
575 (concat (muse-style-element :prefix style
)
577 (or (muse-style-element :osuffix style
)
578 (muse-style-element :suffix style
)))
581 (muse-delete-file-if-exists output-file
))))))
583 (provide 'muse-project
)
585 ;;; muse-project.el ends here