blob: 7b6cf5d572cb6aa5031e586bf6573fca7477af36 (
about) (
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
|
(defvar org-fc-type-cloze-max-hole-property "FC_CLOZE_MAX")
(defvar org-fc-type-cloze-type-property "FC_CLOZE_TYPE")
;; NOTE: The context type is not implemented yet
(defvar org-fc-type-cloze-types
'(deletion enumeration context single))
(defvar org-fc-type-cloze--overlays '())
(defvar org-fc-type-cloze-context 1
"Number of surrounding cards to show for 'context' type cards")
(defface org-fc-type-cloze-hole-face
'((t (:bold t)))
"Face for org-fc cloze card holes."
:group 'org-fc)
(defvar org-fc-type-cloze-hole-re
(rx
(seq
"{{"
(group-n 1 (* (or (seq "$" (+ (not (any "$"))) "$")
(not (any "}"))))) "}"
(? (seq "{" (group-n 2 (* (or
(seq "$" (not (any "$")) "$")
(not (any "}"))))) "}"))
(? "@" (group-n 3 (+ digit)))
"}"))
"Regexp for a cloze hole without an id.")
(defvar org-fc-type-cloze-id-hole-re
(rx
(seq
"{{"
(group-n 1 (* (or (seq "$" (+ (not (any "$"))) "$")
(not (any "}"))))) "}"
(? (seq "{" (group-n 2 (* (or
(seq "$" (not (any "$")) "$")
(not (any "}"))))) "}"))
(seq "@" (group-n 3 (+ digit)))
"}"))
"Regexp for a cloze hole with an id.")
(defun org-fc-type-cloze-max-hole-id ()
(let ((max-id (org-entry-get (point) org-fc-type-cloze-max-hole-property)))
(if max-id
(string-to-number max-id)
-1)))
(defun org-fc-type-cloze-hole (deletion)
"Generate the string used to mark the hole left by DELETION"
(format "[%s...]" (or (plist-get deletion :hint) "")))
;; NOTE: The way parts of the hole are hidden / revealed is probably
;; unnecessarily complicated. I couldn't get latex / org text emphasis
;; to work otherwise. If the hole has no hint, we can't use any
;; properties of match 2.
(defun org-fc-type-cloze--overlay-current ()
"Generate a list of overlays to display the hole currently
being reviewed."
(if (match-beginning 2)
(list
:before-text
(org-fc-hide-region hole-beg (match-beginning 1))
:text
(org-fc-hide-region (match-beginning 1) (match-end 1))
:separator
(org-fc-hide-region (match-end 1) (match-beginning 2) "[...")
:hint
(org-fc-overlay-region (match-beginning 2) (match-end 2))
:after-hint
(org-fc-hide-region (match-end 2) hole-end "]"))
(list
:before-text
(org-fc-hide-region hole-beg (match-beginning 1))
:text
(org-fc-hide-region (match-beginning 1) (match-end 1))
:hint
(org-fc-hide-region (match-end 1) hole-end "[...]"))))
(defun org-fc-type-cloze-hide-holes (hole type)
(save-excursion
(org-fc-goto-entry-heading)
(let* ((el (org-element-at-point))
(end (org-element-property :contents-end el))
(overlays nil)
(seen-current nil))
(while (re-search-forward org-fc-type-cloze-id-hole-re end t)
(let ((text (match-string 1))
(hint (match-string 2))
(id (string-to-number (match-string 3)))
(hole-beg (match-beginning 0))
(hole-end (match-end 0)))
(if (= hole id)
(progn (setq overlays (org-fc-type-cloze--overlay-current))
(setq seen-current t))
(case type
('enumeration
(if seen-current
(org-fc-hide-region hole-beg hole-end "...")
(progn
(org-fc-hide-region hole-beg (match-beginning 1))
(org-fc-hide-region (match-end 1) hole-end))))
('deletion
(progn
(org-fc-hide-region hole-beg (match-beginning 1))
(org-fc-hide-region (match-end 1) hole-end)))
('single
(org-fc-hide-region hole-beg hole-end "..."))
(t (error "org-fc: Unknown cloze card type %s" type))))))
overlays)))
(defun org-fc-type-cloze-flip ()
(if-let ((overlays org-fc-type-cloze--overlays))
(progn
(if (plist-member overlays :separator)
(org-fc-hide-overlay (plist-get overlays :separator)))
(if (plist-member overlays :after-hint)
(org-fc-hide-overlay (plist-get overlays :after-hint)))
(org-fc-hide-overlay (plist-get overlays :hint))
;; (delete-overlay (plist-get overlays :text))
(org-fc-show-overlay
(plist-get overlays :text)
'org-fc-type-cloze-hole-face)))
(org-fc-review-rate-hydra/body))
(defun org-fc-type-cloze-setup (position)
(let ((hole (string-to-number position))
(cloze-type (intern (org-entry-get (point) org-fc-type-cloze-type-property))))
(org-show-subtree)
(setq
org-fc-type-cloze--overlays
(org-fc-type-cloze-hide-holes hole cloze-type)))
(org-fc-review-flip-hydra/body))
(defun org-fc-type-cloze-read-type ()
(intern
(completing-read
"Cloze Type: "
org-fc-type-cloze-types)))
(defun org-fc-type-cloze-init (type)
"Initialize the current heading for use as a cloze card of subtype TYPE.
Processes all holes in the card text."
(interactive (list (org-fc-type-cloze-read-type)))
(unless (member type org-fc-type-cloze-types)
(error "Invalid cloze card type: %s" type))
(org-fc--init-card "cloze")
(org-fc-type-cloze-update)
(org-set-property
org-fc-type-cloze-type-property
(format "%s" type)))
(defun org-fc-type-cloze-update ()
"Update the review data & deletions of the current heading."
(let* ((el (org-element-at-point))
(end (org-element-property :contents-end el))
(hole-id (1+ (org-fc-type-cloze-max-hole-id)))
ids)
(save-excursion
(while (re-search-forward org-fc-type-cloze-hole-re end t)
(let ((id (match-string 3))
(hole-end (match-end 0)))
(unless id
(setq id hole-id)
(incf hole-id 1)
(let ((id-str (number-to-string id)))
(incf end (+ 1 (length id-str)))
(goto-char hole-end)
(backward-char)
(insert "@" id-str)))
(push (format "%s" id) ids))))
(org-set-property
org-fc-type-cloze-max-hole-property
(format "%s" (1- hole-id)))
(org-fc-review-data-update (reverse ids))))
(org-fc-register-type
'cloze
'org-fc-type-cloze-setup
'org-fc-type-cloze-flip
'org-fc-type-cloze-update)
(provide 'org-fc-type-cloze)
|