Fix issue with link suffix in links to other files
[muse-el.git] / lisp / muse-project.el
blob70a2c5d909735eae979eae160d2d3954b96df1ae
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.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;;; Code:
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse)
35 (require 'muse-publish)
36 (require 'cus-edit)
38 (defgroup muse-project nil
39 "Options controlling the behavior of Muse project handling."
40 :group 'muse)
42 (defcustom muse-before-project-publish-hook nil
43 "A hook run before a project is published.
44 Each function is passed the project object, a cons with the format
45 (PROJNAME . SETTINGS)"
46 :type 'hook
47 :group 'muse-project)
49 (defcustom muse-after-project-publish-hook nil
50 "A hook run after a project is published.
51 Each function is passed the project object, a cons with the format
52 (PROJNAME . SETTINGS)"
53 :type 'hook
54 :group 'muse-project)
56 (defvar muse-project-alist-using-customize nil
57 "Used internally by Muse to indicate whether `muse-project-alist'
58 has been modified via the customize interface.")
59 (make-variable-buffer-local 'muse-project-alist-using-customize)
61 (defmacro with-muse-project (project &rest body)
62 `(progn
63 (unless (muse-project ,project)
64 (error "Can't find project %s" ,project))
65 (with-temp-buffer
66 (muse-mode)
67 (setq muse-current-project (muse-project ,project))
68 (muse-project-set-variables)
69 ,@body)))
71 (put 'with-muse-project 'lisp-indent-function 0)
72 (put 'with-muse-project 'edebug-form-spec '(sexp body))
74 (defun muse-project-alist-get (sym)
75 "Turn `muse-project-alist' into something we can customize easily."
76 (when (boundp sym)
77 (setq muse-project-alist-using-customize t)
78 (let* ((val (copy-alist (symbol-value sym)))
79 (head val))
80 (while val
81 (let ((head (car (cdar val)))
82 res)
83 ;; Turn settings of first part into cons cells, symbol->string
84 (while head
85 (cond ((stringp (car head))
86 (add-to-list 'res (car head) t)
87 (setq head (cdr head)))
88 ((symbolp (car head))
89 (add-to-list 'res (list (symbol-name (car head))
90 (cadr head)) t)
91 (setq head (cddr head)))
93 (setq head (cdr head)))))
94 (setcdr (car val) (cons res (cdr (cdar val)))))
95 (let ((styles (cdar val)))
96 ;; Symbol->string in every style
97 (while (cdr styles)
98 (let ((head (cadr styles))
99 res)
100 (while (consp head)
101 (setq res (plist-put res (symbol-name (car head))
102 (cadr head)))
103 (setq head (cddr head)))
104 (setcdr styles (cons res (cddr styles))))
105 (setq styles (cdr styles))))
106 (setq val (cdr val)))
107 head)))
109 (defun muse-project-alist-set (sym val)
110 "Turn customized version of `muse-project-alist' into something
111 Muse can make use of."
112 (set sym val)
113 (when muse-project-alist-using-customize
114 ;; Make sure the unescaped version is written to .emacs
115 (put sym 'saved-value (list (custom-quote val)))
116 ;; Perform unescaping
117 (while val
118 (let ((head (car (cdar val)))
119 res)
120 ;; Turn cons cells into flat list, string->symbol
121 (while head
122 (cond ((stringp (car head))
123 (add-to-list 'res (car head) t))
124 ((consp (car head))
125 (add-to-list 'res (intern (caar head)) t)
126 (add-to-list 'res (car (cdar head)) t)))
127 (setq head (cdr head)))
128 (setcdr (car val) (cons res (cdr (cdar val)))))
129 (let ((styles (cdar val)))
130 ;; String->symbol in every style
131 (while (cdr styles)
132 (let ((head (cadr styles))
133 res)
134 (while (consp head)
135 (setq res (plist-put res (intern (car head))
136 (cadr head)))
137 (setq head (cddr head)))
138 (setcdr styles (cons res (cddr styles))))
139 (setq styles (cdr styles))))
140 (setq val (cdr val)))))
142 (define-widget 'muse-project 'default
143 "A widget that defines a Muse project."
144 :format "\n%v"
145 :value-create 'muse-widget-type-value-create
146 :value-get 'muse-widget-child-value-get
147 :value-delete 'ignore
148 :match 'muse-widget-type-match
149 :type '(cons :format " %v"
150 (repeat :tag "Settings" :format "%{%t%}:\n%v%i\n\n"
151 (choice
152 (string :tag "Directory")
153 (list :tag "Book function"
154 (const :tag ":book-funcall" ":book-funcall")
155 (choice (function)
156 (sexp :tag "Unknown")))
157 (list :tag "Book part"
158 (const :tag ":book-part" ":book-part")
159 (string :tag "Name"))
160 (list :tag "Book style"
161 (const :tag ":book-style" ":book-style")
162 (string :tag "Style"))
163 (list :tag "Default file"
164 (const :tag ":default" ":default")
165 (string :tag "File"))
166 (list :tag "End of book"
167 (const :tag ":book-end" ":book-end")
168 (const t))
169 (list :tag "Force publishing"
170 (const :tag ":force-publish" ":force-publish")
171 (repeat (string :tag "File")))
172 (list :tag "Major mode"
173 (const :tag ":major-mode" ":major-mode")
174 (choice (function :tag "Mode")
175 (sexp :tag "Unknown")))
176 (list :tag "New chapter"
177 (const :tag ":book-chapter" ":book-chapter")
178 (string :tag "Name"))
179 (list :tag "No chapters"
180 (const :tag ":nochapters" ":nochapters")
181 (const t))
182 (list :tag "Set variables"
183 (const :tag ":set" ":set")
184 (repeat (list :inline t
185 (symbol :tag "Variable")
186 (sexp :tag "Setting"))))
187 (list :tag "Visit links using"
188 (const :tag ":visit-link" ":visit-link")
189 (choice (function)
190 (sexp :tag "Unknown")))))
191 (repeat :tag "Styles" :format "%{%t%}:\n%v%i\n\n"
192 (set :tag "Style"
193 (list :inline t
194 :tag "Publishing style"
195 (const :tag ":base" ":base")
196 (string :tag "Style"))
197 (list :inline t
198 :tag "Base URL"
199 (const :tag ":base-url" ":base-url")
200 (string :tag "URL"))
201 (list :inline t
202 :tag "Exclude matching"
203 (const :tag ":exclude" ":exclude")
204 (regexp))
205 (list :inline t
206 :tag "Include matching"
207 (const :tag ":include" ":include")
208 (regexp))
209 (list :inline t
210 :tag "Timestamps file"
211 (const :tag ":timestamps" ":timestamps")
212 (file))
213 (list :inline t
214 :tag "Path"
215 (const :tag ":path" ":path")
216 (string :tag "Path"))))))
218 (defcustom muse-project-alist nil
219 "An alist of Muse projects.
220 A project defines a fileset, and a list of custom attributes for use
221 when publishing files in that project."
222 :type '(choice (const :tag "No projects defined." nil)
223 (repeat (cons :format "%{%t%}:\n\n%v"
224 :tag "Project" :indent 4
225 (string :tag "Project name")
226 muse-project))
227 (sexp :tag "Cannot parse expression"))
228 :get 'muse-project-alist-get
229 :set 'muse-project-alist-set
230 :group 'muse-project)
232 ;; Make it easier to specify a muse-project-alist entry
234 (defcustom muse-project-ignore-regexp
235 (concat "\\`\\(#.*#\\|.*,v\\|.*~\\|\\.\\.?\\|\\.#.*\\|,.*\\)\\'\\|"
236 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\|\\.svn\\|"
237 "_darcs\\)\\(/\\|\\'\\)")
238 "A regexp matching files to be ignored in Muse directories.
240 You should set case-fold-search to nil before using this regexp
241 in code."
242 :type 'regexp
243 :group 'muse-regexp)
245 (defun muse-project-recurse-directory (base)
246 "Recusively retrieve all of the directories underneath BASE.
247 A list of these directories is returned.
249 Directories starting with \".\" will be ignored, as well as those
250 which match `muse-project-ignore-regexp'."
251 (let ((case-fold-search nil)
252 list dir)
253 (when (and (file-directory-p base)
254 (not (string-match muse-project-ignore-regexp base)))
255 (dolist (file (directory-files base t "^[^.]"))
256 (when (and (file-directory-p file)
257 (not (string-match muse-project-ignore-regexp file)))
258 (setq dir (file-name-nondirectory file))
259 (push dir list)
260 (nconc list (mapcar #'(lambda (item)
261 (concat dir "/" item))
262 (muse-project-recurse-directory file)))))
263 list)))
265 (defun muse-project-alist-styles (entry-dir output-dir style)
266 "Return a list of styles to use in a `muse-project-alist' entry.
267 ENTRY-DIR is the top-level directory of the project.
268 OUTPUT-DIR is where Muse files are published, keeping directory structure.
269 STYLE is the publishing style to use.
271 For an example of the use of this function, see
272 `examples/mwolson/muse-init.el' from the Muse distribution."
273 (cons `(:base ,style :path ,(expand-file-name output-dir)
274 :include ,(concat "/" (file-name-nondirectory entry-dir)
275 "/[^/]+$"))
276 (mapcar (lambda (dir)
277 `(:base ,style
278 :path ,(expand-file-name dir output-dir)
279 :include ,(concat "/" dir "/[^/]+$")))
280 (muse-project-recurse-directory entry-dir))))
282 (defun muse-project-alist-dirs (entry-dir)
283 "Return a list of directories to use in a `muse-project-alist' entry.
284 ENTRY-DIR is the top-level directory of the project.
286 For an example of the use of this function, see
287 `examples/mwolson/muse-init.el' from the Muse distribution."
288 (cons (expand-file-name entry-dir)
289 (mapcar (lambda (dir) (expand-file-name dir entry-dir))
290 (muse-project-recurse-directory entry-dir))))
292 ;; Constructing the file-alist
294 (defvar muse-project-file-alist nil
295 "This variable is automagically constructed as needed.")
297 (defvar muse-project-file-alist-hook nil
298 "Functions that are to be exectuted immediately after updating
299 `muse-project-file-alist'.")
301 (defvar muse-current-project nil
302 "Project we are currently visiting.")
303 (make-variable-buffer-local 'muse-current-project)
305 (defvar muse-current-output-style nil
306 "The output style that we are currently using for publishing files.")
308 (defsubst muse-project (&optional project)
309 "Resolve the given PROJECT into a full Muse project, if it is a string."
310 (if (null project)
311 (or muse-current-project
312 (muse-project-of-file))
313 (if (stringp project)
314 (assoc project muse-project-alist)
315 (muse-assert (consp project))
316 project)))
318 (defun muse-project-page-file (page project &optional no-check-p)
319 "Return a filename if PAGE exists within the given Muse PROJECT."
320 (setq project (muse-project project))
321 (let ((dir (file-name-directory page)))
322 (when dir (setq page (file-name-nondirectory page)))
323 (let ((files (muse-collect-alist
324 (muse-project-file-alist project no-check-p)
325 page))
326 (matches nil))
327 (if dir
328 (save-match-data
329 (dolist (file files)
330 (let ((pos (string-match (concat (regexp-quote dir) "\\'")
331 (file-name-directory (cdr file)))))
332 (when pos
333 (setq matches (cons (cons pos (cdr file))
334 matches)))))
335 (car (muse-sort-by-rating matches)))
336 (dolist (file files)
337 (setq matches (cons (cons (length (cdr file)) (cdr file))
338 matches)))
339 (car (muse-sort-by-rating matches '<))))))
341 (defun muse-project-private-p (file)
342 "Return non-nil if NAME is a private page with PROJECT."
343 (unless muse-under-windows-p
344 (setq file (file-truename file))
345 (if (file-attributes file) ; don't publish if no attributes exist
346 (or (when (eq ?- (aref (nth 8 (file-attributes
347 (file-name-directory file))) 7))
348 (message (concat
349 "The " (file-name-directory file)
350 " directory must be readable by others"
351 " in order for its contents to be published.")))
352 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
353 t)))
355 (defun muse-project-file-entries (path)
356 (let* ((names (list t))
357 (lnames names)
358 (case-fold-search nil))
359 (cond
360 ((file-directory-p path)
361 (dolist (file (directory-files
362 path t (when (and muse-file-extension
363 (not (string= muse-file-extension "")))
364 (concat "." muse-file-extension "\\'"))))
365 (unless (or (string-match muse-project-ignore-regexp file)
366 (string-match muse-project-ignore-regexp
367 (file-name-nondirectory file))
368 (file-directory-p file))
369 (setcdr lnames
370 (cons (cons (muse-page-name file) file) nil))
371 (setq lnames (cdr lnames)))))
372 ((file-readable-p path)
373 (setcdr lnames
374 (cons (cons (muse-page-name path) path) nil))
375 (setq lnames (cdr lnames)))
376 (t ; regexp
377 (muse-assert (file-name-directory path))
378 (dolist (file (directory-files
379 (file-name-directory path) t
380 (file-name-nondirectory path)))
381 (unless (or (string-match muse-project-ignore-regexp file)
382 (string-match muse-project-ignore-regexp
383 (file-name-nondirectory file)))
384 (setcdr lnames
385 (cons (cons (muse-page-name file) file) nil))
386 (setq lnames (cdr lnames))))))
387 (cdr names)))
389 (defvar muse-updating-file-alist-p nil
390 "Make sure that recursive calls to `muse-project-file-alist' are bounded.")
392 (defun muse-project-file-alist (&optional project no-check-p)
393 "Return member filenames for the given Muse PROJECT.
394 On UNIX, this list is only updated if one of the directories'
395 contents have changed. On Windows, it is always reread from
396 disk."
397 (setq project (muse-project project))
398 (let* ((file-alist (assoc (car project) muse-project-file-alist))
399 (last-mod (cdr (cdr file-alist))))
400 ;; Determine the last modified of any directory mentioned in the
401 ;; project's pattern list
402 (unless (or muse-under-windows-p no-check-p)
403 (let ((pats (cadr project)))
404 (while pats
405 (if (symbolp (car pats))
406 (setq pats (cddr pats))
407 (let* ((fnd (file-name-directory (car pats)))
408 (dir (cond ((file-directory-p (car pats))
409 (car pats))
410 ((and (not (file-readable-p (car pats)))
412 (file-directory-p fnd))
413 fnd))))
414 (when dir
415 (let ((mod-time (nth 5 (file-attributes dir))))
416 (when (or (null last-mod)
417 (and mod-time
418 (muse-time-less-p last-mod mod-time)))
419 (setq last-mod mod-time)))))
420 (setq pats (cdr pats))))))
421 ;; Either return the currently known list, or read it again from
422 ;; disk
423 (if (or (and no-check-p (cadr file-alist))
424 muse-updating-file-alist-p
425 (not (or muse-under-windows-p
426 (null (cddr file-alist))
427 (null last-mod)
428 (muse-time-less-p (cddr file-alist) last-mod))))
429 (cadr file-alist)
430 (if file-alist
431 (setcdr (cdr file-alist) last-mod)
432 (setq file-alist (cons (car project) (cons nil last-mod))
433 muse-project-file-alist
434 (cons file-alist muse-project-file-alist)))
435 ;; Read in all of the file entries
436 (let ((muse-updating-file-alist-p t))
437 (prog1
438 (save-match-data
439 (setcar
440 (cdr file-alist)
441 (let* ((names (list t))
442 (pats (cadr project)))
443 (while pats
444 (if (symbolp (car pats))
445 (setq pats (cddr pats))
446 (nconc names (muse-project-file-entries (car pats)))
447 (setq pats (cdr pats))))
448 (cdr names))))
449 (run-hooks 'muse-project-file-alist-hook))))))
451 (defun muse-project-of-file (&optional pathname)
452 "Determine which project the given PATHNAME relates to.
453 If PATHNAME is nil, the current buffer's filename is used."
454 (if (and (null pathname) muse-current-project)
455 muse-current-project
456 (unless pathname (setq pathname (muse-current-file)))
457 (save-match-data
458 (when (and (stringp pathname)
459 (not (string= pathname ""))
460 (not (let ((case-fold-search nil))
461 (or (string-match muse-project-ignore-regexp
462 pathname)
463 (string-match muse-project-ignore-regexp
464 (file-name-nondirectory
465 pathname))))))
466 (let* ((file (file-truename pathname))
467 (dir (file-name-directory file))
468 (project-entry muse-project-alist)
469 found)
470 (while (and project-entry (not found))
471 (let ((pats (car (cdar project-entry))))
472 (while (and pats (not found))
473 (if (symbolp (car pats))
474 (setq pats (cddr pats))
475 (let ((truename (file-truename (car pats))))
476 (if (or (string= truename file)
477 (string= truename dir)
478 (string-match (regexp-quote truename) file))
479 (setq found (car project-entry))))
480 (setq pats (cdr pats))))
481 (setq project-entry (cdr project-entry))))
482 found)))))
484 (defun muse-project-after-save-hook ()
485 "Update Muse's file-alist if we are saving a Muse file."
486 (let ((project (muse-project-of-file)))
487 (when project
488 (muse-project-file-alist project))))
490 (add-hook 'after-save-hook 'muse-project-after-save-hook)
492 (defun muse-read-project (prompt &optional no-check-p no-assume)
493 "Read a project name from the minibuffer, if it can't be figured
494 out."
495 (if (null muse-project-alist)
496 (error "There are no Muse projects defined; see `muse-project-alist'.")
497 (or (unless no-check-p
498 (muse-project-of-file))
499 (if (and (not no-assume)
500 (= 1 (length muse-project-alist)))
501 (car muse-project-alist)
502 (assoc (completing-read prompt muse-project-alist)
503 muse-project-alist)))))
505 (defvar muse-project-page-history nil)
507 (defun muse-read-project-file (project prompt &optional default)
508 (let ((name (completing-read prompt (muse-project-file-alist project)
509 nil nil nil 'muse-project-page-history
510 default)))
511 (cons name (muse-project-page-file name project))))
513 ;;;###autoload
514 (defun muse-project-find-file (name project &optional command directory)
515 "Open the Muse page given by NAME in PROJECT.
516 If COMMAND is non-nil, it is the function used to visit the file.
517 If DIRECTORY is non-nil, it is the directory in which the page
518 will be created if it does not already exist. Otherwise, the
519 first directory within the project's fileset is used."
520 (interactive
521 (let* ((project (muse-read-project "Find in project: "
522 current-prefix-arg))
523 (default (muse-get-keyword :default (cadr project)))
524 (entry (muse-read-project-file
525 project (if default
526 (format "Find page: (default: %s) "
527 default)
528 "Find page: ")
529 default)))
530 (list entry project)))
531 (setq project (muse-project project))
532 (let ((project-name (car project)))
533 (unless (interactive-p)
534 (setq project (muse-project project)
535 name (cons name (muse-project-page-file name project))))
536 ;; If we're given a relative or absolute filename, open it as-is
537 (if (and (car name)
538 (save-match-data
539 (or (string-match muse-file-regexp (car name))
540 (string-match muse-image-regexp (car name)))))
541 (setcdr name (car name))
542 ;; At this point, name is (PAGE . FILE).
543 (unless (cdr name)
544 (let ((pats (cadr project)))
545 (while (and pats (null directory))
546 (if (symbolp (car pats))
547 (setq pats (cddr pats))
548 (if (file-directory-p (car pats))
549 (setq directory (car pats) pats nil)
550 (setq pats (cdr pats))))))
551 (when directory
552 (let ((filename (expand-file-name
553 (if (and muse-file-extension
554 (not (string= muse-file-extension "")))
555 (concat (car name) "." muse-file-extension)
556 (car name))
557 directory)))
558 (unless (file-exists-p directory)
559 (make-directory directory t))
560 (setcdr name filename)))))
561 ;; Open the file
562 (if (cdr name)
563 (funcall (or command 'find-file) (cdr name))
564 (error "There is no page %s in project %s."
565 (car name) project-name))))
567 (defun muse-project-choose-style (closure test styles)
568 "Run TEST on STYLES and return first style where TEST yields non-nil.
569 TEST should take two arguments. The first is CLOSURE, which is
570 passed verbatim. The second if the current style to consider.
572 If no style passes TEST, return the first style."
573 (or (catch 'winner
574 (dolist (style styles)
575 (when (funcall test closure style)
576 (throw 'winner style))))
577 (car styles)))
579 (defun muse-project-choose-style-by-link-suffix (given-suffix style)
580 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
581 return non-nil."
582 (let ((link-suffix (or (muse-style-element :link-suffix style)
583 (muse-style-element :suffix style))))
584 (and (stringp link-suffix)
585 (string= given-suffix link-suffix))))
587 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
588 "Given STYLES, return a list of the ones that are considered for FILE.
589 The name of a project may be used for STYLES."
590 (when (stringp styles)
591 (setq styles (cddr (muse-project styles))))
592 (when (and file styles)
593 (let ((used-styles nil))
594 (dolist (style styles)
595 (let ((include-regexp (muse-style-element :include style))
596 (exclude-regexp (muse-style-element :exclude style))
597 (rating nil))
598 (when (and (or ignore-regexp
599 (and (null include-regexp)
600 (null exclude-regexp))
601 (if include-regexp
602 (setq rating (string-match include-regexp file))
603 (not (string-match exclude-regexp file))))
604 (or (not (file-exists-p file))
605 (not (muse-project-private-p file))))
606 (setq used-styles (cons (cons rating style) used-styles)))))
607 (muse-sort-by-rating (nreverse used-styles)))))
609 (defun muse-project-get-applicable-style (file styles)
610 "Choose a style from the STYLES that FILE can publish to.
611 The user is prompted if several styles are found."
612 (muse-publish-get-style
613 (mapcar (lambda (style)
614 (cons (muse-get-keyword :base style) style))
615 (muse-project-applicable-styles file styles))))
617 (defun muse-project-resolve-link (page local-style remote-styles)
618 "Return a published relative link from the output path of one file
619 to another file.
621 The best match for PAGE is determined by comparing the link
622 suffix of the given local style and that of the remote styles.
624 The remote styles are usually populated by
625 `muse-project-applicable-styles'."
626 (let ((link-suffix (or (muse-style-element :link-suffix local-style)
627 (muse-style-element :suffix local-style)))
628 remote-style)
629 (if (not (stringp link-suffix))
630 (setq remote-style (car remote-styles))
631 (setq remote-style (muse-project-choose-style
632 link-suffix
633 #'muse-project-choose-style-by-link-suffix
634 remote-styles)))
635 (muse-publish-link-file
636 (if (null remote-style)
637 page
638 (let ((prefix (muse-style-element :base-url remote-style)))
639 (if prefix
640 (concat prefix page)
641 (file-relative-name (expand-file-name
642 (file-name-nondirectory page)
643 (muse-style-element :path remote-style))
644 (expand-file-name
645 (muse-style-element :path local-style))))))
646 nil remote-style)))
648 (defun muse-project-current-output-style (&optional file project)
649 (or muse-current-output-style
650 (progn
651 (unless file (setq file (muse-current-file)))
652 (unless project (setq project (muse-project-of-file file)))
653 (car (muse-project-applicable-styles file (cddr project))))))
655 (defun muse-project-link-page (page)
656 (let ((project (muse-project-of-file)))
657 (muse-project-resolve-link page
658 (muse-project-current-output-style)
659 (muse-project-applicable-styles
660 (muse-project-page-file page project)
661 (cddr project)))))
663 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
664 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
665 (let (published)
666 (dolist (style styles)
667 (let ((output-dir (muse-style-element :path style))
668 (muse-current-output-style style))
669 ;; ensure the publishing location is available
670 (unless (file-exists-p output-dir)
671 (message "Creating publishing directory %s" output-dir)
672 (make-directory output-dir t))
673 ;; publish the member file!
674 (if (muse-publish-file file style output-dir force)
675 (setq published t))))
676 published))
678 ;;;###autoload
679 (defun muse-project-publish-this-file (&optional force)
680 "Publish the currently-visited file according to `muse-project-alist',
681 prompting if more than one style applies.
683 If FORCE is given, publish the file even if it is up-to-date."
684 (interactive (list current-prefix-arg))
685 (let* ((style (muse-project-get-applicable-style
686 buffer-file-name (cddr muse-current-project)))
687 (output-dir (muse-style-element :path style)))
688 (unless (muse-publish-file buffer-file-name style output-dir force)
689 (message (concat "The published version is up-to-date; use"
690 " C-u C-c C-t to force an update.")))))
692 (defun muse-project-save-buffers (&optional project)
693 (setq project (muse-project project))
694 (map-y-or-n-p
695 (function
696 (lambda (buffer)
697 (and (buffer-modified-p buffer)
698 (not (buffer-base-buffer buffer))
699 (or (buffer-file-name buffer)
700 (progn
701 (set-buffer buffer)
702 (and buffer-offer-save
703 (> (buffer-size) 0))))
704 (with-current-buffer buffer
705 (let ((proj (muse-project-of-file)))
706 (and proj (string= (car proj)
707 (car project)))))
708 (if (buffer-file-name buffer)
709 (format "Save file %s? "
710 (buffer-file-name buffer))
711 (format "Save buffer %s? "
712 (buffer-name buffer))))))
713 (function
714 (lambda (buffer)
715 (set-buffer buffer)
716 (save-buffer)))
717 (buffer-list)
718 '("buffer" "buffers" "save")
719 (if (boundp 'save-some-buffers-action-alist)
720 save-some-buffers-action-alist)))
722 ;;;###autoload
723 (defun muse-project-publish (project &optional force)
724 "Publish the pages of PROJECT that need publishing."
725 (interactive (list (muse-read-project "Publish project: " nil t)
726 current-prefix-arg))
727 (setq project (muse-project project))
728 (let ((styles (cddr project))
729 (muse-current-project project)
730 published)
731 ;; determine the style from the project, or else ask
732 (unless styles
733 (setq styles (list (muse-publish-get-style))))
734 (unless project
735 (error "Cannot find a project to publish"))
736 ;; prompt to save any buffers related to this project
737 (muse-project-save-buffers project)
738 ;; run hook before publishing begins
739 (run-hook-with-args 'muse-before-project-publish-hook project)
740 ;; publish all files in the project, for each style; the actual
741 ;; publishing will only happen if the files are newer than the
742 ;; last published output, or if the file is listed in
743 ;; :force-publish. Files in :force-publish will not trigger the
744 ;; "All pages need to be published" message.
745 (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
746 (file-alist (muse-project-file-alist project)))
747 (dolist (pair file-alist)
748 (when (muse-project-publish-file (cdr pair) styles force)
749 (setq forced-files (delete (car pair) forced-files))
750 (setq published t)))
751 (dolist (file forced-files)
752 (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
753 ;; run hook after publishing ends
754 (run-hook-with-args 'muse-after-project-publish-hook project)
755 ;; notify the user that everything is now done
756 (if published
757 (message "All pages in %s have been published." (car project))
758 (message "No pages in %s need publishing at this time."
759 (car project)))))
761 (defun muse-project-batch-publish ()
762 "Publish Muse files in batch mode."
763 (let ((muse-batch-publishing-p t)
764 force)
765 (if (string= "--force" (or (car command-line-args-left) ""))
766 (setq force t
767 command-line-args-left (cdr command-line-args-left)))
768 (if command-line-args-left
769 (dolist (project command-line-args-left)
770 (message "Publishing project %s ..." project)
771 (muse-project-publish project force))
772 (message "No projects specified."))))
774 (eval-when-compile
775 (put 'make-local-hook 'byte-compile nil))
777 (defun muse-project-set-variables ()
778 "Load project-specific variables."
779 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
780 sym custom-set var)
781 (while vars
782 (setq sym (car vars))
783 (setq custom-set (or (get sym 'custom-set) 'set))
784 (setq var (if (eq (get sym 'custom-type) 'hook)
785 (make-local-hook sym)
786 (make-local-variable sym)))
787 (funcall custom-set var (car (cdr vars)))
788 (setq vars (cdr (cdr vars))))))
790 (defun muse-project-delete-output-files (project)
791 (interactive
792 (list (muse-read-project "Remove all output files for project: " nil t)))
793 (setq project (muse-project project))
794 (let ((file-alist (muse-project-file-alist project))
795 (styles (cddr project))
796 output-file path)
797 (dolist (entry file-alist)
798 (dolist (style styles)
799 (setq output-file
800 (and (setq path (muse-style-element :path style))
801 (expand-file-name
802 (concat (muse-style-element :prefix style)
803 (car entry)
804 (or (muse-style-element :osuffix style)
805 (muse-style-element :suffix style)))
806 path)))
807 (if output-file
808 (muse-delete-file-if-exists output-file))))))
810 (provide 'muse-project)
812 ;;; muse-project.el ends here