Miscellaneous clean-ups.
[muse-el.git] / lisp / muse-project.el
blobb14add5ca2636b25b5db97a27a5bf2f6e92549a7
1 ;;; muse-project.el --- handle Muse projects
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; 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)
36 (require 'cus-edit)
38 (defgroup muse-project nil
39 "Options controlling the behavior of Muse project handling."
40 :group 'muse)
42 (defcustom muse-before-project-publish-hook nil
43 "A hook run before a project is published.
44 Each function is passed the project object, a cons with the format
45 (PROJNAME . SETTINGS)"
46 :type 'hook
47 :group 'muse-project)
49 (defcustom muse-after-project-publish-hook nil
50 "A hook run after a project is published.
51 Each function is passed the project object, a cons with the format
52 (PROJNAME . SETTINGS)"
53 :type 'hook
54 :group 'muse-project)
56 (defvar muse-project-alist-using-customize nil
57 "Used internally by Muse to indicate whether `muse-project-alist'
58 has been modified via the customize interface.")
59 (make-variable-buffer-local 'muse-project-alist-using-customize)
61 (defmacro with-muse-project (project &rest body)
62 `(progn
63 (unless (muse-project ,project)
64 (error "Can't find project %s" ,project))
65 (with-temp-buffer
66 (muse-mode)
67 (setq muse-current-project (muse-project ,project))
68 (muse-project-set-variables)
69 ,@body)))
71 (put 'with-muse-project 'lisp-indent-function 0)
72 (put 'with-muse-project 'edebug-form-spec '(sexp body))
74 (defun muse-project-alist-get (sym)
75 "Turn `muse-project-alist' into something we can customize easily."
76 (when (boundp sym)
77 (setq muse-project-alist-using-customize t)
78 (let* ((val (copy-alist (symbol-value sym)))
79 (head val))
80 (while val
81 (let ((head (car (cdar val)))
82 res)
83 ;; Turn settings of first part into cons cells, symbol->string
84 (while head
85 (cond ((stringp (car head))
86 (add-to-list 'res (car head) t)
87 (setq head (cdr head)))
88 ((symbolp (car head))
89 (add-to-list 'res (list (symbol-name (car head))
90 (cadr head)) t)
91 (setq head (cddr head)))
93 (setq head (cdr head)))))
94 (setcdr (car val) (cons res (cdr (cdar val)))))
95 (let ((styles (cdar val)))
96 ;; Symbol->string in every style
97 (while (cdr styles)
98 (let ((head (cadr styles))
99 res)
100 (while (consp head)
101 (setq res (plist-put res (symbol-name (car head))
102 (cadr head)))
103 (setq head (cddr head)))
104 (setcdr styles (cons res (cddr styles))))
105 (setq styles (cdr styles))))
106 (setq val (cdr val)))
107 head)))
109 (defun muse-project-alist-set (sym val)
110 "Turn customized version of `muse-project-alist' into something
111 Muse can make use of."
112 (set sym val)
113 (when muse-project-alist-using-customize
114 ;; Make sure the unescaped version is written to .emacs
115 (put sym 'saved-value (list (custom-quote val)))
116 ;; Perform unescaping
117 (while val
118 (let ((head (car (cdar val)))
119 res)
120 ;; Turn cons cells into flat list, string->symbol
121 (while head
122 (cond ((stringp (car head))
123 (add-to-list 'res (car head) t))
124 ((consp (car head))
125 (add-to-list 'res (intern (caar head)) t)
126 (add-to-list 'res (car (cdar head)) t)))
127 (setq head (cdr head)))
128 (setcdr (car val) (cons res (cdr (cdar val)))))
129 (let ((styles (cdar val)))
130 ;; String->symbol in every style
131 (while (cdr styles)
132 (let ((head (cadr styles))
133 res)
134 (while (consp head)
135 (setq res (plist-put res (intern (car head))
136 (cadr head)))
137 (setq head (cddr head)))
138 (setcdr styles (cons res (cddr styles))))
139 (setq styles (cdr styles))))
140 (setq val (cdr val)))))
142 (define-widget 'muse-project 'default
143 "A widget that defines a Muse project."
144 :format "\n%v"
145 :value-create 'muse-widget-type-value-create
146 :value-get 'muse-widget-child-value-get
147 :value-delete 'ignore
148 :match 'muse-widget-type-match
149 :type '(cons :format " %v"
150 (repeat :tag "Settings" :format "%{%t%}:\n%v%i\n\n"
151 (choice
152 (string :tag "Directory")
153 (list :tag "Book function"
154 (const :tag ":book-funcall" ":book-funcall")
155 (choice (function)
156 (sexp :tag "Unknown")))
157 (list :tag "Book part"
158 (const :tag ":book-part" ":book-part")
159 (string :tag "Name"))
160 (list :tag "Book style"
161 (const :tag ":book-style" ":book-style")
162 (string :tag "Style"))
163 (list :tag "Default file"
164 (const :tag ":default" ":default")
165 (string :tag "File"))
166 (list :tag "End of book"
167 (const :tag ":book-end" ":book-end")
168 (const t))
169 (list :tag "Force publishing"
170 (const :tag ":force-publish" ":force-publish")
171 (repeat (string :tag "File")))
172 (list :tag "Major mode"
173 (const :tag ":major-mode" ":major-mode")
174 (choice (function :tag "Mode")
175 (sexp :tag "Unknown")))
176 (list :tag "New chapter"
177 (const :tag ":book-chapter" ":book-chapter")
178 (string :tag "Name"))
179 (list :tag "No chapters"
180 (const :tag ":nochapters" ":nochapters")
181 (const t))
182 (list :tag "Set variables"
183 (const :tag ":set" ":set")
184 (repeat (list :inline t
185 (symbol :tag "Variable")
186 (sexp :tag "Setting"))))
187 (list :tag "Visit links using"
188 (const :tag ":visit-link" ":visit-link")
189 (choice (function)
190 (sexp :tag "Unknown")))))
191 (repeat :tag "Styles" :format "%{%t%}:\n%v%i\n\n"
192 (set :tag "Style"
193 (list :inline t
194 :tag "Publishing style"
195 (const :tag ":base" ":base")
196 (string :tag "Style"))
197 (list :inline t
198 :tag "Base URL"
199 (const :tag ":base-url" ":base-url")
200 (string :tag "URL"))
201 (list :inline t
202 :tag "Exclude matching"
203 (const :tag ":exclude" ":exclude")
204 (regexp))
205 (list :inline t
206 :tag "Include matching"
207 (const :tag ":include" ":include")
208 (regexp))
209 (list :inline t
210 :tag "Timestamps file"
211 (const :tag ":timestamps" ":timestamps")
212 (file))
213 (list :inline t
214 :tag "Path"
215 (const :tag ":path" ":path")
216 (string :tag "Path"))))))
218 (defcustom muse-project-alist nil
219 "An alist of Muse projects.
220 A project defines a fileset, and a list of custom attributes for use
221 when publishing files in that project."
222 :type '(choice (const :tag "No projects defined." nil)
223 (repeat (cons :format "%{%t%}:\n\n%v"
224 :tag "Project" :indent 4
225 (string :tag "Project name")
226 muse-project))
227 (sexp :tag "Cannot parse expression"))
228 :get 'muse-project-alist-get
229 :set 'muse-project-alist-set
230 :group 'muse-project)
232 ;; Make it easier to specify a muse-project-alist entry
234 (defcustom muse-project-ignore-regexp
235 (concat "\\`\\(#.*#\\|.*,v\\|.*~\\|\\.\\.?\\|\\.#.*\\|,.*\\)\\'\\|"
236 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\|\\.svn\\|"
237 "_darcs\\)\\(/\\|\\'\\)")
238 "A regexp matching files to be ignored in Wiki directories.
240 You should set case-fold-search to nil before using this regexp
241 in code."
242 :type 'regexp
243 :group 'muse-regexp)
245 (defun muse-project-recurse-directory (base)
246 "Recusively retrieve all of the directories underneath BASE.
247 A list of these directories is returned.
248 Directories starting with \".\" will be ignored, as well as those
249 which match `muse-project-ignore-regexp'."
250 (let ((case-fold-search nil)
251 list dir)
252 (when (and (file-directory-p base)
253 (not (string-match muse-project-ignore-regexp base)))
254 (dolist (file (directory-files base t "^[^.]"))
255 (when (and (file-directory-p file)
256 (not (string-match muse-project-ignore-regexp file)))
257 (setq dir (file-name-nondirectory file))
258 (push dir list)
259 (nconc list (mapcar #'(lambda (item)
260 (concat dir "/" item))
261 (muse-project-recurse-directory file)))))
262 list)))
264 (defun muse-project-alist-styles (entry-dir output-dir style)
265 "Return a list of styles to use in a `muse-project-alist' entry.
266 ENTRY-DIR is the top-level directory of the project.
267 OUTPUT-DIR is where Muse files are published, keeping directory structure.
268 STYLE is the publishing style to use.
270 For an example of the use of this function, see
271 `examples/mwolson/muse-init.el' from the Muse distribution."
272 (cons `(:base ,style :path ,(expand-file-name output-dir)
273 :include ,(concat "/" (file-name-nondirectory entry-dir)
274 "/[^/]+$"))
275 (mapcar (lambda (dir)
276 `(:base ,style
277 :path ,(expand-file-name dir output-dir)
278 :include ,(concat "/" dir "/[^/]+$")))
279 (muse-project-recurse-directory entry-dir))))
281 (defun muse-project-alist-dirs (entry-dir)
282 "Return a list of directories to use in a `muse-project-alist' entry.
283 ENTRY-DIR is the top-level directory of the project.
285 For an example of the use of this function, see
286 `examples/mwolson/muse-init.el' from the Muse distribution."
287 (cons (expand-file-name entry-dir)
288 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
289 (muse-project-recurse-directory entry-dir))))
291 ;; Constructing the file-alist
293 (defvar muse-project-file-alist nil
294 "This variable is automagically constructed as needed.")
296 (defvar muse-project-file-alist-hook nil
297 "Functions that are to be exectuted immediately after updating
298 `muse-project-file-alist'.")
300 (defvar muse-current-project nil
301 "Project we are currently visiting.")
302 (make-variable-buffer-local 'muse-current-project)
304 (defvar muse-current-output-style nil
305 "The output style that we are currently using for publishing files.")
307 (defsubst muse-project (&optional project)
308 "Resolve the given PROJECT into a full Muse project, if it is a string."
309 (if (null project)
310 (or muse-current-project
311 (muse-project-of-file))
312 (if (stringp project)
313 (assoc project muse-project-alist)
314 (muse-assert (consp project))
315 project)))
317 (defun muse-project-page-file (page project &optional no-check-p)
318 "Return a filename if PAGE exists within the given Muse PROJECT."
319 (setq project (muse-project project))
320 (let ((dir (file-name-directory page)))
321 (when dir (setq page (file-name-nondirectory page)))
322 (let ((files (muse-collect-alist
323 (muse-project-file-alist project no-check-p)
324 page))
325 (matches nil))
326 (if dir
327 (save-match-data
328 (dolist (file files)
329 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
330 (file-name-directory (cdr file)))))
331 (when pos
332 (setq matches (cons (cons pos (cdr file))
333 matches)))))
334 (car (muse-sort-by-rating matches)))
335 (dolist (file files)
336 (setq matches (cons (cons (length (cdr file)) (cdr file))
337 matches)))
338 (car (muse-sort-by-rating matches '<))))))
340 (defun muse-project-private-p (file)
341 "Return non-nil if NAME is a private page with PROJECT."
342 (unless muse-under-windows-p
343 (setq file (file-truename file))
344 (if (file-attributes file) ; don't publish if no attributes exist
345 (or (when (eq ?- (aref (nth 8 (file-attributes
346 (file-name-directory file))) 7))
347 (message (concat
348 "The " (file-name-directory file)
349 " directory must be readable by others"
350 " in order for its contents to be published.")))
351 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
352 t)))
354 (defun muse-project-file-entries (path)
355 (let* ((names (list t))
356 (lnames names)
357 (case-fold-search nil))
358 (cond
359 ((file-directory-p path)
360 (dolist (file (directory-files
361 path t (when (and muse-file-extension
362 (not (string= muse-file-extension "")))
363 (concat "." muse-file-extension "\\'"))))
364 (unless (or (string-match muse-project-ignore-regexp file)
365 (file-directory-p file))
366 (setcdr lnames
367 (cons (cons (muse-page-name file) file) nil))
368 (setq lnames (cdr lnames)))))
369 ((file-readable-p path)
370 (setcdr lnames
371 (cons (cons (muse-page-name path) path) nil))
372 (setq lnames (cdr lnames)))
373 (t ; regexp
374 (muse-assert (file-name-directory path))
375 (dolist (file (directory-files
376 (file-name-directory path) t
377 (file-name-nondirectory path)))
378 (unless (string-match muse-project-ignore-regexp file)
379 (setcdr lnames
380 (cons (cons (muse-page-name file) file) nil))
381 (setq lnames (cdr lnames))))))
382 (cdr names)))
384 (defvar muse-updating-file-alist-p nil
385 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
387 (defun muse-project-file-alist (&optional project no-check-p)
388 "Return member filenames for the given Muse PROJECT.
389 On UNIX, this list is only updated if one of the directories'
390 contents have changed. On Windows, it is always reread from
391 disk."
392 (setq project (muse-project project))
393 (let* ((file-alist (assoc (car project) muse-project-file-alist))
394 (last-mod (cdr (cdr file-alist))))
395 ;; Determine the last modified of any directory mentioned in the
396 ;; project's pattern list
397 (unless (or muse-under-windows-p no-check-p)
398 (let ((pats (cadr project)))
399 (while pats
400 (if (symbolp (car pats))
401 (setq pats (cddr pats))
402 (let* ((fnd (file-name-directory (car pats)))
403 (dir (cond ((file-directory-p (car pats))
404 (car pats))
405 ((and (not (file-readable-p (car pats)))
407 (file-directory-p fnd))
408 fnd))))
409 (when dir
410 (let ((mod-time (nth 5 (file-attributes dir))))
411 (when (or (null last-mod)
412 (and mod-time
413 (muse-time-less-p last-mod mod-time)))
414 (setq last-mod mod-time)))))
415 (setq pats (cdr pats))))))
416 ;; Either return the currently known list, or read it again from
417 ;; disk
418 (if (or (and no-check-p (cadr file-alist))
419 muse-updating-file-alist-p
420 (not (or muse-under-windows-p
421 (null (cddr file-alist))
422 (null last-mod)
423 (muse-time-less-p (cddr file-alist) last-mod))))
424 (cadr file-alist)
425 (if file-alist
426 (setcdr (cdr file-alist) last-mod)
427 (setq file-alist (cons (car project) (cons nil last-mod))
428 muse-project-file-alist
429 (cons file-alist muse-project-file-alist)))
430 ;; Read in all of the file entries
431 (let ((muse-updating-file-alist-p t))
432 (prog1
433 (save-match-data
434 (setcar
435 (cdr file-alist)
436 (let* ((names (list t))
437 (pats (cadr project)))
438 (while pats
439 (if (symbolp (car pats))
440 (setq pats (cddr pats))
441 (nconc names (muse-project-file-entries (car pats)))
442 (setq pats (cdr pats))))
443 (cdr names))))
444 (run-hooks 'muse-project-file-alist-hook))))))
446 (defun muse-project-of-file (&optional pathname)
447 "Determine which project the given PATHNAME relates to.
448 If PATHNAME is nil, the current buffer's filename is used."
449 (if (and (null pathname) muse-current-project)
450 muse-current-project
451 (unless pathname (setq pathname (muse-current-file)))
452 (save-match-data
453 (when (and (stringp pathname)
454 (not (string= pathname ""))
455 (not (let ((case-fold-search nil))
456 (string-match muse-project-ignore-regexp pathname))))
457 (let* ((file (file-truename pathname))
458 (dir (file-name-directory file))
459 (project-entry muse-project-alist)
460 found)
461 (while (and project-entry (not found))
462 (let ((pats (car (cdar project-entry))))
463 (while (and pats (not found))
464 (if (symbolp (car pats))
465 (setq pats (cddr pats))
466 (let ((truename (file-truename (car pats))))
467 (if (or (string= truename file)
468 (string= truename dir)
469 (string-match (regexp-quote truename) file))
470 (setq found (car project-entry))))
471 (setq pats (cdr pats))))
472 (setq project-entry (cdr project-entry))))
473 found)))))
475 (defun muse-project-after-save-hook ()
476 "Update Muse's file-alist if we are saving a Muse file."
477 (let ((project (muse-project-of-file)))
478 (when project
479 (muse-project-file-alist project))))
481 (add-hook 'after-save-hook 'muse-project-after-save-hook)
483 (defun muse-read-project (prompt &optional no-check-p no-assume)
484 "Read a project name from the minibuffer, if it can't be figured
485 out."
486 (if (null muse-project-alist)
487 (error "There are no Muse projects defined; see `muse-project-alist'.")
488 (or (unless no-check-p
489 (muse-project-of-file))
490 (if (and (not no-assume)
491 (= 1 (length muse-project-alist)))
492 (car muse-project-alist)
493 (assoc (completing-read prompt muse-project-alist)
494 muse-project-alist)))))
496 (defvar muse-project-page-history nil)
498 (defun muse-read-project-file (project prompt &optional default)
499 (let ((name (completing-read prompt (muse-project-file-alist project)
500 nil nil nil 'muse-project-page-history
501 default)))
502 (cons name (muse-project-page-file name project))))
504 ;;;###autoload
505 (defun muse-project-find-file (name project &optional command directory)
506 "Open the Muse page given by NAME in PROJECT.
507 If COMMAND is non-nil, it is the function used to visit the file.
508 If DIRECTORY is non-nil, it is the directory in which the page
509 will be created if it does not already exist. Otherwise, the
510 first directory within the project's fileset is used."
511 (interactive
512 (let* ((project (muse-read-project "Find in project: "
513 current-prefix-arg))
514 (default (muse-get-keyword :default (cadr project)))
515 (entry (muse-read-project-file
516 project (if default
517 (format "Find page: (default: %s) "
518 default)
519 "Find page: ")
520 default)))
521 (list entry project)))
522 (setq project (muse-project project))
523 (let ((project-name (car project)))
524 (unless (interactive-p)
525 (setq project (muse-project project)
526 name (cons name (muse-project-page-file name project))))
527 ;; If we're given a relative or absolute filename, open it as-is
528 (if (and (car name)
529 (save-match-data
530 (or (string-match muse-file-regexp (car name))
531 (string-match muse-image-regexp (car name)))))
532 (setcdr name (car name))
533 ;; At this point, name is (PAGE . FILE).
534 (unless (cdr name)
535 (let ((pats (cadr project)))
536 (while (and pats (null directory))
537 (if (symbolp (car pats))
538 (setq pats (cddr pats))
539 (if (file-directory-p (car pats))
540 (setq directory (car pats) pats nil)
541 (setq pats (cdr pats))))))
542 (when directory
543 (let ((filename (expand-file-name
544 (if (and muse-file-extension
545 (not (string= muse-file-extension "")))
546 (concat (car name) "." muse-file-extension)
547 (car name))
548 directory)))
549 (unless (file-exists-p directory)
550 (make-directory directory t))
551 (setcdr name filename)))))
552 ;; Open the file
553 (if (cdr name)
554 (funcall (or command 'find-file) (cdr name))
555 (error "There is no page %s in project %s."
556 (car name) project-name))))
558 (defun muse-project-choose-style (closure test styles)
559 "Run TEST on STYLES and return first style where TEST yields non-nil.
560 TEST should take two arguments. The first is CLOSURE, which is
561 passed verbatim. The second if the current style to consider.
563 If no style passes TEST, return the first style."
564 (or (catch 'winner
565 (dolist (style styles)
566 (when (funcall test closure style)
567 (throw 'winner style))))
568 (car styles)))
570 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
571 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
572 return non-nil."
573 (let ((link-suffix (muse-style-element :link-suffix style)))
574 (and (stringp link-suffix)
575 (string= given-suffix link-suffix))))
577 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
578 "Given STYLES, return a list of the ones that are considered for FILE.
579 The name of a project may be used for STYLES."
580 (when (stringp styles)
581 (setq styles (cddr (muse-project styles))))
582 (when (and file styles)
583 (let ((used-styles nil))
584 (dolist (style styles)
585 (let ((include-regexp (muse-style-element :include style))
586 (exclude-regexp (muse-style-element :exclude style))
587 (rating nil))
588 (when (and (or ignore-regexp
589 (and (null include-regexp)
590 (null exclude-regexp))
591 (if include-regexp
592 (setq rating (string-match include-regexp file))
593 (not (string-match exclude-regexp file))))
594 (or (not (file-exists-p file))
595 (not (muse-project-private-p file))))
596 (setq used-styles (cons (cons rating style) used-styles)))))
597 (muse-sort-by-rating (nreverse used-styles)))))
599 (defun muse-project-get-applicable-style (file styles)
600 "Choose a style from the STYLES that FILE can publish to.
601 The user is prompted if several styles are found."
602 (muse-publish-get-style
603 (mapcar (lambda (style)
604 (cons (muse-get-keyword :base style) style))
605 (muse-project-applicable-styles file styles))))
607 (defun muse-project-resolve-link (page local-style remote-styles)
608 "Return a published relative link from the output path of one file
609 to another file.
611 The best match for PAGE is determined by comparing the link
612 suffix of the given local style and that of the remote styles.
614 The remote styles are usually populated by
615 `muse-project-applicable-styles'."
616 (let ((link-suffix (muse-style-element :link-suffix local-style))
617 remote-style)
618 (if (not (stringp link-suffix))
619 (setq remote-style (car remote-styles))
620 (setq remote-style (muse-project-choose-style
621 link-suffix
622 #'muse-project-choose-style-by-link-suffix
623 remote-styles)))
624 (muse-publish-link-file
625 (if (null remote-style)
626 page
627 (let ((prefix (muse-style-element :base-url remote-style)))
628 (if prefix
629 (concat prefix page)
630 (file-relative-name (expand-file-name
631 (file-name-nondirectory page)
632 (muse-style-element :path remote-style))
633 (expand-file-name
634 (muse-style-element :path local-style))))))
635 nil remote-style)))
637 (defun muse-project-current-output-style (&optional file project)
638 (or muse-current-output-style
639 (progn
640 (unless file (setq file (muse-current-file)))
641 (unless project (setq project (muse-project-of-file file)))
642 (car (muse-project-applicable-styles file (cddr project))))))
644 (defun muse-project-link-page (page)
645 (let ((project (muse-project-of-file)))
646 (muse-project-resolve-link page
647 (muse-project-current-output-style)
648 (muse-project-applicable-styles
649 (muse-project-page-file page project)
650 (cddr project)))))
652 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
653 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
654 (let (published)
655 (dolist (style styles)
656 (let ((output-dir (muse-style-element :path style))
657 (muse-current-output-style style))
658 ;; ensure the publishing location is available
659 (unless (file-exists-p output-dir)
660 (message "Creating publishing directory %s" output-dir)
661 (make-directory output-dir t))
662 ;; publish the member file!
663 (if (muse-publish-file file style output-dir force)
664 (setq published t))))
665 published))
667 ;;;###autoload
668 (defun muse-project-publish-this-file (&optional force)
669 "Publish the currently-visited file according to `muse-project-alist',
670 prompting if more than one style applies.
672 If FORCE is given, publish the file even if it is up-to-date."
673 (interactive (list current-prefix-arg))
674 (let* ((style (muse-project-get-applicable-style
675 buffer-file-name (cddr muse-current-project)))
676 (output-dir (muse-style-element :path style)))
677 (unless (muse-publish-file buffer-file-name style output-dir force)
678 (message (concat "The published version is up-to-date; use"
679 " C-u C-c C-t to force an update.")))))
681 (defun muse-project-save-buffers (&optional project)
682 (setq project (muse-project project))
683 (map-y-or-n-p
684 (function
685 (lambda (buffer)
686 (and (buffer-modified-p buffer)
687 (not (buffer-base-buffer buffer))
688 (or (buffer-file-name buffer)
689 (progn
690 (set-buffer buffer)
691 (and buffer-offer-save
692 (> (buffer-size) 0))))
693 (with-current-buffer buffer
694 (let ((proj (muse-project-of-file)))
695 (and proj (string= (car proj)
696 (car project)))))
697 (if (buffer-file-name buffer)
698 (format "Save file %s? "
699 (buffer-file-name buffer))
700 (format "Save buffer %s? "
701 (buffer-name buffer))))))
702 (function
703 (lambda (buffer)
704 (set-buffer buffer)
705 (save-buffer)))
706 (buffer-list)
707 '("buffer" "buffers" "save")
708 (if (boundp 'save-some-buffers-action-alist)
709 save-some-buffers-action-alist)))
711 ;;;###autoload
712 (defun muse-project-publish (project &optional force)
713 "Publish the pages of PROJECT that need publishing."
714 (interactive (list (muse-read-project "Publish project: " nil t)
715 current-prefix-arg))
716 (setq project (muse-project project))
717 (let ((styles (cddr project))
718 (muse-current-project project)
719 published)
720 ;; determine the style from the project, or else ask
721 (unless styles
722 (setq styles (list (muse-publish-get-style))))
723 (unless project
724 (error "Cannot find a project to publish"))
725 ;; prompt to save any buffers related to this project
726 (muse-project-save-buffers project)
727 ;; run hook before publishing begins
728 (run-hook-with-args 'muse-before-project-publish-hook project)
729 ;; publish all files in the project, for each style; the actual
730 ;; publishing will only happen if the files are newer than the
731 ;; last published output, or if the file is listed in
732 ;; :force-publish. Files in :force-publish will not trigger the
733 ;; "All pages need to be published" message.
734 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
735 (file-alist (muse-project-file-alist project)))
736 (dolist (pair file-alist)
737 (when (muse-project-publish-file (cdr pair) styles force)
738 (setq forced-files (delete (car pair) forced-files))
739 (setq published t)))
740 (dolist (file forced-files)
741 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
742 ;; run hook after publishing ends
743 (run-hook-with-args 'muse-after-project-publish-hook project)
744 ;; notify the user that everything is now done
745 (if published
746 (message "All pages in %s have been published." (car project))
747 (message "No pages in %s need publishing at this time."
748 (car project)))))
750 (defun muse-project-batch-publish ()
751 "Publish Muse files in batch mode."
752 (let ((muse-batch-publishing-p t)
753 force)
754 (if (string= "--force" (or (car command-line-args-left) ""))
755 (setq force t
756 command-line-args-left (cdr command-line-args-left)))
757 (if command-line-args-left
758 (dolist (project command-line-args-left)
759 (message "Publishing project %s ..." project)
760 (muse-project-publish project force))
761 (message "No projects specified."))))
763 (eval-when-compile
764 (put 'make-local-hook 'byte-compile nil))
766 (defun muse-project-set-variables ()
767 "Load project-specific variables."
768 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
769 sym custom-set var)
770 (while vars
771 (setq sym (car vars))
772 (setq custom-set (or (get sym 'custom-set) 'set))
773 (setq var (if (eq (get sym 'custom-type) 'hook)
774 (make-local-hook sym)
775 (make-local-variable sym)))
776 (funcall custom-set var (car (cdr vars)))
777 (setq vars (cdr (cdr vars))))))
779 (defun muse-project-delete-output-files (project)
780 (interactive
781 (list (muse-read-project "Remove all output files for project: " nil t)))
782 (setq project (muse-project project))
783 (let ((file-alist (muse-project-file-alist project))
784 (styles (cddr project))
785 output-file path)
786 (dolist (entry file-alist)
787 (dolist (style styles)
788 (setq output-file
789 (and (setq path (muse-style-element :path style))
790 (expand-file-name
791 (concat (muse-style-element :prefix style)
792 (car entry)
793 (or (muse-style-element :osuffix style)
794 (muse-style-element :suffix style)))
795 path)))
796 (if output-file
797 (muse-delete-file-if-exists output-file))))))
799 (provide 'muse-project)
801 ;;; muse-project.el ends here