1 ;;; muse-project.el --- handle Muse projects
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
7 ;; Emacs Muse is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; Emacs Muse is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Emacs Muse; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
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
"Set variables"
185 (const :tag
":set" ":set")
186 (repeat (list :inline t
187 (symbol :tag
"Variable")
188 (sexp :tag
"Setting"))))
189 (list :tag
"Visit links using"
190 (const :tag
":visit-link" ":visit-link")
192 (sexp :tag
"Unknown")))))
193 (repeat :tag
"Styles" :format
"%{%t%}:\n%v%i\n\n"
196 :tag
"Publishing style"
197 (const :tag
":base" ":base")
198 (string :tag
"Style"))
201 (const :tag
":base-url" ":base-url")
204 :tag
"Exclude matching"
205 (const :tag
":exclude" ":exclude")
208 :tag
"Include matching"
209 (const :tag
":include" ":include")
212 :tag
"Timestamps file"
213 (const :tag
":timestamps" ":timestamps")
217 (const :tag
":path" ":path")
218 (string :tag
"Path"))))))
220 (defcustom muse-project-alist nil
221 "An alist of Muse projects.
222 A project defines a fileset, and a list of custom attributes for use
223 when publishing files in that project."
224 :type
'(choice (const :tag
"No projects defined." nil
)
225 (repeat (cons :format
"%{%t%}:\n\n%v"
226 :tag
"Project" :indent
4
227 (string :tag
"Project name")
229 (sexp :tag
"Cannot parse expression"))
230 :get
'muse-project-alist-get
231 :set
'muse-project-alist-set
232 :group
'muse-project
)
234 ;; Make it easier to specify a muse-project-alist entry
236 (defcustom muse-project-ignore-regexp
237 (concat "\\`\\(#.*#\\|.*,v\\|.*~\\|\\.\\.?\\|\\.#.*\\|,.*\\)\\'\\|"
238 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\|\\.svn\\|"
239 "_darcs\\)\\(/\\|\\'\\)")
240 "A regexp matching files to be ignored in Muse directories.
242 You should set `case-fold-search' to nil before using this regexp
247 (defcustom muse-project-publish-private-files t
248 "If this is non-nil, files will be published even if their permissions
249 are set so that no one else on the filesystem can read them.
251 Set this to nil if you would like to indicate that some files
252 should not be published by manually doing \"chmod o-rwx\" on
255 This setting has no effect under Windows (that is, all files are
256 published regardless of permissions) because Windows lacks the
257 needed filesystem attributes."
259 :group
'muse-project
)
261 (defun muse-project-recurse-directory (base)
262 "Recusively retrieve all of the directories underneath BASE.
263 A list of these directories is returned.
265 Directories starting with \".\" will be ignored, as well as those
266 which match `muse-project-ignore-regexp'."
267 (let ((case-fold-search nil
)
269 (when (and (file-directory-p base
)
270 (not (string-match muse-project-ignore-regexp base
)))
271 (dolist (file (directory-files base t
"^[^.]"))
272 (when (and (file-directory-p file
)
273 (not (string-match muse-project-ignore-regexp file
)))
274 (setq dir
(file-name-nondirectory file
))
276 (nconc list
(mapcar #'(lambda (item)
277 (concat dir
"/" item
))
278 (muse-project-recurse-directory file
)))))
281 (defun muse-project-alist-styles (entry-dir output-dir style
)
282 "Return a list of styles to use in a `muse-project-alist' entry.
283 ENTRY-DIR is the top-level directory of the project.
284 OUTPUT-DIR is where Muse files are published, keeping directory structure.
285 STYLE is the publishing style to use.
287 For an example of the use of this function, see
288 `examples/mwolson/muse-init.el' from the Muse distribution."
289 (cons `(:base
,style
:path
,(expand-file-name output-dir
)
290 :include
,(concat "/" (file-name-nondirectory entry-dir
)
292 (mapcar (lambda (dir)
294 :path
,(expand-file-name dir output-dir
)
295 :include
,(concat "/" dir
"/[^/]+$")))
296 (muse-project-recurse-directory entry-dir
))))
298 (defun muse-project-alist-dirs (entry-dir)
299 "Return a list of directories to use in a `muse-project-alist' entry.
300 ENTRY-DIR is the top-level directory of the project.
302 For an example of the use of this function, see
303 `examples/mwolson/muse-init.el' from the Muse distribution."
304 (cons (expand-file-name entry-dir
)
305 (mapcar (lambda (dir) (expand-file-name dir entry-dir
))
306 (muse-project-recurse-directory entry-dir
))))
308 ;; Constructing the file-alist
310 (defvar muse-project-file-alist nil
311 "This variable is automagically constructed as needed.")
313 (defvar muse-project-file-alist-hook nil
314 "Functions that are to be exectuted immediately after updating
315 `muse-project-file-alist'.")
317 (defvar muse-current-project nil
318 "Project we are currently visiting.")
319 (make-variable-buffer-local 'muse-current-project
)
321 (defvar muse-current-output-style nil
322 "The output style that we are currently using for publishing files.")
324 (defsubst muse-project
(&optional project
)
325 "Resolve the given PROJECT into a full Muse project, if it is a string."
327 (or muse-current-project
328 (muse-project-of-file))
329 (if (stringp project
)
330 (assoc project muse-project-alist
)
331 (muse-assert (consp project
))
334 (defun muse-project-page-file (page project
&optional no-check-p
)
335 "Return a filename if PAGE exists within the given Muse PROJECT."
336 (setq project
(muse-project project
))
337 (let ((dir (file-name-directory page
))
340 (setq expanded-path
(concat (expand-file-name
342 (file-name-directory (muse-current-file)))
343 (when muse-file-extension
344 (concat "." muse-file-extension
))))
345 (setq page
(file-name-nondirectory page
)))
346 (let ((files (muse-collect-alist
347 (muse-project-file-alist project no-check-p
)
354 (if (and expanded-path
355 (string= expanded-path
(cdr file
)))
356 (throw 'done
(cdr file
))
357 (let ((pos (string-match (concat (regexp-quote dir
) "\\'")
358 (file-name-directory (cdr file
)))))
360 (setq matches
(cons (cons pos
(cdr file
))
362 ;; if we haven't found an exact match, pick a candidate
363 (car (muse-sort-by-rating matches
)))
365 (setq matches
(cons (cons (length (cdr file
)) (cdr file
))
367 (car (muse-sort-by-rating matches
'<))))))
369 (defun muse-project-private-p (file)
370 "Return non-nil if NAME is a private page with PROJECT."
371 (unless (or muse-under-windows-p
372 muse-project-publish-private-files
)
373 (setq file
(file-truename file
))
374 (if (file-attributes file
) ; don't publish if no attributes exist
375 (or (when (eq ?-
(aref (nth 8 (file-attributes
376 (file-name-directory file
))) 7))
378 "The " (file-name-directory file
)
379 " directory must be readable by others"
380 " in order for its contents to be published.")))
381 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
384 (defun muse-project-file-entries (path)
385 (let* ((names (list t
))
387 (case-fold-search nil
))
389 ((file-directory-p path
)
390 (dolist (file (directory-files
391 path t
(when (and muse-file-extension
392 (not (string= muse-file-extension
"")))
393 (concat "." muse-file-extension
"\\'"))))
394 (unless (or (string-match muse-project-ignore-regexp file
)
395 (string-match muse-project-ignore-regexp
396 (file-name-nondirectory file
))
397 (file-directory-p file
))
399 (cons (cons (muse-page-name file
) file
) nil
))
400 (setq lnames
(cdr lnames
)))))
401 ((file-readable-p path
)
403 (cons (cons (muse-page-name path
) path
) nil
))
404 (setq lnames
(cdr lnames
)))
406 (muse-assert (file-name-directory path
))
407 (dolist (file (directory-files
408 (file-name-directory path
) t
409 (file-name-nondirectory path
)))
410 (unless (or (string-match muse-project-ignore-regexp file
)
411 (string-match muse-project-ignore-regexp
412 (file-name-nondirectory file
)))
414 (cons (cons (muse-page-name file
) file
) nil
))
415 (setq lnames
(cdr lnames
))))))
418 (defvar muse-updating-file-alist-p nil
419 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
421 (defun muse-project-file-alist (&optional project no-check-p
)
422 "Return member filenames for the given Muse PROJECT.
423 On UNIX, this list is only updated if one of the directories'
424 contents have changed. On Windows, it is always reread from
426 (setq project
(muse-project project
))
427 (let* ((file-alist (assoc (car project
) muse-project-file-alist
))
428 (last-mod (cdr (cdr file-alist
))))
429 ;; Determine the last modified of any directory mentioned in the
430 ;; project's pattern list
431 (unless (or muse-under-windows-p no-check-p
)
432 (let ((pats (cadr project
)))
434 (if (symbolp (car pats
))
435 (setq pats
(cddr pats
))
436 (let* ((fnd (file-name-directory (car pats
)))
437 (dir (cond ((file-directory-p (car pats
))
439 ((and (not (file-readable-p (car pats
)))
441 (file-directory-p fnd
))
444 (let ((mod-time (nth 5 (file-attributes dir
))))
445 (when (or (null last-mod
)
447 (muse-time-less-p last-mod mod-time
)))
448 (setq last-mod mod-time
)))))
449 (setq pats
(cdr pats
))))))
450 ;; Either return the currently known list, or read it again from
452 (if (or (and no-check-p
(cadr file-alist
))
453 muse-updating-file-alist-p
454 (not (or muse-under-windows-p
455 (null (cddr file-alist
))
457 (muse-time-less-p (cddr file-alist
) last-mod
))))
460 (setcdr (cdr file-alist
) last-mod
)
461 (setq file-alist
(cons (car project
) (cons nil last-mod
))
462 muse-project-file-alist
463 (cons file-alist muse-project-file-alist
)))
464 ;; Read in all of the file entries
465 (let ((muse-updating-file-alist-p t
))
470 (let* ((names (list t
))
471 (pats (cadr project
)))
473 (if (symbolp (car pats
))
474 (setq pats
(cddr pats
))
475 (nconc names
(muse-project-file-entries (car pats
)))
476 (setq pats
(cdr pats
))))
478 (run-hooks 'muse-project-file-alist-hook
))))))
480 (defun muse-project-of-file (&optional pathname
)
481 "Determine which project the given PATHNAME relates to.
482 If PATHNAME is nil, the current buffer's filename is used."
483 (if (and (null pathname
) muse-current-project
)
485 (unless pathname
(setq pathname
(muse-current-file)))
487 (when (and (stringp pathname
)
488 (not (string= pathname
""))
489 (not (let ((case-fold-search nil
))
490 (or (string-match muse-project-ignore-regexp
492 (string-match muse-project-ignore-regexp
493 (file-name-nondirectory
495 (let* ((file (file-truename pathname
))
496 (dir (file-name-directory file
))
497 (project-entry muse-project-alist
)
499 (while (and project-entry
(not found
))
500 (let ((pats (car (cdar project-entry
))))
501 (while (and pats
(not found
))
502 (if (symbolp (car pats
))
503 (setq pats
(cddr pats
))
504 (let ((truename (file-truename (car pats
))))
505 (if (or (string= truename file
)
506 (string= truename dir
)
507 (string-match (regexp-quote truename
) file
))
508 (setq found
(car project-entry
))))
509 (setq pats
(cdr pats
))))
510 (setq project-entry
(cdr project-entry
))))
513 (defun muse-project-after-save-hook ()
514 "Update Muse's file-alist if we are saving a Muse file."
515 (let ((project (muse-project-of-file)))
517 (muse-project-file-alist project
))))
519 (add-hook 'after-save-hook
'muse-project-after-save-hook
)
521 (defun muse-read-project (prompt &optional no-check-p no-assume
)
522 "Read a project name from the minibuffer, if it can't be figured
524 (if (null muse-project-alist
)
525 (error "There are no Muse projects defined; see `muse-project-alist'.")
526 (or (unless no-check-p
527 (muse-project-of-file))
528 (if (and (not no-assume
)
529 (= 1 (length muse-project-alist
)))
530 (car muse-project-alist
)
531 (assoc (completing-read prompt muse-project-alist
)
532 muse-project-alist
)))))
534 (defvar muse-project-page-history nil
)
536 (defun muse-read-project-file (project prompt
&optional default
)
537 (let ((name (completing-read prompt
(muse-project-file-alist project
)
538 nil nil nil
'muse-project-page-history
540 (cons name
(muse-project-page-file name project
))))
543 (defun muse-project-find-file (name project
&optional command directory
)
544 "Open the Muse page given by NAME in PROJECT.
545 If COMMAND is non-nil, it is the function used to visit the file.
546 If DIRECTORY is non-nil, it is the directory in which the page
547 will be created if it does not already exist. Otherwise, the
548 first directory within the project's fileset is used."
550 (let* ((project (muse-read-project "Find in project: "
552 (default (muse-get-keyword :default
(cadr project
)))
553 (entry (muse-read-project-file
555 (format "Find page: (default: %s) "
559 (list entry project
)))
560 (setq project
(muse-project project
))
561 (let ((project-name (car project
)))
562 (unless (interactive-p)
563 (setq project
(muse-project project
)
564 name
(cons name
(muse-project-page-file name project
))))
565 ;; If we're given a relative or absolute filename, open it as-is
568 (or (string-match "\\`\\.+/" (car name
))
569 (string-match muse-file-regexp
(car name
))
570 (string-match muse-image-regexp
(car name
)))))
571 (setcdr name
(car name
))
572 ;; At this point, name is (PAGE . FILE).
574 (let ((pats (cadr project
)))
575 (while (and pats
(null directory
))
576 (if (symbolp (car pats
))
577 (setq pats
(cddr pats
))
578 (if (file-directory-p (car pats
))
579 (setq directory
(car pats
) pats nil
)
580 (setq pats
(cdr pats
))))))
582 (let ((filename (expand-file-name (car name
) directory
)))
583 (when (and muse-file-extension
584 (not (string= muse-file-extension
""))
585 (not (file-exists-p (car name
))))
586 (setq filename
(concat filename
"." muse-file-extension
)))
587 (unless (file-exists-p directory
)
588 (make-directory directory t
))
589 (setcdr name filename
)))))
592 (funcall (or command
'find-file
) (cdr name
))
593 (error "There is no page %s in project %s."
594 (car name
) project-name
))))
596 (defun muse-project-choose-style (closure test styles
)
597 "Run TEST on STYLES and return first style where TEST yields non-nil.
598 TEST should take two arguments. The first is CLOSURE, which is
599 passed verbatim. The second if the current style to consider.
601 If no style passes TEST, return the first style."
603 (dolist (style styles
)
604 (when (funcall test closure style
)
605 (throw 'winner style
))))
608 (defun muse-project-choose-style-by-link-suffix (given-suffix style
)
609 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
611 (let ((link-suffix (or (muse-style-element :link-suffix style
)
612 (muse-style-element :suffix style
))))
613 (and (stringp link-suffix
)
614 (string= given-suffix link-suffix
))))
616 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
617 "Given STYLES, return a list of the ones that are considered for FILE.
618 The name of a project may be used for STYLES."
619 (when (stringp styles
)
620 (setq styles
(cddr (muse-project styles
))))
621 (when (and file styles
)
622 (let ((used-styles nil
))
623 (dolist (style styles
)
624 (let ((include-regexp (muse-style-element :include style
))
625 (exclude-regexp (muse-style-element :exclude style
))
627 (when (and (or ignore-regexp
628 (and (null include-regexp
)
629 (null exclude-regexp
))
631 (setq rating
(string-match include-regexp file
))
632 (not (string-match exclude-regexp file
))))
633 (or (not (file-exists-p file
))
634 (not (muse-project-private-p file
))))
635 (setq used-styles
(cons (cons rating style
) used-styles
)))))
636 (muse-sort-by-rating (nreverse used-styles
)))))
638 (defun muse-project-get-applicable-style (file styles
)
639 "Choose a style from the STYLES that FILE can publish to.
640 The user is prompted if several styles are found."
641 (muse-publish-get-style
642 (mapcar (lambda (style)
643 (cons (muse-get-keyword :base style
) style
))
644 (muse-project-applicable-styles file styles
))))
646 (defun muse-project-resolve-link (page local-style remote-styles
)
647 "Return a published relative link from the output path of one file
650 The best match for PAGE is determined by comparing the link
651 suffix of the given local style and that of the remote styles.
653 The remote styles are usually populated by
654 `muse-project-applicable-styles'."
655 (let ((link-suffix (or (muse-style-element :link-suffix local-style
)
656 (muse-style-element :suffix local-style
)))
658 (if (not (stringp link-suffix
))
659 (setq remote-style
(car remote-styles
))
660 (setq remote-style
(muse-project-choose-style
662 #'muse-project-choose-style-by-link-suffix
664 (if (null remote-style
)
666 (muse-publish-link-file
667 (let ((prefix (muse-style-element :base-url remote-style
)))
670 (file-relative-name (expand-file-name
671 (file-name-nondirectory page
)
672 (muse-style-element :path remote-style
))
674 (muse-style-element :path local-style
)))))
677 (defun muse-project-current-output-style (&optional file project
)
678 (or muse-current-output-style
680 (unless file
(setq file
(muse-current-file)))
681 (unless project
(setq project
(muse-project-of-file file
)))
682 (car (muse-project-applicable-styles file
(cddr project
))))))
684 (defun muse-project-link-page (page)
685 (let ((project (muse-project-of-file)))
686 (muse-project-resolve-link page
687 (muse-project-current-output-style)
688 (muse-project-applicable-styles
689 (muse-project-page-file page project
)
692 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
693 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
695 (dolist (style styles
)
696 (let ((output-dir (muse-style-element :path style
))
697 (muse-current-output-style style
))
698 ;; ensure the publishing location is available
699 (unless (file-exists-p output-dir
)
700 (message "Creating publishing directory %s" output-dir
)
701 (make-directory output-dir t
))
702 ;; publish the member file!
703 (if (muse-publish-file file style output-dir force
)
704 (setq published t
))))
708 (defun muse-project-publish-this-file (&optional force
)
709 "Publish the currently-visited file according to `muse-project-alist',
710 prompting if more than one style applies.
712 If FORCE is given, publish the file even if it is up-to-date."
713 (interactive (list current-prefix-arg
))
714 (let* ((style (muse-project-get-applicable-style
715 buffer-file-name
(cddr muse-current-project
)))
716 (output-dir (muse-style-element :path style
)))
717 (unless (muse-publish-file buffer-file-name style output-dir force
)
718 (message (concat "The published version is up-to-date; use"
719 " C-u C-c C-t to force an update.")))))
721 (defun muse-project-save-buffers (&optional project
)
722 (setq project
(muse-project project
))
726 (and (buffer-modified-p buffer
)
727 (not (buffer-base-buffer buffer
))
728 (or (buffer-file-name buffer
)
731 (and buffer-offer-save
732 (> (buffer-size) 0))))
733 (with-current-buffer buffer
734 (let ((proj (muse-project-of-file)))
735 (and proj
(string= (car proj
)
737 (if (buffer-file-name buffer
)
738 (format "Save file %s? "
739 (buffer-file-name buffer
))
740 (format "Save buffer %s? "
741 (buffer-name buffer
))))))
747 '("buffer" "buffers" "save")
748 (if (boundp 'save-some-buffers-action-alist
)
749 save-some-buffers-action-alist
)))
752 (defun muse-project-publish (project &optional force
)
753 "Publish the pages of PROJECT that need publishing."
754 (interactive (list (muse-read-project "Publish project: " nil t
)
756 (setq project
(muse-project project
))
757 (let ((styles (cddr project
))
758 (muse-current-project project
)
760 ;; determine the style from the project, or else ask
762 (setq styles
(list (muse-publish-get-style))))
764 (error "Cannot find a project to publish"))
765 ;; prompt to save any buffers related to this project
766 (muse-project-save-buffers project
)
767 ;; run hook before publishing begins
768 (run-hook-with-args 'muse-before-project-publish-hook project
)
769 ;; publish all files in the project, for each style; the actual
770 ;; publishing will only happen if the files are newer than the
771 ;; last published output, or if the file is listed in
772 ;; :force-publish. Files in :force-publish will not trigger the
773 ;; "All pages need to be published" message.
774 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
)))
775 (file-alist (muse-project-file-alist project
)))
776 (dolist (pair file-alist
)
777 (when (muse-project-publish-file (cdr pair
) styles force
)
778 (setq forced-files
(delete (car pair
) forced-files
))
780 (dolist (file forced-files
)
781 (muse-project-publish-file (cdr (assoc file file-alist
)) styles t
)))
782 ;; run hook after publishing ends
783 (run-hook-with-args 'muse-after-project-publish-hook project
)
784 ;; notify the user that everything is now done
786 (message "All pages in %s have been published." (car project
))
787 (message "No pages in %s need publishing at this time."
790 (defun muse-project-batch-publish ()
791 "Publish Muse files in batch mode."
792 (let ((muse-batch-publishing-p t
)
794 (if (string= "--force" (or (car command-line-args-left
) ""))
796 command-line-args-left
(cdr command-line-args-left
)))
797 (if command-line-args-left
798 (dolist (project command-line-args-left
)
799 (message "Publishing project %s ..." project
)
800 (muse-project-publish project force
))
801 (message "No projects specified."))))
804 (put 'make-local-hook
'byte-compile nil
))
806 (defun muse-project-set-variables ()
807 "Load project-specific variables."
808 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
811 (setq sym
(car vars
))
812 (setq custom-set
(or (get sym
'custom-set
) 'set
))
813 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
814 (make-local-hook sym
)
815 (make-local-variable sym
)))
816 (funcall custom-set var
(car (cdr vars
)))
817 (setq vars
(cdr (cdr vars
))))))
819 (defun muse-project-delete-output-files (project)
821 (list (muse-read-project "Remove all output files for project: " nil t
)))
822 (setq project
(muse-project project
))
823 (let ((file-alist (muse-project-file-alist project
))
824 (styles (cddr project
))
826 (dolist (entry file-alist
)
827 (dolist (style styles
)
829 (and (setq path
(muse-style-element :path style
))
831 (concat (muse-style-element :prefix style
)
833 (or (muse-style-element :osuffix style
)
834 (muse-style-element :suffix style
)))
837 (muse-delete-file-if-exists output-file
))))))
839 ;;; muse-project.el ends here