aboutsummaryrefslogtreecommitdiffhomepage
path: root/sundry/editor-syntax-etc/emacs/sisu-spine-ts-mode.el
blob: ef59d14d13c69be7690c19e8d3a2155ec30dc9a4 (plain)
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
;;; sisu-spine-ts-mode.el --- Tree-sitter major mode for SiSU spine markup -*- lexical-binding: t; -*-

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

;; Author: Ralph Amissah <ralph.amissah@gmail.com>
;; Maintainer: Ralph Amissah <ralph.amissah@gmail.com>
;; Keywords: text, syntax, processes, tools
;; Version:   1.0.0
;; URL: https://git.sisudoc.org/projects/sisudoc-spine/tree/sundry/editor-syntax-etc/emacs/sisu-spine-ts-mode.el
;;      https://sisudoc.org/

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

;;; Commentary:

;; Tree-sitter-backed major mode for SiSU spine markup (.sst / .ssm /
;; .ssi).  Sibling to `sisu-spine-mode' (regex / font-lock); requires
;; Emacs 29 or newer with `treesit' built in and the tree-sitter-sisu
;; parser installed.
;;
;; To install the parser inside Emacs:
;;
;;   M-x sisu-spine-ts-install-grammar RET
;;
;; or, equivalently:
;;
;;   (add-to-list 'treesit-language-source-alist
;;     '(sisu "https://git.sisudoc.org/projects"
;;            :source-dir "tree-sitter-sisu/src"))
;;   M-x treesit-install-language-grammar RET sisu RET
;;
;; The mode is auto-enabled for .sst / .ssm / .ssi files when the parser
;; is available.  When it is not, `sisu-spine-mode' (the regex variant)
;; remains available as a fallback.

;;; Code:

(require 'treesit nil t)

(defgroup sisu-spine-ts nil
  "Tree-sitter mode for SiSU spine markup."
  :group 'text
  :prefix "sisu-spine-ts-")

;; ---------------------------------------------------------------------
;; Faces (mirror the structural categories the highlights.scm assigns)
;; ---------------------------------------------------------------------

(defface sisu-spine-ts-heading-1-face
  '((t (:inherit outline-1 :weight bold)))
  "Face for :A~ headings."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-heading-2-face
  '((t (:inherit outline-2 :weight bold)))
  "Face for :B~ headings."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-heading-3-face
  '((t (:inherit outline-3 :weight bold)))
  "Face for :C~ / :D~ headings."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-heading-segment-face
  '((t (:inherit outline-4)))
  "Face for 1~ / 2~ / 3~ segment headings."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-block-delimiter-face
  '((t (:inherit font-lock-keyword-face)))
  "Face for block opening/closing delimiters."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-raw-content-face
  '((t (:inherit font-lock-string-face)))
  "Face for raw block content (code, table, etc.)."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-footnote-face
  '((t (:inherit font-lock-doc-face)))
  "Face for footnote and editor-note bodies."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-link-face
  '((t (:inherit link)))
  "Face for link text and target URLs."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-book-index-face
  '((t (:inherit font-lock-preprocessor-face)))
  "Face for book-index entries (={...})."
  :group 'sisu-spine-ts)

(defface sisu-spine-ts-marker-face
  '((t (:inherit font-lock-builtin-face)))
  "Face for inline-formatting delimiters and other punctuation markers."
  :group 'sisu-spine-ts)

;; ---------------------------------------------------------------------
;; Font-lock rules
;; ---------------------------------------------------------------------

(defvar sisu-spine-ts--font-lock-settings
  (when (fboundp 'treesit-font-lock-rules)
    (treesit-font-lock-rules
     :language 'sisu
     :feature 'comment
     '((version_comment) @font-lock-doc-face
       (header_comment)  @font-lock-comment-face
       (body_comment)    @font-lock-comment-face)

     :language 'sisu
     :feature 'header
     '((header_field key: (header_key)   @font-lock-keyword-face)
       (header_field value: (header_value) @font-lock-string-face)
       (header_continuation) @font-lock-string-face)

     :language 'sisu
     :feature 'heading
     '(((heading_part marker: (part_marker) @m
                       content: (heading_content) @sisu-spine-ts-heading-1-face)
        (:match "^:A~$" @m))
       ((heading_part marker: (part_marker) @m
                       content: (heading_content) @sisu-spine-ts-heading-2-face)
        (:match "^:B~$" @m))
       ((heading_part marker: (part_marker) @m
                       content: (heading_content) @sisu-spine-ts-heading-3-face)
        (:match "^:[CD]~$" @m))
       (heading_part marker: (part_marker) @sisu-spine-ts-marker-face)
       (heading_segment marker: (segment_marker) @sisu-spine-ts-marker-face
                        content: (heading_content) @sisu-spine-ts-heading-segment-face)
       (segment_name) @font-lock-function-name-face
       (suppress_marker) @sisu-spine-ts-marker-face)

     :language 'sisu
     :feature 'block
     '((block_open)  @sisu-spine-ts-block-delimiter-face
       (block_close) @sisu-spine-ts-block-delimiter-face
       (raw_content) @sisu-spine-ts-raw-content-face
       (table_spec)  @sisu-spine-ts-block-delimiter-face)

     :language 'sisu
     :feature 'inline
     '((emphasis)         @italic
       (bold)             @bold
       (italic)           @italic
       (underline)        @underline
       (citation_mark)    @font-lock-string-face
       (superscript)      @font-lock-type-face
       (subscript)        @font-lock-type-face
       (inserted)         @underline
       (strikethrough)    @shadow
       (monospace_inline) @font-lock-constant-face)

     :language 'sisu
     :feature 'note
     '((footnote)         @sisu-spine-ts-footnote-face
       (footnote_marker)  @sisu-spine-ts-marker-face
       (editor_note)      @sisu-spine-ts-footnote-face)

     :language 'sisu
     :feature 'link
     '((link text: (link_text) @sisu-spine-ts-link-face)
       (link target: (url) @sisu-spine-ts-link-face)
       (link target: (anchor_ref) @sisu-spine-ts-link-face)
       (link target: (collection_path) @sisu-spine-ts-link-face)
       (image spec: (image_spec) @sisu-spine-ts-link-face)
       (auto_footnote_marker) @sisu-spine-ts-marker-face
       (inline_anchor) @font-lock-function-name-face
       (anchor_name)   @font-lock-function-name-face)

     :language 'sisu
     :feature 'index
     '((book_index)    @sisu-spine-ts-book-index-face
       (index_content) @font-lock-string-face)

     :language 'sisu
     :feature 'misc
     '((paragraph_prefix) @sisu-spine-ts-marker-face
       (page_break)       @sisu-spine-ts-marker-face
       (horizontal_rule)  @sisu-spine-ts-marker-face
       (line_break)       @sisu-spine-ts-marker-face
       (ocn_suppress)        @font-lock-comment-face
       (ocn_suppress_open)   @font-lock-comment-face
       (ocn_suppress_close)  @font-lock-comment-face
       (composite_include) @font-lock-preprocessor-face
       (include_path)      @font-lock-string-face)))
  "Tree-sitter font-lock rules for `sisu-spine-ts-mode'.")

