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