Ignore Mercurial and bzr metadata directories
[muse-el.git] / lisp / muse-project.el
blob6e2d9f5b6c48c032daf7f25c23ea6c052ec3b42f
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 (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 "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\\|\\.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)
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 For an example of the use of this function, see
293 `examples/mwolson/muse-init.el' from the Muse distribution."
294 (let ((fnd (file-name-nondirectory entry-dir)))
295 (when (string= fnd "")
296 ;; deal with cases like "foo/" that have a trailing slash
297 (setq fnd (file-name-nondirectory (substring entry-dir 0 -1))))
298 (cons `(:base ,style :path ,(expand-file-name output-dir)
299 :include ,(concat "/" fnd "/[^/]+$"))
300 (mapcar (lambda (dir)
301 `(:base ,style
302 :path ,(expand-file-name dir output-dir)
303 :include ,(concat "/" dir "/[^/]+$")))
304 (muse-project-recurse-directory entry-dir)))))
306 (defun muse-project-alist-dirs (entry-dir)
307 "Return a list of directories to use in a `muse-project-alist' entry.
308 ENTRY-DIR is the top-level directory of the project.
310 For an example of the use of this function, see
311 `examples/mwolson/muse-init.el' from the Muse distribution."
312 (cons (expand-file-name entry-dir)
313 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
314 (muse-project-recurse-directory entry-dir))))
316 ;; Constructing the file-alist
318 (defvar muse-project-file-alist nil
319 "This variable is automagically constructed as needed.")
321 (defvar muse-project-file-alist-hook nil
322 "Functions that are to be exectuted immediately after updating
323 `muse-project-file-alist'.")
325 (defvar muse-current-project nil
326 "Project we are currently visiting.")
327 (make-variable-buffer-local 'muse-current-project)
329 (defvar muse-current-output-style nil
330 "The output style that we are currently using for publishing files.")
332 (defsubst muse-project (&optional project)
333 "Resolve the given PROJECT into a full Muse project, if it is a string."
334 (if (null project)
335 (or muse-current-project
336 (muse-project-of-file))
337 (if (stringp project)
338 (assoc project muse-project-alist)
339 (muse-assert (consp project))
340 project)))
342 (defun muse-project-page-file (page project &optional no-check-p)
343 "Return a filename if PAGE exists within the given Muse PROJECT."
344 (setq project (muse-project project))
345 (let ((dir (file-name-directory page))
346 (expanded-path nil))
347 (when dir
348 (setq expanded-path (concat (expand-file-name
349 page
350 (file-name-directory (muse-current-file)))
351 (when muse-file-extension
352 (concat "." muse-file-extension))))
353 (setq page (file-name-nondirectory page)))
354 (let ((files (muse-collect-alist
355 (muse-project-file-alist project no-check-p)
356 page))
357 (matches nil))
358 (if dir
359 (catch 'done
360 (save-match-data
361 (dolist (file files)
362 (if (and expanded-path
363 (string= expanded-path (cdr file)))
364 (throw 'done (cdr file))
365 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
366 (file-name-directory (cdr file)))))
367 (when pos
368 (setq matches (cons (cons pos (cdr file))
369 matches)))))))
370 ;; if we haven't found an exact match, pick a candidate
371 (car (muse-sort-by-rating matches)))
372 (dolist (file files)
373 (setq matches (cons (cons (length (cdr file)) (cdr file))
374 matches)))
375 (car (muse-sort-by-rating matches '<))))))
377 (defun muse-project-private-p (file)
378 "Return non-nil if NAME is a private page with PROJECT."
379 (unless (or muse-under-windows-p
380 muse-project-publish-private-files)
381 (setq file (file-truename file))
382 (if (file-attributes file) ; don't publish if no attributes exist
383 (or (when (eq ?- (aref (nth 8 (file-attributes
384 (file-name-directory file))) 7))
385 (message (concat
386 "The " (file-name-directory file)
387 " directory must be readable by others"
388 " in order for its contents to be published.")))
389 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
390 t)))
392 (defun muse-project-file-entries (path)
393 (let* ((names (list t))
394 (lnames names)
395 (case-fold-search nil))
396 (cond
397 ((file-directory-p path)
398 (dolist (file (directory-files
399 path t (when (and muse-file-extension
400 (not (string= muse-file-extension "")))
401 (concat "." muse-file-extension "\\'"))))
402 (unless (or (string-match muse-project-ignore-regexp file)
403 (string-match muse-project-ignore-regexp
404 (file-name-nondirectory file))
405 (file-directory-p file))
406 (setcdr lnames
407 (cons (cons (muse-page-name file) file) nil))
408 (setq lnames (cdr lnames)))))
409 ((file-readable-p path)
410 (setcdr lnames
411 (cons (cons (muse-page-name path) path) nil))
412 (setq lnames (cdr lnames)))
413 (t ; regexp
414 (muse-assert (file-name-directory path))
415 (dolist (file (directory-files
416 (file-name-directory path) t
417 (file-name-nondirectory path)))
418 (unless (or (string-match muse-project-ignore-regexp file)
419 (string-match muse-project-ignore-regexp
420 (file-name-nondirectory file)))
421 (setcdr lnames
422 (cons (cons (muse-page-name file) file) nil))
423 (setq lnames (cdr lnames))))))
424 (cdr names)))
426 (defvar muse-updating-file-alist-p nil
427 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
429 (defun muse-project-file-alist (&optional project no-check-p)
430 "Return member filenames for the given Muse PROJECT.
431 On UNIX, this list is only updated if one of the directories'
432 contents have changed. On Windows, it is always reread from
433 disk."
434 (setq project (muse-project project))
435 (let* ((file-alist (assoc (car project) muse-project-file-alist))
436 (last-mod (cdr (cdr file-alist))))
437 ;; Determine the last modified of any directory mentioned in the
438 ;; project's pattern list
439 (unless (or muse-under-windows-p no-check-p)
440 (let ((pats (cadr project)))
441 (while pats
442 (if (symbolp (car pats))
443 (setq pats (cddr pats))
444 (let* ((fnd (file-name-directory (car pats)))
445 (dir (cond ((file-directory-p (car pats))
446 (car pats))
447 ((and (not (file-readable-p (car pats)))
449 (file-directory-p fnd))
450 fnd))))
451 (when dir
452 (let ((mod-time (nth 5 (file-attributes dir))))
453 (when (or (null last-mod)
454 (and mod-time
455 (muse-time-less-p last-mod mod-time)))
456 (setq last-mod mod-time)))))
457 (setq pats (cdr pats))))))
458 ;; Either return the currently known list, or read it again from
459 ;; disk
460 (if (or (and no-check-p (cadr file-alist))
461 muse-updating-file-alist-p
462 (not (or muse-under-windows-p
463 (null (cddr file-alist))
464 (null last-mod)
465 (muse-time-less-p (cddr file-alist) last-mod))))
466 (cadr file-alist)
467 (if file-alist
468 (setcdr (cdr file-alist) last-mod)
469 (setq file-alist (cons (car project) (cons nil last-mod))
470 muse-project-file-alist
471 (cons file-alist muse-project-file-alist)))
472 ;; Read in all of the file entries
473 (let ((muse-updating-file-alist-p t))
474 (prog1
475 (save-match-data
476 (setcar
477 (cdr file-alist)
478 (let* ((names (list t))
479 (pats (cadr project)))
480 (while pats
481 (if (symbolp (car pats))
482 (setq pats (cddr pats))
483 (nconc names (muse-project-file-entries (car pats)))
484 (setq pats (cdr pats))))
485 (cdr names))))
486 (run-hooks 'muse-project-file-alist-hook))))))
488 (defun muse-project-of-file (&optional pathname)
489 "Determine which project the given PATHNAME relates to.
490 If PATHNAME is nil, the current buffer's filename is used."
491 (if (and (null pathname) muse-current-project)
492 muse-current-project
493 (unless pathname (setq pathname (muse-current-file)))
494 (save-match-data
495 (when (and (stringp pathname)
496 (not (string= pathname ""))
497 (not (let ((case-fold-search nil))
498 (or (string-match muse-project-ignore-regexp
499 pathname)
500 (string-match muse-project-ignore-regexp
501 (file-name-nondirectory
502 pathname))))))
503 (let* ((file (file-truename pathname))
504 (dir (file-name-directory file))
505 (project-entry muse-project-alist)
506 found)
507 (while (and project-entry (not found))
508 (let ((pats (car (cdar project-entry))))
509 (while (and pats (not found))
510 (if (symbolp (car pats))
511 (setq pats (cddr pats))
512 (let ((truename (file-truename (car pats))))
513 (if (or (string= truename file)
514 (string= truename dir)
515 (string-match (regexp-quote truename) file))
516 (setq found (car project-entry))))
517 (setq pats (cdr pats))))
518 (setq project-entry (cdr project-entry))))
519 found)))))
521 (defun muse-project-after-save-hook ()
522 "Update Muse's file-alist if we are saving a Muse file."
523 (let ((project (muse-project-of-file)))
524 (when project
525 (muse-project-file-alist project))))
527 (add-hook 'after-save-hook 'muse-project-after-save-hook)
529 (defun muse-read-project (prompt &optional no-check-p no-assume)
530 "Read a project name from the minibuffer, if it can't be figured
531 out."
532 (if (null muse-project-alist)
533 (error "There are no Muse projects defined; see `muse-project-alist'")
534 (or (unless no-check-p
535 (muse-project-of-file))
536 (if (and (not no-assume)
537 (= 1 (length muse-project-alist)))
538 (car muse-project-alist)
539 (assoc (completing-read prompt muse-project-alist)
540 muse-project-alist)))))
542 (defvar muse-project-page-history nil)
544 (defun muse-read-project-file (project prompt &optional default)
545 (let ((name (completing-read prompt (muse-project-file-alist project)
546 nil nil nil 'muse-project-page-history
547 default)))
548 (cons name (muse-project-page-file name project))))
550 ;;;###autoload
551 (defun muse-project-find-file (name project &optional command directory)
552 "Open the Muse page given by NAME in PROJECT.
553 If COMMAND is non-nil, it is the function used to visit the file.
554 If DIRECTORY is non-nil, it is the directory in which the page
555 will be created if it does not already exist. Otherwise, the
556 first directory within the project's fileset is used."
557 (interactive
558 (let* ((project (muse-read-project "Find in project: "
559 current-prefix-arg))
560 (default (muse-get-keyword :default (cadr project)))
561 (entry (muse-read-project-file
562 project (if default
563 (format "Find page: (default: %s) "
564 default)
565 "Find page: ")
566 default)))
567 (list entry project)))
568 (setq project (muse-project project))
569 (let ((project-name (car project)))
570 (unless (interactive-p)
571 (setq project (muse-project project)
572 name (cons name (muse-project-page-file name project))))
573 ;; If we're given a relative or absolute filename, open it as-is
574 (if (and (car name)
575 (save-match-data
576 (or (string-match "\\`\\.+/" (car name))
577 (string-match muse-file-regexp (car name))
578 (string-match muse-image-regexp (car name)))))
579 (setcdr name (car name))
580 ;; At this point, name is (PAGE . FILE).
581 (unless (cdr name)
582 (let ((pats (cadr project)))
583 (while (and pats (null directory))
584 (if (symbolp (car pats))
585 (setq pats (cddr pats))
586 (if (file-directory-p (car pats))
587 (setq directory (car pats) pats nil)
588 (setq pats (cdr pats))))))
589 (when directory
590 (let ((filename (expand-file-name (car name) directory)))
591 (when (and muse-file-extension
592 (not (string= muse-file-extension ""))
593 (not (file-exists-p (car name))))
594 (setq filename (concat filename "." muse-file-extension)))
595 (unless (file-exists-p directory)
596 (make-directory directory t))
597 (setcdr name filename)))))
598 ;; Open the file
599 (if (cdr name)
600 (funcall (or command 'find-file) (cdr name))
601 (error "There is no page %s in project %s"
602 (car name) project-name))))
604 (defun muse-project-choose-style (closure test styles)
605 "Run TEST on STYLES and return first style where TEST yields non-nil.
606 TEST should take two arguments. The first is CLOSURE, which is
607 passed verbatim. The second if the current style to consider.
609 If no style passes TEST, return the first style."
610 (or (catch 'winner
611 (dolist (style styles)
612 (when (funcall test closure style)
613 (throw 'winner style))))
614 (car styles)))
616 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
617 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
618 return non-nil."
619 (let ((link-suffix (or (muse-style-element :link-suffix style)
620 (muse-style-element :suffix style))))
621 (and (stringp link-suffix)
622 (string= given-suffix link-suffix))))
624 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
625 "Given STYLES, return a list of the ones that are considered for FILE.
626 The name of a project may be used for STYLES."
627 (when (stringp styles)
628 (setq styles (cddr (muse-project styles))))
629 (when (and file styles)
630 (let ((used-styles nil))
631 (dolist (style styles)
632 (let ((include-regexp (muse-style-element :include style))
633 (exclude-regexp (muse-style-element :exclude style))
634 (rating nil))
635 (when (and (or ignore-regexp
636 (and (null include-regexp)
637 (null exclude-regexp))
638 (if include-regexp
639 (setq rating (string-match include-regexp file))
640 (not (string-match exclude-regexp file))))
641 (or (not (file-exists-p file))
642 (not (muse-project-private-p file))))
643 (setq used-styles (cons (cons rating style) used-styles)))))
644 (muse-sort-by-rating (nreverse used-styles)))))
646 (defun muse-project-get-applicable-style (file styles)
647 "Choose a style from the STYLES that FILE can publish to.
648 The user is prompted if several styles are found."
649 (muse-publish-get-style
650 (mapcar (lambda (style)
651 (cons (muse-get-keyword :base style) style))
652 (muse-project-applicable-styles file styles))))
654 (defun muse-project-resolve-link (page local-style remote-styles)
655 "Return a published relative link from the output path of one file
656 to another file.
658 The best match for PAGE is determined by comparing the link
659 suffix of the given local style and that of the remote styles.
661 The remote styles are usually populated by
662 `muse-project-applicable-styles'."
663 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
664 (muse-style-element :suffix local-style)))
665 remote-style)
666 (if (not (stringp link-suffix))
667 (setq remote-style (car remote-styles))
668 (setq remote-style (muse-project-choose-style
669 link-suffix
670 #'muse-project-choose-style-by-link-suffix
671 remote-styles)))
672 (if (null remote-style)
673 page
674 (muse-publish-link-file
675 (let ((prefix (muse-style-element :base-url remote-style)))
676 (if prefix
677 (concat prefix page)
678 (file-relative-name (expand-file-name
679 (file-name-nondirectory page)
680 (muse-style-element :path remote-style))
681 (expand-file-name
682 (muse-style-element :path local-style)))))
683 remote-style))))
685 (defun muse-project-current-output-style (&optional file project)
686 (or muse-current-output-style
687 (progn
688 (unless file (setq file (muse-current-file)))
689 (unless project (setq project (muse-project-of-file file)))
690 (car (muse-project-applicable-styles file (cddr project))))))
692 (defun muse-project-link-page (page)
693 (let ((project (muse-project-of-file)))
694 (muse-project-resolve-link page
695 (muse-project-current-output-style)
696 (muse-project-applicable-styles
697 (muse-project-page-file page project)
698 (cddr project)))))
700 (defun muse-project-publish-file-default (file style output-dir force)
701 ;; ensure the publishing location is available
702 (unless (file-exists-p output-dir)
703 (message "Creating publishing directory %s" output-dir)
704 (make-directory output-dir t))
705 ;; publish the member file!
706 (muse-publish-file file style output-dir force))
708 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
709 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
710 (let (published)
711 (dolist (style styles)
712 (if (or (not (listp style))
713 (not (cdr style)))
714 (muse-display-warning
715 (concat "Skipping malformed muse-project-alist style."
716 "\nPlease double-check your configuration,"))
717 (let ((output-dir (muse-style-element :path style))
718 (muse-current-output-style style)
719 (fun (or (muse-style-element :publish style t)
720 'muse-project-publish-file-default)))
721 (when (funcall fun file style output-dir force)
722 (setq published t)))))
723 published))
725 ;;;###autoload
726 (defun muse-project-publish-this-file (&optional force)
727 "Publish the currently-visited file according to `muse-project-alist',
728 prompting if more than one style applies.
730 If FORCE is given, publish the file even if it is up-to-date."
731 (interactive (list current-prefix-arg))
732 (let* ((style (muse-project-get-applicable-style
733 buffer-file-name (cddr muse-current-project)))
734 (output-dir (muse-style-element :path style)))
735 (unless (muse-publish-file buffer-file-name style output-dir force)
736 (message (concat "The published version is up-to-date; use"
737 " C-u C-c C-t to force an update.")))))
739 (defun muse-project-save-buffers (&optional project)
740 (setq project (muse-project project))
741 (map-y-or-n-p
742 (function
743 (lambda (buffer)
744 (and (buffer-modified-p buffer)
745 (not (buffer-base-buffer buffer))
746 (or (buffer-file-name buffer)
747 (progn
748 (set-buffer buffer)
749 (and buffer-offer-save
750 (> (buffer-size) 0))))
751 (with-current-buffer buffer
752 (let ((proj (muse-project-of-file)))
753 (and proj (string= (car proj)
754 (car project)))))
755 (if (buffer-file-name buffer)
756 (format "Save file %s? "
757 (buffer-file-name buffer))
758 (format "Save buffer %s? "
759 (buffer-name buffer))))))
760 (function
761 (lambda (buffer)
762 (set-buffer buffer)
763 (save-buffer)))
764 (buffer-list)
765 '("buffer" "buffers" "save")
766 (if (boundp 'save-some-buffers-action-alist)
767 save-some-buffers-action-alist)))
769 (defun muse-project-publish-default (project styles &optional force)
770 "Publish the pages of PROJECT that need publishing."
771 (setq project (muse-project project))
772 (let ((published nil))
773 ;; publish all files in the project, for each style; the actual
774 ;; publishing will only happen if the files are newer than the
775 ;; last published output, or if the file is listed in
776 ;; :force-publish. Files in :force-publish will not trigger the
777 ;; "All pages need to be published" message.
778 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
779 (file-alist (muse-project-file-alist project)))
780 (dolist (pair file-alist)
781 (when (muse-project-publish-file (cdr pair) styles force)
782 (setq forced-files (delete (car pair) forced-files))
783 (setq published t)))
784 (dolist (file forced-files)
785 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
786 ;; run hook after publishing ends
787 (run-hook-with-args 'muse-after-project-publish-hook project)
788 ;; notify the user that everything is now done
789 (if published
790 (message "All pages in %s have been published." (car project))
791 (message "No pages in %s need publishing at this time."
792 (car project)))))
794 ;;;###autoload
795 (defun muse-project-publish (project &optional force)
796 "Publish the pages of PROJECT that need publishing."
797 (interactive (list (muse-read-project "Publish project: " nil t)
798 current-prefix-arg))
799 (setq project (muse-project project))
800 (let ((styles (cddr project))
801 (muse-current-project project))
802 ;; determine the style from the project, or else ask
803 (unless styles
804 (setq styles (list (muse-publish-get-style))))
805 (unless project
806 (error "Cannot find a project to publish"))
807 ;; prompt to save any buffers related to this project
808 (muse-project-save-buffers project)
809 ;; run hook before publishing begins
810 (run-hook-with-args 'muse-before-project-publish-hook project)
811 ;; run the project-level publisher
812 (let ((fun (or (muse-get-keyword :publish-project (cadr project) t)
813 'muse-project-publish-default)))
814 (funcall fun project styles force))))
816 (defun muse-project-batch-publish ()
817 "Publish Muse files in batch mode."
818 (let ((muse-batch-publishing-p t)
819 force)
820 (if (string= "--force" (or (car command-line-args-left) ""))
821 (setq force t
822 command-line-args-left (cdr command-line-args-left)))
823 (if command-line-args-left
824 (dolist (project command-line-args-left)
825 (message "Publishing project %s ..." project)
826 (muse-project-publish project force))
827 (message "No projects specified."))))
829 (eval-when-compile
830 (put 'make-local-hook 'byte-compile nil))
832 (defun muse-project-set-variables ()
833 "Load project-specific variables."
834 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
835 sym custom-set var)
836 (while vars
837 (setq sym (car vars))
838 (setq custom-set (or (get sym 'custom-set) 'set))
839 (setq var (if (eq (get sym 'custom-type) 'hook)
840 (make-local-hook sym)
841 (make-local-variable sym)))
842 (funcall custom-set var (car (cdr vars)))
843 (setq vars (cdr (cdr vars))))))
845 (defun muse-project-delete-output-files (project)
846 (interactive
847 (list (muse-read-project "Remove all output files for project: " nil t)))
848 (setq project (muse-project project))
849 (let ((file-alist (muse-project-file-alist project))
850 (styles (cddr project))
851 output-file path)
852 (dolist (entry file-alist)
853 (dolist (style styles)
854 (setq output-file
855 (and (setq path (muse-style-element :path style))
856 (expand-file-name
857 (concat (muse-style-element :prefix style)
858 (car entry)
859 (or (muse-style-element :osuffix style)
860 (muse-style-element :suffix style)))
861 path)))
862 (if output-file
863 (muse-delete-file-if-exists output-file))))))
865 ;;; muse-project.el ends here