Skip to content

Commit cf44c4f

Browse files
committed
Fix go-next-fn in continue, improve pandoc-file-names
move hash at the end of url for better natural sort in directory
1 parent 9994204 commit cf44c4f

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

speed-type.el

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,14 @@ make the filename more unique.
827827
If `secure-hash-algorithms' should provide sha1 or md5 else the fixed
828828
string \"no-hash\" is used as a prefix instead of a real hash."
829829
(concat
830+
(let ((posix-str (replace-regexp-in-string "-\\." "." (replace-regexp-in-string "-+" "-" (replace-regexp-in-string "[^A-Za-z0-9-]" "-" url)))))
831+
(substring posix-str 0 (min 64 (length posix-str))))
832+
"-"
830833
(let* ((hash-func (or (car (member 'sha1 (secure-hash-algorithms)))
831834
(car (member 'md5 (secure-hash-algorithms)))))
832835
(hash-str (or (when hash-func (funcall hash-func url)) "no-hash"))
833836
(short-hash-str (substring hash-str 0 (min 7 (length hash-str)))))
834-
short-hash-str)
835-
"-"
836-
(let ((posix-str (replace-regexp-in-string "-\\." "." (replace-regexp-in-string "-+" "-" (replace-regexp-in-string "[^A-Za-z0-9-]" "-" url)))))
837-
(substring posix-str 0 (min 64 (length posix-str))))))
837+
short-hash-str)))
838838

839839
(defconst speed-type-pandoc-request-header "\"User-Agent:Emacs: speed-type/1.4 https://github.com/dakra/speed-type\""
840840
"This const is used when pandoc is retrieving content from an url.")
@@ -896,22 +896,23 @@ If the file is already retrieved, will return file-location."
896896

897897
(defun speed-type--retrieve (filename url)
898898
"Return buffer FILENAME content in it or download from URL if file doesn't exist."
899-
(let ((fn (expand-file-name (format "%s.txt" filename) speed-type-directory))
900-
(url-request-method "GET"))
901-
(if (file-readable-p fn)
902-
(find-file-noselect fn t)
903-
(make-directory speed-type-directory 'parents)
904-
(let ((buffer (url-retrieve-synchronously url nil nil 5)))
905-
(when (and buffer (= 200 (url-http-symbol-value-in-buffer
906-
'url-http-response-status
907-
buffer)))
908-
(with-current-buffer buffer
909-
(set-buffer-file-coding-system speed-type-coding-system)
910-
(recode-region url-http-end-of-headers (point-max) 'utf-8-dos speed-type-coding-system)
911-
(write-region url-http-end-of-headers (point-max) fn))
912-
(unless (kill-buffer buffer)
913-
(message "WARNING: Buffer is not closing properly"))
914-
(find-file-noselect fn t))))))
899+
(or (let ((fn (expand-file-name (format "%s.txt" filename) speed-type-directory))
900+
(url-request-method "GET"))
901+
(if (file-readable-p fn)
902+
(find-file-noselect fn t)
903+
(make-directory speed-type-directory 'parents)
904+
(let ((buffer (url-retrieve-synchronously url nil nil 5)))
905+
(when (and buffer (= 200 (url-http-symbol-value-in-buffer
906+
'url-http-response-status
907+
buffer)))
908+
(with-current-buffer buffer
909+
(set-buffer-file-coding-system speed-type-coding-system)
910+
(recode-region url-http-end-of-headers (point-max) 'utf-8-dos speed-type-coding-system)
911+
(write-region url-http-end-of-headers (point-max) fn))
912+
(unless (kill-buffer buffer)
913+
(message "WARNING: Buffer is not closing properly"))
914+
(find-file-noselect fn t)))))
915+
(error "Error retrieving book with URL(%s)" url)))
915916

916917
(defun speed-type--calculate-word-frequency (buffer start end)
917918
"Create a new buffer containing the words of given BUFFER ordered by frequency.
@@ -1905,25 +1906,26 @@ speed-type session with the assembled text."
19051906
If using a prefix while calling this function `C-u', then the FULL text
19061907
will be used. Else some text will be picked randomly."
19071908
(interactive "P")
1908-
(if full
1909-
(speed-type-region (point-min) (point-max))
1910-
(if speed-type-randomize
1911-
(let* ((buf (speed-type-prepare-content-buffer-from-buffer (current-buffer)))
1912-
(text (with-current-buffer buf (speed-type--pick-text-to-type)))
1913-
(line-count (with-current-buffer buf (count-lines (point-min) (point-max))))
1914-
(go-next-fn (lambda () (with-current-buffer buf (speed-type-buffer full)))))
1915-
(speed-type--setup buf
1916-
text
1917-
:file-name (buffer-file-name (current-buffer))
1918-
:title (buffer-name)
1919-
:author (user-full-name)
1920-
:randomize t
1921-
:replay-fn #'speed-type--get-replay-fn
1922-
:go-next-fn go-next-fn
1923-
:add-extra-word-content-fn (lambda () (speed-type--get-separated-thing-at-random-line buf line-count " "))
1924-
:syntax-table (syntax-table)
1925-
:fldf font-lock-defaults))
1926-
(speed-type-continue))))
1909+
(let ((cb (current-buffer)))
1910+
(if full
1911+
(speed-type-region (point-min) (point-max))
1912+
(if speed-type-randomize
1913+
(let* ((buf (speed-type-prepare-content-buffer-from-buffer (current-buffer)))
1914+
(text (with-current-buffer buf (speed-type--pick-text-to-type)))
1915+
(line-count (with-current-buffer buf (count-lines (point-min) (point-max))))
1916+
(go-next-fn (lambda () (with-current-buffer buf (speed-type-buffer full)))))
1917+
(speed-type--setup buf
1918+
text
1919+
:file-name (buffer-file-name (current-buffer))
1920+
:title (buffer-name)
1921+
:author (user-full-name)
1922+
:randomize t
1923+
:replay-fn #'speed-type--get-replay-fn
1924+
:go-next-fn go-next-fn
1925+
:add-extra-word-content-fn (lambda () (speed-type--get-separated-thing-at-random-line buf line-count " "))
1926+
:syntax-table (syntax-table)
1927+
:fldf font-lock-defaults))
1928+
(speed-type-continue (lambda () (with-current-buffer (speed-type-prepare-content-buffer-from-buffer cb) (speed-type-buffer full))))))))
19271929

