-
Notifications
You must be signed in to change notification settings - Fork 4
/
monkeytype.el
1552 lines (1305 loc) · 56.7 KB
/
monkeytype.el
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; monkeytype.el --- Mode for speed typing -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Pablo Barrantes
;; Author: Pablo Barrantes <xjpablobrx@gmail.com>
;; Maintainer: Pablo Barrantes <xjpablobrx@gmail.com>
;; Version: 0.1.3
;; Keywords: games
;; URL: https://github.com/jpablobr/emacs-monkeytype
;; Package-Requires: ((emacs "25.1") (scrollable-quick-peek "0.1.0"))
;;; Commentary:
;; Emacs Monkeytype is a typing game/tutor inspired by monkeytype.com
;; but for Emacs.
;; Features:
;; - Type any text you want
;; - Practice mistyped words
;; - Optional mode-line live WPM
;; - Pause/Resume/Stop/Save functionality
;; - Visual representation of typed text including errors and
;; retries/corrections
;; - Auto stop after 5 seconds of no input
;; - Optionally randomise practice words/transitions
;; - Optionally downcase practice words/transitions
;; - Optionally treat newlines as whitespace
;; - Optionally text auto-fill
;; - Optionally delete trailing whitespace
;; - Select a region of text and treat it as words for practice
;; - After a test, practice troubling/hard key combinations/transitions
;; - Mistyped words or hard transitions can be saved
;; - Saved mistyped/transitions/text can be loaded for practice
;; - Ability to type most (saved) mistyped words
;; - Text for typing can be saved in the `monkeytype-directory' allowing
;; you to resume later on (even after quitting Emacs).
;; - Download web pages directly to monkeytype-directory directory.
;;; License:
;; This file is NOT part of GNU Emacs.
;;
;; 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 2 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/>.
;;; Code:
(require 'cl-lib)
(require 'seq)
(require 'subr-x)
(require 'json)
(require 'map)
(require 'scrollable-quick-peek)
;;;; Customization
(defgroup monkeytype nil
"Speed/touch typing."
:group 'games
:tag "Monkeytype"
:link '(url-link "https://github.com/jpablobr/emacs-monkeytype"))
(defcustom monkeytype-treat-newline-as-space t
"Allow continuing to the next line by pressing space."
:type 'boolean)
(defcustom monkeytype-insert-log nil
"Show log in results section."
:type 'boolean)
(defcustom monkeytype-minimum-transitions 50
"Minimum amount of transitions to practice."
:type 'integer)
(defcustom monkeytype-mode-line '(:eval (monkeytype--mode-line-text))
"Monkeytype mode-line formatting."
:type 'sexp
:risky t)
(defcustom monkeytype-colored-mode-line t
"Toggle coloured mode-line WPM status."
:type 'boolean)
(defcustom monkeytype-mode-line-interval-update 1
"Number of keystrokes after each mode-line update.
Reducing the frequency of the updates helps reduce lagging on longer
text or when typing too fast."
:type 'integer)
(defcustom monkeytype-word-divisor 5.0
"5 is the most common number for these calculations.
Proper word count doesn't work as well since words have different number
of characters. This also makes calculations easier and more accurate."
:type 'integer)
(defcustom monkeytype-auto-fill nil
"Toggle auto fill for typing text.
Defaults `fill-column' setting
See also: `monkeytype-words-auto-fill'"
:type 'boolean)
(defcustom monkeytype-words-auto-fill t
"Toggle auto fill for words/transitions.
It defaults `fill-column' setting. See: `monkeytype-auto-fill'"
:type 'boolean)
(defcustom monkeytype-delete-trailing-whitespace t
"Toggle delete trailing whitespace."
:type 'boolean)
(defcustom monkeytype-downcase t
"Toggle downcasing of mistyped words."
:type 'boolean)
(defcustom monkeytype-directory "~/.monkeytype/"
"Monkeytype directory."
:type 'string)
(defcustom monkeytype-randomize t
"Toggle randomizing of words."
:type 'boolean)
(defcustom monkeytype-excluded-chars-regexp "[^[:alnum:]']"
"Regexp used for getting valid words."
:type 'string)
(defcustom monkeytype-most-mistyped-amount 100
"Amount of words in most mistyped words test."
:type 'integer)
(defcustom monkeytype-file-name-format "%a-%d-%b-%Y-%H-%M-%S"
"Format for time-stamped files for saving."
:type 'string)
(defcustom monkeytype-asciify t
"ASCII character encode downloaded web-pages.
Iconv(1) must be installed."
:type 'boolean)
;;;; Faces
(defgroup monkeytype-faces nil
"Faces used by Monkeytype."
:group 'monkeytype
:group 'faces)
(defface monkeytype-default
'((t :inherit default))
"Face for text area.")
(defface monkeytype-dimmed
'((((class color) (background light)) :foreground "#999999")
(((class color) (background dark)) :foreground "#888888"))
"Face for correctly typed char.")
(defface monkeytype-notice
'((((class color) (background light)) :foreground "#B79508")
(((class color) (background dark)) :foreground "#cd950c"))
"Face for correctly typed char.")
(defface monkeytype-correct
'((t :inherit monkeytype-dimmed))
"Face for correctly typed char.")
(defface monkeytype-error
'((t (
:foreground "#cc6666"
:underline (:color "#cc6666" :style wave))))
"Face for wrongly typed char.")
(defface monkeytype-correction-error
'((t (
:inherit region
:foreground "#ff6c6b"
:underline (:color "#ff6c6b" :style wave))))
"Face for wrongly typed correction.")
(defface monkeytype-correction-correct
'((t (:inherit region :foreground "#b9ca4a")))
"Face for correctly typed correction.")
(defface monkeytype-read-only
'((t :inherit monkeytype-dimmed :strike-through t :slant italic))
"Face for results titles.")
(defface monkeytype-title
'((t :inherit default))
"Face for results titles.")
(defface monkeytype-legend-1
'((t :inherit monkeytype-notice))
"Face for results legend 1.")
(defface monkeytype-legend-2
'((t (:slant italic :height 0.9)))
"Face for results legend 2.")
(defface monkeytype-results-success
'((t (:foreground "#98be65" :height 0.9)))
"Face for results success text.")
(defface monkeytype-results-error
'((t (:foreground "#cc6666" :height 0.9)))
"Face for results error text.")
(defface monkeytype-mode-line-success
'((t (:foreground "#98be65")))
"Face for mode-line success text.")
(defface monkeytype-mode-line-error
'((t (:foreground "#ff6c6b")))
"Face for mode-line error text.")
(defface monkeytype-mode-line-normal
'((t :inherit default))
"Face for mode-line normal text.")
(defface monkeytype-mode-line-info
'((t :inherit monkeytype-notice))
"Face for mode-line info text.")
;;;; Init:
(defvar-local monkeytype--buffer-name "*Monkeytype*")
(defvar-local monkeytype--runs '())
(defvar-local monkeytype--start-time nil)
(defvar-local monkeytype--wpm-peek-text nil)
(defvar monkeytype--typing-buffer nil)
(defvar monkeytype--source-text "")
(defvar monketype--utils-disabled-props
`(
read-only t
rear-nonsticky (read-only)
front-sticky (read-only)
face monkeytype-read-only))
;; Text-File
(defvar monkeytype--text-file-directory nil)
(defvar monkeytype--text-file-last-entry nil)
(defvar monkeytype--text-file nil)
;; Status
(defvar-local monkeytype--status-finished nil)
(defvar-local monkeytype--status-paused nil)
;; Counters
(defvar-local monkeytype--counter-chars 0)
(defvar-local monkeytype--counter-input 0)
(defvar-local monkeytype--counter-error 0)
(defvar-local monkeytype--counter-correction 0)
(defvar-local monkeytype--counter-remaining 0)
(defvar-local monkeytype--counter-ignored-change 0)
;; Run
(defvar-local monkeytype--progress-tracker "")
(defvar-local monkeytype--current-run '())
(defvar-local monkeytype--current-entry '())
(defvar-local monkeytype--current-run-start-datetime nil)
;; Results
(defvar-local monkeytype--previous-last-entry-index nil)
(defvar-local monkeytype--previous-run-last-entry nil)
(defvar-local monkeytype--previous-run '())
(defun monkeytype (text &optional fn)
"Set up a new buffer for the typing exercise on TEXT.
TEXT-FILE-P is used to know if the test is text-file based."
(setq monkeytype--typing-buffer
(generate-new-buffer monkeytype--buffer-name))
(set-buffer monkeytype--typing-buffer)
(erase-buffer)
(setq monkeytype--source-text text)
(setq monkeytype--counter-remaining (1+ (length text)))
(setq monkeytype--progress-tracker (make-string (length text) 0))
(insert monkeytype--source-text)
(goto-char 0)
(when fn (funcall fn))
;; `set-buffer-modified-p' has no be set to nil before adding the
;; change hooks for them to work, so it has to happen right before
;; loading `monkeytype-mode'.
(set-buffer-modified-p nil)
(monkeytype-mode)
(switch-to-buffer monkeytype--typing-buffer)
(message "Monkeytype: Timer will start when you start typing."))
;;;; Utils:
(defvar-local monkeytype--chars '())
(defvar-local monkeytype--words '())
(defvar-local monkeytype--mistyped-words '())
(defvar-local monkeytype--chars-to-words '())
(defvar-local monkeytype--hard-transitions '())
(defvar-local monkeytype--idle-timer nil)
(defun monketype--utils-reset-read-only-text-properties ()
"Reset text properties entire buffer (even read-only)."
;; Allow removing read-only properties.
(setq inhibit-read-only t)
;; Remove all text properties
(set-text-properties (point-min) (point-max) nil)
;; Disable inhibit-read-only back again.
(setq inhibit-read-only nil))
(defun monkeytype--utils-nshuffle (sequence)
"Shuffle given SEQUENCE.
URL `https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle'"
(cl-loop for i from (length sequence) downto 2
do (cl-rotatef (elt sequence (random i))
(elt sequence (1- i))))
sequence)
(defun monkeytype--utils-idle-timer (secs func)
"Idle timer for pausing run after 5 SECS.
FUNC and ARGS are passed directly to `run-with-idle-timer'."
(setq monkeytype--idle-timer (run-with-idle-timer secs nil func)))
(defun monkeytype--utils-file-path (type)
"Build path for the TYPE of file to be saved."
(let* ((dir (when monkeytype--text-file
(string-trim
monkeytype--text-file "^/.+/" "\\.[[:alnum:]]+\\'")))
(path (if dir (concat "text/" dir "/" type) type))
(path (concat monkeytype-directory path))
(file (format-time-string monkeytype-file-name-format))
(file (format "/%s.txt" (downcase file))))
(unless (file-exists-p path) (make-directory path t))
(concat path file)))
(defun monkeytype--utils-text-file-name ()
"Name for the text-file run's JSON file."
(format "%s" (downcase (format-time-string monkeytype-file-name-format))))
(defun monkeytype--utils-save-run (run)
"Save RUN as JSON format `monkeytype--text-file-directory'."
(let* ((dir (concat monkeytype--text-file-directory "json/"))
(path (concat dir (monkeytype--utils-text-file-name) ".json")))
(when (> (length (gethash 'entries run)) 0)
(with-temp-file path (insert (json-encode run))))))
(defun monkeytype--utils-elapsed-seconds ()
"Return float with the total time since start."
(let ((end-time (float-time)))
(if monkeytype--start-time
(- end-time monkeytype--start-time)
0)))
(defun monkeytype--utils-check-same (source typed)
"Return non-nil if both SOURCE and TYPED are white space or the same."
(if monkeytype-treat-newline-as-space
(or (string= source typed)
(and
(= (char-syntax (aref source 0)) ?\s)
(= (char-syntax (aref typed 0)) ?\s)))
(string= source typed)))
(defun monkeytype--utils-seconds-to-minutes (seconds)
"Return minutes in float for SECONDS."
(/ seconds 60.0))
(defun monkeytype--utils-index-words ()
"Index words."
(let ((words (split-string
monkeytype--source-text
monkeytype-excluded-chars-regexp))
(index 1))
(cl-loop for word in words do
(cl-pushnew (cons index word) monkeytype--words)
(setq index (1+ index)))))
(defun monkeytype--utils-index-chars-to-words ()
"Associate by their index chars to words.
See: `monkeytype--utils-index-words'
See: `monkeytype--utils-index-chars'"
(let ((chars (mapcar #'char-to-string monkeytype--source-text))
(word-index 1)
(char-index 1))
(cl-loop for char in chars do
(if (string-match monkeytype-excluded-chars-regexp char)
(progn
(setq word-index (1+ word-index))
(setq char-index (1+ char-index)))
(let ((word (cdr (assoc word-index monkeytype--words))))
(cl-pushnew (cons char-index word) monkeytype--chars-to-words)
(setq char-index (1+ char-index)))))))
(defun monkeytype--utils-index-chars (run)
"Index chars for given RUN."
(unless monkeytype--previous-last-entry-index
(setq monkeytype--previous-last-entry-index 0))
(let* ((start monkeytype--previous-last-entry-index)
(last-entry (elt (gethash 'entries run) 0))
(end (gethash 'source-index last-entry))
(source-text (substring monkeytype--source-text start end))
(chars (mapcar #'char-to-string source-text))
(chars-list '())
(index start))
(cl-loop for char in chars do
(setq index (1+ index))
(cl-pushnew (cons index char) chars-list))
(setq monkeytype--chars (reverse chars-list))
(setq monkeytype--previous-last-entry-index
(gethash 'source-index (elt (gethash 'entries run) 0)))))
(defun monkeytype--utils-format-words (words)
"Format WORDS by applying word related customization settings.
See: `monkeytype-downcase'
See: `monkeytype-randomize'
See: `monkeytype-words-auto-fill'
See: `monkeytype-delete-trailing-whitespace'"
(with-temp-buffer
(insert
(mapconcat
(lambda (word) (if monkeytype-downcase (downcase word) word))
(if monkeytype-randomize (monkeytype--utils-nshuffle words) words)
" "))
(when monkeytype-words-auto-fill
(fill-region (point-min) (point-max)))
(when monkeytype-delete-trailing-whitespace
(delete-trailing-whitespace))
(buffer-string)))
(defun monkeytype--utils-format-text (text)
"Format TEXT (for test) by applying customization settings.
See: `monkeytype-auto-fill'
See: `monkeytype-delete-trailing-whitespace'"
(with-temp-buffer
(insert text)
(when monkeytype-auto-fill (fill-region (point-min) (point-max)))
(when monkeytype-delete-trailing-whitespace (delete-trailing-whitespace))
(buffer-string)))
(defun monkeytype--utils-check-for-iconv ()
"Verify that iconv(1) is installed."
(when monkeytype-asciify
(if (executable-find "iconv")
t
(error "Monketype Error: Iconv(1) executable not installed"))))
(defun monkeytype--utils-check-for-pandoc ()
"Verify that pandoc(1) is installed."
(if (executable-find "pandoc")
t
(error "Monketype Error: pandoc(1) executable not installed")))
;;;; Calc:
(defun monkeytype--calc-words (chars)
"Divide all CHARS by `monkeytype-word-divisor'."
(/ chars monkeytype-word-divisor))
(defun monkeytype--calc-gross-wpm (words minutes)
"Divides WORDS by MINUTES.
See `monkeytype--calc-words' for WORDS."
(/ words minutes))
(defun monkeytype--calc-gross-cpm (chars minutes)
"Divides CHARS by MINUTES."
(/ chars minutes))
(defun monkeytype--calc-net-wpm (words uncorrected-errors minutes)
"Net WPM is the gross WPM minus the UNCORRECTED-ERRORS by MINUTES.
Since there can be up to five errors for each word (see:
`monkeytype--calc-words' for WORDS calculation) if net-WPM is negative, return 0
instead.
See `monkeytype--calc-gross-cpm' for gross WPM."
(let ((net-wpm (- (monkeytype--calc-gross-wpm words minutes)
(/ uncorrected-errors minutes))))
(if (> 0 net-wpm) 0 net-wpm)))
(defun monkeytype--calc-net-cpm (chars uncorrected-errors minutes)
"Net CPM is the gross CPM minus the UNCORRECTED-ERRORS by MINUTES.
See `monkeytype--calc-gross-cpm' for gross CPM.
UNCORRECTED-ERRORS are re-mistyped CHARS."
(let ((net-cpm (- (monkeytype--calc-gross-cpm chars minutes)
(/ uncorrected-errors minutes))))
(if (> 0 net-cpm) 0 net-cpm)))
(defun monkeytype--calc-accuracy (chars correct-chars corrections)
"Accuracy is all CORRECT-CHARS minus CORRECTIONS divided by all CHARS."
(when (> chars 0)
(let* ((a-chars (- correct-chars corrections))
(a-chars (if (> a-chars 0) a-chars 0))
(accuracy (* (/ a-chars (float chars)) 100.00)))
accuracy)))
;;;; Process Input:
(defun monkeytype--process-input (start end delete-length)
"Process input from region START to region END.
DELETE-LENGTH is the amount of deleted chars in case of deletion."
(let* ((source-start (1- start))
(source-end (1- end))
(entry (substring-no-properties
(buffer-substring start end)))
(source (substring monkeytype--source-text source-start source-end))
(state (aref monkeytype--progress-tracker source-start))
(correctp (monkeytype--utils-check-same source entry))
(tracker (if correctp 1 2))
(face-for-entry (monkeytype--typed-text-entry-face correctp))
(valid-input (<= (length source) 1)))
(monkeytype--process-input-restabilize start end state delete-length)
(when valid-input
(store-substring monkeytype--progress-tracker source-start tracker)
(cl-decf monkeytype--counter-remaining)
(unless correctp (cl-incf monkeytype--counter-error))
(set-text-properties start (1+ start) `(face ,face-for-entry))
(unless (= start end) ; Do not add no-char (e.i., deletion) entries
(cl-incf monkeytype--counter-chars)
(monkeytype--process-input-add-to-entries source-start entry source))
(monkeytype--process-input-update-mode-line))
(goto-char end)
(when (= monkeytype--counter-remaining 0) (monkeytype--run-finish))))
(defun monkeytype--process-input-restabilize (start end state deleted)
"Restabilize input-region from START to END.
STATE keeps track of text already typed but deleted (e.i., correction).
DELETED is the number of deleted chars before current char input."
(let* ((source-start (1- start))
(skippedp (>
(+ source-start monkeytype--counter-remaining)
(length monkeytype--source-text)) )
(correctionp (> state 0))
(deleted-text-p (and (= start end) (> deleted 0)))
(deleted-end (+ source-start deleted)))
;; On skips update remaining-counter to reflect current position
(when skippedp
(setq monkeytype--counter-remaining
(- (length monkeytype--source-text) source-start)))
;; Leaving only the newly typed char
(delete-region start end)
(when correctionp
(monkeytype--process-input-rectify-counters state)
;; Reset tracker back to 0 (i.e, new)
(store-substring monkeytype--progress-tracker source-start 0))
;; Re-insert deleted text and update remaining counter
(when deleted-text-p
(setq monkeytype--counter-remaining
(- (length monkeytype--source-text) source-start))
(insert
(substring monkeytype--source-text source-start deleted-end)))))
(defun monkeytype--process-input-rectify-counters (entry-state)
"Update counters on corrections/re-tries based on the ENTRY-STATE.
ENTRY-STATE = 0 not a correction
ENTRY-STATE = 1 correctly re-typed char
ENTRY-STATE = 2 mistyped re-typed char"
(unless (= entry-state 0) ; ignore unless it's a correction
(cl-incf monkeytype--counter-remaining)
;; Update corrections counter on correct or incorrect retries.
;; This means that in terms of wpm-accuracy correct or incorrect
;; retries they get counted affecting the score negatively.
(cl-incf monkeytype--counter-correction)
(when (= entry-state 2)
(cl-decf monkeytype--counter-error))))
(defun monkeytype--process-input-add-to-entries (start typed source)
"Add entry to `monkeytype--current-run'.
START is used for indexing purposes.
TYPED is the char input by the user.
SOURCE is the original char."
(cl-incf monkeytype--counter-input)
(let ((entry (make-hash-table :test 'equal))
(seconds (format-seconds
"%.2h:%z%.2m:%.2s"
(monkeytype--utils-elapsed-seconds))))
(puthash 'chars monkeytype--counter-chars entry)
(puthash 'input-index monkeytype--counter-input entry)
(puthash 'typed-entry typed entry)
(puthash 'source-entry source entry)
(puthash 'source-index (1+ start) entry)
(puthash 'error-count monkeytype--counter-error entry)
(puthash 'correction-count monkeytype--counter-correction entry)
(puthash 'state (aref monkeytype--progress-tracker start) entry)
(puthash 'elapsed-seconds (monkeytype--utils-elapsed-seconds) entry)
(puthash 'formatted-seconds seconds entry)
(add-to-list 'monkeytype--current-run entry)))
(defun monkeytype--process-input-timer-init ()
"Start the idle timer (to wait 5 seconds before pausing).
See: `monkeytype--utils-idle-timer'"
(unless monkeytype--start-time
(setq monkeytype--current-run-start-datetime
(format-time-string "%a-%d-%b-%Y %H:%M:%S"))
(setq monkeytype--start-time (float-time))
(monkeytype--utils-idle-timer 5 'monkeytype-pause)))
(defun monkeytype--process-input-update-mode-line ()
"Update `monkeytype-mode-line' by sending it the current entry info."
(if (and
monkeytype-mode-line
monkeytype-mode-line-interval-update)
(let* ((entry (elt monkeytype--current-run 0))
(char-index (if entry (gethash 'source-index entry) 0)))
(if (and
(> char-index monkeytype-mode-line-interval-update)
(= (mod char-index monkeytype-mode-line-interval-update) 0))
(monkeytype--mode-line-report-status)))))
;;;; Run:
(defun monkeytype--run-pause ()
"Pause run by resetting hooks and `monkeytype--start-time'."
(setq monkeytype--start-time nil)
(cancel-timer monkeytype--idle-timer)
(monkeytype--run-remove-hooks)
(monkeytype--run-add-to-list)
(when monkeytype--text-file
(monkeytype--utils-save-run (elt monkeytype--runs 0)))
(let* ((last-run (elt monkeytype--runs 0))
(last-entry (elt (gethash 'entries last-run) 0))
(end (1+ (gethash 'source-index last-entry))))
(monketype--utils-reset-read-only-text-properties)
(add-text-properties 1 end monketype--utils-disabled-props))
(read-only-mode))
(defun monkeytype--run-finish ()
"Remove typing hooks and print results."
(setq monkeytype--status-finished t)
(monketype--utils-reset-read-only-text-properties)
;; `monketytype--wpm-peek' updated these vars so they
;; have to be set back to nil for the final results.
(setq
monkeytype--text-file-directory nil
monkeytype--text-file-last-entry nil
monkeytype--text-file nil
monkeytype--previous-last-entry-index nil
monkeytype--previous-run-last-entry nil
monkeytype--previous-run '())
(unless monkeytype--status-paused
(setq monkeytype--start-time nil)
(cancel-timer monkeytype--idle-timer)
(monkeytype--run-remove-hooks)
(monkeytype--run-add-to-list)
(setq monkeytype--current-run '())
(when monkeytype--text-file
(monkeytype--utils-save-run (elt monkeytype--runs 0))))
(setq buffer-read-only nil)
(message "Monkeytype: Processing results...")
(monkeytype--results)
(message "Monkeytype: Results generated successfully.")
(monkeytype--mode-line-report-status)
(read-only-mode))
(defun monkeytype--run-add-to-list ()
"Add current run to `monkeytype--runs'."
(let ((run (make-hash-table :test 'equal)))
(puthash 'started-at monkeytype--current-run-start-datetime run)
(puthash 'finished-at (format-time-string "%a-%d-%b-%Y %H:%M:%S") run)
(puthash 'entries monkeytype--current-run run)
(add-to-list 'monkeytype--runs run)))
(defun monkeytype--run-remove-hooks ()
"Remove hooks."
(remove-hook 'after-change-functions #'monkeytype--process-input)
(remove-hook 'first-change-hook #'monkeytype--process-input-timer-init))
(defun monkeytype--run-add-hooks ()
"Add hooks."
(make-local-variable 'after-change-functions)
(make-local-variable 'first-change-hook)
(add-hook 'after-change-functions #'monkeytype--process-input nil t)
(add-hook 'first-change-hook #'monkeytype--process-input-timer-init nil t))
;;;; Results:
(defun monkeytype--results ()
"Print all results."
(erase-buffer)
(when monkeytype--text-file
(let* ((path monkeytype--text-file)
(dir (concat (string-trim path nil "\\.txt\\'") "/json"))
(runs (directory-files dir t "\\.json\\'" nil)))
(setq monkeytype--runs '())
(dolist (run runs)
(let* ((run (json-read-file run))
(run-hash (make-hash-table :test 'equal))
(entries (mapcar
(lambda (x)
(map-into x 'hash-table))
(cdr (assoc 'entries run)))))
(puthash 'started-at (cdr (assoc 'started-at run)) run-hash)
(puthash 'finished-at (cdr (assoc 'finished-at run)) run-hash)
(puthash 'entries entries run-hash)
(add-to-list 'monkeytype--runs run-hash)))))
(when (> (length monkeytype--runs) 1)
(let* ((title-1 "Final Results for a Total of %d Runs\n\n")
(title-1 (format title-1 (length monkeytype--runs)))
(title-1 (propertize title-1 'face 'monkeytype-title))
(title-2 "\n\nRuns Breakdown:\n\n")
(title-2 (propertize title-2 'face 'monkeytype-title)))
(insert (concat title-1 (monkeytype--results-final) title-2))))
(let ((run-index 1))
(dolist (run (reverse monkeytype--runs))
(let* ((title "%d) %s:\n")
(title (format title run-index (gethash 'started-at run)))
(title (replace-regexp-in-string "-" " " title))
(title (propertize title 'face 'monkeytype-title))
(title (and (> (length monkeytype--runs) 1) title))
(run-typed-text (monkeytype--typed-text run))
(run-results (monkeytype--results-run (gethash 'entries run))))
(insert
(concat title run-results "\n\nTyped Text:\n" run-typed-text)))
(setq run-index (1+ run-index))
(when monkeytype-insert-log
(insert (monkeytype--log run)))))
(goto-char (point-min)))
(defun monkeytype--results-get-face (successp)
"Get success or error face based on SUCCESSP."
(if successp 'monkeytype-results-success 'monkeytype-results-error))
(defun monkeytype--results-net-wpm (words errors minutes seconds)
"Net WPM performance result for total WORDS.
Gross-WPM - (ERRORS / MINUTES).
Also shows SECONDS right next to WPM."
(let* ((seconds (format-seconds "%.2h:%z%.2m:%.2s" seconds))
(net-wpm (monkeytype--calc-net-wpm words errors minutes))
(net-wpm (format "%.2f/%s" net-wpm seconds))
(net-wpm (propertize net-wpm 'face 'monkeytype-legend-1))
(gross-wpm (monkeytype--calc-gross-wpm words minutes))
(gross-wpm (format "[%.2f - (" gross-wpm))
(gross-wpm (propertize gross-wpm 'face 'monkeytype-legend-2))
(errors-face (monkeytype--results-get-face (= errors 0)))
(errors-lable (format "%d" errors))
(errors-lable (propertize errors-lable 'face errors-face))
(minutes-lable (format " / %.2f)]\n" minutes))
(minutes-lable (propertize minutes-lable 'face 'monkeytype-legend-2))
(info-label "WPM = Gross-WPM - (uncorrected-errors / minutes)")
(info-label (propertize info-label 'face 'monkeytype-legend-2)))
(concat net-wpm gross-wpm errors-lable minutes-lable info-label)))
(defun monkeytype--results-gross-wpm (words minutes)
"Gross WPM performance result.
Gross-WPM = WORDS / MINUTES."
(let* ((gross-wpm (monkeytype--calc-gross-wpm words minutes))
(gross-wpm (format "%.2f" gross-wpm))
(gross-wpm (propertize gross-wpm 'face 'monkeytype-legend-1))
(open-bracket (propertize "[" 'face 'monkeytype-legend-2))
(words-label (format "%.2f" words))
(words-label (propertize words-label 'face 'monkeytype-results-success))
(minutes-label (format " / %.2f]" minutes))
(minutes-label (propertize minutes-label 'face 'monkeytype-legend-2))
(help-label "\nGross-WPM = words / minutes")
(help-label (propertize help-label 'face 'monkeytype-legend-2)))
(concat gross-wpm open-bracket words-label minutes-label help-label)))
(defun monkeytype--results-accuracy (chars correct corrections)
"Calculate accuracy: ((CORRECT - CORRECTIONS) / CHARS) * 100."
(let* ((acc (monkeytype--calc-accuracy chars correct corrections))
(acc (format "%.2f%%" acc))
(acc (propertize acc 'face 'monkeytype-legend-1))
(correct-lable (format "[((%.2f - " correct))
(correct-lable (propertize correct-lable 'face 'monkeytype-legend-2))
(corrections-face (monkeytype--results-get-face (= corrections 0)))
(corrections-lable (format "%d" corrections))
(corrections-lable (propertize corrections-lable 'face corrections-face))
(chars-lable (format ") / %.2f) * 100]" chars))
(chars-lable (propertize chars-lable 'face 'monkeytype-legend-2))
(help-lable "\nAccuracy = ((correct-chars - corrections) / total-chars) * 100")
(help-lable (propertize help-lable 'face 'monkeytype-legend-2)))
(concat acc correct-lable corrections-lable chars-lable help-lable)))
(defun monkeytype--results-run (run)
"Performance results for RUN."
(let* ((last-entry (elt run 0))
(previous-entry monkeytype--previous-run-last-entry)
(seconds (gethash 'elapsed-seconds last-entry))
(minutes (monkeytype--utils-seconds-to-minutes seconds))
(char-count (gethash 'chars last-entry))
(errors (gethash 'error-count last-entry))
(corrections (gethash 'correction-count last-entry))
(words (monkeytype--calc-words char-count))
(str-format "%s\n\n%s\n\n%s"))
(when previous-entry
(setq char-count (- char-count (gethash 'chars previous-entry)))
(setq words (monkeytype--calc-words char-count))
(setq errors (- errors (gethash 'error-count previous-entry)))
(setq corrections
(- corrections (gethash 'correction-count previous-entry))))
(setq monkeytype--previous-run-last-entry (elt run 0))
(concat
(format
str-format
(monkeytype--results-net-wpm words errors minutes seconds)
(monkeytype--results-accuracy
char-count
(- char-count errors)
corrections)
(monkeytype--results-gross-wpm words minutes)))))
(defun monkeytype--results-final ()
"Final results for typed run(s).
Total time is the sum of all the last entries' elapsed-seconds for each
run."
(let* ((last-entries (mapcar
(lambda (x) (elt (gethash 'entries x) 0))
monkeytype--runs))
(last-entry (elt last-entries 0))
(total-seconds (mapcar
(lambda (x) (gethash 'elapsed-seconds x))
last-entries))
(total-seconds (apply #'+ total-seconds))
(minutes (monkeytype--utils-seconds-to-minutes total-seconds))
(char-count (gethash 'chars last-entry))
(errors (gethash 'error-count last-entry))
(corrections (gethash 'correction-count last-entry))
(words (monkeytype--calc-words char-count))
(str-format "%s\n\n%s\n\n%s")
(net-wpm (monkeytype--results-net-wpm words errors minutes total-seconds))
(acc (monkeytype--results-accuracy char-count (- char-count errors) corrections))
(gross-wpm (monkeytype--results-gross-wpm words minutes)))
(concat (format str-format net-wpm acc gross-wpm))))
;;;; Typed Text:
(defun monkeytype--typed-text-entry-face (correctp &optional correctionp)
"Return the face for the CORRECTP and/or CORRECTIONP entry."
(if correctionp
(if correctp 'monkeytype-correction-correct 'monkeytype-correction-error)
(if correctp 'monkeytype-correct 'monkeytype-error)))
(defun monkeytype--typed-text-newline (source typed)
"Newline substitutions depending on SOURCE and TYPED char."
(if (string= "\n" source)
(if (or (string= " " typed) (string= source typed))
"↵\n"
(concat typed "↵\n"))
typed))
(defun monkeytype--typed-text-whitespace (source typed)
"Whitespace substitutions depending on SOURCE and TYPED char."
(if (and (string= " " typed) (not (string= typed source))) "·" typed))
(defun monkeytype--typed-text-skipped (settled)
"Handle skipped text before the typed char at SETTLED."
(let* ((start (car (car monkeytype--chars)))
(skipped-length (when start (- settled start))))
(if skipped-length
(progn (pop monkeytype--chars) "")
(cl-loop repeat (1+ skipped-length) do
(pop monkeytype--chars))
(substring monkeytype--source-text (1- start) (1- settled)))))
(defun monkeytype--typed-text-add-to-mistyped-list (char)
"Find associated word for CHAR and add it to mistyped list."
(let* ((index (gethash 'source-index char))
(word (cdr (assoc index monkeytype--chars-to-words))))
(when word
(cl-pushnew
(replace-regexp-in-string monkeytype-excluded-chars-regexp "" word)
monkeytype--mistyped-words))))
(defun monkeytype--typed-text-concat-corrections (corrections entry settled)
"Concat propertized CORRECTIONS to SETTLED char.
Also add corrections in ENTRY to `monkeytype--mistyped-word-list'."
(monkeytype--typed-text-add-to-mistyped-list entry)
(format
"%s%s"
settled
(mapconcat
(lambda (correction)
(let* ((correction-char (gethash 'typed-entry correction))
(state (gethash 'state correction))
(correction-face
(monkeytype--typed-text-entry-face (= state 1) t)))
(propertize (format "%s" correction-char) 'face correction-face)))
corrections
"")))
(defun monkeytype--typed-text-collect-errors (settled)
"Add SETTLED char's associated word/transition to their respective list.
This is unless the char doesn't belong to any word as defined by the
`monkeytype-excluded-chars-regexp'."
(unless (= (gethash 'state settled) 1)
(unless (string-match
monkeytype-excluded-chars-regexp
(gethash 'source-entry settled))
(let* ((index (gethash 'source-index settled))
(transition-p (> index 2))
(transition (when transition-p
(substring
monkeytype--source-text
(- index 2)
index)))
(transition-p (and
transition-p
(string-match "[^ \n\t]" transition))))
(when transition-p
(cl-pushnew transition monkeytype--hard-transitions))
(monkeytype--typed-text-add-to-mistyped-list settled)))))
(defun monkeytype--typed-text-to-string (entries)
"Format typed ENTRIES and return a string."
(mapconcat
(lambda (entries-for-source)
(let* ((tries (cdr entries-for-source))
(correctionsp (> (length tries) 1))
(settled (if correctionsp (car (last tries)) (car tries)))
(source-entry (gethash 'source-entry settled))
(typed-entry (monkeytype--typed-text-newline
source-entry
(gethash 'typed-entry settled)))
(typed-entry (monkeytype--typed-text-whitespace
source-entry
typed-entry))
(settled-correctp (= (gethash 'state settled) 1))
(settled-index (gethash 'source-index settled))
(skipped-text (monkeytype--typed-text-skipped settled-index))
(prop-settled (propertize
(format "%s" typed-entry)
'face
(monkeytype--typed-text-entry-face
settled-correctp)))
(prop-settled (concat skipped-text prop-settled))
(corrections (when correctionsp (butlast tries))))
(if correctionsp
(monkeytype--typed-text-concat-corrections
corrections
settled
prop-settled)
(monkeytype--typed-text-collect-errors settled)
(format "%s" prop-settled))))
entries
""))
(defun monkeytype--typed-text (run)
"Typed text for RUN."
(monkeytype--utils-index-chars run)
(unless monkeytype--words
(monkeytype--utils-index-words))
(unless monkeytype--chars-to-words
(monkeytype--utils-index-chars-to-words))
(format
"\n%s\n\n"