muse-html: Fix regexp error.
[muse-el.git] / lisp / muse-wiki.el
blobe3f5280b3b335da06a14db519c8af176e272f822
1 ;;; muse-wiki.el --- wiki features for Muse
3 ;; Copyright (C) 2005 Free Software Foundation, Inc.
5 ;; Author: Yann Hodique <Yann.Hodique@lifl.fr>
6 ;; Keywords:
8 ;; This file is not part of GNU Emacs.
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; 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 ;;; Code:
31 (require 'muse-regexps)
32 (require 'muse-mode)
34 (eval-when-compile
35 (require 'muse-colors))
37 (defgroup muse-wiki nil
38 "Options controlling the behavior of Emacs Muse Wiki features."
39 :group 'muse-mode)
41 (defun muse-wiki-update-wikiword-regexp (sym val)
42 "Update everything related to `muse-wiki-wikiword-regexp'."
43 (set sym val)
44 (when (featurep 'muse-colors)
45 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
47 (defcustom muse-wiki-hide-nop-tag t
48 "If non-nil, hide <nop> tags when coloring a Muse buffer."
49 :type 'boolean
50 :group 'muse-wiki)
52 (defcustom muse-wiki-wikiword-regexp
53 (concat "\\<\\(\\(?:[" muse-regexp-upper
54 "]+[" muse-regexp-lower "]+\\)\\(?:["
55 muse-regexp-upper "]+[" muse-regexp-lower "]+\\)+\\)")
56 "Regexp used to match WikiWords."
57 :type 'regexp
58 :group 'muse-wiki
59 :set 'muse-wiki-update-wikiword-regexp)
61 (defcustom muse-wiki-use-wikiword t
62 "Whether to use color and publish bare WikiNames."
63 :type 'boolean
64 :group 'muse-wiki)
66 (defcustom muse-wiki-allow-nonexistent-wikiword nil
67 "Whether to color bare WikiNames that don't have an existing file."
68 :type 'boolean
69 :group 'muse-wiki)
71 (defcustom muse-wiki-ignore-bare-project-names nil
72 "Determine whether project names without a page specifer are links.
73 If non-nil, project names without a page specifier will not be
74 considered links.
75 When nil, project names without a specifier are highlighted and
76 they link to the default page of the project that they name."
77 :type 'boolean
78 :group 'muse-wiki)
80 (defvar muse-wiki-interwiki-regexp ""
81 "Regexp that matches all interwiki links.
82 This is automatically generated by setting `muse-wiki-interwiki-alist'.
83 It can also be set by calling `muse-wiki-update-interwiki-regexp'.")
85 (defcustom muse-wiki-interwiki-delimiter "#\\|::"
86 "Delimiter regexp used for InterWiki links.
87 If you use groups, use only shy groups."
88 :type 'regexp
89 :group 'muse-wiki)
91 (defcustom muse-wiki-interwiki-replacement ": "
92 "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
93 InterWiki link descriptions.
95 If you want this replacement to happen, you must add
96 `muse-wiki-publish-pretty-interwiki' to
97 `muse-publish-desc-transforms'."
98 :type 'regexp
99 :group 'muse-wiki)
101 (defun muse-wiki-update-interwiki-regexp (value)
102 "Update the value of `muse-wiki-interwiki-regexp' based on VALUE
103 and `muse-project-alist'."
104 (setq muse-wiki-interwiki-regexp
105 (concat "\\<\\(" (mapconcat 'car muse-project-alist "\\|")
106 (when value (concat "\\|" (mapconcat 'car value "\\|")))
107 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
108 "\\)\\(\\sw+\\)\\)?\\>"))
109 (when (featurep 'muse-colors)
110 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
112 (defcustom muse-wiki-interwiki-alist
113 '(("EmacsWiki" . "http://www.emacswiki.org/cgi-bin/wiki/"))
114 "A table of WikiNames that refer to external entities.
115 The format of this table is an alist, or series of cons cells.
116 Each cons cell must be of the form:
118 (WIKINAME . STRING-OR-FUNCTION)
120 The second part of the cons cell may either be a STRING, which in most
121 cases should be a URL, or a FUNCTION. If a function, it will be
122 called with one argument: the tag applied to the Interwiki name, or
123 nil if no tag was used. If the cdr was a STRING and a tag is used,
124 the tag is simply appended.
126 Here are some examples:
128 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
130 Referring to [[JohnWiki::EmacsModules]] then really means:
132 http://alice.dynodns.net/wiki?EmacsModules
134 If a function is used for the replacement text, you can get creative
135 depending on what the tag is. Tags may contain any alphabetic
136 character, any number, % or _. If you need other special characters,
137 use % to specify the hex code, as in %2E. All browsers should support
138 this."
139 :type '(repeat (cons (string :tag "WikiName")
140 (choice (string :tag "URL") function)))
141 :set (function
142 (lambda (sym value)
143 (muse-wiki-update-interwiki-regexp value)
144 (set sym value)))
145 :group 'muse-wiki)
147 (defun muse-wiki-resolve-project-page (&optional project page)
148 "Return the published path from the current page to PAGE of PROJECT.
149 If PAGE is not specified, use the value of :default in PROJECT.
150 If PROJECT is not specified, default to first project of
151 `muse-projects-alist'.
153 Note that PAGE can have several output directories. If this is
154 the case, we will use the first one that matches our current
155 style and ignore the others."
156 (setq project (or project (caar muse-project-alist))
157 page (or page (muse-get-keyword :default
158 (cadr (muse-project project)))))
159 (let* ((page-path (muse-project-page-file page project))
160 (remote-style (when page-path (car (muse-project-applicable-styles
161 page-path project))))
162 (local-style (car (muse-project-applicable-styles
163 (muse-current-file)
164 (cddr (muse-project-of-file))))))
165 (cond ((and remote-style local-style muse-publishing-p)
166 (muse-publish-link-file
167 (file-relative-name (expand-file-name
168 page (muse-style-element :path remote-style))
169 (expand-file-name
170 (muse-style-element :path local-style)))
171 nil remote-style))
172 ((not muse-publishing-p)
173 (if page-path
174 page-path
175 (when muse-wiki-allow-nonexistent-wikiword
176 ;; make a path to a nonexistent file in project
177 (setq page-path (expand-file-name
178 page (car (cadr (muse-project project)))))
179 (if (and muse-file-extension
180 (not (string= muse-file-extension "")))
181 (concat page-path "." muse-file-extension)
182 page-path)))))))
184 (defun muse-wiki-handle-interwiki (&optional string)
185 "If STRING or point has an interwiki link, resolve it and
186 return the first match.
187 Match 1 is set to the link.
188 Match 2 is set to the description."
189 (when (if string (string-match muse-wiki-interwiki-regexp string)
190 (looking-at muse-wiki-interwiki-regexp))
191 (let* ((project (match-string 1 string))
192 (subst (cdr (assoc project muse-wiki-interwiki-alist)))
193 (word (match-string 2 string)))
194 (if subst
195 (if (functionp subst)
196 (funcall subst word)
197 (concat subst word))
198 (and (assoc project muse-project-alist)
199 (or word (not muse-wiki-ignore-bare-project-names))
200 (muse-wiki-resolve-project-page project word))))))
202 (defun muse-wiki-handle-wikiword (&optional string)
203 "If STRING or point has a WikiWord, return it.
204 Match 1 is set to the WikiWord."
205 (when (and muse-wiki-use-wikiword
206 (if string
207 (string-match muse-wiki-wikiword-regexp string)
208 (looking-at muse-wiki-wikiword-regexp))
209 (or muse-wiki-allow-nonexistent-wikiword
210 (and (muse-project-of-file)
211 (muse-project-page-file
212 (match-string 1 string) muse-current-project t))
213 (file-exists-p (match-string 1 string))))
214 (match-string 1 string)))
216 ;; Prettifications
218 (defcustom muse-wiki-publish-small-title-words
219 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
220 "Strings that should be downcased in a page title.
221 This is used by `muse-wiki-publish-pretty-title', which must be
222 called manually."
223 :type '(repeat string)
224 :group 'muse-wiki)
226 (defun muse-wiki-publish-pretty-title (&optional title explicit)
227 "Return a pretty version of the given TITLE.
228 If EXPLICIT is non-nil, TITLE will be returned unmodified."
229 (unless title (setq title (muse-publishing-directive "title")))
230 (if (or explicit
231 (save-match-data (string-match muse-url-regexp title)))
232 title
233 (save-match-data
234 (let ((case-fold-search nil))
235 (while (string-match (concat "\\([" muse-regexp-lower
236 "]\\)\\([" muse-regexp-upper
237 "0-9]\\)")
238 title)
239 (setq title (replace-match "\\1 \\2" t nil title)))
240 (let* ((words (split-string title))
241 (w (cdr words)))
242 (while w
243 (if (member (downcase (car w))
244 muse-wiki-publish-small-title-words)
245 (setcar w (downcase (car w))))
246 (setq w (cdr w)))
247 (mapconcat 'identity words " "))))))
249 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
250 "Replace instances of `muse-wiki-interwiki-delimiter' with
251 `muse-wiki-interwiki-replacement'."
252 (if (or explicit
253 (save-match-data (string-match muse-url-regexp desc)))
254 desc
255 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
256 muse-wiki-interwiki-replacement
257 desc)))
259 ;; Coloring setup
261 (eval-after-load "muse-colors"
262 '(progn
263 (defun muse-wiki-colors-nop-tag (beg end)
264 (add-text-properties beg (+ beg 5)
265 '(invisible muse intangible t)))
266 (defun muse-colors-wikiword-separate ()
267 (add-text-properties (match-beginning 0) (match-end 0)
268 '(invisible muse intangible t)))
270 (add-to-list 'muse-colors-tags
271 '("nop" nil nil muse-wiki-colors-nop-tag)
274 (add-to-list 'muse-colors-markup
275 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
277 (add-to-list 'muse-colors-markup
278 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
280 (add-to-list 'muse-colors-markup
281 '("''''" ?\' muse-colors-wikiword-separate)
282 nil)
284 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
286 ;; Publishing setup
288 (eval-after-load "muse-publish"
289 '(progn
290 (add-to-list 'muse-publish-markup-regexps
291 '(3100 muse-wiki-interwiki-regexp 0 link)
293 (add-to-list 'muse-publish-markup-regexps
294 '(3200 muse-wiki-wikiword-regexp 0 link)
296 (add-to-list 'muse-publish-markup-regexps
297 '(3300 "''''" 0 "")
300 (custom-add-option 'muse-publish-desc-transforms
301 'muse-wiki-publish-pretty-interwiki)
302 (custom-add-option 'muse-publish-desc-transforms
303 'muse-wiki-publish-pretty-title)))
305 ;; Insinuate link handling
307 (custom-add-option 'muse-implicit-link-functions
308 'muse-wiki-handle-interwiki)
309 (custom-add-option 'muse-implicit-link-functions
310 'muse-wiki-handle-wikiword)
312 (custom-add-option 'muse-explicit-link-functions
313 'muse-wiki-handle-interwiki)
315 (add-to-list 'muse-implicit-link-functions
316 'muse-wiki-handle-interwiki t)
317 (add-to-list 'muse-implicit-link-functions
318 'muse-wiki-handle-wikiword t)
320 (add-to-list 'muse-explicit-link-functions
321 'muse-wiki-handle-interwiki t)
323 ;; Update several things when Muse mode is entered
324 (defun muse-wiki-update-custom-values ()
325 "Update some important muse-wiki values that may have been altered manually."
326 (muse-wiki-update-interwiki-regexp muse-wiki-interwiki-alist))
328 (custom-add-option 'muse-mode-hook
329 'muse-wiki-update-custom-values)
331 (add-hook 'muse-mode-hook
332 'muse-wiki-update-custom-values)
334 (provide 'muse-wiki)
335 ;;; muse-wiki.el ends here