-
Notifications
You must be signed in to change notification settings - Fork 5
/
twiki.el
1804 lines (1553 loc) · 55.8 KB
/
twiki.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
;;; twiki.el --- mode for editing Twiki wiki files for emacs
;; Copyright (C) 2010-2012 Christopher J. White
;; Author: Christopher J. White <[email protected]>
;; Maintainer: Christopher J. White <[email protected]>
;; Keywords: twiki, wiki
;; Last-modified: 2012-08-01
;; Version 1.2
;; GNU General Public License v3 (GNU GPL v3),
;;
;; This file is not part of GNU Emacs.
;;
;; This program 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 of the License, or (at
;; your option) any later version.
;;
;; This program 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.
;;
;; The license text: <http://www.gnu.org/licenses/gpl-3.0.html>
;;; Commentary:
;;
;; This package is a major mode for editing TWiki pages
;; (http://twiki.org). Twiki formatted pages are reformatted on
;; import into a twiki buffer to enhance readibility and friendlier
;; editing. After editing, the pages are "exported" back to the twiki
;; format.
;;
;; The key benefits to twiki mode:
;;
;; * Keyboard shortcuts for common operations
;; * Auto-numbering of headings, numbered lists
;; * Better bullet list management
;; * orgtbl-minor mode for table editing
;; * table numbering
;; * font-lock support for syntax highlighting
;;
;; Typical usage:
;;
;; 1. In Web Browser, click edit in Twiki on the page to edit
;;
;; 2. Select all and copy to clipboard
;;
;; 3. In emacs, twiki-import-for-edit - assuming you are not already
;; in a buffer in twiki-mode, this will:
;; * create a temp file
;; * create buffer visiting that file,
;; * set the major mode to twiki-mode
;; * render the buffer for "editing", making bullet/lists more readable
;;
;; 4. Edit as necessary
;;
;; 5. Export to clipboard using C-c e (twiki-export-to-clipboard)
;;
;; 6. Back in Web Browser, Select All, and Paste from clipboard
;; (replacing old text)
;;
;; 7. Optionally save the file elsewhere
;;
;; Interesting twiki-mode keys:
;; C-c h Make current line a header line (asks for level, or prefix arg)
;; C-c 1-6 Make a Header 1-6 line
;; C-c C-h Renumber all headers using 1.4.3 notation
;; C-c C-r Renumber all headers and tables
;; C-c C-t Renumber all tables
;; C-c i Import clipboard
;; C-c e Export to clipboard, rendering bullets/lists back to
;; twiki format
;; Bullet lists:
;; Tab Indent bullet list
;; S-Tab Unindent bullet list
;;
;; Headings:
;; Tab Hide/show direct subtree
;; S-Tab Hide/show all subtrees
;;
;; Server:
;; C-c t p Upload to the server for a preview
;; C-c t s Save as a new revision on the server
;; C-c t u Update the local copy from the latest on the server
;; C-c t t Return the status of the current document
;; C-c t d Diff the current buffer against the server version
;;
;; When saving the buffer to a file, the format is saved as
;; twiki-format (the buffer is rendered for export, then saved).
;;
;;
;;; INSTALLATION:
;;
;; Put this in your .emacs to associate "*.twiki" files with twiki-mode:
;;
;; (add-to-list 'auto-mode-alist'("\\.twiki$" . twiki-mode))
;;
;;; COMMAND LINE TOOL:
;;
;; Install the command line tool 'twikish' in a location in the default path,
;; or set `twiki-shell-cmd' to the proper location.
;;
;; This enables the following integrations from within emacs. See the
;; Server section in the section above.
;;
;; Additional command `twiki-open-topic' can be called to download
;; a topic that hasn't yet been stored locally.
;;
;;
;;; FIREFOX ITS-ALL-TEXT ADDON INTEGRATION
;;
;; Firefox supports "It's All Text!", a plug-in that will allow using emacs (or
;; any editor) to edit the contents of text areas.
;;
;; 1. Install the add-on from:
;; https://addons.mozilla.org/en-US/firefox/addon/its-all-text/
;;
;; 2. Setup the auto-mode-alist for ".twiki" above
;;
;; 3. Right-click in a text area in Firefox, select It's All Text ->
;; Preferences
;;
;; 4. Configure the editor to be "emacsclient", whereever that is on
;; your system
;;
;; 5. Add ".twiki" to the list of file extensions. If you put it
;; first, all text areas will default to .twiki when pressing the
;; hot key or clicking the "Edit" button
;;
;; Note, on Windows if you have emacs installed and you get errors
;; about not finding the socket, you may need to specify the full path
;; to the server socket file that is used by the emacs server you're using.
;; This would have to go into a ".bat" file that in turn calls emacsclient
;; with the additional "--server-file=<>" argument, as that can't be put in
;; the editor command in the Its-All-Text preferences.
;;
;; To get your server-file, try: (process-get server-process :server-file)
;;
;;
;;; CUSTOMIZATION
;;
;; M-x customize-group twiki
;;
;;
;;; AUTO-NUMBERING OF HEADINGS
;;
;; On import or load of a twiki file, if headings were previously
;; numbered, the numbering and min-level will automatically be
;; detected. If not, the user must call 'twiki-setup-heading-numbers'
;; to setup the minimum and maximum heading levels to be numbered.
;; This sets the variables 'twiki-min-heading-level and
;; twiki-max-heading-level. All subsequent renumbering will only
;; affecting headings between min and max inclusive.
;;
;; All headings are number using "1.2.3" notation, one additional level
;; of decimal notation for each level deeper than 'twiki-min-heading level:
;;
;; 1. First level
;; 1.1. Next level
;; 1.2. Same level as above
;; 2. Back to first level
;;
;;
;;; TABLES USING ORGTBL
;;
;; Table editing is handled by orgtbl-mode (see Info topic of Org-mode). Tables
;; are added by simply inserting vertical bars as the beginning of the line, one
;; vertical bar on each end of a row:
;;
;; | *Header 1* | *Header 2* |
;;
;; With the cursor in a table line, the basic keys are as follows:
;;
;; Tab next cell, or if at the end of the row, advance to
;; next row, creating a new row if necessary
;;
;; S-Tab prev cell
;;
;; Ret move to next row in the same column, creating a row
;; if necessary
;;
;; C-c C-c Realign the table
;;
;; Just type away in any cell. On tab, realignment occurs,
;; "beautifying" the table so all rows have the same columns.
;;
;;
;;; COLSPAN
;;
;; Twiki tables support colspan by interpreting two adjacent vertical bars
;; as an extention of the previous column:
;;
;; | Col 1 | Col 2 |
;; | Colspan 2 & 2 ||
;;
;; Unfortunately, orgtbl does not really do colspan and if the table
;; is realigned (on Tab, for example), the table will be reformatted
;; as follows:
;;
;; | Col 1 | Col 2 |
;; | Colspan 2 & 2 | |
;;
;; To get around this, put "<<" in the column that should be merged
;; with the previous column. On export, any columns that have only
;; "<<" as the cell text will get turned back into "||" so that Twiki
;; performs the appropriate colspan:
;;
;; | Col 1 | Col 2 |
;; | Colspan 2 & 2 | << |
;;
;;
;;; COLUMN WIDTH SPECIFIERS
;;
;; It's possible to specify column-width in orgbl as follows:
;;
;; | <10> | | <20> |
;; | *Name* | *Age* | *Address* |
;; | Christo=>| 25 | 1234 Really Long=> |
;;
;; (See org-table for more info on how this works and editing such fields)
;;
;; On export, this descriptor row is put in a comment block so it's hidden
;; from view on the page. On import again, the row is reinstated.
;;
;;
;;; AUTO-NUMBERING OF TABLES
;;
;; Table number simply looks for any line that matches "| *Table [0-9]+:". The
;; number matched is replaced with a simple sequence from the start of the file.
;;
;;; KNOWN ISSUES:
;;
;; - Two lists that are separated by a blank line in twiki syntax get munged
;; together as a single list by twiki-render-for-edit
;;
;;; TODO:
;;
;; - Handle heading / table number references on renumber
;;
;; - Cell-alignment with orgtbl is different than twiki as twiki
;; uses amount of spaces on either side of the content to align
;; the cell. orgtbl is a little smarter about numbers vs not, and
;; supports left or right alignment. Unclear the best way to
;; combine the two.
;;
;;
;;; CHANGE LOG
;;
;; 2011-07-19 (chris)
;; - First public release
;;
;; 2011-07-21 (chris)
;; - Eliminated the question to add numbering on import, added function
;; "twiki-setup-heading-numbers"
;; - Fixed twiki-electric-space so that it does not break table lines
;;
;; 2011-07-22 (chris)
;; - Fixed bugs with renumbering and non-TOC headers containing "!!"
;; - Added supported for file-save to write twiki-format
;;
;; 2011-07-22 (chris)
;; - Fixed hang on export on a bullet list with an empty bullet
;;
;; 2011-07-22 (chris)
;; - Fixed a bug with list numbering of alphanumeric lists (1., A.)
;;
;; 2011-07-29 (chris)
;; - Added support for orgtbl column-descriptor row, commenting out the line
;; on export, reinstating it on import
;;
;; 2011-07-31 (chris) -- Version 1.0.0
;; - cleaned up the code, made a 'twiki' customozation group
;; - added boolean for disabling orgtbl
;; - fixed faces so they work for light and dark background modes
;;
;; 2011-09-03 (chris) -- Version 1.0.1
;; - fixed twiki-electric-space not to break links
;; of the form [[ref][title]] or [[ref]]
;;
;; 2012-05-14 (chris) -- Version 1.0.2
;; - Fixed twiki-electric-space so that it does not break headings
;;
;; 2012-05-19 (chris)
;; - Fixed handling of links so that they are not broken on save
;; - Added support for C-u C-c <num> to set a heading as invisible to the TOC
;; - Turn on outline-minor-mode and support [tab] and [S-tab] to toggle
;; visiblity of subtrees
;;
;; 2012-06-20 (chris) -- Version 1.1.0
;; - Better line break support when links are in a line
;; - Added `twiki-verbatim' [C-c v] to add <verbatim></verbatim>
;; around a block
;;
;; 2012-08-01 (chris) -- Version 1.2
;; - Drop '-' as bullet marker, as this causes problems when '-'
;; in a bullet line happens to end up after a line break
;; - Fixed logic regarding import and overwriting the buffer
;; - Fixed twiki-electric-space so that it does not break incomplete
;; table lines (ie, regonized a table line as starting with |, but
;; not necessarily ending with |)
;; - Added support for twikish command line tool
;;
;; 2012-11-05 (chris) -- Version 1.3
;; - Added font-lock for bumpy case links
;; - Fixed buffer showing modified after save
;; - Fixed problems with longlines-mode
;;
;; 2013-03-04 (chris) -- Version 1.3.1
;; - Fixed Git issue #1 - Emacs 24 filter-buffer-substring dropped NOPROPS arg
;;
(provide 'twiki)
(require 'info)
(require 'cl)
(defconst twiki-version "1.1.0")
;;; ------------------------------------------------------------
;;; Configurable stuff
;;; ------------------------------------------------------------
(defcustom twiki-tab-width 3
"Spacing for bullet lists, etc. Probably best to leave at 3 to match what
twiki expects."
:group 'twiki
:type '(integer)
)
(defcustom twiki-min-heading-level 1
"Min heading level to include for heading renumbering."
:group 'twiki
:type '(integer)
:options '(1 2 3 4 5 6)
)
(defcustom twiki-max-heading-level 6
"*Max heading level to include for heading renumbering."
:group 'twiki
:type '(boolean)
:options '(1 2 3 4 5 6)
)
(defcustom twiki-use-orgtbl t
"Set to nil to disable the use of orgtbl-minor mode for tables"
:group 'twiki
:type '(boolean)
)
(defcustom twiki-silent-import nil
"If non-nil, import into a buffer already in twiki-mode will not ask about overwrite"
:group 'twiki
:type '(boolean)
)
(defcustom twiki-shell-cmd "twikish"
"Shell command that supports commands that interact with the twiki server"
:group 'twiki
:type 'string
)
;;
;; Internal variables
;;
(defvar twiki-heading-base-string ""
"*Base string to use for heading renumbering")
(defvar twiki-start-heading-number ""
"Staring heading number. Computed automatically")
(defvar twiki-block-tags
'("php" "file" "script" "code" "verbatim")
"List of tags that delineate blocks that should not be parsed, such as <file> </file>"
)
(defvar twiki-kill-export-buffer t
"*If non-null, kill the export buffer when done exporting")
(defvar twiki-font-lock-keywords
(list
(list "^\\(---\\+!* .*\\)"
'(1 'twiki-heading-1))
(list "^\\(---\\+\\+!* .*\\)"
'(1 'twiki-heading-2))
(list "^\\(---\\+\\+\\+!* .*\\)"
'(1 'twiki-heading-3))
(list "^\\(---\\+\\+\\+\\+!* .*\\)"
'(1 'twiki-heading-4))
(list "^\\(---\\+\\+\\+\\+\\+!* .*\\)"
'(1 'twiki-heading-5))
(list "\\(:\\?:\\)"
'(1 'highlight))
(list "\\(\\*\\b.*?\\b\\*\\)"
'(1 'bold))
(list "\\(\\b__.*?__\\b\\)"
'(1 'bold-italic))
(list "\\(\\b_.*?_\\b\\)"
'(1 'italic))
(list "\\(\\[\\[.*\\]\\]\\)"
'(1 'highlight))
(list "\\({{.*}}\\)"
'(1 'highlight))
(list "\\(^\\|[^!]\\)\\(\\b[A-Z]+[a-z]+[A-Z]+[a-zA-Z0-9]*\\b\\)"
'(2 'highlight))
)
)
(defvar twiki-debug nil
"Set to non-null to generate debug messages when rendering, etc.")
(defconst twiki-bullet-regex
"\\([*]\\|[0-9]+\\.\\|[a-zA-Z]\\.\\|i+\\.\\|iv\\.\\|v\\.\\)")
(defconst twiki-bullet-renumber-regex
"\\([*-]\\|[0-9]+\\.\\|[a-zA-Z]\\.\\|i+\\.\\|iv\\.\\|v\\.\\)")
(defvar twiki-importing nil)
(defvar twiki-subtree-closed nil)
(defvar twiki-mode-map nil
"The keymap that is used in this mode.")
(if twiki-mode-map
()
(setq twiki-mode-map (make-sparse-keymap))
;; set up the keymap
(define-key twiki-mode-map (read-kbd-macro "C-c h") 'twiki-heading)
(define-key twiki-mode-map (read-kbd-macro "C-c b") 'twiki-bold)
(define-key twiki-mode-map (read-kbd-macro "C-c /") 'twiki-italic)
(define-key twiki-mode-map (read-kbd-macro "C-c C-h") 'twiki-renumber-headings)
(define-key twiki-mode-map (read-kbd-macro "C-c C-r") 'twiki-renumber-all)
(define-key twiki-mode-map (read-kbd-macro "C-c C-t") 'twiki-renumber-tables)
(define-key twiki-mode-map (read-kbd-macro "C-c 1") 'twiki-heading-1)
(define-key twiki-mode-map (read-kbd-macro "C-c 2") 'twiki-heading-2)
(define-key twiki-mode-map (read-kbd-macro "C-c 3") 'twiki-heading-3)
(define-key twiki-mode-map (read-kbd-macro "C-c 4") 'twiki-heading-4)
(define-key twiki-mode-map (read-kbd-macro "C-c 5") 'twiki-heading-5)
(define-key twiki-mode-map (read-kbd-macro "C-c i") 'twiki-import-for-edit)
(define-key twiki-mode-map (read-kbd-macro "C-c e") 'twiki-export-to-clipboard)
(define-key twiki-mode-map (read-kbd-macro "C-c t p") 'twiki-preview)
(define-key twiki-mode-map (read-kbd-macro "C-c t u") 'twiki-update)
(define-key twiki-mode-map (read-kbd-macro "C-c t s") 'twiki-save)
(define-key twiki-mode-map (read-kbd-macro "C-c t t") 'twiki-status)
(define-key twiki-mode-map (read-kbd-macro "C-c t d") 'twiki-diff)
(define-key twiki-mode-map (read-kbd-macro "C-c v") 'twiki-verbatim)
(define-key twiki-mode-map " " 'twiki-electric-space)
(define-key twiki-mode-map [tab] 'twiki-indent-line)
(define-key twiki-mode-map [S-tab] 'twiki-unindent-line)
)
(defvar twiki-mode-syntax-table nil)
(defvar twiki-mode-hook '())
(unless twiki-mode-syntax-table
(setq twiki-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\( "() " twiki-mode-syntax-table)
(modify-syntax-entry ?\) ")( " twiki-mode-syntax-table)
(modify-syntax-entry ?\{ "(} " twiki-mode-syntax-table)
(modify-syntax-entry ?\} "){ " twiki-mode-syntax-table))
;;;###autoload
(defgroup twiki nil "Group for twiki-mode related elements."
:prefix "twiki-"
:group 'hypermedia)
(defface twiki-heading-1
'( (((class color) (background dark)) :foreground "yellow" :height 1.7 :inherit 'variable-pitch)
(((class color) (background light)) :foreground "OrangeRed" :height 1.7 :inherit 'variable-pitch) )
"Face for Twiki heading level 1"
:group 'twiki)
(defface twiki-heading-2
'( (((class color) (background dark)) :foreground "yellow" :height 1.6 :inherit 'variable-pitch)
(((class color) (background light)) :foreground "OrangeRed" :height 1.6 :inherit 'variable-pitch) )
"Face for Twiki heading level 2"
:group 'twiki)
(defface twiki-heading-3
'( (((class color) (background dark)) :foreground "yellow" :height 1.4 :inherit 'variable-pitch)
(((class color) (background light)) :foreground "OrangeRed" :height 1.4 :inherit 'variable-pitch) )
"Face for Twiki heading level 3"
:group 'twiki)
(defface twiki-heading-4
'( (((class color) (background dark)) :foreground "yellow" :height 1.2 :inherit 'variable-pitch)
(((class color) (background light)) :foreground "OrangeRed" :height 1.2 :inherit 'variable-pitch) )
"Face for Twiki heading level 4"
:group 'twiki)
(defface twiki-heading-5
'( (((class color) (background dark)) :foreground "yellow" :underline t :inherit 'variable-pitch)
(((class color) (background light)) :foreground "OrangeRed" :underline t :inherit 'variable-pitch) )
"Face for Twiki heading level 5"
:group 'twiki)
(defun twiki-mode ()
"Major mode for editing Twiki files.
This function ends by invoking the function(s) `twiki-mode-hook'.
\\{twiki-mode-map}"
(interactive)
(kill-all-local-variables)
(make-local-variable 'font-lock-defaults)
(make-local-variable 'twiki-min-heading-level)
(make-local-variable 'twiki-max-heading-level)
(make-local-variable 'twiki-heading-base-string)
(make-local-variable 'twiki-start-heading-number)
(outline-minor-mode)
(make-local-variable 'outline-regexp)
(setq outline-regexp "---\\++")
(setq twiki-start-heading-number nil)
(setq font-lock-defaults '(twiki-font-lock-keywords))
;; become the current major mode
(setq major-mode 'twiki-mode)
(setq mode-name "Twiki")
;; Activate keymap and syntax table.
(use-local-map twiki-mode-map)
(set-syntax-table twiki-mode-syntax-table)
(setq indent-tabs-mode nil)
(setq tab-width twiki-tab-width)
(twiki-render-for-edit)
(add-hook 'before-save-hook
(lambda () (when (eq major-mode 'twiki-mode)
(twiki-render-for-export)
)))
(add-hook 'after-save-hook
(lambda () (when (eq major-mode 'twiki-mode)
(twiki-render-for-edit)
(set-buffer-modified-p nil)
)))
(when twiki-use-orgtbl (turn-on-orgtbl))
(run-hooks 'twiki-mode-hook)
)
;;
;; twiki-heading
;;
(defun twiki-heading (level)
"Insert a heading at the given level"
(interactive "NLevel: ")
(beginning-of-line)
(let ((hs (make-string (abs level) ?+)))
(cond
((looking-at (twiki-heading-regex))
(let ((text (match-string 6))
(base-num (if (< level 0) "" (match-string 3)))
(suppress (if (< level 0) "!!" (match-string 2))))
(replace-match (format "---%s%s%s %s" hs suppress base-num text))
))
((looking-at "^\\(.*\\)$")
(let ((s (or (match-string 1) "")))
(replace-match (format "---%s%s %s" hs (if (< level 0) "!!" "") s))
))
)
(beginning-of-line)
(forward-char (+ (if (< level 0) 6 4) (abs level))))
(twiki-renumber-headings)
)
(defun twiki-heading-1 (hide)
(interactive "P")
(twiki-heading (if hide -1 1)))
(defun twiki-heading-2 (hide)
(interactive "P")
(twiki-heading (if hide -2 2)))
(defun twiki-heading-3 (hide)
(interactive "P")
(twiki-heading (if hide -3 3)))
(defun twiki-heading-4 (hide)
(interactive "P")
(twiki-heading (if hide -4 4)))
(defun twiki-heading-5 (hide)
(interactive "P")
(twiki-heading (if hide -5 5)))
;;
;; twiki-bold
;;
(defun twiki-bold (start end)
"Makes the selected text bold"
(interactive "r")
(goto-char end)
(insert "*")
(goto-char start)
(insert "*")
)
;;
;; twiki-italic
;;
(defun twiki-italic (start end)
"Makes the selected text italic"
(interactive "r")
(goto-char end)
(insert "_")
(goto-char start)
(insert "_")
)
;;
;; twiki-verbatim
;;
(defun twiki-verbatim (start end)
"Makes the selected text italic"
(interactive "r")
(if mark-active
(progn
(goto-char end)
(insert "</verbatim>")
(goto-char start)
(insert "<verbatim>\n")
)
(insert "\n</verbatim>")
(beginning-of-line)
(forward-line -1)
(insert "<verbatim>\n")
)
)
;;
;; twiki-export-to-clipboard ()
;;
(defun twiki-export-to-clipboard ()
(interactive)
"Render the buffer as a twiki-document, putting the result in the
clipboard suitable for pasting into the editing window of the
twiki-page.
In addition to just copying the text in the buffer, rendering performs
the following translations on the buffer contents:
Bullets and Ordered Lists
For viewing and editing, bullets may take the form:
* bullet one
* bullet two
* bullet three
continuation of bullet three
This will be reformatted to:
* bullet one
* bullet tow
* bullet three continuation of bullet three
This makes sure that Twiki sees the list as a contiguous list
\(particularly import for ordered lists\) as opposed to multiple 1 item
lists. In addition, Twiki does not allow indentation of continuation lines.
"
;; Things to do in the active buffer that should persist
(twiki-renumber-headings)
;; Things to do only in the temp buffer that is relevant to twiki format only
(let ((buf (get-buffer-create (format "*twiki-%s*" (buffer-name))))
(text (filter-buffer-substring (point-min) (point-max)))
)
(save-excursion
(set-text-properties 0 (length text) nil text)
(set-buffer buf)
(erase-buffer)
(insert text)
(twiki-render-for-export)
(set-buffer buf)
(goto-char (point-min))
(clipboard-kill-ring-save (point-min) (point-max))
(if twiki-kill-export-buffer
(kill-buffer buf)
(pop-to-buffer buf))
)
)
(message "Exported Twiki text to clipboard")
)
;;
;; twiki-render-for-edit ()
;;
(defun twiki-render-for-edit ()
(interactive)
"Render the current buffer for editing. This beautifies bullets and ordered
lists, making them more readable for display and editing."
(when twiki-debug (message "Rendering for twiki edit"))
(let ((mod (buffer-modified-p)))
(when twiki-debug (message "-- modified: %S" mod))
;; auto-detect min/max header levels
(save-excursion
(goto-char (point-min))
(let ((found-min 20)
found-number-str
(found-max 0))
(when twiki-debug (message "Searching for headings"))
;; Search for all instances of "---+++ 1. Title" at any level
;; Level is the number of "+", save the min level in 'found-min
(while (re-search-forward
"^---\\(\++\\)!*\\( *\\([.0-9]+\\. *\\)?\\)\\(.*\\)"
;;twiki-heading-base-string used to be in a format...
nil t)
(let ((level (length (match-string 1)))
(number-str (match-string 2)))
(when twiki-debug (message "Found level %d header" level))
(cond
;; This header *has* a number-str
((string-match "[^ ]" number-str)
(when (< level found-min)
(setq found-min level)
(if (null found-number-str) (setq found-number-str number-str))))
)
)
)
(when (not (= found-min 20))
(setq twiki-min-heading-level found-min)
(when twiki-debug (message "Start number-str: %s" found-number-str))
(when (string-match
"^ *\\(\\(.*\\)\\.\\)?\\(\\([0-9]+\\)\\.\\) *$"
found-number-str)
(setq twiki-heading-base-string (match-string 1 found-number-str))
(setq twiki-start-heading-number
(string-to-number (match-string 4 found-number-str)))
)
)
)
(when twiki-debug (message "Processing local variabls"))
;; Read any local-variables that may be specified in the text
(hack-local-variables)
;; If were already in twiki-mode, renumber to twiki, then back to
;; numbers
(when twiki-debug (message "Twiki - Renumbering lists..."))
(if (and (eq major-mode 'twiki-mode)
(not twiki-importing))
(twiki-renumber-list nil))
(twiki-renumber-list t)
;; Renumber headings
(when twiki-debug (message "Twiki - Renumbering heading..."))
(twiki-renumber-headings)
(when twiki-debug (message "Twiki - Renumbering tables..."))
(twiki-renumber-tables)
(when twiki-use-orgtbl
;; Tables in twiki support colspan by adjacent bars "||". This
;; doesn't work well with orgtbl minor mode because it wants to
;; reformat all columns to the same width. So, reformat colspan
;; cells with "| << |" to mark them as spanned with the cell to
;; the left. On export, these cells will be converted back to "||"
(goto-char (point-min))
(when twiki-debug (message "Twiki - Reformatting tables..."))
(while (re-search-forward "||" nil t)
(replace-match "| << |")
(backward-char) ; to ensure we find multiple colspan cells: "|||"
)
;; Recognized rows that declare table alignment and column width rows
;; <10> no alignment defined, 10 chars width
;; <r10> for right aligned, 10 chars width
;; <l10> for lef aligned, 10 chars width
(goto-char (point-min))
(while (re-search-forward
"^<!--\\( *|\\( *<[rl]?[0-9]+> *|\\| *|\\)+ *\\) orgtbl.*-->$" nil t)
(replace-match "\\1")
)
)
)
(set-buffer-modified-p mod)
)
(when twiki-debug (message "Twiki - Done"))
)
;;
;; twiki-render-for-export - render the current buffer as twiki export format
;;
(defun twiki-render-for-export ()
(save-excursion
(goto-char (point-min))
(when twiki-debug (message "Twiki export - renumbering lists"))
(twiki-renumber-list nil)
(when twiki-use-orgtbl
;; Convert "| << | " form back to "||" to represent colspan
;; (See above in render-for-edit)
(when twiki-debug (message "Twiki export - reformatting tables"))
(goto-char (point-min))
(while (re-search-forward "| *<< *|" nil t)
(replace-match "||")
(backward-char) ; be to ensure we find multiple colspan cells: "| << | << |"
)
;; Recognized rows that declare table alignment and column width rows
;; <10> no alignment defined, 10 chars width
;; <r10> for right aligned, 10 chars width
;; <l10> for lef aligned, 10 chars width
(goto-char (point-min))
(while (re-search-forward "^\\( *|\\( *<[rl]?[0-9]+> *|\\| *|\\)+ *\\)$" nil t)
(replace-match "<!--\\1 orgtbl column formatting hints -->")
)
)
)
(when twiki-debug (message "Twiki export - Done"))
)
;;
;; twiki-skip-past-blocks
;;
(defun twiki-skip-past-blocks ()
(let ((matches (match-data))
result)
(when twiki-debug (message "twiki-skip-past-blocks @ %d" (point)))
(if (eq 'twiki-syntax-block (car (twiki-line-syntax)))
(if (re-search-forward (format "</%s.*>" (regexp-opt twiki-block-tags t)) nil t)
(setq result t)
(goto-char (point-max))
(setq result t)
)
)
(when twiki-debug (message "twiki-skip-past-blocks done: result %S" result))
(set-match-data matches)
result
)
)
;;
;; twiki-renumber-list
;;
(defun twiki-renumber-list (&optional to-numbers)
(interactive)
"Renumber all bullets and lists in the buffer. If TO-NUMBERS is non-null,
numbered lists and '-' lists are given numbers relative to their level (suitable
for display). Otherwise list numbers are stripped to '-' for twiki synatax.
"
(save-excursion
;;
;; Compress continuation lines
;;
(when twiki-debug (message "twiki-renumber-list: compressing continuation lines"))
(goto-char (point-min))
(while (re-search-forward
;; Looks for bullet / list item line
(format "^\\(\\(%s\\)+\\(%s\\) \\).*$"
(make-string twiki-tab-width ? )
twiki-bullet-renumber-regex)
nil t)
(unless (twiki-skip-past-blocks)
;; Make indent-str a space string same length up to first char or
;; text of bullet
(let ((indent-str (make-string (- (match-end 1) (match-beginning 1)) ? )))
;; while the next line has this indent-str and is not a nested bullet
;; collapse the line
(while (and (looking-at (format "\n\\(%s\\)\\S +" indent-str))
(not (looking-at (format "\n\\(%s\\)%s " indent-str twiki-bullet-renumber-regex))))
(delete-char (- (match-end 1) (match-beginning 1)))
(end-of-line)
)
)
)
)
;;
;; Collapse blank lines between list items
;; Only collapse a single blank line...multiple blank
;; lines separates multiple lists
;;
(when twiki-debug (message "twiki-renumber-list: collapsing blank lines"))
(goto-char (point-min))
(while (re-search-forward
;; Looks for any amount of white space followed by twiki-bullet-renumber-regex
(format "^ +%s.*$" twiki-bullet-renumber-regex)
nil t)
(unless (twiki-skip-past-blocks)
;; while the next-line is blank followed by a bullet line, collapse
(cond
((looking-at (format "\\(\n *\\)\n\\( \\)+%s " twiki-bullet-renumber-regex))
(replace-match (if to-numbers "\n" "") nil nil nil 1)
;;(replace-match (if (or (not (eq major-mode 'twiki-mode))
;; (not twiki-importing))
;; "" "\n")
;; nil nil nil 1)
(end-of-line)
)
((looking-at (format "\\(\\(\n *\\)\\{2,\\}\\)\n +%s " twiki-bullet-renumber-regex))
(replace-match "\n" nil nil nil 1)
(end-of-line))
)
)
)
;; Renumber
(when twiki-debug (message "Renumbering"))
(goto-char (point-min))
(while (re-search-forward
;; Looks for any amount of white space followed by twiki-bullet-renumber-regex
(format "^\\(\\(%s\\)+\\(%s\\)\\( \\)\\).*$"
(make-string twiki-tab-width ? )
twiki-bullet-renumber-regex)
nil t)
(when twiki-debug (message "twiki-renumber-list, continuing renumber"))
(unless (twiki-skip-past-blocks)
(beginning-of-line)
(twiki-renumber-cur-list to-numbers)
)
)
)
)
;;
;; twiki-renumber-cur-list
;;
;; Assumes that the list is in "twiki" format
;;
(defun twiki-renumber-cur-list (to-numbers &optional parent-depth parent-indent add-newline)
(interactive "sFormat: ")
"Renumber the current list starting at point until the end of nested lists."
;; Renumber
(when twiki-debug (message "twiki-renumber-cur-list %s %s %s %s"
to-numbers parent-depth parent-indent add-newline))
(beginning-of-line)
(let ((cur-indent 0)
cur-depth
(num 1)
(bulletstr nil)
(start t)
)
(if (not parent-depth)
(setq parent-depth 0
parent-indent 0))
(setq cur-depth (1+ parent-depth))
(beginning-of-line)
(when twiki-debug
(looking-at "^.*$")
(message "twiki-renumber-cur-list: %s" (match-string 0)))
(while (looking-at
;; Looks for parent-depth followed by twiki-bullet-renumber-regex