-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjira-markup-mode.el
1298 lines (1095 loc) · 45.2 KB
/
jira-markup-mode.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
;;; jira-markup-mode.el --- Emacs Major mode for JIRA-markup-formatted text files
;; Copyright (C) 2012-2015 Matthias Nuessler <m.nuessler@web.de>
;; Author: Matthias Nuessler <m.nuessler@web.de>>
;; Created: July 15, 2012
;; Version: 0.0.1
;; Keywords: jira, markup
;; URL: https://github.com/mnuessler/jira-markup-mode
;; 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 2, 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.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Jira-markup-mode provides a major mode for editing JIRA wiki markup
;; files. Unlike jira.el it does not interact with JIRA.
;;
;; The code is based on markdown-mode.el, a major for mode for editing
;; markdown files.
;; https://confluence.atlassian.com/display/DOC/Confluence+Wiki+Markup
;;; Dependencies:
;; jira-markup-mode requires easymenu, a standard package since GNU Emacs
;; 19 and XEmacs 19, which provides a uniform interface for creating
;; menus in GNU Emacs and XEmacs.
;;; Installation:
;; Make sure to place `jira-markup-mode.el` somewhere in the load-path and add
;; the following lines to your `.emacs` file to associate jira-markup-mode
;; with `.text` files:
;;
;; (autoload 'jira-markup-mode "jira-markup-mode"
;; "Major mode for editing JIRA markup files" t)
;; (setq auto-mode-alist
;; (cons '("\\.text" . jira-markup-mode) auto-mode-alist))
;;
;;; Acknowledgments:
;; Jira-markup-mode has benefited greatly from the efforts of the
;; developers of markdown-mode. Markdown-mode was developed and is
;; maintained by Jason R. Blevins <jrblevin@sdf.org>. For details
;; please refer to http://jblevins.org/projects/markdown-mode/
;;; History:
;; jira-markup-mode was written and is maintained by Matthias Nuessler. The
;; first version was released on TODO, 2012.
;;
;; * 2012-07-TODO: Version 0.1.0
;;; Code:
(require 'easymenu)
(require 'outline)
(eval-when-compile (require 'cl))
;;; Constants =================================================================
(defconst jira-markup-mode-version "0.1.0"
"Jira markup mode version number.")
;;; Customizable variables ====================================================
(defvar jira-markup-mode-hook nil
"Hook run when entering jira markup mode.")
(defgroup jira-markup nil
"Major mode for editing text files in Jira markup format."
:prefix "jira-markup"
:group 'wp
:link '(url-link "https://github.com/mnuessler/jira-markup-mode"))
(defcustom jira-markup-hr-string "----"
"String to use for horizonal rules."
:group 'jira-markup
:type 'string)
(defcustom jira-markup-indent-function 'jira-markup-indent-line
"Function to use to indent."
:group 'jira-markup
:type 'function)
(defcustom jira-markup-indent-on-enter t
"Automatically indent new lines when enter key is pressed."
:group 'jira-markup
:type 'boolean)
(defcustom jira-markup-follow-wiki-link-on-enter t
"Follow wiki link at point (if any) when the enter key is pressed."
:group 'jira-markup
:type 'boolean)
(defcustom jira-markup-wiki-link-alias-first t
"When non-nil, treat aliased wiki links like [[alias text|PageName]].
Otherwise, they will be treated as [[PageName|alias text]]."
:group 'jira-markup
:type 'boolean)
(defcustom jira-markup-uri-types
'("acap" "cid" "data" "dav" "fax" "file" "ftp" "gopher" "http" "https"
"imap" "ldap" "mailto" "mid" "modem" "news" "nfs" "nntp" "pop" "prospero"
"rtsp" "service" "sip" "tel" "telnet" "tip" "urn" "vemmi" "wais")
"Link types for syntax highlighting of URIs."
:group 'jira-markup
:type 'list)
(defcustom jira-markup-link-space-sub-char
"_"
"Character to use instead of spaces when mapping wiki links to filenames."
:group 'jira-markup
:type 'string)
;;; Font lock =================================================================
(require 'font-lock)
(defvar jira-markup-italic-face 'jira-markup-italic-face
"Face name to use for italic text.")
(defvar jira-markup-bold-face 'jira-markup-bold-face
"Face name to use for bold text.")
(defvar jira-markup-header-face 'jira-markup-header-face
"Face name to use as a base for headers.")
(defvar jira-markup-header-face-1 'jira-markup-header-face-1
"Face name to use for level-1 headers.")
(defvar jira-markup-header-face-2 'jira-markup-header-face-2
"Face name to use for level-2 headers.")
(defvar jira-markup-header-face-3 'jira-markup-header-face-3
"Face name to use for level-3 headers.")
(defvar jira-markup-header-face-4 'jira-markup-header-face-4
"Face name to use for level-4 headers.")
(defvar jira-markup-header-face-5 'jira-markup-header-face-5
"Face name to use for level-5 headers.")
(defvar jira-markup-header-face-6 'jira-markup-header-face-6
"Face name to use for level-6 headers.")
(defvar jira-markup-inline-code-face 'jira-markup-inline-code-face
"Face name to use for inline code.")
(defvar jira-markup-list-face 'jira-markup-list-face
"Face name to use for list markers.")
(defvar jira-markup-blockquote-face 'jira-markup-blockquote-face
"Face name to use for blockquote.")
(defvar jira-markup-pre-face 'jira-markup-pre-face
"Face name to use for preformatted text.")
(defvar jira-markup-link-face 'jira-markup-link-face
"Face name to use for links.")
(defvar jira-markup-missing-link-face 'jira-markup-missing-link-face
"Face name to use for links where the linked file does not exist.")
(defvar jira-markup-reference-face 'jira-markup-reference-face
"Face name to use for reference.")
(defvar jira-markup-url-face 'jira-markup-url-face
"Face name to use for URLs.")
(defvar jira-markup-link-title-face 'jira-markup-link-title-face
"Face name to use for reference link titles.")
(defgroup jira-markup-faces nil
"Faces used in Jira-Markup Mode"
:group 'jira-markup
:group 'faces)
(defface jira-markup-italic-face
'((t (:inherit font-lock-variable-name-face :slant italic)))
"Face for italic text."
:group 'jira-markup-faces)
(defface jira-markup-bold-face
'((t (:inherit font-lock-variable-name-face :weight bold)))
"Face for bold text."
:group 'jira-markup-faces)
(defface jira-markup-header-face
'((t (:inherit font-lock-function-name-face :weight bold)))
"Base face for headers."
:group 'jira-markup-faces)
(defface jira-markup-header-face-1
'((t (:inherit jira-markup-header-face)))
"Face for level-1 headers."
:group 'jira-markup-faces)
(defface jira-markup-header-face-2
'((t (:inherit jira-markup-header-face)))
"Face for level-2 headers."
:group 'jira-markup-faces)
(defface jira-markup-header-face-3
'((t (:inherit jira-markup-header-face)))
"Face for level-3 headers."
:group 'jira-markup-faces)
(defface jira-markup-header-face-4
'((t (:inherit jira-markup-header-face)))
"Face for level-4 headers."
:group 'jira-markup-faces)
(defface jira-markup-header-face-5
'((t (:inherit jira-markup-header-face)))
"Face for level-5 headers."
:group 'jira-markup-faces)
(defface jira-markup-header-face-6
'((t (:inherit jira-markup-header-face)))
"Face for level-6 headers."
:group 'jira-markup-faces)
(defface jira-markup-inline-code-face
'((t (:inherit font-lock-constant-face)))
"Face for inline code."
:group 'jira-markup-faces)
(defface jira-markup-list-face
'((t (:inherit font-lock-builtin-face)))
"Face for list item markers."
:group 'jira-markup-faces)
(defface jira-markup-blockquote-face
'((t (:inherit font-lock-doc-face)))
"Face for blockquote sections."
:group 'jira-markup-faces)
(defface jira-markup-pre-face
'((t (:inherit font-lock-constant-face)))
"Face for preformatted text."
:group 'jira-markup-faces)
(defface jira-markup-link-face
'((t (:inherit font-lock-keyword-face)))
"Face for links."
:group 'jira-markup-faces)
(defface jira-markup-missing-link-face
'((t (:inherit font-lock-warning-face)))
"Face for missing links."
:group 'jira-markup-faces)
(defface jira-markup-reference-face
'((t (:inherit font-lock-type-face)))
"Face for link references."
:group 'jira-markup-faces)
(defface jira-markup-url-face
'((t (:inherit font-lock-string-face)))
"Face for URLs."
:group 'jira-markup-faces)
(defface jira-markup-link-title-face
'((t (:inherit font-lock-comment-face)))
"Face for reference link titles."
:group 'jira-markup-faces)
;; regular expressions
;; TODO
(defconst jira-markup-regex-link-inline
"\\(!?\\[[^]]*?\\]\\)\\(([^\\)]*)\\)"
"Regular expression for a [text](file) or an image link ![text](file).")
;; TODO
(defconst jira-markup-regex-link-reference
"\\(!?\\[[^]]+?\\]\\)[ ]?\\(\\[[^]]*?\\]\\)"
"Regular expression for a reference link [text][id].")
;; TODO
(defconst jira-markup-regex-reference-definition
"^ \\{0,3\\}\\(\\[.*\\]\\):\\s *\\(.*?\\)\\s *\\( \"[^\"]*\"$\\|$\\)"
"Regular expression for a link id [definition]: ...")
;; TODO
(defconst jira-markup-regex-header
"#+\\|\\S-.*\n\\(?:\\(===+\\)\\|\\(---+\\)\\)$"
"Regexp identifying Jira-Markup headers.")
; headings
(defconst jira-markup-regex-header-1
"^\\s-*h1\\.\\s-+.*$"
"Regular expression for level 1 headers.")
(defconst jira-markup-regex-header-2
"^\\s-*h2\\.\\s-+.*$"
"Regular expression for level 2 headers.")
(defconst jira-markup-regex-header-3
"^\\s-*h3\\.\\s-+.*$"
"Regular expression for level 3 headers.")
(defconst jira-markup-regex-header-4
"^\\s-*h4\\.\\s-+.*$"
"Regular expression for level 4 headers.")
(defconst jira-markup-regex-header-5
"^\\s-*h5\\.\\s-+.*$"
"Regular expression for level 5 headers.")
(defconst jira-markup-regex-header-6
"^\\s-*h6\\.\\s-+.*$"
"Regular expression for level 6 headers.")
(defconst jira-markup-regex-hr
"^\\s-*----$"
"Regular expression for matching Jira-Markup horizontal rules.")
;; TODO
(defconst jira-markup-regex-code
"\\(^\\|[^\\]\\)\\(\\(`\\{1,2\\}\\)\\([^ \\]\\|[^ ]\\(.\\|\n[^\n]\\)*?[^ \\]\\)\\3\\)"
"Regular expression for matching inline code fragments.")
;; TODO
(defconst jira-markup-regex-pre
"^\\( \\|\t\\).*$"
"Regular expression for matching preformatted text sections.")
;; TODO
(defconst jira-markup-regex-list
"^[ \t]*\\([0-9]+\\.\\|[\\*\\+-]\\) "
"Regular expression for matching list markers.")
; text effects
(defconst jira-markup-regex-strong
"\\s-+\\*.*\\*\\s-+"
"Regular expression for matching *strong* text.")
(defconst jira-markup-regex-emphasis
"\\s-+_.*_\\s-+"
"Regular expression for matching _emphasized_ text.")
; new
(defconst jira-markup-regex-citation
"\\s-+\\?\\?.*\\s-+"
"Regular expression for matching ??cited?? text.")
; new
(defconst jira-markup-regex-deleted
"\\s-+-.*-\\s-"
"Regular expression for matching -deleted- text.")
; new
(defconst jira-markup-regex-inserted
"\\s-+\\+.*\\+\\s-+"
"Regular expression for matching +inserted+ text.")
; new
(defconst jira-markup-regex-superscript
"\\s-+^.*^\\s-+"
"Regular expression for matching ^superscript^ text.")
; new
(defconst jira-markup-regex-subscript
"\\s-+~.*~\\s-+"
"Regular expression for matching ~subscript~ text.")
; new
(defconst jira-markup-regex-monospaced
"\\s-+{{.*}}\\s-+"
"Regular expression for matching {{monospaced}} text.")
(defconst jira-markup-regex-blockquote
"^[ \t]*bq\\."
"Regular expression for matching blockquote lines.")
;; TODO
(defconst jira-markup-regex-line-break
" $"
"Regular expression for matching line breaks.")
;; TODO
(defconst jira-markup-regex-wiki-link
"\\[\\[\\([^]|]+\\)\\(|\\([^]]+\\)\\)?\\]\\]"
"Regular expression for matching wiki links.
This matches typical bracketed [[WikiLinks]] as well as 'aliased'
wiki links of the form [[PageName|link text]]. In this regular
expression, #1 matches the page name and #3 matches the link
text.")
(defconst jira-markup-regex-uri
(concat
"\\(" (mapconcat 'identity jira-markup-uri-types "\\|")
"\\):[^]\t\n\r<>,;() ]+")
"Regular expression for matching inline URIs.")
(defconst jira-markup-regex-angle-uri
(concat
"\\(<\\)\\("
(mapconcat 'identity jira-markup-uri-types "\\|")
"\\):[^]\t\n\r<>,;()]+\\(>\\)")
"Regular expression for matching inline URIs in angle brackets.")
(defconst jira-markup-regex-email
"<\\(\\sw\\|\\s_\\|\\s.\\)+@\\(\\sw\\|\\s_\\|\\s.\\)+>"
"Regular expression for matching inline email addresses.")
(defconst jira-markup-regex-list-indent
"^\\(\\s *\\)\\([0-9]+\\.\\|[\\*\\+-]\\)\\(\\s +\\)"
"Regular expression for matching indentation of list items.")
(defvar jira-markup-mode-font-lock-keywords-basic
(list
'(jira-markup-match-pre-blocks 0 jira-markup-pre-face t t)
'(jira-markup-match-fenced-code-blocks 0 jira-markup-pre-face t t)
(cons jira-markup-regex-blockquote 'jira-markup-blockquote-face)
(cons jira-markup-regex-header-1 'jira-markup-header-face-1)
(cons jira-markup-regex-header-2 'jira-markup-header-face-2)
(cons jira-markup-regex-header-3 'jira-markup-header-face-3)
(cons jira-markup-regex-header-4 'jira-markup-header-face-4)
(cons jira-markup-regex-header-5 'jira-markup-header-face-5)
(cons jira-markup-regex-header-6 'jira-markup-header-face-6)
(cons jira-markup-regex-hr 'jira-markup-header-face)
(cons jira-markup-regex-code '(2 jira-markup-inline-code-face))
(cons jira-markup-regex-angle-uri 'jira-markup-link-face)
(cons jira-markup-regex-uri 'jira-markup-link-face)
(cons jira-markup-regex-email 'jira-markup-link-face)
(cons jira-markup-regex-list 'jira-markup-list-face)
(cons jira-markup-regex-link-inline
'((1 jira-markup-link-face t)
(2 jira-markup-url-face t)))
(cons jira-markup-regex-link-reference
'((1 jira-markup-link-face t)
(2 jira-markup-reference-face t)))
(cons jira-markup-regex-reference-definition
'((1 jira-markup-reference-face t)
(2 jira-markup-url-face t)
(3 jira-markup-link-title-face t)))
(cons jira-markup-regex-strong '(2 jira-markup-bold-face))
(cons jira-markup-regex-emphasis '(2 jira-markup-italic-face))
)
"Syntax highlighting for Jira-Markup files.")
(defvar jira-markup-mode-font-lock-keywords
(append
jira-markup-mode-font-lock-keywords-basic)
"Default highlighting expressions for Jira-Markup mode.")
;;; Jira-Markup parsing functions ================================================
(defun jira-markup-cur-line-blank-p ()
"Return t if the current line is blank and nil otherwise."
(save-excursion
(beginning-of-line)
(re-search-forward "^\\s *$" (point-at-eol) t)))
(defun jira-markup-prev-line-blank-p ()
"Return t if the previous line is blank and nil otherwise.
If we are at the first line, then consider the previous line to be blank."
(save-excursion
(if (= (point-at-bol) (point-min))
t
(forward-line -1)
(jira-markup-cur-line-blank-p))))
(defun jira-markup-next-line-blank-p ()
"Return t if the next line is blank and nil otherwise.
If we are at the last line, then consider the next line to be blank."
(save-excursion
(if (= (point-at-bol) (point-max))
t
(forward-line 1)
(jira-markup-cur-line-blank-p))))
(defun jira-markup-prev-line-indent-p ()
"Return t if the previous line is indented and nil otherwise."
(save-excursion
(forward-line -1)
(goto-char (point-at-bol))
(if (re-search-forward "^\\s " (point-at-eol) t) t)))
(defun jira-markup-cur-line-indent ()
"Return the number of leading whitespace characters in the current line."
(save-excursion
(goto-char (point-at-bol))
(re-search-forward "^\\s +" (point-at-eol) t)
(current-column)))
(defun jira-markup-prev-line-indent ()
"Return the number of leading whitespace characters in the previous line."
(save-excursion
(forward-line -1)
(jira-markup-cur-line-indent)))
(defun jira-markup-next-line-indent ()
"Return the number of leading whitespace characters in the next line."
(save-excursion
(forward-line 1)
(jira-markup-cur-line-indent)))
(defun jira-markup-cur-non-list-indent ()
"Return the number of leading whitespace characters in the current line."
(save-excursion
(beginning-of-line)
(when (re-search-forward jira-markup-regex-list-indent (point-at-eol) t)
(current-column))))
(defun jira-markup-prev-non-list-indent ()
"Return position of the first non-list-marker on the previous line."
(save-excursion
(forward-line -1)
(jira-markup-cur-non-list-indent)))
(defun jira-markup--next-block ()
"Move the point to the start of the next text block."
(forward-line)
(while (and (or (not (jira-markup-prev-line-blank-p))
(jira-markup-cur-line-blank-p))
(not (eobp)))
(forward-line)))
(defun jira-markup--end-of-level (level)
"Move the point to the end of region with indentation at least LEVEL."
(let (indent)
(while (and (not (< (setq indent (jira-markup-cur-line-indent)) level))
(not (>= indent (+ level 4)))
(not (eobp)))
(jira-markup--next-block))
(unless (eobp)
;; Move back before any trailing blank lines
(while (and (jira-markup-prev-line-blank-p)
(not (bobp)))
(forward-line -1))
(forward-line -1)
(end-of-line))))
(defun jira-markup-match-pre-blocks (last)
"Match Jira-Markup pre blocks from point to LAST.
A region matches as if it is indented at least four spaces
relative to the nearest previous block of lesser non-list-marker
indentation."
(let (cur-begin cur-end cur-indent prev-indent prev-list stop match found)
;; Don't start in the middle of a block
(unless (and (bolp)
(jira-markup-prev-line-blank-p)
(not (jira-markup-cur-line-blank-p)))
(jira-markup--next-block))
;; Move to the first full block in the region with indent 4 or more
(while (and (not (>= (setq cur-indent (jira-markup-cur-line-indent)) 4))
(not (>= (point) last)))
(jira-markup--next-block))
(setq cur-begin (point))
(jira-markup--end-of-level cur-indent)
(setq cur-end (point))
(setq match nil)
(setq stop (> cur-begin cur-end))
(while (and (<= cur-end last) (not stop) (not match))
;; Move to the nearest preceding block of lesser (non-marker) indentation
(setq prev-indent (+ cur-indent 1))
(goto-char cur-begin)
(setq found nil)
(while (and (>= prev-indent cur-indent)
(not (and prev-list
(eq prev-indent cur-indent)))
(not (bobp)))
;; Move point to the last line of the previous block.
(forward-line -1)
(while (and (jira-markup-cur-line-blank-p)
(not (bobp)))
(forward-line -1))
;; Update the indentation level using either the
;; non-list-marker indentation, if the previous line is the
;; start of a list, or the actual indentation.
(setq prev-list (jira-markup-cur-non-list-indent))
(setq prev-indent (or prev-list
(jira-markup-cur-line-indent)))
(setq found t))
;; If the loop didn't execute
(unless found
(setq prev-indent 0))
;; Compare with prev-indent minus its remainder mod 4
(setq prev-indent (- prev-indent (mod prev-indent 4)))
;; Set match data and return t if we have a match
(if (>= cur-indent (+ prev-indent 4))
;; Match
(progn
(setq match t)
(set-match-data (list cur-begin cur-end))
;; Leave point at end of block
(goto-char cur-end)
(forward-line))
;; Move to the next block (if possible)
(goto-char cur-end)
(jira-markup--next-block)
(setq cur-begin (point))
(setq cur-indent (jira-markup-cur-line-indent))
(jira-markup--end-of-level cur-indent)
(setq cur-end (point))
(setq stop (equal cur-begin cur-end))))
match))
(defun jira-markup-match-fenced-code-blocks (last)
"Match fenced code blocks from the point to LAST."
(cond ((search-forward-regexp "^\\([~]\\{3,\\}\\)" last t)
(beginning-of-line)
(let ((beg (point)))
(forward-line)
(cond ((search-forward-regexp
(concat "^" (match-string 1) "~*") last t)
(set-match-data (list beg (point)))
t)
(t nil))))
(t nil)))
(defun jira-markup-font-lock-extend-region ()
"Extend the search region to include an entire block of text.
This helps improve font locking for block constructs such as pre blocks."
;; Avoid compiler warnings about these global variables from font-lock.el.
;; See the documentation for variable `font-lock-extend-region-functions'.
(eval-when-compile (defvar font-lock-beg) (defvar font-lock-end))
(save-excursion
(goto-char font-lock-beg)
(let ((found (re-search-backward "\n\n" nil t)))
(when found
(goto-char font-lock-end)
(when (re-search-forward "\n\n" nil t)
(beginning-of-line)
(setq font-lock-end (point)))
(setq font-lock-beg found)))))
;;; Syntax Table ==============================================================
(defvar jira-markup-mode-syntax-table
(let ((jira-markup-mode-syntax-table (make-syntax-table)))
(modify-syntax-entry ?\" "w" jira-markup-mode-syntax-table)
jira-markup-mode-syntax-table)
"Syntax table for `jira-markup-mode'.")
;;; Element Insertion =========================================================
(defun jira-markup-wrap-or-insert (s1 s2)
"Insert the strings S1 and S2.
If Transient Mark mode is on and a region is active, wrap the strings S1
and S2 around the region."
(if (and transient-mark-mode mark-active)
(let ((a (region-beginning)) (b (region-end)))
(goto-char a)
(insert s1)
(goto-char (+ b (length s1)))
(insert s2))
(insert s1 s2)))
(defun jira-markup-insert-hr ()
"Insert a horizonal rule using `jira-markup-hr-string'."
(interactive)
;; Leading blank line
(when (and (>= (point) (+ (point-min) 2))
(not (looking-back "\n\n" 2)))
(insert "\n"))
;; Insert custom HR string
(insert (concat jira-markup-hr-string "\n"))
;; Following blank line
(backward-char)
(unless (looking-at "\n\n")
(insert "\n")))
(defun jira-markup-insert-bold ()
"Insert markup for a bold word or phrase.
If Transient Mark mode is on and a region is active, it is made bold."
(interactive)
(jira-markup-wrap-or-insert "*" "*")
(backward-char 1))
(defun jira-markup-insert-italic ()
"Insert markup for an italic word or phrase.
If Transient Mark mode is on and a region is active, it is made italic."
(interactive)
(jira-markup-wrap-or-insert "_" "_")
(backward-char 1))
(defun jira-markup-insert-code ()
"Insert markup for an inline code fragment.
If Transient Mark mode is on and a region is active, it is marked
as inline code."
(interactive)
(jira-markup-wrap-or-insert "{{" "}}")
(backward-char 2))
(defun jira-markup-insert-link ()
"Insert an inline link of the form []().
If Transient Mark mode is on and a region is active, it is used
as the link text."
(interactive)
(jira-markup-wrap-or-insert "[" "|")
(insert "]")
(backward-char 1))
(defun jira-markup-insert-reference-link-region (url label title)
"Insert a reference link at point using the region as the link text."
(interactive "sLink URL: \nsLink Label (optional): \nsLink Title (optional): ")
(let ((text (buffer-substring (region-beginning) (region-end))))
(delete-region (region-beginning) (region-end))
(jira-markup-insert-reference-link text url label title)))
(defun jira-markup-insert-reference-link (text url label title)
"Insert a reference link at point.
The link label definition is placed at the end of the current
paragraph."
(interactive "sLink Text: \nsLink URL: \nsLink Label (optional): \nsLink Title (optional): ")
(let (end)
(insert (concat "[" text "][" label "]"))
(setq end (point))
(forward-paragraph)
(insert "\n[")
(if (> (length label) 0)
(insert label)
(insert text))
(insert (concat "]: " url))
(unless (> (length url) 0)
(setq end (point)))
(when (> (length title) 0)
(insert (concat " \"" title "\"")))
(insert "\n")
(unless (looking-at "\n")
(insert "\n"))
(goto-char end)))
(defun jira-markup-insert-wiki-link ()
"Insert a wiki link of the form [[WikiLink]].
If Transient Mark mode is on and a region is active, it is used
as the link text."
(interactive)
(jira-markup-wrap-or-insert "[[" "]]")
(backward-char 2))
(defun jira-markup-insert-image ()
"Insert an inline image tag of the form ![]().
If Transient Mark mode is on and a region is active, it is used
as the alt text of the image."
(interactive)
(jira-markup-wrap-or-insert "![" "]")
(insert "()")
(backward-char 1))
(defun jira-markup-insert-header-1 ()
"Insert a first level atx-style (hash mark) header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
(interactive)
(jira-markup-insert-header 1))
(defun jira-markup-insert-header-2 ()
"Insert a second level atx-style (hash mark) header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
(interactive)
(jira-markup-insert-header 2))
(defun jira-markup-insert-header-3 ()
"Insert a third level atx-style (hash mark) header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
(interactive)
(jira-markup-insert-header 3))
(defun jira-markup-insert-header-4 ()
"Insert a fourth level atx-style (hash mark) header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
(interactive)
(jira-markup-insert-header 4))
(defun jira-markup-insert-header-5 ()
"Insert a fifth level atx-style (hash mark) header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
(interactive)
(jira-markup-insert-header 5))
(defun jira-markup-insert-header-6 ()
"Insert a sixth level atx-style (hash mark) header.
If Transient Mark mode is on and a region is active, it is used
as the header text."
(interactive)
(jira-markup-insert-header 6))
(defun jira-markup-insert-header (n)
"Insert a header.
With no prefix argument, insert a level-1 header. With prefix N,
insert a level-N header. If Transient Mark mode is on and the
region is active, it is used as the header text."
(interactive "p")
(unless n ; Test to see if n is defined
(setq n 1)) ; Default to level 1 header
(jira-markup-wrap-or-insert (concat "h" (int-to-string n) ". ") ""))
(defun jira-markup-insert-blockquote ()
"Start a blockquote section (or blockquote the region).
If Transient Mark mode is on and a region is active, it is used as
the blockquote text."
(interactive)
(if (and (boundp 'transient-mark-mode) transient-mark-mode mark-active)
(jira-markup-blockquote-region (region-beginning) (region-end))
(insert "bq. ")))
(defun jira-markup-block-region (beg end prefix)
"Format the region using a block prefix.
Arguments BEG and END specify the beginning and end of the
region. The characters PREFIX will appear at the beginning
of each line."
(if mark-active
(save-excursion
;; Ensure that there is a leading blank line
(goto-char beg)
(when (and (>= (point) (+ (point-min) 2))
(not (looking-back "\n\n" 2)))
(insert "\n")
(setq beg (1+ beg))
(setq end (1+ end)))
;; Move back before any blank lines at the end
(goto-char end)
(while (and (looking-back "\n" 1)
(not (equal (point) (point-min))))
(backward-char)
(setq end (1- end)))
;; Ensure that there is a trailing blank line
(goto-char end)
(if (not (or (looking-at "\n\n")
(and (equal (1+ end) (point-max)) (looking-at "\n"))))
(insert "\n"))
;; Insert PREFIX
(goto-char beg)
(beginning-of-line)
(while (< (point-at-bol) end)
(insert prefix)
(setq end (+ (length prefix) end))
(forward-line)))))
(defun jira-markup-blockquote-region (beg end)
"Blockquote the region.
Arguments BEG and END specify the beginning and end of the region."
(interactive "*r")
(jira-markup-block-region beg end "> "))
(defun jira-markup-insert-pre ()
"Start a preformatted section (or apply to the region).
If Transient Mark mode is on and a region is active, it is marked
as preformatted text."
(interactive)
(if (and (boundp 'transient-mark-mode) transient-mark-mode mark-active)
(jira-markup-pre-region (region-beginning) (region-end))
(insert " ")))
(defun jira-markup-pre-region (beg end)
"Format the region as preformatted text.
Arguments BEG and END specify the beginning and end of the region."
(interactive "*r")
(jira-markup-block-region beg end " "))
;;; Indentation ====================================================================
(defun jira-markup-indent-find-next-position (cur-pos positions)
"Return the position after the index of CUR-POS in POSITIONS."
(while (and positions
(not (equal cur-pos (car positions))))
(setq positions (cdr positions)))
(or (cadr positions) 0))
(defun jira-markup-indent-line ()
"Indent the current line using some heuristics.
If the _previous_ command was either `jira-markup-enter-key' or
`jira-markup-cycle', then we should cycle to the next
reasonable indentation position. Otherwise, we could have been
called directly by `jira-markup-enter-key', by an initial call of
`jira-markup-cycle', or indirectly by `auto-fill-mode'. In
these cases, indent to the default position."
(interactive)
(let ((positions (jira-markup-calc-indents))
(cur-pos (current-column)))
(if (not (equal this-command 'jira-markup-cycle))
(indent-line-to (car positions))
(setq positions (sort (delete-dups positions) '<))
(indent-line-to
(jira-markup-indent-find-next-position cur-pos positions)))))
(defun jira-markup-calc-indents ()
"Return a list of indentation columns to cycle through.
The first element in the returned list should be considered the
default indentation level."
(let (pos prev-line-pos positions)
;; Previous line indent
(setq prev-line-pos (jira-markup-prev-line-indent))
(setq positions (cons prev-line-pos positions))
;; Previous non-list-marker indent
(setq pos (jira-markup-prev-non-list-indent))
(when pos
(setq positions (cons pos positions))
(setq positions (cons (+ pos tab-width) positions)))
;; Indentation of the previous line + tab-width
(cond
(prev-line-pos
(setq positions (cons (+ prev-line-pos tab-width) positions)))
(t
(setq positions (cons tab-width positions))))
;; Indentation of the previous line - tab-width
(if (and prev-line-pos
(> prev-line-pos tab-width))
(setq positions (cons (- prev-line-pos tab-width) positions)))
;; Indentation of preceeding list item
(setq pos
(save-excursion
(forward-line -1)
(catch 'break
(while (not (equal (point) (point-min)))
(forward-line -1)
(goto-char (point-at-bol))
(when (re-search-forward jira-markup-regex-list-indent (point-at-eol) t)
(throw 'break (length (match-string 1)))))
nil)))
(if (and pos (not (eq pos prev-line-pos)))
(setq positions (cons pos positions)))
;; First column
(setq positions (cons 0 positions))
(reverse positions)))
(defun jira-markup-do-normal-return ()
"Insert a newline and optionally indent the next line."
(newline)
(if jira-markup-indent-on-enter
(funcall indent-line-function)))
(defun jira-markup-enter-key ()
"Handle RET according to context.
If there is a wiki link at the point, follow it unless
`jira-markup-follow-wiki-link-on-enter' is nil. Otherwise, process
it in the usual way."
(interactive)
(if (and jira-markup-follow-wiki-link-on-enter (jira-markup-wiki-link-p))
(jira-markup-follow-wiki-link-at-point)
(jira-markup-do-normal-return)))
;;; Keymap ====================================================================
(defvar jira-markup-mode-map
(let ((map (make-keymap)))
;; Element insertion
(define-key map "\C-c\C-al" 'jira-markup-insert-link)
(define-key map "\C-c\C-ar" 'jira-markup-insert-reference-link-dwim)
(define-key map "\C-c\C-aw" 'jira-markup-insert-wiki-link)
(define-key map "\C-c\C-ii" 'jira-markup-insert-image)
(define-key map "\C-c\C-t1" 'jira-markup-insert-header-1)
(define-key map "\C-c\C-t2" 'jira-markup-insert-header-2)
(define-key map "\C-c\C-t3" 'jira-markup-insert-header-3)
(define-key map "\C-c\C-t4" 'jira-markup-insert-header-4)
(define-key map "\C-c\C-t5" 'jira-markup-insert-header-5)
(define-key map "\C-c\C-t6" 'jira-markup-insert-header-6)
(define-key map "\C-c\C-pb" 'jira-markup-insert-bold)
(define-key map "\C-c\C-ss" 'jira-markup-insert-bold)
(define-key map "\C-c\C-pi" 'jira-markup-insert-italic)
(define-key map "\C-c\C-se" 'jira-markup-insert-italic)
(define-key map "\C-c\C-pf" 'jira-markup-insert-code)
(define-key map "\C-c\C-sc" 'jira-markup-insert-code)
(define-key map "\C-c\C-sb" 'jira-markup-insert-blockquote)
(define-key map "\C-c\C-s\C-b" 'jira-markup-blockquote-region)