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.
31 (require 'muse-regexps
)
35 (require 'muse-colors
))
37 (defgroup muse-wiki nil
38 "Options controlling the behavior of Emacs Muse Wiki features."
41 (defun muse-wiki-update-wikiword-regexp (sym val
)
42 "Update everything related to `muse-wiki-wikiword-regexp'."
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."
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."
59 :set
'muse-wiki-update-wikiword-regexp
)
61 (defcustom muse-wiki-use-wikiword t
62 "Whether to use color and publish bare WikiNames."
66 (defcustom muse-wiki-allow-nonexistent-wikiword nil
67 "Whether to color bare WikiNames that don't have an existing file."
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
75 When nil, project names without a specifier are highlighted and
76 they link to the default page of the project that they name."
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."
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'."
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
139 :type
'(repeat (cons (string :tag
"WikiName")
140 (choice (string :tag
"URL") function
)))
143 (muse-wiki-update-interwiki-regexp value
)
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
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
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
172 (file-relative-name (expand-file-name
174 (muse-style-element :path remote-style
))
176 (muse-style-element :path local-style
))))
178 ((not muse-publishing-p
)
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
)
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
)))
200 (and (match-beginning 2)
201 (substring string
(match-beginning 2)))
202 (match-string 2 string
))))
204 (if (functionp subst
)
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
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
)))
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
232 :type
'(repeat string
)
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") "")))
240 (save-match-data (string-match muse-url-regexp title
)))
243 (let ((case-fold-search nil
))
244 (while (string-match (concat "\\([" muse-regexp-lower
245 "]\\)\\([" muse-regexp-upper
248 (setq title
(replace-match "\\1 \\2" t nil title
)))
249 (let* ((words (split-string title
))
252 (if (member (downcase (car w
))
253 muse-wiki-publish-small-title-words
)
254 (setcar w
(downcase (car 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'."
262 (save-match-data (string-match muse-url-regexp desc
)))
264 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
265 muse-wiki-interwiki-replacement
270 (eval-after-load "muse-colors"
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
)
293 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
297 (eval-after-load "muse-publish"
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
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
)
344 ;;; muse-wiki.el ends here