Skip to content

Commit 2f6276a

Browse files
authored
Merge pull request #105 from dakra/89-only-complete-when-all-correct
Add complete-all-correct and trigger complete if all correct
2 parents b87d8d5 + 5943e6b commit 2f6276a

2 files changed

Lines changed: 65 additions & 50 deletions

File tree

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,13 @@ are added if both are set they accumulate each other.
264264
### Completion and Menu
265265

266266
A typing session is considered complete when every character in the
267-
buffer has a `speed-type-char-status` property. Once all characters
268-
have such a value, completion is triggered. Before displaying the
269-
menu, the buffer-local-vars are stored into the stats-file (when
270-
customized).
267+
buffer has a `speed-type-char-status` property (see also
268+
`speed-type-complete-all-correct`). Once all characters have such a
269+
value, completion is triggered. It's also possible to call
270+
`speed-type-complete` interactivly.
271+
272+
Before displaying the menu, the buffer-local-vars are stored into the
273+
stats-file (when customized).
271274

272275
At this point, the `speed-type-buffer` becomes read-only and only the
273276
menu-control keys remain active. The user has the choice to either

speed-type.el

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ the transformed string that is used for the speed type exercise.
121121
E.g. if you always want lowercase words, set:
122122
`speed-type-wordlist-transform' to `downcase'."
123123
:type '(choice (const :tag "None" nil)
124-
(function :tag "Transform function"))
125-
:group 'speed-type)
124+
(function :tag "Transform function")))
126125

127126
(defcustom speed-type-default-lang nil
128127
"Default language for training wordlists. Ask when NIL."
@@ -191,24 +190,21 @@ E.g. if you always want lowercase words, set:
191190
(const :tag "Tagabawa" bgs)
192191
(const :tag "Tagalog" tl)
193192
(const :tag "Telugu" te)
194-
(const :tag "Welsh" cy))
195-
:group 'speed-type)
193+
(const :tag "Welsh" cy)))
196194

