-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathspeed-type.el
More file actions
2831 lines (2506 loc) · 138 KB
/
speed-type.el
File metadata and controls
2831 lines (2506 loc) · 138 KB
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
;;; speed-type.el --- Practice touch and speed typing -*- lexical-binding: t -*-
;; Copyright (C) 2015 Gunther Hagleitner
;; Author: Gunther Hagleitner
;; Maintainer: Daniel Kraus <daniel@kraus.my>, lordnik22
;; Version: 1.7
;; Keywords: games
;; URL: https://github.com/dakra/speed-type
;; Package-Requires: ((emacs "27.1") (compat "29.1.3"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file is NOT part of GNU Emacs.
;;; License:
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Speed-type allows you to practice your touch typing skills. You can
;; test yourself by typing snippets from online books or use any piece
;; of text or code you have in Emacs. Speed-type keeps track of your
;; stats (WPM, CPM, accuracy) while you are typing.
;;; Code:
(require 'text-property-search)
(require 'cl-lib)
(require 'cl-macs)
(require 'compat)
(require 'url)
(require 'url-handlers)
(require 'url-http)
(require 'thingatpt)
(require 'dom)
(when (version< emacs-version "29.1")
(eval-and-compile
(defmacro with-undo-amalgamate (&rest body)
"Like `progn' but perform BODY with amalgamated undo barriers.
This allows multiple operations to be undone in a single step.
When undo is disabled this behaves like `progn'."
(declare (indent 0) (debug t))
(let ((handle (make-symbol "--change-group-handle--")))
`(let ((,handle (prepare-change-group))
;; Don't truncate any undo data in the middle of this,
;; otherwise Emacs might truncate part of the resulting
;; undo step: we want to mimic the behavior we'd get if the
;; undo-boundaries were never added in the first place.
(undo-outer-limit nil)
(undo-limit most-positive-fixnum)
(undo-strong-limit most-positive-fixnum))
(unwind-protect
(progn
(activate-change-group ,handle)
,@body)
(progn
(accept-change-group ,handle)
(undo-amalgamate-change-group ,handle))))))))
(defgroup speed-type nil
"Practice touch-typing in Emacs."
:group 'games)
(defcustom speed-type-buffer-name "*speed-type*"
"Name of buffer in which the user completes his typing session."
:type 'string)
(defcustom speed-type-content-buffer-name "*speed-type-content*"
"Name of buffer consisting of the content-source for the speed-type buffer."
:type 'string)
(defcustom speed-type-preview-buffer-name "*speed-type-preview*"
"Name of buffer consisting of the preview for the speed-type buffer."
:type 'string)
(defcustom speed-type-min-chars 200
"The minimum number of chars to type required when the text is picked randomly."
:type 'integer)
(defcustom speed-type-max-chars 450
"The maximum number of chars to type required when the text is picked randomly."
:type 'integer)
(defcustom speed-type-text-picker-tolerance 20
"Char-count allowed to exceed MAX if text-picker would cut of word otherwise."
:type 'integer)
(defcustom speed-type-pause-delay-seconds 5
"Define after which idle delay it should pause the timer."
:type 'integer)
(defcustom speed-type-gb-book-list
'(1342 11 1952 1661 74 1232 23 135 5200 2591 844 84 98 2701 1400 16328 174
46 4300 345 1080 2500 829 1260 6130 1184 768 32032 521 1399 55)
"List of book numbers to use from the gutenberg web site.
Book numbers can be picked from https://www.gutenberg.org, when looking
at a book url (e.g for url https://www.gutenberg.org/ebooks/14577,
14577 is the book number)."
:type '(repeat integer))
(defcustom speed-type-directory (locate-user-emacs-file "speed-type")
"Directory in which the gutenberg books will be saved."
:type 'directory)
(defcustom speed-type-wordlist-urls
'((English . "http://web.archive.org/web/20170227200416/http://wortschatz.uni-leipzig.de/Papers/top10000en.txt")
(German . "http://web.archive.org/web/20170227200416/http://wortschatz.uni-leipzig.de/Papers/top10000de.txt")
(French . "http://web.archive.org/web/20170227200416/http://wortschatz.uni-leipzig.de/Papers/top10000fr.txt")
(Dutch . "http://web.archive.org/web/20170227200416/http://wortschatz.uni-leipzig.de/Papers/top10000nl.txt"))
"Alist of language name as key and a URL where to download a wordlist for it."
:type '(alist :key-type symbol :value-type string))
(defcustom speed-type-stop-words '()
"List of stop words which will be excluded for top-x-commands.
Can be a list or a path to a file which contains words newline separated."
:type '(choice
(list :tag "List of words" string)
(file :tag "Store list in a file\n" :value (concat speed-type-directory "/stop-words.txt"))))
(defcustom speed-type-quote-urls
'((johnVonNeumann . "https://www.azquotes.com/author/10753-John_von_Neumann")
(happiness . "https://www.azquotes.com/quotes/topics/happiness.html")
(alanTuring . "https://www.azquotes.com/author/14856-Alan_Turing"))
"List of name as key and an URL to azquotes which will be downloaded and parsed."
:type '(alist :key-type symbol :value-type string))
(defcustom speed-type-wordlist-transform nil
"OBSOLETE: Better use `speed-type-transform-hook'.
Function to transform wordlist before starting the exercise.
The function should take the `buffer-string' as argument and return
the transformed string that is used for the speed type exercise.
E.g. if you always want lowercase words, set:
`speed-type-wordlist-transform' to `downcase'."
:type '(choice (const :tag "None" nil)
(function :tag "Transform function")))
(defcustom speed-type-default-lang nil
"Default language for training wordlists. Ask when NIL."
:type '(choice (const :tag "None" nil)
(const :tag "Afrikaans" af)
(const :tag "Aleut" ale)
(const :tag "Arabic" ar)
(const :tag "Arapaho" arp)
(const :tag "Bodo" brx)
(const :tag "Breton" br)
(const :tag "Bulgarian" bg)
(const :tag "Caló" rmq)
(const :tag "Catalan" ca)
(const :tag "Cebuano" ceb)
(const :tag "Chinese" zh)
(const :tag "Czech" cs)
(const :tag "Danish" da)
(const :tag "Dutch" nl)
(const :tag "English" en)
(const :tag "Esperanto" eo)
(const :tag "Estonian" et)
(const :tag "Farsi" fa)
(const :tag "Finnish" fi)
(const :tag "French" fr)
(const :tag "Frisian" fy)
(const :tag "Friulian" fur)
(const :tag "Gaelic Scottish" gla)
(const :tag "Galician" gl)
(const :tag "Gamilaraay" kld)
(const :tag "German" de)
(const :tag "Greek" el)
(const :tag "Greek Ancient" grc)
(const :tag "Hebrew" he)
(const :tag "Hungarian" hu)
(const :tag "Icelandic" is)
(const :tag "Iloko" ilo)
(const :tag "Interlingua" ia)
(const :tag "Inuktitut" iu)
(const :tag "Irish" ga)
(const :tag "Italian" it)
(const :tag "Japanese" ja)
(const :tag "Kashubian" csb)
(const :tag "Khasi" kha)
(const :tag "Korean" ko)
(const :tag "Latin" la)
(const :tag "Maori" mi)
(const :tag "Mayan Languages" myn)
(const :tag "Middle English" enm)
(const :tag "Nahuatl" nah)
(const :tag "Napoletano-Calabrese" nap)
(const :tag "Navajo" nav)
(const :tag "North American Indian" nai)
(const :tag "Norwegian" no)
(const :tag "Occitan" oc)
(const :tag "Ojibwa" oji)
(const :tag "Old English" ang)
(const :tag "Polish" pl)
(const :tag "Portuguese" pt)
(const :tag "Romanian" ro)
(const :tag "Russian" ru)
(const :tag "Sanskrit" sa)
(const :tag "Serbian" sr)
(const :tag "Slovenian" sl)
(const :tag "Spanish" es)
(const :tag "Swedish" sv)
(const :tag "Tagabawa" bgs)
(const :tag "Tagalog" tl)
(const :tag "Telugu" te)
(const :tag "Welsh" cy)))
(defcustom speed-type-replace-regexs '()
"Alist of regex to replace and their replacement, in the form:
`(bad-regex . good-regex)'
To remove without replacement, use the form: `(bad-regex . \"\")'.
It's possible to reference capture groups in good-regex.
Keep in mind the escaping hell."
:type '(alist :key-type string :value-type string))
(defcustom speed-type-replace-strings '(("“" . "\"") ("”" . "\"") ("‘" . "'") ("’" . "'") ("—" . "-") ("–" . "-") ("Æ" . "Ae") ("æ" . "ae") ("»" . "\"") ("«" . "\"") ("„" . "\"") ("…" . "..."))
"Alist of literal strings to replace and their replacement, in the form:
`(bad-string . good-string)'
To remove without replacement, use the form: `(bad-string . \"\")'"
:type '(alist :key-type string :value-type string))
(defcustom speed-type-randomize t
"Affects the text-picker when starting speed-type-buffer or speed-type-text.
When non-nil it picks a random portion, otherwise it checks for existing
records to start from. If nothing found will take text-portion from the
beginning."
:type 'boolean)
(defcustom speed-type-downcase nil
"If t will downcase content."
:type 'boolean)
(defcustom speed-type-point-motion-on-error 'point-move
"Define the behavior of point when mistyping a character.
when point-move (default), moves the point one character further.
when point-stay, stays at the current position until correct character is typed."
:type 'symbol)
(defcustom speed-type-complete-all-correct nil
"This flag controls the behaviour of triggering complete in speed-type session.
When non-nil, will complete if all characters are typed correctly (have
status correct or ignore)
When nil, will complete when all characters have a status (any non-nil
status value)."
:type 'boolean)
(defcustom speed-type-add-extra-words-on-error 0
"How many new words should be added on error.
When 0 or less, no words are added. The typing-session will only
be complete when these extra words are typed too. Recommanded is
something between 1 and 7.
Similar to `speed-type-add-extra-words-on-non-consecutive-errors'
they accumulate each other if both variables are set."
:type 'integer)
(defcustom speed-type-add-extra-words-on-non-consecutive-errors 0
"How many new words should be added on a non-consecutive error.
A non-consecutive error is a mistyped character where the previous
one was correctly typed.
When 0 or less, no words are added. The typing-session will only
be complete when these extra words are typed too. Recommended is
something between 1 and 7.
Similar to `speed-type-add-extra-words-on-error',
they accumulate each other if both variables are set."
:type 'integer)
(defcustom speed-type-save-statistic-option 'always
"Save the stats for the play or not."
:type '(choice (const :tag "Always" always)
(const :tag "Never" never)
(const :tag "Ask" ask)))
(defcustom speed-type-statistic-filename (concat speed-type-directory "/" "speed-type-statistic.el")
"This filename points to the speed-type stats file.
The file contains all relevant buffer-local variables for each
speed-type session completed.
Stats are stored using a rolling strategy.
See also `speed-type-max-num-records'."
:type 'string)
(defcustom speed-type-provide-preview-option nil
"When t will open a separate window where typed characters are inserted.
It tracks the speed-type session.
When symbol `hidden' is used will track the input but won't open a new
window.
When nil won't track the speed-type session."
:type '(choice (const :tag "Yes" t)
(const :tag "Hidden" hidden)
(const :tag "No" nil)))
(defcustom speed-type-max-num-records 10000
"Maximum number of saved records a stats-file is going to hold."
:type '(natnum :tag "None negative number." ))
(defcustom speed-type-code-modes '(prog-mode yaml-mode xml-mode html-mode)
"Define which modes should be handled as code.
These modes will have syntax highlighting and NO `fill-region' will be called."
:type '(repeat symbol))
(defcustom speed-type-ignore-whitespace-for-complete t
"Defines if whitespace should be ignored for speed-type-session to be complete.
When non-nil, the completion of the speed-type-session is triggered even when
there are untyped blank-characters.
If nil, the completion is only triggered if all characters are typed."
:type 'boolean)
(defcustom speed-type-transform-hook
'(speed-type--replace-string-hook
speed-type--replace-regex-hook
speed-type--filter-stop-word-hook
speed-type--left-align-hook
speed-type--delete-trailing-whitespace-hook
speed-type--downcase-hook
speed-type--fill-region-hook)
"Hook run when text is inserted to speed-type-buffer."
:type 'hook)
(defface speed-type-default
'()
"Default face for `speed-type'.")
(defface speed-type-correct-face
'((t :inherit success :weight normal))
"Face for correctly typed characters.")
(defface speed-type-consecutive-error-face
'((t :inherit error :underline t))
"Face for incorrectly typed characters where the previous is already an error.")
(defface speed-type-error-face
'((t :inherit error :underline t))
"Face for incorrectly typed characters.")
(defface speed-type-info-face
'((t :inherit font-lock-comment-face :underline t))
"Face for point-movement in preview buffer.")
;; internal variables
(defvar speed-type--gb-url-format "https://www.gutenberg.org/cache/epub/%d/pg%d.txt")
(defvar speed-type-explaining-message "
Gross wpm/cpm ignore uncorrected errors and indicate raw speed.
Net wpm/cpm take uncorrected errors into account and are a measure
of effective or net speed.")
(defvar speed-type-stats-format "\n
Skill: %s
Net WPM: %d
Net CPM: %d
Gross WPM: %d
Gross CPM: %d
APM: %d
APC: %.3f
Accuracy: %.2f%%
Total time: %s
Total chars: %d
Corrections: %d
Best correct streak: %d
Total errors: %d
Total non-consecutive errors: %d
%s")
(defvar speed-type-stats-analysis-format "\n
Num of records: %d
From--To: <%s>--<%s>
Note: 'nil' values are excluded from the calculations.
| | Median | Avg | SD | Min | Max |
| Skill: | %7s | %7s | ------- | %7s | %7s |
| Net WPM: | %7d | %7d | %7d | %7d | %7d |
| Net CPM: | %7d | %7d | %7d | %7d | %7d |
| Gross WPM: | %7d | %7d | %7d | %7d | %7d |
| Gross CPM: | %7d | %7d | %7d | %7d | %7d |
| APM: | %7d | %7d | %7d | %7d | %7d |
| APC: | %7.3f | %7.3f | %7.3f | %7.3f | %7.3f |
| Accuracy: | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%% | %6.2f%% |
| Total time: | %6.1fs | %6.1fs | %6.1fs | %6.1fs | %6.1fs |
| Total chars: | %7d | %7d | %7d | %7d | %7d |
| Corrections: | %7d | %7d | %7d | %7d | %7d |
| Best correct streak: | %7d | %7d | %7d | %7d | %7d |
| Total errors: | %7d | %7d | %7d | %7d | %7d |
| Non-consecutive errors: | %7d | %7d | %7d | %7d | %7d |")
(defvar-keymap speed-type-mode-completed-map
:doc "Key when speed-type session is completed (menu)."
"q" #'speed-type-quit
"d" #'speed-type--display-statistic
"r" #'speed-type-replay
"n" #'speed-type-play-next
"c" #'speed-type-play-continue
"t" #'speed-type-toggle-preview)
(defvar-keymap speed-type-mode-map
:doc "Keymap for `speed-type-mode'."
"C-c C-k" #'speed-type-complete
"C-c C-f" #'speed-type-finish-animation
"TAB" #'speed-type-code-tab
"RET" #'speed-type-code-ret)
(define-derived-mode speed-type-mode fundamental-mode "SpeedType"
"Major mode for practicing touch typing."
:group "speed-type")
;; buffer local internal variables
(defvar-local speed-type--text-type nil
"Symbol indicating what text-picker is used.
It can be one of following symbol:
- continue-text-section
- random-text-section
- random-wordlist
- transform-text
- quote")
(defvar-local speed-type--preview-buffer nil)
(defvar-local speed-type--last-position 0
"Used in preview-buffer to detect unusual point-movement in speed-type-buffer.")
(defvar-local speed-type--randomize nil
"Used to determine if continue-at-point should be stored.")
(defvar-local speed-type--continue-at-point nil
"Used to determine at which point in content-buffer to continue.")
(defvar-local speed-type--file-name nil
"Used as identification when storing the progress for a book or buffer.")
(defvar-local speed-type--max-point-on-complete nil
"Used for replay marking the end of the content to type.
It's the point within speed-type-buffer.")
(defvar-local speed-type--time-register nil
"Holds timestamps and used to calculate duration of a speed-type session.")
(defvar-local speed-type--last-modified-tick nil
"Detect property-only-changes between before- and after-functions.
It's a property-only-change when modified-tick is the same in before and after.")
(defvar-local speed-type--last-changed-text nil
"Store characters which are going be compared against actual.
It's used in the before-change-hook.")
(defvar-local speed-type--buffer nil)
(defvar-local speed-type--content-buffer nil)
(defvar-local speed-type--entries 0 "Count the number of inserted characters typed (but each only ones).")
(defvar-local speed-type--actions 0 "Count the number of key typed (which are bound to a command).")
(defvar-local speed-type--errors 0 "Counts mistyped characters.")
(defvar-local speed-type--current-correct-streak 0 "Tracks the correct streak since last error or beginning.")
(defvar-local speed-type--best-correct-streak 0 "The highest count of consecutively correct typed characters.")
(defvar-local speed-type--non-consecutive-errors 0 "Counts mistyped characters but only if previous was correct.")
(defvar-local speed-type--corrections 0
"Counts the speed-type-status transition of characters from error to correct.")
(defvar-local speed-type--title nil
"Holds the title of the speed-type session.")
(defvar-local speed-type--author nil
"Holds the author of the speed-type session.")
(defvar-local speed-type--lang nil
"Holds the language of the typing content.")
(defvar-local speed-type--n-words nil
"Specifies the limit for picking a random word from a word frequency list.")
(defvar-local speed-type--add-extra-word-content-fn nil
"Generate word which is going to be added to the speed-type session on error.")
(defvar-local speed-type--extra-words-animation-timer nil
"Timer which inserts characters one by one for a fancy insertion-animation.")
(defvar-local speed-type--extra-words-queue '()
"Holds characters which are inserted by speed-type--extra-words-animation-timer.")
(defvar-local speed-type--go-next-fn nil
"Procecures how to call setup for the next-action.")
(defvar-local speed-type--continue-fn nil
"Procecures how to call setup for the continue-action.")
(defvar-local speed-type--replay-fn nil
"Procecures how to call setup for the replay-action.")
(defvar-local speed-type--extra-word-quote nil
"Used for \"quote boundary\" for `speed-type--add-extra-word-content-fn'.")
(defvar-local speed-type--idle-pause-timer nil
"Trigger code if speed-type session stays untouched for a certain duration.")
(defun speed-type--resume ()
"Resume the current typing session.
Adding the idle timer again, and pushing the newest time to stack."
(unless speed-type--idle-pause-timer
(setq speed-type--idle-pause-timer (run-with-idle-timer speed-type-pause-delay-seconds nil #'speed-type-pause)
speed-type--time-register (append speed-type--time-register (list (float-time))))))
(defun speed-type-pause ()
"Pushes the current time to the start-time variable.
The list of times is used to calculate the overall active typing time."
(interactive)
(message "Speed-type: session is paused. Resume will be triggered on buffer-change.")
(when speed-type--idle-pause-timer
(setq speed-type--idle-pause-timer nil
speed-type--time-register (append speed-type--time-register (list (float-time))))))
(defun speed-type--/ (number divisor)
"Divide NUMBER by DIVISOR when DIVISOR is not null.
Otherwise return 0."
(if (zerop divisor)
0
(/ number divisor)))
(defun speed-type--seconds-to-minutes (seconds)
"Return minutes in float for SECONDS."
(/ seconds 60.0))
(defun speed-type--gross-wpm (entries seconds)
"Return gross words-per-minute.
Computes words-per-minute as (ENTRIES/5) / (SECONDS/60)."
(round (speed-type--/ (/ entries 5.0)
(speed-type--seconds-to-minutes seconds))))
(defun speed-type--gross-Xpm (entries seconds)
"Return gross characters-per-minute.
Computes characters-per-minute as ENTRIES / (SECONDS/60)."
(round (speed-type--/ entries (speed-type--seconds-to-minutes seconds))))
(defun speed-type--net-wpm (entries errors corrections seconds)
"Return net words-per-minute.
Computes net words-per-minute as:
UNCORRECTED-ERRORS = ERRORS - CORRECTIONS
((ENTRIES/5) - UNCORRECTED-ERRORS) / (SECONDS/60)."
(let ((net-wpm (round (- (speed-type--gross-wpm entries seconds)
(speed-type--/ (- errors corrections)
(speed-type--seconds-to-minutes seconds))))))
(if (> 0 net-wpm) 0 net-wpm)))
(defun speed-type--net-cpm (entries errors corrections seconds)
"Return net characters-per-minute.
Computes net characters-per-minute as:
UNCORRECTED-ERRORS = ERRORS - CORRECTIONS
(ENTRIES - UNCORRECTED-ERRORS) / (SECONDS/60)."
(let ((net-cpm (round (- (speed-type--gross-Xpm entries seconds)
(speed-type--/ (- errors corrections)
(speed-type--seconds-to-minutes seconds))))))
(if (> 0 net-cpm) 0 net-cpm)))
(defun speed-type--accuracy (total-entries correct-entries corrections)
"Return accuracy.
Accuracy is computed as (CORRECT-ENTRIES - CORRECTIONS) / TOTAL-ENTRIES."
(let* ((correct-entries (- correct-entries corrections))
(correct-entries (if (> correct-entries 0) correct-entries 0)))
(* (round (* (speed-type--/ correct-entries (float total-entries)) 100.0) 0.01) 0.01)))
(defun speed-type--skill (wpm)
"Return skill for WPM."
(cond
((null wpm) "Zero or Infinity")
((< wpm 25) "Rookie")
((< wpm 30) "Novice")
((< wpm 40) "Adept")
((< wpm 55) "Expert")
((< wpm 80) "Master")
(t "Legend")))
(defvar speed-type-coding-system 'utf-8-unix
"The coding system speed-type uses for saving the stats.
Changing this value while Emacs is running is supported, but considered
unwise, unless you know what you are doing.")
(defconst speed-type-file-format-version 1
"The current version of the format used by speed-type statistic files.
You should never need to change this.
- 0 = initial version.
- 1 = fix by maybe adding a newline")
(defun speed-type-statistic-variables ()
"Define the structure of raw-data used for calculating the median-stats.
If the structure is changed, SPEED-TYPE-FILE-FORMAT-VERSION must
be incremented and a migration must be coded in
SPEED-TYPE-MAYBE-UPGRADE-FILE-FORMAT."
(let ((entries speed-type--entries)
(actions speed-type--actions)
(errors speed-type--errors)
(corrections speed-type--corrections)
(seconds (speed-type--elapsed-time speed-type--time-register)))
(list (cons 'speed-type--create-time (decode-time (float-time) (current-time-zone)))
(cons 'speed-type--title speed-type--title)
(cons 'speed-type--author speed-type--author)
(cons 'speed-type--lang speed-type--lang)
(cons 'speed-type--n-words speed-type--n-words)
(cons 'speed-type--entries entries)
(cons 'speed-type--errors errors)
(cons 'speed-type--non-consecutive-errors speed-type--non-consecutive-errors)
(cons 'speed-type--corrections corrections)
(cons 'speed-type--elapsed-time seconds)
(cons 'speed-type--time-register speed-type--time-register)
(cons 'speed-type--gross-wpm (speed-type--gross-wpm entries seconds))
(cons 'speed-type--gross-cpm (speed-type--gross-Xpm entries seconds))
(cons 'speed-type--gross-apm (speed-type--gross-Xpm actions seconds))
(cons 'speed-type--apc (speed-type--/ (float actions) entries))
(cons 'speed-type--net-wpm (speed-type--net-wpm entries errors corrections seconds))
(cons 'speed-type--net-cpm (speed-type--net-cpm entries errors corrections seconds))
(cons 'speed-type--accuracy (speed-type--accuracy entries (- entries errors) corrections))
(cons 'speed-type--continue-at-point (unless speed-type--randomize (speed-type--get-continue-point)))
(cons 'speed-type--file-name speed-type--file-name)
(cons 'speed-type--best-correct-streak speed-type--best-correct-streak))))
(defun speed-type--stop-word-p (word)
"Return given WORD when it is a stop-word.
What a stop-word is, is defined by `speed-type-stop-words'."
(cond ((not (stringp word)) (error "Given WORD(%s) must be a string" word))
((listp speed-type-stop-words) (car (member word speed-type-stop-words)))
((not (stringp speed-type-stop-words)) (error "Custom variable SPEED-TYPE-STOP-WORDS(%s) must be a list or filename" speed-type-stop-words))
((file-readable-p (expand-file-name speed-type-stop-words speed-type-directory))
(with-current-buffer (find-file-noselect (expand-file-name speed-type-stop-words speed-type-directory) t)
(when (save-excursion (goto-char (point-min)) (re-search-forward (concat "^" word "$") nil t 1)) word)))
(t (user-error "Custom variable `speed-type-stop-words' must be a list or a filename in `speed-type-directory'"))))
(defun speed-type-grok-file-format-version ()
"Integer which indicates the file-format version of speed-type statistic file.
Expects to be called from `point-min' in a speed-type statistic file."
(if (looking-at "^;;;;")
(save-excursion
(save-match-data
(re-search-forward "[0-9]")
(forward-char -1)
(read (current-buffer))))
;; Else this is format version 0, the original one, which didn't
;; even have version stamps.
0))
(defun speed-type--maybe-insert-newline ()
"Move last closing parantheses to own line if not already.
We use `pp' to write the buffer state to the statistic-file.
Since `pp' works a bit differently on EMACS 30.1 for some
reason. It is necessary to check if the last closing parantheses
has it's own line.
This function fixes this, because otherwise we fail to load the file."
(save-excursion
(when (null (progn (goto-char (point-max))
(re-search-backward "^)" nil t 1)))
(re-search-backward ")" nil t 1)
(insert "\n"))))
(defun speed-type-maybe-upgrade-file-format ()
"Check the file-format version of current file.
If the version is not up-to-date, upgrade it automatically.
This expects to be called from `point-min' in a speed-type statistic file."
(let ((version
(with-suppressed-warnings ((obsolete speed-type-grok-file-format-version)) ;; we use the same mechanism as bookmark file
(speed-type-grok-file-format-version))))
(cond
((= version speed-type-file-format-version))
((= version 0) (speed-type--maybe-insert-newline))
(t (error "Speed-type statistic file format version strangeness")))))
(defconst speed-type-end-of-version-stamp-marker
"-*- End Of Speed Type File Format Version Stamp -*-\n"
"This string marks the end of the version stamp in a speed-type statistic file.")
(defun speed-type-insert-file-format-version-stamp (coding)
"Insert text indicating current version of speed-type statistic file format.
CODING is the symbol of the coding-system in which the file is encoded."
(if (memq (coding-system-base coding) '(undecided prefer-utf-8))
(setq coding 'utf-8-emacs))
(insert
(format
";;;; Emacs Speed-type statisitic Format Version %d\
;;;; -*- coding: %S; mode: lisp-data -*-\n"
speed-type-file-format-version (coding-system-base coding)))
(insert ";;; This format is meant to be slightly human-readable;\n"
";;; nevertheless, you probably don't want to edit it.\n"
";;; "
speed-type-end-of-version-stamp-marker))
(defun speed-type--find-last-continue-at-point-in-stats (file-name)
"Search for last speed-type--continue-at-point in given FILE-NAME.
When given FILE-NAME is nil will return nil.
If SPEED-TYPE-STATISTIC-FILE-NAME does not exists yet, will return nil.
If no entries are found with FILE-NAME, will return nil.
Expects FILE-NAME to be a speed-type-stats-file.
Create stats-file with function `speed-type-save-stats'."
(unless (or (null file-name)
(stringp file-name))
(error "Wrong type of argument FILE-NAME(%s)" file-name))
(if file-name
(let ((last (cl-find-if (lambda (e) (and (string-equal-ignore-case (or (cdr (assoc 'speed-type--file-name e)) "") file-name)
(cdr (assoc 'speed-type--continue-at-point e))))
(nreverse (speed-type-load-last-stats speed-type-statistic-filename)))))
(cdr (assoc 'speed-type--continue-at-point last)))
nil))
(defun speed-type-save-stats-when-customized ()
"Check the custom variable SPEED-TYPE-SAVE-STATISTIC-OPTION and save stats."
(when (and (not (= 0 speed-type--entries)) (not (eq speed-type-save-statistic-option 'never)))
(when (if (eq speed-type-save-statistic-option 'ask) (y-or-n-p "Save statistic?") t)
(speed-type-save-stats speed-type-statistic-filename (with-current-buffer speed-type--buffer (speed-type-statistic-variables))))))
(defun speed-type-save-stats (file session-stats &optional alt-msg)
"Write given SESSION-STATS to FILE.
SESSIONS-STATS is a alist containing the local-variables of the typing-session:
- `((speed-type--entries . 2) ...)
See detailed alist SPEED-TYPE-STATISTIC-VARIABLES.
This function is heavily inspired by bookmark+-save-stats.
Non-nil ALT-MSG is a message format string to use in place of the
default, \"Saving statistics of current speed-type session to
file `%s'...\". The string must contain a `%s' construct, so that
it can be passed along with FILE to `format'. At the end,
\"done\" is appended to the message."
(unless (listp session-stats) (error "Given SESSION-STATS(%s) is not an alist" session-stats))
(unless (stringp file) (error "Given FILE(%s) is not a string" file))
(let ((msg (or alt-msg "Saving statistics of current speed-type session to file `%s'..."))
(coding-system-for-write speed-type-coding-system)
(print-length nil)
(print-level nil)
(existing-buf (get-file-buffer file))
(emacs-lisp-mode-hook nil) ; Avoid inserting automatic file header if existing empty file, so
(lisp-mode-hook nil) ; better chance `speed-type-maybe-upgrade-file-format' signals error.
start end)
(when (file-directory-p file) (error "FILE(%s) is a directory, not a file" file))
(message msg (abbreviate-file-name file))
(with-current-buffer (let ((enable-local-variables ())) (find-file-noselect file))
(goto-char (point-min))
(if (file-exists-p file)
(speed-type-maybe-upgrade-file-format)
(delete-region (point-min) (point-max)) ; In case a find-file hook inserted a header, etc.
(unless (boundp 'speed-type-coding-system) ; Emacs < 25.2.
(speed-type-insert-file-format-version-stamp coding-system-for-write))
(insert "(\n)"))
(setq start (and (file-exists-p file)
(or (save-excursion (goto-char (point-min))
(search-forward (concat speed-type-end-of-version-stamp-marker "(")
nil t))
(error "Invalid %s" file)))
end (and start
(or (save-excursion (goto-char start) (and (looking-at ")") start))
(save-excursion (goto-char (point-max)) (re-search-backward "^)" nil t))
(error "Invalid %s" file))))
(if (not start) ; New file, no header yet.
(goto-char 2)
;; Existing file - delete old entry unless max is not reached. Rolling.
(when (> (/ (count-lines start end) (length (or (speed-type-statistic-variables) '(1)))) speed-type-max-num-records)
(save-excursion
(goto-char start)
(or (looking-at "(") (search-forward "(" nil t 1))
(let ((bounds (bounds-of-thing-at-point 'sexp)))
(kill-region (car bounds) (+ 1 (cdr bounds))))))
(goto-char (and start
(or (save-excursion (goto-char start) (and (looking-at ")") start))
(save-excursion (goto-char (point-max)) (re-search-backward "^)" nil t))
(error "Invalid %s" file)))))
(pp session-stats (current-buffer))
(speed-type--maybe-insert-newline)
(when (boundp 'speed-type-coding-system) ; Emacs 25.2+. See bug #25365
;; Make sure specified encoding can encode the speed-type stats. If not, suggest utf-8-emacs as default.
(with-coding-priority '(utf-8-emacs)
(setq coding-system-for-write (select-safe-coding-system (point-min) (point-max)
(list t coding-system-for-write))))
(when start (delete-region 1 (1- start))) ; Delete old header.
(goto-char 1)
(speed-type-insert-file-format-version-stamp coding-system-for-write))
(let ((require-final-newline t)
(errorp nil))
(condition-case nil
(write-file file)
(file-error (setq errorp t)
;; Do NOT raise error. (Need to be able to exit.)
(let ((msg (format "CANNOT WRITE FILE(%s)" file)))
(if (fboundp 'display-warning)
(display-warning 'speed-type msg)
(message msg)
(sit-for 4)))))
(when (boundp 'speed-type-coding-system) ; Emacs 25.2+
(setq speed-type-coding-system coding-system-for-write))
(unless existing-buf (kill-buffer (current-buffer)))
(unless errorp (message (concat msg "done") file))))))
(defun speed-type-stats-list-from-buffer ()
"Read and return a speed-type stats list from the current buffer.
Point is irrelevant and unaffected."
(let ((stats (save-excursion
(goto-char (point-min))
(if (search-forward speed-type-end-of-version-stamp-marker nil t)
(condition-case err
(read (current-buffer))
(error (error "Cannot read definitions in speed type statistic file: %s"
(error-message-string err))))
;; Else we're dealing with format version 0
(error "Buffer is not in speed-type statistic format")))))
stats))
(defun speed-type--calc-standard-deviation (symbol stats)
(unless (symbolp symbol) (error "Given SYMBOL(%s) is not a symbol" symbol))
(unless (listp stats) (error "Given STATS(%s) is not an list" stats))
(or (when-let* ((numbers (sort (remove nil (mapcar (lambda (e) (cdr (assoc symbol e))) stats)) '<))
(avg (speed-type--calc-avg symbol stats))
(sum-of-variance (apply '+ (mapcar (lambda (n) (expt (- n avg) 2)) numbers)))
(num-of-records (length numbers))
(standard-deviation (sqrt (/ sum-of-variance num-of-records))))
standard-deviation)
0))
(defun speed-type--calc-max (symbol stats &optional comparator-fn)
(unless (symbolp symbol) (error "Given SYMBOL(%s) is not a symbol" symbol))
(unless (listp stats) (error "Given STATS(%s) is not an list" stats))
(or (when-let* ((numbers (sort (remove nil (mapcar (lambda (e) (cdr (assoc symbol e))) stats)) (or comparator-fn '>=)))
(max (nth 0 numbers)))
max)
0))
(defun speed-type--calc-min (symbol stats &optional comparator-fn)
(unless (symbolp symbol) (error "Given SYMBOL(%s) is not a symbol" symbol))
(unless (listp stats) (error "Given STATS(%s) is not an list" stats))
(or (when-let* ((numbers (sort (remove nil (mapcar (lambda (e) (cdr (assoc symbol e))) stats)) (or comparator-fn '<)))
(min (nth 0 numbers)))
min)
0))
(defun speed-type--calc-avg (symbol stats)
"Calculate the average of given SYMBOL in STATS."
(unless (symbolp symbol) (error "Given SYMBOL(%s) is not a symbol" symbol))
(unless (listp stats) (error "Given STATS(%s) is not an list" stats))
(or (when-let* ((numbers (remove nil (mapcar (lambda (e) (cdr (assoc symbol e))) stats)))
(sum-of-records (apply '+ numbers))
(num-of-records (length numbers))
(avg (/ sum-of-records num-of-records)))
avg)
0))
(defun speed-type--calc-median (symbol stats)
"Calculate the median of given SYMBOL in STATS."
(unless (and (not (eq t symbol)) (not (null symbol)) (symbolp symbol)) (error "Given SYMBOL(%s) is not a symbol" symbol))
(unless (listp stats) (error "Given STATS(%s) is not an list" stats))
(or (when-let* ((numbers (sort (remove nil (mapcar (lambda (e) (cdr (assoc symbol e))) stats)) '<))
(num-of-records (length numbers))
(medians (if (eq (% num-of-records 2) 0)
(/ (+ (nth (- (/ num-of-records 2) 1) numbers)
(nth (/ num-of-records 2) numbers))
2)
(nth (/ num-of-records 2) numbers))))
medians)
0))
(defun speed-type--calc-stats (stats)
"Calculate the median of each numerical value in STATS.
Additional provide length and skill-value."
(if stats
(let ((median-gross-wpm (speed-type--calc-median 'speed-type--gross-wpm stats))
(avg-gross-wpm (speed-type--calc-avg 'speed-type--gross-wpm stats))
(min-gross-wpm (speed-type--calc-min 'speed-type--gross-wpm stats))
(max-gross-wpm (speed-type--calc-max 'speed-type--gross-wpm stats)))
(list
(length stats) (format-time-string "%F %T" (encode-time (speed-type--calc-min 'speed-type--create-time stats (lambda (e1 e2) (time-less-p (encode-time e1) (encode-time e2)))))) (format-time-string "%F %T" (encode-time (speed-type--calc-max 'speed-type--create-time stats (lambda (e1 e2) (time-less-p (encode-time e2) (encode-time e1))))))
(speed-type--skill median-gross-wpm) (speed-type--skill avg-gross-wpm) (speed-type--skill min-gross-wpm) (speed-type--skill max-gross-wpm)
(speed-type--calc-median 'speed-type--net-wpm stats) (speed-type--calc-avg 'speed-type--net-wpm stats) (speed-type--calc-standard-deviation 'speed-type--net-wpm stats) (speed-type--calc-min 'speed-type--net-wpm stats) (speed-type--calc-max 'speed-type--net-wpm stats)
(speed-type--calc-median 'speed-type--net-cpm stats) (speed-type--calc-avg 'speed-type--net-cpm stats) (speed-type--calc-standard-deviation 'speed-type--net-cpm stats) (speed-type--calc-min 'speed-type--net-cpm stats) (speed-type--calc-max 'speed-type--net-cpm stats)
median-gross-wpm avg-gross-wpm (speed-type--calc-standard-deviation 'speed-type--gross-wpm stats) min-gross-wpm max-gross-wpm
(speed-type--calc-median 'speed-type--gross-cpm stats) (speed-type--calc-avg 'speed-type--gross-cpm stats) (speed-type--calc-standard-deviation 'speed-type--gross-cpm stats) (speed-type--calc-min 'speed-type--gross-cpm stats) (speed-type--calc-max 'speed-type--gross-cpm stats)
(speed-type--calc-median 'speed-type--gross-apm stats) (speed-type--calc-avg 'speed-type--gross-apm stats) (speed-type--calc-standard-deviation 'speed-type--gross-apm stats) (speed-type--calc-min 'speed-type--gross-apm stats) (speed-type--calc-max 'speed-type--gross-apm stats)
(speed-type--calc-median 'speed-type--apc stats) (speed-type--calc-avg 'speed-type--apc stats) (speed-type--calc-standard-deviation 'speed-type--apc stats) (speed-type--calc-min 'speed-type--apc stats) (speed-type--calc-max 'speed-type--apc stats)
(speed-type--calc-median 'speed-type--accuracy stats) (speed-type--calc-avg 'speed-type--accuracy stats) (speed-type--calc-standard-deviation 'speed-type--accuracy stats) (speed-type--calc-min 'speed-type--accuracy stats) (speed-type--calc-max 'speed-type--accuracy stats)
(speed-type--calc-median 'speed-type--elapsed-time stats) (speed-type--calc-avg 'speed-type--elapsed-time stats) (speed-type--calc-standard-deviation 'speed-type--elapsed-time stats) (speed-type--calc-min 'speed-type--elapsed-time stats) (speed-type--calc-max 'speed-type--elapsed-time stats)
(speed-type--calc-median 'speed-type--entries stats) (speed-type--calc-avg 'speed-type--entries stats) (speed-type--calc-standard-deviation 'speed-type--entries stats) (speed-type--calc-min 'speed-type--entries stats) (speed-type--calc-max 'speed-type--entries stats)
(speed-type--calc-median 'speed-type--corrections stats) (speed-type--calc-avg 'speed-type--corrections stats) (speed-type--calc-standard-deviation 'speed-type--corrections stats) (speed-type--calc-min 'speed-type--corrections stats) (speed-type--calc-max 'speed-type--corrections stats)
(speed-type--calc-median 'speed-type--best-correct-streak stats) (speed-type--calc-avg 'speed-type--best-correct-streak stats) (speed-type--calc-standard-deviation 'speed-type--best-correct-streak stats) (speed-type--calc-min 'speed-type--best-correct-streak stats) (speed-type--calc-max 'speed-type--best-correct-streak stats)
(speed-type--calc-median 'speed-type--errors stats) (speed-type--calc-avg 'speed-type--errors stats) (speed-type--calc-standard-deviation 'speed-type--errors stats) (speed-type--calc-min 'speed-type--errors stats) (speed-type--calc-max 'speed-type--errors stats)
(speed-type--calc-median 'speed-type--non-consecutive-errors stats) (speed-type--calc-avg 'speed-type--non-consecutive-errors stats) (speed-type--calc-standard-deviation 'speed-type--non-consecutive-errors stats) (speed-type--calc-min 'speed-type--non-consecutive-errors stats) (speed-type--calc-max 'speed-type--non-consecutive-errors stats)))
'(0 "empty" "empty" "empty" "empty" "empty" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)))
(defun speed-type-display-menu ()
"Display and set controls the user can make in this speed-type session.
leave buffer in read-only mode."
(read-only-mode -1)
(insert "\n\n"
(format " [%s]uit\n"
(propertize "q" 'face 'highlight))
(format " [%s]eplay this sample\n"
(propertize "r" 'face 'highlight)))
(when speed-type--continue-fn
(insert (format " [%s]ontinue on this sample\n"
(propertize "c" 'face 'highlight))))
(when (not (eq 'never speed-type-save-statistic-option))
(insert (format " [%s]isplay statistic\n"
(propertize "d" 'face 'highlight))))
(when (not (null speed-type-provide-preview-option))
(insert (format " [%s]oggle preview\n"
(propertize "t" 'face 'highlight))))
(when speed-type--go-next-fn
(insert (format " [%s]ext random sample\n"
(propertize "n" 'face 'highlight))))
(let ((this-scroll-margin
(min (max 0 scroll-margin)
(truncate (/ (window-body-height) 4.0)))))
(recenter this-scroll-margin t))
(let ((view-read-only nil))
(read-only-mode))
(use-local-map speed-type-mode-completed-map))
(defun speed-type-load-last-stats (file)
"Load speed-type stats from FILE (which must be in the standard format).
Return the list of stats read from FILE.
If you use `speed-type--load-stats' to load a file that does not contain a
proper speed-type stats list, then when speed-type stats are saved the current
speed-type stats file will likely become corrupted. You should load only
speed-type files that were created using the speed-type functions."
;; Load.
(when (null file) (error "While loading stats given FILE was nil"))
(setq file (abbreviate-file-name (expand-file-name file)))
(when (file-directory-p file) (error "FILE(%s) is a directory, not a file" file))
(message "Loading speed-type stats from FILE(%s)..." file)
(if (file-readable-p file)
(let ((existing-buf (get-file-buffer file))
blist)
(with-current-buffer (let ((enable-local-variables ())) (find-file-noselect file))
(goto-char (point-min))
(speed-type-maybe-upgrade-file-format)
(setq blist (speed-type-stats-list-from-buffer))
(unless (listp blist) (error "Invalid speed-type stats list in FILE(%s)" file))
(when (boundp 'speed-type-coding-system) ; Emacs 25.2+
(setq speed-type-coding-system buffer-file-coding-system))
(unless (eq existing-buf (current-buffer)) (kill-buffer (current-buffer))))
(message "Speed-type stats in FILE(%s) loaded" file)
blist)
nil))
(defun speed-type--url-to-filename (url)
"Convert URL to a POSIX-standard compatible form.
The return value has no file-extension, it has to be appended
separately. When URL contains a file-extension it will become part
of the filename.
Return value can be used as filename except URL is nil or blank-string
in such a case return URL as is.
URL with different whitespace at end and/or begin result in the same
return value.
A shorten hash is appended to make the filename more unique.
Therefore `secure-hash-algorithms' should provide sha1 or md5 else
\"no-hash\" is appended instead of a real hash."
(if (or (null url) (string-blank-p url))
url
(let ((url (string-trim url)))
(concat
(let ((posix-str (replace-regexp-in-string "^-\\|-$" "" (replace-regexp-in-string "-\\." "." (replace-regexp-in-string "-+" "-" (replace-regexp-in-string "[^A-Za-z0-9-]" "-" url))))))
(substring posix-str 0 (min 64 (length posix-str))))
"-"
(let* ((hash-func (or (car (member 'sha1 (secure-hash-algorithms)))
(car (member 'md5 (secure-hash-algorithms)))))
(hash-str (or (when hash-func (funcall hash-func url)) "no-hash"))
(short-hash-str (substring hash-str 0 (min 7 (length hash-str)))))
short-hash-str)))))
(defconst speed-type-pandoc-request-header "\"User-Agent:Emacs: speed-type/1.4 https://github.com/dakra/speed-type\""
"This const is used when pandoc is retrieving content from an url.")
(defun speed-type--pandoc-top-filename (url)
"Create a filename using URL for a top-x-file."
(expand-file-name (format "%s-top-x.txt" (speed-type--url-to-filename url)) speed-type-directory))
(defun speed-type-retrieve-pandoc (url)