]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/nxhtml/related/wikipedia-mode.el
remove toolbar and menubar
[.emacs.d.git] / emacs / nxhtml / related / wikipedia-mode.el
1 ;;; wikipedia-mode.el --- Mode for editing Wikipedia articles off-line
2 ;; Copyright (C) 2003, 2004, 2006 Chong Yidong, Uwe Brauer
3
4 ;; Author: Chong Yidong <cyd at stupidchicken com>
5 ;; Maintainer: Uwe Brauer <oub at mat.ucm.es>
6 ;; Version: 0.51
7 ;; Keywords: wiki
8 ;; $Id: wikipedia-mode.el,v 1.5 2006/05/30 15:16:45 oub Exp oub $
9
10
11 ;; This file is not part of GNU Emacs.
12
13 ;;{{{ GPL2
14
15 ;; This file is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2 of
18 ;; the License, or (at your option) any later version.
19
20 ;; This file is distributed in the hope that it will be
21 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
22 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
23 ;; PURPOSE.  See the GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public
26 ;; License along with GNU Emacs; if not, write to the Free
27 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 ;; MA 02111-1307 USA
29
30 ;;}}}
31
32 ;;; Commentary:
33
34 ;; This is `wikipedia-mode', a major mode for editing articles written
35 ;; in the markup language used by Wikipedia, the free on-line
36 ;; encyclopedia (http://www.wikipedia.org). It is intended to work
37 ;; with GNU Emacs 21.x, and Xemacs 21.4.x. See below for details.
38
39 ;; wikipedia mode can be found also at:
40 ;; http://en.wikipedia.org/wiki/Wikipedia:Wikipedia-mode.el
41
42 ;;{{{ INSTALLING WIKIPEDIA-MODE
43
44 ;; Installing wikipedia-mode
45 ;; =========================
46 ;;
47 ;; Save wikipedia-mode.el in a convenient directory, preferably in
48 ;; your `load-path'. Add the following to your `user-init-file':
49 ;;
50 ;;   (autoload 'wikipedia-mode
51 ;;     "wikipedia-mode.el"
52 ;;     "Major mode for editing documents in Wikipedia markup." t)
53 ;;
54 ;; If you did not save wikipedia-mode.el in your `load-path', you must
55 ;; use the full pathname. On MS Windows, use forward slashes (/)
56 ;; rather than back slashes (\) to indicate the directory, e.g.:
57 ;;
58 ;;   (autoload 'wikipedia-mode
59 ;;     "C:/Documents and Settings/USERNAME/.emacs.d/Wikipedia-mode.el"
60 ;;     "Major mode for editing documents in Wikipedia markup." t)
61 ;;
62 ;; If you want to associate filenames ending in ".wiki" with
63 ;; wikipedia-mode, add the following to your init file:
64 ;;
65 ;;   (setq auto-mode-alist
66 ;;     (cons '("\\.wiki\\'" . wikipedia-mode) auto-mode-alist))
67
68 ;;}}}
69
70 ;;{{{ REQUIREMENTS
71
72 ;; This  is not a real requirements but I highly recommend to use
73 ;; outline-magic written by Carsten Dominik. If you don't want to use it
74 ;; you have to comment out the relevant reference to outline magic.
75 ;; It can be found at
76 ;; http://www.astro.uva.nl/~dominik/Tools/outline-magic.el
77
78
79
80
81 ;;}}}
82
83 ;;{{{ RECOMMENDATIONS INSTALLING LONGLINES-MODE
84
85 ;; Installing longlines-mode
86 ;; =========================
87 ;;
88 ;; If you are using Emacs 22 or later longlines-mode is included so
89 ;; please skip this section!
90 ;;
91 ;; Wikipedia articles don't use newline characters to break paragraphs
92 ;; into lines, so each paragraph looks like a super-long line to
93 ;; Emacs. To let Emacs handle "soft word wrapping", you need to
94 ;; download a third-party package, longlines-mode.
95 ;;
96 ;; Download longlines.el, saving into your `load-path':
97 ;;
98 ;;   http://www.emacswiki.org/elisp/longlines.el
99 ;;
100 ;; Add the following to your `user-init-file':
101 ;;
102 ;;   (autoload 'longlines-mode "longlines.el"
103 ;;     "Minor mode for editing long lines." t)
104 ;;
105 ;;
106 ;; WARNING: if you insert text from one file in wikipedia-mode to
107 ;; another file in wikipedia-mode I strongly recommend, to turn
108 ;; longlines-mode off, before the copying!
109
110 ;;}}}
111
112 ;;{{{ RECOMMENDATIONS INSTALLING PABBREV-MODE
113
114 ;; Installing pabbrev-mode
115 ;; =========================
116 ;;
117 ;; You may find pabbrev.el useful, which can be found at
118 ;; http://www.russet.org.uk/download/emacs/pabbrev.el
119
120
121 ;;}}}
122
123 ;;{{{ Xemacs or (GNU) Emacs
124
125 ;; Xemacs or (GNU) Emacs
126 ;; =====================
127 ;; Usually that is a question of taste. However almost all wikipedia
128 ;; articles nowadays use UTF8 coding, so the question which of the
129 ;; Macsen to use, boils down to which degree UTF8 support is
130 ;; implemented (no mule Xemacs is ruled out). While Xemacs has the
131 ;; better font support, the UTF8 support still is not complete and
132 ;; hence at the time being it is sad for the maintainer (a long time
133 ;; Xemacs user) to recommend NOT to use Xemacs, even not 21.5.x, which
134 ;; has a much better implemented UTF8 coding engine. That might
135 ;; however change in the foreseeable future....
136 ;; WARNING: at least for me in Debian testing/unstable Emacs does not
137 ;; ship all fonts necessary for a flawless editing of UTF8  files. For
138 ;; example you can chose Greek input, write Greek text, but then when
139 ;; you close and open the file again, the Greek symbol are not
140 ;; displayed but you see empty blocks. The reason seems that emacs
141 ;; chooses for the input fonts other fonts as for the display (don't
142 ;; ask me). However for installing the (ugly) UTF8 compatible fonts
143 ;; from ..... solved that problem.
144
145
146 ;;}}}
147
148 ;;{{{ INSTALLING EE-HELPER or MOZEX
149
150 ;; Installing the helper programs.
151 ;; =========================
152 ;; Helper Programs: MozEx and EE-HELPER. There are three possibilities
153 ;; in order to use Emacs as an external editor
154 ;;
155 ;;     (1) Firefox add-on It's All Text: Recommended. The elisp
156 ;;         library its-all-text.el makes it easier to use this.
157 ;;
158 ;;         PROS: Easy to intall, supported. (You need to add Emacs
159 ;;               client as the editor.) Can be used for editing other
160 ;;               text fields with Emacs too.
161 ;;
162 ;;     (2) EE-HELPER: This is perl script which will communicate with
163 ;;         the  wikipedia server. However that sometimes be slow.
164
165 ;;         PROS: if the editor supports UTF8, then ee-helper will
166 ;;               pass the coding flawlessly.
167 ;;
168 ;;         CONTRA: the problem with this script is that it directly
169 ;;                 communicates with the wikipedia site and does not
170 ;;                 warn you about simultaneous editing. Use it with
171 ;;                 care!!! Moreover section editing is not implemented.
172
173 ;;     (3) MozEx: this is a Java-script which allows to communicate
174 ;;         Mozilla (or Firefox) directly with Emacs.
175
176 ;;         PROS: After finishing editing you use the wikipedia
177 ;;               software to submit your changes and not the script,
178 ;;               so you are warned about possible conflicting editing.
179 ;;
180 ;;         CONTRA: the official version does not support UTF8,
181 ;;                 however there is now a new semi official version which
182 ;;                 does support UTF8.
183
184 ;; Installing It's All Text
185 ;; ========================
186 ;;
187 ;; Go to the home page and follow the instructions there:
188 ;;
189 ;;   https://addons.mozilla.org/en-US/firefox/addon/4125
190 ;;
191 ;; Then open It's All Text preferences from the Firefox Add-ons page
192 ;; and choose emacsclient as your editor (emacsclientw.exe on
193 ;; Windows).
194
195 ;; Installing ee-helper
196 ;; ====================
197 ;;
198 ;; Download   the perl script  from
199 ;;
200 ;;   http://meta.wikimedia.org/wiki/Help:External_editors
201 ;;
202 ;; and follow the instructions. configure the .ee-ini file.  chance in
203 ;; your personal wikipedia-mode-map account setting the editing
204 ;; functions: activate the `external editor' option.
205
206 ;; Installing MozEx
207 ;; ================
208 ;;
209 ;; If your web browser is Mozilla or Firefox, take a look at the MozEx
210 ;; extension, which allows you to call Emacs for editing text boxes:
211 ;;
212 ;;   http://mozex.mozdev.org/development.html
213 ;;
214 ;; See also
215 ;;
216 ;;   http://www.emacswiki.org/cgi-bin/wiki/FireFox
217 ;;
218 ;; If you mostly use MozEx to edit Wikipedia articles, it might be
219 ;; worthwhile to tell Emacs to enter wikipedia-mode whenever it is
220 ;; called by MozEx. Just add this to your `user-init-file':
221 ;;
222 ;;   (add-to-list 'auto-mode-alist '("mozex.\\.*" . wikipedia-mode))
223
224 ;;     Recall: you have to click on edit (either edit article or edit
225 ;;             section), then use mouse3 (or shift f10), then select
226 ;;             mozex, then edit textarea: Edit-->mouse3-->mozex-->Edit
227 ;;             Textarea. After editing, you have to _click_ on the
228 ;;             text in the browser otherwise Mozilla will ignore your
229 ;;             typing.
230
231 ;;}}}
232
233 ;;{{{ NEWS
234
235
236 ;; NEWS
237 ;; ==================================
238 ;;     (1) Font setting has changed.
239 ;;     (2) Some makeup formats have been added: italics, bold, strong
240 ;;         emphasise, links.
241 ;;     (3) outline-cycle from Carsten Dominiks outline-magic has been
242 ;;         added.
243 ;;     (4) "Draft", "send" and "reply" (for discussion pages)
244 ;;         abilities 'based' on ideas of John Wigleys remember.el: see
245 ;;         the functions wikipedia-draft-*
246 ;;         RATIONALE: This comes handy in 2 situations
247 ;;            1. You are editing articles which various authors (this I
248 ;;               think is the usual case), you then want not to submit
249 ;;               your edit immediately but want to copy it somewhere and
250 ;;               to continue later. You can use the following functions
251 ;;               for doing that:
252 ;;               wikipedia-draft-buffer \C-c\C-b
253 ;;               wikipedia-draft-region \C-c\C-r
254 ;;               then the buffer/region will be appended to the
255 ;;               wikipedia-draft-data-file (default is
256 ;;               "~/Wiki/discussions/draft.wiki", which you can visit via
257 ;;               wikipedia-draft-view-draft) and it will be
258 ;;               surrounded by the ^L marks in order to set a page.
259 ;;               moreover on top on that a section header == will be
260 ;;               inserted, which consists of the Word Draft, a subject
261 ;;               you are asked for and a date stamp.
262 ;;
263 ;;               Another possibility consists in using the function
264 ;;               wikipedia-draft, bound to \C-c \C-m then a new buffer
265 ;;               will opened already in wikipedia mode. You edit and then
266 ;;               either can send the content of the buffer to the
267 ;;               wikipedia-draft-data-file in the same manner as
268 ;;               described above using the function
269 ;;               wikipedia-draft-buffer (bound to \C-c\C-k)
270 ;;
271 ;;               BACK: In order to copy/send the content of temporary
272 ;;               buffer or of a page in the wikipedia-draft-data-file
273 ;;               back in to your wikipedia file, use the function
274 ;;               `wikipedia-send-to-mozex'. You
275 ;;               will be asked to which buffer to copy your text!
276 ;;
277 ;;
278 ;;            2. You want to reply  in a discussion page to a specific
279 ;;               contribution, you can use either the function
280 ;;
281 ;;               \\[wikipedia-reply-at-point-simple] bound to [(meta shift r)]
282 ;;               which inserts a newline, a hline, and the signature of
283 ;;               the author. Or can use
284 ;;               \\[wikipedia-draft-reply] bound  [(meta r)]
285 ;;               which does the same as wikipedia-reply-at-point-simple
286 ;;               but in a temporary draft buffer.
287 ;;
288 ;;               BACK: In order to copy/send the content of that buffer
289 ;;               back in to your wikipedia file, use the function
290 ;;               \\[wikipedia-send-to-mozex] bound to "\C-c\C-c". You
291 ;;               will be asked to which buffer to copy your text! If
292 ;;               you want a copy to be send to your draft file, use
293 ;;               the variable  wikipedia-draft-send-archive
294 ;;
295
296 ;;}}}
297
298 ;;{{{ NEW FUNCTIONS AND VARIABLES
299
300
301 ;; VERSION 0.4
302 ;;==================
303 ;; NEW FUNCTIONS
304 ;; ------------------
305 ;; wikipedia-insert-enumerate
306 ;; wikipedia-insert-itemize
307 ;; wikipedia-insert-strong-emphasis (renamed to wikipedia-insert-bold-italic)
308 ;; wikipedia-insert-bold
309 ;; wikipedia-insert-italics
310 ;; wikipedia-insert-header
311 ;; wikipedia-insert-link-wiki
312 ;; wikipedia-turn-on-outline-minor-mode
313 ;; wikipedia-insert-signature
314 ;; wikipedia-insert-hline
315 ;; wikipedia-unfill-paragraph-or-region
316 ;; wikipedia-start-paragraph
317 ;; wikipedia-hardlines
318 ;; wikipedia-outline-magic-keys
319 ;; wikipedia-enhance-indent
320 ;; wikipedia-yank-prefix
321 ;; wikipedia-simple-outline-promote
322 ;; wikipedia-simple-outline-demote
323 ;; wikipedia-next-long-line
324 ;; wikipedia-unfill-paragraph
325 ;; wikipedia-rename-buffer
326 ;; wikipedia-draft
327 ;; wikipedia-draft-buffer-desc
328 ;; wikipedia-draft-append-to-file
329 ;; wikipedia-draft-page
330 ;; wikipedia-draft-region (&optional beg end)
331 ;; wikipedia-draft-buffer
332 ;; wikipedia-draft-clipboard
333 ;; wikipedia-draft-mode
334 ;; wikipedia-draft-view-draft
335 ;; wikipedia-mark-section
336 ;; wikipedia-activate-region
337 ;; wikipedia-copy-page-to-register
338 ;; wikipedia-insert-page-to-register
339 ;; wikipedia-send-to-mozex (target-buffer)
340 ;; wikipedia-reply-at-point-simple
341 ;; wikipedia-draft-reply
342 ;; wikipedia-insert-quotation-with-signature
343 ;; wikipedia-insert-quotation
344
345 ;; NEW VARIABLES
346 ;;---------------------
347 ;; wikipedia-enumerate-with-terminate-paragraph
348 ;; wikipedia-draft-buffer "*Wikipedia-Draft*"
349 ;; wikipedia-draft-mode-map
350 ;; wikipedia-draft-mode-hook
351 ;; wikipedia-draft-register ?R
352 ;; wikipedia-draft-filter-functions
353 ;; wikipedia-draft-handler-functions '(wikipedia-draft-append-to-file)
354 ;; wikipedia-draft-data-file "~/Wiki/discussions/draft.wiki"
355 ;; wikipedia-draft-leader-text "== "
356 ;; wikipedia-draft-page ?S
357 ;; wikipedia-draft-send-archive
358 ;; wikipedia-reply-with-quote
359
360
361 ;; VERSION 0.5
362 ;;====================================
363 ;; NEW FUNCTIONS
364 ;; ------------------------------------
365 ;;  wikipedia-insert-audio
366 ;;  wikipedia-insert-image
367 ;;  wikipedia-insert-link-www (renamed to wikipedia-insert-link-external)
368 ;;  wikipedia-insert-user
369 ;;  wikipedia-mark-signature
370 ;;  wikipedia-outline-cycle
371 ;;  wikipedia-reply-at-signature
372 ;;  wikipedia-terminate-paragraph-and-indent
373 ;;  wikipedia-yank-prefix
374
375 ;; NEW VARIABLES (defvar, defcustom, defconst)
376 ;; ----------------------
377 ;; wikipedia-reply-with-hline
378 ;; wikipedia-user-simplify-signature
379 ;; wikipedia-english-or-german
380 ;; wikipedia-draft-reply-register ?M
381 ;; wikipedia-mode-version
382
383 ;; VERSION 0.51
384 ;;====================================
385 ;;
386 ;; - Now requires Emacs 22 or higher.
387 ;; - Cleaned the code in various ways.
388 ;; - Removed wikipedia-english-or-german
389 ;; - Removed some private stuff
390 ;; - Simplified some key bindings.
391 ;; - Changed some key bindings to those used in org-mode.
392 ;; - Added wikipedia-lang etc
393 ;; - Added support for templates
394 ;; - Adjusted header end after inserting heading/promoting/demoting
395 ;; - Removed some functions that are already supported by outline.el
396 ;; - Changed wikipedia-mode menus
397 ;; - Added support for bullets and numbering
398
399 ;;}}}
400
401 ;;{{{ TODO
402
403 ;; Todo
404 ;; ----
405
406
407 ;; * Implement TeX highlighting in <math> environment
408 ;; * Implement (La)TeX input syntax, following the ideas of CDlatex.el
409 ;; * Make outline-cycle work correctly
410 ;; * wikipedia-reply-at-point-simple should use regexp!
411
412 ;;}}}
413
414
415
416 ;;; Code:
417
418 (require 'org)
419
420 (defconst wikipedia-mode-version (concat "0." (substring "$Revision: 1.5 $" 13 14))
421   "$Id: wikipedia-mode.el,v 1.5 2006/05/30 15:16:45 oub Exp oub $
422
423 Report bugs to: Uwe Brauer oub at mat.ucm.es")
424
425 ;;{{{ LANGS
426
427 ;; (defvar wikipedia-english-or-german t
428 ;; "*Variable in order to set the english (t) or german (nil) environment.")
429
430 (require 'tutorial) ;; for lang strings
431
432 (defvar wikipedia-lang "English")
433
434 (defvar wikipedia-langs-added nil)
435 ;;(defconst xlang-strings nil)
436
437 (unless wikipedia-langs-added
438   (defun add-lang-strings (lang new-strings)
439     (let ((lang-rec (assoc lang lang-strings)))
440       (if lang-rec
441           (dolist (str new-strings)
442             (nconc (cdr lang-rec) (list str)))
443         (setq lang-rec (cons lang new-strings))
444         (add-to-list 'lang-strings lang-rec))))
445
446   (add-lang-strings "English"
447                     '(
448                       (wikip-username-prompt . "Name of user: ")
449                       (wikip-image-mark . "[[Image:")
450                       (wikip-media-mark . "[[Media:")
451                       (wikip-utc . "(UTC)")
452                       (wikip-user-mark . "[[User:")
453                       ))
454   (add-lang-strings "Deutsch"
455                     '(
456                       (wikip-username-prompt . "Name des Benutzers: ")
457                       (wikip-image-mark . "[[Bild:")
458                       (wikip-media-mark . "[[Bild:")
459                       (wikip-utc . "(CET)")
460                       (wikip-user-mark . "[[Benutzer:")
461                       ))
462   (setq wikipedia-langs-added t))
463
464 ;;}}}
465
466 ;;{{{ TAGS
467
468 (defvar wikipedia-simple-tags
469   '("b" "big" "blockquote" "br" "caption" "code" "center" "cite" "del"
470     "dfn" "dl" "em" "i" "ins" "kbd" "math" "nowiki" "ol" "pre" "samp"
471     "small" "strike" "strong" "sub" "sup" "tt" "u" "ul" "var")
472   "Tags that do not accept arguments.")
473
474 (defvar wikipedia-complex-tags
475   '("a" "div" "font" "table" "td" "th" "tr")
476   "Tags that accept arguments.")
477
478 (defvar wikipedia-url-protocols
479   '("ftp" "gopher" "http" "https" "mailto" "news")
480   "Valid protocols for URLs in Wikipedia articles.")
481
482 ;;}}}
483
484 ;;{{{ FACES
485
486 (defvar font-wikipedia-sedate-face                      'font-wikipedia-sedate-face
487   "Face to use for Wikipedia minor keywords.")
488
489 (defvar font-wikipedia-italic-face                      'font-wikipedia-italic-face
490   "Face to use for Wikipedia italics.")
491 (defvar font-wikipedia-bold-face                        'font-wikipedia-bold-face
492   "Face to use for Wikipedia bolds.")
493 (defvar font-wikipedia-math-face                        'font-wikipedia-math-face
494   "Face to use for Wikipedia math environments.")
495 (defvar font-wikipedia-string-face                  'font-wikipedia-string-face
496   "Face to use for strings.  This is set by Font Wikipedia.")
497 (defvar font-wikipedia-verbatim-face                'font-wikipedia-verbatim-face
498   "Face to use for text in verbatim macros or environments.")
499
500
501
502
503 (defface font-wikipedia-bold-face
504   (let ((font (cond ((assq :inherit custom-face-attributes) '(:inherit bold))
505                     ((assq :weight custom-face-attributes) '(:weight bold))
506                     (t '(:bold t)))))
507     `((((class grayscale) (background light))
508        (:foreground "DimGray" ,@font))
509       (((class grayscale) (background dark))
510        (:foreground "LightGray" ,@font))
511       (((class color) (background light))
512        (:foreground "DarkOliveGreen" ,@font))
513       (((class color) (background dark))
514        (:foreground "OliveDrab" ,@font))
515       (t (,@font))))
516   "Face used to highlight text to be typeset in bold."
517   :group 'font-wikipedia-highlighting-faces)
518
519 (defface font-wikipedia-italic-face
520   (let ((font (cond ((assq :inherit custom-face-attributes) '(:inherit italic))
521                     ((assq :slant custom-face-attributes) '(:slant italic))
522                     (t '(:italic t)))))
523     `((((class grayscale) (background light))
524        (:foreground "DimGray" ,@font))
525       (((class grayscale) (background dark))
526        (:foreground "LightGray" ,@font))
527       (((class color) (background light))
528        (:foreground "DarkOliveGreen" ,@font))
529       (((class color) (background dark))
530        (:foreground "OliveDrab" ,@font))
531       (t (,@font))))
532   "Face used to highlight text to be typeset in italic."
533   :group 'font-wikipedia-highlighting-faces)
534
535 (defface font-wikipedia-math-face
536   (let ((font (cond ((assq :inherit custom-face-attributes)
537                      '(:inherit underline))
538                     (t '(:underline t)))))
539     `((((class grayscale) (background light))
540        (:foreground "DimGray" ,@font))
541       (((class grayscale) (background dark))
542        (:foreground "LightGray" ,@font))
543       (((class color) (background light))
544        (:foreground "SaddleBrown"))
545       (((class color) (background dark))
546        (:foreground "burlywood"))
547       (t (,@font))))
548   "Face used to highlight math."
549   :group 'font-wikipedia-highlighting-faces)
550
551 (defface font-wikipedia-sedate-face
552   '((((class grayscale) (background light)) (:foreground "DimGray"))
553     (((class grayscale) (background dark))  (:foreground "LightGray"))
554     (((class color) (background light)) (:foreground "DimGray"))
555     (((class color) (background dark))  (:foreground "LightGray"))
556 ;;;(t (:underline t))
557     )
558   "Face used to highlight sedate stuff."
559   :group 'font-wikipedia-highlighting-faces)
560
561 (defface font-wikipedia-string-face
562   (let ((font (cond ((assq :inherit custom-face-attributes) '(:inherit italic))
563                     ((assq :slant custom-face-attributes) '(:slant italic))
564                     (t '(:italic t)))))
565     `((((type tty) (class color))
566        (:foreground "green"))
567       (((class grayscale) (background light))
568        (:foreground "DimGray" ,@font))
569       (((class grayscale) (background dark))
570        (:foreground "LightGray" ,@font))
571       (((class color) (background light))
572        (:foreground "RosyBrown"))
573       (((class color) (background dark))
574        (:foreground "LightSalmon"))
575       (t (,@font))))
576   "Face used to highlight strings."
577   :group 'font-wikipedia-highlighting-faces)
578
579 (defface font-wikipedia-warning-face
580   (let ((font (cond ((assq :inherit custom-face-attributes) '(:inherit bold))
581                     ((assq :weight custom-face-attributes) '(:weight bold))
582                     (t '(:bold t)))))
583     `((((class grayscale)(background light))
584        (:foreground "DimGray" ,@font))
585       (((class grayscale)(background dark))
586        (:foreground "LightGray" ,@font))
587       (((class color)(background light))
588        (:foreground "red" ,@font))
589       (((class color)(background dark))
590        (:foreground "red" ,@font))
591       (t (,@font))))
592   "Face for important keywords."
593   :group 'font-wikipedia-highlighting-faces)
594
595 (defface font-wikipedia-verbatim-face
596   (let ((font (if (and (assq :inherit custom-face-attributes)
597                        (if (featurep 'xemacs)
598                            (find-face 'fixed-pitch)
599                          (facep 'fixed-pitch)))
600                   '(:inherit fixed-pitch)
601                 '(:family "courier"))))
602     `((((class grayscale) (background light))
603        (:foreground "DimGray" ,@font))
604       (((class grayscale) (background dark))
605        (:foreground "LightGray" ,@font))
606       (((class color) (background light))
607        (:foreground "SaddleBrown" ,@font))
608       (((class color) (background dark))
609        (:foreground "burlywood" ,@font))
610       (t (,@font))))
611   "Face used to highlight TeX verbatim environments."
612   :group 'font-wikipedia-highlighting-faces)
613
614
615 (defvar wikipedia-font-lock-keywords
616   (list
617
618    ;; Apostrophe-style text markup
619    (cons "''''\\([^']\\|[^']'\\)*?\\(''''\\|\n\n\\)"
620          'font-lock-builtin-face)
621    (cons "'''\\([^']\\|[^']'\\)*?\\('''\\|\n\n\\)"
622                                         ;           'font-lock-builtin-face)
623          'font-wikipedia-bold-face)
624    (cons "''\\([^']\\|[^']'\\)*?\\(''\\|\n\n\\)"
625          'font-wikipedia-italic-face)
626
627    ;; Headers and dividers
628    (list "^\\(==+\\)\\(.*\\)\\(\\1\\)"
629          '(1 font-lock-builtin-face)
630                                         ;                  '(2 wikipedia-header-face)
631          '(2 font-wikipedia-sedate-face)
632          '(3 font-lock-builtin-face))
633    (cons "^-----*" 'font-lock-builtin-face)
634
635    ;; Bare URLs and ISBNs
636    (cons (concat "\\(^\\| \\)" (regexp-opt wikipedia-url-protocols t)
637                  "://[-A-Za-z0-9._\/~%+&#?!=()@]+")
638          'font-lock-variable-name-face)
639    (cons "\\(^\\| \\)ISBN [-0-9A-Z]+" 'font-lock-variable-name-face)
640
641    ;; Colon indentation, lists, definitions, and tables
642    (cons "^\\(:+\\|[*#]+\\||[}-]?\\|{|\\)" 'font-lock-builtin-face)
643
644    (list "^\\(;\\)\\([^:\n]*\\)\\(:?\\)"
645          '(1 font-lock-builtin-face)
646          '(2 font-lock-keyword-face)
647          '(3 font-lock-builtin-face))
648
649
650
651    ;; Tags and comments
652
653    (list (concat "\\(</?\\)"
654                  (regexp-opt wikipedia-simple-tags t) "\\(>\\)")
655          '(1 font-lock-builtin-face t t)
656          '(2 font-lock-function-name-face t t)
657          '(3 font-lock-builtin-face t t))
658    (list (concat "\\(</?\\)"
659                  (regexp-opt wikipedia-complex-tags t)
660                  "\\(\\(?: \\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?\\)\\(>\\)")
661          '(1 font-lock-builtin-face t t)
662          '(2 font-lock-function-name-face t t)
663          '(3 font-lock-keyword-face t t)
664          '(4 font-lock-builtin-face t t))
665    (cons (concat "<!-- \\([^->]\\|>\\|-\\([^-]\\|-[^>]\\)\\)*-->")
666          '(0 font-lock-comment-face t t))
667
668
669
670    ;; External Links
671
672    (list (concat "\\(\\[\\)\\(\\(?:"
673                  (regexp-opt wikipedia-url-protocols)
674                  "\\)://[-A-Za-z0-9._\/~%-+&#?!=()@]+\\)\\(\\(?: [^]\n]*\\)?\\)\\(\\]\\)")
675          '(1 font-lock-builtin-face t t)
676          '(2 font-lock-variable-name-face t t)
677          '(3 font-lock-keyword-face t t)
678          '(4 font-lock-builtin-face t t))
679
680
681
682
683    ;; Wiki links
684    '("\\(\\[\\[\\)\\([^]\n|]*\\)\\(|?\\)\\([^]\n]*\\)\\(\\]\\]\\)"
685      (1 font-lock-builtin-face t t)
686      (2 font-lock-variable-name-face t t)
687      (3 font-lock-builtin-face t t)
688      (4 font-lock-keyword-face t t)
689      (5 font-lock-builtin-face t t))
690
691    ;; Wiki variables
692    '("\\({{\\)\\(.+?\\)\\(}}\\)"
693      (1 font-lock-builtin-face t t)
694      (2 font-lock-variable-name-face t t)
695      (3 font-lock-builtin-face t t))
696
697    ;; Character entity references
698    (cons "&#?[a-zA-Z0-9]+;" '(0 font-lock-type-face t t))
699
700    ;; Preformatted text
701    (cons "^ .*$" '(0 font-lock-constant-face t t))
702
703    ;; Math environment (uniform highlight only, no TeX markup)
704    (list "<math>\\(\\(\n?.\\)*\\)</math>"
705          '(1 font-lock-keyword-face t t))))
706
707                                         ; )
708
709 ;;}}}
710
711 ;;{{{ Menu and header stuff
712
713 (defvar wikipedia-imenu-generic-expression
714   ;;(list '(nil "^==+ *\\(.*[^\n=]\\)==+" 1))
715   (list '(nil "^=+ *\\(.*[^\n=]\\)=+" 1))
716   "Imenu expression for `wikipedia-mode'.  See `imenu-generic-expression'.")
717
718 ;; (defun wikipedia-next-header ()
719 ;;   "Move point to the end of the next section header."
720 ;;   (interactive)
721 ;;   (let ((oldpoint (point)))
722 ;;     (end-of-line)
723 ;;     (if (re-search-forward "\\(^==+\\).*\\1" (point-max) t)
724 ;;         (beginning-of-line)
725 ;;       (goto-char oldpoint)
726 ;;       (message "No section headers after point."))))
727
728 ;; (defun wikipedia-prev-header ()
729 ;;   "Move point to the start of the previous section header."
730 ;;   (interactive)
731 ;;   (unless (re-search-backward "\\(^==+\\).*\\1" (point-min) t)
732 ;;     (message "No section headers before point.")))
733
734 ;;}}}
735
736 ;;{{{ Paragraph terminate and filling stuff (Chong)
737
738 (defun wikipedia-terminate-paragraph () ;Version:1.58
739   "New list item or paragraph.
740 In a list, start a new list item. In a paragraph, start a new
741 paragraph.
742
743 If the current paragraph is colon indented, the new paragraph
744 will be indented in the same way."
745   (interactive)
746   (let (indent-chars)
747     (save-excursion
748       (beginning-of-line)
749       (while (cond ((looking-at "^$") nil)
750                    ((looking-at "^\\(\\(?: \\|:+\\|[#*]+\\) *\\)")
751                     (setq indent-chars (match-string 1)) nil)
752                    ((eq (point) (point-min)) nil)
753                    ((progn (forward-line -1) t)))
754         t))
755     (newline) (if (not indent-chars) (newline)
756                 (insert indent-chars))))
757
758 (defun wikipedia-terminate-paragraph-and-indent ()
759   "New list item or paragraph, ignore *,#.
760 In a list, start a new list item. In a paragraph, start a new
761 paragraph but *,# will be ignored.
762
763 If the current paragraph is colon ; indented, the new paragraph
764 will be indented in the same way."
765   (interactive)
766   (let (indent-chars)
767     (save-excursion
768       (beginning-of-line)
769       (while (cond ((looking-at "^$") nil)
770                    ((looking-at "^\\(\\(?: \\|:+\\) *\\)")
771                     (setq indent-chars (match-string 1)) nil)
772                    ((eq (point) (point-min)) nil)
773                    ((progn (forward-line -1) t)))
774         t))
775     (newline) (if (not indent-chars) (newline)
776                 (insert indent-chars))))
777
778
779 (defun wikipedia-link-fill-nobreak-p ()
780   "Function for `fill-nobreak-predicate'.
781 When filling, don't break the line for preformatted (fixed-width)
782 text or inside a Wiki link."
783   (save-excursion
784     (let ((pos (point)))
785       (or (eq (char-after (line-beginning-position)) ? )
786           (if (re-search-backward "\\[\\[" (line-beginning-position) t)
787               ;; Break if the link is really really long.
788               ;; You often get this with captioned images.
789               (null (or (> (- pos (point)) fill-column)
790                         (re-search-forward "\\]\\]" pos t))))))))
791
792 (defun wikipedia-fill-article ()
793   "Fill the entire article."
794   (interactive)
795   (save-excursion
796     (fill-region (point-min) (point-max))))
797
798 (defun wikipedia-unfill-article ()
799   "Unfill article.
800 Undo filling, deleting stand-alone newlines (newlines that do not
801 end paragraphs, list entries, etc.)"
802   (interactive)
803   (save-excursion
804     (goto-char (point-min))
805     (while (re-search-forward ".\\(\n\\)\\([^# *;:|!\n]\\|----\\)" nil t)
806       (replace-match " " nil nil nil 1)))
807   (message "Stand-alone newlines deleted"))
808
809
810 (defun wikipedia-unfill-paragraph-with-newline (&optional justifyp)
811   (interactive "P")
812   (let ((before (point)))               ;Version:1.3
813     (save-excursion
814       (forward-paragraph)
815       (or (bolp) (newline 1))
816       (let ((end (point))
817             (start (progn (backward-paragraph) (point))))
818         (goto-char before)
819         (while (re-search-forward ".\\(\n\\)\\([^# *;:|!\n]\\|----\\)" nil t)
820           (replace-match " " nil nil nil 1))))))
821                                         ;  (message "Stand-alone newlines IN PARAGRAPH deleted"))
822
823 (defun wikipedia-unfill-region ()
824   "Unfill region.
825 Undo filling, deleting stand-alone newlines (newlines that do not
826 end paragraphs, list entries, etc.) see also the function
827 \\[wikipedia-unfill-paragraph-or-region] and the even simpler
828 function \\[wikipedia-unfill-paragraph-simple]."
829   (interactive)
830   (save-excursion
831     (narrow-to-region (point) (mark))
832     (goto-char (point-min))
833     (while (re-search-forward ".\\(\n\\)\\([^# *;:|!\n]\\|----\\)" nil t)
834       (replace-match " " nil nil nil 1)))
835   (message "Stand-alone newlines deleted")
836   (widen))
837
838 ;;}}}
839
840 ;;{{{ Main function wikipedia mode (using define-derived mode)
841
842 ;;{{{ KEY SETTING
843 ;; (defvar wikipedia-outline-map
844 ;;   (let ((map (make-sparse-keymap)))
845 ;;     (define-key map [(down)] 'outline-next-visible-heading)
846 ;;     (define-key map [(up)] 'outline-previous-visible-heading)
847 ;;     (define-key map "n" 'outline-next-visible-heading)
848 ;;     (define-key map "p" 'outline-previous-visible-heading)
849 ;;     (define-key map "f" 'outline-forward-same-level)
850 ;;     (define-key map "b" 'outline-backward-same-level)
851 ;;     (define-key map "u" 'outline-up-heading)
852 ;;     (define-key map "/" 'org-occur)
853 ;;     (define-key map "\C-c\C-n" 'outline-next-visible-heading)
854 ;;     (define-key map "\C-c\C-p" 'outline-previous-visible-heading)
855 ;;     (define-key map "\C-c\C-f" 'outline-forward-same-level)
856 ;;     (define-key map "\C-c\C-b" 'outline-backward-same-level)
857 ;;     (define-key map "\C-c\C-u" 'outline-up-heading)
858 ;;     map))
859
860
861 (defvar wikipedia-mode-map
862   (let ((map (make-sparse-keymap)))
863     ;;(define-key map "\M-n" 'wikipedia-next-header)
864     (define-key map "\C-c\C-n" 'wikipedia-next-long-line)
865     ;; (define-key map "\M-p" 'wikipedia-prev-header)
866     ;;  (define-key map [(meta down)] 'wikipedia-next-header)
867     ;;  (define-key map [(meta up)]   'wikipedia-prev-header)
868     (define-key map "\C-j" 'wikipedia-terminate-paragraph)
869     ;;(define-key map "RET" 'wikipedia-newline)
870     (define-key map [(shift return)] 'wikipedia-newline)
871
872       ;;(define-key mm [separator-format] '("--"))
873 ;;       (define-key mm [outline]
874 ;;         '("Toggle Outline Mode..." . outline-minor-mode))
875       ;;(define-key mm [separator-edit-structure] '("--"))
876
877
878     ;; Use some mnemonic
879     ;;(define-key map "\C-c\C-q"    'wikipedia-unfill-article)
880     (define-key map "\C-c\M-qua"    'wikipedia-unfill-article)
881     ;;(define-key map "\C-c\M-q"    'wikipedia-fill-article)
882     (define-key map "\C-c\M-qfa"    'wikipedia-fill-article)
883     ;;(define-key map "\M-u" 'wikipedia-unfill-paragraph-or-region)
884     (define-key map "\C-c\M-qur" 'wikipedia-unfill-paragraph-or-region)
885     ;;(define-key map "\C-c\C-u" 'wikipedia-unfill-paragraph-simple)
886     (define-key map "\C-c\M-qup" 'wikipedia-unfill-paragraph-simple)
887
888     (define-key map "\C-c\C-fs" 'wikipedia-insert-bold-italic)
889     (define-key map "\C-c\C-fb" 'wikipedia-insert-bold) ;Version:1.3
890     (define-key map "\C-c\C-fi" 'wikipedia-insert-italics)
891     (define-key map "\C-c\C-fn" 'wikipedia-insert-nowiki)
892
893     (define-key map "\C-c\C-ts" 'wikipedia-insert-signature)
894     (define-key map "\C-c\C-tt" 'wikipedia-insert-template)
895     (define-key map "\C-c\C-tu" 'wikipedia-insert-user)
896     ;;(define-key map "\C-c\C-fq" 'wikipedia-insert-quotation)
897     ;;(define-key map "\C-c\C-fh" 'wikipedia-insert-header)
898     (define-key map "\C-c\C-fr" 'wikipedia-insert-hline) ;Version:1.30
899     (define-key map "\C-c\C-li" 'wikipedia-insert-link-wiki)
900     (define-key map "\C-c\C-le" 'wikipedia-insert-link-external)
901
902     ;; Breaks key binding conventions:
903     ;;(define-key map [(meta f7)] 'wikipedia-draft)
904     ;;(define-key map [(meta f8)] 'wikipedia-reply-at-point-simple)
905     ;;(define-key map [(meta f9)]  'wikipedia-draft-view-draft)
906
907     (define-key map "\C-c\C-r"  'wikipedia-reply-at-point-simple)
908
909     ;; Breaks key binding conventions:
910     ;;(define-key map "\C-cr" 'wikipedia-draft-region)
911
912     (define-key map [(meta r)]  'wikipedia-draft-reply)
913     (define-key map "\C-c\C-m" 'wikipedia-draft) ;Version:1.25
914     (define-key map "\C-c\C-b" 'wikipedia-draft-region)
915     (define-key map "\C-c\C-d" 'wikipedia-draft-buffer)
916     (define-key map "\C-c\C-k" 'wikipedia-draft-buffer)
917     (define-key map "\C-c\C-p" 'wikipedia-draft-copy-page-to-register) ;Version:1.39
918     ;; (define-key map "\C-c\C-c" 'wikipedia-draft-send-to-mozex)
919     (define-key map "\C-c\C-s" 'wikipedia-draft-yank-page-to-register)
920
921     (define-key map [(control meta prior)] 'wikipedia-enhance-indent)
922     (define-key map [(control meta next)] 'wikipedia-yank-prefix)
923     (define-key map [(meta return)] 'wikipedia-insert-enumerate)
924     (define-key map [(meta control return)] 'wikipedia-insert-enumerate-nonewline)
925     ;; private setting
926     ;; This is bound to C-j by default:
927     ;;(define-key map [(shift return)] 'newline-and-indent) ;Version:1.24
928     (define-key map "\C-\\" 'wikipedia-insert-itemize) ;Version:1.28
929     (define-key map [(control return)] 'wikipedia-insert-itemize)
930
931     ;; The next three breaks Emacs key binding conventions, are they really necessary?
932     ;;(define-key map "\C-ca" 'auto-capitalize-mode)
933     ;;(define-key map "\C-ci" 'set-input-method)
934     ;;(define-key map "\C-ct" 'toggle-input-method) ;Version:1.23
935
936     (define-key map [(shift tab)] 'org-shifttab)
937     (define-key map [backtab]     'org-shifttab)
938     (define-key map [tab]         'org-cycle)
939
940     map))
941
942 (defvar wikipedia-org-menu nil)
943 ;; From org.el:
944 (easy-menu-define wikipedia-org-menu wikipedia-mode-map "Wikipedia menu"
945   '("Wikipedia"
946     ("Show/Hide"
947      ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
948      ["Cycle Global Visibility" org-shifttab]
949      ["Sparse Tree" org-occur t]
950      ["Reveal Context" org-reveal t]
951      ["Show All" show-all t]
952      "--"
953      ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
954     "--"
955     ["New Heading" outline-insert-heading t]
956     ("Navigate Headings"
957      ["Up" outline-up-heading t]
958      ["Next" outline-next-visible-heading t]
959      ["Previous" outline-previous-visible-heading t]
960      ["Next Same Level" outline-forward-same-level t]
961      ["Previous Same Level" outline-backward-same-level t]
962      "--"
963      ["Jump" org-goto t])
964     ("Edit Structure"
965      ["Move Subtree Up" outline-move-subtree-up]
966      ["Move Subtree Down" outline-move-subtree-down]
967      "--"
968      ["Copy Subtree"  org-copy-special]
969      ["Cut Subtree"  org-cut-special]
970      ["Paste Subtree"  org-paste-special]
971      "--"
972      ["Promote Heading" wikipedia-simple-outline-promote]
973      ["Promote Subtree" outline-promote]
974      ["Demote Heading" wikipedia-simple-outline-demote]
975      ["Demote Subtree"  outline-demote])
976     ("Filling"
977      ["Unfill article" wikipedia-unfill-article]
978      ["Fill article" wikipedia-fill-article])
979     "--"
980     ("Format"
981      ["Horizontal line" wikipedia-insert-hline]
982      "--"
983      ["Indent Paragraph" wikipedia-indent-paragraph]
984      ["Deindent Paragraph" wikipedia-deindent-paragraph]
985      "--"
986      ["Insert No Wiki Formatting" wikipedia-insert-nowiki]
987      ["Bold-Italic" wikipedia-insert-bold-italic]
988      ["Italic" wikipedia-insert-italics]
989      ["Bold" wikipedia-insert-bold]
990      "--"
991      ("Insert"
992       ("Templates"
993        ["Site Specific Template" wikipedia-insert-template]
994        ["Signature" wikipedia-insert-signature]
995        )
996       ("Links"
997        ["External Link" wikipedia-insert-link-external]
998        ["Internal Wiki Link" wikipedia-insert-link-wiki]
999        )
1000       ("Bullets and numbering"
1001        ["Bullet" wikipedia-insert-bullet]
1002        ["Numbering" wikipedia-insert-numbering]
1003        )
1004       ))
1005     ))
1006 ;;}}}
1007
1008
1009 ;;;###autoload
1010 ;;{{{ Main function wikipedia-mode
1011
1012 (define-derived-mode wikipedia-mode outline-mode "Wikipedia"
1013   "Major mode for editing wikimedia style wikis.
1014 Major mode for editing articles written in the markup language
1015 used by Wikipedia, the free on-line
1016 encyclopedia (see URL `http://www.wikipedia.org').
1017
1018 There are several ways to use wikipedia-mode:
1019
1020 - You can simply cut and paste articles between Emacs and your
1021   web browser's text box.
1022 - If you are using Firefox you can use the It's All Text add-on
1023   for Firefox.
1024 - You can use MozEx, a Mozilla/Firefox web browser extension that
1025   allows you to call Emacs from a text
1026   box (see URL `http://mozex.mozdev.org/').
1027 - Another way is to use the PERL script ee-helper, which allows
1028   you to up and download wiki texts.
1029
1030 Wikipedia articles are usually unfilled: newline characters are not
1031 used for breaking paragraphs into lines. Unfortunately, Emacs does not
1032 handle word wrapping yet. As a workaround, wikipedia-mode turns on
1033 longlines-mode automatically. In case something goes wrong, the
1034 following commands may come in handy:
1035
1036 \\[wikipedia-fill-article] fills the buffer.
1037 \\[wikipedia-unfill-article] unfills the buffer.
1038 Be warned that function can be dead  slow, better use wikipedia-unfill-paragraph-or-region.
1039 \\[wikipedia-unfill-paragraph-or-region] unfills the paragraph
1040 \\[wikipedia-unfill-paragraph-simple] doehe same but simpler.
1041
1042
1043
1044 The following commands put in markup structures.
1045
1046 \\[wikipedia-insert-bold-italic] bold+italic
1047 \\[wikipedia-insert-bold] bold text
1048 \\[wikipedia-insert-italics] italics
1049 \\[wikipedia-insert-nowiki] no wiki markup
1050 \\[wikipedia-insert-link-wiki] inserts a link
1051
1052 The following commands are also defined:
1053 \\[wikipedia-insert-user] inserts user name
1054 \\[wikipedia-insert-signature] inserts ~~~~
1055 \\[wikipedia-insert-enumerate] inserts enumerate type structures
1056 \\[wikipedia-insert-itemize] inserts itemize type structures
1057 \\[wikipedia-insert-hline] inserts a hline
1058
1059 The draft functionality
1060 \\[wikipedia-draft]
1061 \\[wikipedia-draft-region]
1062 \\[wikipedia-draft-view-draft]
1063 \\[wikipedia-draft-page]
1064 \\[wikipedia-draft-buffer]
1065
1066 Replying and sending functionality
1067 \\[wikipedia-reply-at-point-simple]
1068 \\[wikipedia-draft-reply]
1069
1070
1071 The register functionality
1072 \\[wikipedia-copy-page-to-register]
1073 \\[defun wikipedia-insert-page-to-register]
1074
1075
1076 Some simple editing commands.
1077 \\[wikipedia-enhance-indent]
1078 \\[wikipedia-yank-prefix]
1079 \\[wikipedia-unfill-paragraph-or-region]
1080
1081
1082
1083 \\[wikipedia-terminate-paragraph]     starts a new list item or paragraph in a context-aware manner."
1084
1085   (set (make-local-variable 'adaptive-fill-regexp) "[ ]*")
1086   (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?-- *")
1087   (set (make-local-variable 'comment-end-skip) " *--\\([ \n]*>\\)?")
1088   (set (make-local-variable 'comment-start) "<!-- ")
1089   (set (make-local-variable 'comment-end) " -->")
1090   (set (make-local-variable 'paragraph-start)
1091        "\\*\\| \\|#\\|;\\|:\\||\\|!\\|$")
1092   (set (make-local-variable 'sentence-end-double-space) nil)
1093   (set (make-local-variable 'font-lock-multiline) t)
1094   (set (make-local-variable 'font-lock-defaults)
1095        '(wikipedia-font-lock-keywords t nil nil nil))
1096   (set (make-local-variable 'fill-nobreak-predicate)
1097        'wikipedia-link-fill-nobreak-p)
1098   (set (make-local-variable 'auto-fill-inhibit-regexp) "^[ *#:|;]")
1099
1100   ;; Support for outline-minor-mode. No key conflicts, so we'll use
1101   ;; the normal outline-mode prefix.
1102   ;;(set (make-local-variable 'outline-regexp) "==+")
1103   (set (make-local-variable 'outline-regexp) "=+")
1104   ;;(set (make-local-variable 'outline-heading-end-regexp) "=+") ;; For betting fixing
1105   ;;  (set (make-local-variable 'outline-regexp) "=+")
1106   ;;  (set (make-local-variable 'outline-regexp) ":")
1107
1108   ;; Fix-me: Why change this?? Should not the user do this globally instead?
1109   ;;(set (make-local-variable 'outline-minor-mode-prefix) "\C-c\C-o")
1110
1111   ;; Fix-mde For longlines-mode??:
1112   (set (make-local-variable 'auto-fill-inhibit-regexp) "^[ *#:|;]")
1113
1114   ;: From org.el:
1115   ;; Get rid of Outline menus, they are not needed
1116   ;; Need to do this here because define-derived-mode sets up
1117   ;; the keymap so late.  Still, it is a waste to call this each time
1118   ;; we switch another buffer into org-mode.
1119   (if (featurep 'xemacs)
1120       (when (boundp 'outline-mode-menu-heading)
1121         ;; Assume this is Greg's port, it used easymenu
1122         (easy-menu-remove outline-mode-menu-heading)
1123         (easy-menu-remove outline-mode-menu-show)
1124         (easy-menu-remove outline-mode-menu-hide))
1125     (define-key wikipedia-mode-map [menu-bar headings] 'undefined)
1126     (define-key wikipedia-mode-map [menu-bar hide] 'undefined)
1127     (define-key wikipedia-mode-map [menu-bar show] 'undefined))
1128
1129   (easy-menu-add wikipedia-org-menu)
1130
1131   ;; Turn on the Imenu automatically.
1132 ;;   (when menu-bar-mode
1133 ;;     (set (make-local-variable 'imenu-generic-expression)
1134 ;;          wikipedia-imenu-generic-expression)
1135 ;;     (imenu-add-to-menubar "TOC"))
1136
1137   (modify-syntax-entry ?< "(>" wikipedia-mode-syntax-table)
1138   (modify-syntax-entry ?> ")<" wikipedia-mode-syntax-table)
1139
1140   ;;}}}
1141
1142   ;;; This is the wrong way to do it, see add-hook:
1143   ;; (make-local-variable 'change-major-mode-hook)
1144
1145   ;; Check if our version of outline.el has the new header hooks:
1146   (require 'outline)
1147   (when (boundp 'outline-demote-hook)
1148     (add-hook 'outline-demote-hook 'wikipedia-outline-insert-heading-f nil t)
1149     (add-hook 'outline-promote-hook 'wikipedia-outline-insert-heading-f nil t)
1150     (add-hook 'outline-insert-heading-hook 'wikipedia-outline-insert-heading-f nil t)
1151     )
1152   )
1153
1154 (defun wikipedia-outline-insert-heading-f ()
1155   (insert "  ")
1156   (backward-char)
1157   (wikipedia-adjust-header-end))
1158
1159 ;; wikipedia-mode ends here
1160 ;;}}}
1161
1162 ;;{{{ longlines-mode
1163
1164 (defun wikipedia-turn-on-longlines ()   ;Version:1.58
1165   "Turn on longlines-mode if it is defined."
1166   (if (functionp 'longlines-mode)
1167       (longlines-mode 1)))
1168
1169 (defcustom wikipedia-use-longlines-mode nil
1170   "Turn on longlines-mode' if non-nil.
1171 Unfortunately there are some bugs in `longlines-mode' so turning
1172 it on is an option currently."
1173   :type 'boolean
1174   :set (lambda (sym val)
1175          (set-default sym val)
1176          (if val
1177              (add-hook 'wikipedia-mode-hook 'wikipedia-turn-on-longlines)
1178            (remove-hook 'wikipedia-mode-hook 'wikipedia-turn-on-longlines)))
1179   :group 'wikipedia)
1180
1181 ;;}}}
1182
1183 ;; New formating stuff for inserting simple formating structures such
1184
1185 ;;{{{ Insert makeup and templates
1186
1187 (defvar wikipedia-enumerate-with-terminate-paragraph nil
1188   "*Before insert enumerate/itemize do \\[wikipedia-terminate-paragraph].")
1189
1190 (defun wikipedia-region-active-p ()
1191   (or (and (boundp 'zmacs-region-active-p) zmacs-region-active-p)
1192       (and (boundp 'transient-mark-mode) transient-mark-mode mark-active)))
1193
1194 (defun wikipedia-insert-around-region (before after)
1195   (if (wikipedia-region-active-p)
1196       (save-excursion
1197         (let ((beginning (region-beginning))
1198               (end       (region-end))
1199               (check     (= (string-to-char before) ?')))
1200           ;; When we are inserting ' do not mix them:
1201           (if (or (not check)
1202                   (not (memq ?'
1203                              (append (buffer-substring-no-properties
1204                                       beginning end) nil))))
1205               (progn
1206                 (goto-char end)
1207                 (insert after)
1208                 (goto-char beginning)
1209                 (insert before))
1210             (message "Sorry, the region already contains the char '.")
1211             )))
1212     (insert before)
1213     (insert after)
1214     (backward-char (length after))))
1215
1216 (defun wikipedia-insert-enumerate ()
1217   "Insert enumerated items.
1218 Format depends on `wikipedia-enumerate-with-terminate-paragraph'.
1219 Note however that `wikipedia-terminate-paragraph' does not work
1220 very well will longlines-mode."
1221   (interactive)
1222   (if wikipedia-enumerate-with-terminate-paragraph
1223       (progn
1224         (wikipedia-terminate-paragraph)
1225         (insert "#"))
1226     (newline nil)
1227     (insert ":#")))
1228
1229
1230
1231
1232
1233 (defun wikipedia-insert-itemize ()
1234   "Insert not enumerated items.
1235 Format depends on `wikipedia-enumerate-with-terminate-paragraph'.
1236 Note however that the `wikipedia-terminate-paragraph' does not
1237 work very well will longlines-mode."
1238   (interactive)
1239   (if wikipedia-enumerate-with-terminate-paragraph
1240       (progn
1241         (wikipedia-terminate-paragraph)
1242         (insert "*"))
1243     (newline nil)
1244     (insert ":*")))
1245
1246
1247 (defun wikipedia-insert-bold-italic ()
1248   "Insert strong emphasis.
1249 Uses four apostrophes (e.g. ''''FOO''''.) When mark is active, surrounds region."
1250   (interactive)
1251   (wikipedia-insert-around-region "''''" "''''"))
1252
1253
1254 (defun wikipedia-insert-bold ()
1255   "Insert bold.
1256 Uses three apostrophes (e.g. '''FOO'''.) When mark is active,
1257 surrounds region."
1258   (interactive)
1259   (wikipedia-insert-around-region "'''" "'''"))
1260
1261
1262 (defun wikipedia-insert-italics ()
1263   "Insert italics.
1264 Uses two apostrophes (e.g. ''FOO''.) When mark is active,
1265 surrounds region."
1266   (interactive)
1267   (wikipedia-insert-around-region "''" "''"))
1268
1269 (defun wikipedia-indent-paragraph ()
1270   (interactive)
1271   (backward-paragraph)
1272   )
1273 (defun wikipedia-deindent-paragraph ()
1274   (interactive)
1275   )
1276 ;;}}}
1277 ;;{{{ Templates
1278
1279 ;; http://en.wikipedia.org/wiki/Template:Quotation
1280 ;; http://en.wikipedia.org/wiki/Help:A_quick_guide_to_templates
1281 (defvar wikipedia-site nil)
1282 (make-variable-buffer-local 'wikipedia-site)
1283 (defvar wikipedia-site-history nil)
1284
1285 (defcustom wikipedia-templates nil
1286   "Templates for different wikis."
1287   :type '(repeat (list
1288                   (string :tag "Wiki site")
1289                   (repeat
1290                    (list
1291                     (string :tag "Template name")
1292                     (string :tag "Template code")))))
1293   :group 'wikipedia)
1294
1295 (defun wikipedia-insert-template ()
1296   "Prompts for a template and inserts it."
1297   (interactive)
1298   (let* ((t-name-code (wikipedia-get-template))
1299          (t-name (car t-name-code))
1300          ;; Ask how to insert:
1301          (choices '("Evaluate when page is created"
1302                     "Substitute when saving this source page"
1303                     "Show template when page is fetched"
1304                     "Insert template itself"))
1305          (hist (copy-sequence choices))
1306          (default (car choices))
1307          (choice (completing-read
1308                   "How do you want to insert the template? "
1309                   choices
1310                   nil
1311                   t
1312                   default
1313                   (cons 'hist 1))))
1314 ;;     (lwarn 't :warning "t-name=%s" t-name)
1315 ;;     (lwarn 't :warning "choice=%s" choice)
1316 ;;     (lwarn 't :warning "0=%s" (nth 0 choices))
1317 ;;     (lwarn 't :warning "1=%s" (nth 1 choices))
1318 ;;     (lwarn 't :warning "2=%s" (nth 2 choices))
1319     (cond
1320      ((string= choice (nth 0 choices))
1321       ;;(lwarn 't :warning "evaluate=>%s" (concat "{{" t-name "}}"))
1322       (insert "{{" t-name "}}")
1323       )
1324      ((string= choice (nth 1 choices))
1325       ;;(lwarn 't :warning "subst=>%s" (concat "{{subst:" t-name "}}"))
1326       (insert "{{subst:" t-name "}}")
1327       )
1328      ((string= choice (nth 2 choices))
1329       ;;(lwarn 't :warning "raw=>%s" (concat "{{msgnw:" t-name "}}"))
1330       (insert "{{msgnw:" t-name "}}")
1331       )
1332      ((string= choice (nth 3 choices))
1333       (insert (cdr t-name-code))
1334       ))))
1335
1336 (defun wikipedia-get-template ()
1337   (let* ((sites (mapcar (lambda (t-sites)
1338                          (car t-sites))
1339                         wikipedia-templates))
1340          (hist (copy-sequence sites))
1341          (default-site wikipedia-site)
1342          (histpos (if (not default-site)
1343                        1
1344                     (catch 'pos
1345                       (let ((n 0))
1346                         (dolist (elt sites)
1347                           (setq n (1+ n))
1348                           (when (string= default-site elt)
1349                             (throw 'pos n)))))))
1350          (site (if (= 1 (length sites))
1351                    (car sites)
1352                  (completing-read "Wiki site: "
1353                                   sites
1354                                   nil
1355                                   t
1356                                   default-site
1357                                   (cons 'hist histpos))))
1358          (s-t (assoc site wikipedia-templates))
1359          (templates (car (cdr s-t)))
1360          (t-names (mapcar (lambda (t-for-site)
1361                             ;;(lwarn 't :warning "t-for-site=%s" t-for-site)
1362                             (car t-for-site))
1363                           templates))
1364          (t-name (wikipedia-get-template-name site templates))
1365          (code (car (cdr (assoc t-name templates))))
1366          )
1367     (setq wikipedia-site site)
1368 ;;     (lwarn 't :warning "site=%s" site)
1369 ;;     (lwarn 't :warning "s-t=%s" s-t)
1370 ;;     (lwarn 't :warning "templates=%s" templates)
1371 ;;     (lwarn 't :warning "t-names=%s" t-names)
1372 ;;     (lwarn 't :warning "t-name=%s" t-name)
1373 ;;     (lwarn 't :warning "code=%s" code)
1374     (cons t-name code)))
1375
1376 (defun wikipedia-get-template-name (site templates)
1377   ""
1378   (let* ((prompt "Template: ")
1379          (minibuffer-local-must-match-map (copy-keymap minibuffer-local-must-match-map))
1380          (hist (mapcar (lambda (elt)
1381                          (car elt))
1382                        templates))
1383          (desc-fun (lambda ()
1384                      (let ((s (minibuffer-contents-no-properties)))
1385                        (when (< 0 (length s))
1386                          (wikipedia-describe-template s site templates)))))
1387          (up-fun (lambda () (interactive)
1388                    (previous-history-element 1)
1389                    (funcall desc-fun)))
1390          (down-fun (lambda () (interactive)
1391                      (next-history-element 1)
1392                      (funcall desc-fun)))
1393          (default nil)
1394          (histpos (if (not default) 1))
1395          (default-name (if default
1396                            default
1397                          (car (nth (1- histpos) templates))))
1398          (tpl-name nil)
1399          )
1400     (define-key minibuffer-local-must-match-map [up] up-fun)
1401     (define-key minibuffer-local-must-match-map [down] down-fun)
1402     (save-window-excursion
1403       (wikipedia-describe-template default-name site templates)
1404       (setq tpl-name (completing-read prompt
1405                                       templates
1406                                       nil ; predicate
1407                                       t   ; require-match
1408                                       default-name ;; initial-input
1409                                       (cons 'hist histpos) ;; hist
1410                                       )))
1411   (when (= 0 (length tpl-name))
1412     (error "No template name given"))
1413   (let ((tpl (assoc tpl-name templates)))
1414     (unless tpl
1415       (error "There is no template named %s for site %s" tpl-name site))
1416     ;;(lwarn 't :warning "tpl=%s" tpl)
1417     )
1418   tpl-name))
1419
1420 (defun wikipedia-describe-template (name site templates)
1421   (let ((tpl-rec (assoc name templates)))
1422     (with-output-to-temp-buffer (help-buffer)
1423       (with-current-buffer (help-buffer)
1424         (help-setup-xref (list #'wikipedia-describe-template name site templates) (interactive-p))
1425         (let ((inhibit-read-only t)
1426               start end
1427               here
1428               (tpl (assoc name templates)))
1429           ;;(insert (format "%S\n\n" family))
1430           (insert (format "%s - a wiki template for site %s" name site))
1431           (insert "\n\n")
1432           (setq start (point))
1433           (insert (cadr tpl))
1434           (setq end (point))
1435           (with-no-warnings (print-help-return-message))
1436           ;;(put-text-property start end 'face 'highlight)
1437           (goto-char start)
1438           (setq here (point))
1439           (while (re-search-forward "\\(?:<onlyinclude>\\|</onlyinclude>\\|<noinclude>.*</noinclude>\\)" end t)
1440             (put-text-property here (match-beginning 0) 'face 'highlight)
1441             (setq here (point))
1442             )
1443           (put-text-property (point) end 'face 'highlight)
1444           )))))
1445
1446 (defun wikipedia-insert-nowiki ()
1447   "Mark the region as 'nowiki'.
1448 When mark is active, surrounds region."
1449   (interactive)
1450   (wikipedia-insert-around-region "<nowiki>" "</nowiki>"))
1451
1452
1453
1454 (defun wikipedia-insert-user ()
1455   "Prompt for a user name, insert [[User:foo]]"
1456   (interactive)
1457   (let ((user (read-string (get-lang-string wikipedia-lang 'wikip-username-prompt)))
1458         (user-mark (get-lang-string wikipedia-lang 'wikip-user-mark)))
1459     (insert (concat user-mark user "|" user "]]"))))
1460
1461 (defun wikipedia-insert-signature ()    ;Version:1.4
1462   "Insert \"~~~~\".
1463 This will be shown as your user identity when showing the page."
1464   (interactive)
1465   (insert "~~~~"))
1466
1467
1468
1469 (defun wikipedia-insert-reply-prefix () ;Version:1.60
1470   "Quotation box of the form {{Quotation}}{{}}. When mark is active,
1471 surrounds region."
1472   (interactive)
1473   (beginning-of-line 1)
1474   (search-forward "[[")
1475   (backward-char 2)
1476   (mark-sexp 1)
1477   (copy-to-register wikipedia-draft-reply-register (region-beginning) (region-end) nil)
1478   (end-of-line 1)
1479   (wikipedia-terminate-paragraph)
1480   (beginning-of-line 1)
1481   (kill-line nil)
1482   (insert "----")
1483   (newline 1)
1484   (yank)
1485   (insert ":'''Re: ")
1486   (insert-register wikipedia-draft-reply-register 1)
1487   (insert "''' ")
1488   (end-of-line 1))
1489
1490 ;; (defun wikipedia-insert-header ()
1491 ;;   "Insert subheader via  == (e.g. == FOO ==.)"
1492 ;;   (interactive)
1493 ;;   (unless (bolp)
1494 ;;     (beginning-of-line))
1495 ;;   (insert "== ")
1496 ;;   (end-of-line)
1497 ;;   (insert " ==")
1498 ;;   (backward-char 3))
1499
1500 (defvar wikipedia-link-wiki-history nil)
1501
1502 (defun wikipedia-insert-link-wiki ()
1503   "Insert link via [[ (e.g. [[FOO]].) When mark is active, surround region."
1504   (interactive)
1505   (if (wikipedia-region-active-p)
1506       (wikipedia-insert-around-region "[[" "]]")
1507     (let* ((link (read-string "Wiki link: " nil wikipedia-link-wiki-history))
1508            (name (read-string "Name (optional): ")))
1509       (insert "[[" link)
1510       (when (< 0 (length name))
1511         (insert "|" name))
1512       (insert "]]"))))
1513
1514 (defun wikipedia-insert-link-external ()
1515   "Insert link via [[ (e.g. [http://FOO].) When mark is active, surround region."
1516   (interactive)
1517   (if (wikipedia-region-active-p)
1518       (wikipedia-insert-around-region "[" "]")
1519     (let* ((choices '("Plain" "Footnote" "Named"))
1520            (hist (copy-sequence choices))
1521            (style (completing-read
1522                    "Link style: " ; prompt
1523                    choices ; collection
1524                    nil ; predicate
1525                    t ; requite-match
1526                    "Plain" ; initial-input
1527                    (cons 'hist 1) ; hist
1528                    ))
1529            (url (read-string "URL: "))
1530            name)
1531       ;;(lwarn 't :warning "style=%s" style)
1532       (cond
1533        ((string= style "Plain")
1534         (insert url))
1535        ((string= style "Footnote")
1536         (insert "[" url "]"))
1537        ((string= style "Named")
1538         (let ((name (read-string "Link name: ")))
1539           (insert "[" url)
1540           (when (< 0 (length name))
1541             (insert " " name))
1542           (insert "]")))
1543        (t
1544         (error "Internal error, bad style=%s" style))))))
1545
1546
1547 (defun wikipedia-insert-image ()
1548   "Insert link image, e.g. [[Image:FOO]].
1549 When mark is active, surround region."
1550   (interactive)
1551   (let ((img-mark (get-lang-string wikipedia-lang 'wikip-image-mark)))
1552     (wikipedia-insert-around-region img-mark "]]")))
1553
1554 (defun wikipedia-insert-audio ()
1555   "Insert audio link, e.g. [[Media:FOO]].
1556 When mark is active, surround region."
1557   (interactive)
1558   (let ((aud-mark (get-lang-string wikipedia-lang 'wikip-audio-mark)))
1559     (wikipedia-insert-around-region aud-mark "]]")))
1560
1561
1562
1563
1564 ;; (defun wikipedia-turn-on-outline-minor-mode ()
1565 ;;   "Turn on outline minor mode."
1566 ;;   ;;(interactive)
1567 ;;   (outline-minor-mode nil))
1568
1569
1570
1571
1572
1573
1574 (defun wikipedia-insert-hline ()        ;Version:1.29
1575   "Insert \"----\"  "
1576   (interactive)
1577   (end-of-line)
1578   (insert hard-newline "----" hard-newline))
1579
1580 (defun wikipedia-newline (&optional arg)
1581   "Insert newline and check for bullets and numbering."
1582   (interactive "*P")
1583   (let ((here (point))
1584         (line-type nil))
1585     (beginning-of-line)
1586     (when (eq ?* (char-after))
1587       (setq line-type 'bullet))
1588     (when (eq ?# (char-after))
1589       (setq line-type 'numbered))
1590     (if (and line-type
1591              (looking-at ".\\s-*$"))
1592         (progn
1593           (delete-region (match-beginning 0) (match-end 0))
1594           (newline arg))
1595       (goto-char here)
1596       (newline arg)
1597       (cond
1598        ((eq line-type 'bullet)
1599         (insert "* "))
1600        ((eq line-type 'numbered)
1601         (insert "# "))))))
1602
1603 ;;}}}
1604
1605 ;;{{{ bullets and numbering
1606
1607 ;; Fix-me: Seems like this and my newline stuff already was there ... ;-)
1608 (defun wikipedia-insert-bullet ()
1609   "Insert a bullet."
1610   (interactive)
1611   (end-of-line)
1612   (newline)
1613   (insert "* "))
1614
1615 (defun wikipedia-insert-numbering ()
1616   "Insert numbering."
1617   (interactive)
1618   (end-of-line)
1619   (newline)
1620   (insert "# "))
1621
1622 ;;}}}
1623
1624 ;;{{{ filling and longline
1625
1626 (defun wikipedia-unfill-paragraph-or-region () ;Version:1.7
1627   "Unfill region.
1628 This function does NOT explicitly search for \"soft newlines\" as
1629 does wikipedia-unfill-region."
1630   (interactive)
1631   (when use-hard-newlines
1632     ;;  (backward-paragraph 1)
1633     ;;  (next-line 1)
1634     (beginning-of-line 1)
1635     (set-fill-prefix)
1636     ;; Fix-me: The use of make-local-variable here looks incorrect,
1637     ;; use lexical binding instead:
1638 ;;     (set (make-local-variable 'use-hard-newlines) nil)
1639 ;;     (set (make-local-variable 'sentence-end-double-space) t)
1640 ;;     (set (make-local-variable 'paragraph-start) "[ ã€€        \n\f]")
1641     (let ((use-hard-newlines nil)
1642           (sentence-end-double-space t)
1643           (paragraph-start nil))
1644       (when  (featurep 'xemacs)
1645         (let ((fill-column (point-max)))
1646           (fill-paragraph-or-region nil)))
1647       (unless  (featurep 'xemacs)
1648         (let ((fill-column (point-max)))
1649           (fill-paragraph nil)))))
1650 ;;     (set (make-local-variable 'use-hard-newlines) t)
1651 ;;     (set (make-local-variable 'sentence-end-double-space) nil)
1652 ;;     (set (make-local-variable 'paragraph-start) "\\*\\| \\|#\\|;\\|:\\||\\|!\\|$"))
1653   (unless use-hard-newlines
1654     ;;  (backward-paragraph 1)
1655     ;;  (next-line 1)
1656     (beginning-of-line 1)
1657     (set-fill-prefix)
1658 ;;     (set (make-local-variable 'sentence-end-double-space) t)
1659 ;;     (set (make-local-variable 'paragraph-start)
1660     (let ((sentence-end-double-space t)
1661           (paragraph-start nil))
1662       (when  (featurep 'xemacs)
1663         (let ((fill-column (point-max)))
1664           (fill-paragraph-or-region nil)))
1665       (unless  (featurep 'xemacs)
1666         (let ((fill-column (point-max)))
1667           (fill-paragraph nil))))
1668 ;;     (set (make-local-variable 'sentence-end-double-space) nil)
1669 ;;     (set (make-local-variable 'paragraph-start) "\\*\\| \\|#\\|;\\|:\\||\\|!\\|$")
1670     ))
1671
1672
1673
1674
1675 ;; (defun wikipedia-start-paragraph ()
1676 ;;   (interactive)
1677 ;;   (set (make-local-variable 'paragraph-start)
1678 ;;        "\\*\\| \\|#\\|;\\|:\\||\\|!\\|$"))
1679
1680 ;; Use function use-hard-newlines instead:
1681 ;; (defun wikipedia-hardlines ()
1682 ;;   "Set use-hard-newlines to NIL."
1683 ;;   (interactive)
1684 ;;   (setq use-hard-newlines nil))
1685
1686 ;; from emacs wiki
1687 (defun wikipedia-next-long-line ()
1688   "Move forward to the next long line with column-width greater
1689   than `fill-column'.
1690
1691   TODO: When function reaches end of buffer, save-excursion to
1692   starting point.
1693   Generalise to make `previous-long-line'."
1694   (interactive)
1695   ;; global-variable: fill-column
1696   (if (= (forward-line) 0)
1697       (let ((line-length
1698              (save-excursion
1699                (end-of-line)
1700                (current-column))))
1701         (if (<= line-length fill-column)
1702             (wikipedia-next-long-line)
1703           (message "Long line found")))
1704     ;; Stop, end of buffer reached.
1705     (error "Long line not found")))
1706
1707
1708 (defun wikipedia-unfill-paragraph-simple ()
1709   "A very simple function for unfilling a paragraph."
1710   (interactive)
1711   (if (functionp 'filladapt-mode)
1712       (filladapt-mode nil))
1713   (let ((fill-column (point-max)))
1714     (fill-paragraph nil)
1715     (if (functionp 'filladapt-mode)
1716         (filladapt-mode nil))))
1717
1718 ;;}}}
1719
1720 ;;{{{ outline and outline-magic stuff
1721
1722 ;;(add-hook 'wikipedia-mode-hook 'wikipedia-turn-on-outline-minor-mode)
1723 ;;(remove-hook 'wikipedia-mode-hook 'wikipedia-turn-on-outline-minor-mode)
1724
1725 (defun wikipedia-outline-cycle ()
1726   (interactive)
1727   (if (functionp 'outline-cycle)
1728       (outline-cycle)))
1729
1730 ;; Fix-me: Unfortunately outline maybe does not take care of the
1731 ;; heading endings when promoting and demoting (I have submitted a bug
1732 ;; report for this and hooks will be added). To work around this we
1733 ;; defadvice outline-demote/promote:
1734
1735 (require 'outline)
1736 (unless (boundp 'outline-demote-hook)
1737
1738   (defadvice outline-demote (after wikipedia-outline-demote-advice
1739                                    (&optional which))
1740     "Adjust heading after demote."
1741     (unless which
1742       (wikipedia-adjust-header-end)))
1743
1744   (defadvice outline-promote (after wikipedia-outline-promote-advice
1745                                     (&optional which))
1746     "Adjust heading after promote."
1747     (unless which
1748       (wikipedia-adjust-header-end)))
1749
1750   (defadvice outline-insert-heading (after wikipedia-outline-insert-heading-advice
1751                                            ())
1752     "Adjust heading after insert new heading."
1753     (wikipedia-adjust-header-end))
1754
1755   )
1756
1757 (defun wikipedia-adjust-header-end ()
1758   (when (eq major-mode 'wikipedia-mode)
1759     (let ((here (point))
1760           (end-pos (line-end-position))
1761           bgn-mark
1762           bgn-len
1763           end-mark
1764           end-len
1765           )
1766       (beginning-of-line)
1767       (when (looking-at outline-regexp)
1768         (setq bgn-mark (match-string-no-properties 0))
1769         (setq bgn-len (length bgn-mark))
1770         (end-of-line)
1771         (if (looking-back outline-regexp nil t)
1772             (when (progn
1773                     (setq end-mark (match-string-no-properties 0))
1774                     (setq end-len (length end-mark))
1775                     (/= end-len bgn-len))
1776               (replace-match bgn-mark))
1777           (insert bgn-mark)))
1778       ;;(lwarn 't :warning "bgn-len=%s, end-len=%s" bgn-len end-len)
1779       (goto-char here))))
1780
1781
1782
1783 ;;(add-hook 'outline-minor-mode-hook  'wikipedia-outline-magic-keys)
1784 (add-hook 'wikipedia-mode-hook  'wikipedia-outline-magic-keys)
1785
1786 (defun wikipedia-outline-magic-keys ()
1787   (interactive)
1788   (unless  (featurep 'xemacs)
1789     (local-set-key [(shift iso-lefttab)] 'wikipedia-outline-cycle))
1790   (local-set-key [iso-left-tab] 'wikipedia-outline-cycle)
1791   ;;(local-set-key [(meta left)]  'outline-promote)
1792   (local-set-key [(meta shift left)]  'outline-promote)
1793   ;;(local-set-key [(meta right)] 'outline-demote)
1794   (local-set-key [(meta shift right)] 'outline-demote)
1795   ;;(local-set-key [(control left)]  'wikipedia-simple-outline-promote)
1796   (local-set-key [(meta left)]  'wikipedia-simple-outline-promote)
1797   ;;(local-set-key [(control right)] 'wikipedia-simple-outline-demote)
1798   (local-set-key [(meta right)] 'wikipedia-simple-outline-demote)
1799   (local-set-key [(shift return)] 'newline-and-indent)
1800   ;;(local-set-key [(control up)] 'outline-move-subtree-up)
1801   (local-set-key [(meta shift up)] 'outline-move-subtree-up)
1802   ;;(local-set-key [(control down)] 'outline-move-subtree-down))
1803   (local-set-key [(meta shift down)] 'outline-move-subtree-down))
1804
1805 (defun wikipedia-enhance-indent ()      ;Version:1.26
1806   (interactive)
1807   (string-rectangle (region-beginning) (region-end) ":"))
1808
1809 (defun wikipedia-yank-prefix ()         ;Version:1.26
1810   (interactive)
1811   (string-rectangle (region-beginning) (region-end) ":"))
1812
1813 ;; modification for outline-magic
1814
1815 (defun wikipedia-simple-outline-promote ()
1816   "Function simple deletes \"=\" and the end and the beginning of line,
1817 does not promote the whole tree!"
1818   (interactive)
1819   (save-excursion
1820     (progn
1821       (beginning-of-line 1)
1822       (search-forward "=")
1823       (delete-char 1 nil)
1824       (end-of-line 1)
1825       (search-backward "=")
1826       (delete-char 1 nil))))
1827
1828 (defun wikipedia-simple-outline-demote ()
1829   "Function simple adds \"=\" and the end and the beginning of line,
1830 does not promote the whole tree!"
1831   (interactive)
1832   (save-excursion
1833     (progn
1834       (beginning-of-line 1)
1835       (search-forward "=")
1836       (insert "=")
1837       (end-of-line 1)
1838       (search-backward "=")
1839       (insert "="))))
1840
1841
1842 (defun wikipedia-rename-buffer ()       ;Version:1.5
1843   "Make sure that the option UNIQUE is used."
1844   (interactive)
1845   (rename-buffer (read-string "Name of new buffer (unique): " ) 1))
1846
1847 ;;}}}
1848
1849 ;;{{{ wikipedia drafts functionality: `stolen' from remember.el:
1850
1851 (defgroup wikipedia-draft nil
1852   "A mode to wikipedia-draft information."
1853   :group 'data)
1854
1855 ;;; User Variables:
1856
1857 (defcustom wikipedia-draft-mode-hook nil
1858   "*Functions run upon entering wikipedia-draft-mode."
1859   :type 'hook
1860   :group 'wikipedia-draft)
1861
1862 (defcustom wikipedia-draft-register ?R
1863   "The register in which the window configuration is stored."
1864   :type 'character
1865   :group 'wikipedia-draft)
1866
1867 (defcustom wikipedia-draft-filter-functions nil
1868   "*Functions run to filter wikipedia-draft data.
1869 All functions are run in the wikipedia-draft buffer."
1870   :type 'hook
1871   :group 'wikipedia-draft)
1872
1873 (defcustom wikipedia-draft-handler-functions '(wikipedia-draft-append-to-file)
1874   "*Functions run to process wikipedia-draft data.
1875 Each function is called with the current buffer narrowed to what the
1876 user wants wikipedia-drafted.
1877 If any function returns non-nil, the data is assumed to have been
1878 recorded somewhere by that function. "
1879   :type 'hook
1880   :group 'wikipedia-draft)
1881
1882 (defcustom wikipedia-draft-data-file "~/Wiki/discussions/draft.wiki"
1883   "*The file in which to store the wikipedia drafts."
1884   :type 'file
1885   :group 'wikipedia-draft)
1886
1887 (defcustom wikipedia-draft-reply-register ?M
1888   "The register in which the window configuration is stored."
1889   :type 'character
1890   :group 'wikipedia-draft)
1891
1892 (defcustom wikipedia-draft-page ?S      ;Version:1.37
1893   "The register in which the a page of the wiki draft file is stored."
1894   :type 'character
1895   :group 'wikipedia-draft)
1896
1897
1898 (defcustom wikipedia-draft-leader-text "== "
1899   "*The text used to begin each wikipedia-draft item."
1900   :type 'string
1901   :group 'wikipedia-draft)
1902
1903
1904 ;;; Internal Variables:
1905
1906 (defvar wikipedia-draft-buffer "*Wikipedia-Draft*"
1907   "The name of the wikipedia-draft (temporary) data entry buffer.")
1908
1909 ;;; User Functions:
1910
1911 ;;;###autoload
1912 (defun wikipedia-draft ()
1913   "Open a temporary buffer in wikipedia mode for editing an
1914  wikipedia draft, which an arbitrary piece of data. After
1915  finishing the editing either use \\[wikipedia-draft-buffer] to
1916  send the data into the wikipedia-draft-data-file, or send the
1917  buffer using `wikipedia-draft-send-to-mozex' and insert it later
1918  into a wikipedia article."
1919   (interactive)
1920   (window-configuration-to-register wikipedia-draft-register)
1921   (let ((buf (get-buffer-create wikipedia-draft-buffer)))
1922     (switch-to-buffer-other-window buf)
1923     (wikipedia-mode)
1924     (message " C-c C-k sends to draft file, C-c C-c sends to org buffer.")))
1925
1926
1927
1928
1929
1930 (defsubst wikipedia-draft-time-to-seconds (time)
1931   "Convert TIME to a floating point number."
1932   (+ (* (car time) 65536.0)
1933      (cadr time)
1934      (/ (or (car (cdr (cdr time))) 0) 1000000.0)))
1935
1936 (defsubst wikipedia-draft-mail-date (&optional rfc822-p)
1937   "Return a simple date.  Nothing fancy."
1938   (if rfc822-p
1939       (format-time-string "%a, %e %b %Y %T %z" (current-time))
1940     (format-time-string "%c" (current-time))))
1941
1942 (defun wikipedia-draft-buffer-desc ()
1943   "Using the first line of the current buffer, create a short description."
1944   (buffer-substring (point-min)
1945                     (save-excursion
1946                       (goto-char (point-min))
1947                       (end-of-line)
1948                       (if (> (- (point) (point-min)) 60)
1949                           (goto-char (+ (point-min) 60)))
1950                       (point))))
1951
1952
1953 ;; Wikipedia-Drafting to plain files:
1954
1955
1956 (defun wikipedia-draft-append-to-file ()
1957   "Add a header together with a subject to the text and add it to the
1958 draft file. It might be better if longlines-mode is off."
1959   (let ((text (buffer-string))
1960         (desc (wikipedia-draft-buffer-desc)))
1961     (with-temp-buffer
1962       (insert "\n\n")
1963       (insert wikipedia-draft-leader-text)
1964       (insert "Draft: ")                ;Version:1.39
1965       (insert (read-string "Enter Subject: "))
1966       (insert " ")
1967       (insert (current-time-string))
1968       (insert " ")
1969       (insert wikipedia-draft-leader-text)
1970       (insert "\n\n")                   ;Version:1.27
1971       (insert "\f")
1972       (insert "\n\n")
1973       (insert text)
1974       (insert "\n")
1975       (insert "\f")
1976       (insert "\n")
1977       (if (not (bolp))
1978           (insert "\n\n"))
1979       (if (find-buffer-visiting wikipedia-draft-data-file)
1980           (let ((wikipedia-draft-text (buffer-string)))
1981             (set-buffer (get-file-buffer wikipedia-draft-data-file))
1982             (save-excursion
1983               (goto-char (point-max))
1984               (insert "\n")
1985               (insert wikipedia-draft-text)
1986               (insert "\n")
1987               (save-buffer)))
1988         (append-to-file (point-min) (point-max) wikipedia-draft-data-file)))))
1989
1990
1991 (setq wikipedia-draft-handler-functions 'wikipedia-draft-append-to-file)
1992
1993
1994 (custom-add-option 'wikipedia-draft-handler-functions 'wikipedia-draft-append-to-file)
1995
1996 ;;;###autoload
1997 (defun wikipedia-draft-page ()          ;Version:1.32
1998   (interactive)
1999   (mark-page)
2000   (copy-region-as-kill (region-beginning) (region-end))
2001   (wikipedia-draft)
2002   (yank nil))
2003
2004
2005 (defun wikipedia-draft-region (&optional beg end)
2006   "Wikipedia-Draft the data from BEG to END.
2007 If called from within the wikipedia-draft buffer, BEG and END are ignored,
2008 and the entire buffer will be wikipedia-drafted.  If called from any other
2009 buffer, that region, plus any context information specific to that
2010 region, will be wikipedia-drafted."
2011   (interactive)
2012   (let ((b (or beg (min (point) (or (mark) (point-min)))))
2013         (e (or end (max (point) (or (mark) (point-max))))))
2014     (save-restriction
2015       (narrow-to-region b e)
2016       (run-hook-with-args-until-success 'wikipedia-draft-handler-functions)
2017       (when (equal wikipedia-draft-buffer (buffer-name))
2018         (kill-buffer (current-buffer))
2019         (jump-to-register wikipedia-draft-register)))))
2020
2021 ;;
2022 ;;;###autoload
2023 (defun wikipedia-draft-buffer ()
2024   "Wikipedia-draft-buffer sends the contents of the current (temporary)
2025 buffer to the wikipedia-draft-buffer, see the variable
2026 wikipedia-draft-data-file."
2027   (interactive)
2028   (wikipedia-draft-region  (point-min) (point-max)))
2029
2030 (defun wikipedia-draft-clipboard ()
2031   "Wikipedia-Draft the contents of the current clipboard.
2032 Most useful for wikipedia-drafting things from Netscape or other X Windows
2033 application."
2034   (interactive)
2035   (with-temp-buffer
2036     (insert (x-get-clipboard))
2037     (run-hook-with-args-until-success 'wikipedia-draft-handler-functions)))
2038
2039 ;;;###autoload
2040
2041
2042 ;;; Internal Functions:
2043 (defvar wikipedia-draft-send-archive t  ;Version:1.56
2044   "*Archive the reply.")
2045
2046 (defvar wikipedia-draft-mode-map
2047   (let ((m (make-sparse-keymap)))
2048     (define-key m "\C-c\C-k" 'wikipedia-draft-buffer)
2049     (define-key m "\C-c\C-d" 'wikipedia-draft-buffer)
2050     m))
2051
2052 (defun wikipedia-draft-mode ()
2053   "Major mode for output from \\[wikipedia-draft].
2054 \\<wikipedia-draft-mode-map> This buffer is used to collect data that
2055 you want wikipedia-draft.  Just hit \\[wikipedia-draft-region] when
2056 you're done entering, and it will go ahead and file the data for
2057 latter retrieval, and possible indexing.
2058 \\{wikipedia-draft-mode-map}"
2059   (interactive)
2060   (kill-all-local-variables)
2061   (indented-text-mode)
2062   (use-local-map wikipedia-draft-mode-map)
2063   (setq major-mode 'wikipedia-draft-mode
2064         mode-name "Wikipedia-Draft")
2065   (run-hooks 'wikipedia-draft-mode-hook))
2066
2067
2068 (defun wikipedia-draft-view-draft ()
2069   (interactive)
2070   "Simple shortcut to visit the file, which contains the wikipedia drafts."
2071   (find-file wikipedia-draft-data-file))
2072
2073 ;;}}}
2074
2075 ;;{{{ functions for marking regions
2076
2077 (defun wikipedia-mark-section ()        ;Version:1.36
2078   "Set mark at end of current logical section, and point at top."
2079   (interactive)
2080   (re-search-forward (concat  "== "  "[a-z,A-z \t]*"
2081                               " =="))
2082   (re-search-backward "^")
2083   (set-mark (point))
2084   (re-search-backward (concat "== "  "[a-z,A-z \t]*"
2085                               " "))
2086   (wikipedia-activate-region))
2087
2088 (defun wikipedia-mark-signature ()      ;Version:1.36
2089   "Set mark at end of current logical section, and point at top."
2090   (interactive)
2091   (re-search-forward "]]") ;;[[ ]]
2092   (re-search-backward "^")
2093   (set-mark (point))
2094   (re-search-backward "[[")
2095   (wikipedia-activate-region))
2096
2097 (when (featurep 'xemacs)
2098   (fset 'wikipedia-activate-region (symbol-function 'zmacs-activate-region)))
2099
2100 (unless (featurep 'xemacs)
2101   (defun wikipedia-activate-region ()
2102     nil))
2103
2104 ;;}}}
2105
2106 ;;{{{ `reply' and `send' functions
2107
2108 (defun wikipedia-draft-copy-page-to-register () ;Version:1.47
2109   "Copy a page via the wikipedia-draft-register."
2110   (interactive)
2111   (save-excursion
2112     (narrow-to-page nil)
2113     (copy-to-register wikipedia-draft-page (point-min) (point-max) nil)
2114     (message "draft page copied to wikipedia register wikipedia-draft-page.")
2115     (widen)))
2116
2117                                         ;aux function
2118 (defun wikipedia-draft-yank-page-to-register () ;Version:1.50
2119   "Insert a page   via the  wikipedia-draft-register."
2120   (interactive)
2121   (insert-register wikipedia-draft-page nil))
2122
2123
2124
2125 (defun wikipedia-draft-send-to-mozex (target-buffer) ;Version:1.56
2126   "Copy the current page from the wikipedia draft file  to
2127   TARGET-BUFFER, this buffer is named  something like  mozex.textarea.
2128 Check the variable wikipedia-draft-send-archive.  If it is t, then
2129 additionally the text will be archived in the draft.wiki file. Check
2130 longlines-mode, it might be better if it is set off."
2131   (interactive "bTarget buffer: ")
2132   (let ((src-buf (current-buffer)))
2133     (wikipedia-draft-copy-page-to-register)
2134     (switch-to-buffer target-buffer)
2135     (end-of-line 1)
2136     (newline 1)
2137     (wikipedia-draft-yank-page-to-register)
2138     (message "The page has been sent (copied) to the mozex file!")
2139     (switch-to-buffer "*Wikipedia-Draft*")
2140     (when wikipedia-draft-send-archive          ;Version:1.56
2141       (let ((text (buffer-string))              ;Version:1.59
2142             (desc (wikipedia-draft-buffer-desc)))
2143         (with-temp-buffer
2144           (insert "\n\n")
2145           (insert wikipedia-draft-leader-text)
2146           (insert-register wikipedia-draft-reply-register 1)
2147           (insert " ")
2148           (insert (current-time-string))
2149           (insert " ")
2150           (insert wikipedia-draft-leader-text)
2151           (insert "\n\n")
2152           (insert "\f")
2153           (insert "\n\n")
2154           (insert text)
2155           (insert "\n")
2156           (insert "\f")
2157           (insert "\n")
2158           (if (not (bolp))
2159               (insert "\n\n"))
2160           (if (find-buffer-visiting wikipedia-draft-data-file)
2161               (let ((wikipedia-draft-text (buffer-string)))
2162                 (set-buffer (get-file-buffer wikipedia-draft-data-file))
2163                 (save-excursion
2164                   (goto-char (point-max))
2165                   (insert "\n")
2166                   (insert wikipedia-draft-text)
2167                   (insert "\n")
2168                   (save-buffer)))
2169             (append-to-file (point-min) (point-max) wikipedia-draft-data-file)))))
2170     (when (equal wikipedia-draft-buffer (buffer-name))
2171       (kill-buffer (current-buffer)))
2172     (switch-to-buffer target-buffer)))
2173
2174
2175 ;;Apr_22_2006
2176 (defvar wikipedia-reply-with-hline nil
2177   "*Whether to use a hline as a header seperator in the reply.")
2178
2179 (defvar wikipedia-reply-with-quote nil  ;Version:1.60
2180   "*Whether to use a quotation tempalate or not.")
2181
2182 (defvar wikipedia-user-simplify-signature t
2183   "*Simple varible in order to threat complicated signatures of users, which uses
2184 fonts and other makeup.")
2185
2186 (defun wikipedia-reply-at-signature ()  ;Version:1.40
2187   "Very simple function to add the reply prefix to the signature,
2188 sorrounded by the boldface makeup. You have to set the point BEFORE
2189 the signature, then the functions inserts the following
2190 :'''Re: [[User:foo]]'''."
2191   (interactive)
2192   (beginning-of-line 1)
2193   (search-forward "[[")
2194   (mark-word 3)
2195   (yank)
2196   (end-of-line 1)
2197   (wikipedia-terminate-paragraph)
2198   (insert ":'''Re: ")
2199   (insert "[[")
2200   (yank)
2201   (insert "]]")
2202   (insert "'''"))
2203
2204
2205
2206
2207 (defun wikipedia-draft-reply ()         ;Version:1.62
2208   "Open a temporary buffer in wikipedia mode for editing an wikipedia
2209 draft, with an arbitrary piece of data. After finishing the editing
2210 |]]:either use \"C-c C-k\" \\[wikipedia-draft-buffer] to send the data into
2211 the wikipedia-draft-data-file, or send the buffer \"C-c\C-c\",
2212 \\[wikipedia-draft-send-to-mozex] to the current wikipedia article via
2213 mozex. Check the varibale wikipedia-draft-send-archive."
2214   (interactive)
2215   (wikipedia-reply-at-point-simple)
2216   (beginning-of-line 1)
2217   (kill-line nil)
2218   (save-excursion
2219     (window-configuration-to-register wikipedia-draft-register)
2220     (let ((buf (get-buffer-create wikipedia-draft-buffer)))
2221       (switch-to-buffer-other-window buf)
2222       (wikipedia-mode)
2223       (if (functionp 'pabbrev-mode)
2224           (pabbrev-mode))
2225       (when (not wikipedia-reply-with-quote)
2226         (when  wikipedia-reply-with-hline
2227           (insert "----")
2228           (newline 1))
2229         (yank)
2230         (end-of-line 1))
2231       (when wikipedia-reply-with-quote
2232         (insert "{{Quotation|")
2233         (yank)
2234         (insert "'''Re: ")
2235         (insert-register wikipedia-draft-reply-register 1)
2236         (insert "''' |~~~~}}")
2237         (backward-char 7))
2238       (message " C-c C-k sends to draft, C-c C-c sends to org buffer."))))
2239
2240 (defun wikipedia-reply-at-point-simple () ;Version:1.65
2241   "Reply to posts in discussion forums.
2242 You have to put the region around the signature, then the
2243 functions inserts the following
2244 :'''Re: [[User:foo]]'''."
2245   (interactive)
2246   (beginning-of-line 1)
2247   (search-forward (get-lang-string wikipedia-lang 'wikip-utc))
2248   (search-backward (get-lang-string wikipedia-lang 'wikip-user-mark))
2249   (when (not wikipedia-user-simplify-signature)
2250     (mark-word 3))
2251   (when  wikipedia-user-simplify-signature
2252     (mark-word 2))
2253   (copy-to-register wikipedia-draft-reply-register (region-beginning) (region-end) nil)
2254   (end-of-line 1)
2255   (wikipedia-terminate-paragraph-and-indent)
2256   (insert ":'''Re: ")
2257   (insert-register wikipedia-draft-reply-register 1)
2258   (when  wikipedia-user-simplify-signature
2259     (insert "|]]''' "))
2260   (when  (not wikipedia-user-simplify-signature)
2261     (insert "]]''' ")))
2262
2263 ;;}}}
2264
2265 ;;{{{ Optional private stuff:
2266
2267 ;; (defun wikipedia-insert-quotation-with-signature () ;Version:1.60
2268 ;;   "Insert quotation with signature.
2269 ;; When mark is active, surrounds region."
2270 ;;   (interactive)
2271 ;;   (wikipedia-insert-around-region "{{Quotation|" "}}{{~~~~}}"))
2272
2273 ;; (defun wikipedia-insert-quotation () ;Version:1.60
2274 ;;   "Insert quotation box.
2275 ;; When mark is active, surrounds region."
2276 ;;   (interactive)
2277 ;;   ;; Fix-me: This uses a template, is that really always available???
2278 ;;   (wikipedia-insert-around-region "{{Quotation|" "}}"))
2279
2280 ;; (define-key wikipedia-mode-map "\C-c\C-fv" 'wikipedia-insert-bible-verse-template)
2281 ;;
2282 ;; (defun wikipedia-insert-bible-verse-template ()
2283 ;;   "Insert a template for the quotation of bible verses."
2284 ;;   (interactive)
2285 ;;   (insert "({{niv|")
2286 ;; (let ((name    (read-string "Name: ")))
2287 ;;      (insert (concat name "|"))
2288 ;; (let ((verse (read-string "Verse: ")))
2289 ;;   (insert (concat verse "|" name " " verse "}})")))))
2290
2291 ;;}}}
2292
2293 (provide 'wikipedia-mode)
2294 ;;; wikipedia-mode.el ends here.
2295
2296