Apply latest journal-related updates from johnw
[muse-el.git] / muse-project.el
blob37be5f77f7b63c9f795e8034eb5e4d70bb20abfa
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Muse Project Maintainance
4 ;;
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 (require 'muse)
8 (require 'muse-publish)
10 (defgroup muse-project nil
11 "Options controlling the behaviour of Muse project handling."
12 :group 'muse)
14 (defcustom muse-before-project-publish-hook nil
15 "A hook run before a project is published.
16 Each function is passed the project object, a cons with the format
17 (PROJNAME . SETTINGS)"
18 :type 'hook
19 :group 'muse-project)
21 (defcustom muse-after-project-publish-hook nil
22 "A hook run after a project is published.
23 Each function is passed the project object, a cons with the format
24 (PROJNAME . SETTINGS)"
25 :type 'hook
26 :group 'muse-project)
28 (defcustom muse-project-alist nil
29 "An alist of Muse projects.
30 A project defines a fileset, and a list of custom attributes for use
31 when publishing files in that project."
32 :type '(alist :key-type string :value sexp)
33 :group 'muse-project)
35 (defvar muse-project-file-alist nil
36 "This variable is automagically constructed as needed.")
38 (defcustom muse-project-ignore-regexp
39 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
40 "A regexp matching files to be ignored in Wiki directories."
41 :type 'regexp
42 :group 'muse-project)
44 (defvar muse-current-project nil)
45 (make-variable-buffer-local 'muse-current-project)
47 (defsubst muse-project (&optional project)
48 "Resolve the given PROJECT into a full Muse project, if it is a string."
49 (if (null project)
50 (or muse-current-project
51 (muse-project-of-file))
52 (if (stringp project)
53 (assoc project muse-project-alist)
54 (muse-assert (consp project))
55 project)))
57 (defsubst muse-project-page-file (page project &optional no-check-p)
58 "Return a filename if PAGE exists within the given Muse PROJECT."
59 (setq project (muse-project project))
60 (cdr (assoc page (muse-project-file-alist project no-check-p))))
62 (defun muse-project-private-p (file)
63 "Return non-nil if NAME is a private page with PROJECT."
64 (unless muse-under-windows-p
65 (setq file (file-truename file))
66 (or (eq ?- (aref (nth 8 (file-attributes (file-name-directory file))) 7))
67 (eq ?- (aref (nth 8 (file-attributes file)) 7)))))
69 (defun muse-project-file-entries (path)
70 (let* ((names (list t))
71 (lnames names)
72 file)
73 (cond
74 ((file-directory-p path)
75 (dolist (file (directory-files path t))
76 (unless (or (string-match muse-project-ignore-regexp file)
77 (file-directory-p file))
78 (setcdr lnames
79 (cons (cons (muse-page-name file) file) nil))
80 (setq lnames (cdr lnames)))))
81 ((file-readable-p path)
82 (setcdr lnames
83 (cons (cons (muse-page-name path) path) nil))
84 (setq lnames (cdr lnames)))
85 (t ; regexp
86 (dolist (file (directory-files
87 (file-name-directory path) t
88 (file-name-nondirectory path)))
89 (unless (string-match muse-project-ignore-regexp file)
90 (setcdr lnames
91 (cons (cons (muse-page-name file) file) nil))
92 (setq lnames (cdr lnames))))))
93 (cdr names)))
95 (defun muse-project-file-alist (&optional project no-check-p)
96 "Return member filenames for the given Muse PROJECT.
97 On UNIX, this list is only updated if one of the directories'
98 contents have changed. On Windows, it is always reread from
99 disk."
100 (setq project (muse-project project))
101 (let ((file-alist (assoc (car project) muse-project-file-alist))
102 last-mod)
103 ;; Determine the last modified of any directory mentioned in the
104 ;; project's pattern list
105 (unless (or muse-under-windows-p no-check-p)
106 (let ((pats (cadr project)))
107 (while pats
108 (if (symbolp (car pats))
109 (setq pats (cddr pats))
110 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
111 (and (not (file-readable-p (car pats)))
112 (file-directory-p
113 (file-name-directory (car pats)))
114 (file-name-directory (car pats))))))
115 (if dir
116 (let ((mod-time (nth 5 (file-attributes dir))))
117 (if (or (null last-mod)
118 (and mod-time
119 (muse-time-less-p last-mod mod-time)))
120 (setq last-mod mod-time)))))
121 (setq pats (cdr pats))))))
122 ;; Either return the currently known list, or read it again from
123 ;; disk
124 (if (or (and no-check-p (cadr file-alist))
125 (not (or muse-under-windows-p
126 (null (cddr file-alist))
127 (null last-mod)
128 (muse-time-less-p (cddr file-alist) last-mod))))
129 (cadr file-alist)
130 (if file-alist
131 (setcdr (cdr file-alist) last-mod)
132 (setq file-alist (cons (car project) (cons nil last-mod))
133 muse-project-file-alist
134 (cons file-alist muse-project-file-alist)))
135 ;; Read in all of the file entries
136 (save-match-data
137 (setcar
138 (cdr file-alist)
139 (let* ((names (list t))
140 (pats (cadr project)))
141 (while pats
142 (if (symbolp (car pats))
143 (setq pats (cddr pats))
144 (nconc names (muse-project-file-entries (car pats)))
145 (setq pats (cdr pats))))
146 (cdr names)))))))
148 (defun muse-project-of-file (&optional pathname)
149 "Determine which project the given PATHNAME relates to.
150 If PATHNAME is nil, the current buffer's filename is used."
151 (if (and (null pathname) muse-current-project)
152 muse-current-project
153 (when (or pathname buffer-file-name)
154 (let* ((file (file-truename (or pathname buffer-file-name)))
155 (dir (file-name-directory file))
156 (project-entry muse-project-alist)
157 found)
158 (while (and project-entry (not found))
159 (let ((pats (car (cdar project-entry))))
160 (while (and pats (not found))
161 (if (symbolp (car pats))
162 (setq pats (cddr pats))
163 (let ((truename (file-truename (car pats))))
164 (if (or (string= truename file)
165 (string= truename dir)
166 (string-match truename file))
167 (setq found (car project-entry))))
168 (setq pats (cdr pats))))
169 (setq project-entry (cdr project-entry))))
170 found))))
172 (defun muse-read-project (prompt &optional no-check-p no-assume)
173 "Read a project name from the minibuffer, if it can't be figured
174 out."
175 (if (null muse-project-alist)
176 (error "There are no Muse projects defined; see `muse-project-alist'.")
177 (or (unless no-check-p
178 (muse-project-of-file))
179 (if (and (not no-assume)
180 (= 1 (length muse-project-alist)))
181 (car muse-project-alist)
182 (assoc (completing-read prompt muse-project-alist)
183 muse-project-alist)))))
185 (defvar muse-project-page-history nil)
187 (defun muse-read-project-file (project prompt &optional default)
188 (let ((name (completing-read prompt (muse-project-file-alist project)
189 nil nil nil 'muse-project-page-history
190 default)))
191 (cons name (muse-project-page-file name project))))
193 (defun muse-project-find-file (name project &optional command directory)
194 "Open the Muse page given by NAME in PROJECT.
195 If COMMAND is non-nil, it is the function used to visit the file.
196 If DIRECTORY is non-nil, it is the directory in which the page
197 will be created if it does not already exist. Otherwise, the
198 first directory within the project's fileset is used."
199 (interactive
200 (let* ((project (muse-read-project "Find in project: "
201 current-prefix-arg))
202 (default (muse-get-keyword :default (cadr project)))
203 (entry (muse-read-project-file
204 project (if default
205 (format "Find page: (default: %s) "
206 default)
207 "Find page: ")
208 default)))
209 (list entry project)))
210 (setq project (muse-project project))
211 (let ((project-name (car project)))
212 (unless (interactive-p)
213 (setq project (muse-project project)
214 name (cons name (muse-project-page-file name project))))
215 ;; At this point, name is (PAGE . FILE).
216 (unless (cdr name)
217 (let ((pats (cadr project)))
218 (while (and pats (null directory))
219 (if (symbolp (car pats))
220 (setq pats (cddr pats))
221 (if (file-directory-p (car pats))
222 (setq directory (car pats) pats nil)
223 (setq pats (cdr pats))))))
224 (when directory
225 (let ((filename (expand-file-name (car name) directory)))
226 (unless (file-exists-p directory)
227 (make-directory directory t))
228 (setcdr name filename))))
229 ;; Open the file
230 (if (cdr name)
231 (funcall (or command 'find-file) (cdr name))
232 (error "There is no page %s in project %s."
233 (car name) project-name))))
235 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
236 (let (published)
237 (dolist (style styles)
238 (let ((include-regexp (muse-style-element :include style))
239 (exclude-regexp (muse-style-element :exclude style)))
240 (when (and (or ignore-regexp
241 (and (null include-regexp)
242 (null exclude-regexp))
243 (if include-regexp
244 (string-match include-regexp file)
245 (not (string-match exclude-regexp file))))
246 (not (muse-project-private-p file)))
247 ;; ensure the publishing location is available
248 (let ((output-dir (muse-style-element :path style)))
249 (unless (file-exists-p output-dir)
250 (message "Creating publishing directory %s" output-dir)
251 (make-directory output-dir))
252 ;; publish the member file!
253 (if (muse-publish-file file style output-dir force)
254 (setq published t))))))
255 published))
257 (defun muse-project-save-buffers (&optional project)
258 (setq project (muse-project project))
259 (map-y-or-n-p
260 (function
261 (lambda (buffer)
262 (and (buffer-modified-p buffer)
263 (not (buffer-base-buffer buffer))
264 (or (buffer-file-name buffer)
265 (progn
266 (set-buffer buffer)
267 (and buffer-offer-save
268 (> (buffer-size) 0))))
269 (with-current-buffer buffer
270 (let ((proj (muse-project-of-file)))
271 (and proj (string= (car proj)
272 (car project)))))
273 (if (buffer-file-name buffer)
274 (format "Save file %s? "
275 (buffer-file-name buffer))
276 (format "Save buffer %s? "
277 (buffer-name buffer))))))
278 (function
279 (lambda (buffer)
280 (set-buffer buffer)
281 (save-buffer)))
282 (buffer-list)
283 '("buffer" "buffers" "save")
284 (if (boundp 'save-some-buffers-action-alist)
285 save-some-buffers-action-alist)))
287 (defun muse-project-publish (project &optional force)
288 "Publish the pages of PROJECT that need publishing."
289 (interactive (list (muse-read-project "Publish project: " nil t)
290 current-prefix-arg))
291 (setq project (muse-project project))
292 (let ((styles (cddr project))
293 (muse-current-project project)
294 published)
295 ;; determine the style from the project, or else ask
296 (unless styles
297 (setq styles (list (muse-publish-get-style))))
298 ;; prompt to save any buffers related to this project
299 (muse-project-save-buffers project)
300 ;; run hook before publishing begins
301 (run-hook-with-args 'muse-before-project-publish-hook project)
302 ;; publish all files in the project, for each style; the actual
303 ;; publishing will only happen if the files are newer than the
304 ;; last published output
305 (dolist (pair (muse-project-file-alist project))
306 (if (muse-project-publish-file (cdr pair) styles force)
307 (setq published t)))
308 ;; run hook after publishing ends
309 (run-hook-with-args 'muse-after-project-publish-hook project)
310 ;; notify the user that everything is now done
311 (if published
312 (message "All pages in %s have been published." (car project))
313 (message "No pages in %s need publishing at this time."
314 (car project)))))
316 (defun muse-project-batch-publish ()
317 "Publish Muse files in batch mode."
318 (let ((muse-batch-publishing-p t)
319 force)
320 (if (string= "--force" (car command-line-args-left))
321 (setq force t
322 command-line-args-left (cdr command-line-args-left)))
323 (dolist (project command-line-args-left)
324 (message "Publishing project %s ..." project)
325 (muse-project-publish project force))))
327 (eval-when-compile
328 (put 'make-local-hook 'byte-compile nil))
330 (defun muse-project-set-variables ()
331 "Load project-specific variables."
332 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
333 sym custom-set var)
334 (while vars
335 (setq sym (car vars))
336 (setq custom-set (or (get sym 'custom-set) 'set))
337 (setq var (if (eq (get sym 'custom-type) 'hook)
338 (make-local-hook sym)
339 (make-local-variable sym)))
340 (funcall custom-set var (car (cdr vars)))
341 (setq vars (cdr (cdr vars))))))
343 (defun muse-project-delete-output-files (project)
344 (interactive
345 (list (muse-read-project "Remove all output files for project: " nil t)))
346 (setq project (muse-project project))
347 (let ((file-alist (muse-project-file-alist project))
348 (styles (cddr project))
349 output-file path)
350 (dolist (entry file-alist)
351 (dolist (style styles)
352 (setq output-file
353 (and (setq path (muse-style-element :path style))
354 (expand-file-name
355 (concat (muse-style-element :prefix style)
356 (car entry)
357 (or (muse-style-element :osuffix style)
358 (muse-style-element :suffix style)))
359 path)))
360 (if output-file
361 (muse-delete-file-if-exists output-file))))))
363 (provide 'muse-project)
365 ;;; muse-project.el ends here