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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (require 'muse-publish
)
38 (defgroup muse-project nil
39 "Options controlling the behavior of Muse project handling."
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)"
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)"
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
)
63 (unless (muse-project ,project
)
64 (error "Can't find project %s" ,project
))
67 (setq muse-current-project
(muse-project ,project
))
68 (muse-project-set-variables)
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."
77 (setq muse-project-alist-using-customize t
)
78 (let* ((val (copy-alist (symbol-value sym
)))
81 (let ((head (car (cdar val
)))
83 ;; Turn settings of first part into cons cells, symbol->string
85 (cond ((stringp (car head
))
86 (add-to-list 'res
(car head
) t
)
87 (setq head
(cdr head
)))
89 (add-to-list 'res
(list (symbol-name (car head
))
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
98 (let ((head (cadr styles
))
101 (setq res
(plist-put res
(symbol-name (car head
))
103 (setq head
(cddr head
)))
104 (setcdr styles
(cons res
(cddr styles
))))
105 (setq styles
(cdr styles
))))
106 (setq val
(cdr val
)))
109 (defun muse-project-alist-set (sym val
)
110 "Turn customized version of `muse-project-alist' into something
111 Muse can make use of."
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
118 (let ((head (car (cdar val
)))
120 ;; Turn cons cells into flat list, string->symbol
122 (cond ((stringp (car head
))
123 (add-to-list 'res
(car head
) t
))
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
132 (let ((head (cadr styles
))
135 (setq res
(plist-put res
(intern (car 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."
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"
152 (string :tag
"Directory")
153 (list :tag
"Book function"
154 (const :tag
":book-funcall" ":book-funcall")
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")
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")
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")
190 (sexp :tag
"Unknown")))))
191 (repeat :tag
"Styles" :format
"%{%t%}:\n%v%i\n\n"
194 :tag
"Publishing style"
195 (const :tag
":base" ":base")
196 (string :tag
"Style"))
199 (const :tag
":base-url" ":base-url")
202 :tag
"Exclude matching"
203 (const :tag
":exclude" ":exclude")
206 :tag
"Include matching"
207 (const :tag
":include" ":include")
210 :tag
"Timestamps file"
211 (const :tag
":timestamps" ":timestamps")
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")
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}\\|,.*\\)\\(/\\|\\'\\)")
237 "A regexp matching files to be ignored in Wiki directories.
239 You should set case-fold-search to nil before using this regexp
244 (defun muse-project-recurse-directory (base)
245 "Recusively retrieve all of the directories underneath BASE.
246 A list of these directories is returned.
247 Directories starting with \".\" will be ignored, as well as those
248 which match `muse-project-ignore-regexp'."
249 (let ((case-fold-search nil
)
251 (when (and (file-directory-p base
)
252 (not (string-match muse-project-ignore-regexp base
)))
253 (dolist (file (directory-files base t
"^[^.]"))
254 (when (and (file-directory-p file
)
255 (not (string-match muse-project-ignore-regexp file
)))
256 (setq dir
(file-name-nondirectory file
))
258 (nconc list
(mapcar #'(lambda (item)
259 (concat dir
"/" item
))
260 (muse-project-recurse-directory file
)))))
263 (defun muse-project-alist-styles (entry-dir output-dir style
)
264 "Return a list of styles to use in a `muse-project-alist' entry.
265 ENTRY-DIR is the top-level directory of the project.
266 OUTPUT-DIR is where Muse files are published, keeping directory structure.
267 STYLE is the publishing style to use.
269 For an example of the use of this function, see
270 `examples/mwolson/muse-init.el' from the Muse distribution."
271 (cons `(:base
,style
:path
,(expand-file-name output-dir
)
272 :include
,(concat "/" (file-name-nondirectory entry-dir
)
274 (mapcar (lambda (dir);
276 :path
,(expand-file-name dir output-dir
)
277 :include
,(concat "/" dir
"/[^/]+$")))
278 (muse-project-recurse-directory entry-dir
))))
280 (defun muse-project-alist-dirs (entry-dir)
281 "Return a list of directories to use in a `muse-project-alist' entry.
282 ENTRY-DIR is the top-level directory of the project.
284 For an example of the use of this function, see
285 `examples/mwolson/muse-init.el' from the Muse distribution."
286 (cons (expand-file-name entry-dir
)
287 (mapcar (lambda (dir) (expand-file-name dir entry-dir
))
288 (muse-project-recurse-directory entry-dir
))))
290 ;; Constructing the file-alist
292 (defvar muse-project-file-alist nil
293 "This variable is automagically constructed as needed.")
295 (defvar muse-project-file-alist-hook nil
296 "Functions that are to be exectuted immediately after updating
297 `muse-project-file-alist'.")
299 (defvar muse-current-project nil
300 "Project we are currently visiting.")
301 (make-variable-buffer-local 'muse-current-project
)
303 (defsubst muse-project
(&optional project
)
304 "Resolve the given PROJECT into a full Muse project, if it is a string."
306 (or muse-current-project
307 (muse-project-of-file))
308 (if (stringp project
)
309 (assoc project muse-project-alist
)
310 (muse-assert (consp project
))
313 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
314 "Return a filename if PAGE exists within the given Muse PROJECT."
315 (setq project
(muse-project project
))
316 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
318 (defun muse-project-private-p (file)
319 "Return non-nil if NAME is a private page with PROJECT."
320 (unless muse-under-windows-p
321 (setq file
(file-truename file
))
322 (if (file-attributes file
) ; don't publish if no attributes exist
323 (or (when (eq ?-
(aref (nth 8 (file-attributes
324 (file-name-directory file
))) 7))
326 "The " (file-name-directory file
)
327 " directory must be readable by others"
328 " in order for its contents to be published.")))
329 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
332 (defun muse-project-file-entries (path)
333 (let* ((names (list t
))
335 (case-fold-search nil
))
337 ((file-directory-p path
)
338 (dolist (file (directory-files
339 path t
(when (and muse-file-extension
340 (not (string= muse-file-extension
"")))
341 (concat "." muse-file-extension
"\\'"))))
342 (unless (or (string-match muse-project-ignore-regexp file
)
343 (file-directory-p file
))
345 (cons (cons (muse-page-name file
) file
) nil
))
346 (setq lnames
(cdr lnames
)))))
347 ((file-readable-p path
)
349 (cons (cons (muse-page-name path
) path
) nil
))
350 (setq lnames
(cdr lnames
)))
352 (muse-assert (file-name-directory path
))
353 (dolist (file (directory-files
354 (file-name-directory path
) t
355 (file-name-nondirectory path
)))
356 (unless (string-match muse-project-ignore-regexp file
)
358 (cons (cons (muse-page-name file
) file
) nil
))
359 (setq lnames
(cdr lnames
))))))
362 (defun muse-project-file-alist (&optional project no-check-p
)
363 "Return member filenames for the given Muse PROJECT.
364 On UNIX, this list is only updated if one of the directories'
365 contents have changed. On Windows, it is always reread from
367 (setq project
(muse-project project
))
368 (let* ((file-alist (assoc (car project
) muse-project-file-alist
))
369 (last-mod (cdr (cdr file-alist
))))
370 ;; Determine the last modified of any directory mentioned in the
371 ;; project's pattern list
372 (unless (or muse-under-windows-p no-check-p
)
373 (let ((pats (cadr project
)))
375 (if (symbolp (car pats
))
376 (setq pats
(cddr pats
))
377 (let* ((fnd (file-name-directory (car pats
)))
378 (dir (cond ((file-directory-p (car pats
))
380 ((and (not (file-readable-p (car pats
)))
382 (file-directory-p fnd
))
385 (let ((mod-time (nth 5 (file-attributes dir
))))
386 (when (or (null last-mod
)
388 (muse-time-less-p last-mod mod-time
)))
389 (setq last-mod mod-time
)))))
390 (setq pats
(cdr pats
))))))
391 ;; Either return the currently known list, or read it again from
393 (if (or (and no-check-p
(cadr file-alist
))
394 (not (or muse-under-windows-p
395 (null (cddr file-alist
))
397 (muse-time-less-p (cddr file-alist
) last-mod
))))
400 (setcdr (cdr file-alist
) last-mod
)
401 (setq file-alist
(cons (car project
) (cons nil last-mod
))
402 muse-project-file-alist
403 (cons file-alist muse-project-file-alist
)))
404 ;; Read in all of the file entries
409 (let* ((names (list t
))
410 (pats (cadr project
)))
412 (if (symbolp (car pats
))
413 (setq pats
(cddr pats
))
414 (nconc names
(muse-project-file-entries (car pats
)))
415 (setq pats
(cdr pats
))))
417 (run-hooks 'muse-project-file-alist-hook
)))))
419 (defun muse-project-of-file (&optional pathname
)
420 "Determine which project the given PATHNAME relates to.
421 If PATHNAME is nil, the current buffer's filename is used."
422 (if (and (null pathname
) muse-current-project
)
424 (unless pathname
(setq pathname
(muse-current-file)))
426 (when (and (stringp pathname
)
427 (not (string= pathname
""))
428 (not (let ((case-fold-search nil
))
429 (string-match muse-project-ignore-regexp pathname
))))
430 (let* ((file (file-truename pathname
))
431 (dir (file-name-directory file
))
432 (project-entry muse-project-alist
)
434 (while (and project-entry
(not found
))
435 (let ((pats (car (cdar project-entry
))))
436 (while (and pats
(not found
))
437 (if (symbolp (car pats
))
438 (setq pats
(cddr pats
))
439 (let ((truename (file-truename (car pats
))))
440 (if (or (string= truename file
)
441 (string= truename dir
)
442 (string-match (regexp-quote truename
) file
))
443 (setq found
(car project-entry
))))
444 (setq pats
(cdr pats
))))
445 (setq project-entry
(cdr project-entry
))))
448 (defun muse-project-after-save-hook ()
449 "Update Muse's file-alist if we are saving a Muse file."
450 (let ((project (muse-project-of-file)))
452 (muse-project-file-alist project
))))
454 (add-hook 'after-save-hook
'muse-project-after-save-hook
)
456 (defun muse-read-project (prompt &optional no-check-p no-assume
)
457 "Read a project name from the minibuffer, if it can't be figured
459 (if (null muse-project-alist
)
460 (error "There are no Muse projects defined; see `muse-project-alist'.")
461 (or (unless no-check-p
462 (muse-project-of-file))
463 (if (and (not no-assume
)
464 (= 1 (length muse-project-alist
)))
465 (car muse-project-alist
)
466 (assoc (completing-read prompt muse-project-alist
)
467 muse-project-alist
)))))
469 (defvar muse-project-page-history nil
)
471 (defun muse-read-project-file (project prompt
&optional default
)
472 (let ((name (completing-read prompt
(muse-project-file-alist project
)
473 nil nil nil
'muse-project-page-history
475 (cons name
(muse-project-page-file name project
))))
478 (defun muse-project-find-file (name project
&optional command directory
)
479 "Open the Muse page given by NAME in PROJECT.
480 If COMMAND is non-nil, it is the function used to visit the file.
481 If DIRECTORY is non-nil, it is the directory in which the page
482 will be created if it does not already exist. Otherwise, the
483 first directory within the project's fileset is used."
485 (let* ((project (muse-read-project "Find in project: "
487 (default (muse-get-keyword :default
(cadr project
)))
488 (entry (muse-read-project-file
490 (format "Find page: (default: %s) "
494 (list entry project
)))
495 (setq project
(muse-project project
))
496 (let ((project-name (car project
)))
497 (unless (interactive-p)
498 (setq project
(muse-project project
)
499 name
(cons name
(muse-project-page-file name project
))))
500 ;; If we're given a relative or absolute filename, open it as-is
502 (save-match-data (string-match muse-file-regexp
(car name
))))
503 (setcdr name
(car name
))
504 ;; At this point, name is (PAGE . FILE).
506 (let ((pats (cadr project
)))
507 (while (and pats
(null directory
))
508 (if (symbolp (car pats
))
509 (setq pats
(cddr pats
))
510 (if (file-directory-p (car pats
))
511 (setq directory
(car pats
) pats nil
)
512 (setq pats
(cdr pats
))))))
514 (let ((filename (expand-file-name
515 (if (and muse-file-extension
516 (not (string= muse-file-extension
"")))
517 (concat (car name
) "." muse-file-extension
)
520 (unless (file-exists-p directory
)
521 (make-directory directory t
))
522 (setcdr name filename
)))))
525 (funcall (or command
'find-file
) (cdr name
))
526 (error "There is no page %s in project %s."
527 (car name
) project-name
))))
529 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
530 "Given STYLES, return a list of the ones that are considered for FILE.
531 The name of a project may be used for STYLES."
532 (when (stringp styles
)
533 (setq styles
(cddr (muse-project styles
))))
534 (when (and file styles
)
536 (dolist (style styles
)
537 (let ((include-regexp (muse-style-element :include style
))
538 (exclude-regexp (muse-style-element :exclude style
)))
539 (when (and (or ignore-regexp
540 (and (null include-regexp
)
541 (null exclude-regexp
))
543 (string-match include-regexp file
)
544 (not (string-match exclude-regexp file
))))
545 (or (not (file-exists-p file
))
546 (not (muse-project-private-p file
))))
547 (setq used-styles
(cons style used-styles
)))))
548 (nreverse used-styles
))))
550 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
551 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
553 (dolist (style styles
)
554 (let ((output-dir (muse-style-element :path style
)))
555 ;; ensure the publishing location is available
556 (unless (file-exists-p output-dir
)
557 (message "Creating publishing directory %s" output-dir
)
558 (make-directory output-dir t
))
559 ;; publish the member file!
560 (if (muse-publish-file file style output-dir force
)
561 (setq published t
))))
564 (defun muse-project-save-buffers (&optional project
)
565 (setq project
(muse-project project
))
569 (and (buffer-modified-p buffer
)
570 (not (buffer-base-buffer buffer
))
571 (or (buffer-file-name buffer
)
574 (and buffer-offer-save
575 (> (buffer-size) 0))))
576 (with-current-buffer buffer
577 (let ((proj (muse-project-of-file)))
578 (and proj
(string= (car proj
)
580 (if (buffer-file-name buffer
)
581 (format "Save file %s? "
582 (buffer-file-name buffer
))
583 (format "Save buffer %s? "
584 (buffer-name buffer
))))))
590 '("buffer" "buffers" "save")
591 (if (boundp 'save-some-buffers-action-alist
)
592 save-some-buffers-action-alist
)))
595 (defun muse-project-publish (project &optional force
)
596 "Publish the pages of PROJECT that need publishing."
597 (interactive (list (muse-read-project "Publish project: " nil t
)
599 (setq project
(muse-project project
))
600 (let ((styles (cddr project
))
601 (muse-current-project project
)
603 ;; determine the style from the project, or else ask
605 (setq styles
(list (muse-publish-get-style))))
607 (error "Cannot find a project to publish"))
608 ;; prompt to save any buffers related to this project
609 (muse-project-save-buffers project
)
610 ;; run hook before publishing begins
611 (run-hook-with-args 'muse-before-project-publish-hook project
)
612 ;; publish all files in the project, for each style; the actual
613 ;; publishing will only happen if the files are newer than the
614 ;; last published output, or if the file is listed in
615 ;; :force-publish. Files in :force-publish will not trigger the
616 ;; "All pages need to be published" message.
617 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
)))
618 (file-alist (muse-project-file-alist project
)))
619 (dolist (pair file-alist
)
620 (when (muse-project-publish-file (cdr pair
) styles force
)
621 (setq forced-files
(delete (car pair
) forced-files
))
623 (dolist (file forced-files
)
624 (muse-project-publish-file (cdr (assoc file file-alist
)) styles t
)))
625 ;; run hook after publishing ends
626 (run-hook-with-args 'muse-after-project-publish-hook project
)
627 ;; notify the user that everything is now done
629 (message "All pages in %s have been published." (car project
))
630 (message "No pages in %s need publishing at this time."
633 (defun muse-project-batch-publish ()
634 "Publish Muse files in batch mode."
635 (let ((muse-batch-publishing-p t
)
637 (if (string= "--force" (or (car command-line-args-left
) ""))
639 command-line-args-left
(cdr command-line-args-left
)))
640 (if command-line-args-left
641 (dolist (project command-line-args-left
)
642 (message "Publishing project %s ..." project
)
643 (muse-project-publish project force
))
644 (message "No projects specified."))))
647 (put 'make-local-hook
'byte-compile nil
))
649 (defun muse-project-set-variables ()
650 "Load project-specific variables."
651 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
654 (setq sym
(car vars
))
655 (setq custom-set
(or (get sym
'custom-set
) 'set
))
656 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
657 (make-local-hook sym
)
658 (make-local-variable sym
)))
659 (funcall custom-set var
(car (cdr vars
)))
660 (setq vars
(cdr (cdr vars
))))))
662 (defun muse-project-delete-output-files (project)
664 (list (muse-read-project "Remove all output files for project: " nil t
)))
665 (setq project
(muse-project project
))
666 (let ((file-alist (muse-project-file-alist project
))
667 (styles (cddr project
))
669 (dolist (entry file-alist
)
670 (dolist (style styles
)
672 (and (setq path
(muse-style-element :path style
))
674 (concat (muse-style-element :prefix style
)
676 (or (muse-style-element :osuffix style
)
677 (muse-style-element :suffix style
)))
680 (muse-delete-file-if-exists output-file
))))))
682 (provide 'muse-project
)
684 ;;; muse-project.el ends here