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 (defcustom muse-wiki-ignore-implicit-links-to-current-page nil
65 "Whether to ignore implicit links to the current page.
67 If non-nil, Muse will not recognize implicit links to the current
68 page, both when formatting and publishing."
73 (defvar muse-wiki-wikiword-regexp
))
75 (defun muse-wiki-update-local-wikiword-regexp ()
76 "Update a local copy of `muse-wiki-wikiword-regexp' to include
77 all the files in the project."
78 ;; see if the user wants to append project files
79 (when (and muse-wiki-use-wikiword
80 muse-wiki-match-all-project-files
)
81 ;; make the regexp local
82 (set (make-local-variable 'muse-wiki-wikiword-regexp
)
84 ;; append the files from the project
86 (muse-project-file-alist (muse-project))
89 (default-value 'muse-wiki-wikiword-regexp
)
91 ;; update coloring setup
92 (when (featurep 'muse-colors
)
93 (muse-configure-highlighting
94 'muse-colors-markup muse-colors-markup
))))
96 (add-hook 'muse-update-values-hook
97 'muse-wiki-update-local-wikiword-regexp
)
98 (add-hook 'muse-project-file-alist-hook
99 'muse-wiki-update-local-wikiword-regexp
)
101 (defun muse-wiki-update-wikiword-regexp (sym val
)
102 "Update everything related to `muse-wiki-wikiword-regexp'."
104 (muse-wiki-update-local-wikiword-regexp)
105 (when (featurep 'muse-colors
)
106 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
108 (defcustom muse-wiki-wikiword-regexp
109 (concat "\\<\\(\\(?:[" muse-regexp-upper
110 "]+[" muse-regexp-lower
"]+\\)\\(?:["
111 muse-regexp-upper
"]+[" muse-regexp-lower
"]+\\)+\\)")
112 "Regexp used to match WikiWords."
115 :set
'muse-wiki-update-wikiword-regexp
)
117 (defcustom muse-wiki-ignore-bare-project-names nil
118 "Determine whether project names without a page specifer are links.
119 If non-nil, project names without a page specifier will not be
121 When nil, project names without a specifier are highlighted and
122 they link to the default page of the project that they name."
126 (defvar muse-wiki-interwiki-regexp
""
127 "Regexp that matches all interwiki links.
128 This is automatically generated by setting `muse-wiki-interwiki-alist'.
129 It can also be set by calling `muse-wiki-update-interwiki-regexp'.")
131 (defcustom muse-wiki-interwiki-delimiter
"#\\|::"
132 "Delimiter regexp used for InterWiki links.
133 If you use groups, use only shy groups."
137 (defcustom muse-wiki-interwiki-replacement
": "
138 "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
139 InterWiki link descriptions.
141 If you want this replacement to happen, you must add
142 `muse-wiki-publish-pretty-interwiki' to
143 `muse-publish-desc-transforms'."
148 (defvar muse-wiki-interwiki-alist
))
150 (defun muse-wiki-project-files-with-spaces (&optional project
)
151 "Return a list of files in PROJECT that have spaces."
152 (setq project
(muse-project project
))
155 (mapcar (function (lambda (file)
156 (when (string-match " " (car file
))
157 (setq flist
(cons (car file
) flist
)))))
158 (muse-project-file-alist project
)))
161 (defun muse-wiki-update-interwiki-regexp ()
162 "Update the value of `muse-wiki-interwiki-regexp' based on
163 `muse-wiki-interwiki-alist' and `muse-project-alist'."
164 (setq muse-wiki-interwiki-regexp
165 (concat "\\<\\(" (mapconcat 'car muse-project-alist
"\\|")
166 (when muse-wiki-interwiki-alist
167 (concat "\\|" (mapconcat 'car muse-wiki-interwiki-alist
169 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
171 (when muse-wiki-match-all-project-files
172 ;; append the files from the project
178 (muse-wiki-project-files-with-spaces
181 muse-project-alist
"")
184 (when (featurep 'muse-colors
)
185 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
187 (defcustom muse-wiki-interwiki-alist
188 '(("EmacsWiki" .
"http://www.emacswiki.org/cgi-bin/wiki/"))
189 "A table of WikiNames that refer to external entities.
190 The format of this table is an alist, or series of cons cells.
191 Each cons cell must be of the form:
193 (WIKINAME . STRING-OR-FUNCTION)
195 The second part of the cons cell may either be a STRING, which in most
196 cases should be a URL, or a FUNCTION. If a function, it will be
197 called with one argument: the tag applied to the Interwiki name, or
198 nil if no tag was used. If the cdr was a STRING and a tag is used,
199 the tag is simply appended.
201 Here are some examples:
203 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
205 Referring to [[JohnWiki::EmacsModules]] then really means:
207 http://alice.dynodns.net/wiki?EmacsModules
209 If a function is used for the replacement text, you can get creative
210 depending on what the tag is. Tags may contain any alphabetic
211 character, any number, % or _. If you need other special characters,
212 use % to specify the hex code, as in %2E. All browsers should support
214 :type
'(repeat (cons (string :tag
"WikiName")
215 (choice (string :tag
"URL") function
)))
219 (muse-wiki-update-interwiki-regexp)))
222 (add-hook 'muse-update-values-hook
223 'muse-wiki-update-interwiki-regexp
)
225 (defun muse-wiki-choose-style-by-link-suffix (given-suffix style
)
226 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
228 (let ((link-suffix (muse-style-element :link-suffix style
)))
229 (and (stringp link-suffix
)
230 (string= given-suffix link-suffix
))))
232 (defun muse-wiki-resolve-project-page-1 (page local-styles remote-styles
)
233 "Pick the best match for PAGE given potential local and remote styles.
234 See `muse-wiki-resolve-project-page' for more information."
235 (let ((link-suffix (muse-style-element :link-suffix
))
236 remote-style local-style prefix
)
237 (if (not (stringp link-suffix
))
239 (setq remote-style
(car remote-styles
)
240 local-style
(car local-styles
)))
241 (setq remote-style
(muse-project-choose-style
243 #'muse-wiki-choose-style-by-link-suffix
245 local-style
(muse-project-choose-style
247 #'muse-wiki-choose-style-by-link-suffix
249 (setq prefix
(muse-style-element :base-url remote-style
))
250 (muse-publish-link-file
253 (file-relative-name (expand-file-name
255 (muse-style-element :path remote-style
))
257 (muse-style-element :path local-style
))))
260 (defun muse-wiki-resolve-project-page (&optional project page
)
261 "Return the published path from the current page to PAGE of PROJECT.
262 If PAGE is not specified, use the value of :default in PROJECT.
263 If PROJECT is not specified, default to first project of
264 `muse-projects-alist'.
266 Note that PAGE can have several output directories. If this is
267 the case, we will use the first one that matches our current
268 style and has the same link suffix, ignoring the others. If no
269 style has the same link suffix as the current publishing style,
270 use the first style we find."
271 (setq project
(or (and project
272 (muse-project project
))
273 (car muse-project-alist
))
274 page
(or page
(muse-get-keyword :default
276 (let* ((page-path (muse-project-page-file page project
))
277 (remote-styles (when page-path
(muse-project-applicable-styles
278 page-path
(cddr project
))))
279 (local-styles (muse-project-applicable-styles
281 (cddr (muse-project-of-file)))))
282 (cond ((and remote-styles local-styles muse-publishing-p
)
283 (muse-wiki-resolve-project-page-1 page local-styles remote-styles
))
284 ((not muse-publishing-p
)
287 (when muse-wiki-allow-nonexistent-wikiword
288 ;; make a path to a nonexistent file in project
289 (setq page-path
(expand-file-name
290 page
(car (cadr project
))))
291 (if (and muse-file-extension
292 (not (string= muse-file-extension
"")))
293 (concat page-path
"." muse-file-extension
)
296 (defun muse-wiki-handle-interwiki (&optional string
)
297 "If STRING or point has an interwiki link, resolve it and
298 return the first match.
299 Match 1 is set to the link.
300 Match 2 is set to the description."
301 (when (if string
(string-match muse-wiki-interwiki-regexp string
)
302 (looking-at muse-wiki-interwiki-regexp
))
303 (let* ((project (match-string 1 string
))
304 (subst (cdr (assoc project muse-wiki-interwiki-alist
)))
306 (and (match-beginning 2)
307 (substring string
(match-beginning 2)))
308 (match-string 2 string
))))
310 (if (functionp subst
)
313 (and (assoc project muse-project-alist
)
314 (or word
(not muse-wiki-ignore-bare-project-names
))
315 (muse-wiki-resolve-project-page project word
))))))
317 (defun muse-wiki-handle-wikiword (&optional string
)
318 "If STRING or point has a WikiWord, return it.
319 Match 1 is set to the WikiWord."
320 (when (and muse-wiki-use-wikiword
322 (string-match muse-wiki-wikiword-regexp string
)
323 (looking-at muse-wiki-wikiword-regexp
))
325 (muse-wiki-allow-nonexistent-wikiword
327 ((and muse-wiki-ignore-implicit-links-to-current-page
328 (string= (match-string 1 string
) (muse-page-name)))
330 ((and (muse-project-of-file)
331 (muse-project-page-file
332 (match-string 1 string
) muse-current-project t
))
334 ((file-exists-p (match-string 1 string
))
337 (match-string 1 string
)))
341 (defcustom muse-wiki-publish-small-title-words
342 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
343 "Strings that should be downcased in a page title.
344 This is used by `muse-wiki-publish-pretty-title', which must be
346 :type
'(repeat string
)
349 (defcustom muse-wiki-hide-nop-tag t
350 "If non-nil, hide <nop> tags when coloring a Muse buffer."
354 (defun muse-wiki-publish-pretty-title (&optional title explicit
)
355 "Return a pretty version of the given TITLE.
356 If EXPLICIT is non-nil, TITLE will be returned unmodified."
357 (unless title
(setq title
(or (muse-publishing-directive "title") "")))
359 (save-match-data (string-match muse-url-regexp title
)))
362 (let ((case-fold-search nil
))
363 (while (string-match (concat "\\([" muse-regexp-lower
364 "]\\)\\([" muse-regexp-upper
367 (setq title
(replace-match "\\1 \\2" t nil title
)))
368 (let* ((words (split-string title
))
371 (if (member (downcase (car w
))
372 muse-wiki-publish-small-title-words
)
373 (setcar w
(downcase (car w
))))
375 (mapconcat 'identity words
" "))))))
377 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit
)
378 "Replace instances of `muse-wiki-interwiki-delimiter' with
379 `muse-wiki-interwiki-replacement'."
381 (save-match-data (string-match muse-url-regexp desc
)))
383 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
384 muse-wiki-interwiki-replacement
389 (eval-after-load "muse-colors"
391 (defun muse-wiki-colors-nop-tag (beg end
)
392 (when muse-wiki-hide-nop-tag
393 (add-text-properties beg
(+ beg
5)
394 '(invisible muse intangible t
))))
395 (defun muse-colors-wikiword-separate ()
396 (add-text-properties (match-beginning 0) (match-end 0)
397 '(invisible muse intangible t
)))
399 (add-to-list 'muse-colors-tags
400 '("nop" nil nil muse-wiki-colors-nop-tag
)
403 (add-to-list 'muse-colors-markup
404 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link
)
406 (add-to-list 'muse-colors-markup
407 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link
)
409 (add-to-list 'muse-colors-markup
410 '("''''" ?
\' muse-colors-wikiword-separate
)
413 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
417 (eval-after-load "muse-publish"
419 (add-to-list 'muse-publish-markup-regexps
420 '(3100 muse-wiki-interwiki-regexp
0 link
)
422 (add-to-list 'muse-publish-markup-regexps
423 '(3200 muse-wiki-wikiword-regexp
0 link
)
425 (add-to-list 'muse-publish-markup-regexps
429 (custom-add-option 'muse-publish-desc-transforms
430 'muse-wiki-publish-pretty-interwiki
)
431 (custom-add-option 'muse-publish-desc-transforms
432 'muse-wiki-publish-pretty-title
)))
434 ;; Insinuate link handling
436 (custom-add-option 'muse-implicit-link-functions
437 'muse-wiki-handle-interwiki
)
438 (custom-add-option 'muse-implicit-link-functions
439 'muse-wiki-handle-wikiword
)
441 (custom-add-option 'muse-explicit-link-functions
442 'muse-wiki-handle-interwiki
)
444 (add-to-list 'muse-implicit-link-functions
445 'muse-wiki-handle-interwiki t
)
446 (add-to-list 'muse-implicit-link-functions
447 'muse-wiki-handle-wikiword t
)
449 (add-to-list 'muse-explicit-link-functions
450 'muse-wiki-handle-interwiki t
)
452 ;; Obsolete functions
454 (defun muse-wiki-update-custom-values ()
455 (muse-display-warning
456 (concat "Please remove `muse-wiki-update-custom-values' from"
457 " `muse-mode-hook'. Its use is now deprecated.")))
460 ;;; muse-wiki.el ends here