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., 59 Temple Place - Suite 330, Boston,
20 ;; MA 02111-1307, USA.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (require 'muse-publish
)
37 (defgroup muse-project nil
38 "Options controlling the behaviour 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 (make-variable-buffer-local 'muse-current-project
)
74 (defsubst muse-project
(&optional project
)
75 "Resolve the given PROJECT into a full Muse project, if it is a string."
77 (or muse-current-project
78 (muse-project-of-file))
80 (assoc project muse-project-alist
)
81 (muse-assert (consp project
))
84 (defsubst muse-project-page-file
(page project
&optional no-check-p
)
85 "Return a filename if PAGE exists within the given Muse PROJECT."
86 (setq project
(muse-project project
))
87 (cdr (assoc page
(muse-project-file-alist project no-check-p
))))
89 (defun muse-project-private-p (file)
90 "Return non-nil if NAME is a private page with PROJECT."
91 (unless muse-under-windows-p
92 (setq file
(file-truename file
))
93 (if (file-attributes file
) ; don't publish if no attributes exist
94 (or (eq ?-
(aref (nth 8 (file-attributes
95 (file-name-directory file
))) 7))
96 (eq ?-
(aref (nth 8 (file-attributes file
)) 7)))
99 (defun muse-project-file-entries (path)
100 (let* ((names (list t
))
104 ((file-directory-p path
)
105 (dolist (file (directory-files path t
))
106 (unless (or (string-match muse-project-ignore-regexp file
)
107 (file-directory-p file
))
109 (cons (cons (muse-page-name file
) file
) nil
))
110 (setq lnames
(cdr lnames
)))))
111 ((file-readable-p path
)
113 (cons (cons (muse-page-name path
) path
) nil
))
114 (setq lnames
(cdr lnames
)))
116 (dolist (file (directory-files
117 (file-name-directory path
) t
118 (file-name-nondirectory path
)))
119 (unless (string-match muse-project-ignore-regexp file
)
121 (cons (cons (muse-page-name file
) file
) nil
))
122 (setq lnames
(cdr lnames
))))))
125 (defun muse-project-file-alist (&optional project no-check-p
)
126 "Return member filenames for the given Muse PROJECT.
127 On UNIX, this list is only updated if one of the directories'
128 contents have changed. On Windows, it is always reread from
130 (setq project
(muse-project project
))
131 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
133 ;; Determine the last modified of any directory mentioned in the
134 ;; project's pattern list
135 (unless (or muse-under-windows-p no-check-p
)
136 (let ((pats (cadr project
)))
138 (if (symbolp (car pats
))
139 (setq pats
(cddr pats
))
140 (let ((dir (or (and (file-directory-p (car pats
)) (car pats
))
141 (and (not (file-readable-p (car pats
)))
143 (file-name-directory (car pats
)))
144 (file-name-directory (car pats
))))))
146 (let ((mod-time (nth 5 (file-attributes dir
))))
147 (if (or (null last-mod
)
149 (muse-time-less-p last-mod mod-time
)))
150 (setq last-mod mod-time
)))))
151 (setq pats
(cdr pats
))))))
152 ;; Either return the currently known list, or read it again from
154 (if (or (and no-check-p
(cadr file-alist
))
155 (not (or muse-under-windows-p
156 (null (cddr file-alist
))
158 (muse-time-less-p (cddr file-alist
) last-mod
))))
161 (setcdr (cdr file-alist
) last-mod
)
162 (setq file-alist
(cons (car project
) (cons nil last-mod
))
163 muse-project-file-alist
164 (cons file-alist muse-project-file-alist
)))
165 ;; Read in all of the file entries
169 (let* ((names (list t
))
170 (pats (cadr project
)))
172 (if (symbolp (car pats
))
173 (setq pats
(cddr pats
))
174 (nconc names
(muse-project-file-entries (car pats
)))
175 (setq pats
(cdr pats
))))
178 (defun muse-project-of-file (&optional pathname
)
179 "Determine which project the given PATHNAME relates to.
180 If PATHNAME is nil, the current buffer's filename is used."
181 (if (and (null pathname
) muse-current-project
)
183 (when (or pathname buffer-file-name
)
184 (let* ((file (file-truename (or pathname buffer-file-name
)))
185 (dir (file-name-directory file
))
186 (project-entry muse-project-alist
)
188 (while (and project-entry
(not found
))
189 (let ((pats (car (cdar project-entry
))))
190 (while (and pats
(not found
))
191 (if (symbolp (car pats
))
192 (setq pats
(cddr pats
))
193 (let ((truename (file-truename (car pats
))))
194 (if (or (string= truename file
)
195 (string= truename dir
)
196 (string-match truename file
))
197 (setq found
(car project-entry
))))
198 (setq pats
(cdr pats
))))
199 (setq project-entry
(cdr project-entry
))))
202 (defun muse-read-project (prompt &optional no-check-p no-assume
)
203 "Read a project name from the minibuffer, if it can't be figured
205 (if (null muse-project-alist
)
206 (error "There are no Muse projects defined; see `muse-project-alist'.")
207 (or (unless no-check-p
208 (muse-project-of-file))
209 (if (and (not no-assume
)
210 (= 1 (length muse-project-alist
)))
211 (car muse-project-alist
)
212 (assoc (completing-read prompt muse-project-alist
)
213 muse-project-alist
)))))
215 (defvar muse-project-page-history nil
)
217 (defun muse-read-project-file (project prompt
&optional default
)
218 (let ((name (completing-read prompt
(muse-project-file-alist project
)
219 nil nil nil
'muse-project-page-history
221 (cons name
(muse-project-page-file name project
))))
223 (defun muse-project-find-file (name project
&optional command directory
)
224 "Open the Muse page given by NAME in PROJECT.
225 If COMMAND is non-nil, it is the function used to visit the file.
226 If DIRECTORY is non-nil, it is the directory in which the page
227 will be created if it does not already exist. Otherwise, the
228 first directory within the project's fileset is used."
230 (let* ((project (muse-read-project "Find in project: "
232 (default (muse-get-keyword :default
(cadr project
)))
233 (entry (muse-read-project-file
235 (format "Find page: (default: %s) "
239 (list entry project
)))
240 (setq project
(muse-project project
))
241 (let ((project-name (car project
)))
242 (unless (interactive-p)
243 (setq project
(muse-project project
)
244 name
(cons name
(muse-project-page-file name project
))))
245 ;; At this point, name is (PAGE . FILE).
247 (let ((pats (cadr project
)))
248 (while (and pats
(null directory
))
249 (if (symbolp (car pats
))
250 (setq pats
(cddr pats
))
251 (if (file-directory-p (car pats
))
252 (setq directory
(car pats
) pats nil
)
253 (setq pats
(cdr pats
))))))
255 (let ((filename (expand-file-name (car name
) directory
)))
256 (unless (file-exists-p directory
)
257 (make-directory directory t
))
258 (setcdr name filename
))))
261 (funcall (or command
'find-file
) (cdr name
))
262 (error "There is no page %s in project %s."
263 (car name
) project-name
))))
265 (defun muse-project-publish-file (file styles
&optional force ignore-regexp
)
267 (dolist (style styles
)
268 (let ((include-regexp (muse-style-element :include style
))
269 (exclude-regexp (muse-style-element :exclude style
)))
270 (when (and (or ignore-regexp
271 (and (null include-regexp
)
272 (null exclude-regexp
))
274 (string-match include-regexp file
)
275 (not (string-match exclude-regexp file
))))
276 (not (muse-project-private-p file
)))
277 ;; ensure the publishing location is available
278 (let ((output-dir (muse-style-element :path style
)))
279 (unless (file-exists-p output-dir
)
280 (message "Creating publishing directory %s" output-dir
)
281 (make-directory output-dir
))
282 ;; publish the member file!
283 (if (muse-publish-file file style output-dir force
)
284 (setq published t
))))))
287 (defun muse-project-save-buffers (&optional project
)
288 (setq project
(muse-project project
))
292 (and (buffer-modified-p buffer
)
293 (not (buffer-base-buffer buffer
))
294 (or (buffer-file-name buffer
)
297 (and buffer-offer-save
298 (> (buffer-size) 0))))
299 (with-current-buffer buffer
300 (let ((proj (muse-project-of-file)))
301 (and proj
(string= (car proj
)
303 (if (buffer-file-name buffer
)
304 (format "Save file %s? "
305 (buffer-file-name buffer
))
306 (format "Save buffer %s? "
307 (buffer-name buffer
))))))
313 '("buffer" "buffers" "save")
314 (if (boundp 'save-some-buffers-action-alist
)
315 save-some-buffers-action-alist
)))
317 (defun muse-project-publish (project &optional force
)
318 "Publish the pages of PROJECT that need publishing."
319 (interactive (list (muse-read-project "Publish project: " nil t
)
321 (setq project
(muse-project project
))
322 (let ((styles (cddr project
))
323 (muse-current-project project
)
325 ;; determine the style from the project, or else ask
327 (setq styles
(list (muse-publish-get-style))))
328 ;; prompt to save any buffers related to this project
329 (muse-project-save-buffers project
)
330 ;; run hook before publishing begins
331 (run-hook-with-args 'muse-before-project-publish-hook project
)
332 ;; publish all files in the project, for each style; the actual
333 ;; publishing will only happen if the files are newer than the
334 ;; last published output
335 (dolist (pair (muse-project-file-alist project
))
336 (if (muse-project-publish-file (cdr pair
) styles force
)
338 ;; run hook after publishing ends
339 (run-hook-with-args 'muse-after-project-publish-hook project
)
340 ;; notify the user that everything is now done
342 (message "All pages in %s have been published." (car project
))
343 (message "No pages in %s need publishing at this time."
346 (defun muse-project-batch-publish ()
347 "Publish Muse files in batch mode."
348 (let ((muse-batch-publishing-p t
)
350 (if (string= "--force" (car command-line-args-left
))
352 command-line-args-left
(cdr command-line-args-left
)))
353 (dolist (project command-line-args-left
)
354 (message "Publishing project %s ..." project
)
355 (muse-project-publish project force
))))
358 (put 'make-local-hook
'byte-compile nil
))
360 (defun muse-project-set-variables ()
361 "Load project-specific variables."
362 (let ((vars (muse-get-keyword :set
(cadr muse-current-project
)))
365 (setq sym
(car vars
))
366 (setq custom-set
(or (get sym
'custom-set
) 'set
))
367 (setq var
(if (eq (get sym
'custom-type
) 'hook
)
368 (make-local-hook sym
)
369 (make-local-variable sym
)))
370 (funcall custom-set var
(car (cdr vars
)))
371 (setq vars
(cdr (cdr vars
))))))
373 (defun muse-project-delete-output-files (project)
375 (list (muse-read-project "Remove all output files for project: " nil t
)))
376 (setq project
(muse-project project
))
377 (let ((file-alist (muse-project-file-alist project
))
378 (styles (cddr project
))
380 (dolist (entry file-alist
)
381 (dolist (style styles
)
383 (and (setq path
(muse-style-element :path style
))
385 (concat (muse-style-element :prefix style
)
387 (or (muse-style-element :osuffix style
)
388 (muse-style-element :suffix style
)))
391 (muse-delete-file-if-exists output-file
))))))
393 (provide 'muse-project
)
395 ;;; muse-project.el ends here