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
,(expand-file-name output-dir
)
301 :include
,(concat "/" fnd
"/[^/]+$")
303 (mapcar (lambda (dir)
305 :path
,(expand-file-name dir output-dir
)
306 :include
,(concat "/" dir
"/[^/]+$")
308 (muse-project-recurse-directory entry-dir
)))))
310 (defun muse-project-alist-dirs (entry-dir)
311 "Return a list of directories to use in a `muse-project-alist' entry.
312 ENTRY-DIR is the top-level directory of the project.
314 For an example of the use of this function, see
315 `examples/mwolson/muse-init.el' from the Muse distribution."
316 (cons (expand-file-name entry-dir
)
317 (mapcar (lambda (dir) (expand-file-name dir entry-dir
))
318 (muse-project-recurse-directory entry-dir
))))
320 ;; Constructing the file-alist
322 (defvar muse-project-file-alist nil
323 "This variable is automagically constructed as needed.")
325 (defvar muse-project-file-alist-hook nil
326 "Functions that are to be exectuted immediately after updating
327 `muse-project-file-alist'.")
329 (defvar muse-current-project nil
330 "Project we are currently visiting.")
331 (make-variable-buffer-local 'muse-current-project
)
332 (defvar muse-current-project-global nil
333 "Project we are currently visiting. This is used to propagate the value
334 of `muse-current-project' into a new buffer during publishing.")
336 (defvar muse-current-output-style nil
337 "The output style that we are currently using for publishing files.")
339 (defsubst muse-project
(&optional project
)
340 "Resolve the given PROJECT into a full Muse project, if it is a string."
342 (or muse-current-project
343 (muse-project-of-file))
344 (if (stringp project
)
345 (assoc project muse-project-alist
)
346 (muse-assert (consp project
))
349 (defun muse-project-page-file (page project
&optional no-check-p
)
350 "Return a filename if PAGE exists within the given Muse PROJECT."
351 (setq project
(muse-project project
))
353 ;; if not given a page, return the first directory instead
354 (let ((pats (cadr project
)))
357 (if (symbolp (car pats
))
358 (setq pats
(cddr pats
))
359 (throw 'done
(file-name-as-directory (car pats
)))))))
360 (let ((dir (file-name-directory page
))
363 (setq expanded-path
(concat (expand-file-name
365 (file-name-directory (muse-current-file)))
366 (when muse-file-extension
367 (concat "." muse-file-extension
))))
368 (setq page
(file-name-nondirectory page
)))
369 (let ((files (muse-collect-alist
370 (muse-project-file-alist project no-check-p
)
377 (if (and expanded-path
378 (string= expanded-path
(cdr file
)))
379 (throw 'done
(cdr file
))
380 (let ((pos (string-match (concat (regexp-quote dir
) "\\'")
384 (setq matches
(cons (cons pos
(cdr file
))
386 ;; if we haven't found an exact match, pick a candidate
387 (car (muse-sort-by-rating matches
)))
389 (setq matches
(cons (cons (length (cdr file
)) (cdr file
))
391 (car (muse-sort-by-rating matches
'<)))))))
393 (defun muse-project-private-p (file)
394 "Return non-nil if NAME is a private page with PROJECT."
395 (unless (or muse-under-windows-p
396 muse-project-publish-private-files
)
397 (setq file
(file-truename file
))
398 (if (file-attributes file
) ; don't publish if no attributes exist
399 (or (when (eq ?-
(aref (nth 8 (file-attributes
400 (file-name-directory file
))) 7))
402 "The " (file-name-directory file
)
403 " directory must be readable by others"
404 " in order for its contents to be published.")))
405 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
408 (defun muse-project-file-entries (path)
409 (let* ((names (list t
))
411 (case-fold-search nil
))
413 ((file-directory-p path
)
414 (dolist (file (directory-files
415 path t
(when (and muse-file-extension
416 (not (string= muse-file-extension
"")))
417 (concat "." muse-file-extension
"\\'"))))
418 (unless (or (string-match muse-project-ignore-regexp file
)
419 (string-match muse-project-ignore-regexp
420 (file-name-nondirectory file
))
421 (file-directory-p file
))
423 (cons (cons (muse-page-name file
) file
) nil
))
424 (setq lnames
(cdr lnames
)))))
425 ((file-readable-p path
)
427 (cons (cons (muse-page-name path
) path
) nil
))
428 (setq lnames
(cdr lnames
)))
430 (muse-assert (file-name-directory path
))
431 (dolist (file (directory-files
432 (file-name-directory path
) t
433 (file-name-nondirectory path
)))
434 (unless (or (string-match muse-project-ignore-regexp file
)
435 (string-match muse-project-ignore-regexp
436 (file-name-nondirectory file
)))
438 (cons (cons (muse-page-name file
) file
) nil
))
439 (setq lnames
(cdr lnames
))))))
442 (defvar muse-updating-file-alist-p nil
443 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
445 (defun muse-project-determine-last-mod (project &optional no-check-p
)
446 "Return the most recent last-modified timestamp of dirs in PROJECT."
447 (let ((last-mod nil
))
448 (unless (or muse-under-windows-p no-check-p
)
449 (let ((pats (cadr project
)))
451 (if (symbolp (car pats
))
452 (setq pats
(cddr pats
))
453 (let* ((fnd (file-name-directory (car pats
)))
454 (dir (cond ((file-directory-p (car pats
))
456 ((and (not (file-readable-p (car pats
)))
458 (file-directory-p fnd
))
461 (let ((mod-time (nth 5 (file-attributes dir
))))
462 (when (or (null last-mod
)
464 (muse-time-less-p last-mod mod-time
)))
465 (setq last-mod mod-time
)))))
466 (setq pats
(cdr pats
))))))
469 (defun muse-project-file-alist (&optional project no-check-p
)
470 "Return member filenames for the given Muse PROJECT.
471 Also, update the `muse-project-file-alist' variable.
473 On UNIX, this alist is only updated if one of the directories'
474 contents have changed. On Windows, it is always reread from
477 If NO-CHECK-P is non-nil, do not update the alist, just return
479 (setq project
(muse-project project
))
480 (when (and project muse-project-alist
)
481 (let* ((file-alist (assoc (car project
) muse-project-file-alist
))
482 (last-mod (muse-project-determine-last-mod project no-check-p
)))
483 ;; Either return the currently known list, or read it again from
485 (if (or (and no-check-p
(cadr file-alist
))
486 muse-updating-file-alist-p
487 (not (or muse-under-windows-p
488 (null (cddr file-alist
))
490 (muse-time-less-p (cddr file-alist
) last-mod
))))
493 (setcdr (cdr file-alist
) last-mod
)
494 (setq file-alist
(cons (car project
) (cons nil last-mod
))
495 muse-project-file-alist
496 (cons file-alist muse-project-file-alist
)))
497 ;; Read in all of the file entries
498 (let ((muse-updating-file-alist-p t
))
503 (let* ((names (list t
))
504 (pats (cadr project
)))
506 (if (symbolp (car pats
))
507 (setq pats
(cddr pats
))
508 (nconc names
(muse-project-file-entries (car pats
)))
509 (setq pats
(cdr pats
))))
511 (run-hooks 'muse-project-file-alist-hook
)))))))
513 (defun muse-project-add-to-alist (file &optional project
)
514 "Make sure FILE is added to `muse-project-file-alist'.
516 It works by either calling the `muse-project-file-alist' function
517 if a directory has been modified since we last checked, or
518 manually forcing the file entry to exist in the alist. This
519 works around an issue where if several files being saved at the
520 same time, only the first one will make it into the alist. It is
521 meant to be called by `muse-project-after-save-hook'.
523 The project of the file is determined by either the PROJECT
524 argument, or `muse-project-of-file' if PROJECT is not specified."
525 (setq project
(or (muse-project project
) (muse-project-of-file file
)))
526 (when (and project muse-project-alist
)
527 (let* ((file-alist (assoc (car project
) muse-project-file-alist
))
528 (last-mod (muse-project-determine-last-mod project
)))
529 ;; Determine whether we need to call this
530 (if (or (null (cddr file-alist
))
532 (muse-time-less-p (cddr file-alist
) last-mod
))
533 ;; The directory will show up as modified, so go ahead and
534 ;; call `muse-project-file-alist'
535 (muse-project-file-alist project
)
536 ;; It is not showing as modified, so forcefully add the
537 ;; current file to the project file-alist
538 (let ((muse-updating-file-alist-p t
))
541 (setcar (cdr file-alist
)
542 (nconc (muse-project-file-entries file
)
544 (run-hooks 'muse-project-file-alist-hook
)))))))
546 (defun muse-project-of-file (&optional pathname
)
547 "Determine which project the given PATHNAME relates to.
548 If PATHNAME is nil, the current buffer's filename is used."
549 (if (and (null pathname
) muse-current-project
)
551 (unless pathname
(setq pathname
(muse-current-file)))
553 (when (and (stringp pathname
)
555 (not (string= pathname
""))
556 (not (let ((case-fold-search nil
))
557 (or (string-match muse-project-ignore-regexp
559 (string-match muse-project-ignore-regexp
560 (file-name-nondirectory
562 (let* ((file (file-truename pathname
))
563 (dir (file-name-directory file
))
564 found rating matches
)
566 (dolist (project-entry muse-project-alist
)
567 (let ((pats (cadr project-entry
)))
569 (if (symbolp (car pats
))
570 (setq pats
(cddr pats
))
571 (let ((tname (file-truename (car pats
))))
572 (cond ((or (string= tname file
)
573 (string= (file-name-as-directory tname
) dir
))
574 (throw 'found project-entry
))
575 ((string-match (concat "\\`" (regexp-quote tname
))
577 (setq matches
(cons (cons (match-end 0)
580 (setq pats
(cdr pats
))))))
581 ;; if we haven't found an exact match, pick a candidate
582 (car (muse-sort-by-rating matches
))))))))
584 (defun muse-project-after-save-hook ()
585 "Update Muse's file-alist if we are saving a Muse file."
586 (let ((project (muse-project-of-file)))
588 (muse-project-add-to-alist (buffer-file-name) project
))))
590 (add-hook 'after-save-hook
'muse-project-after-save-hook
)
592 (defun muse-read-project (prompt &optional no-check-p no-assume
)
593 "Read a project name from the minibuffer, if it can't be figured
595 (if (null muse-project-alist
)
596 (error "There are no Muse projects defined; see `muse-project-alist'")
597 (or (unless no-check-p
598 (muse-project-of-file))
599 (if (and (not no-assume
)
600 (= 1 (length muse-project-alist
)))
601 (car muse-project-alist
)
602 (assoc (funcall muse-completing-read-function
603 prompt muse-project-alist
)
604 muse-project-alist
)))))
606 (defvar muse-project-page-history nil
)
608 (defun muse-read-project-file (project prompt
&optional default
)
609 (let* ((file-list (muse-delete-dups
610 (mapcar #'(lambda (a) (list (car a
)))
611 (muse-project-file-alist project
))))
612 (name (funcall muse-completing-read-function
613 prompt file-list nil nil nil
614 'muse-project-page-history default
)))
615 (cons name
(muse-project-page-file name project
))))
618 (defun muse-project-find-file (name project
&optional command directory
)
619 "Open the Muse page given by NAME in PROJECT.
620 If COMMAND is non-nil, it is the function used to visit the file.
621 If DIRECTORY is non-nil, it is the directory in which the page
622 will be created if it does not already exist. Otherwise, the
623 first directory within the project's fileset is used."
625 (let* ((project (muse-read-project "Find in project: "
627 (default (muse-get-keyword :default
(cadr project
)))
628 (entry (muse-read-project-file
630 (format "Find page: (default: %s) "
634 (list entry project
)))
635 (setq project
(muse-project project
))
636 (let ((project-name (car project
)))
637 (unless (interactive-p)
638 (setq project
(muse-project project
)
639 name
(cons name
(muse-project-page-file name project
))))
640 ;; If we're given a relative or absolute filename, open it as-is
643 (or (string-match "\\`\\.+/" (car name
))
644 (string-match muse-file-regexp
(car name
))
645 (string-match muse-image-regexp
(car name
)))))
646 (setcdr name
(car name
))
647 ;; At this point, name is (PAGE . FILE).
649 (let ((pats (cadr project
)))
650 (while (and pats
(null directory
))
651 (if (symbolp (car pats
))
652 (setq pats
(cddr pats
))
653 (if (file-directory-p (car pats
))
654 (setq directory
(car pats
) pats nil
)
655 (setq pats
(cdr pats
))))))
657 (let ((filename (expand-file-name (car name
) directory
)))
658 (when (and muse-file-extension
659 (not (string= muse-file-extension
""))
660 (not (file-exists-p (car name
))))
661 (setq filename
(concat filename
"." muse-file-extension
)))
662 (unless (file-exists-p directory
)
663 (make-directory directory t
))
664 (setcdr name filename
)))))
667 (funcall (or command
'find-file
) (cdr name
))
668 (error "There is no page %s in project %s"
669 (car name
) project-name
))))
671 (defun muse-project-choose-style (closure test styles
)
672 "Run TEST on STYLES and return first style where TEST yields non-nil.
673 TEST should take two arguments. The first is CLOSURE, which is
674 passed verbatim. The second if the current style to consider.
676 If no style passes TEST, return the first style."
678 (dolist (style styles
)
679 (when (funcall test closure style
)
680 (throw 'winner style
))))
683 (defun muse-project-choose-style-by-link-suffix (given-suffix style
)
684 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
686 (let ((link-suffix (or (muse-style-element :link-suffix style
)
687 (muse-style-element :suffix style
))))
688 (and (stringp link-suffix
)
689 (string= given-suffix link-suffix
))))
691 (defun muse-project-applicable-styles (file styles
)
692 "Given STYLES, return a list of the ones that are considered for FILE.
693 The name of a project may be used for STYLES."
694 (when (stringp styles
)
695 (setq styles
(cddr (muse-project styles
))))
696 (when (and file styles
)
697 (let ((used-styles nil
))
698 (dolist (style styles
)
699 (let ((include-regexp (muse-style-element :include style
))
700 (exclude-regexp (muse-style-element :exclude style
))
702 (when (and (or (and (null include-regexp
)
703 (null exclude-regexp
))
705 (setq rating
(string-match include-regexp file
))
706 (not (string-match exclude-regexp file
))))
708 (not (muse-project-private-p file
)))
709 (setq used-styles
(cons (cons rating style
) used-styles
)))))
710 (muse-sort-by-rating (nreverse used-styles
)))))
712 (defun muse-project-get-applicable-style (file styles
)
713 "Choose a style from the STYLES that FILE can publish to.
714 The user is prompted if several styles are found."
715 (muse-publish-get-style
716 (mapcar (lambda (style)
717 (cons (muse-get-keyword :base style
) style
))
718 (muse-project-applicable-styles file styles
))))
720 (defun muse-project-resolve-directory (page local-style remote-style
)
721 "Figure out the directory part of the path that provides a link to PAGE.
722 LOCAL-STYLE is the style of the current Muse file, and
723 REMOTE-STYLE is the style associated with PAGE.
725 If REMOTE-STYLE has a :base-url element, concatenate it and PAGE.
726 Otherwise, return a relative link."
727 (let ((prefix (muse-style-element :base-url remote-style
)))
730 (file-relative-name (expand-file-name
731 (file-name-nondirectory page
)
732 (muse-style-element :path remote-style
))
734 (muse-style-element :path local-style
))))))
736 (defun muse-project-resolve-link (page local-style remote-styles
)
737 "Return a published link from the output path of one file to another file.
739 The best match for PAGE is determined by comparing the link
740 suffix of the given local style and that of the remote styles.
742 The remote styles are usually populated by
743 `muse-project-applicable-styles'.
745 If no remote style is found, return PAGE verbatim
747 If PAGE has a :base-url associated with it, return the
748 concatenation of the :base-url value and PAGE.
750 Otherwise, return a relative path from the directory of
751 LOCAL-STYLE to the best directory among REMOTE-STYLES."
752 (let ((link-suffix (or (muse-style-element :link-suffix local-style
)
753 (muse-style-element :suffix local-style
)))
755 (if (not (stringp link-suffix
))
756 (setq remote-style
(car remote-styles
))
757 (setq remote-style
(muse-project-choose-style
759 #'muse-project-choose-style-by-link-suffix
761 (if (null remote-style
)
763 (setq page
(muse-project-resolve-directory
764 page local-style remote-style
))
765 (concat (file-name-directory page
)
766 (muse-publish-link-name page remote-style
)))))
768 (defun muse-project-current-output-style (&optional file project
)
769 (or muse-current-output-style
771 (unless file
(setq file
(muse-current-file)))
772 (unless project
(setq project
(muse-project-of-file file
)))
773 (car (muse-project-applicable-styles file
(cddr project
))))))
775 (defun muse-project-link-page (page)
776 (let ((project (muse-project-of-file)))
777 (muse-project-resolve-link page
778 (muse-project-current-output-style)
779 (muse-project-applicable-styles
780 (muse-project-page-file page project
)
783 (defun muse-project-publish-file-default (file style output-dir force
)
784 ;; ensure the publishing location is available
785 (unless (file-exists-p output-dir
)
786 (message "Creating publishing directory %s" output-dir
)
787 (make-directory output-dir t
))
788 ;; publish the member file!
789 (muse-publish-file file style output-dir force
))
791 (defun muse-project-publish-file (file styles
&optional force
)
792 (setq styles
(muse-project-applicable-styles file styles
))
794 (dolist (style styles
)
795 (if (or (not (listp style
))
797 (muse-display-warning
798 (concat "Skipping malformed muse-project-alist style."
799 "\nPlease double-check your configuration,"))
800 (let ((output-dir (muse-style-element :path style
))
801 (muse-current-output-style style
)
802 (fun (or (muse-style-element :publish style t
)
803 'muse-project-publish-file-default
)))
804 (when (funcall fun file style output-dir force
)
805 (setq published t
)))))
809 (defun muse-project-publish-this-file (&optional force
)
810 "Publish the currently-visited file according to `muse-project-alist',
811 prompting if more than one style applies.
813 If FORCE is given, publish the file even if it is up-to-date."
814 (interactive (list current-prefix-arg
))
815 (let ((muse-current-project (muse-project-of-file)))
816 (if (not muse-current-project
)
817 ;; file is not part of a project, so fall back to muse-publish
818 (if (interactive-p) (call-interactively 'muse-publish-this-file
)
819 (muse-publish-this-file nil nil force
))
820 (let* ((style (muse-project-get-applicable-style
821 buffer-file-name
(cddr muse-current-project
)))
822 (output-dir (muse-style-element :path style
))
823 (muse-current-project-global muse-current-project
)
824 (muse-current-output-style (list :base
(car style
)
826 (fun (or (muse-style-element :publish style t
)
827 'muse-project-publish-file-default
)))
828 (unless (funcall fun buffer-file-name style output-dir force
)
829 (message (concat "The published version is up-to-date; use"
830 " C-u C-c C-t to force an update.")))))))
832 (defun muse-project-save-buffers (&optional project
)
833 (setq project
(muse-project project
))
839 (and (buffer-modified-p buffer
)
840 (not (buffer-base-buffer buffer
))
841 (or (buffer-file-name buffer
)
844 (and buffer-offer-save
845 (> (buffer-size) 0))))
846 (with-current-buffer buffer
847 (let ((proj (muse-project-of-file)))
848 (and proj
(string= (car proj
)
850 (if (buffer-file-name buffer
)
851 (format "Save file %s? "
852 (buffer-file-name buffer
))
853 (format "Save buffer %s? "
854 (buffer-name buffer
))))))
860 '("buffer" "buffers" "save")
861 (if (boundp 'save-some-buffers-action-alist
)
862 save-some-buffers-action-alist
)))))
864 (defun muse-project-publish-default (project styles
&optional force
)
865 "Publish the pages of PROJECT that need publishing."
866 (setq project
(muse-project project
))
867 (let ((published nil
))
868 ;; publish all files in the project, for each style; the actual
869 ;; publishing will only happen if the files are newer than the
870 ;; last published output, or if the file is listed in
871 ;; :force-publish. Files in :force-publish will not trigger the
872 ;; "All pages need to be published" message.
873 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
)))
874 (file-alist (muse-project-file-alist project
)))
875 (dolist (pair file-alist
)
876 (when (muse-project-publish-file (cdr pair
) styles force
)
877 (setq forced-files
(delete (car pair
) forced-files
))
879 (dolist (file forced-files
)
880 (muse-project-publish-file (cdr (assoc file file-alist
)) styles t
)))
881 ;; run hook after publishing ends
882 (run-hook-with-args 'muse-after-project-publish-hook project
)
883 ;; notify the user that everything is now done
885 (message "All pages in %s have been published." (car project
))
886 (message "No pages in %s need publishing at this time."
890 (defun muse-project-publish (project &optional force
)
891 "Publish the pages of PROJECT that need publishing."
892 (interactive (list (muse-read-project "Publish project: " nil t
)
894 (setq project
(muse-project project
))
895 (let ((styles (cddr project
))
896 (muse-current-project project
)
897 (muse-current-project-global project
))
898 ;; determine the style from the project, or else ask
900 (setq styles
(list (muse-publish-get-style))))
902 (error "Cannot find a project to publish"))
903 ;; prompt to save any buffers related to this project
904 (muse-project-save-buffers project
)
905 ;; run hook before publishing begins
906 (run-hook-with-args 'muse-before-project-publish-hook project
)
907 ;; run the project-level publisher
908 (let ((fun (or (muse-get-keyword :publish-project
(cadr project
) t
)
909 'muse-project-publish-default
)))
910 (funcall fun project styles force
))))
912 (defun muse-project-batch-publish ()
913 "Publish Muse files in batch mode."
914 (let ((muse-batch-publishing-p t
)
916 (if (string= "--force" (or (car command-line-args-left
) ""))
918 command-line-args-left
(cdr command-line-args-left
)))
919 (if command-line-args-left
920 (dolist (project command-line-args-left
)
921 (message "Publishing project %s ..." project
)
922 (muse-project-publish project force
))
923 (message "No projects specified."))))
926 (put 'make-local-hook
'byte-compile nil
))
928 (defun muse-project-set-variables ()
929 "Load project-specific variables."
930 (when (and muse-current-project-global
(null muse-current-project
))
931 (setq muse-current-project muse-current-project-global
))
932 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
935 (setq sym
(car vars
))
936 (setq custom-set
(or (get sym
'custom-set
) 'set
))
937 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
938 (make-local-hook sym
)
939 (make-local-variable sym
)))
940 (funcall custom-set var
(car (cdr vars
)))
941 (setq vars
(cdr (cdr vars
))))))
943 (custom-add-option 'muse-before-publish-hook
'muse-project-set-variables
)
944 (add-to-list 'muse-before-publish-hook
'muse-project-set-variables
)
946 (defun muse-project-delete-output-files (project)
948 (list (muse-read-project "Remove all output files for project: " nil t
)))
949 (setq project
(muse-project project
))
950 (let ((file-alist (muse-project-file-alist project
))
951 (styles (cddr project
))
953 (dolist (entry file-alist
)
954 (dolist (style styles
)
956 (and (setq path
(muse-style-element :path style
))
958 (concat (muse-style-element :prefix style
)
960 (or (muse-style-element :osuffix style
)
961 (muse-style-element :suffix style
)))
964 (muse-delete-file-if-exists output-file
))))))
966 ;;; muse-project.el ends here