File: muse-wiki.el

package info (click to toggle)
muse-el 3.12-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,688 kB
  • ctags: 905
  • sloc: lisp: 11,772; makefile: 304; python: 106; sh: 51; perl: 46
file content (498 lines) | stat: -rw-r--r-- 19,304 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
;;; muse-wiki.el --- wiki features for Muse

;; Copyright (C) 2005, 2006, 2007, 2008  Free Software Foundation, Inc.

;; Author: Yann Hodique <Yann.Hodique@lifl.fr>
;; Keywords:

;; This file is part of Emacs Muse.  It is not part of GNU Emacs.

;; Emacs Muse is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.

;; Emacs Muse is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with Emacs Muse; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;;; Contributors:

;; Per B. Sederberg (per AT med DOT upenn DOT edu) made it so that all
;; files in a Muse project can become implicit links.

;;; Code:

(require 'muse-regexps)
(require 'muse-mode)

(eval-when-compile
  (require 'muse-colors))

(defgroup muse-wiki nil
  "Options controlling the behavior of Emacs Muse Wiki features."
  :group 'muse-mode)

(defcustom muse-wiki-use-wikiword t
  "Whether to use color and publish bare WikiNames."
  :type 'boolean
  :group 'muse-wiki)

(defcustom muse-wiki-allow-nonexistent-wikiword nil
  "Whether to color bare WikiNames that don't have an existing file."
  :type 'boolean
  :group 'muse-wiki)

(defcustom muse-wiki-match-all-project-files nil
  "If non-nil, Muse will color and publish implicit links to any
file in your project, regardless of whether its name is a WikiWord."
  :type 'boolean
  :group 'muse-wiki)

(defcustom muse-wiki-ignore-implicit-links-to-current-page nil
  "If non-nil, Muse will not recognize implicit links to the current
page, both when formatting and publishing."
  :type 'boolean
  :group 'muse-wiki)

(defvar muse-wiki-project-file-regexp nil
  "Regexp used to match the files in the current project.

This is set by `muse-wiki-update-project-file-regexp' automatically
when `muse-wiki-match-all-project-files' is non-nil.")
(make-variable-buffer-local 'muse-wiki-project-file-regexp)

(defun muse-wiki-update-project-file-regexp ()
  "Update a local copy of `muse-wiki-project-file-regexp' to include
all the files in the project."
  ;; see if the user wants to match project files
  (when muse-wiki-match-all-project-files
    (let ((files (mapcar #'car (muse-project-file-alist (muse-project)))))
      (setq muse-wiki-project-file-regexp
            (when files
              (concat "\\("
                      ;; include all files from the project
                      (regexp-opt files 'words)
                      "\\)"))))
    ;; update coloring setup
    (when (featurep 'muse-colors)
      (muse-configure-highlighting
       'muse-colors-markup muse-colors-markup))))

(add-hook 'muse-update-values-hook
          'muse-wiki-update-project-file-regexp)
(add-hook 'muse-project-file-alist-hook
          'muse-wiki-update-project-file-regexp)

(defcustom muse-wiki-wikiword-regexp
  (concat "\\<\\(\\(?:[" muse-regexp-upper
          "]+[" muse-regexp-lower "]+\\)\\(?:["
          muse-regexp-upper "]+[" muse-regexp-lower "]+\\)+\\)")
  "Regexp used to match WikiWords."
  :set (function
        (lambda (sym value)
          (set sym value)
          (when (featurep 'muse-colors)
            (muse-configure-highlighting
             'muse-colors-markup muse-colors-markup))))
  :type 'regexp
  :group 'muse-wiki)

(defcustom muse-wiki-ignore-bare-project-names nil
  "Determine whether project names without a page specifer are links.

If non-nil, project names without a page specifier will not be
considered links.

When nil, project names without a specifier are highlighted and
they link to the default page of the project that they name."
  :type 'boolean
  :group 'muse-wiki)

(defvar muse-wiki-interwiki-regexp nil
  "Regexp that matches all interwiki links.

This is automatically generated by setting `muse-wiki-interwiki-alist'.
It can also be set by calling `muse-wiki-update-interwiki-regexp'.")

(defcustom muse-wiki-interwiki-delimiter "#\\|::"
  "Delimiter regexp used for InterWiki links.

If you use groups, use only shy groups."
  :type 'regexp
  :group 'muse-wiki)

(defcustom muse-wiki-interwiki-replacement ": "
  "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
InterWiki link descriptions.

If you want this replacement to happen, you must add
`muse-wiki-publish-pretty-interwiki' to
`muse-publish-desc-transforms'."
  :type 'regexp
  :group 'muse-wiki)

(eval-when-compile
  (defvar muse-wiki-interwiki-alist))

(defun muse-wiki-project-files-with-spaces (&optional project)
  "Return a list of files in PROJECT that have spaces."
  (setq project (muse-project project))
  (let ((flist nil))
    (save-match-data
      (dolist (filename (muse-project-file-alist project))
        (when (string-match " " filename)
          (setq flist (cons filename flist)))))
    flist))

(defun muse-wiki-update-interwiki-regexp ()
  "Update the value of `muse-wiki-interwiki-regexp' based on
`muse-wiki-interwiki-alist' and `muse-project-alist'."
  (if (null muse-project-alist)
      (setq muse-wiki-interwiki-regexp nil)
    (setq muse-wiki-interwiki-regexp
          (concat "\\<\\(" (regexp-opt (mapcar #'car muse-project-alist))
                  (when muse-wiki-interwiki-alist
                    (let ((interwiki-rules (mapcar #'car
                                                   muse-wiki-interwiki-alist)))
                      (when interwiki-rules
                        (concat "\\|" (regexp-opt interwiki-rules)))))
                  "\\)\\(?:\\(" muse-wiki-interwiki-delimiter
                  "\\)\\("
                  (when muse-wiki-match-all-project-files
                    ;; append the files from the project
                    (let ((files nil))
                      (dolist (proj muse-project-alist)
                        (setq files
                              (nconc (muse-wiki-project-files-with-spaces
                                      (car proj))
                                     files)))
                      (when files
                        (concat (regexp-opt files) "\\|"))))
                  "\\sw+\\)\\(#\\S-+\\)?\\)?\\>"))
    (when (featurep 'muse-colors)
      (muse-configure-highlighting 'muse-colors-markup muse-colors-markup))))

(defcustom muse-wiki-interwiki-alist
  '(("EmacsWiki" . "http://www.emacswiki.org/cgi-bin/wiki/"))
  "A table of WikiNames that refer to external entities.

The format of this table is an alist, or series of cons cells.
Each cons cell must be of the form:

  (WIKINAME . STRING-OR-FUNCTION)

The second part of the cons cell may either be a STRING, which in most
cases should be a URL, or a FUNCTION.  If a function, it will be
called with one argument: the tag applied to the Interwiki name, or
nil if no tag was used.  If the cdr was a STRING and a tag is used,
the tag is simply appended.

Here are some examples:

  (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")

Referring to [[JohnWiki::EmacsModules]] then really means:

  http://alice.dynodns.net/wiki?EmacsModules

If a function is used for the replacement text, you can get creative
depending on what the tag is.  Tags may contain any alphabetic
character, any number, % or _.  If you need other special characters,
use % to specify the hex code, as in %2E.  All browsers should support
this."
  :type '(repeat (cons (string :tag "WikiName")
                       (choice (string :tag "URL") function)))
  :set (function
        (lambda (sym value)
          (set sym value)
          (muse-wiki-update-interwiki-regexp)))
  :group 'muse-wiki)

(add-hook 'muse-update-values-hook
          'muse-wiki-update-interwiki-regexp)

(defun muse-wiki-resolve-project-page (&optional project page)
  "Return the published path from the current page to PAGE of PROJECT.

If PAGE is not specified, use the value of :default in PROJECT.

If PROJECT is not specified, default to the current project.  If
no project is current, use the first project of
`muse-projects-alist'.

Note that PAGE can have several output directories.  If this is
the case, we will use the first one that matches our current
style and has the same link suffix, ignoring the others.  If no
style has the same link suffix as the current publishing style,
use the first style we find."
  (setq project (or (and project
                         (muse-project project))
                    (muse-project)
                    (car muse-project-alist))
        page (or page (muse-get-keyword :default (cadr project))))
  (let* ((page-path (and muse-project-alist
                         (muse-project-page-file page project)))
         (remote-styles (and page-path (muse-project-applicable-styles
                                        page-path (cddr project))))
         (local-style (muse-project-current-output-style)))
    (cond ((and remote-styles local-style muse-publishing-p)
           (muse-project-resolve-link page local-style remote-styles))
          ((not muse-publishing-p)
           (if page-path
               page-path
             (when muse-wiki-allow-nonexistent-wikiword
               ;; make a path to a nonexistent file in project
               (setq page-path (expand-file-name
                                page (car (cadr project))))
               (if (and muse-file-extension
                        (not (string= muse-file-extension "")))
                   (concat page-path "." muse-file-extension)
                 page-path)))))))

(defun muse-wiki-handle-implicit-interwiki (&optional string)
  "If STRING or point has an interwiki link, resolve it to a filename.

Match string 0 is set to the link."
  (when (and muse-wiki-interwiki-regexp
             (if string (string-match muse-wiki-interwiki-regexp string)
               (looking-at muse-wiki-interwiki-regexp)))
    (let* ((project (match-string 1 string))
           (subst (cdr (assoc project muse-wiki-interwiki-alist)))
           (word (match-string 3 string))
           (anchor (if (match-beginning 4)
                       (match-string 4 string)
                     "")))
      (if subst
          (if (functionp subst)
              (and (setq word (funcall subst word))
                   (concat word anchor))
            (concat subst word anchor))
        (and (assoc project muse-project-alist)
             (or word (not muse-wiki-ignore-bare-project-names))
             (setq word (muse-wiki-resolve-project-page project word))
             (concat word anchor))))))

(defun muse-wiki-handle-explicit-interwiki (&optional string)
  "If STRING or point has an interwiki link, resolve it to a filename."
  (let ((right-pos (if string (length string) (match-end 1))))
    (when (and muse-wiki-interwiki-regexp
               (if string (string-match muse-wiki-interwiki-regexp string)
                 (save-restriction
                   (narrow-to-region (point) right-pos)
                   (looking-at muse-wiki-interwiki-regexp))))
      (let* ((project (match-string 1 string))
             (subst (cdr (assoc project muse-wiki-interwiki-alist)))
             (anchor (and (match-beginning 4)
                          (match-string 4 string)))
             (word (when (match-end 2)
                     (cond (anchor (match-string 3 string))
                           (string (substring string (match-end 2)))
                           (right-pos (buffer-substring (match-end 2)
                                                        right-pos))
                           (t nil)))))
        (if (and (null word)
                 right-pos
                 (not (= right-pos (match-end 1))))
            ;; if only a project name was found, it must take up the
            ;; entire string or link
            nil
          (unless anchor
            (if (or (null word)
                    (not (string-match "#[^#]+\\'" word)))
                (setq anchor "")
              (setq anchor (match-string 0 word))
              (setq word (substring word 0 (match-beginning 0)))))
          (if subst
              (if (functionp subst)
                  (and (setq word (funcall subst word))
                       (concat word anchor))
                (concat subst word anchor))
            (and (assoc project muse-project-alist)
                 (or word (not muse-wiki-ignore-bare-project-names))
                 (setq word (muse-wiki-resolve-project-page project word))
                 (concat word anchor))))))))

(defun muse-wiki-handle-wikiword (&optional string)
  "If STRING or point has a WikiWord, return it.

Match 1 is set to the WikiWord."
  (when (and (or (and muse-wiki-match-all-project-files
                      muse-wiki-project-file-regexp
                      (if string
                          (string-match muse-wiki-project-file-regexp string)
                        (looking-at muse-wiki-project-file-regexp)))
                 (and muse-wiki-use-wikiword
                      (if string
                          (string-match muse-wiki-wikiword-regexp string)
                        (looking-at muse-wiki-wikiword-regexp))))
             (cond
              (muse-wiki-allow-nonexistent-wikiword
               t)
              ((and muse-wiki-ignore-implicit-links-to-current-page
                    (string= (match-string 1 string) (muse-page-name)))
               nil)
              ((and (muse-project-of-file)
                    (muse-project-page-file
                     (match-string 1 string) muse-current-project t))
               t)
              ((file-exists-p (match-string 1 string))
               t)
              (t nil)))
    (match-string 1 string)))

;;; Prettifications

(defcustom muse-wiki-publish-small-title-words
  '("the" "and" "at" "on" "of" "for" "in" "an" "a")
  "Strings that should be downcased in a page title.

This is used by `muse-wiki-publish-pretty-title', which must be
called manually."
  :type '(repeat string)
  :group 'muse-wiki)

(defcustom muse-wiki-hide-nop-tag t
  "If non-nil, hide <nop> tags when coloring a Muse buffer."
  :type 'boolean
  :group 'muse-wiki)

(defun muse-wiki-publish-pretty-title (&optional title explicit)
  "Return a pretty version of the given TITLE.

If EXPLICIT is non-nil, TITLE will be returned unmodified."
  (unless title (setq title (or (muse-publishing-directive "title") "")))
  (if (or explicit
          (save-match-data (string-match muse-url-regexp title)))
      title
    (save-match-data
      (let ((case-fold-search nil))
        (while (string-match (concat "\\([" muse-regexp-lower
                                     "]\\)\\([" muse-regexp-upper
                                     "0-9]\\)")
                             title)
          (setq title (replace-match "\\1 \\2" t nil title)))
        (let* ((words (split-string title))
               (w (cdr words)))
          (while w
            (if (member (downcase (car w))
                        muse-wiki-publish-small-title-words)
                (setcar w (downcase (car w))))
            (setq w (cdr w)))
          (mapconcat 'identity words " "))))))

(defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
  "Replace instances of `muse-wiki-interwiki-delimiter' with
`muse-wiki-interwiki-replacement'."
  (if (or explicit
          (save-match-data (string-match muse-url-regexp desc)))
      desc
    (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
                                   muse-wiki-interwiki-replacement
                                   desc)))

;;; Coloring setup

(eval-after-load "muse-colors"
  '(progn
     (defun muse-wiki-colors-nop-tag (beg end)
       "Inhibit the colorization of inhibit links just after the tag.

Example: <nop>WikiWord"
       (when muse-wiki-hide-nop-tag
         (add-text-properties beg (+ beg 5)
                              '(invisible muse intangible t)))
       (unless (> (+ beg 6) (point-max))
         (add-text-properties (+ beg 5) (+ beg 6)
                              '(muse-no-implicit-link t))))
     (defun muse-colors-wikiword-separate ()
       (add-text-properties (match-beginning 0) (match-end 0)
                            '(invisible muse intangible t)))

     (add-to-list 'muse-colors-tags
                  '("nop" nil nil nil muse-wiki-colors-nop-tag)
                  t)

     (add-to-list 'muse-colors-markup
                  '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
                  t)
     (add-to-list 'muse-colors-markup
                  '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
                  t)
     (add-to-list 'muse-colors-markup
                  '(muse-wiki-project-file-regexp t muse-colors-implicit-link)
                  t)
     (add-to-list 'muse-colors-markup
                  '("''''" ?\' muse-colors-wikiword-separate)
                  nil)

     (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))

;;; Publishing setup

(eval-after-load "muse-publish"
  '(progn
     (defun muse-wiki-publish-nop-tag (beg end)
       "Inhibit the colorization of inhibit links just after the tag.

Example: <nop>WikiWord"
       (unless (= (point) (point-max))
         (muse-publish-mark-read-only (point) (+ (point) 1))))

     (add-to-list 'muse-publish-markup-tags
                  '("nop" nil nil nil muse-wiki-publish-nop-tag)
                  t)

     (add-to-list 'muse-publish-markup-regexps
                  '(3100 muse-wiki-interwiki-regexp 0 link)
                  t)
     (add-to-list 'muse-publish-markup-regexps
                  '(3200 muse-wiki-wikiword-regexp 0 link)
                  t)
     (add-to-list 'muse-publish-markup-regexps
                  '(3250 muse-wiki-project-file-regexp 0 link)
                  t)
     (add-to-list 'muse-publish-markup-regexps
                  '(3300 "''''" 0 "")
                  t)

     (custom-add-option 'muse-publish-desc-transforms
                        'muse-wiki-publish-pretty-interwiki)
     (custom-add-option 'muse-publish-desc-transforms
                        'muse-wiki-publish-pretty-title)))

;;; Insinuate link handling

(custom-add-option 'muse-implicit-link-functions
                   'muse-wiki-handle-implicit-interwiki)
(custom-add-option 'muse-implicit-link-functions
                   'muse-wiki-handle-wikiword)

(custom-add-option 'muse-explicit-link-functions
                   'muse-wiki-handle-explicit-interwiki)

(add-to-list 'muse-implicit-link-functions
             'muse-wiki-handle-implicit-interwiki t)
(add-to-list 'muse-implicit-link-functions
             'muse-wiki-handle-wikiword t)

(add-to-list 'muse-explicit-link-functions
             'muse-wiki-handle-explicit-interwiki t)

;;; Obsolete functions

(defun muse-wiki-update-custom-values ()
  (muse-display-warning
   (concat "Please remove `muse-wiki-update-custom-values' from"
           " `muse-mode-hook'.  Its use is now deprecated.")))

(provide 'muse-wiki)
;;; muse-wiki.el ends here