-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiki-inter.el
172 lines (139 loc) · 5.8 KB
/
wiki-inter.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
;;; wiki-inter.el --- interwikis for wiki.el
;; Copyright (C) 2001 Alex Schroeder <alex@gnu.org>
;; Emacs Lisp Archive Entry
;; Filename: wiki-inter.el
;; Version: 1.0.2
;; Keywords: hypermedia
;; Author: Alex Schroeder <alex@gnu.org>
;; Maintainer: Alex Schroeder <alex@gnu.org>
;; Description: Support InterWiki links for wiki.el
;; Compatibility: Emacs20, XEmacs21
;; This file is not part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2, or (at your option) any later
;; version.
;;
;; This is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
;; for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
;;; Commentary:
;; Support Interwiki link such as JargonFile:luser for wiki.el without
;; aktually hacking wiki.el.
;; This appends `wiki-inter-highlight' to `wiki-highlight-buffer-hook'
;; such that interlink are highlighted and get their own local keymap,
;; and it advises `wiki-no-name-p' such that wiki names within the
;; interlink are not recognized as such.
;; Customize `wiki-inter-links' to add more magic.
;; Note that in order to publish, `wiki-inter-link-publish' must be on
;; `wiki-pub-rules'. Loading this file puts it there, if you have
;; customized the rules, however, you will have to add
;; `wiki-inter-link-publish' manually.
;;; Code:
(require 'wiki)
(require 'thingatpt); for thing-at-point-url-path-regexp et al.
(defgroup wiki-inter nil
"InterWiki links."
:group 'wiki)
(defvar wiki-inter-local-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'wiki-inter-link-at-point)
(if (featurep 'xemacs)
(define-key map (kbd "<button2>") 'wiki-inter-link-at-mouse)
(define-key map (kbd "<mouse-2>") 'wiki-inter-link-at-mouse))
map)
"Local keymap used by wiki minor mode while on a InterWiki link.")
(defcustom wiki-inter-links
'(("Meatball" . "http://usemod.com/cgi-bin/mb.pl?%s")
("Wiki" . "http://www.c2.com/cgi/wiki?%s")
("Jargon" . "http://www.tuxedo.org/~esr/jargon/html/entry/%s.html"))
"Alist of InterWiki link names and URL templates.
Interwiki links are written as HOST:PAGE where PAGE is a wiki page on
the HOST wiki. Each element in this list has the form (HOST
. TEMPLATE). Every Interwiki link HOST:PAGE will then point to a new
URL. URL is the result of passing TEMPLATE and PAGE to `format'.
Example: In order to make Meatball:StartingPoints a valid link, use an
element (\"Meatball\" . \"http://usemod.com/cgi-bin/mb.pl?%s\"). This
will result in http://usemod.com/cgi-bin/mb.pl?StartingPoints."
:type '(repeat (cons (string :tag "Interwiki Host")
(string :tag "URL Fragment")))
:group 'wiki-inter)
(defun wiki-inter-hosts ()
"Return the host names in `wiki-inter-links'."
(mapcar (lambda (w) (car w)) wiki-inter-links))
(defun wiki-inter-regexp ()
"Return the regexp to match interlinks.
See `wiki-inter-links'."
(concat "\\(" (mapconcat 'identity (wiki-inter-hosts) "\\|") "\\)"
":\\(" thing-at-point-url-path-regexp "\\)"))
(defun wiki-inter-link-p ()
"Return non-nil if point is at an interlink."
(thing-at-point-looking-at (wiki-inter-regexp)))
(defun wiki-inter-url (interlink)
"Return URL based on INTERLINK and `wiki-inter-links'."
(save-match-data
(if (string-match (wiki-inter-regexp) interlink)
(let ((host (match-string 1 interlink))
(page (match-string 2 interlink)))
(format (cdr (assoc host wiki-inter-links)) page))
(error "Unknown interlink: %S" interlink))))
;;; Highlighting
(defun wiki-inter-highlight ()
"Highlight Interwiki links as defined by `wiki-inter-links'."
(save-excursion
(goto-char (point-min))
(let ((regexp (wiki-inter-regexp)))
(while (re-search-forward regexp nil t)
(wiki-make-extent (match-beginning 0) (match-end 0)
wiki-inter-local-map nil)))))
(add-hook 'wiki-highlight-buffer-hook 'wiki-inter-highlight t)
(defadvice wiki-no-name-p (after wiki-inter-no-name-p activate)
"Return nil if point is within an Interlink."
(setq ad-return-value
(or ad-return-value
(looking-at (wiki-inter-regexp)))))
;;; Following Links
(defun wiki-inter-link (interlink)
"Follow the INTERLINK.
Uses `wiki-inter-url' to extract the URL and `browse-url' to follow it."
(browse-url (wiki-inter-url interlink)))
(defun wiki-inter-link-at-point ()
"Find Interlink at point.
See `wiki-inter-link-p' and `wiki-inter-link'."
(interactive)
(if (wiki-inter-link-p)
(wiki-inter-link (match-string 0))
(error "Point is not at an Interlink")))
(defun wiki-inter-link-at-mouse (event)
"Find Interlink at the mouse position.
See `wiki-inter-link-at-point'."
(interactive "e")
(save-excursion
(if (and (functionp 'event-start); Emacs
(functionp 'posn-window)
(functionp 'posn-point))
(let ((posn (event-start event)))
(set-buffer (window-buffer (posn-window posn)))
(goto-char (posn-point posn)))
(let ((posn (event-point event))); XEmacs
(set-buffer (event-buffer event))
(goto-char posn)))
(wiki-inter-link-at-point)))
;;; Publishing
(defun wiki-inter-link-publish ()
"Replaces Interwiki links."
(goto-char (point-min))
(let ((regexp (wiki-inter-regexp)))
(while (re-search-forward regexp nil t)
(let* ((link (match-string 0))
(url (wiki-inter-url link)))
(replace-match (concat "<a href=\"" url "\">" link "</a>") t)))))
(add-hook 'wiki-pub-rules 'wiki-inter-link-publish t)
(provide 'wiki-inter)
;; wiki-inter.el ends here