Update ChangeLog.
[muse-el.git] / lisp / muse-project.el
blob30b5d50b4af612585f9eccd9b1ff4a03b353fe1f
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 ,(if (muse-file-remote-p output-dir)
301 output-dir
302 (expand-file-name output-dir))
303 :include ,(concat "/" fnd "/[^/]+$")
304 ,@other)
305 (mapcar (lambda (dir)
306 `(:base ,style
307 :path ,(expand-file-name dir output-dir)
308 :include ,(concat "/" dir "/[^/]+$")
309 ,@other))
310 (muse-project-recurse-directory entry-dir)))))
312 (defun muse-project-alist-dirs (entry-dir)
313 "Return a list of directories to use in a `muse-project-alist' entry.
314 ENTRY-DIR is the top-level directory of the project.
316 For an example of the use of this function, see
317 `examples/mwolson/muse-init.el' from the Muse distribution."
318 (cons (expand-file-name entry-dir)
319 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
320 (muse-project-recurse-directory entry-dir))))
322 ;; Constructing the file-alist
324 (defvar muse-project-file-alist nil
325 "This variable is automagically constructed as needed.")
327 (defvar muse-project-file-alist-hook nil
328 "Functions that are to be exectuted immediately after updating
329 `muse-project-file-alist'.")
331 (defvar muse-current-project nil
332 "Project we are currently visiting.")
333 (make-variable-buffer-local 'muse-current-project)
334 (defvar muse-current-project-global nil
335 "Project we are currently visiting. This is used to propagate the value
336 of `muse-current-project' into a new buffer during publishing.")
338 (defvar muse-current-output-style nil
339 "The output style that we are currently using for publishing files.")
341 (defsubst muse-project (&optional project)
342 "Resolve the given PROJECT into a full Muse project, if it is a string."
343 (if (null project)
344 (or muse-current-project
345 (muse-project-of-file))
346 (if (stringp project)
347 (assoc project muse-project-alist)
348 (muse-assert (consp project))
349 project)))
351 (defun muse-project-page-file (page project &optional no-check-p)
352 "Return a filename if PAGE exists within the given Muse PROJECT."
353 (setq project (muse-project project))
354 (if (null page)
355 ;; if not given a page, return the first directory instead
356 (let ((pats (cadr project)))
357 (catch 'done
358 (while pats
359 (if (symbolp (car pats))
360 (setq pats (cddr pats))
361 (throw 'done (file-name-as-directory (car pats)))))))
362 (let ((dir (file-name-directory page))
363 (expanded-path nil))
364 (when dir
365 (setq expanded-path (concat (expand-file-name
366 page
367 (file-name-directory (muse-current-file)))
368 (when muse-file-extension
369 (concat "." muse-file-extension))))
370 (setq page (file-name-nondirectory page)))
371 (let ((files (muse-collect-alist
372 (muse-project-file-alist project no-check-p)
373 page))
374 (matches nil))
375 (if dir
376 (catch 'done
377 (save-match-data
378 (dolist (file files)
379 (if (and expanded-path
380 (string= expanded-path (cdr file)))
381 (throw 'done (cdr file))
382 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
383 (file-name-directory
384 (cdr file)))))
385 (when pos
386 (setq matches (cons (cons pos (cdr file))
387 matches)))))))
388 ;; if we haven't found an exact match, pick a candidate
389 (car (muse-sort-by-rating matches)))
390 (dolist (file files)
391 (setq matches (cons (cons (length (cdr file)) (cdr file))
392 matches)))
393 (car (muse-sort-by-rating matches '<)))))))
395 (defun muse-project-private-p (file)
396 "Return non-nil if NAME is a private page with PROJECT."
397 (unless (or muse-under-windows-p
398 muse-project-publish-private-files)
399 (setq file (file-truename file))
400 (if (file-attributes file) ; don't publish if no attributes exist
401 (or (when (eq ?- (aref (nth 8 (file-attributes
402 (file-name-directory file))) 7))
403 (message (concat
404 "The " (file-name-directory file)
405 " directory must be readable by others"
406 " in order for its contents to be published.")))
407 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
408 t)))
410 (defun muse-project-file-entries (path)
411 (let* ((names (list t))
412 (lnames names)
413 (case-fold-search nil))
414 (cond
415 ((file-directory-p path)
416 (dolist (file (directory-files
417 path t (when (and muse-file-extension
418 (not (string= muse-file-extension "")))
419 (concat "." muse-file-extension "\\'"))))
420 (unless (or (string-match muse-project-ignore-regexp file)
421 (string-match muse-project-ignore-regexp
422 (file-name-nondirectory file))
423 (file-directory-p file))
424 (setcdr lnames
425 (cons (cons (muse-page-name file) file) nil))
426 (setq lnames (cdr lnames)))))
427 ((file-readable-p path)
428 (setcdr lnames
429 (cons (cons (muse-page-name path) path) nil))
430 (setq lnames (cdr lnames)))
431 (t ; regexp
432 (muse-assert (file-name-directory path))
433 (dolist (file (directory-files
434 (file-name-directory path) t
435 (file-name-nondirectory path)))
436 (unless (or (string-match muse-project-ignore-regexp file)
437 (string-match muse-project-ignore-regexp
438 (file-name-nondirectory file)))
439 (setcdr lnames
440 (cons (cons (muse-page-name file) file) nil))
441 (setq lnames (cdr lnames))))))
442 (cdr names)))
444 (defvar muse-updating-file-alist-p nil
445 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
447 (defun muse-project-determine-last-mod (project &optional no-check-p)
448 "Return the most recent last-modified timestamp of dirs in PROJECT."
449 (let ((last-mod nil))
450 (unless (or muse-under-windows-p no-check-p)
451 (let ((pats (cadr project)))
452 (while pats
453 (if (symbolp (car pats))
454 (setq pats (cddr pats))
455 (let* ((fnd (file-name-directory (car pats)))
456 (dir (cond ((file-directory-p (car pats))
457 (car pats))
458 ((and (not (file-readable-p (car pats)))
460 (file-directory-p fnd))
461 fnd))))
462 (when dir
463 (let ((mod-time (nth 5 (file-attributes dir))))
464 (when (or (null last-mod)
465 (and mod-time
466 (muse-time-less-p last-mod mod-time)))
467 (setq last-mod mod-time)))))
468 (setq pats (cdr pats))))))
469 last-mod))
471 (defun muse-project-file-alist (&optional project no-check-p)
472 "Return member filenames for the given Muse PROJECT.
473 Also, update the `muse-project-file-alist' variable.
475 On UNIX, this alist is only updated if one of the directories'
476 contents have changed. On Windows, it is always reread from
477 disk.
479 If NO-CHECK-P is non-nil, do not update the alist, just return
480 the current one."
481 (setq project (muse-project project))
482 (when (and project muse-project-alist)
483 (let* ((file-alist (assoc (car project) muse-project-file-alist))
484 (last-mod (muse-project-determine-last-mod project no-check-p)))
485 ;; Either return the currently known list, or read it again from
486 ;; disk
487 (if (or (and no-check-p (cadr file-alist))
488 muse-updating-file-alist-p
489 (not (or muse-under-windows-p
490 (null (cddr file-alist))
491 (null last-mod)
492 (muse-time-less-p (cddr file-alist) last-mod))))
493 (cadr file-alist)
494 (if file-alist
495 (setcdr (cdr file-alist) last-mod)
496 (setq file-alist (cons (car project) (cons nil last-mod))
497 muse-project-file-alist
498 (cons file-alist muse-project-file-alist)))
499 ;; Read in all of the file entries
500 (let ((muse-updating-file-alist-p t))
501 (prog1
502 (save-match-data
503 (setcar
504 (cdr file-alist)
505 (let* ((names (list t))
506 (pats (cadr project)))
507 (while pats
508 (if (symbolp (car pats))
509 (setq pats (cddr pats))
510 (nconc names (muse-project-file-entries (car pats)))
511 (setq pats (cdr pats))))
512 (cdr names))))
513 (run-hooks 'muse-project-file-alist-hook)))))))
515 (defun muse-project-add-to-alist (file &optional project)
516 "Make sure FILE is added to `muse-project-file-alist'.
518 It works by either calling the `muse-project-file-alist' function
519 if a directory has been modified since we last checked, or
520 manually forcing the file entry to exist in the alist. This
521 works around an issue where if several files being saved at the
522 same time, only the first one will make it into the alist. It is
523 meant to be called by `muse-project-after-save-hook'.
525 The project of the file is determined by either the PROJECT
526 argument, or `muse-project-of-file' if PROJECT is not specified."
527 (setq project (or (muse-project project) (muse-project-of-file file)))
528 (when (and project muse-project-alist)
529 (let* ((file-alist (assoc (car project) muse-project-file-alist))
530 (last-mod (muse-project-determine-last-mod project)))
531 ;; Determine whether we need to call this
532 (if (or (null (cddr file-alist))
533 (null last-mod)
534 (muse-time-less-p (cddr file-alist) last-mod))
535 ;; The directory will show up as modified, so go ahead and
536 ;; call `muse-project-file-alist'
537 (muse-project-file-alist project)
538 ;; It is not showing as modified, so forcefully add the
539 ;; current file to the project file-alist
540 (let ((muse-updating-file-alist-p t))
541 (prog1
542 (save-match-data
543 (setcar (cdr file-alist)
544 (nconc (muse-project-file-entries file)
545 (cadr file-alist))))
546 (run-hooks 'muse-project-file-alist-hook)))))))
548 (defun muse-project-of-file (&optional pathname)
549 "Determine which project the given PATHNAME relates to.
550 If PATHNAME is nil, the current buffer's filename is used."
551 (if (and (null pathname) muse-current-project)
552 muse-current-project
553 (unless pathname (setq pathname (muse-current-file)))
554 (save-match-data
555 (when (and (stringp pathname)
556 muse-project-alist
557 (not (string= pathname ""))
558 (not (let ((case-fold-search nil))
559 (or (string-match muse-project-ignore-regexp
560 pathname)
561 (string-match muse-project-ignore-regexp
562 (file-name-nondirectory
563 pathname))))))
564 (let* ((file (file-truename pathname))
565 (dir (file-name-directory file))
566 found rating matches)
567 (catch 'found
568 (dolist (project-entry muse-project-alist)
569 (let ((pats (cadr project-entry)))
570 (while pats
571 (if (symbolp (car pats))
572 (setq pats (cddr pats))
573 (let ((tname (file-truename (car pats))))
574 (cond ((or (string= tname file)
575 (string= (file-name-as-directory tname) dir))
576 (throw 'found project-entry))
577 ((string-match (concat "\\`" (regexp-quote tname))
578 file)
579 (setq matches (cons (cons (match-end 0)
580 project-entry)
581 matches)))))
582 (setq pats (cdr pats))))))
583 ;; if we haven't found an exact match, pick a candidate
584 (car (muse-sort-by-rating matches))))))))
586 (defun muse-project-after-save-hook ()
587 "Update Muse's file-alist if we are saving a Muse file."
588 (let ((project (muse-project-of-file)))
589 (when project
590 (muse-project-add-to-alist (buffer-file-name) project))))
592 (add-hook 'after-save-hook 'muse-project-after-save-hook)
594 (defun muse-read-project (prompt &optional no-check-p no-assume)
595 "Read a project name from the minibuffer, if it can't be figured
596 out."
597 (if (null muse-project-alist)
598 (error "There are no Muse projects defined; see `muse-project-alist'")
599 (or (unless no-check-p
600 (muse-project-of-file))
601 (if (and (not no-assume)
602 (= 1 (length muse-project-alist)))
603 (car muse-project-alist)
604 (assoc (funcall muse-completing-read-function
605 prompt muse-project-alist)
606 muse-project-alist)))))
608 (defvar muse-project-page-history nil)
610 (defun muse-read-project-file (project prompt &optional default)
611 (let* ((file-list (muse-delete-dups
612 (mapcar #'(lambda (a) (list (car a)))
613 (muse-project-file-alist project))))
614 (name (funcall muse-completing-read-function
615 prompt file-list nil nil nil
616 'muse-project-page-history default)))
617 (cons name (muse-project-page-file name project))))
619 ;;;###autoload
620 (defun muse-project-find-file (name project &optional command directory)
621 "Open the Muse page given by NAME in PROJECT.
622 If COMMAND is non-nil, it is the function used to visit the file.
623 If DIRECTORY is non-nil, it is the directory in which the page
624 will be created if it does not already exist. Otherwise, the
625 first directory within the project's fileset is used."
626 (interactive
627 (let* ((project (muse-read-project "Find in project: "
628 current-prefix-arg))
629 (default (muse-get-keyword :default (cadr project)))
630 (entry (muse-read-project-file
631 project (if default
632 (format "Find page: (default: %s) "
633 default)
634 "Find page: ")
635 default)))
636 (list entry project)))
637 (setq project (muse-project project))
638 (let ((project-name (car project)))
639 (unless (interactive-p)
640 (setq project (muse-project project)
641 name (cons name (muse-project-page-file name project))))
642 ;; If we're given a relative or absolute filename, open it as-is
643 (if (and (car name)
644 (save-match-data
645 (or (string-match "\\`\\.+/" (car name))
646 (string-match muse-file-regexp (car name))
647 (string-match muse-image-regexp (car name)))))
648 (setcdr name (car name))
649 ;; At this point, name is (PAGE . FILE).
650 (unless (cdr name)
651 (let ((pats (cadr project)))
652 (while (and pats (null directory))
653 (if (symbolp (car pats))
654 (setq pats (cddr pats))
655 (if (file-directory-p (car pats))
656 (setq directory (car pats) pats nil)
657 (setq pats (cdr pats))))))
658 (when directory
659 (let ((filename (expand-file-name (car name) directory)))
660 (when (and muse-file-extension
661 (not (string= muse-file-extension ""))
662 (not (file-exists-p (car name))))
663 (setq filename (concat filename "." muse-file-extension)))
664 (unless (file-exists-p directory)
665 (make-directory directory t))
666 (setcdr name filename)))))
667 ;; Open the file
668 (if (cdr name)
669 (funcall (or command 'find-file) (cdr name))
670 (error "There is no page %s in project %s"
671 (car name) project-name))))
673 (defun muse-project-choose-style (closure test styles)
674 "Run TEST on STYLES and return first style where TEST yields non-nil.
675 TEST should take two arguments. The first is CLOSURE, which is
676 passed verbatim. The second if the current style to consider.
678 If no style passes TEST, return the first style."
679 (or (catch 'winner
680 (dolist (style styles)
681 (when (funcall test closure style)
682 (throw 'winner style))))
683 (car styles)))
685 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
686 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
687 return non-nil."
688 (let ((link-suffix (or (muse-style-element :link-suffix style)
689 (muse-style-element :suffix style))))
690 (and (stringp link-suffix)
691 (string= given-suffix link-suffix))))
693 (defun muse-project-applicable-styles (file styles)
694 "Given STYLES, return a list of the ones that are considered for FILE.
695 The name of a project may be used for STYLES."
696 (when (stringp styles)
697 (setq styles (cddr (muse-project styles))))
698 (when (and file styles)
699 (let ((used-styles nil))
700 (dolist (style styles)
701 (let ((include-regexp (muse-style-element :include style))
702 (exclude-regexp (muse-style-element :exclude style))
703 (rating nil))
704 (when (and (or (and (null include-regexp)
705 (null exclude-regexp))
706 (if include-regexp
707 (setq rating (string-match include-regexp file))
708 (not (string-match exclude-regexp file))))
709 (file-exists-p file)
710 (not (muse-project-private-p file)))
711 (setq used-styles (cons (cons rating style) used-styles)))))
712 (muse-sort-by-rating (nreverse used-styles)))))
714 (defun muse-project-get-applicable-style (file styles)
715 "Choose a style from the STYLES that FILE can publish to.
716 The user is prompted if several styles are found."
717 (muse-publish-get-style
718 (mapcar (lambda (style)
719 (cons (muse-get-keyword :base style) style))
720 (muse-project-applicable-styles file styles))))
722 (defun muse-project-resolve-directory (page local-style remote-style)
723 "Figure out the directory part of the path that provides a link to PAGE.
724 LOCAL-STYLE is the style of the current Muse file, and
725 REMOTE-STYLE is the style associated with PAGE.
727 If REMOTE-STYLE has a :base-url element, concatenate it and PAGE.
728 Otherwise, return a relative link."
729 (let ((prefix (muse-style-element :base-url remote-style)))
730 (if prefix
731 (concat prefix page)
732 (file-relative-name (expand-file-name
733 (file-name-nondirectory page)
734 (muse-style-element :path remote-style))
735 (expand-file-name
736 (muse-style-element :path local-style))))))
738 (defun muse-project-resolve-link (page local-style remote-styles)
739 "Return a published link from the output path of one file to another file.
741 The best match for PAGE is determined by comparing the link
742 suffix of the given local style and that of the remote styles.
744 The remote styles are usually populated by
745 `muse-project-applicable-styles'.
747 If no remote style is found, return PAGE verbatim
749 If PAGE has a :base-url associated with it, return the
750 concatenation of the :base-url value and PAGE.
752 Otherwise, return a relative path from the directory of
753 LOCAL-STYLE to the best directory among REMOTE-STYLES."
754 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
755 (muse-style-element :suffix local-style)))
756 remote-style)
757 (if (not (stringp link-suffix))
758 (setq remote-style (car remote-styles))
759 (setq remote-style (muse-project-choose-style
760 link-suffix
761 #'muse-project-choose-style-by-link-suffix
762 remote-styles)))
763 (if (null remote-style)
764 page
765 (setq page (muse-project-resolve-directory
766 page local-style remote-style))
767 (concat (file-name-directory page)
768 (muse-publish-link-name page remote-style)))))
770 (defun muse-project-current-output-style (&optional file project)
771 (or muse-current-output-style
772 (progn
773 (unless file (setq file (muse-current-file)))
774 (unless project (setq project (muse-project-of-file file)))
775 (car (muse-project-applicable-styles file (cddr project))))))
777 (defun muse-project-link-page (page)
778 (let ((project (muse-project-of-file)))
779 (muse-project-resolve-link page
780 (muse-project-current-output-style)
781 (muse-project-applicable-styles
782 (muse-project-page-file page project)
783 (cddr project)))))
785 (defun muse-project-publish-file-default (file style output-dir force)
786 ;; ensure the publishing location is available
787 (unless (file-exists-p output-dir)
788 (message "Creating publishing directory %s" output-dir)
789 (make-directory output-dir t))
790 ;; publish the member file!
791 (muse-publish-file file style output-dir force))
793 (defun muse-project-publish-file (file styles &optional force)
794 (setq styles (muse-project-applicable-styles file styles))
795 (let (published)
796 (dolist (style styles)
797 (if (or (not (listp style))
798 (not (cdr style)))
799 (muse-display-warning
800 (concat "Skipping malformed muse-project-alist style."
801 "\nPlease double-check your configuration,"))
802 (let ((output-dir (muse-style-element :path style))
803 (muse-current-output-style style)
804 (fun (or (muse-style-element :publish style t)
805 'muse-project-publish-file-default)))
806 (when (funcall fun file style output-dir force)
807 (setq published t)))))
808 published))
810 ;;;###autoload
811 (defun muse-project-publish-this-file (&optional force style)
812 "Publish the currently-visited file according to `muse-project-alist',
813 prompting if more than one style applies.
815 If FORCE is given, publish the file even if it is up-to-date.
817 If STYLE is given, use that publishing style rather than
818 prompting for one."
819 (interactive (list current-prefix-arg))
820 (let ((muse-current-project (muse-project-of-file)))
821 (if (not muse-current-project)
822 ;; file is not part of a project, so fall back to muse-publish
823 (if (interactive-p) (call-interactively 'muse-publish-this-file)
824 (muse-publish-this-file style nil force))
825 (unless style
826 (setq style (muse-project-get-applicable-style
827 buffer-file-name (cddr muse-current-project))))
828 (let* ((output-dir (muse-style-element :path style))
829 (muse-current-project-global muse-current-project)
830 (muse-current-output-style (list :base (car style)
831 :path output-dir))
832 (fun (or (muse-style-element :publish style t)
833 'muse-project-publish-file-default)))
834 (unless (funcall fun buffer-file-name style output-dir force)
835 (message (concat "The published version is up-to-date; use"
836 " C-u C-c C-t to force an update.")))))))
838 (defun muse-project-save-buffers (&optional project)
839 (setq project (muse-project project))
840 (when project
841 (save-excursion
842 (map-y-or-n-p
843 (function
844 (lambda (buffer)
845 (and (buffer-modified-p buffer)
846 (not (buffer-base-buffer buffer))
847 (or (buffer-file-name buffer)
848 (progn
849 (set-buffer buffer)
850 (and buffer-offer-save
851 (> (buffer-size) 0))))
852 (with-current-buffer buffer
853 (let ((proj (muse-project-of-file)))
854 (and proj (string= (car proj)
855 (car project)))))
856 (if (buffer-file-name buffer)
857 (format "Save file %s? "
858 (buffer-file-name buffer))
859 (format "Save buffer %s? "
860 (buffer-name buffer))))))
861 (function
862 (lambda (buffer)
863 (set-buffer buffer)
864 (save-buffer)))
865 (buffer-list)
866 '("buffer" "buffers" "save")
867 (if (boundp 'save-some-buffers-action-alist)
868 save-some-buffers-action-alist)))))
870 (defun muse-project-publish-default (project styles &optional force)
871 "Publish the pages of PROJECT that need publishing."
872 (setq project (muse-project project))
873 (let ((published nil))
874 ;; publish all files in the project, for each style; the actual
875 ;; publishing will only happen if the files are newer than the
876 ;; last published output, or if the file is listed in
877 ;; :force-publish. Files in :force-publish will not trigger the
878 ;; "All pages need to be published" message.
879 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
880 (file-alist (muse-project-file-alist project)))
881 (dolist (pair file-alist)
882 (when (muse-project-publish-file (cdr pair) styles force)
883 (setq forced-files (delete (car pair) forced-files))
884 (setq published t)))
885 (dolist (file forced-files)
886 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
887 ;; run hook after publishing ends
888 (run-hook-with-args 'muse-after-project-publish-hook project)
889 ;; notify the user that everything is now done
890 (if published
891 (message "All pages in %s have been published." (car project))
892 (message "No pages in %s need publishing at this time."
893 (car project)))))
895 ;;;###autoload
896 (defun muse-project-publish (project &optional force)
897 "Publish the pages of PROJECT that need publishing."
898 (interactive (list (muse-read-project "Publish project: " nil t)
899 current-prefix-arg))
900 (setq project (muse-project project))
901 (let ((styles (cddr project))
902 (muse-current-project project)
903 (muse-current-project-global project))
904 ;; determine the style from the project, or else ask
905 (unless styles
906 (setq styles (list (muse-publish-get-style))))
907 (unless project
908 (error "Cannot find a project to publish"))
909 ;; prompt to save any buffers related to this project
910 (muse-project-save-buffers project)
911 ;; run hook before publishing begins
912 (run-hook-with-args 'muse-before-project-publish-hook project)
913 ;; run the project-level publisher
914 (let ((fun (or (muse-get-keyword :publish-project (cadr project) t)
915 'muse-project-publish-default)))
916 (funcall fun project styles force))))
918 (defun muse-project-batch-publish ()
919 "Publish Muse files in batch mode."
920 (let ((muse-batch-publishing-p t)
921 force)
922 (if (string= "--force" (or (car command-line-args-left) ""))
923 (setq force t
924 command-line-args-left (cdr command-line-args-left)))
925 (if command-line-args-left
926 (dolist (project command-line-args-left)
927 (message "Publishing project %s ..." project)
928 (muse-project-publish project force))
929 (message "No projects specified."))))
931 (eval-when-compile
932 (put 'make-local-hook 'byte-compile nil))
934 (defun muse-project-set-variables ()
935 "Load project-specific variables."
936 (when (and muse-current-project-global (null muse-current-project))
937 (setq muse-current-project muse-current-project-global))
938 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
939 sym custom-set var)
940 (while vars
941 (setq sym (car vars))
942 (setq custom-set (or (get sym 'custom-set) 'set))
943 (setq var (if (eq (get sym 'custom-type) 'hook)
944 (make-local-hook sym)
945 (make-local-variable sym)))
946 (funcall custom-set var (car (cdr vars)))
947 (setq vars (cdr (cdr vars))))))
949 (custom-add-option 'muse-before-publish-hook 'muse-project-set-variables)
950 (add-to-list 'muse-before-publish-hook 'muse-project-set-variables)
952 (defun muse-project-delete-output-files (project)
953 (interactive
954 (list (muse-read-project "Remove all output files for project: " nil t)))
955 (setq project (muse-project project))
956 (let ((file-alist (muse-project-file-alist project))
957 (styles (cddr project))
958 output-file path)
959 (dolist (entry file-alist)
960 (dolist (style styles)
961 (setq output-file
962 (and (setq path (muse-style-element :path style))
963 (expand-file-name
964 (concat (muse-style-element :prefix style)
965 (car entry)
966 (or (muse-style-element :osuffix style)
967 (muse-style-element :suffix style)))
968 path)))
969 (if output-file
970 (muse-delete-file-if-exists output-file))))))
972 ;;; muse-project.el ends here