197195
(defcustom speed-type-replace-strings '(("" . "\"") ("" . "\"") ("" . "'") ("" . "'") ("" . "-") ("" . "-") ("Æ" . "Ae") ("æ" . "ae") ("»" . "\"") ("«" . "\""))
198196
"Alist of strings to replace and their replacement, in the form:
199197
`(bad-string . good-string)'
200198
To remove without replacement, use the form: `(bad-string . \"\")'"
201-
:type '(alist :key-type string :value-type string)
202-
:group 'speed-type)
199+
:type '(alist :key-type string :value-type string))
203200

204201
(defcustom speed-type-randomize t
205202
"Affects the text-picker when starting speed-type-buffer or speed-type-text.
206203
207204
When non-nil it picks a random portion, otherwise it checks for existing
208205
records to start from. If nothing found will take text-portion from the
209206
beginning."
210-
:type 'boolean
211-
:group 'speed-type)
207+
:type 'boolean)
212208

213209
(defcustom speed-type-downcase nil
214210
"Toggle downcasing of mistyped words."
@@ -220,8 +216,17 @@ beginning."
220216
when point-move (default), moves the point one character further.
221217
222218
when point-stay, stays at the current position until correct character is typed."
223-
:type 'symbol
224-
:group 'speed-type)
219+
:type 'symbol)
220+
221+
(defcustom speed-type-complete-all-correct nil
222+
"This flag controls the behaviour of triggering complete in speed-type session.
223+
224+
When non-nil, will complete if all characters are typed correctly (have
225+
status correct or ignore)
226+
227+
When nil, will complete when all characters have a status (any non-nil
228+
status value)."
229+
:type 'boolean)
225230

226231
(defcustom speed-type-add-extra-words-on-error 0
227232
"How many new words should be added on error.
@@ -1255,9 +1260,16 @@ are color coded and stats are gathered about the typing performance."
12551260
(car (overlays-at end)))))
12561261
(move-overlay overlay (1- (overlay-end overlay)) (overlay-end overlay)) (current-buffer))
12571262
(speed-type--diff orig new-text start end)
1258-
(when (and (not (save-excursion (text-property-search-forward 'speed-type-char-status 'nil t)))
1259-
(not (save-excursion (text-property-search-backward 'speed-type-char-status 'nil t)))
1260-
(not (text-property-any (point-min) (point-max) 'speed-type-char-status 'nil)))
1263+
(when
1264+
(and (not (save-excursion (text-property-search-forward 'speed-type-char-status 'nil t)))
1265+
(not (save-excursion (text-property-search-backward 'speed-type-char-status 'nil t)))
1266+
(null speed-type--extra-words-queue)
1267+
(not (text-property-any (point-min) (point-max) 'speed-type-char-status 'nil))
1268+
(or (not speed-type-complete-all-correct)
1269+
(and speed-type-complete-all-correct
1270+
(not (save-excursion (text-property-search-forward 'speed-type-char-status 'error t)))
1271+
(not (save-excursion (text-property-search-backward 'speed-type-char-status 'error t)))
1272+
(not (text-property-any (point-min) (point-max) 'speed-type-char-status 'error)))))
12611273
(speed-type-complete)))
12621274
(beep)
12631275
(message "End of buffer")))))
@@ -1279,39 +1291,39 @@ and property adjusted. A new string is returned with adjusted
12791291
properties."
12801292
(save-excursion
12811293
(eval `(,(or (and object 'with-temp-buffer) 'with-current-buffer)
1282-
(unless ,object (current-buffer))
1283-
(progn
1284-
(when ,object (insert ,object))
1285-
(dolist (pair ',map)
1286-
(let ((from (car pair))
1287-
(to (cdr pair)))
1288-
(goto-char (point-min))
1289-
(while (search-forward from nil t)
1290-
(let* ((start (match-beginning 0))
1291-
(end (match-end 0))
1292-
(prop-start (previous-single-property-change start ',property speed-type--buffer (point-min)))
1293-
(prop-end (next-single-property-change end ',property speed-type--buffer (point-max)))
1294-
(len-from (length from))
1295-
(len-to (length to))
1296-
(old-property-value (get-text-property start ',property)))
1297-
(replace-match to t t)
1298-
1299-
(cond
1300-
;; TO longer → extend property region
1301-
((> len-to len-from)
1302-
(put-text-property
1303-
prop-start (+ prop-end (- len-to len-from))
1304-
',property old-property-value))
1305-
1306-
;; FROM longer → shrink property region
1307-
((< len-to len-from)
1308-
(let ((new-prop-end (- prop-end (- len-from len-to))))
1309-
(when (> new-prop-end prop-start)
1310-
(put-text-property
1311-
prop-start new-prop-end
1312-
',property old-property-value))))
1313-
(t (put-text-property prop-start prop-end ',property old-property-value)))))))
1314-
(when ,object (buffer-string)))))))
1294+
(unless ,object (current-buffer))
1295+
(progn
1296+
(when ,object (insert ,object))
1297+
(dolist (pair ',map)
1298+
(let ((from (car pair))
1299+
(to (cdr pair)))
1300+
(goto-char (point-min))
1301+
(while (search-forward from nil t)
1302+
(let* ((start (match-beginning 0))
1303+
(end (match-end 0))
1304+
(prop-start (previous-single-property-change start ',property speed-type--buffer (point-min)))
1305+
(prop-end (next-single-property-change end ',property speed-type--buffer (point-max)))
1306+
(len-from (length from))
1307+
(len-to (length to))
1308+
(old-property-value (get-text-property start ',property)))
1309+
(replace-match to t t)
1310+
1311+
(cond
1312+
;; TO longer → extend property region
1313+
((> len-to len-from)
1314+
(put-text-property
1315+
prop-start (+ prop-end (- len-to len-from))
1316+
',property old-property-value))
1317+
1318+
;; FROM longer → shrink property region
1319+
((< len-to len-from)
1320+
(let ((new-prop-end (- prop-end (- len-from len-to))))
1321+
(when (> new-prop-end prop-start)
1322+
(put-text-property
1323+
prop-start new-prop-end
1324+
',property old-property-value))))
1325+
(t (put-text-property prop-start prop-end ',property old-property-value)))))))
1326+
(when ,object (buffer-string)))))))
13151327

13161328
(cl-defun speed-type--setup
13171329
(content-buffer text &key file-name title author lang n-words randomize continue-fn add-extra-word-content-fn replay-fn go-next-fn syntax-table fldf)

0 commit comments

Comments
 (0)