Skip to content

Commit

Permalink
rewrite syntax-table definition of markdown-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed May 25, 2024
1 parent e847053 commit 340a7f1
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 18 deletions.
36 changes: 36 additions & 0 deletions extensions/markdown-mode/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Hello

```lisp
(defpackage :foo
(:use :cl))
(in-package :foo)
(defun hello ()
(write-line "Hello World"))
```

```c
#include <stdio.h>

int main(void) {
printf("Hello World\n");
}
```
## Test
this is test
- foo
- bar
- baz
* hoge
* piyo
+ hoge
+ piyo
1. one
2. two
3. three
3 changes: 2 additions & 1 deletion extensions/markdown-mode/lem-markdown-mode.asd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(defsystem "lem-markdown-mode"
:depends-on ("lem")
:serial t
:components ((:file "markdown-mode")))
:components ((:file "syntax-parser")
(:file "markdown-mode")))
19 changes: 2 additions & 17 deletions extensions/markdown-mode/markdown-mode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,11 @@
:markdown-mode))
(in-package :lem-markdown-mode)

(defun make-tmlanguage-markdown ()
(let* ((patterns (make-tm-patterns
(make-tm-match "^#.*$" :name 'syntax-constant-attribute)
(make-tm-match "^>.*$" :name 'syntax-string-attribute)
(make-tm-region '(:sequence "```")
'(:sequence "```")
:name 'syntax-string-attribute
:patterns (make-tm-patterns (make-tm-match "\\\\.")))
(make-tm-match "([-*_] ?)([-*_] ?)([-*_] ?)+"
:name 'syntax-comment-attribute)
(make-tm-match "^ *([*+\\-]|([0-9]+\\.)) +"
:name 'syntax-keyword-attribute))))
(make-tmlanguage :patterns patterns)))

(defvar *markdown-syntax-table*
(let ((table (make-syntax-table
:space-chars '(#\space #\tab #\newline)
:string-quote-chars '(#\`)))
(tmlanguage (make-tmlanguage-markdown)))
(set-syntax-parser table tmlanguage)
:string-quote-chars '(#\`))))
(set-syntax-parser table (lem-markdown-mode/syntax-parser:make-syntax-parser))
table))

(define-major-mode markdown-mode language-mode
Expand Down
59 changes: 59 additions & 0 deletions extensions/markdown-mode/syntax-parser.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(defpackage :lem-markdown-mode/syntax-parser
(:use :cl :lem)
(:export :make-syntax-parser
:scan-buffer))
(in-package :lem-markdown-mode/syntax-parser)

(defclass syntax-parser () ())

(defun make-syntax-parser ()
(make-instance 'syntax-parser))

(defmethod lem/buffer/internal::%syntax-scan-region ((parser syntax-parser) start end)
(scan-region start end))

(defun put-line-attribute (point attribute)
(with-point ((start point)
(end point))
(line-start start)
(line-end end)
(put-text-property start end :attribute attribute)))

(defun scan-code-block (point end)
(log:info point end)
(let* ((groups (nth-value 1 (looking-at point "^```(.*)")))
(language-name (and groups (elt groups 0)))
(syntax-table (get-syntax-table-by-mode-name language-name)))
(line-offset point 1)
(with-point ((start point))
(loop :while (point< point end)
:until (looking-at point "^```")
:while (line-offset point 1))
(if syntax-table
(let ((lem/buffer/internal::*recursive-syntax-scan* nil))
(syntax-scan-region start point :syntax-table syntax-table))
(put-text-property start point :attribute 'syntax-string-attribute)))))

(defun scan-region (start end)
(with-point ((point start))
(loop :while (point< point end)
:do (cond ((looking-at point "^#")
(put-line-attribute point 'syntax-constant-attribute))
((looking-at point "^>")
(put-line-attribute point 'syntax-string-attribute))
((looking-at point "^\\s*[-*+]")
(back-to-indentation point)
(with-point ((start point)
(end point))
(character-offset end 1)
(put-text-property start end :attribute 'syntax-keyword-attribute)))
((looking-at point "^\\s*(?:\\d)+\\.\\s")
(back-to-indentation point)
(with-point ((start point)
(end point))
(skip-chars-forward end #'digit-char-p)
(character-offset end 1)
(put-text-property start end :attribute 'syntax-keyword-attribute)))
((looking-at point "^```")
(scan-code-block point end)))
:while (line-offset point 1))))

0 comments on commit 340a7f1

Please sign in to comment.