Put movement by list and paragraph functions in muse.el.
[muse-el.git] / lisp / muse-wiki.el
blob363a81cca624e4f3a838c318cf077881a4727683
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 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 (and project
157 (muse-project project))
158 (car muse-project-alist))
159 page (or page (muse-get-keyword :default
160 (cadr project))))
161 (let* ((page-path (muse-project-page-file page project))
162 (remote-style (when page-path (car (muse-project-applicable-styles
163 page-path (cddr project)))))
164 (local-style (car (muse-project-applicable-styles
165 (muse-current-file)
166 (cddr (muse-project-of-file))))))
167 (cond ((and remote-style local-style muse-publishing-p)
168 (let ((prefix (muse-style-element :base-url remote-style)))
169 (muse-publish-link-file
170 (if prefix
171 (concat prefix page)
172 (file-relative-name (expand-file-name
173 page
174 (muse-style-element :path remote-style))
175 (expand-file-name
176 (muse-style-element :path local-style))))
177 nil remote-style)))
178 ((not muse-publishing-p)
179 (if page-path
180 page-path
181 (when muse-wiki-allow-nonexistent-wikiword
182 ;; make a path to a nonexistent file in project
183 (setq page-path (expand-file-name
184 page (car (cadr project))))
185 (if (and muse-file-extension
186 (not (string= muse-file-extension "")))
187 (concat page-path "." muse-file-extension)
188 page-path)))))))
190 (defun muse-wiki-handle-interwiki (&optional string)
191 "If STRING or point has an interwiki link, resolve it and
192 return the first match.
193 Match 1 is set to the link.
194 Match 2 is set to the description."
195 (when (if string (string-match muse-wiki-interwiki-regexp string)
196 (looking-at muse-wiki-interwiki-regexp))
197 (let* ((project (match-string 1 string))
198 (subst (cdr (assoc project muse-wiki-interwiki-alist)))
199 (word (if string
200 (and (match-beginning 2)
201 (substring string (match-beginning 2)))
202 (match-string 2 string))))
203 (if subst
204 (if (functionp subst)
205 (funcall subst word)
206 (concat subst word))
207 (and (assoc project muse-project-alist)
208 (or word (not muse-wiki-ignore-bare-project-names))
209 (muse-wiki-resolve-project-page project word))))))
211 (defun muse-wiki-handle-wikiword (&optional string)
212 "If STRING or point has a WikiWord, return it.
213 Match 1 is set to the WikiWord."
214 (when (and muse-wiki-use-wikiword
215 (if string
216 (string-match muse-wiki-wikiword-regexp string)
217 (looking-at muse-wiki-wikiword-regexp))
218 (or muse-wiki-allow-nonexistent-wikiword
219 (and (muse-project-of-file)
220 (muse-project-page-file
221 (match-string 1 string) muse-current-project t))
222 (file-exists-p (match-string 1 string))))
223 (match-string 1 string)))
225 ;; Prettifications
227 (defcustom muse-wiki-publish-small-title-words
228 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
229 "Strings that should be downcased in a page title.
230 This is used by `muse-wiki-publish-pretty-title', which must be
231 called manually."
232 :type '(repeat string)
233 :group 'muse-wiki)
235 (defun muse-wiki-publish-pretty-title (&optional title explicit)
236 "Return a pretty version of the given TITLE.
237 If EXPLICIT is non-nil, TITLE will be returned unmodified."
238 (unless title (setq title (or (muse-publishing-directive "title") "")))
239 (if (or explicit
240 (save-match-data (string-match muse-url-regexp title)))
241 title
242 (save-match-data
243 (let ((case-fold-search nil))
244 (while (string-match (concat "\\([" muse-regexp-lower
245 "]\\)\\([" muse-regexp-upper
246 "0-9]\\)")
247 title)
248 (setq title (replace-match "\\1 \\2" t nil title)))
249 (let* ((words (split-string title))
250 (w (cdr words)))
251 (while w
252 (if (member (downcase (car w))
253 muse-wiki-publish-small-title-words)
254 (setcar w (downcase (car w))))
255 (setq w (cdr w)))
256 (mapconcat 'identity words " "))))))
258 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
259 "Replace instances of `muse-wiki-interwiki-delimiter' with
260 `muse-wiki-interwiki-replacement'."
261 (if (or explicit
262 (save-match-data (string-match muse-url-regexp desc)))
263 desc
264 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
265 muse-wiki-interwiki-replacement
266 desc)))
268 ;; Coloring setup
270 (eval-after-load "muse-colors"
271 '(progn
272 (defun muse-wiki-colors-nop-tag (beg end)
273 (add-text-properties beg (+ beg 5)
274 '(invisible muse intangible t)))
275 (defun muse-colors-wikiword-separate ()
276 (add-text-properties (match-beginning 0) (match-end 0)
277 '(invisible muse intangible t)))
279 (add-to-list 'muse-colors-tags
280 '("nop" nil nil muse-wiki-colors-nop-tag)
283 (add-to-list 'muse-colors-markup
284 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
286 (add-to-list 'muse-colors-markup
287 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
289 (add-to-list 'muse-colors-markup
290 '("''''" ?\' muse-colors-wikiword-separate)
291 nil)
293 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
295 ;; Publishing setup
297 (eval-after-load "muse-publish"
298 '(progn
299 (add-to-list 'muse-publish-markup-regexps
300 '(3100 muse-wiki-interwiki-regexp 0 link)
302 (add-to-list 'muse-publish-markup-regexps
303 '(3200 muse-wiki-wikiword-regexp 0 link)
305 (add-to-list 'muse-publish-markup-regexps
306 '(3300 "''''" 0 "")
309 (custom-add-option 'muse-publish-desc-transforms
310 'muse-wiki-publish-pretty-interwiki)
311 (custom-add-option 'muse-publish-desc-transforms
312 'muse-wiki-publish-pretty-title)))
314 ;; Insinuate link handling
316 (custom-add-option 'muse-implicit-link-functions
317 'muse-wiki-handle-interwiki)
318 (custom-add-option 'muse-implicit-link-functions
319 'muse-wiki-handle-wikiword)
321 (custom-add-option 'muse-explicit-link-functions
322 'muse-wiki-handle-interwiki)
324 (add-to-list 'muse-implicit-link-functions
325 'muse-wiki-handle-interwiki t)
326 (add-to-list 'muse-implicit-link-functions
327 'muse-wiki-handle-wikiword t)
329 (add-to-list 'muse-explicit-link-functions
330 'muse-wiki-handle-interwiki t)
332 ;; Update several things when Muse mode is entered
333 (defun muse-wiki-update-custom-values ()
334 "Update some important muse-wiki values that may have been altered manually."
335 (muse-wiki-update-interwiki-regexp muse-wiki-interwiki-alist))
337 (custom-add-option 'muse-mode-hook
338 'muse-wiki-update-custom-values)
340 (add-hook 'muse-mode-hook
341 'muse-wiki-update-custom-values)
343 (provide 'muse-wiki)
344 ;;; muse-wiki.el ends here