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 (defcustom muse-project-alist nil
56 "An alist of Muse projects.
57 A project defines a fileset, and a list of custom attributes for use
58 when publishing files in that project."
59 :type
'(alist :key-type string
:value sexp
)
62 (defvar muse-project-file-alist nil
63 "This variable is automagically constructed as needed.")
65 (defcustom muse-project-ignore-regexp
66 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
67 "A regexp matching files to be ignored in Wiki directories."
71 (defvar muse-current-project nil
72 "Project we are currently visiting.")
73 (make-variable-buffer-local 'muse-current-project
)
75 (defsubst muse-project
(&optional project
)
76 "Resolve the given PROJECT into a full Muse project, if it is a string."
78 (or muse-current-project
79 (muse-project-of-file))
81 (assoc project muse-project-alist
)
82 (muse-assert (consp project
))
85 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
86 "Return a filename if PAGE exists within the given Muse PROJECT."
87 (setq project
(muse-project project
))
88 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
90 (defun muse-project-private-p (file)
91 "Return non-nil if NAME is a private page with PROJECT."
92 (unless muse-under-windows-p
93 (setq file
(file-truename file
))
94 (if (file-attributes file
) ; don't publish if no attributes exist
95 (or (when (eq ?-
(aref (nth 8 (file-attributes
96 (file-name-directory file
))) 7))
98 "The " (file-name-directory file
)
99 " directory must be readable by others"
100 " in order for its contents to be published.")))
101 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
104 (defun muse-project-file-entries (path)
105 (let* ((names (list t
))
108 ((file-directory-p path
)
109 (dolist (file (directory-files path t
))
110 (unless (or (string-match muse-project-ignore-regexp file
)
111 (file-directory-p file
))
113 (cons (cons (muse-page-name file
) file
) nil
))
114 (setq lnames
(cdr lnames
)))))
115 ((file-readable-p path
)
117 (cons (cons (muse-page-name path
) path
) nil
))
118 (setq lnames
(cdr lnames
)))
120 (dolist (file (directory-files
121 (file-name-directory path
) t
122 (file-name-nondirectory path
)))
123 (unless (string-match muse-project-ignore-regexp file
)
125 (cons (cons (muse-page-name file
) file
) nil
))
126 (setq lnames
(cdr lnames
))))))
129 (defun muse-project-file-alist (&optional project no-check-p
)
130 "Return member filenames for the given Muse PROJECT.
131 On UNIX, this list is only updated if one of the directories'
132 contents have changed. On Windows, it is always reread from
134 (setq project
(muse-project project
))
135 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
137 ;; Determine the last modified of any directory mentioned in the
138 ;; project's pattern list
139 (unless (or muse-under-windows-p no-check-p
)
140 (let ((pats (cadr project
)))
142 (if (symbolp (car pats
))
143 (setq pats
(cddr pats
))
144 (let ((dir (or (and (file-directory-p (car pats
)) (car pats
))
145 (and (not (file-readable-p (car pats
)))
147 (file-name-directory (car pats
)))
148 (file-name-directory (car pats
))))))
150 (let ((mod-time (nth 5 (file-attributes dir
))))
151 (if (or (null last-mod
)
153 (muse-time-less-p last-mod mod-time
)))
154 (setq last-mod mod-time
)))))
155 (setq pats
(cdr pats
))))))
156 ;; Either return the currently known list, or read it again from
158 (if (or (and no-check-p
(cadr file-alist
))
159 (not (or muse-under-windows-p
160 (null (cddr file-alist
))
162 (muse-time-less-p (cddr file-alist
) last-mod
))))
165 (setcdr (cdr file-alist
) last-mod
)
166 (setq file-alist
(cons (car project
) (cons nil last-mod
))
167 muse-project-file-alist
168 (cons file-alist muse-project-file-alist
)))
169 ;; Read in all of the file entries
173 (let* ((names (list t
))
174 (pats (cadr project
)))
176 (if (symbolp (car pats
))
177 (setq pats
(cddr pats
))
178 (nconc names
(muse-project-file-entries (car pats
)))
179 (setq pats
(cdr pats
))))
182 (defun muse-project-of-file (&optional pathname
)
183 "Determine which project the given PATHNAME relates to.
184 If PATHNAME is nil, the current buffer's filename is used."
185 (if (and (null pathname
) muse-current-project
)
187 (when (or pathname muse-current-file buffer-file-name
)
188 (let* ((file (file-truename (or pathname buffer-file-name
)))
189 (dir (file-name-directory file
))
190 (project-entry muse-project-alist
)
192 (while (and project-entry
(not found
))
193 (let ((pats (car (cdar project-entry
))))
194 (while (and pats
(not found
))
195 (if (symbolp (car pats
))
196 (setq pats
(cddr pats
))
197 (let ((truename (file-truename (car pats
))))
198 (if (or (string= truename file
)
199 (string= truename dir
)
200 (string-match truename file
))
201 (setq found
(car project-entry
))))
202 (setq pats
(cdr pats
))))
203 (setq project-entry
(cdr project-entry
))))
206 (defun muse-read-project (prompt &optional no-check-p no-assume
)
207 "Read a project name from the minibuffer, if it can't be figured
209 (if (null muse-project-alist
)
210 (error "There are no Muse projects defined; see `muse-project-alist'.")
211 (or (unless no-check-p
212 (muse-project-of-file))
213 (if (and (not no-assume
)
214 (= 1 (length muse-project-alist
)))
215 (car muse-project-alist
)
216 (assoc (completing-read prompt muse-project-alist
)
217 muse-project-alist
)))))
219 (defvar muse-project-page-history nil
)
221 (defun muse-read-project-file (project prompt
&optional default
)
222 (let ((name (completing-read prompt
(muse-project-file-alist project
)
223 nil nil nil
'muse-project-page-history
225 (cons name
(muse-project-page-file name project
))))
227 (defun muse-project-find-file (name project
&optional command directory
)
228 "Open the Muse page given by NAME in PROJECT.
229 If COMMAND is non-nil, it is the function used to visit the file.
230 If DIRECTORY is non-nil, it is the directory in which the page
231 will be created if it does not already exist. Otherwise, the
232 first directory within the project's fileset is used."
234 (let* ((project (muse-read-project "Find in project: "
236 (default (muse-get-keyword :default
(cadr project
)))
237 (entry (muse-read-project-file
239 (format "Find page: (default: %s) "
243 (list entry project
)))
244 (setq project
(muse-project project
))
245 (let ((project-name (car project
)))
246 (unless (interactive-p)
247 (setq project
(muse-project project
)
248 name
(cons name
(muse-project-page-file name project
))))
249 ;; At this point, name is (PAGE . FILE).
251 (let ((pats (cadr project
)))
252 (while (and pats
(null directory
))
253 (if (symbolp (car pats
))
254 (setq pats
(cddr pats
))
255 (if (file-directory-p (car pats
))
256 (setq directory
(car pats
) pats nil
)
257 (setq pats
(cdr pats
))))))
259 (let ((filename (expand-file-name (car name
) directory
)))
260 (unless (file-exists-p directory
)
261 (make-directory directory t
))
262 (setcdr name filename
))))
265 (funcall (or command
'find-file
) (cdr name
))
266 (error "There is no page %s in project %s."
267 (car name
) project-name
))))
269 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
271 (dolist (style styles
)
272 (let ((include-regexp (muse-style-element :include style
))
273 (exclude-regexp (muse-style-element :exclude style
)))
274 (when (and (or ignore-regexp
275 (and (null include-regexp
)
276 (null exclude-regexp
))
278 (string-match include-regexp file
)
279 (not (string-match exclude-regexp file
))))
280 (not (muse-project-private-p file
)))
281 ;; ensure the publishing location is available
282 (let ((output-dir (muse-style-element :path style
)))
283 (unless (file-exists-p output-dir
)
284 (message "Creating publishing directory %s" output-dir
)
285 (make-directory output-dir
))
286 ;; publish the member file!
287 (if (muse-publish-file file style output-dir force
)
288 (setq published t
))))))
291 (defun muse-project-save-buffers (&optional project
)
292 (setq project
(muse-project project
))
296 (and (buffer-modified-p buffer
)
297 (not (buffer-base-buffer buffer
))
298 (or (buffer-file-name buffer
)
301 (and buffer-offer-save
302 (> (buffer-size) 0))))
303 (with-current-buffer buffer
304 (let ((proj (muse-project-of-file)))
305 (and proj
(string= (car proj
)
307 (if (buffer-file-name buffer
)
308 (format "Save file %s? "
309 (buffer-file-name buffer
))
310 (format "Save buffer %s? "
311 (buffer-name buffer
))))))
317 '("buffer" "buffers" "save")
318 (if (boundp 'save-some-buffers-action-alist
)
319 save-some-buffers-action-alist
)))
321 (defun muse-project-publish (project &optional force
)
322 "Publish the pages of PROJECT that need publishing."
323 (interactive (list (muse-read-project "Publish project: " nil t
)
325 (setq project
(muse-project project
))
326 (let ((styles (cddr project
))
327 (muse-current-project project
)
329 ;; determine the style from the project, or else ask
331 (setq styles
(list (muse-publish-get-style))))
332 ;; prompt to save any buffers related to this project
333 (muse-project-save-buffers project
)
334 ;; run hook before publishing begins
335 (run-hook-with-args 'muse-before-project-publish-hook project
)
336 ;; publish all files in the project, for each style; the actual
337 ;; publishing will only happen if the files are newer than the
338 ;; last published output
339 (dolist (pair (muse-project-file-alist project
))
340 (if (muse-project-publish-file (cdr pair
) styles force
)
342 ;; run hook after publishing ends
343 (run-hook-with-args 'muse-after-project-publish-hook project
)
344 ;; notify the user that everything is now done
346 (message "All pages in %s have been published." (car project
))
347 (message "No pages in %s need publishing at this time."
350 (defun muse-project-batch-publish ()
351 "Publish Muse files in batch mode."
352 (let ((muse-batch-publishing-p t
)
354 (if (string= "--force" (car command-line-args-left
))
356 command-line-args-left
(cdr command-line-args-left
)))
357 (dolist (project command-line-args-left
)
358 (message "Publishing project %s ..." project
)
359 (muse-project-publish project force
))))
362 (put 'make-local-hook
'byte-compile nil
))
364 (defun muse-project-set-variables ()
365 "Load project-specific variables."
366 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
369 (setq sym
(car vars
))
370 (setq custom-set
(or (get sym
'custom-set
) 'set
))
371 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
372 (make-local-hook sym
)
373 (make-local-variable sym
)))
374 (funcall custom-set var
(car (cdr vars
)))
375 (setq vars
(cdr (cdr vars
))))))
377 (defun muse-project-delete-output-files (project)
379 (list (muse-read-project "Remove all output files for project: " nil t
)))
380 (setq project
(muse-project project
))
381 (let ((file-alist (muse-project-file-alist project
))
382 (styles (cddr project
))
384 (dolist (entry file-alist
)
385 (dolist (style styles
)
387 (and (setq path
(muse-style-element :path style
))
389 (concat (muse-style-element :prefix style
)
391 (or (muse-style-element :osuffix style
)
392 (muse-style-element :suffix style
)))
395 (muse-delete-file-if-exists output-file
))))))
397 (provide 'muse-project
)
399 ;;; muse-project.el ends here