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