Add invalid directory assertion.
[muse-el.git] / lisp / muse-project.el
blobf90770897816e18108641093962ed5ff02a4bf23
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 (while head
64 (cond ((stringp (car head))
65 (add-to-list 'res (car head))
66 (setq head (cdr head)))
67 ((and (symbolp (car head))
68 (or (symbolp (cadr head))
69 (stringp (cadr head))))
70 (add-to-list 'res (list (car head) (cadr head)) t)
71 (setq head (cddr head)))
73 (setq head (cdr head)))))
74 (setcdr (car val) (cons res (cdr (cdar val)))))
75 (setq val (cdr val)))
76 head)))
78 (defun muse-project-alist-set (sym val)
79 "Turn customized version of `muse-project-alist' into something
80 Muse can make use of."
81 (set sym val)
82 (while val
83 (let ((head (car (cdar val)))
84 res)
85 (while head
86 (cond ((stringp (car head))
87 (add-to-list 'res (car head) t))
88 ((consp (car head))
89 (add-to-list 'res (caar head) t)
90 (add-to-list 'res (car (cdar head)) t)))
91 (setq head (cdr head)))
92 (setcdr (car val) (cons res (cdr (cdar val)))))
93 (setq val (cdr val))))
95 (define-widget 'muse-project 'lazy
96 "A widget that defines a Muse project."
97 :tag "Project"
98 :type '(cons (repeat :tag "Contents"
99 (choice
100 (string :tag "Directory")
101 (list :tag "Setting"
102 (choice :tag "Property"
103 (const :book-chapter)
104 (const :book-end)
105 (const :book-funcall)
106 (const :book-part)
107 (const :book-style)
108 (const :default)
109 (const :major-mode)
110 (const :nochapters)
111 (const :set)
112 (const :visit-link))
113 (choice :tag "Value"
114 (string) (symbol)))))
115 (repeat (repeat :tag "Style"
116 (group :inline t
117 (choice
118 :tag "Property"
119 (const :base)
120 (const :base-url)
121 (const :exclude)
122 (const :include)
123 (const :path))
124 (string :tag "Value"))))))
126 (defcustom muse-project-alist nil
127 "An alist of Muse projects.
128 A project defines a fileset, and a list of custom attributes for use
129 when publishing files in that project."
130 :type '(alist
131 :key-type (string :tag "Project name")
132 :value-type muse-project)
133 :get 'muse-project-alist-get
134 :set 'muse-project-alist-set
135 :group 'muse-project)
137 (defvar muse-project-file-alist nil
138 "This variable is automagically constructed as needed.")
140 (defcustom muse-project-ignore-regexp
141 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
142 "A regexp matching files to be ignored in Wiki directories."
143 :type 'regexp
144 :group 'muse-regexp)
146 (defvar muse-current-project nil
147 "Project we are currently visiting.")
148 (make-variable-buffer-local 'muse-current-project)
150 (defsubst muse-project (&optional project)
151 "Resolve the given PROJECT into a full Muse project, if it is a string."
152 (if (null project)
153 (or muse-current-project
154 (muse-project-of-file))
155 (if (stringp project)
156 (assoc project muse-project-alist)
157 (muse-assert (consp project))
158 project)))
160 (defsubst muse-project-page-file (page project &optional no-check-p)
161 "Return a filename if PAGE exists within the given Muse PROJECT."
162 (setq project (muse-project project))
163 (cdr (assoc page (muse-project-file-alist project no-check-p))))
165 (defun muse-project-private-p (file)
166 "Return non-nil if NAME is a private page with PROJECT."
167 (unless muse-under-windows-p
168 (setq file (file-truename file))
169 (if (file-attributes file) ; don't publish if no attributes exist
170 (or (when (eq ?- (aref (nth 8 (file-attributes
171 (file-name-directory file))) 7))
172 (message (concat
173 "The " (file-name-directory file)
174 " directory must be readable by others"
175 " in order for its contents to be published.")))
176 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
177 t)))
179 (defun muse-project-file-entries (path)
180 (let* ((names (list t))
181 (lnames names))
182 (cond
183 ((file-directory-p path)
184 (dolist (file (directory-files
185 path t (when muse-file-extension
186 (concat "." muse-file-extension "\\'"))))
187 (unless (or (string-match muse-project-ignore-regexp file)
188 (file-directory-p file))
189 (setcdr lnames
190 (cons (cons (muse-page-name file) file) nil))
191 (setq lnames (cdr lnames)))))
192 ((file-readable-p path)
193 (setcdr lnames
194 (cons (cons (muse-page-name path) path) nil))
195 (setq lnames (cdr lnames)))
196 (t ; regexp
197 (muse-assert (file-name-directory path))
198 (dolist (file (directory-files
199 (file-name-directory path) t
200 (file-name-nondirectory path)))
201 (unless (string-match muse-project-ignore-regexp file)
202 (setcdr lnames
203 (cons (cons (muse-page-name file) file) nil))
204 (setq lnames (cdr lnames))))))
205 (cdr names)))
207 (defun muse-project-file-alist (&optional project no-check-p)
208 "Return member filenames for the given Muse PROJECT.
209 On UNIX, this list is only updated if one of the directories'
210 contents have changed. On Windows, it is always reread from
211 disk."
212 (setq project (muse-project project))
213 (let ((file-alist (assoc (car project) muse-project-file-alist))
214 last-mod)
215 ;; Determine the last modified of any directory mentioned in the
216 ;; project's pattern list
217 (unless (or muse-under-windows-p no-check-p)
218 (let ((pats (cadr project)))
219 (while pats
220 (if (symbolp (car pats))
221 (setq pats (cddr pats))
222 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
223 (and (not (file-readable-p (car pats)))
224 (file-directory-p
225 (file-name-directory (car pats)))
226 (file-name-directory (car pats))))))
227 (if dir
228 (let ((mod-time (nth 5 (file-attributes dir))))
229 (if (or (null last-mod)
230 (and mod-time
231 (muse-time-less-p last-mod mod-time)))
232 (setq last-mod mod-time)))))
233 (setq pats (cdr pats))))))
234 ;; Either return the currently known list, or read it again from
235 ;; disk
236 (if (or (and no-check-p (cadr file-alist))
237 (not (or muse-under-windows-p
238 (null (cddr file-alist))
239 (null last-mod)
240 (muse-time-less-p (cddr file-alist) last-mod))))
241 (cadr file-alist)
242 (if file-alist
243 (setcdr (cdr file-alist) last-mod)
244 (setq file-alist (cons (car project) (cons nil last-mod))
245 muse-project-file-alist
246 (cons file-alist muse-project-file-alist)))
247 ;; Read in all of the file entries
248 (save-match-data
249 (setcar
250 (cdr file-alist)
251 (let* ((names (list t))
252 (pats (cadr project)))
253 (while pats
254 (if (symbolp (car pats))
255 (setq pats (cddr pats))
256 (nconc names (muse-project-file-entries (car pats)))
257 (setq pats (cdr pats))))
258 (cdr names)))))))
260 (defun muse-project-of-file (&optional pathname)
261 "Determine which project the given PATHNAME relates to.
262 If PATHNAME is nil, the current buffer's filename is used."
263 (if (and (null pathname) muse-current-project)
264 muse-current-project
265 (when (or pathname
266 (and (boundp 'muse-publishing-current-file)
267 muse-publishing-current-file)
268 buffer-file-name)
269 (let* ((file (file-truename (or pathname buffer-file-name)))
270 (dir (file-name-directory file))
271 (project-entry muse-project-alist)
272 found)
273 (while (and project-entry (not found))
274 (let ((pats (car (cdar project-entry))))
275 (while (and pats (not found))
276 (if (symbolp (car pats))
277 (setq pats (cddr pats))
278 (let ((truename (file-truename (car pats))))
279 (if (or (string= truename file)
280 (string= truename dir)
281 (string-match truename file))
282 (setq found (car project-entry))))
283 (setq pats (cdr pats))))
284 (setq project-entry (cdr project-entry))))
285 found))))
287 (defun muse-read-project (prompt &optional no-check-p no-assume)
288 "Read a project name from the minibuffer, if it can't be figured
289 out."
290 (if (null muse-project-alist)
291 (error "There are no Muse projects defined; see `muse-project-alist'.")
292 (or (unless no-check-p
293 (muse-project-of-file))
294 (if (and (not no-assume)
295 (= 1 (length muse-project-alist)))
296 (car muse-project-alist)
297 (assoc (completing-read prompt muse-project-alist)
298 muse-project-alist)))))
300 (defvar muse-project-page-history nil)
302 (defun muse-read-project-file (project prompt &optional default)
303 (let ((name (completing-read prompt (muse-project-file-alist project)
304 nil nil nil 'muse-project-page-history
305 default)))
306 (cons name (muse-project-page-file name project))))
308 (defun muse-project-find-file (name project &optional command directory)
309 "Open the Muse page given by NAME in PROJECT.
310 If COMMAND is non-nil, it is the function used to visit the file.
311 If DIRECTORY is non-nil, it is the directory in which the page
312 will be created if it does not already exist. Otherwise, the
313 first directory within the project's fileset is used."
314 (interactive
315 (let* ((project (muse-read-project "Find in project: "
316 current-prefix-arg))
317 (default (muse-get-keyword :default (cadr project)))
318 (entry (muse-read-project-file
319 project (if default
320 (format "Find page: (default: %s) "
321 default)
322 "Find page: ")
323 default)))
324 (list entry project)))
325 (setq project (muse-project project))
326 (let ((project-name (car project)))
327 (unless (interactive-p)
328 (setq project (muse-project project)
329 name (cons name (muse-project-page-file name project))))
330 ;; If we're given a relative name, open it as-is
331 (if (and (car name)
332 (save-match-data (string-match "\\.\\./" (car name))))
333 (setcdr name (if muse-file-extension
334 (concat (car name) "." muse-file-extension)
335 (car name)))
336 ;; At this point, name is (PAGE . FILE).
337 (unless (cdr name)
338 (let ((pats (cadr project)))
339 (while (and pats (null directory))
340 (if (symbolp (car pats))
341 (setq pats (cddr pats))
342 (if (file-directory-p (car pats))
343 (setq directory (car pats) pats nil)
344 (setq pats (cdr pats))))))
345 (when directory
346 (let ((filename (expand-file-name
347 (if muse-file-extension
348 (concat (car name) "." muse-file-extension)
349 (car name))
350 directory)))
351 (unless (file-exists-p directory)
352 (make-directory directory t))
353 (setcdr name filename)))))
354 ;; Open the file
355 (if (cdr name)
356 (funcall (or command 'find-file) (cdr name))
357 (error "There is no page %s in project %s."
358 (car name) project-name))))
360 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
361 "Given STYLES, return a list of the ones that are considered for FILE.
362 The name of a project may be used for STYLES."
363 (when (stringp styles)
364 (setq styles (cddr (muse-project styles))))
365 (muse-assert (and file styles))
366 (let (used-styles)
367 (dolist (style styles)
368 (let ((include-regexp (muse-style-element :include style))
369 (exclude-regexp (muse-style-element :exclude style)))
370 (when (and (or ignore-regexp
371 (and (null include-regexp)
372 (null exclude-regexp))
373 (if include-regexp
374 (string-match include-regexp file)
375 (not (string-match exclude-regexp file))))
376 (or (not (file-exists-p file))
377 (not (muse-project-private-p file))))
378 (add-to-list 'used-styles style))))
379 used-styles))
381 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
382 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
383 (let (published)
384 (dolist (style styles)
385 (let ((output-dir (muse-style-element :path style)))
386 ;; ensure the publishing location is available
387 (unless (file-exists-p output-dir)
388 (message "Creating publishing directory %s" output-dir)
389 (make-directory output-dir))
390 ;; publish the member file!
391 (if (muse-publish-file file style output-dir force)
392 (setq published t))))
393 published))
395 (defun muse-project-save-buffers (&optional project)
396 (setq project (muse-project project))
397 (map-y-or-n-p
398 (function
399 (lambda (buffer)
400 (and (buffer-modified-p buffer)
401 (not (buffer-base-buffer buffer))
402 (or (buffer-file-name buffer)
403 (progn
404 (set-buffer buffer)
405 (and buffer-offer-save
406 (> (buffer-size) 0))))
407 (with-current-buffer buffer
408 (let ((proj (muse-project-of-file)))
409 (and proj (string= (car proj)
410 (car project)))))
411 (if (buffer-file-name buffer)
412 (format "Save file %s? "
413 (buffer-file-name buffer))
414 (format "Save buffer %s? "
415 (buffer-name buffer))))))
416 (function
417 (lambda (buffer)
418 (set-buffer buffer)
419 (save-buffer)))
420 (buffer-list)
421 '("buffer" "buffers" "save")
422 (if (boundp 'save-some-buffers-action-alist)
423 save-some-buffers-action-alist)))
425 (defun muse-project-publish (project &optional force)
426 "Publish the pages of PROJECT that need publishing."
427 (interactive (list (muse-read-project "Publish project: " nil t)
428 current-prefix-arg))
429 (setq project (muse-project project))
430 (let ((styles (cddr project))
431 (muse-current-project project)
432 published)
433 ;; determine the style from the project, or else ask
434 (unless styles
435 (setq styles (list (muse-publish-get-style))))
436 ;; prompt to save any buffers related to this project
437 (muse-project-save-buffers project)
438 ;; run hook before publishing begins
439 (run-hook-with-args 'muse-before-project-publish-hook project)
440 ;; publish all files in the project, for each style; the actual
441 ;; publishing will only happen if the files are newer than the
442 ;; last published output
443 (dolist (pair (muse-project-file-alist project))
444 (if (muse-project-publish-file (cdr pair) styles force)
445 (setq published t)))
446 ;; run hook after publishing ends
447 (run-hook-with-args 'muse-after-project-publish-hook project)
448 ;; notify the user that everything is now done
449 (if published
450 (message "All pages in %s have been published." (car project))
451 (message "No pages in %s need publishing at this time."
452 (car project)))))
454 (defun muse-project-batch-publish ()
455 "Publish Muse files in batch mode."
456 (let ((muse-batch-publishing-p t)
457 force)
458 (if (string= "--force" (or (car command-line-args-left) ""))
459 (setq force t
460 command-line-args-left (cdr command-line-args-left)))
461 (if command-line-args-left
462 (dolist (project command-line-args-left)
463 (message "Publishing project %s ..." project)
464 (muse-project-publish project force))
465 (message "No projects specified."))))
467 (eval-when-compile
468 (put 'make-local-hook 'byte-compile nil))
470 (defun muse-project-set-variables ()
471 "Load project-specific variables."
472 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
473 sym custom-set var)
474 (while vars
475 (setq sym (car vars))
476 (setq custom-set (or (get sym 'custom-set) 'set))
477 (setq var (if (eq (get sym 'custom-type) 'hook)
478 (make-local-hook sym)
479 (make-local-variable sym)))
480 (funcall custom-set var (car (cdr vars)))
481 (setq vars (cdr (cdr vars))))))
483 (defun muse-project-delete-output-files (project)
484 (interactive
485 (list (muse-read-project "Remove all output files for project: " nil t)))
486 (setq project (muse-project project))
487 (let ((file-alist (muse-project-file-alist project))
488 (styles (cddr project))
489 output-file path)
490 (dolist (entry file-alist)
491 (dolist (style styles)
492 (setq output-file
493 (and (setq path (muse-style-element :path style))
494 (expand-file-name
495 (concat (muse-style-element :prefix style)
496 (car entry)
497 (or (muse-style-element :osuffix style)
498 (muse-style-element :suffix style)))
499 path)))
500 (if output-file
501 (muse-delete-file-if-exists output-file))))))
503 (provide 'muse-project)
505 ;;; muse-project.el ends here