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.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (provide 'muse-project
)
37 (require 'muse-publish
)
40 (defgroup muse-project nil
41 "Options controlling the behavior of Muse project handling."
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)"
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)"
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
)
65 (unless (muse-project ,project
)
66 (error "Can't find project %s" ,project
))
69 (setq muse-current-project
(muse-project ,project
))
70 (muse-project-set-variables)
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."
79 (setq muse-project-alist-using-customize t
)
80 (let* ((val (copy-alist (symbol-value sym
)))
83 (let ((head (car (cdar val
)))
85 ;; Turn settings of first part into cons cells, symbol->string
87 (cond ((stringp (car head
))
88 (add-to-list 'res
(car head
) t
)
89 (setq head
(cdr head
)))
91 (add-to-list 'res
(list (symbol-name (car head
))
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
100 (let ((head (cadr styles
))
103 (setq res
(plist-put res
(symbol-name (car head
))
105 (setq head
(cddr head
)))
106 (setcdr styles
(cons res
(cddr styles
))))
107 (setq styles
(cdr styles
))))
108 (setq val
(cdr val
)))
111 (defun muse-project-alist-set (sym val
)
112 "Turn customized version of `muse-project-alist' into something
113 Muse can make use of."
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
120 (let ((head (car (cdar val
)))
122 ;; Turn cons cells into flat list, string->symbol
124 (cond ((stringp (car head
))
125 (add-to-list 'res
(car head
) t
))
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
134 (let ((head (cadr styles
))
137 (setq res
(plist-put res
(intern (car 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."
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"
154 (string :tag
"Directory")
155 (list :tag
"Book function"
156 (const :tag
":book-funcall" ":book-funcall")
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")
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")
184 (list :tag
"Project-level publishing function"
185 (const :tag
":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")
197 (sexp :tag
"Unknown")))))
198 (repeat :tag
"Output styles" :format
"%{%t%}:\n%v%i\n\n"
201 :tag
"Publishing style"
202 (const :tag
":base" ":base")
203 (string :tag
"Style"))
206 (const :tag
":base-url" ":base-url")
209 :tag
"Exclude matching"
210 (const :tag
":exclude" ":exclude")
213 :tag
"Include matching"
214 (const :tag
":include" ":include")
217 :tag
"Timestamps file"
218 (const :tag
":timestamps" ":timestamps")
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")
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
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
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."
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
)
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
))
281 (nconc list
(mapcar #'(lambda (item)
282 (concat dir
"/" item
))
283 (muse-project-recurse-directory file
)))))
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
)
302 (expand-file-name output-dir
))
303 :include
,(concat "/" fnd
"/[^/]+$")
305 (mapcar (lambda (dir)
307 :path
,(expand-file-name dir output-dir
)
308 :include
,(concat "/" dir
"/[^/]+$")
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."
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
))
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
))
355 ;; if not given a page, return the first directory instead
356 (let ((pats (cadr project
)))
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
))
365 (setq expanded-path
(concat (expand-file-name
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
)
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
) "\\'")
386 (setq matches
(cons (cons pos
(cdr file
))
388 ;; if we haven't found an exact match, pick a candidate
389 (car (muse-sort-by-rating matches
)))
391 (setq matches
(cons (cons (length (cdr file
)) (cdr file
))
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))
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)))
410 (defun muse-project-file-entries (path)
411 (let* ((names (list t
))
413 (case-fold-search nil
))
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
))
425 (cons (cons (muse-page-name file
) file
) nil
))
426 (setq lnames
(cdr lnames
)))))
427 ((file-readable-p path
)
429 (cons (cons (muse-page-name path
) path
) nil
))
430 (setq lnames
(cdr lnames
)))
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
)))
440 (cons (cons (muse-page-name file
) file
) nil
))
441 (setq lnames
(cdr lnames
))))))
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
)))
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
))
458 ((and (not (file-readable-p (car pats
)))
460 (file-directory-p fnd
))
463 (let ((mod-time (nth 5 (file-attributes dir
))))
464 (when (or (null last-mod
)
466 (muse-time-less-p last-mod mod-time
)))
467 (setq last-mod mod-time
)))))
468 (setq pats
(cdr pats
))))))
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
479 If NO-CHECK-P is non-nil, do not update the alist, just return
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
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
))
492 (muse-time-less-p (cddr file-alist
) last-mod
))))
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
))
505 (let* ((names (list t
))
506 (pats (cadr project
)))
508 (if (symbolp (car pats
))
509 (setq pats
(cddr pats
))
510 (nconc names
(muse-project-file-entries (car pats
)))
511 (setq pats
(cdr pats
))))
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
))
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
))
543 (setcar (cdr file-alist
)
544 (nconc (muse-project-file-entries file
)
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
)
553 (unless pathname
(setq pathname
(muse-current-file)))
555 (when (and (stringp pathname
)
557 (not (string= pathname
""))
558 (not (let ((case-fold-search nil
))
559 (or (string-match muse-project-ignore-regexp
561 (string-match muse-project-ignore-regexp
562 (file-name-nondirectory
564 (let* ((file (file-truename pathname
))
565 (dir (file-name-directory file
))
566 found rating matches
)
568 (dolist (project-entry muse-project-alist
)
569 (let ((pats (cadr project-entry
)))
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
))
579 (setq matches
(cons (cons (match-end 0)
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)))
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
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
))))
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."
627 (let* ((project (muse-read-project "Find in project: "
629 (default (muse-get-keyword :default
(cadr project
)))
630 (entry (muse-read-project-file
632 (format "Find page: (default: %s) "
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
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).
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
))))))
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
)))))
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."
680 (dolist (style styles
)
681 (when (funcall test closure style
)
682 (throw 'winner style
))))
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,
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
))
704 (when (and (or (and (null include-regexp
)
705 (null exclude-regexp
))
707 (setq rating
(string-match include-regexp file
))
708 (not (string-match exclude-regexp 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
)))
732 (file-relative-name (expand-file-name
733 (file-name-nondirectory page
)
734 (muse-style-element :path remote-style
))
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
)))
757 (if (not (stringp link-suffix
))
758 (setq remote-style
(car remote-styles
))
759 (setq remote-style
(muse-project-choose-style
761 #'muse-project-choose-style-by-link-suffix
763 (if (null remote-style
)
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
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
)
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
))
796 (dolist (style styles
)
797 (if (or (not (listp 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
)))))
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
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
))
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
)
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
))
845 (and (buffer-modified-p buffer
)
846 (not (buffer-base-buffer buffer
))
847 (or (buffer-file-name 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
)
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
))))))
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
))
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
891 (message "All pages in %s have been published." (car project
))
892 (message "No pages in %s need publishing at this time."
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
)
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
906 (setq styles
(list (muse-publish-get-style))))
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
)
922 (if (string= "--force" (or (car command-line-args-left
) ""))
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."))))
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
)))
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)
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
))
959 (dolist (entry file-alist
)
960 (dolist (style styles
)
962 (and (setq path
(muse-style-element :path style
))
964 (concat (muse-style-element :prefix style
)
966 (or (muse-style-element :osuffix style
)
967 (muse-style-element :suffix style
)))
970 (muse-delete-file-if-exists output-file
))))))
972 ;;; muse-project.el ends here