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