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>
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.
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.
34 (require 'muse-regexps
)
38 (require 'muse-colors
))
40 (defgroup muse-wiki nil
41 "Options controlling the behavior of Emacs Muse Wiki features."
44 (defcustom muse-wiki-use-wikiword t
45 "Whether to use color and publish bare WikiNames."
49 (defcustom muse-wiki-allow-nonexistent-wikiword nil
50 "Whether to color bare WikiNames that don't have an existing file."
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
59 If non-nil, Muse will color and publish implicit links to any
60 file in your project."
64 (defvar muse-wiki-updating-wikiword-p nil
65 "Prevent recursive calls to `muse-wiki-update-local-wikiword-regexp'.")
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
)
81 ;; append the files from the project
83 (muse-project-file-alist (muse-project))
86 (default-value 'muse-wiki-wikiword-regexp
)
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'."
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."
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
118 When nil, project names without a specifier are highlighted and
119 they link to the default page of the project that they name."
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."
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'."
145 (defvar muse-wiki-interwiki-alist
))
147 (defun muse-wiki-update-interwiki-regexp ()
148 "Update the value of `muse-wiki-interwiki-regexp' based on
149 `muse-wiki-interwiki-alist' and `muse-project-alist'."
150 (when muse-wiki-match-all-project-files
151 (make-local-variable 'muse-wiki-interwiki-regexp
))
152 (setq muse-wiki-interwiki-regexp
153 (concat "\\<\\(" (mapconcat 'car muse-project-alist
"\\|")
154 (when muse-wiki-interwiki-alist
155 (concat "\\|" (mapconcat 'car muse-wiki-interwiki-alist
157 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
159 (when muse-wiki-match-all-project-files
160 ;; append the files from the project
163 (muse-project-file-alist (muse-project))
167 (when (featurep 'muse-colors
)
168 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
170 (defcustom muse-wiki-interwiki-alist
171 '(("EmacsWiki" .
"http://www.emacswiki.org/cgi-bin/wiki/"))
172 "A table of WikiNames that refer to external entities.
173 The format of this table is an alist, or series of cons cells.
174 Each cons cell must be of the form:
176 (WIKINAME . STRING-OR-FUNCTION)
178 The second part of the cons cell may either be a STRING, which in most
179 cases should be a URL, or a FUNCTION. If a function, it will be
180 called with one argument: the tag applied to the Interwiki name, or
181 nil if no tag was used. If the cdr was a STRING and a tag is used,
182 the tag is simply appended.
184 Here are some examples:
186 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
188 Referring to [[JohnWiki::EmacsModules]] then really means:
190 http://alice.dynodns.net/wiki?EmacsModules
192 If a function is used for the replacement text, you can get creative
193 depending on what the tag is. Tags may contain any alphabetic
194 character, any number, % or _. If you need other special characters,
195 use % to specify the hex code, as in %2E. All browsers should support
197 :type
'(repeat (cons (string :tag
"WikiName")
198 (choice (string :tag
"URL") function
)))
202 (muse-wiki-update-interwiki-regexp)))
205 (add-hook 'muse-update-values-hook
206 'muse-wiki-update-interwiki-regexp
)
208 (defun muse-wiki-resolve-project-page (&optional project page
)
209 "Return the published path from the current page to PAGE of PROJECT.
210 If PAGE is not specified, use the value of :default in PROJECT.
211 If PROJECT is not specified, default to first project of
212 `muse-projects-alist'.
214 Note that PAGE can have several output directories. If this is
215 the case, we will use the first one that matches our current
216 style and ignore the others."
217 (setq project
(or (and project
218 (muse-project project
))
219 (car muse-project-alist
))
220 page
(or page
(muse-get-keyword :default
222 (let* ((page-path (muse-project-page-file page project
))
223 (remote-style (when page-path
(car (muse-project-applicable-styles
224 page-path
(cddr project
)))))
225 (local-style (car (muse-project-applicable-styles
227 (cddr (muse-project-of-file))))))
228 (cond ((and remote-style local-style muse-publishing-p
)
229 (let ((prefix (muse-style-element :base-url remote-style
)))
230 (muse-publish-link-file
233 (file-relative-name (expand-file-name
235 (muse-style-element :path remote-style
))
237 (muse-style-element :path local-style
))))
239 ((not muse-publishing-p
)
242 (when muse-wiki-allow-nonexistent-wikiword
243 ;; make a path to a nonexistent file in project
244 (setq page-path
(expand-file-name
245 page
(car (cadr project
))))
246 (if (and muse-file-extension
247 (not (string= muse-file-extension
"")))
248 (concat page-path
"." muse-file-extension
)
251 (defun muse-wiki-handle-interwiki (&optional string
)
252 "If STRING or point has an interwiki link, resolve it and
253 return the first match.
254 Match 1 is set to the link.
255 Match 2 is set to the description."
256 (when (if string
(string-match muse-wiki-interwiki-regexp string
)
257 (looking-at muse-wiki-interwiki-regexp
))
258 (let* ((project (match-string 1 string
))
259 (subst (cdr (assoc project muse-wiki-interwiki-alist
)))
261 (and (match-beginning 2)
262 (substring string
(match-beginning 2)))
263 (match-string 2 string
))))
265 (if (functionp subst
)
268 (and (assoc project muse-project-alist
)
269 (or word
(not muse-wiki-ignore-bare-project-names
))
270 (muse-wiki-resolve-project-page project word
))))))
272 (defun muse-wiki-handle-wikiword (&optional string
)
273 "If STRING or point has a WikiWord, return it.
274 Match 1 is set to the WikiWord."
275 (when (and muse-wiki-use-wikiword
277 (string-match muse-wiki-wikiword-regexp string
)
278 (looking-at muse-wiki-wikiword-regexp
))
279 (or muse-wiki-allow-nonexistent-wikiword
280 (and (muse-project-of-file)
281 (muse-project-page-file
282 (match-string 1 string
) muse-current-project t
))
283 (file-exists-p (match-string 1 string
))))
284 (match-string 1 string
)))
288 (defcustom muse-wiki-publish-small-title-words
289 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
290 "Strings that should be downcased in a page title.
291 This is used by `muse-wiki-publish-pretty-title', which must be
293 :type
'(repeat string
)
296 (defcustom muse-wiki-hide-nop-tag t
297 "If non-nil, hide <nop> tags when coloring a Muse buffer."
301 (defun muse-wiki-publish-pretty-title (&optional title explicit
)
302 "Return a pretty version of the given TITLE.
303 If EXPLICIT is non-nil, TITLE will be returned unmodified."
304 (unless title
(setq title
(or (muse-publishing-directive "title") "")))
306 (save-match-data (string-match muse-url-regexp title
)))
309 (let ((case-fold-search nil
))
310 (while (string-match (concat "\\([" muse-regexp-lower
311 "]\\)\\([" muse-regexp-upper
314 (setq title
(replace-match "\\1 \\2" t nil title
)))
315 (let* ((words (split-string title
))
318 (if (member (downcase (car w
))
319 muse-wiki-publish-small-title-words
)
320 (setcar w
(downcase (car w
))))
322 (mapconcat 'identity words
" "))))))
324 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit
)
325 "Replace instances of `muse-wiki-interwiki-delimiter' with
326 `muse-wiki-interwiki-replacement'."
328 (save-match-data (string-match muse-url-regexp desc
)))
330 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
331 muse-wiki-interwiki-replacement
336 (eval-after-load "muse-colors"
338 (defun muse-wiki-colors-nop-tag (beg end
)
339 (when muse-wiki-hide-nop-tag
340 (add-text-properties beg
(+ beg
5)
341 '(invisible muse intangible t
))))
342 (defun muse-colors-wikiword-separate ()
343 (add-text-properties (match-beginning 0) (match-end 0)
344 '(invisible muse intangible t
)))
346 (add-to-list 'muse-colors-tags
347 '("nop" nil nil muse-wiki-colors-nop-tag
)
350 (add-to-list 'muse-colors-markup
351 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link
)
353 (add-to-list 'muse-colors-markup
354 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link
)
356 (add-to-list 'muse-colors-markup
357 '("''''" ?
\' muse-colors-wikiword-separate
)
360 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
364 (eval-after-load "muse-publish"
366 (add-to-list 'muse-publish-markup-regexps
367 '(3100 muse-wiki-interwiki-regexp
0 link
)
369 (add-to-list 'muse-publish-markup-regexps
370 '(3200 muse-wiki-wikiword-regexp
0 link
)
372 (add-to-list 'muse-publish-markup-regexps
376 (custom-add-option 'muse-publish-desc-transforms
377 'muse-wiki-publish-pretty-interwiki
)
378 (custom-add-option 'muse-publish-desc-transforms
379 'muse-wiki-publish-pretty-title
)))
381 ;; Insinuate link handling
383 (custom-add-option 'muse-implicit-link-functions
384 'muse-wiki-handle-interwiki
)
385 (custom-add-option 'muse-implicit-link-functions
386 'muse-wiki-handle-wikiword
)
388 (custom-add-option 'muse-explicit-link-functions
389 'muse-wiki-handle-interwiki
)
391 (add-to-list 'muse-implicit-link-functions
392 'muse-wiki-handle-interwiki t
)
393 (add-to-list 'muse-implicit-link-functions
394 'muse-wiki-handle-wikiword t
)
396 (add-to-list 'muse-explicit-link-functions
397 'muse-wiki-handle-interwiki t
)
399 ;; Obsolete functions
401 (defun muse-wiki-update-custom-values ()
402 (muse-display-warning
403 (concat "Please remove `muse-wiki-update-custom-values' from"
404 " `muse-mode-hook'. Its use is now deprecated.")))
407 ;;; muse-wiki.el ends here