Always end the published document with a newline.
[muse-el.git] / muse-project.el
blobc4320393605b28cc1fadc43494c6d4d7cb5c0bf8
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 (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)))
97 t)))
99 (defun muse-project-file-entries (path)
100 (let* ((names (list t))
101 (lnames names)
102 file)
103 (cond
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))
108 (setcdr lnames
109 (cons (cons (muse-page-name file) file) nil))
110 (setq lnames (cdr lnames)))))
111 ((file-readable-p path)
112 (setcdr lnames
113 (cons (cons (muse-page-name path) path) nil))
114 (setq lnames (cdr lnames)))
115 (t ; regexp
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)
120 (setcdr lnames
121 (cons (cons (muse-page-name file) file) nil))
122 (setq lnames (cdr lnames))))))
123 (cdr names)))
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
129 disk."
130 (setq project (muse-project project))
131 (let ((file-alist (assoc (car project) muse-project-file-alist))
132 last-mod)
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)))
137 (while pats
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)))
142 (file-directory-p
143 (file-name-directory (car pats)))
144 (file-name-directory (car pats))))))
145 (if dir
146 (let ((mod-time (nth 5 (file-attributes dir))))
147 (if (or (null last-mod)
148 (and mod-time
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
153 ;; disk
154 (if (or (and no-check-p (cadr file-alist))
155 (not (or muse-under-windows-p
156 (null (cddr file-alist))
157 (null last-mod)
158 (muse-time-less-p (cddr file-alist) last-mod))))
159 (cadr file-alist)
160 (if file-alist
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
166 (save-match-data
167 (setcar
168 (cdr file-alist)
169 (let* ((names (list t))
170 (pats (cadr project)))
171 (while pats
172 (if (symbolp (car pats))
173 (setq pats (cddr pats))
174 (nconc names (muse-project-file-entries (car pats)))
175 (setq pats (cdr pats))))
176 (cdr names)))))))
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)
182 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)
187 found)
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))))
200 found))))
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
204 out."
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
220 default)))
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."
229 (interactive
230 (let* ((project (muse-read-project "Find in project: "
231 current-prefix-arg))
232 (default (muse-get-keyword :default (cadr project)))
233 (entry (muse-read-project-file
234 project (if default
235 (format "Find page: (default: %s) "
236 default)
237 "Find page: ")
238 default)))
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).
246 (unless (cdr name)
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))))))
254 (when directory
255 (let ((filename (expand-file-name (car name) directory)))
256 (unless (file-exists-p directory)
257 (make-directory directory t))
258 (setcdr name filename))))
259 ;; Open the file
260 (if (cdr name)
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)
266 (let (published)
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))
273 (if include-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))))))
285 published))
287 (defun muse-project-save-buffers (&optional project)
288 (setq project (muse-project project))
289 (map-y-or-n-p
290 (function
291 (lambda (buffer)
292 (and (buffer-modified-p buffer)
293 (not (buffer-base-buffer buffer))
294 (or (buffer-file-name buffer)
295 (progn
296 (set-buffer 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)
302 (car project)))))
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))))))
308 (function
309 (lambda (buffer)
310 (set-buffer buffer)
311 (save-buffer)))
312 (buffer-list)
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)
320 current-prefix-arg))
321 (setq project (muse-project project))
322 (let ((styles (cddr project))
323 (muse-current-project project)
324 published)
325 ;; determine the style from the project, or else ask
326 (unless styles
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)
337 (setq published t)))
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
341 (if published
342 (message "All pages in %s have been published." (car project))
343 (message "No pages in %s need publishing at this time."
344 (car project)))))
346 (defun muse-project-batch-publish ()
347 "Publish Muse files in batch mode."
348 (let ((muse-batch-publishing-p t)
349 force)
350 (if (string= "--force" (car command-line-args-left))
351 (setq force t
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))))
357 (eval-when-compile
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)))
363 sym custom-set var)
364 (while vars
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)
374 (interactive
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))
379 output-file path)
380 (dolist (entry file-alist)
381 (dolist (style styles)
382 (setq output-file
383 (and (setq path (muse-style-element :path style))
384 (expand-file-name
385 (concat (muse-style-element :prefix style)
386 (car entry)
387 (or (muse-style-element :osuffix style)
388 (muse-style-element :suffix style)))
389 path)))
390 (if output-file
391 (muse-delete-file-if-exists output-file))))))
393 (provide 'muse-project)
395 ;;; muse-project.el ends here