-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown-mode.el
2324 lines (2034 loc) · 86.3 KB
/
markdown-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
;;; markdown-mode.el --- Emacs Major mode for Markdown-formatted text files
;; Copyright (C) 2007-2011 Jason R. Blevins <[email protected]>
;; Copyright (C) 2007, 2009 Edward O'Connor <[email protected]>
;; Copyright (C) 2007 Conal Elliott <[email protected]>
;; Copyright (C) 2008 Greg Bognar <[email protected]>
;; Copyright (C) 2008 Dmitry Dzhus <[email protected]>
;; Copyright (C) 2008 Bryan Kyle <[email protected]>
;; Copyright (C) 2008 Ben Voui <[email protected]>
;; Copyright (C) 2009 Ankit Solanki <[email protected]>
;; Copyright (C) 2009 Hilko Bengen <[email protected]>
;; Copyright (C) 2009 Peter Williams <[email protected]>
;; Copyright (C) 2010 George Ogata <[email protected]>
;; Copyright (C) 2011 Eric Merritt <[email protected]>
;; Copyright (C) 2011 Philippe Ivaldi <[email protected]>
;; Copyright (C) 2011 Jeremiah Dodds <[email protected]>
;; Copyright (C) 2011 Christopher J. Madsen <[email protected]>
;; Copyright (C) 2011 Shigeru Fukaya <[email protected]>
;; Copyright (C) 2011 Joost Kremers <[email protected]>
;; Author: Jason R. Blevins <[email protected]>
;; Maintainer: Jason R. Blevins <[email protected]>
;; Created: May 24, 2007
;; Version: 1.8.1
;; Keywords: Markdown, GitHub Flavored Markdown, itex
;; URL: http://jblevins.org/projects/markdown-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:
;; markdown-mode is a major mode for editing [Markdown][]-formatted
;; text files in GNU Emacs. markdown-mode is free software, licensed
;; under the GNU GPL.
;;
;; [Markdown]: http://daringfireball.net/projects/markdown/
;;
;; The latest stable version is markdown-mode 1.8.1, released on August 15, 2011:
;;
;; * [markdown-mode.el][]
;; * [Screenshot][]
;; * [Release notes][]
;;
;; [markdown-mode.el]: http://jblevins.org/projects/markdown-mode/markdown-mode.el
;; [screenshot]: http://jblevins.org/projects/markdown-mode/screenshots/20110812-001.png
;; [release notes]: http://jblevins.org/projects/markdown-mode/rev-1-8-1
;;
;; markdown-mode is also available in several package managers, including:
;;
;; * Debian and Ubuntu Linux: [emacs-goodies-el][]
;; * RedHat and Fedora Linux: [emacs-goodies][]
;; * OpenBSD: [textproc/markdown-mode][]
;; * Arch Linux (AUR): [emacs-markdown-mode-git][]
;;
;; [emacs-goodies-el]: http://packages.debian.org/emacs-goodies-el
;; [emacs-goodies]: https://admin.fedoraproject.org/pkgdb/acls/name/emacs-goodies
;; [textproc/markdown-mode]: http://pkgsrc.se/textproc/markdown-mode
;; [emacs-markdown-mode-git]: http://aur.archlinux.org/packages.php?ID=30389
;;
;; The latest development version can be downloaded directly
;; ([markdown-mode.el][devel.el]) or it can be obtained from the
;; (browsable and clonable) Git repository at
;; <http://jblevins.org/git/markdown-mode.git>. The entire repository,
;; including the full project history, can be cloned via the Git protocol
;; by running
;;
;; git clone git://jblevins.org/git/markdown-mode.git
;;
;; [devel.el]: http://jblevins.org/git/markdown-mode.git/plain/markdown-mode.el
;;; Dependencies:
;; markdown-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 `markdown-mode.el` somewhere in the load-path and add
;; the following lines to your `.emacs` file to associate markdown-mode
;; with `.text` files:
;;
;; (autoload 'markdown-mode "markdown-mode"
;; "Major mode for editing Markdown files" t)
;; (setq auto-mode-alist
;; (cons '("\\.text" . markdown-mode) auto-mode-alist))
;;
;; There is no consensus on an official file extension so change `.text` to
;; `.mdwn`, `.md`, `.mdt`, or whatever you call your markdown files.
;;; Customization:
;; Although no configuration is *necessary* there are a few things
;; that can be customized. The `M-x customize-mode` command
;; provides an interface to all of the possible customizations:
;;
;; * `markdown-command' - the command used to run Markdown (default:
;; `markdown'). This variable may be customized to pass
;; command-line options to your Markdown processor of choice, but
;; this command must accept input from `stdin`. If it does not, a
;; simple wrapper script can be used to write `stdin` to a file
;; and then pass that file to your Markdown interpreter. Ideally,
;; this command will produce an XHTML fragment around which
;; markdown-mode will wrap a header and footer (which can be
;; further customized). However, it attempts to detect whether
;; the command produces standalone XHTML output (via
;; `markdown-xhtml-standalone-regexp'), in which case no header
;; and footer content will be added.
;;
;; * `markdown-command-needs-filename' - set to non-nil if
;; `markdown-command' does not accept input from stdin (default: nil).
;; Instead, it will be passed a filename as the final command-line
;; option. As a result, you will only be able to run Markdown
;; from buffers which are visiting a file.
;;
;; * `markdown-hr-string' - string to use when inserting horizontal
;; rules (default: `* * * * *').
;;
;; * `markdown-bold-underscore' - set to a non-nil value to use two
;; underscores for bold instead of two asterisks (default: `nil').
;;
;; * `markdown-italic-underscore' - set to a non-nil value to use
;; underscores for italic instead of asterisks (default: `nil').
;;
;; * `markdown-indent-function' - the function to use for automatic
;; indentation (default: `markdown-indent-line').
;;
;; * `markdown-indent-on-enter' - set to a non-nil value to
;; automatically indent new lines when the enter key is pressed
;; (default: `t')
;;
;; * `markdown-follow-wiki-link-on-enter' - set to a non-nil value
;; to automatically open a linked document in a new buffer if the
;; cursor is an wiki link
;; (default: `t')
;;
;; * `markdown-wiki-link-alias-first' - set to a non-nil value to
;; treat aliased wiki links like `[[link text|PageName]]`.
;; When set to nil, they will be treated as `[[PageName|link text]]'.
;;
;; * `markdown-uri-types' - a list of protocols for URIs that
;; `markdown-mode' should highlight.
;;
;; * `markdown-enable-math' - syntax highlighting for
;; LaTeX fragments (default: `nil').
;;
;; * `markdown-css-path' - CSS file to link to in XHTML output.
;;
;; * `markdown-xhtml-header-content' - additional content to include
;; in the XHTML `<head>` block.
;;
;; * `markdown-xhtml-standalone-regexp' - a regular expression which
;; indicates whether the output of `markdown-command' is standalone
;; XHTML (default: `^\\(\<\?xml\\|\<!DOCTYPE\\|\<html\\)`). If
;; this is not matched, we assume this output is a fragment and add
;; our own header and footer.
;;
;; * `markdown-link-space-sub-char' - a character to replace spaces
;; when mapping wiki links to filenames (default: `_`).
;; For example, use an underscore for compatibility with the
;; Python Markdown WikiLinks extension or a hyphen for compatibility
;; with Github wiki links.
;;
;; Additionally, the faces used for syntax highlighting can be modified to
;; your liking by issuing `M-x customize-group RET markdown-faces`
;; or by using the "Markdown Faces" link at the bottom of the mode
;; customization screen.
;;; Usage:
;; Keybindings are grouped by prefixes based on their function. For
;; example, commands dealing with headers begin with `C-c C-t`. The
;; primary commands in each group will are described below. You can
;; obtain a list of all keybindings by pressing `C-c C-h`.
;;
;; * Anchors: `C-c C-a`
;;
;; `C-c C-a l` inserts inline links of the form `[text](url)`.
;; `C-c C-a r` inserts reference links of the form `[text][label]`.
;; The label definition will be placed at the end of the current
;; block. `C-c C-a w` acts similarly for wiki links of the form
;; `[[WikiLink]]`. In all cases, if there is an active region, the
;; text in the region is used as the link text.
;;
;; * Commands: `C-c C-c`
;;
;; `C-c C-c m` will run Markdown on the current buffer and preview
;; the output in another buffer while `C-c C-c p` runs Markdown on
;; the current buffer and previews the output in a browser.
;; `C-c C-c e` will run Markdown on the current buffer and save
;; the result in the file `basename.html`, where `basename` is the
;; name of the Markdown file with the extension removed. **This
;; file will be overwritten without notice.** Press `C-c C-c v`
;; to view the exported file in a browser.
;;
;; `C-c C-c c` will check for undefined references. If there are
;; any, a small buffer will open with a list of undefined
;; references and the line numbers on which they appear. In Emacs
;; 22 and greater, selecting a reference from this list and
;; pressing `RET` will insert an empty reference definition at the
;; end of the buffer. Similarly, selecting the line number will
;; jump to the corresponding line.
;;
;; * Images: `C-c C-i`
;;
;; `C-c C-i i` inserts an image, using the active region (if any)
;; as the alt text.
;;
;; * Physical styles: `C-c C-p`
;;
;; These commands all act on text in the active region, if any,
;; and insert empty markup fragments otherwise. `C-c C-p b` makes
;; the selected text bold, `C-c C-p f` formats the region as
;; fixed-width text, and `C-c C-p i` is used for italic text.
;;
;; * Logical styles: `C-c C-s`
;;
;; These commands all act on text in the active region, if any,
;; and insert empty markup fragments otherwise. Logical styles
;; include blockquote (`C-c C-s b`), preformatted (`C-c C-s p`),
;; code (`C-c C-s c`), emphasis (`C-c C-s e`), and strong
;; (`C-c C-s s`).
;;
;; * Headers: `C-c C-t`
;;
;; All header commands use text in the active region, if any, as
;; the header text. To insert an atx or hash style level-n
;; header, press `C-c C-t n` where n is between 1 and 6. For a
;; top-level setext or underline style header press `C-c C-t t`
;; (mnemonic: title) and for a second-level underline-style header
;; press `C-c C-t s` (mnemonic: section).
;;
;; * Footnotes: `C-c C-f`
;;
;; To create a new footnote at the point, press `C-c C-f n`.
;; Press `C-c C-f g` with the point at a footnote to jump to the
;; location where the footnote text is defined. Then, press
;; `C-c C-f b` to return to the footnote marker in the main text.
;;
;; * Other elements:
;;
;; `C-c -` inserts a horizontal rule.
;;
;; * Wiki-Link Navigation:
;;
;; Use `M-p` and `M-n` to quickly jump to the previous and next
;; wiki links, respectively.
;;
;; * Outline Navigation:
;;
;; Navigation between headings is possible using `outline-mode'.
;; Use `C-M-n` and `C-M-p` to move between the next and previous
;; visible headings. Similarly, `C-M-f` and `C-M-b` move to the
;; next and previous visible headings at the same level as the one
;; at the point. Finally, `C-M-u` will move up to a lower-level
;; (more inclusive) visible heading.
;;
;; Many of the commands described above behave differently depending on
;; whether Transient Mark mode is enabled or not. When it makes sense,
;; if Transient Mark mode is on and a region is active, the command
;; applies to the text in the region (e.g., `C-c C-p b` makes the region
;; bold). For users who prefer to work outside of Transient Mark mode,
;; in Emacs 22 it can be enabled temporarily by pressing `C-SPC C-SPC`.
;;
;; When applicable, commands that specifically act on the region even
;; outside of Transient Mark mode have the same keybinding as the with
;; the exception of an additional `C-` prefix. For example,
;; `markdown-insert-blockquote' is bound to `C-c C-s b` and only acts on
;; the region in Transient Mark mode while `markdown-blockquote-region'
;; is bound to `C-c C-s C-b` and always applies to the region (when
;; nonempty).
;;
;; markdown-mode attempts to be flexible in how it handles
;; indentation. When you press `TAB` repeatedly, the point will cycle
;; through several possible indentation levels corresponding to things
;; you might have in mind when you press `RET` at the end of a line or
;; `TAB`. For example, you may want to start a new list item,
;; continue a list item with hanging indentation, indent for a nested
;; pre block, and so on.
;;
;; markdown-mode supports outline-minor-mode as well as org-mode-style
;; visibility cycling for atx- or hash-style headers. There are two
;; types of visibility cycling: Pressing `S-TAB` cycles globally between
;; the table of contents view (headers only), outline view (top-level
;; headers only), and the full document view. Pressing `TAB` while the
;; point is at a header will cycle through levels of visibility for the
;; subtree: completely folded, visible children, and fully visible.
;; Note that mixing hash and underline style headers will give undesired
;; results.
;;; Extensions:
;; Besides supporting the basic Markdown syntax, markdown-mode also
;; includes syntax highlighting for `[[Wiki Links]]` by default. Wiki
;; links may be followed automatically by hitting the enter key when
;; your curser is on a wiki link or by hitting `C-c C-w`. The
;; autofollowing on enter key may be controlled with the
;; `markdown-follow-wiki-link-on-enter' customization. Use `M-p` and
;; `M-n` to quickly jump to the previous and next wiki links,
;; respectively. Aliased or piped wiki links of the form
;; `[[link text|PageName]]` are also supported. Since some wikis
;; reverse these components, set `markdown-wiki-link-alias-first'
;; to nil to treat them as `[[PageName|link text]]`.
;;
;; [SmartyPants][] support is possible by customizing `markdown-command'.
;; If you install `SmartyPants.pl` at, say, `/usr/local/bin/smartypants`,
;; then you can set `markdown-command' to `"markdown | smartypants"`.
;; You can do this either by using `M-x customize-group markdown`
;; or by placing the following in your `.emacs` file:
;;
;; (defun markdown-custom ()
;; "markdown-mode-hook"
;; (setq markdown-command "markdown | smartypants"))
;; (add-hook 'markdown-mode-hook '(lambda() (markdown-custom)))
;;
;; [SmartyPants]: http://daringfireball.net/projects/smartypants/
;;
;; Experimental syntax highlighting for mathematical expressions written
;; in LaTeX (only expressions denoted by `$..$`, `$$..$$`, or `\[..\]`)
;; can be enabled by setting `markdown-enable-math' to a non-nil value,
;; either via customize or by placing `(setq markdown-enable-itex t)`
;; in `.emacs`, and restarting Emacs.
;;
;; A [GitHub Flavored Markdown](http://github.github.com/github-flavored-markdown/)
;; mode, `gfm-mode', is also available. The GitHub implementation of
;; differs slightly from standard Markdown. Most importantly, newlines are
;; significant and trigger hard line breaks. As such, `gfm-mode' turns off
;; `auto-fill-mode' and turns on `visual-line-mode' (or `longlines-mode' if
;; `visual-line-mode' is not available). Wiki links in this mode will be
;; treated as on GitHub, with hyphens replacing spaces in filenames and
;; where the first letter of the filename capitalized. For example,
;; `[[wiki link]]' will map to a file named `Wiki-link` with the same
;; extension as the current file.
;;; Acknowledgments:
;; markdown-mode has benefited greatly from the efforts of the
;; following people:
;;
;; * Cyril Brulebois <[email protected]> for Debian packaging.
;; * Conal Elliott <[email protected]> for a font-lock regexp patch.
;; * Edward O'Connor <[email protected]> for a font-lock regexp fix and
;; GitHub Flavored Markdown mode (`gfm-mode').
;; * Greg Bognar <[email protected]> for menus and running
;; `markdown' with an active region.
;; * Daniel Burrows <[email protected]> for filing Debian bug #456592.
;; * Peter S. Galbraith <[email protected]> for maintaining emacs-goodies-el.
;; * Dmitry Dzhus <[email protected]> for reference checking functions.
;; * Bryan Kyle <[email protected]> for indentation code.
;; * Ben Voui <[email protected]> for font-lock face customizations.
;; * Ankit Solanki <[email protected]> for longlines.el
;; compatibility and custom CSS.
;; * Hilko Bengen <[email protected]> for proper XHTML output.
;; * Jose A. Ortega Ruiz <[email protected]> for Emacs 23 fixes.
;; * Alec Resnick <[email protected]> for bug reports.
;; * Joost Kremers <[email protected]> for bug reports
;; regarding indentation.
;; * Peter Williams <[email protected]> for fill-paragraph
;; enhancements.
;; * George Ogata <[email protected]> for fixing several
;; byte-compilation warnings.
;; * Eric Merritt <[email protected]> for wiki link features.
;; * Philippe Ivaldi <[email protected]> for XHTML preview
;; customizations and XHTML export.
;; * Jeremiah Dodds <[email protected]> for supporting
;; Markdown processors which do not accept input from stdin.
;; * Werner Dittmann <[email protected]> for bug reports
;; regarding the cl dependency and auto-fill-mode and indentation.
;; * Scott Pfister <[email protected]> for generalizing the space
;; substitution character for mapping wiki links to filenames.
;; * Marcin Kasperski <[email protected]> for a patch to
;; escape shell commands.
;; * Christopher J. Madsen <[email protected]> for patches to fix a match
;; data bug and to prefer `visual-line-mode' in `gfm-mode'.
;; * Shigeru Fukaya <[email protected]> for better adherence to
;; Emacs Lisp coding conventions.
;; * Donald Curtis <[email protected]> for fixing the `paragraph-fill' regexp.
;; * Kevin Porter <[email protected]> for wiki link handling in `gfm-mode'.
;;; Bugs:
;; Although markdown-mode is developed and tested primarily using
;; GNU Emacs 24, compatibility with earlier Emacsen is also a
;; priority.
;;
;; If you find any bugs in markdown-mode, please construct a test case
;; or a patch and email me at <[email protected]>.
;;; History:
;; markdown-mode was written and is maintained by Jason Blevins. The
;; first version was released on May 24, 2007.
;;
;; * 2007-05-24: Version 1.1
;; * 2007-05-25: Version 1.2
;; * 2007-06-05: [Version 1.3][]
;; * 2007-06-29: Version 1.4
;; * 2008-05-24: [Version 1.5][]
;; * 2008-06-04: [Version 1.6][]
;; * 2009-10-01: [Version 1.7][]
;; * 2011-08-12: [Version 1.8][]
;; * 2011-08-15: [Version 1.8.1][]
;;
;; [Version 1.3]: http://jblevins.org/projects/markdown-mode/rev-1-3
;; [Version 1.5]: http://jblevins.org/projects/markdown-mode/rev-1-5
;; [Version 1.6]: http://jblevins.org/projects/markdown-mode/rev-1-6
;; [Version 1.7]: http://jblevins.org/projects/markdown-mode/rev-1-7
;; [Version 1.8]: http://jblevins.org/projects/markdown-mode/rev-1-8
;; [Version 1.8.1]: http://jblevins.org/projects/markdown-mode/rev-1-8-1
;;; Code:
(require 'easymenu)
(require 'outline)
(eval-when-compile (require 'cl))
;;; Constants =================================================================
(defconst markdown-mode-version "1.8.1"
"Markdown mode version number.")
(defconst markdown-output-buffer-name "*markdown-output*"
"Name of temporary buffer for markdown command output.")
;;; Customizable variables ====================================================
(defvar markdown-mode-hook nil
"Hook run when entering Markdown mode.")
(defgroup markdown nil
"Major mode for editing text files in Markdown format."
:prefix "markdown-"
:group 'wp
:link '(url-link "http://jblevins.org/projects/markdown-mode/"))
(defcustom markdown-command "markdown"
"Command to run markdown."
:group 'markdown
:type 'string)
(defcustom markdown-command-needs-filename nil
"Set to non-nil if `markdown-command' does not accept input from stdin.
Instead, it will be passed a filename as the final command-line
option. As a result, you will only be able to run Markdown from
buffers which are visiting a file."
:group 'markdown
:type 'boolean)
(defcustom markdown-hr-string "* * * * *"
"String to use for horizonal rules."
:group 'markdown
:type 'string)
(defcustom markdown-bold-underscore nil
"Use two underscores for bold instead of two asterisks."
:group 'markdown
:type 'boolean)
(defcustom markdown-italic-underscore nil
"Use underscores for italic instead of asterisks."
:group 'markdown
:type 'boolean)
(defcustom markdown-indent-function 'markdown-indent-line
"Function to use to indent."
:group 'markdown
:type 'function)
(defcustom markdown-indent-on-enter t
"Automatically indent new lines when enter key is pressed."
:group 'markdown
:type 'boolean)
(defcustom markdown-follow-wiki-link-on-enter t
"Follow wiki link at point (if any) when the enter key is pressed."
:group 'markdown
:type 'boolean)
(defcustom markdown-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 'markdown
:type 'boolean)
(defcustom markdown-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 'markdown
:type 'list)
(defcustom markdown-enable-math nil
"Syntax highlighting for inline LaTeX expressions.
This will not take effect until Emacs is restarted."
:group 'markdown
:type 'boolean)
(defcustom markdown-css-path ""
"URL of CSS file to link to in the output XHTML."
:group 'markdown
:type 'string)
(defcustom markdown-xhtml-header-content ""
"Additional content to include in the XHTML <head> block."
:group 'markdown
:type 'string)
(defcustom markdown-xhtml-standalone-regexp
"^\\(\<\?xml\\|\<!DOCTYPE\\|\<html\\)"
"Regexp indicating whether `markdown-command' output is standalone XHTML."
:group 'markdown
:type 'regexp)
(defcustom markdown-link-space-sub-char
"_"
"Character to use instead of spaces when mapping wiki links to filenames."
:group 'markdown
:type 'string)
(defcustom markdown-footnote-location 'end
"Position where new footnotes are inserted in the document."
:group 'markdown
:type '(choice (const :tag "At the end of the document" end)
(const :tag "Immediately after the paragraph" immediately)
(const :tag "Before next header" header)))
;;; Font lock =================================================================
(require 'font-lock)
(defvar markdown-italic-face 'markdown-italic-face
"Face name to use for italic text.")
(defvar markdown-bold-face 'markdown-bold-face
"Face name to use for bold text.")
(defvar markdown-header-face 'markdown-header-face
"Face name to use as a base for headers.")
(defvar markdown-header-face-1 'markdown-header-face-1
"Face name to use for level-1 headers.")
(defvar markdown-header-face-2 'markdown-header-face-2
"Face name to use for level-2 headers.")
(defvar markdown-header-face-3 'markdown-header-face-3
"Face name to use for level-3 headers.")
(defvar markdown-header-face-4 'markdown-header-face-4
"Face name to use for level-4 headers.")
(defvar markdown-header-face-5 'markdown-header-face-5
"Face name to use for level-5 headers.")
(defvar markdown-header-face-6 'markdown-header-face-6
"Face name to use for level-6 headers.")
(defvar markdown-inline-code-face 'markdown-inline-code-face
"Face name to use for inline code.")
(defvar markdown-list-face 'markdown-list-face
"Face name to use for list markers.")
(defvar markdown-blockquote-face 'markdown-blockquote-face
"Face name to use for blockquote.")
(defvar markdown-pre-face 'markdown-pre-face
"Face name to use for preformatted text.")
(defvar markdown-link-face 'markdown-link-face
"Face name to use for links.")
(defvar markdown-missing-link-face 'markdown-missing-link-face
"Face name to use for links where the linked file does not exist.")
(defvar markdown-reference-face 'markdown-reference-face
"Face name to use for reference.")
(defvar markdown-footnote-face 'markdown-footnote-face
"Face name to use for footnote identifiers.")
(defvar markdown-url-face 'markdown-url-face
"Face name to use for URLs.")
(defvar markdown-link-title-face 'markdown-link-title-face
"Face name to use for reference link titles.")
(defvar markdown-comment-face 'markdown-comment-face
"Face name to use for HTML comments.")
(defvar markdown-math-face 'markdown-math-face
"Face name to use for LaTeX expressions.")
(defgroup markdown-faces nil
"Faces used in Markdown Mode"
:group 'markdown
:group 'faces)
(defface markdown-italic-face
'((t (:inherit font-lock-variable-name-face :slant italic)))
"Face for italic text."
:group 'markdown-faces)
(defface markdown-bold-face
'((t (:inherit font-lock-variable-name-face :weight bold)))
"Face for bold text."
:group 'markdown-faces)
(defface markdown-header-face
'((t (:inherit font-lock-function-name-face :weight bold)))
"Base face for headers."
:group 'markdown-faces)
(defface markdown-header-face-1
'((t (:inherit markdown-header-face)))
"Face for level-1 headers."
:group 'markdown-faces)
(defface markdown-header-face-2
'((t (:inherit markdown-header-face)))
"Face for level-2 headers."
:group 'markdown-faces)
(defface markdown-header-face-3
'((t (:inherit markdown-header-face)))
"Face for level-3 headers."
:group 'markdown-faces)
(defface markdown-header-face-4
'((t (:inherit markdown-header-face)))
"Face for level-4 headers."
:group 'markdown-faces)
(defface markdown-header-face-5
'((t (:inherit markdown-header-face)))
"Face for level-5 headers."
:group 'markdown-faces)
(defface markdown-header-face-6
'((t (:inherit markdown-header-face)))
"Face for level-6 headers."
:group 'markdown-faces)
(defface markdown-inline-code-face
'((t (:inherit font-lock-constant-face)))
"Face for inline code."
:group 'markdown-faces)
(defface markdown-list-face
'((t (:inherit font-lock-builtin-face)))
"Face for list item markers."
:group 'markdown-faces)
(defface markdown-blockquote-face
'((t (:inherit font-lock-doc-face)))
"Face for blockquote sections."
:group 'markdown-faces)
(defface markdown-pre-face
'((t (:inherit font-lock-constant-face)))
"Face for preformatted text."
:group 'markdown-faces)
(defface markdown-link-face
'((t (:inherit font-lock-keyword-face)))
"Face for links."
:group 'markdown-faces)
(defface markdown-missing-link-face
'((t (:inherit font-lock-warning-face)))
"Face for missing links."
:group 'markdown-faces)
(defface markdown-reference-face
'((t (:inherit font-lock-type-face)))
"Face for link references."
:group 'markdown-faces)
(defface markdown-footnote-face
'((t (:inherit font-lock-keyword-face)))
"Face for footnote markers."
:group 'markdown-faces)
(defface markdown-url-face
'((t (:inherit font-lock-string-face)))
"Face for URLs."
:group 'markdown-faces)
(defface markdown-link-title-face
'((t (:inherit font-lock-comment-face)))
"Face for reference link titles."
:group 'markdown-faces)
(defface markdown-comment-face
'((t (:inherit font-lock-comment-face)))
"Face for HTML comments."
:group 'markdown-faces)
(defface markdown-math-face
'((t (:inherit font-lock-string-face)))
"Face for LaTeX expressions."
:group 'markdown-faces)
(defconst markdown-regex-link-inline
"\\(!?\\[[^]]*?\\]\\)\\(([^\\)]*)\\)"
"Regular expression for a [text](file) or an image link ![text](file).")
(defconst markdown-regex-link-reference
"\\(!?\\[[^]]+?\\]\\)[ ]?\\(\\[[^]]*?\\]\\)"
"Regular expression for a reference link [text][id].")
(defconst markdown-regex-reference-definition
"^ \\{0,3\\}\\(\\[.*\\]\\):\\s *\\(.*?\\)\\s *\\( \"[^\"]*\"$\\|$\\)"
"Regular expression for a link definition [id]: ...")
(defconst markdown-regex-footnote
"\\(\\[\\^.+?\\]\\)"
"Regular expression for a footnote marker [^fn].")
(defconst markdown-regex-header
"#+\\|\\S-.*\n\\(?:\\(===+\\)\\|\\(---+\\)\\)$"
"Regexp identifying Markdown headers.")
(defconst markdown-regex-header-1-atx
"^\\(# \\)\\(.*?\\)\\($\\| #+$\\)"
"Regular expression for level 1 atx-style (hash mark) headers.")
(defconst markdown-regex-header-2-atx
"^\\(## \\)\\(.*?\\)\\($\\| #+$\\)"
"Regular expression for level 2 atx-style (hash mark) headers.")
(defconst markdown-regex-header-3-atx
"^\\(### \\)\\(.*?\\)\\($\\| #+$\\)"
"Regular expression for level 3 atx-style (hash mark) headers.")
(defconst markdown-regex-header-4-atx
"^\\(#### \\)\\(.*?\\)\\($\\| #+$\\)"
"Regular expression for level 4 atx-style (hash mark) headers.")
(defconst markdown-regex-header-5-atx
"^\\(##### \\)\\(.*?\\)\\($\\| #+$\\)"
"Regular expression for level 5 atx-style (hash mark) headers.")
(defconst markdown-regex-header-6-atx
"^\\(###### \\)\\(.*?\\)\\($\\| #+$\\)"
"Regular expression for level 6 atx-style (hash mark) headers.")
(defconst markdown-regex-header-1-setext
"^\\(.*\\)\n\\(===+\\)$"
"Regular expression for level 1 setext-style (underline) headers.")
(defconst markdown-regex-header-2-setext
"^\\(.*\\)\n\\(---+\\)$"
"Regular expression for level 2 setext-style (underline) headers.")
(defconst markdown-regex-hr
"^\\(\\*[ ]?\\*[ ]?\\*[ ]?[\\* ]*\\|-[ ]?-[ ]?-[--- ]*\\)$"
"Regular expression for matching Markdown horizontal rules.")
(defconst markdown-regex-code
"\\(^\\|[^\\]\\)\\(\\(`\\{1,2\\}\\)\\([^ \\]\\|[^ ]\\(.\\|\n[^\n]\\)*?[^ \\]\\)\\3\\)"
"Regular expression for matching inline code fragments.")
(defconst markdown-regex-pre
"^\\( \\|\t\\).*$"
"Regular expression for matching preformatted text sections.")
(defconst markdown-regex-list
"^[ \t]*\\([0-9]+\\.\\|[\\*\\+-]\\) "
"Regular expression for matching list markers.")
(defconst markdown-regex-bold
"\\(^\\|[^\\]\\)\\(\\([*_]\\{2\\}\\)\\(.\\|\n[^\n]\\)*?[^\\ ]\\3\\)"
"Regular expression for matching bold text.")
(defconst markdown-regex-italic
"\\(^\\|[^\\]\\)\\(\\([*_]\\)\\([^ \\]\\3\\|[^ ]\\(.\\|\n[^\n]\\)*?[^\\ ]\\3\\)\\)"
"Regular expression for matching italic text.")
(defconst markdown-regex-blockquote
"^>.*$"
"Regular expression for matching blockquote lines.")
(defconst markdown-regex-line-break
" $"
"Regular expression for matching line breaks.")
(defconst markdown-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 markdown-regex-uri
(concat
"\\(" (mapconcat 'identity markdown-uri-types "\\|")
"\\):[^]\t\n\r<>,;() ]+")
"Regular expression for matching inline URIs.")
(defconst markdown-regex-angle-uri
(concat
"\\(<\\)\\("
(mapconcat 'identity markdown-uri-types "\\|")
"\\):[^]\t\n\r<>,;()]+\\(>\\)")
"Regular expression for matching inline URIs in angle brackets.")
(defconst markdown-regex-email
"<\\(\\sw\\|\\s_\\|\\s.\\)+@\\(\\sw\\|\\s_\\|\\s.\\)+>"
"Regular expression for matching inline email addresses.")
(defconst markdown-regex-latex-expression
"\\(^\\|[^\\]\\)\\(\\$\\($\\([^\\$]\\|\\\\.\\)*\\$\\|\\([^\\$]\\|\\\\.\\)*\\)\\$\\)"
"Regular expression for itex $..$ or $$..$$ math mode expressions.")
(defconst markdown-regex-latex-display
"^\\\\\\[\\(.\\|\n\\)*?\\\\\\]$"
"Regular expression for itex \[..\] display mode expressions.")
(defconst markdown-regex-list-indent
"^\\(\\s *\\)\\([0-9]+\\.\\|[\\*\\+-]\\)\\(\\s +\\)"
"Regular expression for matching indentation of list items.")
(defvar markdown-mode-font-lock-keywords-basic
(list
'(markdown-match-pre-blocks 0 markdown-pre-face t t)
'(markdown-match-fenced-code-blocks 0 markdown-pre-face t t)
(cons markdown-regex-blockquote 'markdown-blockquote-face)
(cons markdown-regex-header-1-setext 'markdown-header-face-1)
(cons markdown-regex-header-2-setext 'markdown-header-face-2)
(cons markdown-regex-header-1-atx 'markdown-header-face-1)
(cons markdown-regex-header-2-atx 'markdown-header-face-2)
(cons markdown-regex-header-3-atx 'markdown-header-face-3)
(cons markdown-regex-header-4-atx 'markdown-header-face-4)
(cons markdown-regex-header-5-atx 'markdown-header-face-5)
(cons markdown-regex-header-6-atx 'markdown-header-face-6)
(cons markdown-regex-hr 'markdown-header-face)
'(markdown-match-comments 0 markdown-comment-face t t)
(cons markdown-regex-code '(2 markdown-inline-code-face))
(cons markdown-regex-angle-uri 'markdown-link-face)
(cons markdown-regex-uri 'markdown-link-face)
(cons markdown-regex-email 'markdown-link-face)
(cons markdown-regex-list 'markdown-list-face)
(cons markdown-regex-link-inline
'((1 markdown-link-face t)
(2 markdown-url-face t)))
(cons markdown-regex-link-reference
'((1 markdown-link-face t)
(2 markdown-reference-face t)))
(cons markdown-regex-reference-definition
'((1 markdown-reference-face t)
(2 markdown-url-face t)
(3 markdown-link-title-face t)))
(cons markdown-regex-footnote 'markdown-footnote-face)
(cons markdown-regex-bold '(2 markdown-bold-face))
(cons markdown-regex-italic '(2 markdown-italic-face))
)
"Syntax highlighting for Markdown files.")
(defconst markdown-mode-font-lock-keywords-latex
(list
;; Math mode $..$ or $$..$$
(cons markdown-regex-latex-expression '(2 markdown-math-face))
;; Display mode equations with brackets: \[ \]
(cons markdown-regex-latex-display 'markdown-math-face)
;; Equation reference (eq:foo)
(cons "(eq:\\w+)" 'markdown-reference-face)
;; Equation reference \eqref{foo}
(cons "\\\\eqref{\\w+}" 'markdown-reference-face))
"Syntax highlighting for LaTeX fragments.")
(defvar markdown-mode-font-lock-keywords
(append
(if markdown-enable-math
markdown-mode-font-lock-keywords-latex)
markdown-mode-font-lock-keywords-basic)
"Default highlighting expressions for Markdown mode.")
;; Footnotes
(defvar markdown-footnote-counter 0
"Counter for footnote numbers.")
(make-variable-buffer-local 'markdown-footnote-counter)
(defconst markdown-footnote-chars
"[[:alnum:]-]"
"Regular expression maching any character that is allowed in a footnote identifier.")
;;; Compatibility =============================================================
;; Handle replace-regexp-in-string in XEmacs 21
(defun markdown-replace-regexp-in-string (regexp rep string)
"Compatibility wrapper to provide `replace-regexp-in-string'."
(if (featurep 'xemacs)
(replace-in-string string regexp rep)
(replace-regexp-in-string regexp rep string)))
;;; Markdown parsing functions ================================================
(defun markdown-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 markdown-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)
(markdown-cur-line-blank-p))))
(defun markdown-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)
(markdown-cur-line-blank-p))))
(defun markdown-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 markdown-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 markdown-prev-line-indent ()
"Return the number of leading whitespace characters in the previous line."
(save-excursion
(forward-line -1)
(markdown-cur-line-indent)))
(defun markdown-next-line-indent ()
"Return the number of leading whitespace characters in the next line."
(save-excursion
(forward-line 1)
(markdown-cur-line-indent)))
(defun markdown-cur-non-list-indent ()
"Return the number of leading whitespace characters in the current line."
(save-excursion
(beginning-of-line)
(when (re-search-forward markdown-regex-list-indent (point-at-eol) t)
(current-column))))
(defun markdown-prev-non-list-indent ()
"Return position of the first non-list-marker on the previous line."
(save-excursion
(forward-line -1)
(markdown-cur-non-list-indent)))
(defun markdown--next-block ()
"Move the point to the start of the next text block."
(forward-line)
(while (and (or (not (markdown-prev-line-blank-p))
(markdown-cur-line-blank-p))
(not (eobp)))
(forward-line)))
(defun markdown--end-of-level (level)
"Move the point to the end of region with indentation at least LEVEL."
(let (indent)
(while (and (not (< (setq indent (markdown-cur-line-indent)) level))
(not (>= indent (+ level 4)))
(not (eobp)))
(markdown--next-block))
(unless (eobp)
;; Move back before any trailing blank lines