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
297
298
|
;;; gloss-core.el --- Data layer for gloss -*- lexical-binding: t -*-
;; Copyright (C) 2026 Craig Jennings
;; Author: Craig Jennings <c@cjennings.net>
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; Storage and lookup primitives for `gloss'. Owns the in-memory
;; cache (a hash table keyed by term) and the org file I/O.
;;
;; Public API:
;; `gloss-core-lookup' TERM -> entry plist or nil
;; `gloss-core-save' TERM BODY SOURCE -> entry plist (saved)
;; `gloss-core-list' -> (TERM ...)
;; `gloss-core-find-buffer-position' TERM -> marker
;;
;; Entry plist shape:
;; (:term "anaphora"
;; :body "Reference to..."
;; :source wiktionary
;; :added "2026-04-28"
;; :marker #<marker at N in gloss.org>)
;;
;; The cache is loaded lazily on the first lookup of the session and
;; refreshed automatically when `gloss-file's mtime advances past the
;; last load time (catching out-of-band edits — other Emacs sessions,
;; git pull, hand edits, sed). A parse failure during reload preserves
;; the existing cache and surfaces a one-line message; the next lookup
;; will retry.
;;; Code:
(require 'org)
(require 'subr-x)
(defgroup gloss nil
"Personal glossary with online-sourced definitions."
:group 'tools
:prefix "gloss-")
(defcustom gloss-file
(expand-file-name "gloss.org" (or (bound-and-true-p org-directory)
user-emacs-directory))
"Path to the glossary org file."
:type 'file
:group 'gloss)
(defcustom gloss-debug nil
"When non-nil, write diagnostic events to *gloss-debug*."
:type 'boolean
:group 'gloss)
(defvar gloss-core--cache nil
"Hash table mapping TERM (string) to entry plist, or nil if cold.")
(defvar gloss-core--cache-mtime nil
"Mtime of `gloss-file' at last successful cache load, or nil.")
(defun gloss-core--cache-reset ()
"Reset the in-memory cache. Used by tests and `gloss-reload'."
(setq gloss-core--cache nil
gloss-core--cache-mtime nil))
(defun gloss-core--extract-body ()
"Extract the body text of the org entry at point.
Point should be on the heading line. Skips the heading itself, any
planning line, and the properties drawer. Trims trailing whitespace."
(save-excursion
(org-back-to-heading t)
(forward-line 1)
(when (looking-at-p "^[ \t]*\\(?:DEADLINE\\|SCHEDULED\\|CLOSED\\):")
(forward-line 1))
(when (looking-at-p "^[ \t]*:PROPERTIES:")
(re-search-forward "^[ \t]*:END:[ \t]*$" nil t)
(forward-line 1))
(let ((start (point))
(end (save-excursion
(if (re-search-forward "^\\* " nil t)
(line-beginning-position)
(point-max)))))
(string-trim (buffer-substring-no-properties start end)))))
(defun gloss-core--parse-file-into-cache ()
"Read `gloss-file' and return a fresh cache populated from its entries.
Each entry plist has :term, :body, :source, :added, :marker."
(let ((cache (make-hash-table :test 'equal))
(buf (find-file-noselect gloss-file)))
(with-current-buffer buf
;; Pick up out-of-band changes (other Emacs sessions, git pull,
;; manual edits, sed) before parsing.
(unless (verify-visited-file-modtime buf)
(revert-buffer t t t))
(unless (derived-mode-p 'org-mode)
(let ((org-mode-hook nil))
(org-mode)))
(save-excursion
(goto-char (point-min))
(org-map-entries
(lambda ()
(when (= 1 (org-current-level))
(let* ((term (substring-no-properties
(org-get-heading t t t t)))
(heading-marker (point-marker))
(source (org-entry-get nil "SOURCE"))
(added (org-entry-get nil "ADDED"))
(body (gloss-core--extract-body)))
(puthash term
(list :term term
:body body
:source (and source (intern source))
:added added
:marker heading-marker)
cache)))))))
cache))
(defun gloss-core--cache-ensure ()
"Ensure the cache reflects current `gloss-file' contents.
On parse failure, preserve the existing cache and surface a message."
(cond
((not (file-exists-p gloss-file))
(gloss-core--cache-reset))
(t
(let ((mtime (file-attribute-modification-time
(file-attributes gloss-file))))
(when (or (null gloss-core--cache)
(null gloss-core--cache-mtime)
(time-less-p gloss-core--cache-mtime mtime))
(condition-case err
(let ((new-cache (gloss-core--parse-file-into-cache)))
(setq gloss-core--cache new-cache
gloss-core--cache-mtime mtime))
(error
(message "gloss: glossary file appears corrupt (%s); cache not refreshed"
(error-message-string err)))))))))
(defun gloss-core--cache-ensure-or-init ()
"Ensure the cache is loaded. Create `gloss-file' if missing."
(unless (file-exists-p gloss-file)
(let ((dir (file-name-directory gloss-file)))
(when (and dir (not (file-exists-p dir)))
(make-directory dir t)))
(with-temp-file gloss-file
(insert "#+TITLE: Glossary\n#+STARTUP: showall\n\n")))
(gloss-core--cache-ensure)
(unless gloss-core--cache
(setq gloss-core--cache (make-hash-table :test 'equal)
gloss-core--cache-mtime (file-attribute-modification-time
(file-attributes gloss-file)))))
(defun gloss-core--find-insertion-point (term)
"Return the buffer position where TERM should be inserted alphabetically.
Compares case-insensitively against existing top-level headings. Returns
`point-max' if all existing headings sort before TERM."
(save-excursion
(goto-char (point-min))
(let ((target-down (downcase term))
(insert-point nil))
(while (and (not insert-point)
(re-search-forward "^\\* \\(.*\\)$" nil t))
(when (string-greaterp (downcase (match-string-no-properties 1))
target-down)
(setq insert-point (match-beginning 0))))
(or insert-point (point-max)))))
(defun gloss-core--format-entry (term body source added)
"Return the org-formatted text for an entry. Always ends with a blank line."
(format "* %s\n:PROPERTIES:\n:SOURCE: %s\n:ADDED: %s\n:END:\n%s\n\n"
term
(if source (symbol-name source) "manual")
added
body))
(defun gloss-core--insert-entry (term body source)
"Insert a new entry into `gloss-file' at the alphabetical position.
Return the entry plist."
(let ((added (format-time-string "%Y-%m-%d"))
(buf (find-file-noselect gloss-file)))
(with-current-buffer buf
(save-excursion
(goto-char (gloss-core--find-insertion-point term))
(insert (gloss-core--format-entry term body source added)))
(save-buffer))
(gloss-core--cache-reset)
(gloss-core--cache-ensure)
(gethash term gloss-core--cache)))
(defun gloss-core--goto-heading (term)
"Move point in the current buffer to the start of TERM's heading line."
(goto-char (point-min))
(unless (re-search-forward (format "^\\* %s$" (regexp-quote term)) nil t)
(error "gloss-core: term %S not found in glossary" term))
(beginning-of-line))
(defun gloss-core--delete-current-entry ()
"Delete the org entry starting at point through the next heading or EOF.
Point should be on the heading line."
(let ((start (point))
(end (save-excursion
(forward-line 1)
(if (re-search-forward "^\\* " nil t)
(line-beginning-position)
(point-max)))))
(delete-region start end)))
(defun gloss-core--replace-entry (term body source)
"Replace TERM's existing entry with new BODY and SOURCE. Updates ADDED to today."
(let ((added (format-time-string "%Y-%m-%d"))
(buf (find-file-noselect gloss-file)))
(with-current-buffer buf
(save-excursion
(gloss-core--goto-heading term)
(gloss-core--delete-current-entry)
(goto-char (gloss-core--find-insertion-point term))
(insert (gloss-core--format-entry term body source added)))
(save-buffer))
(gloss-core--cache-reset)
(gloss-core--cache-ensure)
(gethash term gloss-core--cache)))
(defun gloss-core--append-entry-body (term additional-body _source)
"Append ADDITIONAL-BODY to TERM's existing entry, separated by a blank line.
The original SOURCE is preserved (the new SOURCE arg is ignored)."
(let* ((existing (gethash term gloss-core--cache))
(combined-body (concat (plist-get existing :body)
"\n\n"
additional-body))
(combined-source (plist-get existing :source)))
(gloss-core--replace-entry term combined-body combined-source)))
(defun gloss-core--prompt-collision (term)
"Prompt the user about a save collision on TERM.
Return one of \\='replace, \\='append, \\='cancel."
(let ((choice (completing-read
(format "Term %S already exists. Action: " term)
'("Replace" "Append" "Cancel")
nil t nil nil "Cancel")))
(pcase choice
("Replace" 'replace)
("Append" 'append)
(_ 'cancel))))
;;;; Public API
(defun gloss-core-lookup (term)
"Look up TERM in the glossary. Return the entry plist or nil."
(when (and term (stringp term) (not (string-empty-p term)))
(gloss-core--cache-ensure)
(and gloss-core--cache (gethash term gloss-core--cache))))
(defun gloss-core-save (term body source &optional collision-action)
"Save TERM with BODY and SOURCE to the glossary.
COLLISION-ACTION is one of \\='replace, \\='append, \\='cancel. If TERM
exists and COLLISION-ACTION is nil, prompt the user. Return the saved
entry plist, or nil on cancel."
(unless (and term (stringp term)
(not (string-empty-p (string-trim (or term "")))))
(user-error "gloss-core-save: term must be a non-empty string"))
(unless (and body (stringp body)
(not (string-empty-p (string-trim (or body "")))))
(user-error "gloss-core-save: body must be a non-empty string"))
(gloss-core--cache-ensure-or-init)
(let* ((existing (gethash term gloss-core--cache))
(action (or collision-action
(and existing (gloss-core--prompt-collision term)))))
(cond
((null existing)
(gloss-core--insert-entry term body source))
((eq action 'replace)
(gloss-core--replace-entry term body source))
((eq action 'append)
(gloss-core--append-entry-body term body source))
((eq action 'cancel) nil))))
(defun gloss-core-list ()
"Return all glossary terms in case-insensitive alphabetical order, or nil."
(gloss-core--cache-ensure)
(when gloss-core--cache
(let (terms)
(maphash (lambda (k _v) (push k terms)) gloss-core--cache)
(when terms
(sort terms (lambda (a b)
(string-lessp (downcase a) (downcase b))))))))
(defun gloss-core-find-buffer-position (term)
"Return a marker at TERM's heading position in `gloss-file', or nil if missing."
(when-let* ((entry (gloss-core-lookup term))
(marker (plist-get entry :marker)))
(if (and (marker-buffer marker)
(buffer-live-p (marker-buffer marker)))
marker
;; Buffer was killed; force a reload and try once more.
(gloss-core--cache-reset)
(when-let ((entry2 (gloss-core-lookup term)))
(plist-get entry2 :marker)))))
(provide 'gloss-core)
;;; gloss-core.el ends here
|