Make customize of muse-project-alist work in Emacs21 and XEmacs.
[muse-el.git] / lisp / muse-project.el
blob1acd8240ce0ae851858aedd531b659d331a42493
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
10 ;; version.
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
15 ;; for more details.
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.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;;; Code:
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse)
35 (require 'muse-publish)
37 (defgroup muse-project nil
38 "Options controlling the behavior of Muse project handling."
39 :group 'muse)
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)"
45 :type 'hook
46 :group 'muse-project)
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)"
52 :type 'hook
53 :group 'muse-project)
55 (defun muse-project-alist-get (sym)
56 "Turn `muse-project-alist' into something we can customize easily."
57 (when (boundp sym)
58 (let* ((val (copy-alist (symbol-value sym)))
59 (head val))
60 (while val
61 (let ((head (car (cdar val)))
62 res)
63 ;; Turn settings of first part into cons cells, symbol->string
64 (while head
65 (cond ((stringp (car head))
66 (add-to-list 'res (car head) t)
67 (setq head (cdr head)))
68 ((symbolp (car head))
69 (add-to-list 'res (list (symbol-name (car head))
70 (cadr head)) t)
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
77 (while (cdr styles)
78 (let ((head (cadr styles))
79 res)
80 (while (consp head)
81 (setq res (plist-put res (symbol-name (car head))
82 (cadr head)))
83 (setq head (cddr head)))
84 (setcdr styles (cons res (cddr styles))))
85 (setq styles (cdr styles))))
86 (setq val (cdr val)))
87 head)))
89 (defun muse-project-alist-set (sym val)
90 "Turn customized version of `muse-project-alist' into something
91 Muse can make use of."
92 (set sym val)
93 (while val
94 (let ((head (car (cdar val)))
95 res)
96 ;; Turn cons cells into flat list, string->symbol
97 (while head
98 (cond ((stringp (car head))
99 (add-to-list 'res (car head) t))
100 ((consp (car head))
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
107 (while (cdr styles)
108 (let ((head (cadr styles))
109 res)
110 (while (consp head)
111 (setq res (plist-put res (intern (car head))
112 (cadr 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."
120 :format "\n%v"
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"
126 (choice
127 (string :tag "Directory")
128 (list :tag "Book function"
129 (const :tag ":book-funcall" ":book-funcall")
130 (choice (function)
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")
143 (const t))
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")
156 (const t))
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")
163 (choice (function)
164 (sexp :tag "Unknown")))))
165 (repeat :tag "Styles" :format "%{%t%}:\n%v%i\n\n"
166 (set :tag "Style"
167 (list :inline t
168 :tag "Publishing style"
169 (const :tag ":base" ":base")
170 (string :tag "Style"))
171 (list :inline t
172 :tag "Base URL"
173 (const :tag ":base-url" ":base-url")
174 (string :tag "URL"))
175 (list :inline t
176 :tag "Exclude matching"
177 (const :tag ":exclude" ":exclude")
178 (regexp))
179 (list :inline t
180 :tag "Include matching"
181 (const :tag ":include" ":include")
182 (regexp))
183 (list :inline t
184 :tag "Path"
185 (const :tag ":path" ":path")
186 (string :tag "Path"))))))
188 (defcustom muse-project-alist nil
189 "An alist of Muse projects.
190 A project defines a fileset, and a list of custom attributes for use
191 when publishing files in that project."
192 :type '(choice (const :tag "No projects defined." nil)
193 (repeat (cons :format "%{%t%}:\n\n%v"
194 :tag "Project" :indent 4
195 (string :tag "Project name")
196 muse-project))
197 (sexp :tag "Cannot parse expression"))
198 :get 'muse-project-alist-get
199 :set 'muse-project-alist-set
200 :group 'muse-project)
202 (defvar muse-project-file-alist nil
203 "This variable is automagically constructed as needed.")
205 (defcustom muse-project-ignore-regexp
206 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
207 "A regexp matching files to be ignored in Wiki directories."
208 :type 'regexp
209 :group 'muse-regexp)
211 (defvar muse-current-project nil
212 "Project we are currently visiting.")
213 (make-variable-buffer-local 'muse-current-project)
215 (defsubst muse-project (&optional project)
216 "Resolve the given PROJECT into a full Muse project, if it is a string."
217 (if (null project)
218 (or muse-current-project
219 (muse-project-of-file))
220 (if (stringp project)
221 (assoc project muse-project-alist)
222 (muse-assert (consp project))
223 project)))
225 (defsubst muse-project-page-file (page project &optional no-check-p)
226 "Return a filename if PAGE exists within the given Muse PROJECT."
227 (setq project (muse-project project))
228 (cdr (assoc page (muse-project-file-alist project no-check-p))))
230 (defun muse-project-private-p (file)
231 "Return non-nil if NAME is a private page with PROJECT."
232 (unless muse-under-windows-p
233 (setq file (file-truename file))
234 (if (file-attributes file) ; don't publish if no attributes exist
235 (or (when (eq ?- (aref (nth 8 (file-attributes
236 (file-name-directory file))) 7))
237 (message (concat
238 "The " (file-name-directory file)
239 " directory must be readable by others"
240 " in order for its contents to be published.")))
241 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
242 t)))
244 (defun muse-project-file-entries (path)
245 (let* ((names (list t))
246 (lnames names))
247 (cond
248 ((file-directory-p path)
249 (dolist (file (directory-files
250 path t (when muse-file-extension
251 (concat "." muse-file-extension "\\'"))))
252 (unless (or (string-match muse-project-ignore-regexp file)
253 (file-directory-p file))
254 (setcdr lnames
255 (cons (cons (muse-page-name file) file) nil))
256 (setq lnames (cdr lnames)))))
257 ((file-readable-p path)
258 (setcdr lnames
259 (cons (cons (muse-page-name path) path) nil))
260 (setq lnames (cdr lnames)))
261 (t ; regexp
262 (muse-assert (file-name-directory path))
263 (dolist (file (directory-files
264 (file-name-directory path) t
265 (file-name-nondirectory path)))
266 (unless (string-match muse-project-ignore-regexp file)
267 (setcdr lnames
268 (cons (cons (muse-page-name file) file) nil))
269 (setq lnames (cdr lnames))))))
270 (cdr names)))
272 (defun muse-project-file-alist (&optional project no-check-p)
273 "Return member filenames for the given Muse PROJECT.
274 On UNIX, this list is only updated if one of the directories'
275 contents have changed. On Windows, it is always reread from
276 disk."
277 (setq project (muse-project project))
278 (let ((file-alist (assoc (car project) muse-project-file-alist))
279 last-mod)
280 ;; Determine the last modified of any directory mentioned in the
281 ;; project's pattern list
282 (unless (or muse-under-windows-p no-check-p)
283 (let ((pats (cadr project)))
284 (while pats
285 (if (symbolp (car pats))
286 (setq pats (cddr pats))
287 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
288 (and (not (file-readable-p (car pats)))
289 (file-directory-p
290 (file-name-directory (car pats)))
291 (file-name-directory (car pats))))))
292 (if dir
293 (let ((mod-time (nth 5 (file-attributes dir))))
294 (if (or (null last-mod)
295 (and mod-time
296 (muse-time-less-p last-mod mod-time)))
297 (setq last-mod mod-time)))))
298 (setq pats (cdr pats))))))
299 ;; Either return the currently known list, or read it again from
300 ;; disk
301 (if (or (and no-check-p (cadr file-alist))
302 (not (or muse-under-windows-p
303 (null (cddr file-alist))
304 (null last-mod)
305 (muse-time-less-p (cddr file-alist) last-mod))))
306 (cadr file-alist)
307 (if file-alist
308 (setcdr (cdr file-alist) last-mod)
309 (setq file-alist (cons (car project) (cons nil last-mod))
310 muse-project-file-alist
311 (cons file-alist muse-project-file-alist)))
312 ;; Read in all of the file entries
313 (save-match-data
314 (setcar
315 (cdr file-alist)
316 (let* ((names (list t))
317 (pats (cadr project)))
318 (while pats
319 (if (symbolp (car pats))
320 (setq pats (cddr pats))
321 (nconc names (muse-project-file-entries (car pats)))
322 (setq pats (cdr pats))))
323 (cdr names)))))))
325 (defun muse-project-of-file (&optional pathname)
326 "Determine which project the given PATHNAME relates to.
327 If PATHNAME is nil, the current buffer's filename is used."
328 (if (and (null pathname) muse-current-project)
329 muse-current-project
330 (setq pathname (or pathname
331 (and (boundp 'muse-publishing-current-file)
332 muse-publishing-current-file)
333 buffer-file-name))
334 (when pathname
335 (let* ((file (file-truename pathname))
336 (dir (file-name-directory file))
337 (project-entry muse-project-alist)
338 found)
339 (save-match-data
340 (while (and project-entry (not found))
341 (let ((pats (car (cdar project-entry))))
342 (while (and pats (not found))
343 (if (symbolp (car pats))
344 (setq pats (cddr pats))
345 (let ((truename (file-truename (car pats))))
346 (if (or (string= truename file)
347 (string= truename dir)
348 (string-match truename file))
349 (setq found (car project-entry))))
350 (setq pats (cdr pats))))
351 (setq project-entry (cdr project-entry)))))
352 found))))
354 (defun muse-read-project (prompt &optional no-check-p no-assume)
355 "Read a project name from the minibuffer, if it can't be figured
356 out."
357 (if (null muse-project-alist)
358 (error "There are no Muse projects defined; see `muse-project-alist'.")
359 (or (unless no-check-p
360 (muse-project-of-file))
361 (if (and (not no-assume)
362 (= 1 (length muse-project-alist)))
363 (car muse-project-alist)
364 (assoc (completing-read prompt muse-project-alist)
365 muse-project-alist)))))
367 (defvar muse-project-page-history nil)
369 (defun muse-read-project-file (project prompt &optional default)
370 (let ((name (completing-read prompt (muse-project-file-alist project)
371 nil nil nil 'muse-project-page-history
372 default)))
373 (cons name (muse-project-page-file name project))))
375 (defun muse-project-find-file (name project &optional command directory)
376 "Open the Muse page given by NAME in PROJECT.
377 If COMMAND is non-nil, it is the function used to visit the file.
378 If DIRECTORY is non-nil, it is the directory in which the page
379 will be created if it does not already exist. Otherwise, the
380 first directory within the project's fileset is used."
381 (interactive
382 (let* ((project (muse-read-project "Find in project: "
383 current-prefix-arg))
384 (default (muse-get-keyword :default (cadr project)))
385 (entry (muse-read-project-file
386 project (if default
387 (format "Find page: (default: %s) "
388 default)
389 "Find page: ")
390 default)))
391 (list entry project)))
392 (setq project (muse-project project))
393 (let ((project-name (car project)))
394 (unless (interactive-p)
395 (setq project (muse-project project)
396 name (cons name (muse-project-page-file name project))))
397 ;; If we're given a relative name, open it as-is
398 (if (and (car name)
399 (save-match-data (string-match "\\.\\./" (car name))))
400 (setcdr name (if muse-file-extension
401 (concat (car name) "." muse-file-extension)
402 (car name)))
403 ;; At this point, name is (PAGE . FILE).
404 (unless (cdr name)
405 (let ((pats (cadr project)))
406 (while (and pats (null directory))
407 (if (symbolp (car pats))
408 (setq pats (cddr pats))
409 (if (file-directory-p (car pats))
410 (setq directory (car pats) pats nil)
411 (setq pats (cdr pats))))))
412 (when directory
413 (let ((filename (expand-file-name
414 (if muse-file-extension
415 (concat (car name) "." muse-file-extension)
416 (car name))
417 directory)))
418 (unless (file-exists-p directory)
419 (make-directory directory t))
420 (setcdr name filename)))))
421 ;; Open the file
422 (if (cdr name)
423 (funcall (or command 'find-file) (cdr name))
424 (error "There is no page %s in project %s."
425 (car name) project-name))))
427 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
428 "Given STYLES, return a list of the ones that are considered for FILE.
429 The name of a project may be used for STYLES."
430 (when (stringp styles)
431 (setq styles (cddr (muse-project styles))))
432 (muse-assert (and file styles))
433 (let (used-styles)
434 (dolist (style styles)
435 (let ((include-regexp (muse-style-element :include style))
436 (exclude-regexp (muse-style-element :exclude style)))
437 (when (and (or ignore-regexp
438 (and (null include-regexp)
439 (null exclude-regexp))
440 (if include-regexp
441 (string-match include-regexp file)
442 (not (string-match exclude-regexp file))))
443 (or (not (file-exists-p file))
444 (not (muse-project-private-p file))))
445 (add-to-list 'used-styles style))))
446 used-styles))
448 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
449 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
450 (let (published)
451 (dolist (style styles)
452 (let ((output-dir (muse-style-element :path style)))
453 ;; ensure the publishing location is available
454 (unless (file-exists-p output-dir)
455 (message "Creating publishing directory %s" output-dir)
456 (make-directory output-dir))
457 ;; publish the member file!
458 (if (muse-publish-file file style output-dir force)
459 (setq published t))))
460 published))
462 (defun muse-project-save-buffers (&optional project)
463 (setq project (muse-project project))
464 (map-y-or-n-p
465 (function
466 (lambda (buffer)
467 (and (buffer-modified-p buffer)
468 (not (buffer-base-buffer buffer))
469 (or (buffer-file-name buffer)
470 (progn
471 (set-buffer buffer)
472 (and buffer-offer-save
473 (> (buffer-size) 0))))
474 (with-current-buffer buffer
475 (let ((proj (muse-project-of-file)))
476 (and proj (string= (car proj)
477 (car project)))))
478 (if (buffer-file-name buffer)
479 (format "Save file %s? "
480 (buffer-file-name buffer))
481 (format "Save buffer %s? "
482 (buffer-name buffer))))))
483 (function
484 (lambda (buffer)
485 (set-buffer buffer)
486 (save-buffer)))
487 (buffer-list)
488 '("buffer" "buffers" "save")
489 (if (boundp 'save-some-buffers-action-alist)
490 save-some-buffers-action-alist)))
492 (defun muse-project-publish (project &optional force)
493 "Publish the pages of PROJECT that need publishing."
494 (interactive (list (muse-read-project "Publish project: " nil t)
495 current-prefix-arg))
496 (setq project (muse-project project))
497 (let ((styles (cddr project))
498 (muse-current-project project)
499 published)
500 ;; determine the style from the project, or else ask
501 (unless styles
502 (setq styles (list (muse-publish-get-style))))
503 ;; prompt to save any buffers related to this project
504 (muse-project-save-buffers project)
505 ;; run hook before publishing begins
506 (run-hook-with-args 'muse-before-project-publish-hook project)
507 ;; publish all files in the project, for each style; the actual
508 ;; publishing will only happen if the files are newer than the
509 ;; last published output
510 (let ((forced-files (muse-get-keyword :force-publish (cadr project))))
511 (dolist (pair (muse-project-file-alist project))
512 (when (muse-project-publish-file
513 (cdr pair) styles (or force (member (car pair) forced-files)))
514 (setq published t))))
515 ;; run hook after publishing ends
516 (run-hook-with-args 'muse-after-project-publish-hook project)
517 ;; notify the user that everything is now done
518 (if published
519 (message "All pages in %s have been published." (car project))
520 (message "No pages in %s need publishing at this time."
521 (car project)))))
523 (defun muse-project-batch-publish ()
524 "Publish Muse files in batch mode."
525 (let ((muse-batch-publishing-p t)
526 force)
527 (if (string= "--force" (or (car command-line-args-left) ""))
528 (setq force t
529 command-line-args-left (cdr command-line-args-left)))
530 (if command-line-args-left
531 (dolist (project command-line-args-left)
532 (message "Publishing project %s ..." project)
533 (muse-project-publish project force))
534 (message "No projects specified."))))
536 (eval-when-compile
537 (put 'make-local-hook 'byte-compile nil))
539 (defun muse-project-set-variables ()
540 "Load project-specific variables."
541 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
542 sym custom-set var)
543 (while vars
544 (setq sym (car vars))
545 (setq custom-set (or (get sym 'custom-set) 'set))
546 (setq var (if (eq (get sym 'custom-type) 'hook)
547 (make-local-hook sym)
548 (make-local-variable sym)))
549 (funcall custom-set var (car (cdr vars)))
550 (setq vars (cdr (cdr vars))))))
552 (defun muse-project-delete-output-files (project)
553 (interactive
554 (list (muse-read-project "Remove all output files for project: " nil t)))
555 (setq project (muse-project project))
556 (let ((file-alist (muse-project-file-alist project))
557 (styles (cddr project))
558 output-file path)
559 (dolist (entry file-alist)
560 (dolist (style styles)
561 (setq output-file
562 (and (setq path (muse-style-element :path style))
563 (expand-file-name
564 (concat (muse-style-element :prefix style)
565 (car entry)
566 (or (muse-style-element :osuffix style)
567 (muse-style-element :suffix style)))
568 path)))
569 (if output-file
570 (muse-delete-file-if-exists output-file))))))
572 (provide 'muse-project)
574 ;;; muse-project.el ends here