-
Notifications
You must be signed in to change notification settings - Fork 1
/
erin.el
1055 lines (940 loc) · 39.1 KB
/
erin.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; erin.el --- Emacs editing mode for TWiki pages
;; Copyright (C) 2007-2009 Neil Van Dyke. 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 3, 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 Emacs; see the file `COPYING'. If not,
;; write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;; Boston, MA 02111-1307, USA.
;; Author: Neil Van Dyke <neil AT neilvandyke DOT org>
;; Web: http://www.neilvandyke.org/erin-twiki-emacs/
;; CVS: $Id: erin.el,v 1.116 2009/08/19 08:27:28 neilpair Exp $
;; Keywords: html hypermedia hypertext text twiki wiki major-mode
;;; COMMENTARY:
;; `erin.el' is an Emacs editing mode for the Wiki markup of TWiki. The
;; current version of `erin.el' mostly just does semi-WYSIWYG fontifying
;; and heading renumbering.
;;
;; To install, put file `erin.el' into one of the directories that's listed in
;; your Emacs `load-path' variable, (optionally) byte-compile `erin.el', and
;; add the following expression to your `.emacs' file:
;;
;; (require 'erin)
;;
;; Files named with the extension `.twiki' will be opened in `erin-mode'
;; automatically. You can see a sampler of fontified TWiki markup with the key
;; sequence: M-x erin-sampler RET
;;
;; Headings can be renumbered with the `erin-renumber-headings' command (key
;; sequence C-c C-r). The number of pluses in the TWiki markup denotes the
;; level of the heading. Headings with TWiki `!!' markup after the pluses are
;; unnumbered. For an example of unnumbered and numbered headings:
;;
;; ---+!! My Important Wiki Paper
;; ---+ 1 Introduction
;; ---+ 2 Theory
;; ---++ 2.1 Pretty Pictures View of Theory
;; ---++ 2.2 Cryptic Equations View of Theory
;; ---+++ 2.2.1 Integral Symbol
;; ---+ 3 Practice
;;
;; Just as TWiki is not named after Twiki, `erin.el' is not named after Twiki's
;; arguably more attractive co-star, Erin Gray.
;;; HISTORY:
;; [Unofficial version, 2014-10-01] URL support added
;; by Thomas Fitzsimmons <[email protected]>
;;
;; Customize the following variables:
;;
;; `erin-url-format': the format of a TWiki URL with no trailing slash
;; and %s in place of the operation; e.g. "http://twiki.org/cgi-bin/%s/TWiki"
;; `erin-username': the TWiki username
;; `erin-local-directory': the directory in which temporary TWiki
;; files will be stored; defaults to "~/.emacs.d/erin".
;;
;; [Version 0.6, 2009-08-19] Removed the erroneous setting of
;; `fill-paragraph-function'. Thanks to Michael Shields for reporting.
;;
;; [Version 0.5, 2009-08-13] Note that I am no longer working on this package,
;; since I am no longer using Twiki. Changed license to GPL3. Made the
;; variable reference regexp not search past newline. Corrected docstring for
;; `erin-mode'. Comment changes. Included some commented-out partial
;; implementation of paragraph filling and infrastructure for paragraph info.
;;
;; [Version 0.4, 2007-02-28] New command `erin-renumber-headings'. List markup
;; is now highlighted to arbitrary depths, rather than only 6 levels. Fixed
;; bug in lower-case ordered-lists, introduced in 0.3.
;;
;; [Version 0.3, 2007-02-20] Default face colors are now reasonable on
;; dark-background (e.g., reverse-video) Emacs. In a double-square-brackets
;; link with no alternate text and a WikiWord qualified with a Wiki name, mark
;; the Wiki name as part of the link text (underlined, by default). In ordered
;; lists, now only supports "a", "A", "i", and "I", rather than all letters.
;; Comment changes.
;;
;; [Version 0.2, 2007-02-13] Fixed bug in trying to match newline before a
;; huggable. Made huggers more liberal about contents (e.g., "*bold:*" now
;; works). Made huggers permit some punctuation characters immediately
;; following.
;;
;; [Version 0.1, 2007-02-13] First release. It's already very useful, even in
;; its primitive state, and I have an immediate need for a released version.
;;; CODE:
(require 'font-lock)
;; URL requirement.
(require 'mm-url)
(defgroup erin nil
"*erin.el editing mode for TWiki pages"
:group 'hypermedia
:group 'wp)
;; URL defcustoms.
(defcustom erin-local-directory (expand-file-name "~/.emacs.d/erin")
"Directory for temporary TWiki files.
`erin-edit-topic' downloads raw TWiki text and saves it as a
temporary file named <topic_name>.<unique_temp_string>.twiki in
`erin-local-directory'."
:type 'string
:group 'erin)
(defcustom erin-username ""
"TWiki username."
:type 'string
:group 'erin)
(defcustom erin-url-format ""
"Format of the TWiki URLs.
You can look this up from a web browser. The %s will be replaced
with the TWiki operation being performed."
:type 'string
:group 'erin)
(defcustom erin-mode-hook nil
"*Hooks run when `erin-mode' is turned on."
:type 'hook
:group 'erin)
(defconst erin-address-background "#ddddff")
(defconst erin-fixed-background "#ffdd00")
(defconst erin-variable-background "#40ff40")
(defconst erin-prominent-markup-foreground-light "red4")
(defconst erin-prominent-markup-foreground-dark "red1")
(defface erin-markup-face
'((t (:bold t :foreground "gray50")))
"Face used for TWiki markup."
:group 'erin)
(defface erin-bullet-face
`((((class color) (background dark))
(:bold t :foreground ,erin-prominent-markup-foreground-dark))
(t
(:bold t :foreground ,erin-prominent-markup-foreground-light)))
"Face used for TWiki bullets."
:group 'erin)
(defface erin-deemph-markup-face
;; TODO: Make a dark-background variant of this.
'((t (:bold nil :underline nil :slant normal :foreground "gray75")))
"Face used for de-emphasized TWiki markup."
:group 'erin)
(defface erin-prominent-markup-face
`((((class color) (background dark))
(:bold t :foreground ,erin-prominent-markup-foreground-dark))
(t
(:bold t :foreground ,erin-prominent-markup-foreground-light)))
"Face used for prominent TWiki markup."
:group 'erin)
(defface erin-separator-face
;; TODO: inherit from erin-prominent-markup-face
`((((class color) (background dark))
(:bold t :foreground ,erin-prominent-markup-foreground-dark))
(t
(:bold t :foreground ,erin-prominent-markup-foreground-light)))
"Face used for TWiki separator markup."
:group 'erin)
(defface erin-bold-face
'((t (:bold t)))
"Face used for TWiki boldface."
:group 'erin)
(defface erin-italic-face
'((t (:slant italic)))
"Face used for TWiki italic."
:group 'erin)
(defface erin-bold-italic-face
'((t (:bold t :slant italic)))
"Face used for TWiki bold-italic."
:group 'erin)
(defface erin-fixed-face
`((t (:foreground "black" :background ,erin-fixed-background)))
"Face used for TWiki fixed."
:group 'erin)
(defface erin-bold-fixed-face
`((t (:bold t :foreground "black" :background ,erin-fixed-background)))
"Face used for TWiki bold-fixed."
:group 'erin)
(defface erin-heading-1-face
'((t (:bold t :family "Helvetica" :height 2.48832)))
"Face used for TWiki level 1 headings."
:group 'erin)
(defface erin-heading-2-face
'((t (:bold t :family "Helvetica" :height 2.0736)))
"Face used for TWiki level 2 headings."
:group 'erin)
(defface erin-heading-3-face
'((t (:bold t :family "Helvetica" :height 1.728)))
"Face used for TWiki level 3 headings."
:group 'erin)
(defface erin-heading-4-face
'((t (:bold t :family "Helvetica" :height 1.44)))
"Face used for TWiki level 4 headings."
:group 'erin)
(defface erin-heading-5-face
'((t (:bold t :family "Helvetica" :height 1.2)))
"Face used for TWiki level 5 headings."
:group 'erin)
(defface erin-heading-6-face
'((t (:bold t :family "Helvetica" :height 1.0)))
"Face used for TWiki level 6 headings."
:group 'erin)
(defface erin-variable-face
`((t (:bold t :foreground "black"
:background ,erin-variable-background)))
"Face used for TWiki variable references."
:group 'erin)
(defface erin-variable-parameters-face
`((t (:bold nil :foreground "black"
:background ,erin-variable-background)))
"Face used for parameters of TWiki variable references."
:group 'erin)
(defface erin-address-link-face
`((t (:underline t :foreground "blue"
:background ,erin-address-background)))
"Face used for TWiki address (WikiWord, URL, etc.) links."
:group 'erin)
(defface erin-address-nonlink-face
`((t (:bold nil :slant normal :underline nil :foreground "black"
:background ,erin-address-background)))
"Face used for TWiki address (WikiWord, URL, etc.) non-links."
:group 'erin)
(defface erin-nonaddress-link-face
'((t (:underline t :foreground "blue")))
"Face used for TWiki non-address (WikiWord, URL, etc.) links."
:group 'erin)
(defface erin-normal-face
'((t ()))
"Do not modify this."
;; :group 'erin
)
(defvar erin-mode-syntax-table
(let ((x (make-syntax-table)))
;; Punctuation Characters (Note: This is necessary for bold markup regexps
;; and such to detect non-word-boundaries.)
(modify-syntax-entry ?! "." x)
(modify-syntax-entry ?* "." x)
(modify-syntax-entry ?, "." x)
(modify-syntax-entry ?. "." x)
(modify-syntax-entry ?: "." x)
(modify-syntax-entry ?: "." x)
(modify-syntax-entry ?= "." x)
(modify-syntax-entry ?? "." x)
(modify-syntax-entry ?\( "." x)
(modify-syntax-entry ?\) "." x)
(modify-syntax-entry ?\; "." x)
(modify-syntax-entry ?\[ "." x)
(modify-syntax-entry ?\] "." x)
(modify-syntax-entry ?_ "." x)
x))
(defvar erin-mode-map
(let ((x (make-sparse-keymap)))
;; Note: It's the year 2007, Buck Rogers has already left the planet, and
;; we can bind a flow-control character to something.
(define-key x "\C-c\C-r" 'erin-renumber-headings)
(define-key x "\C-c\C-s" 'erin-sampler)
;; URL keybindings.
(define-key x "\C-c\C-c" 'erin-save-topic)
(define-key x "\C-c\C-k" 'erin-kill-buffer-and-delete-file)
(define-key x "\C-c\C-q" 'erin-cancel-edit)
x))
(defvar erin-font-lock-keywords
;; Let's see how much we can do with font-lock regexps before we decide to
;; rewrite the whole thing to do a proper parse.
(let* ((wikiword "[A-Z][a-z]+[A-Z][A-Za-z0-9]+")
(possibly-qualified-wikiword
(concat "\\(\\(?:[A-Z][A-Za-z]*\\.\\)?\\)\\(" wikiword "\\)"))
;; Note: possibly-qualified-wikiword introduces 2 items of match
;; data. The first might be zero-length, but will always be non-nil.
(no-leading-nonwhitespace "\\(?:[ \t\n]\\|\\`\\|^\\)")
;; TODO: We probably need to make this not consume space, otherwise
;; "*bold* *bold*" doesn't work. "*bold* _italic_" does work,
;; however, perhaps because it's different rules.
(no-trailing-nonwhitespaceother "\\(?:[][:,.!?() \t\n]\\|\\'\\|$\\)")
(make-huggable (lambda (exclude)
(concat "\\("
"[^" exclude " \t\r\n]"
"\\(?:"
"[^" exclude "\r\n]"
"*"
"[^" exclude " \t\r\n]"
"\\)?"
"\\)"
;;"\\(?:\\>\\)"
))))
`(
;; "%BR%"
("%BR%" 0 'erin-prominent-markup-face)
;; Variable references. (TODO: We should properly parse the quoted
;; strings in the attributes.)
("\\(%\\)\\([A-Za-z]+\\)\\(\\(?:{[^}\r\n]*}\\)?\\)\\(%\\)"
(1 'erin-deemph-markup-face)
(2 'erin-variable-face)
(3 'erin-variable-parameters-face)
(4 'erin-deemph-markup-face))
;; TODO: Is "!!" an escape? If so, we should put it here, but we need
;; to make sure that the "!!" in headings still works.
;; Escaped WikiWord.
(,(concat "\\(!\\)\\(" possibly-qualified-wikiword "\\)\\b")
(1 'erin-deemph-markup-face)
(2 'erin-normal-face))
;; Escaped double open-brackets. (TODO: The double-bracket matching is
;; still matching on this later; it's just not overriding the face on the
;; open-brackets, but it *is* setting faces on everything else.)
("\\(!\\)\\(\\[\\[\\)"
(1 'erin-deemph-markup-face)
(2 'erin-normal-face))
;; Anchor.
(,(concat "^\\(#\\)" possibly-qualified-wikiword)
(1 'erin-prominent-markup-face)
(2 'erin-address-nonlink-face)
(3 'erin-address-nonlink-face))
;; Headings. (Note: This is inefficient, but there's no good regexp way
;; to say "use this face if we have one plus, and this other face if we
;; have two pluses. Done in reverse order, to consume all leading
;; pluses, to permit a heading that starts with a plus (after "!!" and/or
;; whitespace).)
,@(reverse
(let ((regexp-rest '("\\(?:!!\\)?\\)\\(?:[ \t]*\\)\\([^\n]*\\)")))
(mapcar (function
(lambda (face)
(setq regexp-rest (cons "\\+" regexp-rest))
`(,(apply 'concat "^\\(-\\{3,\\}" regexp-rest)
(1 'erin-deemph-markup-face)
(2 (quote ,face)))))
'(erin-heading-1-face
erin-heading-2-face
erin-heading-3-face
erin-heading-4-face
erin-heading-5-face
erin-heading-6-face))))
;; Separator.
("^\\(---+\\)[ \t\r]*$" 1 'erin-separator-face)
;; Unordered list item. (TODO: Make this a bullet character.)
("^\\(?: \\)\\{1,\\}\\(\\*\\)[ \t]" 1 'erin-bullet-face)
;; Ordered list item.
(,(concat
"^\\(?: \\)\\{1,\\}"
"\\(\\(?:[1-9]+\\|[AaIi]\\)\\.\\)"
;;"\\(1\\.\\)"
"[ \t]")
1 'erin-prominent-markup-face)
;; Description list item.
("^\\(?: \\)\\{1,\\}\\(\\$\\)[ \t]+[^:\r\n]*\\(:\\)[ \t]"
(1 'erin-prominent-markup-face)
(2 'erin-prominent-markup-face))
;; Square-brackets link with WikiWord.
(,(concat "\\(\\[\\[\\)"
possibly-qualified-wikiword
"\\(\\]\\]\\)")
(1 'erin-deemph-markup-face)
(2 'erin-address-link-face)
(3 'erin-address-link-face)
(4 'erin-deemph-markup-face))
;; Square-brackets link with non-WikiWord (fontify entire thing as
;; WikiWord link).
(,(concat "\\(\\[\\[\\)"
"\\([^][\r\n]*\\)"
"\\(\\]\\]\\)")
(1 'erin-deemph-markup-face)
(2 'erin-address-link-face)
(3 'erin-deemph-markup-face))
;; Square-brackets-alternate link.
(,(concat "\\(\\[\\[\\)" ; =1
"\\([^]\r\n]*\\)" ; =2
"\\(\\]\\[\\)" ; =3
"\\(" ; <4
"\\(?:" ; <a
"[^][\r\n]+"
"\\|" ; |a
"\\][^]\r\n]"
"\\|" ; |a
"\\["
"\\)" ; >a
"+"
"\\)" ; >4
"\\(\\]\\]\\)" ; =5
)
(1 'erin-deemph-markup-face)
(2 'erin-address-nonlink-face)
(3 'erin-deemph-markup-face)
(4 'erin-nonaddress-link-face)
(5 'erin-deemph-markup-face))
;; Plain WikiWord link.
(,(concat "\\b" possibly-qualified-wikiword "\\b")
(1 'erin-address-nonlink-face)
(2 'erin-address-link-face))
;; Bold.
(,(concat no-leading-nonwhitespace
"\\(\\*\\)"
(funcall make-huggable "*")
"\\(\\*\\)"
no-trailing-nonwhitespaceother)
(1 'erin-deemph-markup-face)
(2 'erin-bold-face)
(3 'erin-deemph-markup-face))
;; Bold-Italic.
(,(concat no-leading-nonwhitespace
"\\(__\\)"
(funcall make-huggable "_")
"\\(__\\)"
no-trailing-nonwhitespaceother)
(1 'erin-deemph-markup-face)
(2 'erin-bold-italic-face)
(3 'erin-deemph-markup-face))
;; Italic.
(,(concat no-leading-nonwhitespace
"\\(_\\)"
(funcall make-huggable "_")
"\\(_\\)"
no-trailing-nonwhitespaceother)
(1 'erin-deemph-markup-face)
(2 'erin-italic-face)
(3 'erin-deemph-markup-face))
;; Bold-Fixed.
(,(concat no-leading-nonwhitespace
"\\(==\\)"
(funcall make-huggable "=")
"\\(==\\)"
no-trailing-nonwhitespaceother)
(1 'erin-deemph-markup-face)
(2 'erin-bold-fixed-face)
(3 'erin-deemph-markup-face))
;; Fixed.
(,(concat no-leading-nonwhitespace
"\\(=\\)"
(funcall make-huggable "=")
"\\(=\\)"
no-trailing-nonwhitespaceother)
(1 'erin-deemph-markup-face)
(2 'erin-fixed-face)
(3 'erin-deemph-markup-face))
;; TODO: Fontify tag contents correctly. The tags like "<b>" will use
;; the erin-deemph-markup-face, and ones like "<noautolink>" will use the
;; erin-prominent-markup-face.
;; HTML/XML-like tag. (Note: Presently, we don't support attributes.)
("</?[a-zA-Z][a-zA-Z0-9]*[ \t]*/?>" 0 'erin-prominent-markup-face)
)))
(defun erin-indent-line-function (&rest args)
;; TODO:
(apply 'indent-relative-maybe args))
(defmacro erin-setq-local (sym val)
(or (symbolp sym) (signal 'wrong-type-argument `(symbolp ,sym)))
`(progn (make-local-variable (quote ,sym))
(setq ,sym ,val)))
(defun erin-mode ()
"Major mode for editing TWiki pages.
\\{erin-mode-map}"
(interactive)
(kill-all-local-variables)
(setq major-mode 'erin-mode)
(setq mode-name "Erin-TWiki")
(use-local-map erin-mode-map)
(set-syntax-table erin-mode-syntax-table)
;; (erin-setq-local fill-paragraph-function 'erin-fill-paragraph)
(erin-setq-local font-lock-beginning-of-syntax-function 'beginning-of-line)
(erin-setq-local font-lock-defaults '(erin-font-lock-keywords t nil nil nil))
(erin-setq-local indent-line-function 'erin-indent-line-function)
(erin-setq-local indent-tabs-mode nil)
(run-hooks 'erin-mode-hook))
(defun erin-sampler ()
(interactive)
(save-excursion
(set-buffer (get-buffer-create "*erin-TWiki-sampler*"))
(buffer-disable-undo)
(goto-char (point-min))
(delete-region (point-min) (point-max))
(insert
"This is a sampler of TWiki markup as displayed by erin-mode.\n"
"Things labeled \"(TODO)\" are displayed incorrectly.\n\n"
"WikiWord\n"
"WIKI.WikiWord\n"
"NONWikiWord\n"
"WIKI.NONWikiWord\n"
"[[WikiWord]]\n"
"[[WIKI.WikiWord]]\n"
"[[NONWikiWord]]\n"
"[[WIKI.NONWikiWord]]\n"
"[[http://host/]]\n"
"[[WikiWord][link text]]\n"
"[[WIKI.WikiWord][link text]]\n"
"[[NONWikiWord][link text]]\n"
"[[http://host/][link text]]\n"
"[[WikiWord#AnchorName]]\n"
"#AnchorName Text\n"
"!WikiWord\n"
"![[NONWikiWord]] (TODO)\n"
"\n"
"*bold* normal *bold words* here*not*bold\n"
"_italic_ normal _italic words_ here_not_italic\n"
"=fixed= normal =fixed words= here=not=fixed\n"
"__bold italic__ normal here__not__bolditalic\n"
"==bold fixed== normal here==not==boldfixed\n"
"\n"
"normal *x* normal *x:* normal *x*: normal\n"
"\n"
"%TOC% %X% %T% %VARIABLE%\n"
"%VARIABLE{ parameter=\"value\" }%\n"
"%BR%\n"
"\n"
"------------------------------\n"
"---+ First-Level Heading\n"
"---++ Second-Level Heading\n"
"---+++ Third-Level Heading\n"
"---++++ Fourth-Level Heading\n"
"---+++++ Fifth-Level Heading\n"
"---++++++ Sixth-Level Heading\n"
" * Non-bullet\n"
" * First-level bullet\n"
" * Second-level bullet\n"
" * Third-level bullet\n"
" * Fourth-level bullet\n"
" * Fifth-level bullet\n"
" * Sixth-level bullet\n"
" 1. Non-numbered\n"
" 1. First-level\n"
" 1. Second-level\n"
" 1. Third-level\n"
" 1. Fourth-level\n"
" 1. Fifth-level\n"
" 1. Sixth-level\n"
" 111. Multi-digit\n"
" A. Capital letters\n"
" a. Lowercase letters\n"
" I. Roman capitals\n"
" i. Roman lowercase\n"
" $ name: First-level\n"
" $ name: Second-level\n"
" $ name: Third-level\n"
" $ name: Fourth-level\n"
" $ name: Fifth-level\n"
" $ name: Sixth-level\n"
"\n"
"<verbatim> WikiWord *bold* </verbatim> (TODO)\n"
"<foo>some text</foo> (TODO)\n"
"<b>some text</b> (TODO)\n"
"<i>some text</i> (TODO)\n"
"<emph>some text</emph> (TODO)\n"
"<code>some text</code> (TODO)\n"
"<noautolink> WikiWord </noautolink> (TODO)\n"
"<a href=\"http://host/\">link text</a> (TODO)\n"
"\n"
"| *One* | *Two* | *Three* | (TODO)\n"
"| A | B | C | (TODO)\n"
"| Two-Column || Two-Row | (TODO)\n"
"| X | Y |^| (TODO)\n")
(buffer-enable-undo)
(set-buffer-modified-p nil)
(goto-char (point-min))
(erin-mode)
(pop-to-buffer (current-buffer))))
(defun erin-rv-sampler ()
(interactive)
(start-process
"erin-rv-sampler" nil "emacs" "-rv" "--eval"
"(progn (require 'erin) (erin-sampler) (delete-other-windows))"))
(defconst erin-renumber-headings-re
(concat
"^-\\{3,\\}"
"\\(\\++\\)" ; =1 pluses
"\\(!!\\)?" ; =2 bangs
"\\(" ; <3 replace
"[ \t]*"
"\\(" ; <4 number
"[0-9]+\\(?:\\.[0-9]+\\)*"
"\\)?" ; >4
"\\.?"
"[ \t]*"
"\\)" ; >3
;;"\\([^\r\n]*\\)"
))
(defun erin-renumber-headings ()
(interactive)
(save-excursion
(save-match-data
(goto-char (point-min))
(let ((last-number (list 0)))
(while (re-search-forward erin-renumber-headings-re nil t)
;; Go to the beginning of line, so that we don't accidentally advance
;; a line by replacing an existing number string with something
;; shorter.
(beginning-of-line)
;; Replace existing replaceable part.
(replace-match
;; Do we have a double-bang?
(if (match-beginning 2)
;; Double-bang, so make sure it doesn't have a number, and
;; otherwise normalize the whitespace before the text.
" "
;; No double-bang, so make sure it has the correct number. Do
;; this in as anti-Scheme fashion as possible.
(concat
" "
(let ((level (- (match-end 1) (match-beginning 1)))
(probe last-number))
(while (> level 0)
(setq level (1- level))
(if (= 0 level)
(progn (setcar probe (1+ (car probe)))
(setcdr probe nil))
(setq probe
(or (cdr probe)
(setcdr probe (cons 0 nil))))))
(mapconcat 'number-to-string last-number "."))
" "))
t t nil 3)
;; Advance to next line.
(forward-line))))))
;; Filling:
;; PARAINFO and PARATYPE
;;
;; PARAINFO Is a list of the format:
;;
;; (PARATYPE START FIRSTLINEP { EXTRA }* )
;;
;; START is the point that is the start of the paragraph.
;;
;; FIRSTLINEP is a boolean value for whether or not PT is in the first line
;; of the paragraph.
;;
;; PARATYPE is one of the symbols from the table below. The contents of the
;; EXTRA value depends on PARATYPE.
;;
;; PARATYPE EXTRA elements
;; --------------------- ---------------------------------
;; blank-line
;; description-list-item (<item-depth> <item-label-string>)
;; heading <heading-level>
;; normal
;; ordered-list-item (<item-depth> <item-number-string>)
;; separator
;; table-row
;; unordered-list-item (<item-depth>)
;; (defmacro erin-make-parainfo (paratype start firstlinep extra)
;; `(vector ,paratype ,start ,firstlinep ,extra))
;;
;; (defmacro erin-get-parainfo-paratype (parainfo) `(aref ,parainfo 0))
;; (defmacro erin-get-parainfo-start (parainfo) `(aref ,parainfo 1))
;; (defmacro erin-get-parainfo-firstlinep (parainfo) `(aref ,parainfo 2))
;; (defmacro erin-get-parainfo-extras (parainfo) `(aref ,parainfo 3))
;;
;; (defmacro erin-set-parainfo-paratype (parainfo x) `(aset ,parainfo 0 ,x))
;; (defmacro erin-set-parainfo-start (parainfo x) `(aset ,parainfo 1 ,x))
;; (defmacro erin-set-parainfo-firstlinep (parainfo x) `(aset ,parainfo 2 ,x))
;; (defmacro erin-set-parainfo-extras (parainfo x) `(aset ,parainfo 3 ,x))
;;
;; ;; TODO: Change parainfo to an array.
;;
;; ;; (defun erin-find-line-para-something-from-point (want-parainfo
;; ;; default-paratype
;; ;; previous-backslash-presence)
;; ;; "This does the bulk of the work for `erin-initial-parainfo' !!!"
;;
;; ;; '!!!)
;;
;; ;; (defun erin-find-next-para (!!!)
;; ;; '!!!)
;;
;; (defun erin-initial-parainfo (pt)
;; (save-excursion
;; (save-match-data
;; ;; Note: This is a helper function for erin-paragraph-start-info. It
;; ;; works from point and changes point, as well as stomps on match data.
;; (let ((parainfo nil)
;; (last-normal-parainfo nil)
;; (firstlinep t))
;; ;; Loop until we've set `parainfo', which should be a list beginning
;; ;; with paragraph type symbol and followed by any type-specific
;; ;; elements.
;; (while (not parainfo)
;; ;; Make sure we're at the beginning of line.
;; (beginning-of-line)
;; ;; Does the previous line end in a backslash?
;; ;;
;; ;; TODO: In current versions of TWiki, does backslash really continue
;; ;; all paragraphs, only table rows, or some other subset?
;; (if (save-excursion
;; (condition-case err
;; ;; Note: This won't work with CR-LF newlines.
;; (progn (backward-char 2)
;; (looking-at "\\\\"))
;; (beginning-of-buffer nil)
;; (end-of-buffer nil)))
;; ;; Preceding line ends in a backslash, so go to previous line for
;; ;; the next iteration.
;; (forward-line -1)
;; ;; Preceding line doesn't end in a backslash, determine the
;; ;; paragraph type from the current line, and leave point at first
;; ;; character of paragraph.
;; (cond
;; ;; Table row.
;; ((looking-at "|")
;; (setq parainfo
;; (erin-make-parainfo 'table-row (point) firstlinep nil)))
;; ;; Blank line.
;; ((looking-at "[ \t]*$")
;; ;; TODO: Maybe we should find the first blank line.
;; (setq parainfo (or last-normal-parainfo
;; (erin-make-parainfo 'blank-line
;; (point)
;; firstlinep
;; nil))))
;; ;; Separator or heading.
;; ((looking-at "-\\{3,\\}\\(?:\\(\\++\\)\\|\\(?:[ \t\r]*$\\)\\)")
;; (setq parainfo
;; (or last-normal-parainfo
;; (if (match-beginning 1)
;; (erin-make-parainfo 'heading
;; (point)
;; firstlinep
;; (- (match-end 1) (match-beginning 1)))
;; (erin-make-parainfo 'separator
;; (point)
;; firstlinep
;; nil)))))
;; ;; List item?
;; ((looking-at "\\( \\)+\\([0-9]+\\|[AaIi]\\)\\.")
;; ;; TODO: !!! Add unordered-list and description-item to this
;; ;; regexp. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;; (let ((item-depth (/ (- (match-end 1) (point)) 3)))
;; (setq parainfo
;; (erin-make-parainfo 'ordered-list-item
;; (point)
;; firstlinep
;; (list
;; item-depth
;; (match-string-no-properties 2))))))
;; ;; Nothing else matched, so we might be in a `normal', so set
;; ;; `last-normal-parainfo' to normal and either use that as the
;; ;; parainfo (if we're at the start of buffer) or move point to
;; ;; previous line and iterate.
;; (t (setq last-normal-parainfo
;; (erin-make-parainfo 'normal (point) firstlinep nil))
;; (if (bobp)
;; (setq parainfo last-normal-parainfo)
;; (forward-line -1)))))
;; ;; Set `first-line-p' to nil, in case we're advancing a line and
;; ;; iterating.
;; (setq firstlinep nil))
;; ;; Done iterating, so return the parainfo.
;; parainfo))))
;;
;; ;; TODO: !!!!!!!!!!! add END field to parainfo, and populate for the one-liner
;; ;; partypes as we scan them.
;;
;; (defun erin-parainfo (pt)
;; (let ((parainfo (erin-initial-parainfo pt)))
;; ;; TODO: !!! Also find the end of the paragraph, and set it in
;; ;; parainfo.
;; parainfo))
;;
;; (defun erin-fill-paragraph (arg)
;; (interactive (list nil))
;;
;; (let* ((parainfo (erin-parainfo (point)))
;; (paratype (erin-get-parainfo-paratype parainfo)))
;;
;; ;; (cond
;; ;; ((eq paratype 'normal)
;; ;; ;;!!!!!!!!!!!!!!!!
;; ;; )
;;
;; ;; ;; TODO: Implement other types.
;; '!!!
;;
;; (message "DEBUG: %S" parainfo)))
;; URL support.
(defun erin-got-login-page ()
"Check whether the login page was retrieved.
Return t if the TWiki login page was retrieved by
`url-retrieve-synchronously', nil otherwise."
(save-excursion
(goto-char (point-min))
(search-forward "<title>(TWiki login)" nil t)))
(defun erin-format-url (operation endpoint)
"Format a TWiki URL.
Return a URL by inserting OPERATION into `erin-url-format' and
adding the ENDPOINT."
(when (equal erin-url-format "")
(error "Customize erin-url-format"))
(concat (format erin-url-format operation) "/" endpoint))
;;;###autoload
(defun erin-log-in ()
"Log in to the TWiki.
This function will prompt for a TWiki username and password. It
will auto-fill the username with `erin-username'.
Upon successful log-in Emacs will store a TWiki session cookie in
`url-cookie-storage'. If this session cookie expires the user
will need to call `erin-log-in' again."
(interactive)
(let ((url-request-method "POST")
(url-request-extra-headers `(("Content-Type"
. "application/x-www-form-urlencoded")))
(url-request-data (mm-url-encode-www-form-urlencoded
(list (cons "username"
(read-from-minibuffer
"Username: " erin-username))
(cons "password"
(password-read "Password: "))))))
(with-current-buffer
(url-retrieve-synchronously (erin-format-url "login" "WebHome"))
(if (not (erin-got-login-page))
(message "erin: Login succeeded")
(message "erin: Login failed")))))
(defun erin-log-out ()
"Log out of the TWiki."
(interactive)
(let ((url-request-method "POST")
(url-request-extra-headers `(("Content-Type"
. "application/x-www-form-urlencoded")))
(url-request-data (mm-url-encode-www-form-urlencoded
(list (cons "logout" "1")))))
(with-current-buffer
(url-retrieve-synchronously (erin-format-url "login" "WebHome"))
(message "erin: Logout succeeded"))))
(defun erin-edit-topic (topic)
"Edit a topic page from the TWiki.
First, this function downloads the text of the page titled TOPIC
from the TWiki, to a uniquely-named temporary file in
`erin-local-directory'. It then opens the file for editing in a
new buffer."
(interactive "MTopic: ")
(let ((url-request-method "GET")
(url-request-extra-headers '())
(url-request-data "")
;; . can't be used in topic names so use it as a separator.
;; When the topic page contained in this file is saved, we
;; strip off the leading directory and the trailing
;; .<temp-string>.twiki and use the result as the topic page
;; to post to.
(temporary-file-name))
(save-excursion
(with-current-buffer
(url-retrieve-synchronously (erin-format-url "edit" topic))
(if (erin-got-login-page)
(message "erin: Download failed. Log in again")
(progn
(message "erin: Download succeeded")
(goto-char (point-min))
(search-forward-regexp "<textarea.*?>")
(delete-region (point-min) (point))
(goto-char (point-max))
(search-backward-regexp "</textarea")
(delete-region (point) (point-max))
(mm-url-decode-entities)
(goto-char (point-min))
(if (not (file-name-absolute-p
erin-local-directory))
(error (concat "The variable erin-local-directory"
" must hold an absolute path")))
(make-directory erin-local-directory t)
(setq temporary-file-name (make-temp-file
(concat (file-name-as-directory
erin-local-directory)
topic ".") nil ".twiki"))
(write-region (point-min) (point-max) temporary-file-name))))
(when temporary-file-name
(find-file temporary-file-name)))))
(defun erin-current-topic ()
"Return the topic name of the current buffer.
The topic name is the base name of the file name returned by the
function `buffer-file-name', excluding the first period and
anything after it. TWiki topic names must not contain periods so
the period in the file name can be used as a delimiter."
(car (split-string (file-name-nondirectory (buffer-file-name))
"\\.")))
(defun erin-save-topic ()
"Save the edited contents of the current buffer's topic page to the TWiki.
See `erin-current-topic' for how the topic name is determined."
(interactive)
(let ((url-request-method "POST")
(url-request-extra-headers
`(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data (mm-url-encode-www-form-urlencoded
(list (cons "text" (buffer-string)))))
(name (erin-current-topic)))
(if (y-or-n-p
(format "Post this buffer's contents as topic %s? " name))
(with-current-buffer
(url-retrieve-synchronously (erin-format-url "save" name))
(goto-char (point-min))
(if (search-forward
(concat "Location: " (erin-format-url "view" name)) nil t)
(message "erin: Save succeeded")
(message "erin: Save failed. Log in again")))
(message "erin: Save cancelled"))))
(defun erin-cancel-edit ()
"Cancel editifng the current buffer's topic page in the TWiki.
See `erin-current-topic' for how the topic name is determined."
(interactive)
(let ((url-request-method "POST")
(url-request-extra-headers `(("Content-Type"
. "application/x-www-form-urlencoded")))
(url-request-data (mm-url-encode-www-form-urlencoded
(list (cons "action_cancel" "Cancel"))))
(name (erin-current-topic)))
(with-current-buffer
;; No return checking here. In other words, we assume this
;; works since if it doesn't it's probably because the user is
;; not logged in, in which case other users won't be blocked.
(url-retrieve-synchronously (erin-format-url "save" name))
(message
(format "erin: Edit cancelled. Others can now edit the %s topic"
name)))))
(defun erin-kill-buffer-and-delete-file ()
"Finish editing.
Kill the current buffer and delete the file associated with it."
(interactive)
(if (y-or-n-p (format "Delete file %s and kill buffer %s? "
(buffer-file-name) (buffer-name)))
(progn
;; Try to cancel the edit in case the user didn't save before
;; this.