Repository navigation
Expand file tree
/
Copy pathpoly-tiddlywiki.el
More file actions
245 lines (214 loc) · 9.03 KB
/
Copy pathpoly-tiddlywiki.el
File metadata and controls
245 lines (214 loc) · 9.03 KB
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
;;; poly-tiddlywiki.el --- Polymode support for TiddlyWiki -*- lexical-binding: t; -*-
;; Copyright (C) 2026
;; Author: Your Name
;; Keywords: languages, tiddlywiki, polymode
;;; Commentary:
;; This file provides polymode support for TiddlyWiki mode, enabling
;; proper syntax highlighting and indentation for code blocks.
;;
;; To use, add to your config:
;; (require 'poly-tiddlywiki)
;;
;; Then use `poly-tiddlywiki-mode' instead of `tiddlywiki-mode'.
;; You can set this automatically:
;; (add-to-list 'auto-mode-alist '("\\.tid\\'" . poly-tiddlywiki-mode))
;;; Code:
(require 'polymode)
(require 'tiddlywiki-mode)
;;; ============================================================
;;; Configuration
;;; ============================================================
(defcustom tiddlywiki-code-block-run-hooks nil
"If non-nil, run mode hooks in code blocks.
When nil (the default), code blocks use lightweight initialization
that provides syntax highlighting and indentation without running
mode hooks. This prevents heavy operations like LSP from starting
for each code block.
Set to non-nil if you want full mode functionality in code blocks."
:type 'boolean
:group 'tiddlywiki)
(defcustom tiddlywiki-code-block-hooks-blacklist
'(lsp
lsp-deferred
lsp-mode
eglot-ensure
cargo-minor-mode
flycheck-mode
flymake-mode
company-mode
corfu-mode
yas-minor-mode
yas-minor-mode-on
tree-sitter-mode
tree-sitter-hl-mode
copilot-mode)
"List of hook functions that should NOT run in code blocks.
When `tiddlywiki-code-block-run-hooks' is nil, functions in this
list will be blocked from mode hooks. All other hooks will run normally.
This is useful to prevent heavy operations like LSP from starting
for each code block while still allowing lightweight hooks like
`show-paren-mode' or `electric-pair-local-mode'."
:type '(repeat function)
:group 'tiddlywiki)
(defcustom tiddlywiki-code-block-default-mode 'prog-mode
"Default mode for code blocks without a specified language."
:type 'symbol
:group 'tiddlywiki)
;;; ============================================================
;;; Language to Mode Mapping
;;; ============================================================
(defcustom tiddlywiki-language-mode-alist
'(("elisp" . emacs-lisp-mode)
("emacs-lisp" . emacs-lisp-mode)
("python" . python-mode)
("julia" . julia-mode)
("javascript" . js-mode)
("js" . js-mode)
("typescript" . typescript-mode)
("ts" . typescript-mode)
("ruby" . ruby-mode)
("rust" . rust-mode)
("go" . go-mode)
("c" . c-mode)
("cpp" . c++-mode)
;; relint suppression: Repetition
("c++" . c++-mode)
("java" . java-mode)
("html" . html-mode)
("css" . css-mode)
("json" . json-mode)
("yaml" . yaml-mode)
("yml" . yaml-mode)
("xml" . xml-mode)
("sql" . sql-mode)
("sh" . sh-mode)
("bash" . sh-mode)
("shell" . sh-mode)
("lisp" . lisp-mode)
("scheme" . scheme-mode)
("clojure" . clojure-mode)
("clj" . clojure-mode)
("haskell" . haskell-mode)
("hs" . haskell-mode)
("lua" . lua-mode)
("perl" . perl-mode)
("php" . php-mode)
("r" . r-mode)
("scala" . scala-mode)
("swift" . swift-mode)
("kotlin" . kotlin-mode)
("markdown" . markdown-mode)
("md" . markdown-mode)
("org" . org-mode)
("latex" . latex-mode)
("tex" . tex-mode))
"Alist mapping language names to Emacs major modes.
Each entry is (LANGUAGE . MODE) where LANGUAGE is the string used
after ``` in code blocks and MODE is the corresponding major mode."
:type '(alist :key-type string :value-type symbol)
:group 'tiddlywiki)
;;; ============================================================
;;; Language Resolution
;;; ============================================================
(defun tiddlywiki-polymode--get-mode-symbol (name)
"Get mode symbol for language NAME using `tiddlywiki-language-mode-alist'.
Returns nil if no mode is found."
(when (and name (stringp name) (> (length name) 0))
(or (cdr (assoc (downcase name) tiddlywiki-language-mode-alist))
(let ((mode-sym (intern (concat (downcase name) "-mode"))))
(when (fboundp mode-sym)
mode-sym)))))
;; Advice to use our custom language-to-mode mapping
(defun tiddlywiki-polymode--around-get-mode-symbol (orig-fun name &optional fallback)
"Advice for `pm-get-mode-symbol-from-name' to use our language alist.
ORIG-FUN is the original function, NAME is the language name,
FALLBACK is the optional fallback mode."
(if (and name (stringp name) (> (length name) 0))
(or (tiddlywiki-polymode--get-mode-symbol name)
(funcall orig-fun name fallback))
;; If name is nil or empty, return fallback or default
(or fallback tiddlywiki-code-block-default-mode)))
(advice-add 'pm-get-mode-symbol-from-name
:around #'tiddlywiki-polymode--around-get-mode-symbol)
;;; ============================================================
;;; Hook Management - Disable mode hooks in inner buffers
;;; ============================================================
;; We use Emacs's built-in delay-mode-hooks mechanism to prevent
;; mode hooks from running in polymode inner buffers.
;; We use Emacs's built-in delay-mode-hooks mechanism to prevent
;; mode hooks from running in polymode inner buffers.
;; When delay-mode-hooks is t, run-mode-hooks stores hooks in
;; delayed-mode-hooks instead of running them. We then run only
;; the whitelisted functions and discard the rest.
(defun tiddlywiki-polymode--run-filtered-hooks ()
"Run delayed mode hooks, filtering out blacklisted functions.
Goes through `delayed-mode-hooks', and for each hook runs all
functions EXCEPT those in `tiddlywiki-code-block-hooks-blacklist'."
(dolist (hook delayed-mode-hooks)
(when (boundp hook)
(dolist (func (symbol-value hook))
(when (and (functionp func)
(not (memq func tiddlywiki-code-block-hooks-blacklist)))
(ignore-errors
(funcall func)))))))
(defun tiddlywiki-polymode--around-pm-mode-setup (orig-fun mode &optional buffer)
"Advice around `pm--mode-setup' to inhibit hooks for tiddlywiki inner buffers.
ORIG-FUN is the original function, MODE is the mode to setup, BUFFER is optional."
(if (and (not tiddlywiki-code-block-run-hooks)
;; Check if we're setting up an inner buffer (indirect buffer)
(buffer-base-buffer (or buffer (current-buffer)))
;; Check if base buffer uses tiddlywiki-mode
(with-current-buffer (buffer-base-buffer (or buffer (current-buffer)))
(or (eq major-mode 'tiddlywiki-mode)
(and (boundp 'polymode-mode) polymode-mode
(boundp 'pm/polymode) pm/polymode
(let ((hostmode (ignore-errors (eieio-oref pm/polymode 'hostmode))))
(and hostmode
(eq (ignore-errors (eieio-oref hostmode 'mode))
'tiddlywiki-mode)))))))
;; Inhibit hooks by using delay-mode-hooks
;; We use setq-default to modify the global value, avoiding the
;; "buffer-local while locally let-bound" warning that occurs with let
(let ((old-delay-mode-hooks (default-value 'delay-mode-hooks)))
(setq-default delay-mode-hooks t)
(unwind-protect
(prog1 (funcall orig-fun mode buffer)
;; Run hooks except blacklisted ones
(tiddlywiki-polymode--run-filtered-hooks)
;; Clear delayed hooks
(setq delayed-mode-hooks nil)
;; Ensure font-lock is enabled for syntax highlighting
(when (and (not font-lock-mode)
(derived-mode-p 'prog-mode 'text-mode))
(font-lock-mode 1)))
(setq-default delay-mode-hooks old-delay-mode-hooks)))
;; Normal execution
(funcall orig-fun mode buffer)))
(advice-add 'pm--mode-setup :around #'tiddlywiki-polymode--around-pm-mode-setup)
;;; ============================================================
;;; Polymode Definitions
;;; ============================================================
(define-hostmode poly-tiddlywiki-hostmode
:mode 'tiddlywiki-mode)
(define-auto-innermode poly-tiddlywiki-code-innermode
:head-matcher "^```\\([a-zA-Z0-9_+-]+\\)[ \t]*$"
:tail-matcher "^```[ \t]*$"
:mode-matcher (cons "^```\\([a-zA-Z0-9_+-]+\\)" 1)
:head-mode 'host
:tail-mode 'host
:fallback-mode 'prog-mode)
(define-polymode poly-tiddlywiki-mode
:hostmode 'poly-tiddlywiki-hostmode
:innermodes '(poly-tiddlywiki-code-innermode))
;;; ============================================================
;;; Unload Function
;;; ============================================================
(defun tiddlywiki-polymode-unload-function ()
"Unload function for tiddlywiki-polymode.
Removes advices added by this package."
(advice-remove 'pm--mode-setup #'tiddlywiki-polymode--around-pm-mode-setup)
(advice-remove 'pm-get-mode-symbol-from-name #'tiddlywiki-polymode--around-get-mode-symbol)
;; Return nil to indicate success
nil)
(provide 'poly-tiddlywiki)
;;; poly-tiddlywiki.el ends here