19281930
;;;###autoload
19291931
(defun speed-type-text ()
@@ -1965,7 +1967,7 @@ will be used. Else some text will be picked randomly."
19651967
:go-next-fn #'speed-type-text
19661968
:continue-fn (lambda () (speed-type--get-continue-fn end))
19671969
:add-extra-word-content-fn (lambda () (speed-type--get-next-word buf))))
1968-
(speed-type-continue (buffer-file-name buffer)))
1970+
(speed-type-continue #'speed-type-text (buffer-file-name buffer)))
19691971
(kill-buffer buffer)))
19701972

19711973
;;;###autoload
@@ -2010,7 +2012,7 @@ If ARG is given will prompt for a specific quote-URL."
20102012
:add-extra-word-content-fn add-extra-word-content-fn)))
20112013

20122014
;;;###autoload
2013-
(defun speed-type-continue (&optional file-name)
2015+
(defun speed-type-continue (go-next-fn &optional file-name)
20142016
"Will continue where user left of in given FILE-NAME.
20152017
20162018
Find last speed-type--continue-at-point of FILE-NAME and setup a speed-type
@@ -2078,7 +2080,7 @@ If FILE-NAME is nil, will use file-name of CURRENT-BUFFER."
20782080
:title title
20792081
:author author
20802082
:replay-fn #'speed-type--get-replay-fn
2081-
:go-next-fn #'speed-type-text
2083+
:go-next-fn go-next-fn
20822084
:continue-fn (lambda () (speed-type--get-continue-fn end))
20832085
:add-extra-word-content-fn (lambda () (speed-type--get-next-word buf))
20842086
:syntax-table (syntax-table)
@@ -2095,7 +2097,8 @@ If the URL is already retrieved will reuse the stored content in
20952097
The file-name of the content is a converted form of URL."
20962098
(interactive "sURL: ")
20972099
(let* ((buffer (speed-type-retrieve-pandoc url))
2098-
(fn (buffer-file-name buffer)))
2100+
(fn (buffer-file-name buffer))
2101+
(go-next-fn (lambda () (call-interactively #'speed-type-pandoc))))
20992102
(if speed-type-randomize
21002103
(let* ((buf (speed-type-prepare-content-buffer-from-buffer buffer))
21012104
(title (buffer-name))
@@ -2107,10 +2110,10 @@ The file-name of the content is a converted form of URL."
21072110
:file-name fn
21082111
:title title
21092112
:replay-fn #'speed-type--get-replay-fn
2110-
:go-next-fn #'speed-type-pandoc
2113+
:go-next-fn go-next-fn
21112114
:continue-fn (lambda () (speed-type--get-continue-fn end))
21122115
:add-extra-word-content-fn (lambda () (speed-type--get-next-word buf))))
2113-
(speed-type-continue fn))
2116+
(speed-type-continue go-next-fn fn))
21142117
(kill-buffer buffer) ;; buffer is retrieved, remove it again to not clutter the buffer-list
21152118
))
21162119

0 commit comments

Comments
 (0)