muse-xml: Separate section from title.
[muse-el.git] / lisp / muse-project.el
blob7caebe291af1169506326abdfb58113431354c86
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 "Timestamps file"
185 (const :tag ":timestamps" ":timestamps")
186 (file))
187 (list :inline t
188 :tag "Path"
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")
200 muse-project))
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."
213 :type 'regexp
214 :group 'muse-regexp)
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."
222 (if (null project)
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))
228 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))
242 (message (concat
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)))
247 t)))
249 (defun muse-project-file-entries (path)
250 (let* ((names (list t))
251 (lnames names))
252 (cond
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))
259 (setcdr lnames
260 (cons (cons (muse-page-name file) file) nil))
261 (setq lnames (cdr lnames)))))
262 ((file-readable-p path)
263 (setcdr lnames
264 (cons (cons (muse-page-name path) path) nil))
265 (setq lnames (cdr lnames)))
266 (t ; regexp
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)
272 (setcdr lnames
273 (cons (cons (muse-page-name file) file) nil))
274 (setq lnames (cdr lnames))))))
275 (cdr names)))
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
281 disk."
282 (setq project (muse-project project))
283 (let ((file-alist (assoc (car project) muse-project-file-alist))
284 last-mod)
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)))
289 (while pats
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)))
294 (file-directory-p
295 (file-name-directory (car pats)))
296 (file-name-directory (car pats))))))
297 (if dir
298 (let ((mod-time (nth 5 (file-attributes dir))))
299 (if (or (null last-mod)
300 (and mod-time
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
305 ;; disk
306 (if (or (and no-check-p (cadr file-alist))
307 (not (or muse-under-windows-p
308 (null (cddr file-alist))
309 (null last-mod)
310 (muse-time-less-p (cddr file-alist) last-mod))))
311 (cadr file-alist)
312 (if file-alist
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
318 (save-match-data
319 (setcar
320 (cdr file-alist)
321 (let* ((names (list t))
322 (pats (cadr project)))
323 (while pats
324 (if (symbolp (car pats))
325 (setq pats (cddr pats))
326 (nconc names (muse-project-file-entries (car pats)))
327 (setq pats (cdr pats))))
328 (cdr names)))))))
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)
334 muse-current-project
335 (setq pathname (or pathname
336 (and (boundp 'muse-publishing-current-file)
337 muse-publishing-current-file)
338 buffer-file-name))
339 (save-match-data
340 (when (and pathname
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)
345 found)
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))))
358 found)))))
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
362 out."
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
378 default)))
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."
387 (interactive
388 (let* ((project (muse-read-project "Find in project: "
389 current-prefix-arg))
390 (default (muse-get-keyword :default (cadr project)))
391 (entry (muse-read-project-file
392 project (if default
393 (format "Find page: (default: %s) "
394 default)
395 "Find page: ")
396 default)))
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
404 (if (and (car name)
405 (save-match-data (string-match "\\.\\./" (car name))))
406 (setcdr name (if muse-file-extension
407 (concat (car name) "." muse-file-extension)
408 (car name)))
409 ;; At this point, name is (PAGE . FILE).
410 (unless (cdr name)
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))))))
418 (when directory
419 (let ((filename (expand-file-name
420 (if muse-file-extension
421 (concat (car name) "." muse-file-extension)
422 (car name))
423 directory)))
424 (unless (file-exists-p directory)
425 (make-directory directory t))
426 (setcdr name filename)))))
427 ;; Open the file
428 (if (cdr name)
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))
439 (let (used-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))
446 (if include-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))))
452 used-styles))
454 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
455 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
456 (let (published)
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))))
466 published))
468 (defun muse-project-save-buffers (&optional project)
469 (setq project (muse-project project))
470 (map-y-or-n-p
471 (function
472 (lambda (buffer)
473 (and (buffer-modified-p buffer)
474 (not (buffer-base-buffer buffer))
475 (or (buffer-file-name buffer)
476 (progn
477 (set-buffer 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)
483 (car project)))))
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))))))
489 (function
490 (lambda (buffer)
491 (set-buffer buffer)
492 (save-buffer)))
493 (buffer-list)
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)
501 current-prefix-arg))
502 (setq project (muse-project project))
503 (let ((styles (cddr project))
504 (muse-current-project project)
505 published)
506 ;; determine the style from the project, or else ask
507 (unless styles
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))
523 (setq published t)))
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
529 (if published
530 (message "All pages in %s have been published." (car project))
531 (message "No pages in %s need publishing at this time."
532 (car project)))))
534 (defun muse-project-batch-publish ()
535 "Publish Muse files in batch mode."
536 (let ((muse-batch-publishing-p t)
537 force)
538 (if (string= "--force" (or (car command-line-args-left) ""))
539 (setq force t
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."))))
547 (eval-when-compile
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)))
553 sym custom-set var)
554 (while vars
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)
564 (interactive
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))
569 output-file path)
570 (dolist (entry file-alist)
571 (dolist (style styles)
572 (setq output-file
573 (and (setq path (muse-style-element :path style))
574 (expand-file-name
575 (concat (muse-style-element :prefix style)
576 (car entry)
577 (or (muse-style-element :osuffix style)
578 (muse-style-element :suffix style)))
579 path)))
580 (if output-file
581 (muse-delete-file-if-exists output-file))))))
583 (provide 'muse-project)
585 ;;; muse-project.el ends here