Permit table generation to be disabled for a single Muse page.
[muse-el.git] / lisp / muse-wiki.el
blobc348996652fea15115e71994accd21c2ef171cc4
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 (defvar muse-wiki-updating-wikiword-p nil
65 "Prevent recursive calls to `muse-wiki-update-local-wikiword-regexp'.")
67 (eval-when-compile
68 (defvar muse-wiki-wikiword-regexp))
70 (defun muse-wiki-update-local-wikiword-regexp ()
71 "Update a local copy of `muse-wiki-wikiword-regexp' to include
72 all the files in the project."
73 ;; see if the user wants to append project files
74 (when (and muse-wiki-use-wikiword
75 muse-wiki-match-all-project-files
76 (not muse-wiki-updating-wikiword-p))
77 (let ((muse-wiki-updating-wikiword-p t))
78 ;; make the regexp local
79 (set (make-local-variable 'muse-wiki-wikiword-regexp)
80 (concat "\\(\\<\\(?:"
81 ;; append the files from the project
82 (mapconcat 'car
83 (muse-project-file-alist (muse-project))
84 "\\|")
85 "\\)\\>\\|\\(?:"
86 (default-value 'muse-wiki-wikiword-regexp)
87 "\\)\\)"))
88 ;; update coloring setup
89 (when (featurep 'muse-colors)
90 (muse-configure-highlighting
91 'muse-colors-markup muse-colors-markup)))))
93 (add-hook 'muse-update-values-hook
94 'muse-wiki-update-local-wikiword-regexp)
95 (add-hook 'muse-project-file-alist-hook
96 'muse-wiki-update-local-wikiword-regexp)
98 (defun muse-wiki-update-wikiword-regexp (sym val)
99 "Update everything related to `muse-wiki-wikiword-regexp'."
100 (set sym val)
101 (muse-wiki-update-local-wikiword-regexp)
102 (when (featurep 'muse-colors)
103 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
105 (defcustom muse-wiki-wikiword-regexp
106 (concat "\\<\\(\\(?:[" muse-regexp-upper
107 "]+[" muse-regexp-lower "]+\\)\\(?:["
108 muse-regexp-upper "]+[" muse-regexp-lower "]+\\)+\\)")
109 "Regexp used to match WikiWords."
110 :type 'regexp
111 :group 'muse-wiki
112 :set 'muse-wiki-update-wikiword-regexp)
114 (defcustom muse-wiki-ignore-bare-project-names nil
115 "Determine whether project names without a page specifer are links.
116 If non-nil, project names without a page specifier will not be
117 considered links.
118 When nil, project names without a specifier are highlighted and
119 they link to the default page of the project that they name."
120 :type 'boolean
121 :group 'muse-wiki)
123 (defvar muse-wiki-interwiki-regexp ""
124 "Regexp that matches all interwiki links.
125 This is automatically generated by setting `muse-wiki-interwiki-alist'.
126 It can also be set by calling `muse-wiki-update-interwiki-regexp'.")
128 (defcustom muse-wiki-interwiki-delimiter "#\\|::"
129 "Delimiter regexp used for InterWiki links.
130 If you use groups, use only shy groups."
131 :type 'regexp
132 :group 'muse-wiki)
134 (defcustom muse-wiki-interwiki-replacement ": "
135 "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
136 InterWiki link descriptions.
138 If you want this replacement to happen, you must add
139 `muse-wiki-publish-pretty-interwiki' to
140 `muse-publish-desc-transforms'."
141 :type 'regexp
142 :group 'muse-wiki)
144 (defun muse-wiki-update-interwiki-regexp (value)
145 "Update the value of `muse-wiki-interwiki-regexp' based on VALUE
146 and `muse-project-alist'."
147 (setq muse-wiki-interwiki-regexp
148 (concat "\\<\\(" (mapconcat 'car muse-project-alist "\\|")
149 (when value (concat "\\|" (mapconcat 'car value "\\|")))
150 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
151 "\\)\\(\\sw+\\)\\)?\\>"))
152 (when (featurep 'muse-colors)
153 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
155 (defcustom muse-wiki-interwiki-alist
156 '(("EmacsWiki" . "http://www.emacswiki.org/cgi-bin/wiki/"))
157 "A table of WikiNames that refer to external entities.
158 The format of this table is an alist, or series of cons cells.
159 Each cons cell must be of the form:
161 (WIKINAME . STRING-OR-FUNCTION)
163 The second part of the cons cell may either be a STRING, which in most
164 cases should be a URL, or a FUNCTION. If a function, it will be
165 called with one argument: the tag applied to the Interwiki name, or
166 nil if no tag was used. If the cdr was a STRING and a tag is used,
167 the tag is simply appended.
169 Here are some examples:
171 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
173 Referring to [[JohnWiki::EmacsModules]] then really means:
175 http://alice.dynodns.net/wiki?EmacsModules
177 If a function is used for the replacement text, you can get creative
178 depending on what the tag is. Tags may contain any alphabetic
179 character, any number, % or _. If you need other special characters,
180 use % to specify the hex code, as in %2E. All browsers should support
181 this."
182 :type '(repeat (cons (string :tag "WikiName")
183 (choice (string :tag "URL") function)))
184 :set (function
185 (lambda (sym value)
186 (muse-wiki-update-interwiki-regexp value)
187 (set sym value)))
188 :group 'muse-wiki)
190 (add-hook 'muse-update-values-hook
191 (lambda ()
192 (muse-wiki-update-interwiki-regexp muse-wiki-interwiki-alist)))
194 (defun muse-wiki-resolve-project-page (&optional project page)
195 "Return the published path from the current page to PAGE of PROJECT.
196 If PAGE is not specified, use the value of :default in PROJECT.
197 If PROJECT is not specified, default to first project of
198 `muse-projects-alist'.
200 Note that PAGE can have several output directories. If this is
201 the case, we will use the first one that matches our current
202 style and ignore the others."
203 (setq project (or (and project
204 (muse-project project))
205 (car muse-project-alist))
206 page (or page (muse-get-keyword :default
207 (cadr project))))
208 (let* ((page-path (muse-project-page-file page project))
209 (remote-style (when page-path (car (muse-project-applicable-styles
210 page-path (cddr project)))))
211 (local-style (car (muse-project-applicable-styles
212 (muse-current-file)
213 (cddr (muse-project-of-file))))))
214 (cond ((and remote-style local-style muse-publishing-p)
215 (let ((prefix (muse-style-element :base-url remote-style)))
216 (muse-publish-link-file
217 (if prefix
218 (concat prefix page)
219 (file-relative-name (expand-file-name
220 page
221 (muse-style-element :path remote-style))
222 (expand-file-name
223 (muse-style-element :path local-style))))
224 nil remote-style)))
225 ((not muse-publishing-p)
226 (if page-path
227 page-path
228 (when muse-wiki-allow-nonexistent-wikiword
229 ;; make a path to a nonexistent file in project
230 (setq page-path (expand-file-name
231 page (car (cadr project))))
232 (if (and muse-file-extension
233 (not (string= muse-file-extension "")))
234 (concat page-path "." muse-file-extension)
235 page-path)))))))
237 (defun muse-wiki-handle-interwiki (&optional string)
238 "If STRING or point has an interwiki link, resolve it and
239 return the first match.
240 Match 1 is set to the link.
241 Match 2 is set to the description."
242 (when (if string (string-match muse-wiki-interwiki-regexp string)
243 (looking-at muse-wiki-interwiki-regexp))
244 (let* ((project (match-string 1 string))
245 (subst (cdr (assoc project muse-wiki-interwiki-alist)))
246 (word (if string
247 (and (match-beginning 2)
248 (substring string (match-beginning 2)))
249 (match-string 2 string))))
250 (if subst
251 (if (functionp subst)
252 (funcall subst word)
253 (concat subst word))
254 (and (assoc project muse-project-alist)
255 (or word (not muse-wiki-ignore-bare-project-names))
256 (muse-wiki-resolve-project-page project word))))))
258 (defun muse-wiki-handle-wikiword (&optional string)
259 "If STRING or point has a WikiWord, return it.
260 Match 1 is set to the WikiWord."
261 (when (and muse-wiki-use-wikiword
262 (if string
263 (string-match muse-wiki-wikiword-regexp string)
264 (looking-at muse-wiki-wikiword-regexp))
265 (or muse-wiki-allow-nonexistent-wikiword
266 (and (muse-project-of-file)
267 (muse-project-page-file
268 (match-string 1 string) muse-current-project t))
269 (file-exists-p (match-string 1 string))))
270 (match-string 1 string)))
272 ;; Prettifications
274 (defcustom muse-wiki-publish-small-title-words
275 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
276 "Strings that should be downcased in a page title.
277 This is used by `muse-wiki-publish-pretty-title', which must be
278 called manually."
279 :type '(repeat string)
280 :group 'muse-wiki)
282 (defcustom muse-wiki-hide-nop-tag t
283 "If non-nil, hide <nop> tags when coloring a Muse buffer."
284 :type 'boolean
285 :group 'muse-wiki)
287 (defun muse-wiki-publish-pretty-title (&optional title explicit)
288 "Return a pretty version of the given TITLE.
289 If EXPLICIT is non-nil, TITLE will be returned unmodified."
290 (unless title (setq title (or (muse-publishing-directive "title") "")))
291 (if (or explicit
292 (save-match-data (string-match muse-url-regexp title)))
293 title
294 (save-match-data
295 (let ((case-fold-search nil))
296 (while (string-match (concat "\\([" muse-regexp-lower
297 "]\\)\\([" muse-regexp-upper
298 "0-9]\\)")
299 title)
300 (setq title (replace-match "\\1 \\2" t nil title)))
301 (let* ((words (split-string title))
302 (w (cdr words)))
303 (while w
304 (if (member (downcase (car w))
305 muse-wiki-publish-small-title-words)
306 (setcar w (downcase (car w))))
307 (setq w (cdr w)))
308 (mapconcat 'identity words " "))))))
310 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit)
311 "Replace instances of `muse-wiki-interwiki-delimiter' with
312 `muse-wiki-interwiki-replacement'."
313 (if (or explicit
314 (save-match-data (string-match muse-url-regexp desc)))
315 desc
316 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
317 muse-wiki-interwiki-replacement
318 desc)))
320 ;; Coloring setup
322 (eval-after-load "muse-colors"
323 '(progn
324 (defun muse-wiki-colors-nop-tag (beg end)
325 (add-text-properties beg (+ beg 5)
326 '(invisible muse intangible t)))
327 (defun muse-colors-wikiword-separate ()
328 (add-text-properties (match-beginning 0) (match-end 0)
329 '(invisible muse intangible t)))
331 (add-to-list 'muse-colors-tags
332 '("nop" nil nil muse-wiki-colors-nop-tag)
335 (add-to-list 'muse-colors-markup
336 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link)
338 (add-to-list 'muse-colors-markup
339 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link)
341 (add-to-list 'muse-colors-markup
342 '("''''" ?\' muse-colors-wikiword-separate)
343 nil)
345 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup)))
347 ;; Publishing setup
349 (eval-after-load "muse-publish"
350 '(progn
351 (add-to-list 'muse-publish-markup-regexps
352 '(3100 muse-wiki-interwiki-regexp 0 link)
354 (add-to-list 'muse-publish-markup-regexps
355 '(3200 muse-wiki-wikiword-regexp 0 link)
357 (add-to-list 'muse-publish-markup-regexps
358 '(3300 "''''" 0 "")
361 (custom-add-option 'muse-publish-desc-transforms
362 'muse-wiki-publish-pretty-interwiki)
363 (custom-add-option 'muse-publish-desc-transforms
364 'muse-wiki-publish-pretty-title)))
366 ;; Insinuate link handling
368 (custom-add-option 'muse-implicit-link-functions
369 'muse-wiki-handle-interwiki)
370 (custom-add-option 'muse-implicit-link-functions
371 'muse-wiki-handle-wikiword)
373 (custom-add-option 'muse-explicit-link-functions
374 'muse-wiki-handle-interwiki)
376 (add-to-list 'muse-implicit-link-functions
377 'muse-wiki-handle-interwiki t)
378 (add-to-list 'muse-implicit-link-functions
379 'muse-wiki-handle-wikiword t)
381 (add-to-list 'muse-explicit-link-functions
382 'muse-wiki-handle-interwiki t)
384 ;; Obsolete functions
386 (defun muse-wiki-update-custom-values ()
387 (muse-display-warning
388 (concat "Please remove `muse-wiki-update-custom-values' from"
389 " `muse-mode-hook'. Its use is now deprecated.")))
391 (provide 'muse-wiki)
392 ;;; muse-wiki.el ends here