muse-mode: Publish page before viewing it.
[muse-el.git] / lisp / muse-project.el
blob1e265f1d13c6786cd685968eb6cf3cead5c5caad
1 ;;; muse-project.el --- handle Muse projects
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 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 3, 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 (provide 'muse-project)
36 (require 'muse)
37 (require 'muse-publish)
38 (require 'cus-edit)
40 (defgroup muse-project nil
41 "Options controlling the behavior of Muse project handling."
42 :group 'muse)
44 (defcustom muse-before-project-publish-hook nil
45 "A hook run before a project is published.
46 Each function is passed the project object, a cons with the format
47 (PROJNAME . SETTINGS)"
48 :type 'hook
49 :group 'muse-project)
51 (defcustom muse-after-project-publish-hook nil
52 "A hook run after a project is published.
53 Each function is passed the project object, a cons with the format
54 (PROJNAME . SETTINGS)"
55 :type 'hook
56 :group 'muse-project)
58 (defvar muse-project-alist-using-customize nil
59 "Used internally by Muse to indicate whether `muse-project-alist'
60 has been modified via the customize interface.")
61 (make-variable-buffer-local 'muse-project-alist-using-customize)
63 (defmacro with-muse-project (project &rest body)
64 `(progn
65 (unless (muse-project ,project)
66 (error "Can't find project %s" ,project))
67 (with-temp-buffer
68 (muse-mode)
69 (setq muse-current-project (muse-project ,project))
70 (muse-project-set-variables)
71 ,@body)))
73 (put 'with-muse-project 'lisp-indent-function 0)
74 (put 'with-muse-project 'edebug-form-spec '(sexp body))
76 (defun muse-project-alist-get (sym)
77 "Turn `muse-project-alist' into something we can customize easily."
78 (when (boundp sym)
79 (setq muse-project-alist-using-customize t)
80 (let* ((val (copy-alist (symbol-value sym)))
81 (head val))
82 (while val
83 (let ((head (car (cdar val)))
84 res)
85 ;; Turn settings of first part into cons cells, symbol->string
86 (while head
87 (cond ((stringp (car head))
88 (add-to-list 'res (car head) t)
89 (setq head (cdr head)))
90 ((symbolp (car head))
91 (add-to-list 'res (list (symbol-name (car head))
92 (cadr head)) t)
93 (setq head (cddr head)))
95 (setq head (cdr head)))))
96 (setcdr (car val) (cons res (cdr (cdar val)))))
97 (let ((styles (cdar val)))
98 ;; Symbol->string in every style
99 (while (cdr styles)
100 (let ((head (cadr styles))
101 res)
102 (while (consp head)
103 (setq res (plist-put res (symbol-name (car head))
104 (cadr head)))
105 (setq head (cddr head)))
106 (setcdr styles (cons res (cddr styles))))
107 (setq styles (cdr styles))))
108 (setq val (cdr val)))
109 head)))
111 (defun muse-project-alist-set (sym val)
112 "Turn customized version of `muse-project-alist' into something
113 Muse can make use of."
114 (set sym val)
115 (when muse-project-alist-using-customize
116 ;; Make sure the unescaped version is written to .emacs
117 (put sym 'saved-value (list (custom-quote val)))
118 ;; Perform unescaping
119 (while val
120 (let ((head (car (cdar val)))
121 res)
122 ;; Turn cons cells into flat list, string->symbol
123 (while head
124 (cond ((stringp (car head))
125 (add-to-list 'res (car head) t))
126 ((consp (car head))
127 (add-to-list 'res (intern (caar head)) t)
128 (add-to-list 'res (car (cdar head)) t)))
129 (setq head (cdr head)))
130 (setcdr (car val) (cons res (cdr (cdar val)))))
131 (let ((styles (cdar val)))
132 ;; String->symbol in every style
133 (while (cdr styles)
134 (let ((head (cadr styles))
135 res)
136 (while (consp head)
137 (setq res (plist-put res (intern (car head))
138 (cadr head)))
139 (setq head (cddr head)))
140 (setcdr styles (cons res (cddr styles))))
141 (setq styles (cdr styles))))
142 (setq val (cdr val)))))
144 (define-widget 'muse-project 'default
145 "A widget that defines a Muse project."
146 :format "\n%v"
147 :value-create 'muse-widget-type-value-create
148 :value-get 'muse-widget-child-value-get
149 :value-delete 'ignore
150 :match 'muse-widget-type-match
151 :type '(cons :format " %v"
152 (repeat :tag "Settings" :format "%{%t%}:\n%v%i\n\n"
153 (choice
154 (string :tag "Directory")
155 (list :tag "Book function"
156 (const :tag ":book-funcall" ":book-funcall")
157 (choice (function)
158 (sexp :tag "Unknown")))
159 (list :tag "Book part"
160 (const :tag ":book-part" ":book-part")
161 (string :tag "Name"))
162 (list :tag "Book style"
163 (const :tag ":book-style" ":book-style")
164 (string :tag "Style"))
165 (list :tag "Default file"
166 (const :tag ":default" ":default")
167 (string :tag "File"))
168 (list :tag "End of book"
169 (const :tag ":book-end" ":book-end")
170 (const t))
171 (list :tag "Force publishing"
172 (const :tag ":force-publish" ":force-publish")
173 (repeat (string :tag "File")))
174 (list :tag "Major mode"
175 (const :tag ":major-mode" ":major-mode")
176 (choice (function :tag "Mode")
177 (sexp :tag "Unknown")))
178 (list :tag "New chapter"
179 (const :tag ":book-chapter" ":book-chapter")
180 (string :tag "Name"))
181 (list :tag "No chapters"
182 (const :tag ":nochapters" ":nochapters")
183 (const t))
184 (list :tag "Project-level publishing function"
185 (const :tag ":publish-project"
186 ":publish-project")
187 (choice (function :tag "Function")
188 (sexp :tag "Unknown")))
189 (list :tag "Set variables"
190 (const :tag ":set" ":set")
191 (repeat (list :inline t
192 (symbol :tag "Variable")
193 (sexp :tag "Setting"))))
194 (list :tag "Visit links using"
195 (const :tag ":visit-link" ":visit-link")
196 (choice (function)
197 (sexp :tag "Unknown")))))
198 (repeat :tag "Output styles" :format "%{%t%}:\n%v%i\n\n"
199 (set :tag "Style"
200 (list :inline t
201 :tag "Publishing style"
202 (const :tag ":base" ":base")
203 (string :tag "Style"))
204 (list :inline t
205 :tag "Base URL"
206 (const :tag ":base-url" ":base-url")
207 (string :tag "URL"))
208 (list :inline t
209 :tag "Exclude matching"
210 (const :tag ":exclude" ":exclude")
211 (regexp))
212 (list :inline t
213 :tag "Include matching"
214 (const :tag ":include" ":include")
215 (regexp))
216 (list :inline t
217 :tag "Timestamps file"
218 (const :tag ":timestamps" ":timestamps")
219 (file))
220 (list :inline t
221 :tag "Path"
222 (const :tag ":path" ":path")
223 (string :tag "Path"))))))
225 (defcustom muse-project-alist nil
226 "An alist of Muse projects.
227 A project defines a fileset, and a list of custom attributes for use
228 when publishing files in that project."
229 :type '(choice (const :tag "No projects defined." nil)
230 (repeat (cons :format "%{%t%}:\n\n%v"
231 :tag "Project" :indent 4
232 (string :tag "Project name")
233 muse-project))
234 (sexp :tag "Cannot parse expression"))
235 :get 'muse-project-alist-get
236 :set 'muse-project-alist-set
237 :group 'muse-project)
239 ;; Make it easier to specify a muse-project-alist entry
241 (defcustom muse-project-ignore-regexp
242 (concat "\\`\\(#.*#\\|.*,v\\|.*~\\|\\.\\.?\\|\\.#.*\\|,.*\\)\\'\\|"
243 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\|\\.svn\\|"
244 "\\.hg\\|\\.git\\|\\.bzr\\|_darcs\\)\\(/\\|\\'\\)")
245 "A regexp matching files to be ignored in Muse directories.
247 You should set `case-fold-search' to nil before using this regexp
248 in code."
249 :type 'regexp
250 :group 'muse-regexp)
252 (defcustom muse-project-publish-private-files t
253 "If this is non-nil, files will be published even if their permissions
254 are set so that no one else on the filesystem can read them.
256 Set this to nil if you would like to indicate that some files
257 should not be published by manually doing \"chmod o-rwx\" on
258 them.
260 This setting has no effect under Windows (that is, all files are
261 published regardless of permissions) because Windows lacks the
262 needed filesystem attributes."
263 :type 'boolean
264 :group 'muse-project)
266 (defun muse-project-recurse-directory (base)
267 "Recusively retrieve all of the directories underneath BASE.
268 A list of these directories is returned.
270 Directories starting with \".\" will be ignored, as well as those
271 which match `muse-project-ignore-regexp'."
272 (let ((case-fold-search nil)
273 list dir)
274 (when (and (file-directory-p base)
275 (not (string-match muse-project-ignore-regexp base)))
276 (dolist (file (directory-files base t "^[^.]"))
277 (when (and (file-directory-p file)
278 (not (string-match muse-project-ignore-regexp file)))
279 (setq dir (file-name-nondirectory file))
280 (push dir list)
281 (nconc list (mapcar #'(lambda (item)
282 (concat dir "/" item))
283 (muse-project-recurse-directory file)))))
284 list)))
286 (defun muse-project-alist-styles (entry-dir output-dir style &rest other)
287 "Return a list of styles to use in a `muse-project-alist' entry.
288 ENTRY-DIR is the top-level directory of the project.
289 OUTPUT-DIR is where Muse files are published, keeping directory structure.
290 STYLE is the publishing style to use.
292 OTHER contains other definitions to add to each style. It is optional.
294 For an example of the use of this function, see
295 `examples/mwolson/muse-init.el' from the Muse distribution."
296 (let ((fnd (file-name-nondirectory entry-dir)))
297 (when (string= fnd "")
298 ;; deal with cases like "foo/" that have a trailing slash
299 (setq fnd (file-name-nondirectory (substring entry-dir 0 -1))))
300 (cons `(:base ,style :path ,(expand-file-name output-dir)
301 :include ,(concat "/" fnd "/[^/]+$")
302 ,@other)
303 (mapcar (lambda (dir)
304 `(:base ,style
305 :path ,(expand-file-name dir output-dir)
306 :include ,(concat "/" dir "/[^/]+$")
307 ,@other))
308 (muse-project-recurse-directory entry-dir)))))
310 (defun muse-project-alist-dirs (entry-dir)
311 "Return a list of directories to use in a `muse-project-alist' entry.
312 ENTRY-DIR is the top-level directory of the project.
314 For an example of the use of this function, see
315 `examples/mwolson/muse-init.el' from the Muse distribution."
316 (cons (expand-file-name entry-dir)
317 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
318 (muse-project-recurse-directory entry-dir))))
320 ;; Constructing the file-alist
322 (defvar muse-project-file-alist nil
323 "This variable is automagically constructed as needed.")
325 (defvar muse-project-file-alist-hook nil
326 "Functions that are to be exectuted immediately after updating
327 `muse-project-file-alist'.")
329 (defvar muse-current-project nil
330 "Project we are currently visiting.")
331 (make-variable-buffer-local 'muse-current-project)
332 (defvar muse-current-project-global nil
333 "Project we are currently visiting. This is used to propagate the value
334 of `muse-current-project' into a new buffer during publishing.")
336 (defvar muse-current-output-style nil
337 "The output style that we are currently using for publishing files.")
339 (defsubst muse-project (&optional project)
340 "Resolve the given PROJECT into a full Muse project, if it is a string."
341 (if (null project)
342 (or muse-current-project
343 (muse-project-of-file))
344 (if (stringp project)
345 (assoc project muse-project-alist)
346 (muse-assert (consp project))
347 project)))
349 (defun muse-project-page-file (page project &optional no-check-p)
350 "Return a filename if PAGE exists within the given Muse PROJECT."
351 (setq project (muse-project project))
352 (if (null page)
353 ;; if not given a page, return the first directory instead
354 (let ((pats (cadr project)))
355 (catch 'done
356 (while pats
357 (if (symbolp (car pats))
358 (setq pats (cddr pats))
359 (throw 'done (file-name-as-directory (car pats)))))))
360 (let ((dir (file-name-directory page))
361 (expanded-path nil))
362 (when dir
363 (setq expanded-path (concat (expand-file-name
364 page
365 (file-name-directory (muse-current-file)))
366 (when muse-file-extension
367 (concat "." muse-file-extension))))
368 (setq page (file-name-nondirectory page)))
369 (let ((files (muse-collect-alist
370 (muse-project-file-alist project no-check-p)
371 page))
372 (matches nil))
373 (if dir
374 (catch 'done
375 (save-match-data
376 (dolist (file files)
377 (if (and expanded-path
378 (string= expanded-path (cdr file)))
379 (throw 'done (cdr file))
380 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
381 (file-name-directory
382 (cdr file)))))
383 (when pos
384 (setq matches (cons (cons pos (cdr file))
385 matches)))))))
386 ;; if we haven't found an exact match, pick a candidate
387 (car (muse-sort-by-rating matches)))
388 (dolist (file files)
389 (setq matches (cons (cons (length (cdr file)) (cdr file))
390 matches)))
391 (car (muse-sort-by-rating matches '<)))))))
393 (defun muse-project-private-p (file)
394 "Return non-nil if NAME is a private page with PROJECT."
395 (unless (or muse-under-windows-p
396 muse-project-publish-private-files)
397 (setq file (file-truename file))
398 (if (file-attributes file) ; don't publish if no attributes exist
399 (or (when (eq ?- (aref (nth 8 (file-attributes
400 (file-name-directory file))) 7))
401 (message (concat
402 "The " (file-name-directory file)
403 " directory must be readable by others"
404 " in order for its contents to be published.")))
405 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
406 t)))
408 (defun muse-project-file-entries (path)
409 (let* ((names (list t))
410 (lnames names)
411 (case-fold-search nil))
412 (cond
413 ((file-directory-p path)
414 (dolist (file (directory-files
415 path t (when (and muse-file-extension
416 (not (string= muse-file-extension "")))
417 (concat "." muse-file-extension "\\'"))))
418 (unless (or (string-match muse-project-ignore-regexp file)
419 (string-match muse-project-ignore-regexp
420 (file-name-nondirectory file))
421 (file-directory-p file))
422 (setcdr lnames
423 (cons (cons (muse-page-name file) file) nil))
424 (setq lnames (cdr lnames)))))
425 ((file-readable-p path)
426 (setcdr lnames
427 (cons (cons (muse-page-name path) path) nil))
428 (setq lnames (cdr lnames)))
429 (t ; regexp
430 (muse-assert (file-name-directory path))
431 (dolist (file (directory-files
432 (file-name-directory path) t
433 (file-name-nondirectory path)))
434 (unless (or (string-match muse-project-ignore-regexp file)
435 (string-match muse-project-ignore-regexp
436 (file-name-nondirectory file)))
437 (setcdr lnames
438 (cons (cons (muse-page-name file) file) nil))
439 (setq lnames (cdr lnames))))))
440 (cdr names)))
442 (defvar muse-updating-file-alist-p nil
443 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
445 (defun muse-project-determine-last-mod (project &optional no-check-p)
446 "Return the most recent last-modified timestamp of dirs in PROJECT."
447 (let ((last-mod nil))
448 (unless (or muse-under-windows-p no-check-p)
449 (let ((pats (cadr project)))
450 (while pats
451 (if (symbolp (car pats))
452 (setq pats (cddr pats))
453 (let* ((fnd (file-name-directory (car pats)))
454 (dir (cond ((file-directory-p (car pats))
455 (car pats))
456 ((and (not (file-readable-p (car pats)))
458 (file-directory-p fnd))
459 fnd))))
460 (when dir
461 (let ((mod-time (nth 5 (file-attributes dir))))
462 (when (or (null last-mod)
463 (and mod-time
464 (muse-time-less-p last-mod mod-time)))
465 (setq last-mod mod-time)))))
466 (setq pats (cdr pats))))))
467 last-mod))
469 (defun muse-project-file-alist (&optional project no-check-p)
470 "Return member filenames for the given Muse PROJECT.
471 Also, update the `muse-project-file-alist' variable.
473 On UNIX, this alist is only updated if one of the directories'
474 contents have changed. On Windows, it is always reread from
475 disk.
477 If NO-CHECK-P is non-nil, do not update the alist, just return
478 the current one."
479 (setq project (muse-project project))
480 (when (and project muse-project-alist)
481 (let* ((file-alist (assoc (car project) muse-project-file-alist))
482 (last-mod (muse-project-determine-last-mod project no-check-p)))
483 ;; Either return the currently known list, or read it again from
484 ;; disk
485 (if (or (and no-check-p (cadr file-alist))
486 muse-updating-file-alist-p
487 (not (or muse-under-windows-p
488 (null (cddr file-alist))
489 (null last-mod)
490 (muse-time-less-p (cddr file-alist) last-mod))))
491 (cadr file-alist)
492 (if file-alist
493 (setcdr (cdr file-alist) last-mod)
494 (setq file-alist (cons (car project) (cons nil last-mod))
495 muse-project-file-alist
496 (cons file-alist muse-project-file-alist)))
497 ;; Read in all of the file entries
498 (let ((muse-updating-file-alist-p t))
499 (prog1
500 (save-match-data
501 (setcar
502 (cdr file-alist)
503 (let* ((names (list t))
504 (pats (cadr project)))
505 (while pats
506 (if (symbolp (car pats))
507 (setq pats (cddr pats))
508 (nconc names (muse-project-file-entries (car pats)))
509 (setq pats (cdr pats))))
510 (cdr names))))
511 (run-hooks 'muse-project-file-alist-hook)))))))
513 (defun muse-project-add-to-alist (file &optional project)
514 "Make sure FILE is added to `muse-project-file-alist'.
516 It works by either calling the `muse-project-file-alist' function
517 if a directory has been modified since we last checked, or
518 manually forcing the file entry to exist in the alist. This
519 works around an issue where if several files being saved at the
520 same time, only the first one will make it into the alist. It is
521 meant to be called by `muse-project-after-save-hook'.
523 The project of the file is determined by either the PROJECT
524 argument, or `muse-project-of-file' if PROJECT is not specified."
525 (setq project (or (muse-project project) (muse-project-of-file file)))
526 (when (and project muse-project-alist)
527 (let* ((file-alist (assoc (car project) muse-project-file-alist))
528 (last-mod (muse-project-determine-last-mod project)))
529 ;; Determine whether we need to call this
530 (if (or (null (cddr file-alist))
531 (null last-mod)
532 (muse-time-less-p (cddr file-alist) last-mod))
533 ;; The directory will show up as modified, so go ahead and
534 ;; call `muse-project-file-alist'
535 (muse-project-file-alist project)
536 ;; It is not showing as modified, so forcefully add the
537 ;; current file to the project file-alist
538 (let ((muse-updating-file-alist-p t))
539 (prog1
540 (save-match-data
541 (setcar (cdr file-alist)
542 (nconc (muse-project-file-entries file)
543 (cadr file-alist))))
544 (run-hooks 'muse-project-file-alist-hook)))))))
546 (defun muse-project-of-file (&optional pathname)
547 "Determine which project the given PATHNAME relates to.
548 If PATHNAME is nil, the current buffer's filename is used."
549 (if (and (null pathname) muse-current-project)
550 muse-current-project
551 (unless pathname (setq pathname (muse-current-file)))
552 (save-match-data
553 (when (and (stringp pathname)
554 muse-project-alist
555 (not (string= pathname ""))
556 (not (let ((case-fold-search nil))
557 (or (string-match muse-project-ignore-regexp
558 pathname)
559 (string-match muse-project-ignore-regexp
560 (file-name-nondirectory
561 pathname))))))
562 (let* ((file (file-truename pathname))
563 (dir (file-name-directory file))
564 found rating matches)
565 (catch 'found
566 (dolist (project-entry muse-project-alist)
567 (let ((pats (cadr project-entry)))
568 (while pats
569 (if (symbolp (car pats))
570 (setq pats (cddr pats))
571 (let ((tname (file-truename (car pats))))
572 (cond ((or (string= tname file)
573 (string= (file-name-as-directory tname) dir))
574 (throw 'found project-entry))
575 ((string-match (concat "\\`" (regexp-quote tname))
576 file)
577 (setq matches (cons (cons (match-end 0)
578 project-entry)
579 matches)))))
580 (setq pats (cdr pats))))))
581 ;; if we haven't found an exact match, pick a candidate
582 (car (muse-sort-by-rating matches))))))))
584 (defun muse-project-after-save-hook ()
585 "Update Muse's file-alist if we are saving a Muse file."
586 (let ((project (muse-project-of-file)))
587 (when project
588 (muse-project-add-to-alist (buffer-file-name) project))))
590 (add-hook 'after-save-hook 'muse-project-after-save-hook)
592 (defun muse-read-project (prompt &optional no-check-p no-assume)
593 "Read a project name from the minibuffer, if it can't be figured
594 out."
595 (if (null muse-project-alist)
596 (error "There are no Muse projects defined; see `muse-project-alist'")
597 (or (unless no-check-p
598 (muse-project-of-file))
599 (if (and (not no-assume)
600 (= 1 (length muse-project-alist)))
601 (car muse-project-alist)
602 (assoc (funcall muse-completing-read-function
603 prompt muse-project-alist)
604 muse-project-alist)))))
606 (defvar muse-project-page-history nil)
608 (defun muse-read-project-file (project prompt &optional default)
609 (let* ((file-list (muse-delete-dups
610 (mapcar #'(lambda (a) (list (car a)))
611 (muse-project-file-alist project))))
612 (name (funcall muse-completing-read-function
613 prompt file-list nil nil nil
614 'muse-project-page-history default)))
615 (cons name (muse-project-page-file name project))))
617 ;;;###autoload
618 (defun muse-project-find-file (name project &optional command directory)
619 "Open the Muse page given by NAME in PROJECT.
620 If COMMAND is non-nil, it is the function used to visit the file.
621 If DIRECTORY is non-nil, it is the directory in which the page
622 will be created if it does not already exist. Otherwise, the
623 first directory within the project's fileset is used."
624 (interactive
625 (let* ((project (muse-read-project "Find in project: "
626 current-prefix-arg))
627 (default (muse-get-keyword :default (cadr project)))
628 (entry (muse-read-project-file
629 project (if default
630 (format "Find page: (default: %s) "
631 default)
632 "Find page: ")
633 default)))
634 (list entry project)))
635 (setq project (muse-project project))
636 (let ((project-name (car project)))
637 (unless (interactive-p)
638 (setq project (muse-project project)
639 name (cons name (muse-project-page-file name project))))
640 ;; If we're given a relative or absolute filename, open it as-is
641 (if (and (car name)
642 (save-match-data
643 (or (string-match "\\`\\.+/" (car name))
644 (string-match muse-file-regexp (car name))
645 (string-match muse-image-regexp (car name)))))
646 (setcdr name (car name))
647 ;; At this point, name is (PAGE . FILE).
648 (unless (cdr name)
649 (let ((pats (cadr project)))
650 (while (and pats (null directory))
651 (if (symbolp (car pats))
652 (setq pats (cddr pats))
653 (if (file-directory-p (car pats))
654 (setq directory (car pats) pats nil)
655 (setq pats (cdr pats))))))
656 (when directory
657 (let ((filename (expand-file-name (car name) directory)))
658 (when (and muse-file-extension
659 (not (string= muse-file-extension ""))
660 (not (file-exists-p (car name))))
661 (setq filename (concat filename "." muse-file-extension)))
662 (unless (file-exists-p directory)
663 (make-directory directory t))
664 (setcdr name filename)))))
665 ;; Open the file
666 (if (cdr name)
667 (funcall (or command 'find-file) (cdr name))
668 (error "There is no page %s in project %s"
669 (car name) project-name))))
671 (defun muse-project-choose-style (closure test styles)
672 "Run TEST on STYLES and return first style where TEST yields non-nil.
673 TEST should take two arguments. The first is CLOSURE, which is
674 passed verbatim. The second if the current style to consider.
676 If no style passes TEST, return the first style."
677 (or (catch 'winner
678 (dolist (style styles)
679 (when (funcall test closure style)
680 (throw 'winner style))))
681 (car styles)))
683 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
684 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
685 return non-nil."
686 (let ((link-suffix (or (muse-style-element :link-suffix style)
687 (muse-style-element :suffix style))))
688 (and (stringp link-suffix)
689 (string= given-suffix link-suffix))))
691 (defun muse-project-applicable-styles (file styles)
692 "Given STYLES, return a list of the ones that are considered for FILE.
693 The name of a project may be used for STYLES."
694 (when (stringp styles)
695 (setq styles (cddr (muse-project styles))))
696 (when (and file styles)
697 (let ((used-styles nil))
698 (dolist (style styles)
699 (let ((include-regexp (muse-style-element :include style))
700 (exclude-regexp (muse-style-element :exclude style))
701 (rating nil))
702 (when (and (or (and (null include-regexp)
703 (null exclude-regexp))
704 (if include-regexp
705 (setq rating (string-match include-regexp file))
706 (not (string-match exclude-regexp file))))
707 (file-exists-p file)
708 (not (muse-project-private-p file)))
709 (setq used-styles (cons (cons rating style) used-styles)))))
710 (muse-sort-by-rating (nreverse used-styles)))))
712 (defun muse-project-get-applicable-style (file styles)
713 "Choose a style from the STYLES that FILE can publish to.
714 The user is prompted if several styles are found."
715 (muse-publish-get-style
716 (mapcar (lambda (style)
717 (cons (muse-get-keyword :base style) style))
718 (muse-project-applicable-styles file styles))))
720 (defun muse-project-resolve-directory (page local-style remote-style)
721 "Figure out the directory part of the path that provides a link to PAGE.
722 LOCAL-STYLE is the style of the current Muse file, and
723 REMOTE-STYLE is the style associated with PAGE.
725 If REMOTE-STYLE has a :base-url element, concatenate it and PAGE.
726 Otherwise, return a relative link."
727 (let ((prefix (muse-style-element :base-url remote-style)))
728 (if prefix
729 (concat prefix page)
730 (file-relative-name (expand-file-name
731 (file-name-nondirectory page)
732 (muse-style-element :path remote-style))
733 (expand-file-name
734 (muse-style-element :path local-style))))))
736 (defun muse-project-resolve-link (page local-style remote-styles)
737 "Return a published link from the output path of one file to another file.
739 The best match for PAGE is determined by comparing the link
740 suffix of the given local style and that of the remote styles.
742 The remote styles are usually populated by
743 `muse-project-applicable-styles'.
745 If no remote style is found, return PAGE verbatim
747 If PAGE has a :base-url associated with it, return the
748 concatenation of the :base-url value and PAGE.
750 Otherwise, return a relative path from the directory of
751 LOCAL-STYLE to the best directory among REMOTE-STYLES."
752 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
753 (muse-style-element :suffix local-style)))
754 remote-style)
755 (if (not (stringp link-suffix))
756 (setq remote-style (car remote-styles))
757 (setq remote-style (muse-project-choose-style
758 link-suffix
759 #'muse-project-choose-style-by-link-suffix
760 remote-styles)))
761 (if (null remote-style)
762 page
763 (setq page (muse-project-resolve-directory
764 page local-style remote-style))
765 (concat (file-name-directory page)
766 (muse-publish-link-name page remote-style)))))
768 (defun muse-project-current-output-style (&optional file project)
769 (or muse-current-output-style
770 (progn
771 (unless file (setq file (muse-current-file)))
772 (unless project (setq project (muse-project-of-file file)))
773 (car (muse-project-applicable-styles file (cddr project))))))
775 (defun muse-project-link-page (page)
776 (let ((project (muse-project-of-file)))
777 (muse-project-resolve-link page
778 (muse-project-current-output-style)
779 (muse-project-applicable-styles
780 (muse-project-page-file page project)
781 (cddr project)))))
783 (defun muse-project-publish-file-default (file style output-dir force)
784 ;; ensure the publishing location is available
785 (unless (file-exists-p output-dir)
786 (message "Creating publishing directory %s" output-dir)
787 (make-directory output-dir t))
788 ;; publish the member file!
789 (muse-publish-file file style output-dir force))
791 (defun muse-project-publish-file (file styles &optional force)
792 (setq styles (muse-project-applicable-styles file styles))
793 (let (published)
794 (dolist (style styles)
795 (if (or (not (listp style))
796 (not (cdr style)))
797 (muse-display-warning
798 (concat "Skipping malformed muse-project-alist style."
799 "\nPlease double-check your configuration,"))
800 (let ((output-dir (muse-style-element :path style))
801 (muse-current-output-style style)
802 (fun (or (muse-style-element :publish style t)
803 'muse-project-publish-file-default)))
804 (when (funcall fun file style output-dir force)
805 (setq published t)))))
806 published))
808 ;;;###autoload
809 (defun muse-project-publish-this-file (&optional force style)
810 "Publish the currently-visited file according to `muse-project-alist',
811 prompting if more than one style applies.
813 If FORCE is given, publish the file even if it is up-to-date.
815 If STYLE is given, use that publishing style rather than
816 prompting for one."
817 (interactive (list current-prefix-arg))
818 (let ((muse-current-project (muse-project-of-file)))
819 (if (not muse-current-project)
820 ;; file is not part of a project, so fall back to muse-publish
821 (if (interactive-p) (call-interactively 'muse-publish-this-file)
822 (muse-publish-this-file nil nil force))
823 (unless style
824 (setq style (muse-project-get-applicable-style
825 buffer-file-name (cddr muse-current-project))))
826 (let* ((output-dir (muse-style-element :path style))
827 (muse-current-project-global muse-current-project)
828 (muse-current-output-style (list :base (car style)
829 :path output-dir))
830 (fun (or (muse-style-element :publish style t)
831 'muse-project-publish-file-default)))
832 (unless (funcall fun buffer-file-name style output-dir force)
833 (message (concat "The published version is up-to-date; use"
834 " C-u C-c C-t to force an update.")))))))
836 (defun muse-project-save-buffers (&optional project)
837 (setq project (muse-project project))
838 (when project
839 (save-excursion
840 (map-y-or-n-p
841 (function
842 (lambda (buffer)
843 (and (buffer-modified-p buffer)
844 (not (buffer-base-buffer buffer))
845 (or (buffer-file-name buffer)
846 (progn
847 (set-buffer buffer)
848 (and buffer-offer-save
849 (> (buffer-size) 0))))
850 (with-current-buffer buffer
851 (let ((proj (muse-project-of-file)))
852 (and proj (string= (car proj)
853 (car project)))))
854 (if (buffer-file-name buffer)
855 (format "Save file %s? "
856 (buffer-file-name buffer))
857 (format "Save buffer %s? "
858 (buffer-name buffer))))))
859 (function
860 (lambda (buffer)
861 (set-buffer buffer)
862 (save-buffer)))
863 (buffer-list)
864 '("buffer" "buffers" "save")
865 (if (boundp 'save-some-buffers-action-alist)
866 save-some-buffers-action-alist)))))
868 (defun muse-project-publish-default (project styles &optional force)
869 "Publish the pages of PROJECT that need publishing."
870 (setq project (muse-project project))
871 (let ((published nil))
872 ;; publish all files in the project, for each style; the actual
873 ;; publishing will only happen if the files are newer than the
874 ;; last published output, or if the file is listed in
875 ;; :force-publish. Files in :force-publish will not trigger the
876 ;; "All pages need to be published" message.
877 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
878 (file-alist (muse-project-file-alist project)))
879 (dolist (pair file-alist)
880 (when (muse-project-publish-file (cdr pair) styles force)
881 (setq forced-files (delete (car pair) forced-files))
882 (setq published t)))
883 (dolist (file forced-files)
884 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
885 ;; run hook after publishing ends
886 (run-hook-with-args 'muse-after-project-publish-hook project)
887 ;; notify the user that everything is now done
888 (if published
889 (message "All pages in %s have been published." (car project))
890 (message "No pages in %s need publishing at this time."
891 (car project)))))
893 ;;;###autoload
894 (defun muse-project-publish (project &optional force)
895 "Publish the pages of PROJECT that need publishing."
896 (interactive (list (muse-read-project "Publish project: " nil t)
897 current-prefix-arg))
898 (setq project (muse-project project))
899 (let ((styles (cddr project))
900 (muse-current-project project)
901 (muse-current-project-global project))
902 ;; determine the style from the project, or else ask
903 (unless styles
904 (setq styles (list (muse-publish-get-style))))
905 (unless project
906 (error "Cannot find a project to publish"))
907 ;; prompt to save any buffers related to this project
908 (muse-project-save-buffers project)
909 ;; run hook before publishing begins
910 (run-hook-with-args 'muse-before-project-publish-hook project)
911 ;; run the project-level publisher
912 (let ((fun (or (muse-get-keyword :publish-project (cadr project) t)
913 'muse-project-publish-default)))
914 (funcall fun project styles force))))
916 (defun muse-project-batch-publish ()
917 "Publish Muse files in batch mode."
918 (let ((muse-batch-publishing-p t)
919 force)
920 (if (string= "--force" (or (car command-line-args-left) ""))
921 (setq force t
922 command-line-args-left (cdr command-line-args-left)))
923 (if command-line-args-left
924 (dolist (project command-line-args-left)
925 (message "Publishing project %s ..." project)
926 (muse-project-publish project force))
927 (message "No projects specified."))))
929 (eval-when-compile
930 (put 'make-local-hook 'byte-compile nil))
932 (defun muse-project-set-variables ()
933 "Load project-specific variables."
934 (when (and muse-current-project-global (null muse-current-project))
935 (setq muse-current-project muse-current-project-global))
936 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
937 sym custom-set var)
938 (while vars
939 (setq sym (car vars))
940 (setq custom-set (or (get sym 'custom-set) 'set))
941 (setq var (if (eq (get sym 'custom-type) 'hook)
942 (make-local-hook sym)
943 (make-local-variable sym)))
944 (funcall custom-set var (car (cdr vars)))
945 (setq vars (cdr (cdr vars))))))
947 (custom-add-option 'muse-before-publish-hook 'muse-project-set-variables)
948 (add-to-list 'muse-before-publish-hook 'muse-project-set-variables)
950 (defun muse-project-delete-output-files (project)
951 (interactive
952 (list (muse-read-project "Remove all output files for project: " nil t)))
953 (setq project (muse-project project))
954 (let ((file-alist (muse-project-file-alist project))
955 (styles (cddr project))
956 output-file path)
957 (dolist (entry file-alist)
958 (dolist (style styles)
959 (setq output-file
960 (and (setq path (muse-style-element :path style))
961 (expand-file-name
962 (concat (muse-style-element :prefix style)
963 (car entry)
964 (or (muse-style-element :osuffix style)
965 (muse-style-element :suffix style)))
966 path)))
967 (if output-file
968 (muse-delete-file-if-exists output-file))))))
970 ;;; muse-project.el ends here