forked from polymode/poly-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly-markdown.el
166 lines (146 loc) · 6.96 KB
/
poly-markdown.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
;;; poly-markdown.el --- Polymode for markdown-mode -*- lexical-binding: t -*-
;;
;; Author: Vitalie Spinu
;; Maintainer: Vitalie Spinu
;; Copyright (C) 2018
;; Version: 0.2.2
;; Package-Requires: ((emacs "25") (polymode "0.2.2") (markdown-mode "2.3"))
;; URL: https://github.com/polymode/poly-markdown
;; Keywords: emacs
;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'polymode)
(require 'poly-lock)
(require 'markdown-mode)
;; Declarations
(defvar markdown-enable-math)
(define-obsolete-variable-alias 'pm-host/markdown 'poly-markdown-hostmode "v0.2")
(define-obsolete-variable-alias 'pm-inner/markdown-yaml-metadata 'poly-markdown-yaml-metadata-innermode "v0.2")
(define-obsolete-variable-alias 'pm-inner/markdown-fenced-code 'poly-markdown-fenced-code-innermode "v0.2")
(define-obsolete-variable-alias 'pm-inner/markdown-inline-code 'poly-markdown-inline-code-innermode "v0.2")
(define-obsolete-variable-alias 'pm-inner/markdown-displayed-math 'poly-markdown-displayed-math-innermode "v0.2")
(define-obsolete-variable-alias 'pm-inner/markdown-inline-math 'poly-markdown-inline-math-innermode "v0.2")
(define-obsolete-variable-alias 'pm-poly/markdown 'poly-markdown-polymode "v0.2")
(define-hostmode poly-markdown-hostmode
:mode 'markdown-mode
:init-functions '(poly-markdown-remove-markdown-hooks))
(define-innermode poly-markdown-root-innermode
:mode nil
:fallback-mode 'host
:head-mode 'host
:tail-mode 'host)
(define-innermode poly-markdown-yaml-metadata-innermode poly-markdown-root-innermode
:mode 'yaml-mode
:head-matcher (pm-make-text-property-matcher 'markdown-yaml-metadata-begin :inc-end)
:tail-matcher (pm-make-text-property-matcher 'markdown-yaml-metadata-end))
;; allow extra . before language name https://github.com/polymode/polymode/issues/296
;; allow extra = before language name https://github.com/polymode/poly-markdown/issues/22
(define-auto-innermode poly-markdown-fenced-code-innermode poly-markdown-root-innermode
:head-matcher (cons "^[ \t]*\\(```[ \t]*{?[[:alpha:].=].*\n\\)" 1)
:tail-matcher (cons "^[ \t]*\\(```\\)[ \t]*$" 1)
:mode-matcher (cons "```[ \t]*{?[.=]?\\(?:lang *= *\\)?\\([^ \t\n;=,}]+\\)" 1))
;; Intended to be inherited from by more specialized innermodes.
;; FIXME: Some font-lock issues on deletion.
;; TOTHINK: Can be added to the default configuration for the sake of the
;; default fallback, but it's problematic given that inline code blocks often
;; have arbitrary non-code or language specific content.
(define-innermode poly-markdown-inline-code-innermode poly-markdown-root-innermode
:head-matcher (cons "[^`]\\(`\\)[[:alpha:]+-&({*[]" 1)
:tail-matcher (cons "[^`]\\(`\\)[^`]" 1)
;; :mode-matcher (cons "`[ \t]*{?\\(?:lang *= *\\)?\\([[:alpha:]+-]+\\)" 1)
:allow-nested nil)
(defun poly-markdown-displayed-math-head-matcher (count)
(when markdown-enable-math
(when (re-search-forward "\\\\\\[\\|^[ \t]*\\(\\$\\$\\)." nil t count)
(if (match-beginning 1)
(cons (match-beginning 1) (match-end 1))
(cons (match-beginning 0) (match-end 0))))))
(defun poly-markdown-displayed-math-tail-matcher (_count)
(when markdown-enable-math
(if (match-beginning 1)
;; head matched an $$..$$ block
(when (re-search-forward "[^$]\\(\\$\\$\\)[^$[:alnum:]]" nil t)
(cons (match-beginning 1) (match-end 1)))
;; head matched an \[..\] block
(when (re-search-forward "\\\\\\]" nil t)
(cons (match-beginning 0) (match-end 0))))))
(define-innermode poly-markdown-displayed-math-innermode poly-markdown-root-innermode
"Displayed math $$..$$ innermode.
Tail must be flowed by a new line but head need not (a space or
comment character would do)."
:mode 'latex-mode
:head-matcher #'poly-markdown-displayed-math-head-matcher
:tail-matcher #'poly-markdown-displayed-math-tail-matcher
:allow-nested nil)
(defun poly-markdown-inline-math-head-matcher (count)
(when markdown-enable-math
(when (re-search-forward "\\\\(\\|[ \t\n]\\(\\$\\)[^ $\t[:digit:]]" nil t count)
(if (match-beginning 1)
(cons (match-beginning 1) (match-end 1))
(cons (match-beginning 0) (match-end 0))))))
(defun poly-markdown-inline-math-tail-matcher (_count)
(when markdown-enable-math
(if (match-beginning 1)
;; head matched an $..$ block
(when (re-search-forward "[^ $\\\t]\\(\\$\\)[^$[:alnum:]]" nil t)
(cons (match-beginning 1) (match-end 1)))
;; head matched an \(..\) block
(when (re-search-forward "\\\\)" nil t)
(cons (match-beginning 0) (match-end 0))))))
(define-innermode poly-markdown-inline-math-innermode poly-markdown-root-innermode
"Inline math $..$ block.
First $ must be preceded by a white-space character and followed
by a non-whitespace/digit character. The closing $ must be
preceded by a non-whitespace and not followed by an alphanumeric
character."
:mode 'latex-mode
:head-matcher #'poly-markdown-inline-math-head-matcher
:tail-matcher #'poly-markdown-inline-math-tail-matcher
:allow-nested nil)
;;;###autoload (autoload 'poly-markdown-mode "poly-markdown")
(define-polymode poly-markdown-mode
:hostmode 'poly-markdown-hostmode
:innermodes '(poly-markdown-fenced-code-innermode
;; poly-markdown-inline-code-innermode
poly-markdown-displayed-math-innermode
poly-markdown-inline-math-innermode
poly-markdown-yaml-metadata-innermode))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.md\\'" . poly-markdown-mode))
;;; FIXES:
(defun poly-markdown-remove-markdown-hooks (_)
;; get rid of aggressive hooks (VS[02-09-2018]: probably no longer necessary)
(remove-hook 'window-configuration-change-hook 'markdown-fontify-buffer-wiki-links t)
(remove-hook 'after-change-functions 'markdown-check-change-for-wiki-link t))
;;; Polymode for GitHub Flavored Markdown
(define-hostmode poly-gfm-hostmode poly-markdown-hostmode
:mode 'gfm-mode)
;;;###autoload (autoload 'poly-gfm-mode "poly-markdown")
(define-polymode poly-gfm-mode poly-markdown-mode
:hostmode 'poly-gfm-hostmode)
(provide 'poly-markdown)