;; ---------------------------------------------------------------------
;; Imenu / navigation / things
;; ---------------------------------------------------------------------

(defvar sisu-spine-ts--imenu-settings
  '(("Part headings"
     "\\`heading_part\\'"
     nil
     sisu-spine-ts--imenu-name-part)
    ("Segment headings"
     "\\`heading_segment\\'"
     nil
     sisu-spine-ts--imenu-name-segment))
  "`treesit-simple-imenu-settings' for `sisu-spine-ts-mode'.")

(defun sisu-spine-ts--imenu-name-part (node)
  "Return display name for a heading_part NODE."
  (let ((c (treesit-node-child-by-field-name node "content"))
        (m (treesit-node-child-by-field-name node "marker")))
    (concat (and m (treesit-node-text m t)) " "
            (and c (treesit-node-text c t)))))

(defun sisu-spine-ts--imenu-name-segment (node)
  "Return display name for a heading_segment NODE."
  (let ((c (treesit-node-child-by-field-name node "content"))
        (m (treesit-node-child-by-field-name node "marker")))
    (concat (and m (treesit-node-text m t)) " "
            (and c (treesit-node-text c t)))))

(defvar sisu-spine-ts--thing-settings
  '((sisu
     (defun "\\`heading_\\(part\\|segment\\)\\'")
     (sentence "\\`paragraph\\'")
     (text "\\`\\(text\\|raw_content\\|heading_content\\)\\'")))
  "`treesit-thing-settings' for `sisu-spine-ts-mode'.")

;; ---------------------------------------------------------------------
;; Grammar install helper
;; ---------------------------------------------------------------------

;;;###autoload
(defun sisu-spine-ts-install-grammar ()
  "Register and install the tree-sitter-sisu grammar.
Convenience wrapper around `treesit-install-language-grammar' with the
upstream URL and source directory pre-filled."
  (interactive)
  (unless (boundp 'treesit-language-source-alist)
    (user-error "treesit not available; Emacs 29+ required"))
  (add-to-list 'treesit-language-source-alist
               '(sisu "https://git.sisudoc.org/projects"
                      :source-dir "tree-sitter-sisu/src"))
  (treesit-install-language-grammar 'sisu))

;; ---------------------------------------------------------------------
;; Major mode
;; ---------------------------------------------------------------------

;;;###autoload
(define-derived-mode sisu-spine-ts-mode text-mode "SiSU-Spine[ts]"
  "Major mode for SiSU spine markup, backed by tree-sitter."
  (unless (and (fboundp 'treesit-ready-p) (treesit-ready-p 'sisu))
    (user-error
     "tree-sitter-sisu parser not installed; run M-x sisu-spine-ts-install-grammar"))
  (treesit-parser-create 'sisu)

  ;; Comments
  (setq-local comment-start "% "
              comment-end   ""
              comment-start-skip "%[ \t]+")

  ;; Font-lock
  (setq-local treesit-font-lock-settings sisu-spine-ts--font-lock-settings)
  (setq-local treesit-font-lock-feature-list
              '((comment header heading)
                (block inline note link index)
                (misc)
                ()))

  ;; Imenu / navigation
  (setq-local treesit-simple-imenu-settings sisu-spine-ts--imenu-settings)
  (setq-local treesit-thing-settings sisu-spine-ts--thing-settings)
  (setq-local treesit-defun-type-regexp "\\`heading_\\(part\\|segment\\)\\'")
  (setq-local treesit-defun-name-function
              (lambda (node)
                (let ((c (treesit-node-child-by-field-name node "content")))
                  (and c (treesit-node-text c t)))))

  (treesit-major-mode-setup))

;;;###autoload
(when (fboundp 'treesit-ready-p)
  (dolist (ext '("\\.sst\\'" "\\.ssm\\'" "\\.ssi\\'"))
    ;; Prefer the ts mode iff the parser is installed; otherwise fall
    ;; back to `sisu-spine-mode'.
    (add-to-list 'auto-mode-alist
                 (cons ext
                       (lambda ()
                         (if (treesit-ready-p 'sisu t)
                             (sisu-spine-ts-mode)
                           (sisu-spine-mode)))))))

(provide 'sisu-spine-ts-mode)

;;; sisu-spine-ts-mode.el ends here