Fix trailing backslash error.
[muse-el.git] / lisp / muse-project.el
blob5e966a0ca94b528c7e87b4720de7125754574816
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 ;; Make it easier to specify a muse-project-alist entry
208 (defcustom muse-project-ignore-regexp
209 (concat "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\|,.*\\)\\'\\|"
210 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\)\\(/\\|\\'\\)")
211 "A regexp matching files to be ignored in Wiki directories."
212 :type 'regexp
213 :group 'muse-regexp)
215 (defun muse-project-recurse-directory (base)
216 "Recusively retrieve all of the directories underneath BASE.
217 A list of these directories is returned.
218 Directories starting with \".\" will be ignored, as well as those
219 which match `muse-project-ignore-regexp'."
220 (when (and (file-directory-p base)
221 (not (string-match muse-project-ignore-regexp base)))
222 (let (list dir)
223 (dolist (file (directory-files base t "^[^.]"))
224 (when (and (file-directory-p file)
225 (not (string-match muse-project-ignore-regexp file)))
226 (setq dir (file-name-nondirectory file))
227 (push dir list)
228 (nconc list (mapcar #'(lambda (item)
229 (concat dir "/" item))
230 (muse-project-recurse-directory file)))))
231 list)))
233 (defun muse-project-alist-styles (entry-dir output-dir style)
234 "Return a list of styles to use in a `muse-project-alist' entry.
235 ENTRY-DIR is the top-level directory of the project.
236 OUTPUT-DIR is where Muse files are published, keeping directory structure.
237 STYLE is the publishing style to use.
239 For an example of the use of this function, see
240 `examples/mwolson/muse-init.el' from the Muse distribution."
241 (cons `(:base ,style :path ,(expand-file-name output-dir)
242 :include ,(concat "/" (file-name-nondirectory entry-dir)
243 "/[^/]+$"))
244 (mapcar (lambda (dir);
245 `(:base ,style
246 :path ,(expand-file-name dir output-dir)
247 :include ,(concat "/" dir "/[^/]+$")))
248 (muse-project-recurse-directory entry-dir))))
250 (defun muse-project-alist-dirs (entry-dir)
251 "Return a list of directories to use in a `muse-project-alist' entry.
252 ENTRY-DIR is the top-level directory of the project.
254 For an example of the use of this function, see
255 `examples/mwolson/muse-init.el' from the Muse distribution."
256 (cons (expand-file-name entry-dir)
257 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
258 (muse-project-recurse-directory entry-dir))))
260 ;; Constructing the file-alist
262 (defvar muse-project-file-alist nil
263 "This variable is automagically constructed as needed.")
265 (defvar muse-current-project nil
266 "Project we are currently visiting.")
267 (make-variable-buffer-local 'muse-current-project)
269 (defsubst muse-project (&optional project)
270 "Resolve the given PROJECT into a full Muse project, if it is a string."
271 (if (null project)
272 (or muse-current-project
273 (muse-project-of-file))
274 (if (stringp project)
275 (assoc project muse-project-alist)
276 (muse-assert (consp project))
277 project)))
279 (defsubst muse-project-page-file (page project &optional no-check-p)
280 "Return a filename if PAGE exists within the given Muse PROJECT."
281 (setq project (muse-project project))
282 (cdr (assoc page (muse-project-file-alist project no-check-p))))
284 (defun muse-project-private-p (file)
285 "Return non-nil if NAME is a private page with PROJECT."
286 (unless muse-under-windows-p
287 (setq file (file-truename file))
288 (if (file-attributes file) ; don't publish if no attributes exist
289 (or (when (eq ?- (aref (nth 8 (file-attributes
290 (file-name-directory file))) 7))
291 (message (concat
292 "The " (file-name-directory file)
293 " directory must be readable by others"
294 " in order for its contents to be published.")))
295 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
296 t)))
298 (defun muse-project-file-entries (path)
299 (let* ((names (list t))
300 (lnames names))
301 (cond
302 ((file-directory-p path)
303 (dolist (file (directory-files
304 path t (when muse-file-extension
305 (concat "." muse-file-extension "\\'"))))
306 (unless (or (string-match muse-project-ignore-regexp file)
307 (file-directory-p file))
308 (setcdr lnames
309 (cons (cons (muse-page-name file) file) nil))
310 (setq lnames (cdr lnames)))))
311 ((file-readable-p path)
312 (setcdr lnames
313 (cons (cons (muse-page-name path) path) nil))
314 (setq lnames (cdr lnames)))
315 (t ; regexp
316 (muse-assert (file-name-directory path))
317 (dolist (file (directory-files
318 (file-name-directory path) t
319 (file-name-nondirectory path)))
320 (unless (string-match muse-project-ignore-regexp file)
321 (setcdr lnames
322 (cons (cons (muse-page-name file) file) nil))
323 (setq lnames (cdr lnames))))))
324 (cdr names)))
326 (defun muse-project-file-alist (&optional project no-check-p)
327 "Return member filenames for the given Muse PROJECT.
328 On UNIX, this list is only updated if one of the directories'
329 contents have changed. On Windows, it is always reread from
330 disk."
331 (setq project (muse-project project))
332 (let ((file-alist (assoc (car project) muse-project-file-alist))
333 last-mod)
334 ;; Determine the last modified of any directory mentioned in the
335 ;; project's pattern list
336 (unless (or muse-under-windows-p no-check-p)
337 (let ((pats (cadr project)))
338 (while pats
339 (if (symbolp (car pats))
340 (setq pats (cddr pats))
341 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
342 (and (not (file-readable-p (car pats)))
343 (file-directory-p
344 (file-name-directory (car pats)))
345 (file-name-directory (car pats))))))
346 (if dir
347 (let ((mod-time (nth 5 (file-attributes dir))))
348 (if (or (null last-mod)
349 (and mod-time
350 (muse-time-less-p last-mod mod-time)))
351 (setq last-mod mod-time)))))
352 (setq pats (cdr pats))))))
353 ;; Either return the currently known list, or read it again from
354 ;; disk
355 (if (or (and no-check-p (cadr file-alist))
356 (not (or muse-under-windows-p
357 (null (cddr file-alist))
358 (null last-mod)
359 (muse-time-less-p (cddr file-alist) last-mod))))
360 (cadr file-alist)
361 (if file-alist
362 (setcdr (cdr file-alist) last-mod)
363 (setq file-alist (cons (car project) (cons nil last-mod))
364 muse-project-file-alist
365 (cons file-alist muse-project-file-alist)))
366 ;; Read in all of the file entries
367 (save-match-data
368 (setcar
369 (cdr file-alist)
370 (let* ((names (list t))
371 (pats (cadr project)))
372 (while pats
373 (if (symbolp (car pats))
374 (setq pats (cddr pats))
375 (nconc names (muse-project-file-entries (car pats)))
376 (setq pats (cdr pats))))
377 (cdr names)))))))
379 (defun muse-project-of-file (&optional pathname)
380 "Determine which project the given PATHNAME relates to.
381 If PATHNAME is nil, the current buffer's filename is used."
382 (if (and (null pathname) muse-current-project)
383 muse-current-project
384 (unless pathname (setq pathname (muse-current-file)))
385 (save-match-data
386 (when (and pathname
387 (not (string-match muse-project-ignore-regexp pathname)))
388 (let* ((file (file-truename pathname))
389 (dir (file-name-directory file))
390 (project-entry muse-project-alist)
391 found)
392 (while (and project-entry (not found))
393 (let ((pats (car (cdar project-entry))))
394 (while (and pats (not found))
395 (if (symbolp (car pats))
396 (setq pats (cddr pats))
397 (let ((truename (file-truename (car pats))))
398 (if (or (string= truename file)
399 (string= truename dir)
400 (string-match (regexp-quote truename) file))
401 (setq found (car project-entry))))
402 (setq pats (cdr pats))))
403 (setq project-entry (cdr project-entry))))
404 found)))))
406 (defun muse-read-project (prompt &optional no-check-p no-assume)
407 "Read a project name from the minibuffer, if it can't be figured
408 out."
409 (if (null muse-project-alist)
410 (error "There are no Muse projects defined; see `muse-project-alist'.")
411 (or (unless no-check-p
412 (muse-project-of-file))
413 (if (and (not no-assume)
414 (= 1 (length muse-project-alist)))
415 (car muse-project-alist)
416 (assoc (completing-read prompt muse-project-alist)
417 muse-project-alist)))))
419 (defvar muse-project-page-history nil)
421 (defun muse-read-project-file (project prompt &optional default)
422 (let ((name (completing-read prompt (muse-project-file-alist project)
423 nil nil nil 'muse-project-page-history
424 default)))
425 (cons name (muse-project-page-file name project))))
427 (defun muse-project-find-file (name project &optional command directory)
428 "Open the Muse page given by NAME in PROJECT.
429 If COMMAND is non-nil, it is the function used to visit the file.
430 If DIRECTORY is non-nil, it is the directory in which the page
431 will be created if it does not already exist. Otherwise, the
432 first directory within the project's fileset is used."
433 (interactive
434 (let* ((project (muse-read-project "Find in project: "
435 current-prefix-arg))
436 (default (muse-get-keyword :default (cadr project)))
437 (entry (muse-read-project-file
438 project (if default
439 (format "Find page: (default: %s) "
440 default)
441 "Find page: ")
442 default)))
443 (list entry project)))
444 (setq project (muse-project project))
445 (let ((project-name (car project)))
446 (unless (interactive-p)
447 (setq project (muse-project project)
448 name (cons name (muse-project-page-file name project))))
449 ;; If we're given a relative name, open it as-is
450 (if (and (car name)
451 (save-match-data (string-match "\\.\\./" (car name))))
452 (setcdr name (if muse-file-extension
453 (concat (car name) "." muse-file-extension)
454 (car name)))
455 ;; At this point, name is (PAGE . FILE).
456 (unless (cdr name)
457 (let ((pats (cadr project)))
458 (while (and pats (null directory))
459 (if (symbolp (car pats))
460 (setq pats (cddr pats))
461 (if (file-directory-p (car pats))
462 (setq directory (car pats) pats nil)
463 (setq pats (cdr pats))))))
464 (when directory
465 (let ((filename (expand-file-name
466 (if muse-file-extension
467 (concat (car name) "." muse-file-extension)
468 (car name))
469 directory)))
470 (unless (file-exists-p directory)
471 (make-directory directory t))
472 (setcdr name filename)))))
473 ;; Open the file
474 (if (cdr name)
475 (funcall (or command 'find-file) (cdr name))
476 (error "There is no page %s in project %s."
477 (car name) project-name))))
479 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
480 "Given STYLES, return a list of the ones that are considered for FILE.
481 The name of a project may be used for STYLES."
482 (when (stringp styles)
483 (setq styles (cddr (muse-project styles))))
484 (when (and file styles)
485 (let (used-styles)
486 (dolist (style styles)
487 (let ((include-regexp (muse-style-element :include style))
488 (exclude-regexp (muse-style-element :exclude style)))
489 (when (and (or ignore-regexp
490 (and (null include-regexp)
491 (null exclude-regexp))
492 (if include-regexp
493 (string-match include-regexp file)
494 (not (string-match exclude-regexp file))))
495 (or (not (file-exists-p file))
496 (not (muse-project-private-p file))))
497 (add-to-list 'used-styles style))))
498 used-styles)))
500 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
501 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
502 (let (published)
503 (dolist (style styles)
504 (let ((output-dir (muse-style-element :path style)))
505 ;; ensure the publishing location is available
506 (unless (file-exists-p output-dir)
507 (message "Creating publishing directory %s" output-dir)
508 (make-directory output-dir))
509 ;; publish the member file!
510 (if (muse-publish-file file style output-dir force)
511 (setq published t))))
512 published))
514 (defun muse-project-save-buffers (&optional project)
515 (setq project (muse-project project))
516 (map-y-or-n-p
517 (function
518 (lambda (buffer)
519 (and (buffer-modified-p buffer)
520 (not (buffer-base-buffer buffer))
521 (or (buffer-file-name buffer)
522 (progn
523 (set-buffer buffer)
524 (and buffer-offer-save
525 (> (buffer-size) 0))))
526 (with-current-buffer buffer
527 (let ((proj (muse-project-of-file)))
528 (and proj (string= (car proj)
529 (car project)))))
530 (if (buffer-file-name buffer)
531 (format "Save file %s? "
532 (buffer-file-name buffer))
533 (format "Save buffer %s? "
534 (buffer-name buffer))))))
535 (function
536 (lambda (buffer)
537 (set-buffer buffer)
538 (save-buffer)))
539 (buffer-list)
540 '("buffer" "buffers" "save")
541 (if (boundp 'save-some-buffers-action-alist)
542 save-some-buffers-action-alist)))
544 (defun muse-project-publish (project &optional force)
545 "Publish the pages of PROJECT that need publishing."
546 (interactive (list (muse-read-project "Publish project: " nil t)
547 current-prefix-arg))
548 (setq project (muse-project project))
549 (let ((styles (cddr project))
550 (muse-current-project project)
551 published)
552 ;; determine the style from the project, or else ask
553 (unless styles
554 (setq styles (list (muse-publish-get-style))))
555 ;; prompt to save any buffers related to this project
556 (muse-project-save-buffers project)
557 ;; run hook before publishing begins
558 (run-hook-with-args 'muse-before-project-publish-hook project)
559 ;; publish all files in the project, for each style; the actual
560 ;; publishing will only happen if the files are newer than the
561 ;; last published output, or if the file is listed in
562 ;; :force-publish. Files in :force-publish will not trigger the
563 ;; "All pages need to be published" message.
564 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
565 (file-alist (muse-project-file-alist project)))
566 (dolist (pair file-alist)
567 (when (muse-project-publish-file (cdr pair) styles force)
568 (setq forced-files (delete (car pair) forced-files))
569 (setq published t)))
570 (dolist (file forced-files)
571 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
572 ;; run hook after publishing ends
573 (run-hook-with-args 'muse-after-project-publish-hook project)
574 ;; notify the user that everything is now done
575 (if published
576 (message "All pages in %s have been published." (car project))
577 (message "No pages in %s need publishing at this time."
578 (car project)))))
580 (defun muse-project-batch-publish ()
581 "Publish Muse files in batch mode."
582 (let ((muse-batch-publishing-p t)
583 force)
584 (if (string= "--force" (or (car command-line-args-left) ""))
585 (setq force t
586 command-line-args-left (cdr command-line-args-left)))
587 (if command-line-args-left
588 (dolist (project command-line-args-left)
589 (message "Publishing project %s ..." project)
590 (muse-project-publish project force))
591 (message "No projects specified."))))
593 (eval-when-compile
594 (put 'make-local-hook 'byte-compile nil))
596 (defun muse-project-set-variables ()
597 "Load project-specific variables."
598 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
599 sym custom-set var)
600 (while vars
601 (setq sym (car vars))
602 (setq custom-set (or (get sym 'custom-set) 'set))
603 (setq var (if (eq (get sym 'custom-type) 'hook)
604 (make-local-hook sym)
605 (make-local-variable sym)))
606 (funcall custom-set var (car (cdr vars)))
607 (setq vars (cdr (cdr vars))))))
609 (defun muse-project-delete-output-files (project)
610 (interactive
611 (list (muse-read-project "Remove all output files for project: " nil t)))
612 (setq project (muse-project project))
613 (let ((file-alist (muse-project-file-alist project))
614 (styles (cddr project))
615 output-file path)
616 (dolist (entry file-alist)
617 (dolist (style styles)
618 (setq output-file
619 (and (setq path (muse-style-element :path style))
620 (expand-file-name
621 (concat (muse-style-element :prefix style)
622 (car entry)
623 (or (muse-style-element :osuffix style)
624 (muse-style-element :suffix style)))
625 path)))
626 (if output-file
627 (muse-delete-file-if-exists output-file))))))
629 (provide 'muse-project)
631 ;;; muse-project.el ends here