Merged from mwolson@gnu.org--2006-muse-el (patch 53)
[muse-el.git] / lisp / muse-wiki.el
blob320fb55d6b9f5b86872ef0b39bbb9d5e122e4829
1 ;;; muse-wiki.el --- wiki features for Muse
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Yann Hodique <Yann.Hodique@lifl.fr>
6 ;; Keywords:
8 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
10 ;; Emacs Muse is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
15 ;; Emacs Muse is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with Emacs Muse; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;;; Contributors:
29 ;; Per B. Sederberg (per AT med DOT upenn DOT edu) made it so that all
30 ;; files in a Muse project can become implicit links.
32 ;;; Code:
34 (require 'muse-regexps)
35 (require 'muse-mode)
37 (eval-when-compile
38 (require 'muse-colors))
40 (defgroup muse-wiki nil
41 "Options controlling the behavior of Emacs Muse Wiki features."
42 :group 'muse-mode)
44 (defcustom muse-wiki-use-wikiword t
45 "Whether to use color and publish bare WikiNames."
46 :type 'boolean
47 :group 'muse-wiki)
49 (defcustom muse-wiki-allow-nonexistent-wikiword nil
50 "Whether to color bare WikiNames that don't have an existing file."
51 :type 'boolean
52 :group 'muse-wiki)
54 (defcustom muse-wiki-match-all-project-files nil
55 "Whether to extend WikiName functionality to also match
56 existing filenames, regardless of whether they are named in
57 WikiWord format.
59 If non-nil, Muse will color and publish implicit links to any
60 file in your project."
61 :type 'boolean
62 :group 'muse-wiki)
64 (defcustom muse-wiki-ignore-implicit-links-to-current-page nil
65 "Whether to ignore implicit links to the current page.
67 If non-nil, Muse will not recognize implicit links to the current
68 page, both when formatting and publishing."
69 :type 'boolean
70 :group 'muse-wiki)
72 (defvar muse-wiki-updating-wikiword-p nil
73 "Prevent recursive calls to `muse-wiki-update-local-wikiword-regexp'.")
75 (eval-when-compile
76 (defvar muse-wiki-wikiword-regexp))
78 (defun muse-wiki-update-local-wikiword-regexp ()
79 "Update a local copy of `muse-wiki-wikiword-regexp' to include
80 all the files in the project."
81 ;; see if the user wants to append project files
82 (when (and muse-wiki-use-wikiword
83 muse-wiki-match-all-project-files
84 (not muse-wiki-updating-wikiword-p))
85 (let ((muse-wiki-updating-wikiword-p t))
86 ;; make the regexp local
87 (set (make-local-variable 'muse-wiki-wikiword-regexp)
88 (concat "\\(\\<\\(?:"
89 ;; append the files from the project
90 (mapconcat 'car
91 (muse-project-file-alist (muse-project))
92 "\\|")
93 "\\)\\>\\|\\(?:"
94 (default-value 'muse-wiki-wikiword-regexp)
95 "\\)\\)"))
96 ;; update coloring setup
97 (when (featurep 'muse-colors)
98 (muse-configure-highlighting
99 'muse-colors-markup muse-colors-markup)))))
101 (add-hook 'muse-update-values-hook
102 'muse-wiki-update-local-wikiword-regexp)
103 (add-hook 'muse-project-file-alist-hook
104 'muse-wiki-update-local-wikiword-regexp)
106 (defun muse-wiki-update-wikiword-regexp (sym val)
107 "Update everything related to `muse-wiki-wikiword-regexp'."
108 (set sym val)
109 (muse-wiki-update-local-wikiword-regexp)
110 (when (featurep 'muse-colors)
111 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
113 (defcustom muse-wiki-wikiword-regexp
114 (concat "\\<\\(\\(?:[" muse-regexp-upper
115 "]+[" muse-regexp-lower "]+\\)\\(?:["
116 muse-regexp-upper "]+[" muse-regexp-lower "]+\\)+\\)")
117 "Regexp used to match WikiWords."
118 :type 'regexp
119 :group 'muse-wiki
120 :set 'muse-wiki-update-wikiword-regexp)
122 (defcustom muse-wiki-ignore-bare-project-names nil
123 "Determine whether project names without a page specifer are links.
124 If non-nil, project names without a page specifier will not be
125 considered links.
126 When nil, project names without a specifier are highlighted and
127 they link to the default page of the project that they name."
128 :type 'boolean
129 :group 'muse-wiki)
131 (defvar muse-wiki-interwiki-regexp ""
132 "Regexp that matches all interwiki links.
133 This is automatically generated by setting `muse-wiki-interwiki-alist'.
134 It can also be set by calling `muse-wiki-update-interwiki-regexp'.")
136 (defcustom muse-wiki-interwiki-delimiter "#\\|::"
137 "Delimiter regexp used for InterWiki links.
138 If you use groups, use only shy groups."
139 :type 'regexp
140 :group 'muse-wiki)
142 (defcustom muse-wiki-interwiki-replacement ": "
143 "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
144 InterWiki link descriptions.
146 If you want this replacement to happen, you must add
147 `muse-wiki-publish-pretty-interwiki' to
148 `muse-publish-desc-transforms'."
149 :type 'regexp
150 :group 'muse-wiki)
152 (eval-when-compile
153 (defvar muse-wiki-interwiki-alist))
155 (defun muse-wiki-project-files-with-spaces (&optional project)
156 "Return a list of files in PROJECT that have spaces."
157 (setq project (muse-project project))
158 (let ((flist nil))
159 (save-match-data
160 (mapcar (function (lambda (file)
161 (when (string-match " " (car file))
162 (setq flist (cons (car file) flist)))))
163 (muse-project-file-alist project)))
164 flist))
166 (defun muse-wiki-update-interwiki-regexp ()
167 "Update the value of `muse-wiki-interwiki-regexp' based on
168 `muse-wiki-interwiki-alist' and `muse-project-alist'."
169 (setq muse-wiki-interwiki-regexp
170 (concat "\\<\\(" (mapconcat 'car muse-project-alist "\\|")
171 (when muse-wiki-interwiki-alist
172 (concat "\\|" (mapconcat 'car muse-wiki-interwiki-alist
173 "\\|")))
174 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
175 "\\)\\("
176 (when muse-wiki-match-all-project-files
177 ;; append the files from the project
178 (concat
179 (mapconcat
180 (function
181 (lambda (proj)
182 (mapconcat 'identity
183 (muse-wiki-project-files-with-spaces
184 (car proj))
185 "\\|")))
186 muse-project-alist "")
187 "\\|"))
188 "\\sw+\\)\\)?\\>"))
189 (when (featurep 'muse-colors)
190 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
192 (defcustom muse-wiki-interwiki-alist
193 '(("EmacsWiki" . "http://www.emacswiki.org/cgi-bin/wiki/"))
194 "A table of WikiNames that refer to external entities.
195 The format of this table is an alist, or series of cons cells.
196 Each cons cell must be of the form:
198 (WIKINAME . STRING-OR-FUNCTION)
200 The second part of the cons cell may either be a STRING, which in most
201 cases should be a URL, or a FUNCTION. If a function, it will be
202 called with one argument: the tag applied to the Interwiki name, or
203 nil if no tag was used. If the cdr was a STRING and a tag is used,
204 the tag is simply appended.
206 Here are some examples:
208 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
210 Referring to [[JohnWiki::EmacsModules]] then really means:
212 http://alice.dynodns.net/wiki?EmacsModules
214 If a function is used for the replacement text, you can get creative
215 depending on what the tag is. Tags may contain any alphabetic
216 character, any number, % or _. If you need other special characters,
217 use % to specify the hex code, as in %2E. All browsers should support
218 this."
219 :type '(repeat (cons (string :tag "WikiName")
220 (choice (string :tag "URL") function)))
221 :set (function
222 (lambda (sym value)
223 (set sym value)
224 (muse-wiki-update-interwiki-regexp)))
225 :group 'muse-wiki)
227 (add-hook 'muse-update-values-hook
228 'muse-wiki-update-interwiki-regexp)
230 (defun muse-wiki-resolve-project-page (&optional project page)
231 "Return the published path from the current page to PAGE of PROJECT.
232 If PAGE is not specified, use the value of :default in PROJECT.
233 If PROJECT is not specified, default to first project of
234 `muse-projects-alist'.
236 Note that PAGE can have several output directories. If this is
237 the case, we will use the first one that matches our current
238 style and ignore the others."
239 (setq project (or (and project
240 (muse-project project))
241 (car muse-project-alist))
242 page (or page (muse-get-keyword :default
243 (cadr project))))
244 (let* ((page-path (muse-project-page-file page project))
245 (remote-style (when page-path (car (muse-project-applicable-styles
246 page-path (cddr project)))))
247 (local-style (car (muse-project-applicable-styles
248 (muse-current-file)
249 (cddr (muse-project-of-file))))))
250 (cond ((and remote-style local-style muse-publishing-p)
251 (let ((prefix (muse-style-element :base-url remote-style)))
252 (muse-publish-link-file
253 (if prefix
254 (concat prefix page)
255 (file-relative-name (expand-file-name
256 page
257 (muse-style-element :path remote-style))
258 (expand-file-name
259 (muse-style-element :path local-style))))
260 nil remote-style)))
261 ((not muse-publishing-p)
262 (if page-path
263 page-path
264 (when muse-wiki-allow-nonexistent-wikiword
265 ;; make a path to a nonexistent file in project
266 (setq page-path (expand-file-name
267 page (car (cadr project))))
268 (if (and muse-file-extension
269 (not (string= muse-file-extension "")))
270 (concat page-path "." muse-file-extension)
271 page-path)))))))
273 (defun muse-wiki-handle-interwiki (&optional string)
274 "If STRING or point has an interwiki link, resolve it and
275 return the first match.
276 Match 1 is set to the link.
277 Match 2 is set to the description."
278 (when (if string (string-match muse-wiki-interwiki-regexp string)
279 (looking-at muse-wiki-interwiki-regexp))
280 (let* ((project (match-string 1 string))
281 (subst (cdr (assoc project muse-wiki-interwiki-alist)))
282 (word (if string
283 (and (match-beginning 2)
284 (substring string (match-beginning 2)))
285 (match-string 2 string))))
286 (if subst
287 (if (functionp subst)
288 (funcall subst word)
289 (concat subst word))
290 (and (assoc project muse-project-alist)
291 (or word (not muse-wiki-ignore-bare-project-names))
292 (muse-wiki-resolve-project-page project word))))))
294 (defun muse-wiki-handle-wikiword (&optional string)
295 "If STRING or point has a WikiWord, return it.
296 Match 1 is set to the WikiWord."
297 (when (and muse-wiki-use-wikiword
298 (if string
299 (string-match muse-wiki-wikiword-regexp string)
300 (looking-at muse-wiki-wikiword-regexp))
301 (or muse-wiki-allow-nonexistent-wikiword
302 (and (muse-project-of-file)
303 (muse-project-page-file
304 (match-string 1 string) muse-current-project t))
305 (file-exists-p (match-string 1 string)))
306 (and muse-wiki-ignore-implicit-links-to-current-page
307 (not (string= (match-string 1 string) (muse-page-name)))))
308 (match-string 1 string)))
310 ;; Prettifications
312 (defcustom muse-wiki-publish-small-title-words
313 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
314 "Strings that should be downcased in a page title.
315 This is used by `muse-wiki-publish-pretty-title', which must be
316 called manually."
317 :type '(repeat string)
318 :group 'muse-wiki)
320 (defcustom muse-wiki-hide-nop-tag t
321 "If non-nil, hide <nop> tags when coloring a Muse buffer."
322 :type 'boolean
323 :group 'muse-wiki)
325 (defun muse-wiki-publish-pretty-title (&optional title explicit)
326 "Return a pretty version of the given TITLE.
327 If EXPLICIT is non-nil, TITLE will be returned unmodified."
328 (unless title (setq title (or (muse-publishing-directive "title") "")))
329 (if (or explicit
330 (save-match-data (string-match muse-url-regexp title)))
331 title
332 (save-match-data
333 (let ((case-fold-search nil))
334 (while (string-match (concat "\\([" muse-regexp-lower
335 "]\\)\\([" muse-regexp-upper
336 "0-9]\\)")
337 title)
338 (setq title (replace-match "\\1 \\2" t nil title)))
339 (let* ((words (split-string title))
340 (w (cdr words)))
341 (while w
342 (if (member (downcase (car w))
343 muse-wiki-publish-small-title-words)
344 (setcar w (downcase (car w))))
345 (setq w (cdr w)))
346 (mapconcat 'identity words " "))))))
348 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
349 "Replace instances of `muse-wiki-interwiki-delimiter' with
350 `muse-wiki-interwiki-replacement'."
351 (if (or explicit
352 (save-match-data (string-match muse-url-regexp desc)))
353 desc
354 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
355 muse-wiki-interwiki-replacement
356 desc)))
358 ;; Coloring setup
360 (eval-after-load "muse-colors"
361 '(progn
362 (defun muse-wiki-colors-nop-tag (beg end)
363 (when muse-wiki-hide-nop-tag
364 (add-text-properties beg (+ beg 5)
365 '(invisible muse intangible t))))
366 (defun muse-colors-wikiword-separate ()
367 (add-text-properties (match-beginning 0) (match-end 0)
368 '(invisible muse intangible t)))
370 (add-to-list 'muse-colors-tags
371 '("nop" nil nil muse-wiki-colors-nop-tag)
374 (add-to-list 'muse-colors-markup
375 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
377 (add-to-list 'muse-colors-markup
378 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
380 (add-to-list 'muse-colors-markup
381 '("''''" ?\' muse-colors-wikiword-separate)
382 nil)
384 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
386 ;; Publishing setup
388 (eval-after-load "muse-publish"
389 '(progn
390 (add-to-list 'muse-publish-markup-regexps
391 '(3100 muse-wiki-interwiki-regexp 0 link)
393 (add-to-list 'muse-publish-markup-regexps
394 '(3200 muse-wiki-wikiword-regexp 0 link)
396 (add-to-list 'muse-publish-markup-regexps
397 '(3300 "''''" 0 "")
400 (custom-add-option 'muse-publish-desc-transforms
401 'muse-wiki-publish-pretty-interwiki)
402 (custom-add-option 'muse-publish-desc-transforms
403 'muse-wiki-publish-pretty-title)))
405 ;; Insinuate link handling
407 (custom-add-option 'muse-implicit-link-functions
408 'muse-wiki-handle-interwiki)
409 (custom-add-option 'muse-implicit-link-functions
410 'muse-wiki-handle-wikiword)
412 (custom-add-option 'muse-explicit-link-functions
413 'muse-wiki-handle-interwiki)
415 (add-to-list 'muse-implicit-link-functions
416 'muse-wiki-handle-interwiki t)
417 (add-to-list 'muse-implicit-link-functions
418 'muse-wiki-handle-wikiword t)
420 (add-to-list 'muse-explicit-link-functions
421 'muse-wiki-handle-interwiki t)
423 ;; Obsolete functions
425 (defun muse-wiki-update-custom-values ()
426 (muse-display-warning
427 (concat "Please remove `muse-wiki-update-custom-values' from"
428 " `muse-mode-hook'. Its use is now deprecated.")))
430 (provide 'muse-wiki)
431 ;;; muse-wiki.el ends here