all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 60321d789eb38ce7bc136385655d570899480adc 4625 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
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
 
;;; markdown-ts-mode.el --- Markdown editing mode -*- lexical-binding: t; -*-

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

;; Author: Philip Kaludercic <philipk@posteo.net>
;; Keywords: languages

;; 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:

;; Major mode for Markdown.  The syntax highlighting has been
;; implemented using Tree Sitter.

;;; Code:

(eval-when-compile (require 'pcase))
(require 'treesit)

(defgroup markdown-ts-mode '()
  "Markdown support."
  :prefix "markdown-ts-"
  :group 'text)

(defcustom markdown-ts-mode-executable
  (seq-find #'executable-find '("cmark" "pandoc" "markdown"))
  "Name of the executable used to render Markdown.
It should read the Markdown text via stdin, and render HTML via
the standard output."
  :type 'string)

(defconst markdown-ts-mode-font-lock-rules
  (treesit-font-lock-rules
   :language 'markdown
   :feature 'bracket
   '((["[" "]" "{" "}"]) @font-lock-bracket-face)
   ;; TODO
   )
  "Markdown settings suitable for `treesit-font-lock-settings'.")

(defun markdown-ts-mode-insert-markup (left right)
  "Generate a new command for inserting markup.
Insert LEFT and RIGHT into the current buffer.
Both arguments are strings that are inserted LEFT and RIGHT of
the active region, or if not present the current point.  This
function is meant to be invoked by other commands that specialise
on a specific kind of formatting."
  (lambda ()
    (interactive)
    (if (use-region-p)
        (save-mark-and-excursion
          (goto-char (region-end))
          (insert right)
          (goto-char (region-beginning))
          (insert left))
      (insert left)
      (save-excursion (insert right)))))

(defalias 'markdown-ts-mode-insert-bold
  (markdown-ts-mode-insert-markup "**" "**")
  "Insert bold markup.")

(defalias 'markdown-ts-mode-insert-italics
  (markdown-ts-mode-insert-markup "_" "_")
  "Insert italic markup.")

;; (defalias 'markdown-ts-mode-insert-link (uri)
;;   (markdown-ts-mode-insert-markup
;;    "[" (format "](%s)" uri))
;;   "Insert a link to a URI."
;;   (interactive "MURI: ")
;;   )

(defun markdown-ts-mode-render ()
  "Render the current buffer using EWW."
  (interactive)
  (unless markdown-ts-mode-executable
    (user-error "No markdown available"))
  (let ((bounds (bounds-of-thing-at-point (if (use-region-p)
                                              'region
                                            'buffer)))
        (file (make-temp-file "markdown" nil ".html")))
    (call-process-region (car bounds) (cdr bounds)
                         markdown-ts-mode-executable
                         nil (list :file file))
    (eww-open-file file)))

(defvar-keymap markdown-ts-mode-map
  "C-c C-f C-b" #'markdown-ts-mode-insert-bold
  "C-c C-f C-i" #'markdown-ts-mode-insert-italics
  ;; "C-c C-l" #'markdown-ts-mode-insert-link
  "C-c C-c" #'markdown-ts-mode-render)

;;;###autoload
(define-derived-mode markdown-ts-mode text-mode "MD"
  "Major mode for editing Markdown files.
This implementation makes use of Tree Sitter (see the Info
Node `(elisp) Tree-sitter Major Modes'), and relies on external
support for rendering.  Using `treesit-install-language-grammar'
you can install a grammar."
  (unless (treesit-ready-p 'markdown)
    (user-error "Tree Sitter support is not available"))
  (treesit-parser-create 'markdown)
  (setq-local treesit-font-lock-settings
              markdown-ts-mode-font-lock-rules
              treesit-font-lock-feature-list
              '((constant escape-sequence number property)
                (bracket delimiter error misc-punctuation)
                (string type)
                (comment))
              comment-start "# ")
  (treesit-major-mode-setup))

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

(add-to-list
 'treesit-language-source-alist
 '(markdown "https://github.com/ikatyang/tree-sitter-markdown" "v0.7.1"))

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

debug log:

solving 60321d789eb ...
found 60321d789eb in https://yhetil.org/emacs/875xyrged4.fsf@posteo.net/

applying [1/1] https://yhetil.org/emacs/875xyrged4.fsf@posteo.net/
diff --git a/lisp/textmodes/markdown-ts-mode.el b/lisp/textmodes/markdown-ts-mode.el
new file mode 100644
index 00000000000..60321d789eb

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

index at:
100644 60321d789eb38ce7bc136385655d570899480adc	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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.