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."
72 (defvar muse-wiki-updating-wikiword-p nil
73 "Prevent recursive calls to `muse-wiki-update-local-wikiword-regexp'.")
76 (defvar muse-wiki-wikiword-regexp
))
78 (defun muse-wiki-update-local-wikiword-regexp ()
79 "Update a local copy of `muse-wiki-wikiword-regexp' to include
80 all the files in the project."
81 ;; see if the user wants to append project files
82 (when (and muse-wiki-use-wikiword
83 muse-wiki-match-all-project-files
84 (not muse-wiki-updating-wikiword-p
))
85 (let ((muse-wiki-updating-wikiword-p t
))
86 ;; make the regexp local
87 (set (make-local-variable 'muse-wiki-wikiword-regexp
)
89 ;; append the files from the project
91 (muse-project-file-alist (muse-project))
94 (default-value 'muse-wiki-wikiword-regexp
)
96 ;; update coloring setup
97 (when (featurep 'muse-colors
)
98 (muse-configure-highlighting
99 'muse-colors-markup muse-colors-markup
)))))
101 (add-hook 'muse-update-values-hook
102 'muse-wiki-update-local-wikiword-regexp
)
103 (add-hook 'muse-project-file-alist-hook
104 'muse-wiki-update-local-wikiword-regexp
)
106 (defun muse-wiki-update-wikiword-regexp (sym val
)
107 "Update everything related to `muse-wiki-wikiword-regexp'."
109 (muse-wiki-update-local-wikiword-regexp)
110 (when (featurep 'muse-colors
)
111 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
113 (defcustom muse-wiki-wikiword-regexp
114 (concat "\\<\\(\\(?:[" muse-regexp-upper
115 "]+[" muse-regexp-lower
"]+\\)\\(?:["
116 muse-regexp-upper
"]+[" muse-regexp-lower
"]+\\)+\\)")
117 "Regexp used to match WikiWords."
120 :set
'muse-wiki-update-wikiword-regexp
)
122 (defcustom muse-wiki-ignore-bare-project-names nil
123 "Determine whether project names without a page specifer are links.
124 If non-nil, project names without a page specifier will not be
126 When nil, project names without a specifier are highlighted and
127 they link to the default page of the project that they name."
131 (defvar muse-wiki-interwiki-regexp
""
132 "Regexp that matches all interwiki links.
133 This is automatically generated by setting `muse-wiki-interwiki-alist'.
134 It can also be set by calling `muse-wiki-update-interwiki-regexp'.")
136 (defcustom muse-wiki-interwiki-delimiter
"#\\|::"
137 "Delimiter regexp used for InterWiki links.
138 If you use groups, use only shy groups."
142 (defcustom muse-wiki-interwiki-replacement
": "
143 "Regexp used for replacing `muse-wiki-interwiki-delimiter' in
144 InterWiki link descriptions.
146 If you want this replacement to happen, you must add
147 `muse-wiki-publish-pretty-interwiki' to
148 `muse-publish-desc-transforms'."
153 (defvar muse-wiki-interwiki-alist
))
155 (defun muse-wiki-project-files-with-spaces (&optional project
)
156 "Return a list of files in PROJECT that have spaces."
157 (setq project
(muse-project project
))
160 (mapcar (function (lambda (file)
161 (when (string-match " " (car file
))
162 (setq flist
(cons (car file
) flist
)))))
163 (muse-project-file-alist project
)))
166 (defun muse-wiki-update-interwiki-regexp ()
167 "Update the value of `muse-wiki-interwiki-regexp' based on
168 `muse-wiki-interwiki-alist' and `muse-project-alist'."
169 (setq muse-wiki-interwiki-regexp
170 (concat "\\<\\(" (mapconcat 'car muse-project-alist
"\\|")
171 (when muse-wiki-interwiki-alist
172 (concat "\\|" (mapconcat 'car muse-wiki-interwiki-alist
174 "\\)\\(?:\\(?:" muse-wiki-interwiki-delimiter
176 (when muse-wiki-match-all-project-files
177 ;; append the files from the project
183 (muse-wiki-project-files-with-spaces
186 muse-project-alist
"")
189 (when (featurep 'muse-colors
)
190 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
192 (defcustom muse-wiki-interwiki-alist
193 '(("EmacsWiki" .
"http://www.emacswiki.org/cgi-bin/wiki/"))
194 "A table of WikiNames that refer to external entities.
195 The format of this table is an alist, or series of cons cells.
196 Each cons cell must be of the form:
198 (WIKINAME . STRING-OR-FUNCTION)
200 The second part of the cons cell may either be a STRING, which in most
201 cases should be a URL, or a FUNCTION. If a function, it will be
202 called with one argument: the tag applied to the Interwiki name, or
203 nil if no tag was used. If the cdr was a STRING and a tag is used,
204 the tag is simply appended.
206 Here are some examples:
208 (\"JohnWiki\" . \"http://alice.dynodns.net/wiki?\")
210 Referring to [[JohnWiki::EmacsModules]] then really means:
212 http://alice.dynodns.net/wiki?EmacsModules
214 If a function is used for the replacement text, you can get creative
215 depending on what the tag is. Tags may contain any alphabetic
216 character, any number, % or _. If you need other special characters,
217 use % to specify the hex code, as in %2E. All browsers should support
219 :type
'(repeat (cons (string :tag
"WikiName")
220 (choice (string :tag
"URL") function
)))
224 (muse-wiki-update-interwiki-regexp)))
227 (add-hook 'muse-update-values-hook
228 'muse-wiki-update-interwiki-regexp
)
230 (defun muse-wiki-choose-style-by-link-suffix (given-suffix style
)
231 "If the given STYLE has a link-suffix that equals GIVEN-SUFFIX,
233 (let ((link-suffix (muse-style-element :link-suffix style
)))
234 (and (stringp link-suffix
)
235 (string= given-suffix link-suffix
))))
237 (defun muse-wiki-resolve-project-page-1 (page local-styles remote-styles
)
238 "Pick the best match for PAGE given potential local and remote styles.
239 See `muse-wiki-resolve-project-page' for more information."
240 (let ((link-suffix (muse-style-element :link-suffix
))
241 remote-style local-style prefix
)
242 (if (not (stringp link-suffix
))
244 (setq remote-style
(car remote-styles
)
245 local-style
(car local-styles
)))
246 (setq remote-style
(muse-project-choose-style
248 #'muse-wiki-choose-style-by-link-suffix
250 local-style
(muse-project-choose-style
252 #'muse-wiki-choose-style-by-link-suffix
254 (setq prefix
(muse-style-element :base-url remote-style
))
255 (muse-publish-link-file
258 (file-relative-name (expand-file-name
260 (muse-style-element :path remote-style
))
262 (muse-style-element :path local-style
))))
265 (defun muse-wiki-resolve-project-page (&optional project page
)
266 "Return the published path from the current page to PAGE of PROJECT.
267 If PAGE is not specified, use the value of :default in PROJECT.
268 If PROJECT is not specified, default to first project of
269 `muse-projects-alist'.
271 Note that PAGE can have several output directories. If this is
272 the case, we will use the first one that matches our current
273 style and has the same link suffix, ignoring the others. If no
274 style has the same link suffix as the current publishing style,
275 use the first style we find."
276 (setq project
(or (and project
277 (muse-project project
))
278 (car muse-project-alist
))
279 page
(or page
(muse-get-keyword :default
281 (let* ((page-path (muse-project-page-file page project
))
282 (remote-styles (when page-path
(muse-project-applicable-styles
283 page-path
(cddr project
))))
284 (local-styles (muse-project-applicable-styles
286 (cddr (muse-project-of-file)))))
287 (cond ((and remote-styles local-styles muse-publishing-p
)
288 (muse-wiki-resolve-project-page-1 page local-styles remote-styles
))
289 ((not muse-publishing-p
)
292 (when muse-wiki-allow-nonexistent-wikiword
293 ;; make a path to a nonexistent file in project
294 (setq page-path
(expand-file-name
295 page
(car (cadr project
))))
296 (if (and muse-file-extension
297 (not (string= muse-file-extension
"")))
298 (concat page-path
"." muse-file-extension
)
301 (defun muse-wiki-handle-interwiki (&optional string
)
302 "If STRING or point has an interwiki link, resolve it and
303 return the first match.
304 Match 1 is set to the link.
305 Match 2 is set to the description."
306 (when (if string
(string-match muse-wiki-interwiki-regexp string
)
307 (looking-at muse-wiki-interwiki-regexp
))
308 (let* ((project (match-string 1 string
))
309 (subst (cdr (assoc project muse-wiki-interwiki-alist
)))
311 (and (match-beginning 2)
312 (substring string
(match-beginning 2)))
313 (match-string 2 string
))))
315 (if (functionp subst
)
318 (and (assoc project muse-project-alist
)
319 (or word
(not muse-wiki-ignore-bare-project-names
))
320 (muse-wiki-resolve-project-page project word
))))))
322 (defun muse-wiki-handle-wikiword (&optional string
)
323 "If STRING or point has a WikiWord, return it.
324 Match 1 is set to the WikiWord."
325 (when (and muse-wiki-use-wikiword
327 (string-match muse-wiki-wikiword-regexp string
)
328 (looking-at muse-wiki-wikiword-regexp
))
330 (muse-wiki-allow-nonexistent-wikiword
332 ((and muse-wiki-ignore-implicit-links-to-current-page
333 (string= (match-string 1 string
) (muse-page-name)))
335 ((and (muse-project-of-file)
336 (muse-project-page-file
337 (match-string 1 string
) muse-current-project t
))
339 ((file-exists-p (match-string 1 string
))
342 (match-string 1 string
)))
346 (defcustom muse-wiki-publish-small-title-words
347 '("the" "and" "at" "on" "of" "for" "in" "an" "a")
348 "Strings that should be downcased in a page title.
349 This is used by `muse-wiki-publish-pretty-title', which must be
351 :type
'(repeat string
)
354 (defcustom muse-wiki-hide-nop-tag t
355 "If non-nil, hide <nop> tags when coloring a Muse buffer."
359 (defun muse-wiki-publish-pretty-title (&optional title explicit
)
360 "Return a pretty version of the given TITLE.
361 If EXPLICIT is non-nil, TITLE will be returned unmodified."
362 (unless title
(setq title
(or (muse-publishing-directive "title") "")))
364 (save-match-data (string-match muse-url-regexp title
)))
367 (let ((case-fold-search nil
))
368 (while (string-match (concat "\\([" muse-regexp-lower
369 "]\\)\\([" muse-regexp-upper
372 (setq title
(replace-match "\\1 \\2" t nil title
)))
373 (let* ((words (split-string title
))
376 (if (member (downcase (car w
))
377 muse-wiki-publish-small-title-words
)
378 (setcar w
(downcase (car w
))))
380 (mapconcat 'identity words
" "))))))
382 (defun muse-wiki-publish-pretty-interwiki (desc &optional explicit
)
383 "Replace instances of `muse-wiki-interwiki-delimiter' with
384 `muse-wiki-interwiki-replacement'."
386 (save-match-data (string-match muse-url-regexp desc
)))
388 (muse-replace-regexp-in-string muse-wiki-interwiki-delimiter
389 muse-wiki-interwiki-replacement
394 (eval-after-load "muse-colors"
396 (defun muse-wiki-colors-nop-tag (beg end
)
397 (when muse-wiki-hide-nop-tag
398 (add-text-properties beg
(+ beg
5)
399 '(invisible muse intangible t
))))
400 (defun muse-colors-wikiword-separate ()
401 (add-text-properties (match-beginning 0) (match-end 0)
402 '(invisible muse intangible t
)))
404 (add-to-list 'muse-colors-tags
405 '("nop" nil nil muse-wiki-colors-nop-tag
)
408 (add-to-list 'muse-colors-markup
409 '(muse-wiki-interwiki-regexp t muse-colors-implicit-link
)
411 (add-to-list 'muse-colors-markup
412 '(muse-wiki-wikiword-regexp t muse-colors-implicit-link
)
414 (add-to-list 'muse-colors-markup
415 '("''''" ?
\' muse-colors-wikiword-separate
)
418 (muse-configure-highlighting 'muse-colors-markup muse-colors-markup
)))
422 (eval-after-load "muse-publish"
424 (add-to-list 'muse-publish-markup-regexps
425 '(3100 muse-wiki-interwiki-regexp
0 link
)
427 (add-to-list 'muse-publish-markup-regexps
428 '(3200 muse-wiki-wikiword-regexp
0 link
)
430 (add-to-list 'muse-publish-markup-regexps
434 (custom-add-option 'muse-publish-desc-transforms
435 'muse-wiki-publish-pretty-interwiki
)
436 (custom-add-option 'muse-publish-desc-transforms
437 'muse-wiki-publish-pretty-title
)))
439 ;; Insinuate link handling
441 (custom-add-option 'muse-implicit-link-functions
442 'muse-wiki-handle-interwiki
)
443 (custom-add-option 'muse-implicit-link-functions
444 'muse-wiki-handle-wikiword
)
446 (custom-add-option 'muse-explicit-link-functions
447 'muse-wiki-handle-interwiki
)
449 (add-to-list 'muse-implicit-link-functions
450 'muse-wiki-handle-interwiki t
)
451 (add-to-list 'muse-implicit-link-functions
452 'muse-wiki-handle-wikiword t
)
454 (add-to-list 'muse-explicit-link-functions
455 'muse-wiki-handle-interwiki t
)
457 ;; Obsolete functions
459 (defun muse-wiki-update-custom-values ()
460 (muse-display-warning
461 (concat "Please remove `muse-wiki-update-custom-values' from"
462 " `muse-mode-hook'. Its use is now deprecated.")))
465 ;;; muse-wiki.el ends here