Cater even more to XEmacs and its annoying whims.
[muse-el.git] / lisp / muse-project.el
blobebb4d63481a4d4792c3671ba44bac3289a52c274
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
10 ;; version.
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
15 ;; for more details.
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.
22 ;;; Commentary:
24 ;;; Contributors:
26 ;;; Code:
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Muse Project Maintainance
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (require 'muse)
35 (require 'muse-publish)
37 (defgroup muse-project nil
38 "Options controlling the behavior of Muse project handling."
39 :group 'muse)
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)"
45 :type 'hook
46 :group 'muse-project)
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)"
52 :type 'hook
53 :group 'muse-project)
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)
60 :group 'muse-project)
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."
68 :type 'regexp
69 :group 'muse-regexps)
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."
76 (if (null project)
77 (or muse-current-project
78 (muse-project-of-file))
79 (if (stringp project)
80 (assoc project muse-project-alist)
81 (muse-assert (consp project))
82 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 (when (eq ?- (aref (nth 8 (file-attributes
95 (file-name-directory file))) 7))
96 (message (concat
97 "The " (file-name-directory file)
98 " directory must be readable by others"
99 " in order for its contents to be published.")))
100 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
101 t)))
103 (defun muse-project-file-entries (path)
104 (let* ((names (list t))
105 (lnames names))
106 (cond
107 ((file-directory-p path)
108 (dolist (file (directory-files path t))
109 (unless (or (string-match muse-project-ignore-regexp file)
110 (file-directory-p file))
111 (setcdr lnames
112 (cons (cons (muse-page-name file) file) nil))
113 (setq lnames (cdr lnames)))))
114 ((file-readable-p path)
115 (setcdr lnames
116 (cons (cons (muse-page-name path) path) nil))
117 (setq lnames (cdr lnames)))
118 (t ; regexp
119 (dolist (file (directory-files
120 (file-name-directory path) t
121 (file-name-nondirectory path)))
122 (unless (string-match muse-project-ignore-regexp file)
123 (setcdr lnames
124 (cons (cons (muse-page-name file) file) nil))
125 (setq lnames (cdr lnames))))))
126 (cdr names)))
128 (defun muse-project-file-alist (&optional project no-check-p)
129 "Return member filenames for the given Muse PROJECT.
130 On UNIX, this list is only updated if one of the directories'
131 contents have changed. On Windows, it is always reread from
132 disk."
133 (setq project (muse-project project))
134 (let ((file-alist (assoc (car project) muse-project-file-alist))
135 last-mod)
136 ;; Determine the last modified of any directory mentioned in the
137 ;; project's pattern list
138 (unless (or muse-under-windows-p no-check-p)
139 (let ((pats (cadr project)))
140 (while pats
141 (if (symbolp (car pats))
142 (setq pats (cddr pats))
143 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
144 (and (not (file-readable-p (car pats)))
145 (file-directory-p
146 (file-name-directory (car pats)))
147 (file-name-directory (car pats))))))
148 (if dir
149 (let ((mod-time (nth 5 (file-attributes dir))))
150 (if (or (null last-mod)
151 (and mod-time
152 (muse-time-less-p last-mod mod-time)))
153 (setq last-mod mod-time)))))
154 (setq pats (cdr pats))))))
155 ;; Either return the currently known list, or read it again from
156 ;; disk
157 (if (or (and no-check-p (cadr file-alist))
158 (not (or muse-under-windows-p
159 (null (cddr file-alist))
160 (null last-mod)
161 (muse-time-less-p (cddr file-alist) last-mod))))
162 (cadr file-alist)
163 (if file-alist
164 (setcdr (cdr file-alist) last-mod)
165 (setq file-alist (cons (car project) (cons nil last-mod))
166 muse-project-file-alist
167 (cons file-alist muse-project-file-alist)))
168 ;; Read in all of the file entries
169 (save-match-data
170 (setcar
171 (cdr file-alist)
172 (let* ((names (list t))
173 (pats (cadr project)))
174 (while pats
175 (if (symbolp (car pats))
176 (setq pats (cddr pats))
177 (nconc names (muse-project-file-entries (car pats)))
178 (setq pats (cdr pats))))
179 (cdr names)))))))
181 (defun muse-project-of-file (&optional pathname)
182 "Determine which project the given PATHNAME relates to.
183 If PATHNAME is nil, the current buffer's filename is used."
184 (if (and (null pathname) muse-current-project)
185 muse-current-project
186 (when (or pathname buffer-file-name)
187 (let* ((file (file-truename (or pathname buffer-file-name)))
188 (dir (file-name-directory file))
189 (project-entry muse-project-alist)
190 found)
191 (while (and project-entry (not found))
192 (let ((pats (car (cdar project-entry))))
193 (while (and pats (not found))
194 (if (symbolp (car pats))
195 (setq pats (cddr pats))
196 (let ((truename (file-truename (car pats))))
197 (if (or (string= truename file)
198 (string= truename dir)
199 (string-match truename file))
200 (setq found (car project-entry))))
201 (setq pats (cdr pats))))
202 (setq project-entry (cdr project-entry))))
203 found))))
205 (defun muse-read-project (prompt &optional no-check-p no-assume)
206 "Read a project name from the minibuffer, if it can't be figured
207 out."
208 (if (null muse-project-alist)
209 (error "There are no Muse projects defined; see `muse-project-alist'.")
210 (or (unless no-check-p
211 (muse-project-of-file))
212 (if (and (not no-assume)
213 (= 1 (length muse-project-alist)))
214 (car muse-project-alist)
215 (assoc (completing-read prompt muse-project-alist)
216 muse-project-alist)))))
218 (defvar muse-project-page-history nil)
220 (defun muse-read-project-file (project prompt &optional default)
221 (let ((name (completing-read prompt (muse-project-file-alist project)
222 nil nil nil 'muse-project-page-history
223 default)))
224 (cons name (muse-project-page-file name project))))
226 (defun muse-project-find-file (name project &optional command directory)
227 "Open the Muse page given by NAME in PROJECT.
228 If COMMAND is non-nil, it is the function used to visit the file.
229 If DIRECTORY is non-nil, it is the directory in which the page
230 will be created if it does not already exist. Otherwise, the
231 first directory within the project's fileset is used."
232 (interactive
233 (let* ((project (muse-read-project "Find in project: "
234 current-prefix-arg))
235 (default (muse-get-keyword :default (cadr project)))
236 (entry (muse-read-project-file
237 project (if default
238 (format "Find page: (default: %s) "
239 default)
240 "Find page: ")
241 default)))
242 (list entry project)))
243 (setq project (muse-project project))
244 (let ((project-name (car project)))
245 (unless (interactive-p)
246 (setq project (muse-project project)
247 name (cons name (muse-project-page-file name project))))
248 ;; At this point, name is (PAGE . FILE).
249 (unless (cdr name)
250 (let ((pats (cadr project)))
251 (while (and pats (null directory))
252 (if (symbolp (car pats))
253 (setq pats (cddr pats))
254 (if (file-directory-p (car pats))
255 (setq directory (car pats) pats nil)
256 (setq pats (cdr pats))))))
257 (when directory
258 (let ((filename (expand-file-name (car name) directory)))
259 (unless (file-exists-p directory)
260 (make-directory directory t))
261 (setcdr name filename))))
262 ;; Open the file
263 (if (cdr name)
264 (funcall (or command 'find-file) (cdr name))
265 (error "There is no page %s in project %s."
266 (car name) project-name))))
268 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
269 (let (published)
270 (dolist (style styles)
271 (let ((include-regexp (muse-style-element :include style))
272 (exclude-regexp (muse-style-element :exclude style)))
273 (when (and (or ignore-regexp
274 (and (null include-regexp)
275 (null exclude-regexp))
276 (if include-regexp
277 (string-match include-regexp file)
278 (not (string-match exclude-regexp file))))
279 (not (muse-project-private-p file)))
280 ;; ensure the publishing location is available
281 (let ((output-dir (muse-style-element :path style)))
282 (unless (file-exists-p output-dir)
283 (message "Creating publishing directory %s" output-dir)
284 (make-directory output-dir))
285 ;; publish the member file!
286 (if (muse-publish-file file style output-dir force)
287 (setq published t))))))
288 published))
290 (defun muse-project-save-buffers (&optional project)
291 (setq project (muse-project project))
292 (map-y-or-n-p
293 (function
294 (lambda (buffer)
295 (and (buffer-modified-p buffer)
296 (not (buffer-base-buffer buffer))
297 (or (buffer-file-name buffer)
298 (progn
299 (set-buffer buffer)
300 (and buffer-offer-save
301 (> (buffer-size) 0))))
302 (with-current-buffer buffer
303 (let ((proj (muse-project-of-file)))
304 (and proj (string= (car proj)
305 (car project)))))
306 (if (buffer-file-name buffer)
307 (format "Save file %s? "
308 (buffer-file-name buffer))
309 (format "Save buffer %s? "
310 (buffer-name buffer))))))
311 (function
312 (lambda (buffer)
313 (set-buffer buffer)
314 (save-buffer)))
315 (buffer-list)
316 '("buffer" "buffers" "save")
317 (if (boundp 'save-some-buffers-action-alist)
318 save-some-buffers-action-alist)))
320 (defun muse-project-publish (project &optional force)
321 "Publish the pages of PROJECT that need publishing."
322 (interactive (list (muse-read-project "Publish project: " nil t)
323 current-prefix-arg))
324 (setq project (muse-project project))
325 (let ((styles (cddr project))
326 (muse-current-project project)
327 published)
328 ;; determine the style from the project, or else ask
329 (unless styles
330 (setq styles (list (muse-publish-get-style))))
331 ;; prompt to save any buffers related to this project
332 (muse-project-save-buffers project)
333 ;; run hook before publishing begins
334 (run-hook-with-args 'muse-before-project-publish-hook project)
335 ;; publish all files in the project, for each style; the actual
336 ;; publishing will only happen if the files are newer than the
337 ;; last published output
338 (dolist (pair (muse-project-file-alist project))
339 (if (muse-project-publish-file (cdr pair) styles force)
340 (setq published t)))
341 ;; run hook after publishing ends
342 (run-hook-with-args 'muse-after-project-publish-hook project)
343 ;; notify the user that everything is now done
344 (if published
345 (message "All pages in %s have been published." (car project))
346 (message "No pages in %s need publishing at this time."
347 (car project)))))
349 (defun muse-project-batch-publish ()
350 "Publish Muse files in batch mode."
351 (let ((muse-batch-publishing-p t)
352 force)
353 (if (string= "--force" (car command-line-args-left))
354 (setq force t
355 command-line-args-left (cdr command-line-args-left)))
356 (dolist (project command-line-args-left)
357 (message "Publishing project %s ..." project)
358 (muse-project-publish project force))))
360 (eval-when-compile
361 (put 'make-local-hook 'byte-compile nil))
363 (defun muse-project-set-variables ()
364 "Load project-specific variables."
365 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
366 sym custom-set var)
367 (while vars
368 (setq sym (car vars))
369 (setq custom-set (or (get sym 'custom-set) 'set))
370 (setq var (if (eq (get sym 'custom-type) 'hook)
371 (make-local-hook sym)
372 (make-local-variable sym)))
373 (funcall custom-set var (car (cdr vars)))
374 (setq vars (cdr (cdr vars))))))
376 (defun muse-project-delete-output-files (project)
377 (interactive
378 (list (muse-read-project "Remove all output files for project: " nil t)))
379 (setq project (muse-project project))
380 (let ((file-alist (muse-project-file-alist project))
381 (styles (cddr project))
382 output-file path)
383 (dolist (entry file-alist)
384 (dolist (style styles)
385 (setq output-file
386 (and (setq path (muse-style-element :path style))
387 (expand-file-name
388 (concat (muse-style-element :prefix style)
389 (car entry)
390 (or (muse-style-element :osuffix style)
391 (muse-style-element :suffix style)))
392 path)))
393 (if output-file
394 (muse-delete-file-if-exists output-file))))))
396 (provide 'muse-project)
398 ;;; muse-project.el ends here