Fix compiler warning induced by last patch.
[muse-el.git] / lisp / muse-project.el
blob9f9a74ff3f4681e8e8912c0f2e48cfc14d9ddfac
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 "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."
77 (if (null project)
78 (or muse-current-project
79 (muse-project-of-file))
80 (if (stringp project)
81 (assoc project muse-project-alist)
82 (muse-assert (consp project))
83 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))
97 (message (concat
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)))
102 t)))
104 (defun muse-project-file-entries (path)
105 (let* ((names (list t))
106 (lnames names))
107 (cond
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))
112 (setcdr lnames
113 (cons (cons (muse-page-name file) file) nil))
114 (setq lnames (cdr lnames)))))
115 ((file-readable-p path)
116 (setcdr lnames
117 (cons (cons (muse-page-name path) path) nil))
118 (setq lnames (cdr lnames)))
119 (t ; regexp
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)
124 (setcdr lnames
125 (cons (cons (muse-page-name file) file) nil))
126 (setq lnames (cdr lnames))))))
127 (cdr names)))
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
133 disk."
134 (setq project (muse-project project))
135 (let ((file-alist (assoc (car project) muse-project-file-alist))
136 last-mod)
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)))
141 (while pats
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)))
146 (file-directory-p
147 (file-name-directory (car pats)))
148 (file-name-directory (car pats))))))
149 (if dir
150 (let ((mod-time (nth 5 (file-attributes dir))))
151 (if (or (null last-mod)
152 (and mod-time
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
157 ;; disk
158 (if (or (and no-check-p (cadr file-alist))
159 (not (or muse-under-windows-p
160 (null (cddr file-alist))
161 (null last-mod)
162 (muse-time-less-p (cddr file-alist) last-mod))))
163 (cadr file-alist)
164 (if file-alist
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
170 (save-match-data
171 (setcar
172 (cdr file-alist)
173 (let* ((names (list t))
174 (pats (cadr project)))
175 (while pats
176 (if (symbolp (car pats))
177 (setq pats (cddr pats))
178 (nconc names (muse-project-file-entries (car pats)))
179 (setq pats (cdr pats))))
180 (cdr names)))))))
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)
186 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)
191 found)
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))))
204 found))))
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
208 out."
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
224 default)))
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."
233 (interactive
234 (let* ((project (muse-read-project "Find in project: "
235 current-prefix-arg))
236 (default (muse-get-keyword :default (cadr project)))
237 (entry (muse-read-project-file
238 project (if default
239 (format "Find page: (default: %s) "
240 default)
241 "Find page: ")
242 default)))
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).
250 (unless (cdr name)
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))))))
258 (when directory
259 (let ((filename (expand-file-name (car name) directory)))
260 (unless (file-exists-p directory)
261 (make-directory directory t))
262 (setcdr name filename))))
263 ;; Open the file
264 (if (cdr name)
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)
270 (let (published)
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))
277 (if include-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))))))
289 published))
291 (defun muse-project-save-buffers (&optional project)
292 (setq project (muse-project project))
293 (map-y-or-n-p
294 (function
295 (lambda (buffer)
296 (and (buffer-modified-p buffer)
297 (not (buffer-base-buffer buffer))
298 (or (buffer-file-name buffer)
299 (progn
300 (set-buffer 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)
306 (car project)))))
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))))))
312 (function
313 (lambda (buffer)
314 (set-buffer buffer)
315 (save-buffer)))
316 (buffer-list)
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)
324 current-prefix-arg))
325 (setq project (muse-project project))
326 (let ((styles (cddr project))
327 (muse-current-project project)
328 published)
329 ;; determine the style from the project, or else ask
330 (unless styles
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)
341 (setq published t)))
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
345 (if published
346 (message "All pages in %s have been published." (car project))
347 (message "No pages in %s need publishing at this time."
348 (car project)))))
350 (defun muse-project-batch-publish ()
351 "Publish Muse files in batch mode."
352 (let ((muse-batch-publishing-p t)
353 force)
354 (if (string= "--force" (car command-line-args-left))
355 (setq force t
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))))
361 (eval-when-compile
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)))
367 sym custom-set var)
368 (while vars
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)
378 (interactive
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))
383 output-file path)
384 (dolist (entry file-alist)
385 (dolist (style styles)
386 (setq output-file
387 (and (setq path (muse-style-element :path style))
388 (expand-file-name
389 (concat (muse-style-element :prefix style)
390 (car entry)
391 (or (muse-style-element :osuffix style)
392 (muse-style-element :suffix style)))
393 path)))
394 (if output-file
395 (muse-delete-file-if-exists output-file))))))
397 (provide 'muse-project)
399 ;;; muse-project.el ends here