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