1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Muse Project Maintainance
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 (require 'muse-publish
)
10 (defgroup muse-project nil
11 "Options controlling the behaviour of Muse project handling."
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)"
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)"
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
)
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."
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."
50 (or muse-current-project
51 (muse-project-of-file))
53 (assoc project muse-project-alist
)
54 (muse-assert (consp 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
))
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
))
79 (cons (cons (muse-page-name file
) file
) nil
))
80 (setq lnames
(cdr lnames
)))))
81 ((file-readable-p path
)
83 (cons (cons (muse-page-name path
) path
) nil
))
84 (setq lnames
(cdr lnames
)))
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
)
91 (cons (cons (muse-page-name file
) file
) nil
))
92 (setq lnames
(cdr lnames
))))))
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
100 (setq project
(muse-project project
))
101 (let ((file-alist (assoc (car project
) muse-project-file-alist
))
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
)))
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
)))
113 (file-name-directory (car pats
)))
114 (file-name-directory (car pats
))))))
116 (let ((mod-time (nth 5 (file-attributes dir
))))
117 (if (or (null last-mod
)
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
124 (if (or (and no-check-p
(cadr file-alist
))
125 (not (or muse-under-windows-p
126 (null (cddr file-alist
))
128 (muse-time-less-p (cddr file-alist
) last-mod
))))
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
139 (let* ((names (list t
))
140 (pats (cadr project
)))
142 (if (symbolp (car pats
))
143 (setq pats
(cddr pats
))
144 (nconc names
(muse-project-file-entries (car pats
)))
145 (setq pats
(cdr pats
))))
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
)
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
)
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
))))
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
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
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."
200 (let* ((project (muse-read-project "Find in project: "
202 (default (muse-get-keyword :default
(cadr project
)))
203 (entry (muse-read-project-file
205 (format "Find page: (default: %s) "
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).
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
))))))
225 (let ((filename (expand-file-name (car name
) directory
)))
226 (unless (file-exists-p directory
)
227 (make-directory directory t
))
228 (setcdr name filename
))))
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
)
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
))
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
))))))
257 (defun muse-project-save-buffers (&optional project
)
258 (setq project
(muse-project project
))
262 (and (buffer-modified-p buffer
)
263 (not (buffer-base-buffer buffer
))
264 (or (buffer-file-name 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
)
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
))))))
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
)
291 (setq project
(muse-project project
))
292 (let ((styles (cddr project
))
293 (muse-current-project project
)
295 ;; determine the style from the project, or else ask
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
)
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
312 (message "All pages in %s have been published." (car project
))
313 (message "No pages in %s need publishing at this time."
316 (defun muse-project-batch-publish ()
317 "Publish Muse files in batch mode."
318 (let ((muse-batch-publishing-p t
)
320 (if (string= "--force" (car command-line-args-left
))
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
))))
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
)))
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)
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
))
350 (dolist (entry file-alist
)
351 (dolist (style styles
)
353 (and (setq path
(muse-style-element :path style
))
355 (concat (muse-style-element :prefix style
)
357 (or (muse-style-element :osuffix style
)
358 (muse-style-element :suffix style
)))
361 (muse-delete-file-if-exists output-file
))))))
363 (provide 'muse-project
)
365 ;;; muse-project.el ends here