Improve debian build and put together a package.
[muse-el.git] / lisp / muse-project.el
blob236f92d17af24e0cc7552d4361e8893a8b99b313
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 (defun muse-project-alist-get (sym)
56 "Turn `muse-project-alist' into something we can customize easily."
57 (when (boundp sym)
58 (let* ((val (copy-alist (symbol-value sym)))
59 (head val))
60 (while val
61 (let ((head (car (cdar val)))
62 res)
63 (while head
64 (cond ((stringp (car head))
65 (add-to-list 'res (car head))
66 (setq head (cdr head)))
67 ((symbolp (car head))
68 (add-to-list 'res (list (car head) (cadr head)) t)
69 (setq head (cddr head)))
71 (setq head (cdr head)))))
72 (setcdr (car val) (cons res (cdr (cdar val)))))
73 (setq val (cdr val)))
74 head)))
76 (defun muse-project-alist-set (sym val)
77 "Turn customized version of `muse-project-alist' into something
78 Muse can make use of."
79 (set sym val)
80 (while val
81 (let ((head (car (cdar val)))
82 res)
83 (while head
84 (cond ((stringp (car head))
85 (add-to-list 'res (car head) t))
86 ((consp (car head))
87 (add-to-list 'res (caar head) t)
88 (add-to-list 'res (car (cdar head)) t)))
89 (setq head (cdr head)))
90 (setcdr (car val) (cons res (cdr (cdar val)))))
91 (setq val (cdr val))))
93 (define-widget 'muse-project 'lazy
94 "A widget that defines a Muse project."
95 :tag "Project"
96 :type '(cons (repeat :tag "Settings"
97 (choice
98 (string :tag "Directory")
99 (list :tag "Book function"
100 (const :book-funcall)
101 (choice (function)
102 (sexp :tag "Unknown")))
103 (list :tag "Book part"
104 (const :book-part)
105 (string :tag "Name"))
106 (list :tag "Book style"
107 (const :book-style)
108 (string :tag "Style"))
109 (list :tag "Default file"
110 (const :default)
111 (string :tag "File"))
112 (list :tag "End of book"
113 (const :book-end)
114 (const t))
115 (list :tag "Force publishing"
116 (const :force-publish)
117 (repeat (string :tag "File")))
118 (list :tag "Major mode"
119 (const :major-mode)
120 (choice (function :tag "Mode")
121 (sexp :tag "Unknown")))
122 (list :tag "New chapter"
123 (const :book-chapter)
124 (string :tag "Name"))
125 (list :tag "No chapters"
126 (const :nochapters)
127 (const t))
128 (list :tag "Set variables"
129 (const :set)
130 (repeat (list (symbol :tag "Variable")
131 (sexp :tag "Setting"))))
132 (list :tag "Visit links using"
133 (const :visit-link)
134 (choice (function)
135 (sexp :tag "Unknown")))))
136 (repeat :tag "Styles"
137 (repeat :tag "Style" :inline t
138 (set :tag "Setting"
139 (list :inline t
140 :tag "Publishing style"
141 (const :base)
142 (string :tag "Style"))
143 (list :inline t
144 :tag "Base URL"
145 (const :base-url)
146 (string :tag "URL"))
147 (list :inline t
148 :tag "Exclude matching"
149 (const :exclude)
150 (regexp))
151 (list :inline t
152 :tag "Include matching"
153 (const :include)
154 (regexp))
155 (list :inline t
156 :tag "Path"
157 (const :path)
158 (string :tag "Path")))))))
160 (defcustom muse-project-alist nil
161 "An alist of Muse projects.
162 A project defines a fileset, and a list of custom attributes for use
163 when publishing files in that project."
164 :type '(alist
165 :key-type (string :tag "Project name")
166 :value-type muse-project)
167 :get 'muse-project-alist-get
168 :set 'muse-project-alist-set
169 :group 'muse-project)
171 (defvar muse-project-file-alist nil
172 "This variable is automagically constructed as needed.")
174 (defcustom muse-project-ignore-regexp
175 "\\`\\(\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\)\\'"
176 "A regexp matching files to be ignored in Wiki directories."
177 :type 'regexp
178 :group 'muse-regexp)
180 (defvar muse-current-project nil
181 "Project we are currently visiting.")
182 (make-variable-buffer-local 'muse-current-project)
184 (defsubst muse-project (&optional project)
185 "Resolve the given PROJECT into a full Muse project, if it is a string."
186 (if (null project)
187 (or muse-current-project
188 (muse-project-of-file))
189 (if (stringp project)
190 (assoc project muse-project-alist)
191 (muse-assert (consp project))
192 project)))
194 (defsubst muse-project-page-file (page project &optional no-check-p)
195 "Return a filename if PAGE exists within the given Muse PROJECT."
196 (setq project (muse-project project))
197 (cdr (assoc page (muse-project-file-alist project no-check-p))))
199 (defun muse-project-private-p (file)
200 "Return non-nil if NAME is a private page with PROJECT."
201 (unless muse-under-windows-p
202 (setq file (file-truename file))
203 (if (file-attributes file) ; don't publish if no attributes exist
204 (or (when (eq ?- (aref (nth 8 (file-attributes
205 (file-name-directory file))) 7))
206 (message (concat
207 "The " (file-name-directory file)
208 " directory must be readable by others"
209 " in order for its contents to be published.")))
210 (eq ?- (aref (nth 8 (file-attributes file)) 7)))
211 t)))
213 (defun muse-project-file-entries (path)
214 (let* ((names (list t))
215 (lnames names))
216 (cond
217 ((file-directory-p path)
218 (dolist (file (directory-files
219 path t (when muse-file-extension
220 (concat "." muse-file-extension "\\'"))))
221 (unless (or (string-match muse-project-ignore-regexp file)
222 (file-directory-p file))
223 (setcdr lnames
224 (cons (cons (muse-page-name file) file) nil))
225 (setq lnames (cdr lnames)))))
226 ((file-readable-p path)
227 (setcdr lnames
228 (cons (cons (muse-page-name path) path) nil))
229 (setq lnames (cdr lnames)))
230 (t ; regexp
231 (muse-assert (file-name-directory path))
232 (dolist (file (directory-files
233 (file-name-directory path) t
234 (file-name-nondirectory path)))
235 (unless (string-match muse-project-ignore-regexp file)
236 (setcdr lnames
237 (cons (cons (muse-page-name file) file) nil))
238 (setq lnames (cdr lnames))))))
239 (cdr names)))
241 (defun muse-project-file-alist (&optional project no-check-p)
242 "Return member filenames for the given Muse PROJECT.
243 On UNIX, this list is only updated if one of the directories'
244 contents have changed. On Windows, it is always reread from
245 disk."
246 (setq project (muse-project project))
247 (let ((file-alist (assoc (car project) muse-project-file-alist))
248 last-mod)
249 ;; Determine the last modified of any directory mentioned in the
250 ;; project's pattern list
251 (unless (or muse-under-windows-p no-check-p)
252 (let ((pats (cadr project)))
253 (while pats
254 (if (symbolp (car pats))
255 (setq pats (cddr pats))
256 (let ((dir (or (and (file-directory-p (car pats)) (car pats))
257 (and (not (file-readable-p (car pats)))
258 (file-directory-p
259 (file-name-directory (car pats)))
260 (file-name-directory (car pats))))))
261 (if dir
262 (let ((mod-time (nth 5 (file-attributes dir))))
263 (if (or (null last-mod)
264 (and mod-time
265 (muse-time-less-p last-mod mod-time)))
266 (setq last-mod mod-time)))))
267 (setq pats (cdr pats))))))
268 ;; Either return the currently known list, or read it again from
269 ;; disk
270 (if (or (and no-check-p (cadr file-alist))
271 (not (or muse-under-windows-p
272 (null (cddr file-alist))
273 (null last-mod)
274 (muse-time-less-p (cddr file-alist) last-mod))))
275 (cadr file-alist)
276 (if file-alist
277 (setcdr (cdr file-alist) last-mod)
278 (setq file-alist (cons (car project) (cons nil last-mod))
279 muse-project-file-alist
280 (cons file-alist muse-project-file-alist)))
281 ;; Read in all of the file entries
282 (save-match-data
283 (setcar
284 (cdr file-alist)
285 (let* ((names (list t))
286 (pats (cadr project)))
287 (while pats
288 (if (symbolp (car pats))
289 (setq pats (cddr pats))
290 (nconc names (muse-project-file-entries (car pats)))
291 (setq pats (cdr pats))))
292 (cdr names)))))))
294 (defun muse-project-of-file (&optional pathname)
295 "Determine which project the given PATHNAME relates to.
296 If PATHNAME is nil, the current buffer's filename is used."
297 (if (and (null pathname) muse-current-project)
298 muse-current-project
299 (setq pathname (or pathname
300 (and (boundp 'muse-publishing-current-file)
301 muse-publishing-current-file)
302 buffer-file-name))
303 (when pathname
304 (let* ((file (file-truename pathname))
305 (dir (file-name-directory file))
306 (project-entry muse-project-alist)
307 found)
308 (save-match-data
309 (while (and project-entry (not found))
310 (let ((pats (car (cdar project-entry))))
311 (while (and pats (not found))
312 (if (symbolp (car pats))
313 (setq pats (cddr pats))
314 (let ((truename (file-truename (car pats))))
315 (if (or (string= truename file)
316 (string= truename dir)
317 (string-match truename file))
318 (setq found (car project-entry))))
319 (setq pats (cdr pats))))
320 (setq project-entry (cdr project-entry)))))
321 found))))
323 (defun muse-read-project (prompt &optional no-check-p no-assume)
324 "Read a project name from the minibuffer, if it can't be figured
325 out."
326 (if (null muse-project-alist)
327 (error "There are no Muse projects defined; see `muse-project-alist'.")
328 (or (unless no-check-p
329 (muse-project-of-file))
330 (if (and (not no-assume)
331 (= 1 (length muse-project-alist)))
332 (car muse-project-alist)
333 (assoc (completing-read prompt muse-project-alist)
334 muse-project-alist)))))
336 (defvar muse-project-page-history nil)
338 (defun muse-read-project-file (project prompt &optional default)
339 (let ((name (completing-read prompt (muse-project-file-alist project)
340 nil nil nil 'muse-project-page-history
341 default)))
342 (cons name (muse-project-page-file name project))))
344 (defun muse-project-find-file (name project &optional command directory)
345 "Open the Muse page given by NAME in PROJECT.
346 If COMMAND is non-nil, it is the function used to visit the file.
347 If DIRECTORY is non-nil, it is the directory in which the page
348 will be created if it does not already exist. Otherwise, the
349 first directory within the project's fileset is used."
350 (interactive
351 (let* ((project (muse-read-project "Find in project: "
352 current-prefix-arg))
353 (default (muse-get-keyword :default (cadr project)))
354 (entry (muse-read-project-file
355 project (if default
356 (format "Find page: (default: %s) "
357 default)
358 "Find page: ")
359 default)))
360 (list entry project)))
361 (setq project (muse-project project))
362 (let ((project-name (car project)))
363 (unless (interactive-p)
364 (setq project (muse-project project)
365 name (cons name (muse-project-page-file name project))))
366 ;; If we're given a relative name, open it as-is
367 (if (and (car name)
368 (save-match-data (string-match "\\.\\./" (car name))))
369 (setcdr name (if muse-file-extension
370 (concat (car name) "." muse-file-extension)
371 (car name)))
372 ;; At this point, name is (PAGE . FILE).
373 (unless (cdr name)
374 (let ((pats (cadr project)))
375 (while (and pats (null directory))
376 (if (symbolp (car pats))
377 (setq pats (cddr pats))
378 (if (file-directory-p (car pats))
379 (setq directory (car pats) pats nil)
380 (setq pats (cdr pats))))))
381 (when directory
382 (let ((filename (expand-file-name
383 (if muse-file-extension
384 (concat (car name) "." muse-file-extension)
385 (car name))
386 directory)))
387 (unless (file-exists-p directory)
388 (make-directory directory t))
389 (setcdr name filename)))))
390 ;; Open the file
391 (if (cdr name)
392 (funcall (or command 'find-file) (cdr name))
393 (error "There is no page %s in project %s."
394 (car name) project-name))))
396 (defun muse-project-applicable-styles (file styles &optional ignore-regexp)
397 "Given STYLES, return a list of the ones that are considered for FILE.
398 The name of a project may be used for STYLES."
399 (when (stringp styles)
400 (setq styles (cddr (muse-project styles))))
401 (muse-assert (and file styles))
402 (let (used-styles)
403 (dolist (style styles)
404 (let ((include-regexp (muse-style-element :include style))
405 (exclude-regexp (muse-style-element :exclude style)))
406 (when (and (or ignore-regexp
407 (and (null include-regexp)
408 (null exclude-regexp))
409 (if include-regexp
410 (string-match include-regexp file)
411 (not (string-match exclude-regexp file))))
412 (or (not (file-exists-p file))
413 (not (muse-project-private-p file))))
414 (add-to-list 'used-styles style))))
415 used-styles))
417 (defun muse-project-publish-file (file styles &optional force ignore-regexp)
418 (setq styles (muse-project-applicable-styles file styles ignore-regexp))
419 (let (published)
420 (dolist (style styles)
421 (let ((output-dir (muse-style-element :path style)))
422 ;; ensure the publishing location is available
423 (unless (file-exists-p output-dir)
424 (message "Creating publishing directory %s" output-dir)
425 (make-directory output-dir))
426 ;; publish the member file!
427 (if (muse-publish-file file style output-dir force)
428 (setq published t))))
429 published))
431 (defun muse-project-save-buffers (&optional project)
432 (setq project (muse-project project))
433 (map-y-or-n-p
434 (function
435 (lambda (buffer)
436 (and (buffer-modified-p buffer)
437 (not (buffer-base-buffer buffer))
438 (or (buffer-file-name buffer)
439 (progn
440 (set-buffer buffer)
441 (and buffer-offer-save
442 (> (buffer-size) 0))))
443 (with-current-buffer buffer
444 (let ((proj (muse-project-of-file)))
445 (and proj (string= (car proj)
446 (car project)))))
447 (if (buffer-file-name buffer)
448 (format "Save file %s? "
449 (buffer-file-name buffer))
450 (format "Save buffer %s? "
451 (buffer-name buffer))))))
452 (function
453 (lambda (buffer)
454 (set-buffer buffer)
455 (save-buffer)))
456 (buffer-list)
457 '("buffer" "buffers" "save")
458 (if (boundp 'save-some-buffers-action-alist)
459 save-some-buffers-action-alist)))
461 (defun muse-project-publish (project &optional force)
462 "Publish the pages of PROJECT that need publishing."
463 (interactive (list (muse-read-project "Publish project: " nil t)
464 current-prefix-arg))
465 (setq project (muse-project project))
466 (let ((styles (cddr project))
467 (muse-current-project project)
468 published)
469 ;; determine the style from the project, or else ask
470 (unless styles
471 (setq styles (list (muse-publish-get-style))))
472 ;; prompt to save any buffers related to this project
473 (muse-project-save-buffers project)
474 ;; run hook before publishing begins
475 (run-hook-with-args 'muse-before-project-publish-hook project)
476 ;; publish all files in the project, for each style; the actual
477 ;; publishing will only happen if the files are newer than the
478 ;; last published output
479 (let ((forced-files (muse-get-keyword :force-publish (cadr project))))
480 (dolist (pair (muse-project-file-alist project))
481 (when (muse-project-publish-file
482 (cdr pair) styles (or force (member (car pair) forced-files)))
483 (setq published t))))
484 ;; run hook after publishing ends
485 (run-hook-with-args 'muse-after-project-publish-hook project)
486 ;; notify the user that everything is now done
487 (if published
488 (message "All pages in %s have been published." (car project))
489 (message "No pages in %s need publishing at this time."
490 (car project)))))
492 (defun muse-project-batch-publish ()
493 "Publish Muse files in batch mode."
494 (let ((muse-batch-publishing-p t)
495 force)
496 (if (string= "--force" (or (car command-line-args-left) ""))
497 (setq force t
498 command-line-args-left (cdr command-line-args-left)))
499 (if command-line-args-left
500 (dolist (project command-line-args-left)
501 (message "Publishing project %s ..." project)
502 (muse-project-publish project force))
503 (message "No projects specified."))))
505 (eval-when-compile
506 (put 'make-local-hook 'byte-compile nil))
508 (defun muse-project-set-variables ()
509 "Load project-specific variables."
510 (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
511 sym custom-set var)
512 (while vars
513 (setq sym (car vars))
514 (setq custom-set (or (get sym 'custom-set) 'set))
515 (setq var (if (eq (get sym 'custom-type) 'hook)
516 (make-local-hook sym)
517 (make-local-variable sym)))
518 (funcall custom-set var (car (cdr vars)))
519 (setq vars (cdr (cdr vars))))))
521 (defun muse-project-delete-output-files (project)
522 (interactive
523 (list (muse-read-project "Remove all output files for project: " nil t)))
524 (setq project (muse-project project))
525 (let ((file-alist (muse-project-file-alist project))
526 (styles (cddr project))
527 output-file path)
528 (dolist (entry file-alist)
529 (dolist (style styles)
530 (setq output-file
531 (and (setq path (muse-style-element :path style))
532 (expand-file-name
533 (concat (muse-style-element :prefix style)
534 (car entry)
535 (or (muse-style-element :osuffix style)
536 (muse-style-element :suffix style)))
537 path)))
538 (if output-file
539 (muse-delete-file-if-exists output-file))))))
541 (provide 'muse-project)
543 ;;; muse-project.el ends here