~ubuntu-branches/ubuntu/raring/muse-el/raring

« back to all changes in this revision

Viewing changes to lisp/muse-project.el

  • Committer: Bazaar Package Importer
  • Author(s): Michael W. Olson (GNU address)
  • Date: 2005-12-17 12:11:27 UTC
  • Revision ID: james.westby@ubuntu.com-20051217121127-b4yfr70a7hnrexdg
Tags: upstream-3.02.5
ImportĀ upstreamĀ versionĀ 3.02.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;;; muse-project.el --- handle Muse projects
 
2
 
 
3
;; Copyright (C) 2004, 2005  Free Software Foundation, Inc.
 
4
 
 
5
;; This file is not part of GNU Emacs.
 
6
 
 
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.
 
11
;;
 
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.
 
16
;;
 
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.
 
21
 
 
22
;;; Commentary:
 
23
 
 
24
;;; Contributors:
 
25
 
 
26
;;; Code:
 
27
 
 
28
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
29
;;
 
30
;; Muse Project Maintainance
 
31
;;
 
32
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
33
 
 
34
(require 'muse)
 
35
(require 'muse-publish)
 
36
 
 
37
(defgroup muse-project nil
 
38
  "Options controlling the behavior of Muse project handling."
 
39
  :group 'muse)
 
40
 
 
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)
 
47
 
 
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)
 
54
 
 
55
(defvar muse-project-alist-using-customize nil
 
56
  "Used internally by Muse to indicate whether `muse-project-alist'
 
57
has been modified via the customize interface.")
 
58
(make-variable-buffer-local 'muse-project-alist-using-customize)
 
59
 
 
60
(defmacro with-muse-project (project &rest body)
 
61
  `(progn
 
62
     (unless (muse-project ,project)
 
63
       (error "Can't find project %s" ,project))
 
64
     (with-temp-buffer
 
65
       (muse-mode)
 
66
       (setq muse-current-project (muse-project ,project))
 
67
       (muse-project-set-variables)
 
68
       ,@body)))
 
69
 
 
70
(put 'with-muse-project 'lisp-indent-function 0)
 
71
(put 'with-muse-project 'edebug-form-spec '(sexp body))
 
72
 
 
73
(defun muse-project-alist-get (sym)
 
74
  "Turn `muse-project-alist' into something we can customize easily."
 
75
  (when (boundp sym)
 
76
    (setq muse-project-alist-using-customize t)
 
77
    (let* ((val (copy-alist (symbol-value sym)))
 
78
           (head val))
 
79
      (while val
 
80
        (let ((head (car (cdar val)))
 
81
              res)
 
82
          ;; Turn settings of first part into cons cells, symbol->string
 
83
          (while head
 
84
            (cond ((stringp (car head))
 
85
                   (add-to-list 'res (car head) t)
 
86
                   (setq head (cdr head)))
 
87
                  ((symbolp (car head))
 
88
                   (add-to-list 'res (list (symbol-name (car head))
 
89
                                           (cadr head)) t)
 
90
                   (setq head (cddr head)))
 
91
                  (t
 
92
                   (setq head (cdr head)))))
 
93
          (setcdr (car val) (cons res (cdr (cdar val)))))
 
94
        (let ((styles (cdar val)))
 
95
          ;; Symbol->string in every style
 
96
          (while (cdr styles)
 
97
            (let ((head (cadr styles))
 
98
                  res)
 
99
              (while (consp head)
 
100
                (setq res (plist-put res (symbol-name (car head))
 
101
                                     (cadr head)))
 
102
                (setq head (cddr head)))
 
103
              (setcdr styles (cons res (cddr styles))))
 
104
            (setq styles (cdr styles))))
 
105
        (setq val (cdr val)))
 
106
      head)))
 
107
 
 
108
(defun muse-project-alist-set (sym val)
 
109
  "Turn customized version of `muse-project-alist' into something
 
110
Muse can make use of."
 
111
  (set sym val)
 
112
  (when muse-project-alist-using-customize
 
113
    ;; Make sure the unescaped version is written to .emacs
 
114
    (put sym 'saved-value (list (custom-quote val)))
 
115
    ;; Perform unescaping
 
116
    (while val
 
117
      (let ((head (car (cdar val)))
 
118
            res)
 
119
        ;; Turn cons cells into flat list, string->symbol
 
120
        (while head
 
121
          (cond ((stringp (car head))
 
122
                 (add-to-list 'res (car head) t))
 
123
                ((consp (car head))
 
124
                 (add-to-list 'res (intern (caar head)) t)
 
125
                 (add-to-list 'res (car (cdar head)) t)))
 
126
          (setq head (cdr head)))
 
127
        (setcdr (car val) (cons res (cdr (cdar val)))))
 
128
      (let ((styles (cdar val)))
 
129
        ;; String->symbol in every style
 
130
        (while (cdr styles)
 
131
          (let ((head (cadr styles))
 
132
                res)
 
133
            (while (consp head)
 
134
              (setq res (plist-put res (intern (car head))
 
135
                                   (cadr head)))
 
136
              (setq head (cddr head)))
 
137
            (setcdr styles (cons res (cddr styles))))
 
138
          (setq styles (cdr styles))))
 
139
      (setq val (cdr val)))))
 
140
 
 
141
(define-widget 'muse-project 'default
 
142
  "A widget that defines a Muse project."
 
143
  :format "\n%v"
 
144
  :value-create 'muse-widget-type-value-create
 
145
  :value-get 'muse-widget-child-value-get
 
146
  :value-delete 'ignore
 
147
  :match 'muse-widget-type-match
 
148
  :type '(cons :format "    %v"
 
149
               (repeat :tag "Settings" :format "%{%t%}:\n%v%i\n\n"
 
150
                       (choice
 
151
                        (string :tag "Directory")
 
152
                        (list :tag "Book function"
 
153
                              (const :tag ":book-funcall" ":book-funcall")
 
154
                              (choice (function)
 
155
                                      (sexp :tag "Unknown")))
 
156
                        (list :tag "Book part"
 
157
                              (const :tag ":book-part" ":book-part")
 
158
                              (string :tag "Name"))
 
159
                        (list :tag "Book style"
 
160
                              (const :tag ":book-style" ":book-style")
 
161
                              (string :tag "Style"))
 
162
                        (list :tag "Default file"
 
163
                              (const :tag ":default" ":default")
 
164
                              (string :tag "File"))
 
165
                        (list :tag "End of book"
 
166
                              (const :tag ":book-end" ":book-end")
 
167
                              (const t))
 
168
                        (list :tag "Force publishing"
 
169
                              (const :tag ":force-publish" ":force-publish")
 
170
                              (repeat (string :tag "File")))
 
171
                        (list :tag "Major mode"
 
172
                              (const :tag ":major-mode" ":major-mode")
 
173
                              (choice (function :tag "Mode")
 
174
                                      (sexp :tag "Unknown")))
 
175
                        (list :tag "New chapter"
 
176
                              (const :tag ":book-chapter" ":book-chapter")
 
177
                              (string :tag "Name"))
 
178
                        (list :tag "No chapters"
 
179
                              (const :tag ":nochapters" ":nochapters")
 
180
                              (const t))
 
181
                        (list :tag "Set variables"
 
182
                              (const :tag ":set" ":set")
 
183
                              (repeat (list :inline t
 
184
                                            (symbol :tag "Variable")
 
185
                                            (sexp :tag "Setting"))))
 
186
                        (list :tag "Visit links using"
 
187
                              (const :tag ":visit-link" ":visit-link")
 
188
                              (choice (function)
 
189
                                      (sexp :tag "Unknown")))))
 
190
               (repeat :tag "Styles" :format "%{%t%}:\n%v%i\n\n"
 
191
                       (set :tag "Style"
 
192
                            (list :inline t
 
193
                                  :tag "Publishing style"
 
194
                                  (const :tag ":base" ":base")
 
195
                                  (string :tag "Style"))
 
196
                            (list :inline t
 
197
                                  :tag "Base URL"
 
198
                                  (const :tag ":base-url" ":base-url")
 
199
                                  (string :tag "URL"))
 
200
                            (list :inline t
 
201
                                  :tag "Exclude matching"
 
202
                                  (const :tag ":exclude" ":exclude")
 
203
                                  (regexp))
 
204
                            (list :inline t
 
205
                                  :tag "Include matching"
 
206
                                  (const :tag ":include" ":include")
 
207
                                  (regexp))
 
208
                            (list :inline t
 
209
                                  :tag "Timestamps file"
 
210
                                  (const :tag ":timestamps" ":timestamps")
 
211
                                  (file))
 
212
                            (list :inline t
 
213
                                  :tag "Path"
 
214
                                  (const :tag ":path" ":path")
 
215
                                  (string :tag "Path"))))))
 
216
 
 
217
(defcustom muse-project-alist nil
 
218
  "An alist of Muse projects.
 
219
A project defines a fileset, and a list of custom attributes for use
 
220
when publishing files in that project."
 
221
  :type '(choice (const :tag "No projects defined." nil)
 
222
                 (repeat (cons :format "%{%t%}:\n\n%v"
 
223
                               :tag "Project" :indent 4
 
224
                               (string :tag "Project name")
 
225
                               muse-project))
 
226
                 (sexp :tag "Cannot parse expression"))
 
227
  :get 'muse-project-alist-get
 
228
  :set 'muse-project-alist-set
 
229
  :group 'muse-project)
 
230
 
 
231
;; Make it easier to specify a muse-project-alist entry
 
232
 
 
233
(defcustom muse-project-ignore-regexp
 
234
  (concat "\\`\\(.*\\.?#.*\\|.*,v\\|.*~\\|\\.\\.?\\|,.*\\)\\'\\|"
 
235
          "/\\(CVS\\|RCS\\|\\.arch-ids\\|{arch}\\|,.*\\)\\(/\\|\\'\\)")
 
236
  "A regexp matching files to be ignored in Wiki directories."
 
237
  :type 'regexp
 
238
  :group 'muse-regexp)
 
239
 
 
240
(defun muse-project-recurse-directory (base)
 
241
  "Recusively retrieve all of the directories underneath BASE.
 
242
A list of these directories is returned.
 
243
Directories starting with \".\" will be ignored, as well as those
 
244
which match `muse-project-ignore-regexp'."
 
245
  (when (and (file-directory-p base)
 
246
             (not (string-match muse-project-ignore-regexp base)))
 
247
    (let (list dir)
 
248
      (dolist (file (directory-files base t "^[^.]"))
 
249
        (when (and (file-directory-p file)
 
250
                   (not (string-match muse-project-ignore-regexp file)))
 
251
          (setq dir (file-name-nondirectory file))
 
252
          (push dir list)
 
253
          (nconc list (mapcar #'(lambda (item)
 
254
                                  (concat dir "/" item))
 
255
                              (muse-project-recurse-directory file)))))
 
256
      list)))
 
257
 
 
258
(defun muse-project-alist-styles (entry-dir output-dir style)
 
259
  "Return a list of styles to use in a `muse-project-alist' entry.
 
260
ENTRY-DIR is the top-level directory of the project.
 
261
OUTPUT-DIR is where Muse files are published, keeping directory structure.
 
262
STYLE is the publishing style to use.
 
263
 
 
264
For an example of the use of this function, see
 
265
`examples/mwolson/muse-init.el' from the Muse distribution."
 
266
  (cons `(:base ,style :path ,(expand-file-name output-dir)
 
267
                :include ,(concat "/" (file-name-nondirectory entry-dir)
 
268
                                  "/[^/]+$"))
 
269
        (mapcar (lambda (dir);
 
270
                  `(:base ,style
 
271
                          :path ,(expand-file-name dir output-dir)
 
272
                          :include ,(concat "/" dir "/[^/]+$")))
 
273
                (muse-project-recurse-directory entry-dir))))
 
274
 
 
275
(defun muse-project-alist-dirs (entry-dir)
 
276
  "Return a list of directories to use in a `muse-project-alist' entry.
 
277
ENTRY-DIR is the top-level directory of the project.
 
278
 
 
279
For an example of the use of this function, see
 
280
`examples/mwolson/muse-init.el' from the Muse distribution."
 
281
  (cons (expand-file-name entry-dir)
 
282
        (mapcar (lambda (dir) (expand-file-name dir entry-dir))
 
283
                (muse-project-recurse-directory entry-dir))))
 
284
 
 
285
;; Constructing the file-alist
 
286
 
 
287
(defvar muse-project-file-alist nil
 
288
  "This variable is automagically constructed as needed.")
 
289
 
 
290
(defvar muse-current-project nil
 
291
  "Project we are currently visiting.")
 
292
(make-variable-buffer-local 'muse-current-project)
 
293
 
 
294
(defsubst muse-project (&optional project)
 
295
  "Resolve the given PROJECT into a full Muse project, if it is a string."
 
296
  (if (null project)
 
297
      (or muse-current-project
 
298
          (muse-project-of-file))
 
299
    (if (stringp project)
 
300
        (assoc project muse-project-alist)
 
301
      (muse-assert (consp project))
 
302
      project)))
 
303
 
 
304
(defsubst muse-project-page-file (page project &optional no-check-p)
 
305
  "Return a filename if PAGE exists within the given Muse PROJECT."
 
306
  (setq project (muse-project project))
 
307
  (cdr (assoc page (muse-project-file-alist project no-check-p))))
 
308
 
 
309
(defun muse-project-private-p (file)
 
310
  "Return non-nil if NAME is a private page with PROJECT."
 
311
  (unless muse-under-windows-p
 
312
    (setq file (file-truename file))
 
313
    (if (file-attributes file)  ; don't publish if no attributes exist
 
314
        (or (when (eq ?- (aref (nth 8 (file-attributes
 
315
                                       (file-name-directory file))) 7))
 
316
              (message (concat
 
317
                        "The " (file-name-directory file)
 
318
                        " directory must be readable by others"
 
319
                        " in order for its contents to be published.")))
 
320
            (eq ?- (aref (nth 8 (file-attributes file)) 7)))
 
321
      t)))
 
322
 
 
323
(defun muse-project-file-entries (path)
 
324
  (let* ((names (list t))
 
325
         (lnames names))
 
326
    (cond
 
327
     ((file-directory-p path)
 
328
      (dolist (file (directory-files
 
329
                     path t (when (and muse-file-extension
 
330
                                       (not (string= muse-file-extension "")))
 
331
                              (concat "." muse-file-extension "\\'"))))
 
332
        (unless (or (string-match muse-project-ignore-regexp file)
 
333
                    (file-directory-p file))
 
334
          (setcdr lnames
 
335
                  (cons (cons (muse-page-name file) file) nil))
 
336
          (setq lnames (cdr lnames)))))
 
337
     ((file-readable-p path)
 
338
      (setcdr lnames
 
339
              (cons (cons (muse-page-name path) path) nil))
 
340
      (setq lnames (cdr lnames)))
 
341
     (t                                 ; regexp
 
342
      (muse-assert (file-name-directory path))
 
343
      (dolist (file (directory-files
 
344
                     (file-name-directory path) t
 
345
                     (file-name-nondirectory path)))
 
346
        (unless (string-match muse-project-ignore-regexp file)
 
347
          (setcdr lnames
 
348
                  (cons (cons (muse-page-name file) file) nil))
 
349
          (setq lnames (cdr lnames))))))
 
350
    (cdr names)))
 
351
 
 
352
(defun muse-project-file-alist (&optional project no-check-p)
 
353
  "Return member filenames for the given Muse PROJECT.
 
354
On UNIX, this list is only updated if one of the directories'
 
355
contents have changed.  On Windows, it is always reread from
 
356
disk."
 
357
  (setq project (muse-project project))
 
358
  (let ((file-alist (assoc (car project) muse-project-file-alist))
 
359
        last-mod)
 
360
    ;; Determine the last modified of any directory mentioned in the
 
361
    ;; project's pattern list
 
362
    (unless (or muse-under-windows-p no-check-p)
 
363
      (let ((pats (cadr project)))
 
364
        (while pats
 
365
          (if (symbolp (car pats))
 
366
              (setq pats (cddr pats))
 
367
            (let* ((fnd (file-name-directory (car pats)))
 
368
                   (dir (cond ((file-directory-p (car pats))
 
369
                               (car pats))
 
370
                              ((and (not (file-readable-p (car pats)))
 
371
                                    fnd
 
372
                                    (file-directory-p fnd))
 
373
                               fnd))))
 
374
              (when dir
 
375
                (let ((mod-time (nth 5 (file-attributes dir))))
 
376
                  (when (or (null last-mod)
 
377
                            (and mod-time
 
378
                                 (muse-time-less-p last-mod mod-time)))
 
379
                    (setq last-mod mod-time)))))
 
380
            (setq pats (cdr pats))))))
 
381
    ;; Either return the currently known list, or read it again from
 
382
    ;; disk
 
383
    (if (or (and no-check-p (cadr file-alist))
 
384
            (not (or muse-under-windows-p
 
385
                     (null (cddr file-alist))
 
386
                     (null last-mod)
 
387
                     (muse-time-less-p (cddr file-alist) last-mod))))
 
388
        (cadr file-alist)
 
389
      (if file-alist
 
390
          (setcdr (cdr file-alist) last-mod)
 
391
        (setq file-alist (cons (car project) (cons nil last-mod))
 
392
              muse-project-file-alist
 
393
              (cons file-alist muse-project-file-alist)))
 
394
      ;; Read in all of the file entries
 
395
      (save-match-data
 
396
        (setcar
 
397
         (cdr file-alist)
 
398
         (let* ((names (list t))
 
399
                (pats (cadr project)))
 
400
           (while pats
 
401
             (if (symbolp (car pats))
 
402
                 (setq pats (cddr pats))
 
403
               (nconc names (muse-project-file-entries (car pats)))
 
404
               (setq pats (cdr pats))))
 
405
           (cdr names)))))))
 
406
 
 
407
(defun muse-project-of-file (&optional pathname)
 
408
  "Determine which project the given PATHNAME relates to.
 
409
If PATHNAME is nil, the current buffer's filename is used."
 
410
  (if (and (null pathname) muse-current-project)
 
411
      muse-current-project
 
412
    (unless pathname (setq pathname (muse-current-file)))
 
413
    (save-match-data
 
414
      (when (and pathname
 
415
                 (not (string-match muse-project-ignore-regexp pathname)))
 
416
        (let* ((file (file-truename pathname))
 
417
               (dir  (file-name-directory file))
 
418
               (project-entry muse-project-alist)
 
419
               found)
 
420
          (while (and project-entry (not found))
 
421
            (let ((pats (car (cdar project-entry))))
 
422
              (while (and pats (not found))
 
423
                (if (symbolp (car pats))
 
424
                    (setq pats (cddr pats))
 
425
                  (let ((truename (file-truename (car pats))))
 
426
                    (if (or (string= truename file)
 
427
                            (string= truename dir)
 
428
                            (string-match (regexp-quote truename) file))
 
429
                        (setq found (car project-entry))))
 
430
                  (setq pats (cdr pats))))
 
431
              (setq project-entry (cdr project-entry))))
 
432
          found)))))
 
433
 
 
434
(defun muse-read-project (prompt &optional no-check-p no-assume)
 
435
  "Read a project name from the minibuffer, if it can't be figured
 
436
  out."
 
437
  (if (null muse-project-alist)
 
438
      (error "There are no Muse projects defined; see `muse-project-alist'.")
 
439
    (or (unless no-check-p
 
440
          (muse-project-of-file))
 
441
        (if (and (not no-assume)
 
442
                 (= 1 (length muse-project-alist)))
 
443
            (car muse-project-alist)
 
444
          (assoc (completing-read prompt muse-project-alist)
 
445
                 muse-project-alist)))))
 
446
 
 
447
(defvar muse-project-page-history nil)
 
448
 
 
449
(defun muse-read-project-file (project prompt &optional default)
 
450
  (let ((name (completing-read prompt (muse-project-file-alist project)
 
451
                               nil nil nil 'muse-project-page-history
 
452
                               default)))
 
453
    (cons name (muse-project-page-file name project))))
 
454
 
 
455
(defun muse-project-find-file (name project &optional command directory)
 
456
  "Open the Muse page given by NAME in PROJECT.
 
457
If COMMAND is non-nil, it is the function used to visit the file.
 
458
If DIRECTORY is non-nil, it is the directory in which the page
 
459
will be created if it does not already exist.  Otherwise, the
 
460
first directory within the project's fileset is used."
 
461
  (interactive
 
462
   (let* ((project (muse-read-project "Find in project: "
 
463
                                      current-prefix-arg))
 
464
          (default (muse-get-keyword :default (cadr project)))
 
465
          (entry (muse-read-project-file
 
466
                  project (if default
 
467
                              (format "Find page: (default: %s) "
 
468
                                      default)
 
469
                            "Find page: ")
 
470
                  default)))
 
471
     (list entry project)))
 
472
  (setq project (muse-project project))
 
473
  (let ((project-name (car project)))
 
474
    (unless (interactive-p)
 
475
      (setq project (muse-project project)
 
476
            name (cons name (muse-project-page-file name project))))
 
477
    ;; If we're given a relative or absolute filename, open it as-is
 
478
    (if (and (car name)
 
479
             (save-match-data (string-match muse-file-regexp (car name))))
 
480
        (setcdr name (car name))
 
481
      ;; At this point, name is (PAGE . FILE).
 
482
      (unless (cdr name)
 
483
        (let ((pats (cadr project)))
 
484
          (while (and pats (null directory))
 
485
            (if (symbolp (car pats))
 
486
                (setq pats (cddr pats))
 
487
              (if (file-directory-p (car pats))
 
488
                  (setq directory (car pats) pats nil)
 
489
                (setq pats (cdr pats))))))
 
490
        (when directory
 
491
          (let ((filename (expand-file-name
 
492
                           (if (and muse-file-extension
 
493
                                    (not (string= muse-file-extension "")))
 
494
                               (concat (car name) "." muse-file-extension)
 
495
                             (car name))
 
496
                           directory)))
 
497
            (unless (file-exists-p directory)
 
498
              (make-directory directory t))
 
499
            (setcdr name filename)))))
 
500
    ;; Open the file
 
501
    (if (cdr name)
 
502
        (funcall (or command 'find-file) (cdr name))
 
503
      (error "There is no page %s in project %s."
 
504
             (car name) project-name))))
 
505
 
 
506
(defun muse-project-applicable-styles (file styles &optional ignore-regexp)
 
507
  "Given STYLES, return a list of the ones that are considered for FILE.
 
508
The name of a project may be used for STYLES."
 
509
  (when (stringp styles)
 
510
    (setq styles (cddr (muse-project styles))))
 
511
  (when (and file styles)
 
512
    (let (used-styles)
 
513
      (dolist (style styles)
 
514
        (let ((include-regexp (muse-style-element :include style))
 
515
              (exclude-regexp (muse-style-element :exclude style)))
 
516
          (when (and (or ignore-regexp
 
517
                         (and (null include-regexp)
 
518
                              (null exclude-regexp))
 
519
                         (if include-regexp
 
520
                             (string-match include-regexp file)
 
521
                           (not (string-match exclude-regexp file))))
 
522
                     (or (not (file-exists-p file))
 
523
                         (not (muse-project-private-p file))))
 
524
            (add-to-list 'used-styles style))))
 
525
      used-styles)))
 
526
 
 
527
(defun muse-project-publish-file (file styles &optional force ignore-regexp)
 
528
  (setq styles (muse-project-applicable-styles file styles ignore-regexp))
 
529
  (let (published)
 
530
    (dolist (style styles)
 
531
      (let ((output-dir (muse-style-element :path style)))
 
532
        ;; ensure the publishing location is available
 
533
        (unless (file-exists-p output-dir)
 
534
          (message "Creating publishing directory %s" output-dir)
 
535
          (make-directory output-dir))
 
536
        ;; publish the member file!
 
537
        (if (muse-publish-file file style output-dir force)
 
538
            (setq published t))))
 
539
    published))
 
540
 
 
541
(defun muse-project-save-buffers (&optional project)
 
542
  (setq project (muse-project project))
 
543
  (map-y-or-n-p
 
544
   (function
 
545
    (lambda (buffer)
 
546
      (and (buffer-modified-p buffer)
 
547
           (not (buffer-base-buffer buffer))
 
548
           (or (buffer-file-name buffer)
 
549
               (progn
 
550
                 (set-buffer buffer)
 
551
                 (and buffer-offer-save
 
552
                      (> (buffer-size) 0))))
 
553
           (with-current-buffer buffer
 
554
             (let ((proj (muse-project-of-file)))
 
555
               (and proj (string= (car proj)
 
556
                                  (car project)))))
 
557
           (if (buffer-file-name buffer)
 
558
               (format "Save file %s? "
 
559
                       (buffer-file-name buffer))
 
560
             (format "Save buffer %s? "
 
561
                     (buffer-name buffer))))))
 
562
   (function
 
563
    (lambda (buffer)
 
564
      (set-buffer buffer)
 
565
      (save-buffer)))
 
566
   (buffer-list)
 
567
   '("buffer" "buffers" "save")
 
568
   (if (boundp 'save-some-buffers-action-alist)
 
569
       save-some-buffers-action-alist)))
 
570
 
 
571
(defun muse-project-publish (project &optional force)
 
572
  "Publish the pages of PROJECT that need publishing."
 
573
  (interactive (list (muse-read-project "Publish project: " nil t)
 
574
                     current-prefix-arg))
 
575
  (setq project (muse-project project))
 
576
  (let ((styles (cddr project))
 
577
        (muse-current-project project)
 
578
        published)
 
579
    ;; determine the style from the project, or else ask
 
580
    (unless styles
 
581
      (setq styles (list (muse-publish-get-style))))
 
582
    ;; prompt to save any buffers related to this project
 
583
    (muse-project-save-buffers project)
 
584
    ;; run hook before publishing begins
 
585
    (run-hook-with-args 'muse-before-project-publish-hook project)
 
586
    ;; publish all files in the project, for each style; the actual
 
587
    ;; publishing will only happen if the files are newer than the
 
588
    ;; last published output, or if the file is listed in
 
589
    ;; :force-publish.  Files in :force-publish will not trigger the
 
590
    ;; "All pages need to be published" message.
 
591
    (let ((forced-files (muse-get-keyword :force-publish (cadr project)))
 
592
          (file-alist (muse-project-file-alist project)))
 
593
      (dolist (pair file-alist)
 
594
        (when (muse-project-publish-file (cdr pair) styles force)
 
595
          (setq forced-files (delete (car pair) forced-files))
 
596
          (setq published t)))
 
597
      (dolist (file forced-files)
 
598
        (muse-project-publish-file (cdr (assoc file file-alist)) styles t)))
 
599
    ;; run hook after publishing ends
 
600
    (run-hook-with-args 'muse-after-project-publish-hook project)
 
601
    ;; notify the user that everything is now done
 
602
    (if published
 
603
        (message "All pages in %s have been published." (car project))
 
604
      (message "No pages in %s need publishing at this time."
 
605
               (car project)))))
 
606
 
 
607
(defun muse-project-batch-publish ()
 
608
  "Publish Muse files in batch mode."
 
609
  (let ((muse-batch-publishing-p t)
 
610
        force)
 
611
    (if (string= "--force" (or (car command-line-args-left) ""))
 
612
        (setq force t
 
613
              command-line-args-left (cdr command-line-args-left)))
 
614
    (if command-line-args-left
 
615
        (dolist (project command-line-args-left)
 
616
          (message "Publishing project %s ..." project)
 
617
          (muse-project-publish project force))
 
618
      (message "No projects specified."))))
 
619
 
 
620
(eval-when-compile
 
621
  (put 'make-local-hook 'byte-compile nil))
 
622
 
 
623
(defun muse-project-set-variables ()
 
624
  "Load project-specific variables."
 
625
  (let ((vars (muse-get-keyword :set (cadr muse-current-project)))
 
626
        sym custom-set var)
 
627
    (while vars
 
628
      (setq sym (car vars))
 
629
      (setq custom-set (or (get sym 'custom-set) 'set))
 
630
      (setq var (if (eq (get sym 'custom-type) 'hook)
 
631
                    (make-local-hook sym)
 
632
                  (make-local-variable sym)))
 
633
      (funcall custom-set var (car (cdr vars)))
 
634
      (setq vars (cdr (cdr vars))))))
 
635
 
 
636
(defun muse-project-delete-output-files (project)
 
637
  (interactive
 
638
   (list (muse-read-project "Remove all output files for project: " nil t)))
 
639
  (setq project (muse-project project))
 
640
  (let ((file-alist (muse-project-file-alist project))
 
641
        (styles (cddr project))
 
642
        output-file path)
 
643
    (dolist (entry file-alist)
 
644
      (dolist (style styles)
 
645
        (setq output-file
 
646
              (and (setq path (muse-style-element :path style))
 
647
                   (expand-file-name
 
648
                    (concat (muse-style-element :prefix style)
 
649
                            (car entry)
 
650
                            (or (muse-style-element :osuffix style)
 
651
                                (muse-style-element :suffix style)))
 
652
                    path)))
 
653
        (if output-file
 
654
            (muse-delete-file-if-exists output-file))))))
 
655
 
 
656
(provide 'muse-project)
 
657
 
 
658
;;; muse-project.el ends here