Add with-muse-project macro from parts of patch-260.
[muse-el.git] / lisp / muse-project.el
blob4f9be26887c4ae4487236735dee89eb8ae22911a
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 (defvar muse-project-alist-using-customize nil
56 "Used internally by Muse to indicate whether `muse-project-alist'
57 has been modified via the customize interface.")
58 (make-variable-buffer-local 'muse-project-alist-using-customize)
60 (defmacro with-muse-project (project &rest body)
61 `(progn
62 (unless (muse-project ,project)
63 (error "Can't find project %s" ,project))
64 (with-temp-buffer
65 (muse-mode)
66 (setq muse-current-project (muse-project ,project))
67 (muse-project-set-variables)
68 ,@body)))
70 (put 'with-muse-project 'lisp-indent-function 0)
71 (put 'with-muse-project 'edebug-form-spec '(sexp body))
73 (defun muse-project-alist-get (sym)
74 "Turn `muse-project-alist' into something we can customize easily."
75 (when (boundp sym)
76 (setq muse-project-alist-using-customize t)
77 (let* ((val (copy-alist (symbol-value sym)))
78 (head val))
79 (while val
80 (let ((head (car (cdar val)))
81 res)
82 ;; Turn settings of first part into cons cells, symbol->string
83 (while head
84 (cond ((stringp (car head))
85 (add-to-list 'res (car head) t)
86 (setq head (cdr head)))
87 ((symbolp (car head))
88 (add-to-list 'res (list (symbol-name (car head))
89 (cadr head)) t)
90 (setq head (cddr head)))
92 (setq head (cdr head)))))
93 (setcdr (car val) (cons res (cdr (cdar val)))))
94 (let ((styles (cdar val)))
95 ;; Symbol->string in every style
96 (while (cdr styles)
97 (let ((head (cadr styles))
98 res)
99 (while (consp head)
100 (setq res (plist-put res (symbol-name (car head))
101 (cadr head)))
102 (setq head (cddr head)))
103 (setcdr styles (cons res (cddr styles))))
104 (setq styles (cdr styles))))
105 (setq val (cdr val)))
106 head)))
108 (defun muse-project-alist-set (sym val)
109 "Turn customized version of `muse-project-alist' into something
110 Muse can make use of."
111 (set sym val)
112 (when muse-project-alist-using-customize
113 ;; Make sure the unescaped version is written to .emacs
114 (put sym 'saved-value (list (custom-quote val)))
115 ;; Perform unescaping
116 (while val
117 (let ((head (car (cdar val)))
118 res)
119 ;; Turn cons cells into flat list, string->symbol
120 (while head
121 (cond ((stringp (car head))
122 (add-to-list 'res (car head) t))
123 ((consp (car head))
124 (add-to-list 'res (intern (caar head)) t)
125 (add-to-list 'res (car (cdar head)) t)))
126 (setq head (cdr head)))
127 (setcdr (car val) (cons res (cdr (cdar val)))))
128 (let ((styles (cdar val)))
129 ;; String->symbol in every style
130 (while (cdr styles)
131 (let ((head (cadr styles))
132 res)
133 (while (consp head)
134 (setq res (plist-put res (intern (car head))
135 (cadr head)))
136 (setq head (cddr head)))
137 (setcdr styles (cons res (cddr styles))))
138 (setq styles (cdr styles))))
139 (setq val (cdr val)))))
141 (define-widget 'muse-project 'default
142 "A widget that defines a Muse project."
143 :format "\n%v"
144 :value-create 'muse-widget-type-value-create
145 :value-get 'muse-widget-child-value-get
146 :value-delete 'ignore
147 :match 'muse-widget-type-match
148 :type '(cons :format " %v"
149 (repeat :tag "Settings" :format "%{%t%}:\n%v%i\n\n"
150 (choice
151 (string :tag "Directory")
152 (list :tag "Book function"
153 (const :tag ":book-funcall" ":book-funcall")
154 (choice (function)
155 (sexp :tag "Unknown")))
156 (list :tag "Book part"
157 (const :tag ":book-part" ":book-part")
158 (string :tag "Name"))
159 (list :tag "Book style"
160 (const :tag ":book-style" ":book-style")
161 (string :tag "Style"))
162 (list :tag "Default file"
163 (const :tag ":default" ":default")
164 (string :tag "File"))
165 (list :tag "End of book"
166 (const :tag ":book-end" ":book-end")
167 (const t))
168 (list :tag "Force publishing"
169 (const :tag ":force-publish" ":force-publish")
170 (repeat (string :tag "File")))
171 (list :tag "Major mode"
172 (const :tag ":major-mode" ":major-mode")
173 (choice (function :tag "Mode")
174 (sexp :tag "Unknown")))
175 (list :tag "New chapter"
176 (const :tag ":book-chapter" ":book-chapter")
177 (string :tag "Name"))
178 (list :tag "No chapters"
179 (const :tag ":nochapters" ":nochapters")
180 (const t))
181 (list :tag "Set variables"
182 (const :tag ":set" ":set")
183 (repeat (list :inline t
184 (symbol :tag "Variable")
185 (sexp :tag "Setting"))))
186 (list :tag "Visit links using"
187 (const :tag ":visit-link" ":visit-link")
188 (choice (function)
189 (sexp :tag "Unknown")))))
190 (repeat :tag "Styles" :format "%{%t%}:\n%v%i\n\n"
191 (set :tag "Style"
192 (list :inline t
193 :tag "Publishing style"
194 (const :tag ":base" ":base")
195 (string :tag "Style"))
196 (list :inline t
197 :tag "Base URL"
198 (const :tag ":base-url" ":base-url")
199 (string :tag "URL"))
200 (list :inline t
201 :tag "Exclude matching"
202 (const :tag ":exclude" ":exclude")
203 (regexp))
204 (list :inline t
205 :tag "Include matching"
206 (const :tag ":include" ":include")
207 (regexp))
208 (list :inline t
209 :tag "Timestamps file"
210 (const :tag ":timestamps" ":timestamps")
211 (file))
212 (list :inline t
213 :tag "Path"
214 (const :tag ":path" ":path")
215 (string :tag "Path"))))))
217 (defcustom muse-project-alist nil
218 "An alist of Muse projects.
219 A project defines a fileset, and a list of custom attributes for use
220 when publishing files in that project."
221 :type '(choice (const :tag "No projects defined." nil)
222 (repeat (cons :format "%{%t%}:\n\n%v"
223 :tag "Project" :indent 4
224 (string :tag "Project name")
225 muse-project))
226 (sexp :tag "Cannot parse expression"))
227 :get 'muse-project-alist-get
228 :set 'muse-project-alist-set
229 :group 'muse-project)
231 ;; Make it easier to specify a muse-project-alist entry
233 (defcustom muse-project-ignore-regexp
234 (concat "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\|,.*\\)\\'\\|"
235 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\)\\(/\\|\\'\\)")
236 "A regexp matching files to be ignored in Wiki directories."
237 :type 'regexp
238 :group 'muse-regexp)
240 (defun muse-project-recurse-directory (base)
241 "Recusively retrieve all of the directories underneath BASE.
242 A list of these directories is returned.
243 Directories starting with \".\" will be ignored, as well as those
244 which match `muse-project-ignore-regexp'."
245 (when (and (file-directory-p base)
246 (not (string-match muse-project-ignore-regexp base)))
247 (let (list dir)
248 (dolist (file (directory-files base t "^[^.]"))
249 (when (and (file-directory-p file)
250 (not (string-match muse-project-ignore-regexp file)))
251 (setq dir (file-name-nondirectory file))
252 (push dir list)
253 (nconc list (mapcar #'(lambda (item)
254 (concat dir "/" item))
255 (muse-project-recurse-directory file)))))
256 list)))
258 (defun muse-project-alist-styles (entry-dir output-dir style)
259 "Return a list of styles to use in a `muse-project-alist' entry.
260 ENTRY-DIR is the top-level directory of the project.
261 OUTPUT-DIR is where Muse files are published, keeping directory structure.
262 STYLE is the publishing style to use.
264 For an example of the use of this function, see
265 `examples/mwolson/muse-init.el' from the Muse distribution."
266 (cons `(:base ,style :path ,(expand-file-name output-dir)
267 :include ,(concat "/" (file-name-nondirectory entry-dir)
268 "/[^/]+$"))
269 (mapcar (lambda (dir);
270 `(:base ,style
271 :path ,(expand-file-name dir output-dir)
272 :include ,(concat "/" dir "/[^/]+$")))
273 (muse-project-recurse-directory entry-dir))))
275 (defun muse-project-alist-dirs (entry-dir)
276 "Return a list of directories to use in a `muse-project-alist' entry.
277 ENTRY-DIR is the top-level directory of the project.
279 For an example of the use of this function, see
280 `examples/mwolson/muse-init.el' from the Muse distribution."
281 (cons (expand-file-name entry-dir)
282 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
283 (muse-project-recurse-directory entry-dir))))
285 ;; Constructing the file-alist
287 (defvar muse-project-file-alist nil
288 "This variable is automagically constructed as needed.")
290 (defvar muse-current-project nil
291 "Project we are currently visiting.")
292 (make-variable-buffer-local 'muse-current-project)
294 (defsubst muse-project (&optional project)
295 "Resolve the given PROJECT into a full Muse project, if it is a string."
296 (if (null project)
297 (or muse-current-project
298 (muse-project-of-file))
299 (if (stringp project)
300 (assoc project muse-project-alist)
301 (muse-assert (consp project))
302 project)))
304 (defsubst muse-project-page-file (page project &optional no-check-p)
305 "Return a filename if PAGE exists within the given Muse PROJECT."
306 (setq project (muse-project project))
307 (cdr (assoc page (muse-project-file-alist project no-check-p))))
309 (defun muse-project-private-p (file)
310 "Return non-nil if NAME is a private page with PROJECT."
311 (unless muse-under-windows-p
312 (setq file (file-truename file))
313 (if (file-attributes file) ; don't publish if no attributes exist
314 (or (when (eq ?- (aref (nth 8 (file-attributes
315 (file-name-directory file))) 7))
316 (message (concat
317 "The " (file-name-directory file)
318 " directory must be readable by others"
319 " in order for its contents to be published.")))
320 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
321 t)))
323 (defun muse-project-file-entries (path)
324 (let* ((names (list t))
325 (lnames names))
326 (cond
327 ((file-directory-p path)
328 (dolist (file (directory-files
329 path t (when muse-file-extension
330 (concat "." muse-file-extension "\\'"))))
331 (unless (or (string-match muse-project-ignore-regexp file)
332 (file-directory-p file))
333 (setcdr lnames
334 (cons (cons (muse-page-name file) file) nil))
335 (setq lnames (cdr lnames)))))
336 ((file-readable-p path)
337 (setcdr lnames
338 (cons (cons (muse-page-name path) path) nil))
339 (setq lnames (cdr lnames)))
340 (t ; regexp
341 (muse-assert (file-name-directory path))
342 (dolist (file (directory-files
343 (file-name-directory path) t
344 (file-name-nondirectory path)))
345 (unless (string-match muse-project-ignore-regexp file)
346 (setcdr lnames
347 (cons (cons (muse-page-name file) file) nil))
348 (setq lnames (cdr lnames))))))
349 (cdr names)))
351 (defun muse-project-file-alist (&optional project no-check-p)
352 "Return member filenames for the given Muse PROJECT.
353 On UNIX, this list is only updated if one of the directories'
354 contents have changed. On Windows, it is always reread from
355 disk."
356 (setq project (muse-project project))
357 (let ((file-alist (assoc (car project) muse-project-file-alist))
358 last-mod)
359 ;; Determine the last modified of any directory mentioned in the
360 ;; project's pattern list
361 (unless (or muse-under-windows-p no-check-p)
362 (let ((pats (cadr project)))
363 (while pats
364 (if (symbolp (car pats))
365 (setq pats (cddr pats))
366 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
367 (and (not (file-readable-p (car pats)))
368 (file-directory-p
369 (file-name-directory (car pats)))
370 (file-name-directory (car pats))))))
371 (if dir
372 (let ((mod-time (nth 5 (file-attributes dir))))
373 (if (or (null last-mod)
374 (and mod-time
375 (muse-time-less-p last-mod mod-time)))
376 (setq last-mod mod-time)))))
377 (setq pats (cdr pats))))))
378 ;; Either return the currently known list, or read it again from
379 ;; disk
380 (if (or (and no-check-p (cadr file-alist))
381 (not (or muse-under-windows-p
382 (null (cddr file-alist))
383 (null last-mod)
384 (muse-time-less-p (cddr file-alist) last-mod))))
385 (cadr file-alist)
386 (if file-alist
387 (setcdr (cdr file-alist) last-mod)
388 (setq file-alist (cons (car project) (cons nil last-mod))
389 muse-project-file-alist
390 (cons file-alist muse-project-file-alist)))
391 ;; Read in all of the file entries
392 (save-match-data
393 (setcar
394 (cdr file-alist)
395 (let* ((names (list t))
396 (pats (cadr project)))
397 (while pats
398 (if (symbolp (car pats))
399 (setq pats (cddr pats))
400 (nconc names (muse-project-file-entries (car pats)))
401 (setq pats (cdr pats))))
402 (cdr names)))))))
404 (defun muse-project-of-file (&optional pathname)
405 "Determine which project the given PATHNAME relates to.
406 If PATHNAME is nil, the current buffer's filename is used."
407 (if (and (null pathname) muse-current-project)
408 muse-current-project
409 (unless pathname (setq pathname (muse-current-file)))
410 (save-match-data
411 (when (and pathname
412 (not (string-match muse-project-ignore-regexp pathname)))
413 (let* ((file (file-truename pathname))
414 (dir (file-name-directory file))
415 (project-entry muse-project-alist)
416 found)
417 (while (and project-entry (not found))
418 (let ((pats (car (cdar project-entry))))
419 (while (and pats (not found))
420 (if (symbolp (car pats))
421 (setq pats (cddr pats))
422 (let ((truename (file-truename (car pats))))
423 (if (or (string= truename file)
424 (string= truename dir)
425 (string-match (regexp-quote truename) file))
426 (setq found (car project-entry))))
427 (setq pats (cdr pats))))
428 (setq project-entry (cdr project-entry))))
429 found)))))
431 (defun muse-read-project (prompt &optional no-check-p no-assume)
432 "Read a project name from the minibuffer, if it can't be figured
433 out."
434 (if (null muse-project-alist)
435 (error "There are no Muse projects defined; see `muse-project-alist'.")
436 (or (unless no-check-p
437 (muse-project-of-file))
438 (if (and (not no-assume)
439 (= 1 (length muse-project-alist)))
440 (car muse-project-alist)
441 (assoc (completing-read prompt muse-project-alist)
442 muse-project-alist)))))
444 (defvar muse-project-page-history nil)
446 (defun muse-read-project-file (project prompt &optional default)
447 (let ((name (completing-read prompt (muse-project-file-alist project)
448 nil nil nil 'muse-project-page-history
449 default)))
450 (cons name (muse-project-page-file name project))))
452 (defun muse-project-find-file (name project &optional command directory)
453 "Open the Muse page given by NAME in PROJECT.
454 If COMMAND is non-nil, it is the function used to visit the file.
455 If DIRECTORY is non-nil, it is the directory in which the page
456 will be created if it does not already exist. Otherwise, the
457 first directory within the project's fileset is used."
458 (interactive
459 (let* ((project (muse-read-project "Find in project: "
460 current-prefix-arg))
461 (default (muse-get-keyword :default (cadr project)))
462 (entry (muse-read-project-file
463 project (if default
464 (format "Find page: (default: %s) "
465 default)
466 "Find page: ")
467 default)))
468 (list entry project)))
469 (setq project (muse-project project))
470 (let ((project-name (car project)))
471 (unless (interactive-p)
472 (setq project (muse-project project)
473 name (cons name (muse-project-page-file name project))))
474 ;; If we're given a relative or absolute filename, open it as-is
475 (if (and (car name)
476 (save-match-data (string-match muse-file-regexp (car name))))
477 (setcdr name (car name))
478 ;; At this point, name is (PAGE . FILE).
479 (unless (cdr name)
480 (let ((pats (cadr project)))
481 (while (and pats (null directory))
482 (if (symbolp (car pats))
483 (setq pats (cddr pats))
484 (if (file-directory-p (car pats))
485 (setq directory (car pats) pats nil)
486 (setq pats (cdr pats))))))
487 (when directory
488 (let ((filename (expand-file-name
489 (if muse-file-extension
490 (concat (car name) "." muse-file-extension)
491 (car name))
492 directory)))
493 (unless (file-exists-p directory)
494 (make-directory directory t))
495 (setcdr name filename)))))
496 ;; Open the file
497 (if (cdr name)
498 (funcall (or command 'find-file) (cdr name))
499 (error "There is no page %s in project %s."
500 (car name) project-name))))
502 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
503 "Given STYLES, return a list of the ones that are considered for FILE.
504 The name of a project may be used for STYLES."
505 (when (stringp styles)
506 (setq styles (cddr (muse-project styles))))
507 (when (and file styles)
508 (let (used-styles)
509 (dolist (style styles)
510 (let ((include-regexp (muse-style-element :include style))
511 (exclude-regexp (muse-style-element :exclude style)))
512 (when (and (or ignore-regexp
513 (and (null include-regexp)
514 (null exclude-regexp))
515 (if include-regexp
516 (string-match include-regexp file)
517 (not (string-match exclude-regexp file))))
518 (or (not (file-exists-p file))
519 (not (muse-project-private-p file))))
520 (add-to-list 'used-styles style))))
521 used-styles)))
523 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
524 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
525 (let (published)
526 (dolist (style styles)
527 (let ((output-dir (muse-style-element :path style)))
528 ;; ensure the publishing location is available
529 (unless (file-exists-p output-dir)
530 (message "Creating publishing directory %s" output-dir)
531 (make-directory output-dir))
532 ;; publish the member file!
533 (if (muse-publish-file file style output-dir force)
534 (setq published t))))
535 published))
537 (defun muse-project-save-buffers (&optional project)
538 (setq project (muse-project project))
539 (map-y-or-n-p
540 (function
541 (lambda (buffer)
542 (and (buffer-modified-p buffer)
543 (not (buffer-base-buffer buffer))
544 (or (buffer-file-name buffer)
545 (progn
546 (set-buffer buffer)
547 (and buffer-offer-save
548 (> (buffer-size) 0))))
549 (with-current-buffer buffer
550 (let ((proj (muse-project-of-file)))
551 (and proj (string= (car proj)
552 (car project)))))
553 (if (buffer-file-name buffer)
554 (format "Save file %s? "
555 (buffer-file-name buffer))
556 (format "Save buffer %s? "
557 (buffer-name buffer))))))
558 (function
559 (lambda (buffer)
560 (set-buffer buffer)
561 (save-buffer)))
562 (buffer-list)
563 '("buffer" "buffers" "save")
564 (if (boundp 'save-some-buffers-action-alist)
565 save-some-buffers-action-alist)))
567 (defun muse-project-publish (project &optional force)
568 "Publish the pages of PROJECT that need publishing."
569 (interactive (list (muse-read-project "Publish project: " nil t)
570 current-prefix-arg))
571 (setq project (muse-project project))
572 (let ((styles (cddr project))
573 (muse-current-project project)
574 published)
575 ;; determine the style from the project, or else ask
576 (unless styles
577 (setq styles (list (muse-publish-get-style))))
578 ;; prompt to save any buffers related to this project
579 (muse-project-save-buffers project)
580 ;; run hook before publishing begins
581 (run-hook-with-args 'muse-before-project-publish-hook project)
582 ;; publish all files in the project, for each style; the actual
583 ;; publishing will only happen if the files are newer than the
584 ;; last published output, or if the file is listed in
585 ;; :force-publish. Files in :force-publish will not trigger the
586 ;; "All pages need to be published" message.
587 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
588 (file-alist (muse-project-file-alist project)))
589 (dolist (pair file-alist)
590 (when (muse-project-publish-file (cdr pair) styles force)
591 (setq forced-files (delete (car pair) forced-files))
592 (setq published t)))
593 (dolist (file forced-files)
594 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
595 ;; run hook after publishing ends
596 (run-hook-with-args 'muse-after-project-publish-hook project)
597 ;; notify the user that everything is now done
598 (if published
599 (message "All pages in %s have been published." (car project))
600 (message "No pages in %s need publishing at this time."
601 (car project)))))
603 (defun muse-project-batch-publish ()
604 "Publish Muse files in batch mode."
605 (let ((muse-batch-publishing-p t)
606 force)
607 (if (string= "--force" (or (car command-line-args-left) ""))
608 (setq force t
609 command-line-args-left (cdr command-line-args-left)))
610 (if command-line-args-left
611 (dolist (project command-line-args-left)
612 (message "Publishing project %s ..." project)
613 (muse-project-publish project force))
614 (message "No projects specified."))))
616 (eval-when-compile
617 (put 'make-local-hook 'byte-compile nil))
619 (defun muse-project-set-variables ()
620 "Load project-specific variables."
621 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
622 sym custom-set var)
623 (while vars
624 (setq sym (car vars))
625 (setq custom-set (or (get sym 'custom-set) 'set))
626 (setq var (if (eq (get sym 'custom-type) 'hook)
627 (make-local-hook sym)
628 (make-local-variable sym)))
629 (funcall custom-set var (car (cdr vars)))
630 (setq vars (cdr (cdr vars))))))
632 (defun muse-project-delete-output-files (project)
633 (interactive
634 (list (muse-read-project "Remove all output files for project: " nil t)))
635 (setq project (muse-project project))
636 (let ((file-alist (muse-project-file-alist project))
637 (styles (cddr project))
638 output-file path)
639 (dolist (entry file-alist)
640 (dolist (style styles)
641 (setq output-file
642 (and (setq path (muse-style-element :path style))
643 (expand-file-name
644 (concat (muse-style-element :prefix style)
645 (car entry)
646 (or (muse-style-element :osuffix style)
647 (muse-style-element :suffix style)))
648 path)))
649 (if output-file
650 (muse-delete-file-if-exists output-file))))))
652 (provide 'muse-project)
654 ;;; muse-project.el ends here