unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 063780a772f7b1cdfe61d44bb4eeb15b0521fa8d 3847 bytes (raw)
name: lisp/textmodes/markdown-ts-mode.el 	 # note: path name is non-authoritative(*)

  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
 
;;; markdown-ts-mode.el --- tree sitter support for Markdown  -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Free Software Foundation, Inc.

;; Author     : Rahul Martim Juliato <rahul.juliato@gmail.com>
;; Maintainer : Rahul Martim Juliato <rahul.juliato@gmail.com>
;; Created    : April 2024
;; Keywords   : markdown md languages tree-sitter

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;

;;; Code:

(require 'treesit)
(require 'subr-x)

(declare-function treesit-node-parent "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-parser-create "treesit.c")

(defvar markdown-ts--treesit-settings
  (treesit-font-lock-rules
   :language 'markdown
   :override t
   :feature 'delimiter
   '([ "[" "]" "(" ")" ] @shadow)

   :language 'markdown
   :feature 'paragraph
   '([((atx_heading) @font-lock-keyword-face)
      ((block_quote_marker) @font-lock-string-face)
      ((code_span) @font-lock-string-face)
      ((emphasis) @underline)
      ((image_description) @link)
      ((indented_code_block) @font-lock-string-face)
      ((link_destination) @font-lock-string-face)
      ((setext_heading) @font-lock-keyword-face)
      ((strong_emphasis) @bold)
      ((thematic_break) @shadow)
      (block_quote (block_quote_marker) @font-lock-string-face)
      (block_quote (paragraph) @font-lock-string-face)
      (fenced_code_block (code_fence_content) @font-lock-string-face)
      (fenced_code_block (fenced_code_block_delimiter) @font-lock-doc-face)
      (inline_link (link_destination) @font-lock-string-face)
      (inline_link (link_text) @link)
      (list_item (list_marker_dot) @font-lock-keyword-face)
      (list_item (list_marker_minus) @font-lock-keyword-face)
      (list_item (list_marker_plus) @font-lock-keyword-face)
      (list_item (list_marker_star) @font-lock-keyword-face)
      (shortcut_link (link_text) @link)
      ])))

(defun markdown-ts-imenu-node-p (node)
  "Check if NODE is a valid entry to imenu."
  (equal (treesit-node-type (treesit-node-parent node))
         "atx_heading"))

(defun markdown-ts-imenu-name-function (node)
  "Return an imenu entry if NODE is a valid header."
  (let ((name (treesit-node-text node)))
    (if (markdown-ts-imenu-node-p node)
	(thread-first (treesit-node-parent node)(treesit-node-text))
      name)))

(defun markdown-ts-setup ()
  "Setup treesit for `markdown-ts-mode'."
  (setq-local treesit-font-lock-settings markdown-ts--treesit-settings)
  (treesit-major-mode-setup))

;;;###autoload
(define-derived-mode markdown-ts-mode text-mode "Markdown"
  "Major mode for editing Markdown using tree-sitter grammar."
  (setq-local font-lock-defaults nil
	      treesit-font-lock-feature-list '((delimiter)
					       (paragraph)))

  (setq-local treesit-simple-imenu-settings
              `(("Headings" markdown-ts-imenu-node-p nil markdown-ts-imenu-name-function)))

  (when (treesit-ready-p 'markdown)
    (treesit-parser-create 'markdown)
    (markdown-ts-setup)))

(derived-mode-add-parents 'markdown-ts-mode '(markdown-mode))

(if (treesit-ready-p 'markdown)
    (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-ts-mode)))

(provide 'markdown-ts-mode)
;;; markdown-ts-mode.el ends here

debug log:

solving 063780a772f ...
found 063780a772f in https://yhetil.org/emacs-devel/87edazgt7v.fsf@gmail.com/

applying [1/1] https://yhetil.org/emacs-devel/87edazgt7v.fsf@gmail.com/
diff --git a/lisp/textmodes/markdown-ts-mode.el b/lisp/textmodes/markdown-ts-mode.el
new file mode 100644
index 00000000000..063780a772f

Checking patch lisp/textmodes/markdown-ts-mode.el...
Applied patch lisp/textmodes/markdown-ts-mode.el cleanly.

index at:
100644 063780a772f7b1cdfe61d44bb4eeb15b0521fa8d	lisp/textmodes/markdown-ts-mode.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).