1 ;;; muse-project.el --- handle Muse projects
3 ;; Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 ;; This file is not part of GNU Emacs.
7 ;; This is free software; you can redistribute it and/or modify it under
8 ;; the terms of the GNU General Public License as published by the Free
9 ;; Software Foundation; either version 2, or (at your option) any later
12 ;; This is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; 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
)
37 (defgroup muse-project nil
38 "Options controlling the behavior of Muse project handling."
41 (defcustom muse-before-project-publish-hook nil
42 "A hook run before a project is published.
43 Each function is passed the project object, a cons with the format
44 (PROJNAME . SETTINGS)"
48 (defcustom muse-after-project-publish-hook nil
49 "A hook run after a project is published.
50 Each function is passed the project object, a cons with the format
51 (PROJNAME . SETTINGS)"
55 (defvar muse-project-alist-using-customize nil
56 "Used internally by Muse to indicate whether `muse-project-alist'
57 has been modified via the customize interface.")
58 (make-variable-buffer-local 'muse-project-alist-using-customize
)
60 (defmacro with-muse-project
(project &rest body
)
62 (unless (muse-project ,project
)
63 (error "Can't find project %s" ,project
))
66 (setq muse-current-project
(muse-project ,project
))
67 (muse-project-set-variables)
70 (put 'with-muse-project
'lisp-indent-function
0)
71 (put 'with-muse-project
'edebug-form-spec
'(sexp body
))
73 (defun muse-project-alist-get (sym)
74 "Turn `muse-project-alist' into something we can customize easily."
76 (setq muse-project-alist-using-customize t
)
77 (let* ((val (copy-alist (symbol-value sym
)))
80 (let ((head (car (cdar val
)))
82 ;; Turn settings of first part into cons cells, symbol->string
84 (cond ((stringp (car head
))
85 (add-to-list 'res
(car head
) t
)
86 (setq head
(cdr head
)))
88 (add-to-list 'res
(list (symbol-name (car head
))
90 (setq head
(cddr head
)))
92 (setq head
(cdr head
)))))
93 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
94 (let ((styles (cdar val
)))
95 ;; Symbol->string in every style
97 (let ((head (cadr styles
))
100 (setq res
(plist-put res
(symbol-name (car head
))
102 (setq head
(cddr head
)))
103 (setcdr styles
(cons res
(cddr styles
))))
104 (setq styles
(cdr styles
))))
105 (setq val
(cdr val
)))
108 (defun muse-project-alist-set (sym val
)
109 "Turn customized version of `muse-project-alist' into something
110 Muse can make use of."
112 (when muse-project-alist-using-customize
113 ;; Make sure the unescaped version is written to .emacs
114 (put sym
'saved-value
(list (custom-quote val
)))
115 ;; Perform unescaping
117 (let ((head (car (cdar val
)))
119 ;; Turn cons cells into flat list, string->symbol
121 (cond ((stringp (car head
))
122 (add-to-list 'res
(car head
) t
))
124 (add-to-list 'res
(intern (caar head
)) t
)
125 (add-to-list 'res
(car (cdar head
)) t
)))
126 (setq head
(cdr head
)))
127 (setcdr (car val
) (cons res
(cdr (cdar val
)))))
128 (let ((styles (cdar val
)))
129 ;; String->symbol in every style
131 (let ((head (cadr styles
))
134 (setq res
(plist-put res
(intern (car head
))
136 (setq head
(cddr head
)))
137 (setcdr styles
(cons res
(cddr styles
))))
138 (setq styles
(cdr styles
))))
139 (setq val
(cdr val
)))))
141 (define-widget 'muse-project
'default
142 "A widget that defines a Muse project."
144 :value-create
'muse-widget-type-value-create
145 :value-get
'muse-widget-child-value-get
146 :value-delete
'ignore
147 :match
'muse-widget-type-match
148 :type
'(cons :format
" %v"
149 (repeat :tag
"Settings" :format
"%{%t%}:\n%v%i\n\n"
151 (string :tag
"Directory")
152 (list :tag
"Book function"
153 (const :tag
":book-funcall" ":book-funcall")
155 (sexp :tag
"Unknown")))
156 (list :tag
"Book part"
157 (const :tag
":book-part" ":book-part")
158 (string :tag
"Name"))
159 (list :tag
"Book style"
160 (const :tag
":book-style" ":book-style")
161 (string :tag
"Style"))
162 (list :tag
"Default file"
163 (const :tag
":default" ":default")
164 (string :tag
"File"))
165 (list :tag
"End of book"
166 (const :tag
":book-end" ":book-end")
168 (list :tag
"Force publishing"
169 (const :tag
":force-publish" ":force-publish")
170 (repeat (string :tag
"File")))
171 (list :tag
"Major mode"
172 (const :tag
":major-mode" ":major-mode")
173 (choice (function :tag
"Mode")
174 (sexp :tag
"Unknown")))
175 (list :tag
"New chapter"
176 (const :tag
":book-chapter" ":book-chapter")
177 (string :tag
"Name"))
178 (list :tag
"No chapters"
179 (const :tag
":nochapters" ":nochapters")
181 (list :tag
"Set variables"
182 (const :tag
":set" ":set")
183 (repeat (list :inline t
184 (symbol :tag
"Variable")
185 (sexp :tag
"Setting"))))
186 (list :tag
"Visit links using"
187 (const :tag
":visit-link" ":visit-link")
189 (sexp :tag
"Unknown")))))
190 (repeat :tag
"Styles" :format
"%{%t%}:\n%v%i\n\n"
193 :tag
"Publishing style"
194 (const :tag
":base" ":base")
195 (string :tag
"Style"))
198 (const :tag
":base-url" ":base-url")
201 :tag
"Exclude matching"
202 (const :tag
":exclude" ":exclude")
205 :tag
"Include matching"
206 (const :tag
":include" ":include")
209 :tag
"Timestamps file"
210 (const :tag
":timestamps" ":timestamps")
214 (const :tag
":path" ":path")
215 (string :tag
"Path"))))))
217 (defcustom muse-project-alist nil
218 "An alist of Muse projects.
219 A project defines a fileset, and a list of custom attributes for use
220 when publishing files in that project."
221 :type
'(choice (const :tag
"No projects defined." nil
)
222 (repeat (cons :format
"%{%t%}:\n\n%v"
223 :tag
"Project" :indent
4
224 (string :tag
"Project name")
226 (sexp :tag
"Cannot parse expression"))
227 :get
'muse-project-alist-get
228 :set
'muse-project-alist-set
229 :group
'muse-project
)
231 ;; Make it easier to specify a muse-project-alist entry
233 (defcustom muse-project-ignore-regexp
234 (concat "\\`\\(.*\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\|,.*\\)\\'\\|"
235 "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\)\\(/\\|\\'\\)")
236 "A regexp matching files to be ignored in Wiki directories."
240 (defun muse-project-recurse-directory (base)
241 "Recusively retrieve all of the directories underneath BASE.
242 A list of these directories is returned.
243 Directories starting with \".\" will be ignored, as well as those
244 which match `muse-project-ignore-regexp'."
245 (when (and (file-directory-p base
)
246 (not (string-match muse-project-ignore-regexp base
)))
248 (dolist (file (directory-files base t
"^[^.]"))
249 (when (and (file-directory-p file
)
250 (not (string-match muse-project-ignore-regexp file
)))
251 (setq dir
(file-name-nondirectory file
))
253 (nconc list
(mapcar #'(lambda (item)
254 (concat dir
"/" item
))
255 (muse-project-recurse-directory file
)))))
258 (defun muse-project-alist-styles (entry-dir output-dir style
)
259 "Return a list of styles to use in a `muse-project-alist' entry.
260 ENTRY-DIR is the top-level directory of the project.
261 OUTPUT-DIR is where Muse files are published, keeping directory structure.
262 STYLE is the publishing style to use.
264 For an example of the use of this function, see
265 `examples/mwolson/muse-init.el' from the Muse distribution."
266 (cons `(:base
,style
:path
,(expand-file-name output-dir
)
267 :include
,(concat "/" (file-name-nondirectory entry-dir
)
269 (mapcar (lambda (dir);
271 :path
,(expand-file-name dir output-dir
)
272 :include
,(concat "/" dir
"/[^/]+$")))
273 (muse-project-recurse-directory entry-dir
))))
275 (defun muse-project-alist-dirs (entry-dir)
276 "Return a list of directories to use in a `muse-project-alist' entry.
277 ENTRY-DIR is the top-level directory of the project.
279 For an example of the use of this function, see
280 `examples/mwolson/muse-init.el' from the Muse distribution."
281 (cons (expand-file-name entry-dir
)
282 (mapcar (lambda (dir) (expand-file-name dir entry-dir
))
283 (muse-project-recurse-directory entry-dir
))))
285 ;; Constructing the file-alist
287 (defvar muse-project-file-alist nil
288 "This variable is automagically constructed as needed.")
290 (defvar muse-current-project nil
291 "Project we are currently visiting.")
292 (make-variable-buffer-local 'muse-current-project
)
294 (defsubst muse-project
(&optional project
)
295 "Resolve the given PROJECT into a full Muse project, if it is a string."
297 (or muse-current-project
298 (muse-project-of-file))
299 (if (stringp project
)
300 (assoc project muse-project-alist
)
301 (muse-assert (consp project
))
304 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
305 "Return a filename if PAGE exists within the given Muse PROJECT."
306 (setq project
(muse-project project
))
307 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
309 (defun muse-project-private-p (file)
310 "Return non-nil if NAME is a private page with PROJECT."
311 (unless muse-under-windows-p
312 (setq file
(file-truename file
))
313 (if (file-attributes file
) ; don't publish if no attributes exist
314 (or (when (eq ?-
(aref (nth 8 (file-attributes
315 (file-name-directory file
))) 7))
317 "The " (file-name-directory file
)
318 " directory must be readable by others"
319 " in order for its contents to be published.")))
320 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
323 (defun muse-project-file-entries (path)
324 (let* ((names (list t
))
327 ((file-directory-p path
)
328 (dolist (file (directory-files
329 path t
(when (and muse-file-extension
330 (not (string= muse-file-extension
"")))
331 (concat "." muse-file-extension
"\\'"))))
332 (unless (or (string-match muse-project-ignore-regexp file
)
333 (file-directory-p file
))
335 (cons (cons (muse-page-name file
) file
) nil
))
336 (setq lnames
(cdr lnames
)))))
337 ((file-readable-p path
)
339 (cons (cons (muse-page-name path
) path
) nil
))
340 (setq lnames
(cdr lnames
)))
342 (muse-assert (file-name-directory path
))
343 (dolist (file (directory-files
344 (file-name-directory path
) t
345 (file-name-nondirectory path
)))
346 (unless (string-match muse-project-ignore-regexp file
)
348 (cons (cons (muse-page-name file
) file
) nil
))
349 (setq lnames
(cdr lnames
))))))
352 (defun muse-project-file-alist (&optional project no-check-p
)
353 "Return member filenames for the given Muse PROJECT.
354 On UNIX, this list is only updated if one of the directories'
355 contents have changed. On Windows, it is always reread from
357 (setq project
(muse-project project
))
358 (let* ((file-alist (assoc (car project
) muse-project-file-alist
))
359 (last-mod (cdr (cdr file-alist
))))
360 ;; Determine the last modified of any directory mentioned in the
361 ;; project's pattern list
362 (unless (or muse-under-windows-p no-check-p
)
363 (let ((pats (cadr project
)))
365 (if (symbolp (car pats
))
366 (setq pats
(cddr pats
))
367 (let* ((fnd (file-name-directory (car pats
)))
368 (dir (cond ((file-directory-p (car pats
))
370 ((and (not (file-readable-p (car pats
)))
372 (file-directory-p fnd
))
375 (let ((mod-time (nth 5 (file-attributes dir
))))
376 (when (or (null last-mod
)
378 (muse-time-less-p last-mod mod-time
)))
379 (setq last-mod mod-time
)))))
380 (setq pats
(cdr pats
))))))
381 ;; Either return the currently known list, or read it again from
383 (if (or (and no-check-p
(cadr file-alist
))
384 (not (or muse-under-windows-p
385 (null (cddr file-alist
))
387 (muse-time-less-p (cddr file-alist
) last-mod
))))
390 (setcdr (cdr file-alist
) last-mod
)
391 (setq file-alist
(cons (car project
) (cons nil last-mod
))
392 muse-project-file-alist
393 (cons file-alist muse-project-file-alist
)))
394 ;; Read in all of the file entries
398 (let* ((names (list t
))
399 (pats (cadr project
)))
401 (if (symbolp (car pats
))
402 (setq pats
(cddr pats
))
403 (nconc names
(muse-project-file-entries (car pats
)))
404 (setq pats
(cdr pats
))))
407 (defun muse-project-of-file (&optional pathname
)
408 "Determine which project the given PATHNAME relates to.
409 If PATHNAME is nil, the current buffer's filename is used."
410 (if (and (null pathname
) muse-current-project
)
412 (unless pathname
(setq pathname
(muse-current-file)))
415 (not (string-match muse-project-ignore-regexp pathname
)))
416 (let* ((file (file-truename pathname
))
417 (dir (file-name-directory file
))
418 (project-entry muse-project-alist
)
420 (while (and project-entry
(not found
))
421 (let ((pats (car (cdar project-entry
))))
422 (while (and pats
(not found
))
423 (if (symbolp (car pats
))
424 (setq pats
(cddr pats
))
425 (let ((truename (file-truename (car pats
))))
426 (if (or (string= truename file
)
427 (string= truename dir
)
428 (string-match (regexp-quote truename
) file
))
429 (setq found
(car project-entry
))))
430 (setq pats
(cdr pats
))))
431 (setq project-entry
(cdr project-entry
))))
434 (defun muse-read-project (prompt &optional no-check-p no-assume
)
435 "Read a project name from the minibuffer, if it can't be figured
437 (if (null muse-project-alist
)
438 (error "There are no Muse projects defined; see `muse-project-alist'.")
439 (or (unless no-check-p
440 (muse-project-of-file))
441 (if (and (not no-assume
)
442 (= 1 (length muse-project-alist
)))
443 (car muse-project-alist
)
444 (assoc (completing-read prompt muse-project-alist
)
445 muse-project-alist
)))))
447 (defvar muse-project-page-history nil
)
449 (defun muse-read-project-file (project prompt
&optional default
)
450 (let ((name (completing-read prompt
(muse-project-file-alist project
)
451 nil nil nil
'muse-project-page-history
453 (cons name
(muse-project-page-file name project
))))
456 (defun muse-project-find-file (name project
&optional command directory
)
457 "Open the Muse page given by NAME in PROJECT.
458 If COMMAND is non-nil, it is the function used to visit the file.
459 If DIRECTORY is non-nil, it is the directory in which the page
460 will be created if it does not already exist. Otherwise, the
461 first directory within the project's fileset is used."
463 (let* ((project (muse-read-project "Find in project: "
465 (default (muse-get-keyword :default
(cadr project
)))
466 (entry (muse-read-project-file
468 (format "Find page: (default: %s) "
472 (list entry project
)))
473 (setq project
(muse-project project
))
474 (let ((project-name (car project
)))
475 (unless (interactive-p)
476 (setq project
(muse-project project
)
477 name
(cons name
(muse-project-page-file name project
))))
478 ;; If we're given a relative or absolute filename, open it as-is
480 (save-match-data (string-match muse-file-regexp
(car name
))))
481 (setcdr name
(car name
))
482 ;; At this point, name is (PAGE . FILE).
484 (let ((pats (cadr project
)))
485 (while (and pats
(null directory
))
486 (if (symbolp (car pats
))
487 (setq pats
(cddr pats
))
488 (if (file-directory-p (car pats
))
489 (setq directory
(car pats
) pats nil
)
490 (setq pats
(cdr pats
))))))
492 (let ((filename (expand-file-name
493 (if (and muse-file-extension
494 (not (string= muse-file-extension
"")))
495 (concat (car name
) "." muse-file-extension
)
498 (unless (file-exists-p directory
)
499 (make-directory directory t
))
500 (setcdr name filename
)))))
503 (funcall (or command
'find-file
) (cdr name
))
504 (error "There is no page %s in project %s."
505 (car name
) project-name
))))
507 (defun muse-project-applicable-styles (file styles
&optional ignore-regexp
)
508 "Given STYLES, return a list of the ones that are considered for FILE.
509 The name of a project may be used for STYLES."
510 (when (stringp styles
)
511 (setq styles
(cddr (muse-project styles
))))
512 (when (and file styles
)
514 (dolist (style styles
)
515 (let ((include-regexp (muse-style-element :include style
))
516 (exclude-regexp (muse-style-element :exclude style
)))
517 (when (and (or ignore-regexp
518 (and (null include-regexp
)
519 (null exclude-regexp
))
521 (string-match include-regexp file
)
522 (not (string-match exclude-regexp file
))))
523 (or (not (file-exists-p file
))
524 (not (muse-project-private-p file
))))
525 (add-to-list 'used-styles style
))))
528 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
529 (setq styles
(muse-project-applicable-styles file styles ignore-regexp
))
531 (dolist (style styles
)
532 (let ((output-dir (muse-style-element :path style
)))
533 ;; ensure the publishing location is available
534 (unless (file-exists-p output-dir
)
535 (message "Creating publishing directory %s" output-dir
)
536 (make-directory output-dir
))
537 ;; publish the member file!
538 (if (muse-publish-file file style output-dir force
)
539 (setq published t
))))
542 (defun muse-project-save-buffers (&optional project
)
543 (setq project
(muse-project project
))
547 (and (buffer-modified-p buffer
)
548 (not (buffer-base-buffer buffer
))
549 (or (buffer-file-name buffer
)
552 (and buffer-offer-save
553 (> (buffer-size) 0))))
554 (with-current-buffer buffer
555 (let ((proj (muse-project-of-file)))
556 (and proj
(string= (car proj
)
558 (if (buffer-file-name buffer
)
559 (format "Save file %s? "
560 (buffer-file-name buffer
))
561 (format "Save buffer %s? "
562 (buffer-name buffer
))))))
568 '("buffer" "buffers" "save")
569 (if (boundp 'save-some-buffers-action-alist
)
570 save-some-buffers-action-alist
)))
573 (defun muse-project-publish (project &optional force
)
574 "Publish the pages of PROJECT that need publishing."
575 (interactive (list (muse-read-project "Publish project: " nil t
)
577 (setq project
(muse-project project
))
578 (let ((styles (cddr project
))
579 (muse-current-project project
)
581 ;; determine the style from the project, or else ask
583 (setq styles
(list (muse-publish-get-style))))
584 ;; prompt to save any buffers related to this project
585 (muse-project-save-buffers project
)
586 ;; run hook before publishing begins
587 (run-hook-with-args 'muse-before-project-publish-hook project
)
588 ;; publish all files in the project, for each style; the actual
589 ;; publishing will only happen if the files are newer than the
590 ;; last published output, or if the file is listed in
591 ;; :force-publish. Files in :force-publish will not trigger the
592 ;; "All pages need to be published" message.
593 (let ((forced-files (muse-get-keyword :force-publish
(cadr project
)))
594 (file-alist (muse-project-file-alist project
)))
595 (dolist (pair file-alist
)
596 (when (muse-project-publish-file (cdr pair
) styles force
)
597 (setq forced-files
(delete (car pair
) forced-files
))
599 (dolist (file forced-files
)
600 (muse-project-publish-file (cdr (assoc file file-alist
)) styles t
)))
601 ;; run hook after publishing ends
602 (run-hook-with-args 'muse-after-project-publish-hook project
)
603 ;; notify the user that everything is now done
605 (message "All pages in %s have been published." (car project
))
606 (message "No pages in %s need publishing at this time."
609 (defun muse-project-batch-publish ()
610 "Publish Muse files in batch mode."
611 (let ((muse-batch-publishing-p t
)
613 (if (string= "--force" (or (car command-line-args-left
) ""))
615 command-line-args-left
(cdr command-line-args-left
)))
616 (if command-line-args-left
617 (dolist (project command-line-args-left
)
618 (message "Publishing project %s ..." project
)
619 (muse-project-publish project force
))
620 (message "No projects specified."))))
623 (put 'make-local-hook
'byte-compile nil
))
625 (defun muse-project-set-variables ()
626 "Load project-specific variables."
627 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
630 (setq sym
(car vars
))
631 (setq custom-set
(or (get sym
'custom-set
) 'set
))
632 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
633 (make-local-hook sym
)
634 (make-local-variable sym
)))
635 (funcall custom-set var
(car (cdr vars
)))
636 (setq vars
(cdr (cdr vars
))))))
638 (defun muse-project-delete-output-files (project)
640 (list (muse-read-project "Remove all output files for project: " nil t
)))
641 (setq project
(muse-project project
))
642 (let ((file-alist (muse-project-file-alist project
))
643 (styles (cddr project
))
645 (dolist (entry file-alist
)
646 (dolist (style styles
)
648 (and (setq path
(muse-style-element :path style
))
650 (concat (muse-style-element :prefix style
)
652 (or (muse-style-element :osuffix style
)
653 (muse-style-element :suffix style
)))
656 (muse-delete-file-if-exists output-file
))))))
658 (provide 'muse-project
)
660 ;;; muse-project.el ends here