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
|
;;; gloss.el --- Glossary lookup with online-sourced selection -*- lexical-binding: t -*-
;; Copyright (C) 2026 Craig Jennings
;; Author: Craig Jennings <c@cjennings.net>
;; Created: 28 Apr 2026
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (org "9.3"))
;; Keywords: glossary dictionary terms vocabulary
;; URL: https://github.com/cjennings/gloss
;; 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 of the License, or
;; (at your option) any later version.
;;
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; gloss — Glossary Lookup with Online-Sourced Selection.
;;
;; A personal Emacs glossary on `C-h g'. Looks up terms in a single
;; git-tracked org file. On a local miss, fetches candidate
;; definitions from Wiktionary and prompts the user to pick one to
;; save with provenance. The same org file feeds `org-drill' for
;; spaced-repetition study.
;;
;; Quick start:
;; (require 'gloss)
;; (gloss-install-prefix) ; binds C-h g
;;
;; Default keys under C-h g:
;; g gloss-lookup lookup term (default: word at point)
;; a gloss-add add term manually
;; e gloss-edit edit term in source file
;; o gloss-fetch-online force online fetch
;; D gloss-drill-export tag entries for org-drill
;; l gloss-list-terms browse glossary
;; s gloss-stats summary
;; r gloss-reload refresh cache
;; d gloss-toggle-debug toggle *gloss-debug*
;;
;; See `docs/design/gloss.org' for the full design.
;;; Code:
(require 'gloss-core)
(require 'gloss-fetch)
(require 'gloss-display)
(require 'gloss-drill)
;; The `gloss' defgroup, `gloss-file', and `gloss-debug' defcustoms live in
;; `gloss-core' so they are defined whenever the data layer is required —
;; tests load `gloss-core' directly without pulling in the orchestration here.
(defvar gloss-prefix-map (make-sparse-keymap)
"Keymap for `gloss' commands. Default prefix: C-h g.")
(defun gloss--orchestrate-fetch-result (result)
"Return decision symbol for RESULT plist from `gloss-fetch-definitions'.
Decision values:
:auto-save — exactly one definition.
:pick — two or more definitions.
:error-no-defs — no defs and only :no-defs sources (or all empty).
:error-failed — no defs and only :failed sources.
:error-mixed — no defs but BOTH :no-defs and :failed populated."
(let ((defs (plist-get result :defs))
(no-defs (plist-get result :no-defs))
(failed (plist-get result :failed)))
(cond
((= (length defs) 1) :auto-save)
((> (length defs) 1) :pick)
((and no-defs failed) :error-mixed)
(failed :error-failed)
(t :error-no-defs))))
(defun gloss--lookup-flow (term &optional force-fetch)
"Look up TERM in the glossary. Fetch online on miss.
If FORCE-FETCH is non-nil, bypass the cache and fetch unconditionally.
Returns a symbol naming the action taken: :show, :auto-save, :pick,
:error-no-defs, :error-failed, or :error-mixed."
(let ((cached (and (not force-fetch) (gloss-core-lookup term))))
(if cached
(progn
(gloss-display-show-entry term (plist-get cached :body))
:show)
(let* ((result (gloss-fetch-definitions term))
(action (gloss--orchestrate-fetch-result result)))
(pcase action
(:auto-save
(let* ((def (car (plist-get result :defs)))
(text (plist-get def :text))
(source (plist-get def :source)))
(gloss-core-save term text source 'replace)
(gloss-display-show-entry term text)))
(:pick
(when-let* ((chosen (gloss-display-pick-definition
term (plist-get result :defs)))
(text (plist-get chosen :text))
(source (plist-get chosen :source)))
(gloss-core-save term text source 'replace)
(gloss-display-show-entry term text)))
(:error-no-defs
(message "gloss: no definition found for %s" term))
(:error-failed
(message "gloss: couldn't reach any source for %s" term))
(:error-mixed
(message "gloss: no definition in some sources, others unreachable for %s"
term)))
action))))
;;;###autoload
(defun gloss-lookup (term)
"Look up TERM in the glossary; fetch online on miss."
(interactive (list (read-string "Glossary lookup: " (thing-at-point 'word t))))
(gloss--lookup-flow term))
;;;###autoload
(defun gloss-add (term)
"Add TERM to the glossary manually."
(interactive (list (read-string "Add term: ")))
(ignore term)
(user-error "gloss-add: not yet implemented"))
;;;###autoload
(defun gloss-edit (term)
"Open the source org file at TERM's heading."
(interactive (list (read-string "Edit term: ")))
(ignore term)
(user-error "gloss-edit: not yet implemented"))
;;;###autoload
(defun gloss-fetch-online (term)
"Force online fetch for TERM, bypassing the cache."
(interactive (list (read-string "Fetch online: " (thing-at-point 'word t))))
(gloss--lookup-flow term t))
;;;###autoload
(defun gloss-drill-export ()
"Tag every entry as :drill: for `org-drill'."
(interactive)
(user-error "gloss-drill-export: not yet implemented"))
;;;###autoload
(defun gloss-list-terms ()
"Browse glossary terms via `completing-read'."
(interactive)
(user-error "gloss-list-terms: not yet implemented"))
;;;###autoload
(defun gloss-stats ()
"Summarize the glossary state."
(interactive)
(user-error "gloss-stats: not yet implemented"))
;;;###autoload
(defun gloss-reload ()
"Force reload of the glossary cache from disk."
(interactive)
(user-error "gloss-reload: not yet implemented"))
;;;###autoload
(defun gloss-toggle-debug ()
"Toggle the *gloss-debug* log on or off."
(interactive)
(setq gloss-debug (not gloss-debug))
(message "gloss-debug %s" (if gloss-debug "enabled" "disabled")))
(define-key gloss-prefix-map (kbd "g") #'gloss-lookup)
(define-key gloss-prefix-map (kbd "a") #'gloss-add)
(define-key gloss-prefix-map (kbd "e") #'gloss-edit)
(define-key gloss-prefix-map (kbd "o") #'gloss-fetch-online)
(define-key gloss-prefix-map (kbd "D") #'gloss-drill-export)
(define-key gloss-prefix-map (kbd "l") #'gloss-list-terms)
(define-key gloss-prefix-map (kbd "s") #'gloss-stats)
(define-key gloss-prefix-map (kbd "r") #'gloss-reload)
(define-key gloss-prefix-map (kbd "d") #'gloss-toggle-debug)
;;;###autoload
(defun gloss-install-prefix (&optional key)
"Install `gloss-prefix-map' on KEY (default \\`g' under `help-map', i.e. C-h g)."
(interactive)
(define-key help-map (or key (kbd "g")) gloss-prefix-map))
(provide 'gloss)
;;; gloss.el ends here
|