-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhacker-typer.el
326 lines (287 loc) · 16.8 KB
/
hacker-typer.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
;;; hacker-typer.el --- Pretend to write code like a pro -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Diego A. Mundo
;; Author: Diego A. Mundo <[email protected]>
;; URL: http://github.com/therockmandolinist/emacs-hacker-typer
;; Git-Repository: git://github.com/therockmandolinist/emacs-hacker-typer.git
;; Created: 2016-01-20
;; Version: 1.0.6
;; Keywords: hacker typer multimedia games
;; Package-Requires: ((emacs "24"))
;; 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 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:
;; This package provides a customizable implementation of hackertyper.com in
;; Emacs for your amusement. It opens a buffer in which typing any letter
;; inserts a piece of a specified or randomly chosen script. It can also pull
;; up a picture of "hackerman" on command.
;;; Code:
(defcustom hacker-typer-show-hackerman nil
"If t, show hackerman on calling `hacker-typer'."
:group 'hacker-typer
:type 'boolean)
(defcustom hacker-typer-data-dir
(let* ((parent-dir (if (featurep 'no-littering)
no-littering-var-directory
user-emacs-directory))
(std-dir (expand-file-name (convert-standard-filename "hacker-typer/")
parent-dir)))
std-dir)
"Directory in which to store ‘hacker-typer’ files.
If no-littering is installed, defaults to
no-littering-var-directory/hacker-typer. Otherwise, defaults to
~/.emacs.d/hacker-typer."
:group 'hacker-typer
:type 'string)
(defcustom hacker-typer-type-rate 'random
"The number of characters to type per keystroke.
If set to an integer, types that many characters per keystroke.
If set to 'random (default), types between 3-6 characters, with
the exact number chosen at random. See
`hacker-typer-random-range' to customize this range."
:group 'hacker-typer
:type '(choice
(const :tag "Random" random)
(integer :tag "Fixed" 5)))
(defcustom hacker-typer-random-range '(3 6)
"Range for `hacker-typer-typer-rate' when set to 'random.
The range from which to select how many characters will be typed
at each keystroke (with the first element being the min value,
the second being the max)."
:group 'hacker-typer
:type '(list integer integer))
(defcustom hacker-typer-files
'("https://raw.githubusercontent.com/emacs-mirror/emacs/master/src/emacs.c"
"https://raw.githubusercontent.com/emacs-mirror/emacs/master/src/xselect.c"
"https://raw.githubusercontent.com/emacs-mirror/emacs/master/src/w32font.c"
"https://raw.githubusercontent.com/emacs-mirror/emacs/master/lib-src/ebrowse.c"
"https://raw.githubusercontent.com/emacs-mirror/emacs/master/lwlib/xlwmenu.c")
"A list of files for ‘hacker-typer’ to randomly choose from.
Can be web urls that point directly to files, or local files of
the form: file:///absolute/path/to/file"
:group 'hacker-typer
:type '(repeat string))
(defcustom hacker-typer-remove-comments nil
"If set to t, remove comments from files. This is done using `comment-kill'."
:group 'hacker-typer
:type 'boolean)
(defvar hacker-typer-map (make-sparse-keymap)
"Hacker-typer key map.")
(defvar hacker-typer--start 0)
(defvar hacker-typer--end 0)
(defvar hacker-typer--previous-buffer nil)
;;;###autoload
(defun hacker-typer (arg)
"Start hacker-typer.
This function randomly selects a file from `hacker-typer-files',
downloading it if necessary, and creates an empty buffer in which
every keystroke types out some characters according to
`hacker-typer-type-rate', a la `http://hackertyper.com'. The
buffer is named after the file, with characters randomly
prepended.
If `hacker-typer-show-hackerman' is set to t, also show an
amusing picture of Rami Malek as \"hackerman\".
With prefix argument ARG, prompt for a file to type."
(interactive "P")
(let* ((hack-file (if arg
(hacker-typer--choose-file
(read-file-name "Choose file: "))
(hacker-typer--choose-file)))
(hacker-typer-buffer-name (file-name-nondirectory hack-file))
(mycharset "0123456789abcdefghijklmnopqrstuvwxyz")
(rate-range (apply 'number-sequence hacker-typer-random-range))
(inc-amount (if (eq hacker-typer-type-rate 'random)
(elt rate-range (random (length rate-range)))
hacker-typer-type-rate)))
;; show hackerman if var set
(when hacker-typer-show-hackerman
(hacker-typer-hackerman nil))
;; delete previous hacker typer buffer if exists
(when (and hacker-typer--previous-buffer
(get-buffer hacker-typer--previous-buffer))
(kill-buffer hacker-typer--previous-buffer))
;; set start/end for fetching from file
(setq hacker-typer--start 0)
(setq hacker-typer--end inc-amount)
;; add random chars to buffer name and open buffer
(dotimes (i 20)
(setq hacker-typer-buffer-name
(concat (char-to-string (elt mycharset (random (length mycharset))))
hacker-typer-buffer-name)))
(setq hacker-typer--previous-buffer hacker-typer-buffer-name)
(get-buffer-create hacker-typer-buffer-name)
(switch-to-buffer hacker-typer-buffer-name)
(hacker-typer--set-mode hacker-typer-buffer-name)
;; unbind annoying modes
(dolist (mode '(aggressive-indent-mode
smartparens-mode
whitespace-mode
evil-smartparens-mode))
(when (fboundp mode)
(funcall mode 0)))
;; rebind commands locally and and use map
(dolist (cmd '(self-insert-command
newline
left-char
right-char
previous-line
next-line))
(when (fboundp cmd)
(define-key hacker-typer-map `[remap ,cmd]
`(lambda () (interactive) (hacker-typer--insert-contents ,hack-file)))))
(define-key hacker-typer-map (kbd "DEL")
`(lambda () (interactive) (hacker-typer--insert-contents ,hack-file)))
(define-key hacker-typer-map [remap keyboard-quit] 'hacker-typer-quit)
(use-local-map hacker-typer-map)
(when (fboundp 'evil-insert-state)
(evil-insert-state))))
;;;###autoload
(defun hacker-typer-quit ()
"Kill ‘hacker-typer’ buffers."
(interactive)
(when (get-buffer hacker-typer--previous-buffer)
(kill-buffer hacker-typer--previous-buffer))
(when (get-buffer "*hackerman*")
(kill-buffer "*hackerman*"))
(delete-other-windows))
;;;###autoload
(defun hacker-typer-hackerman (arg)
"⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⢀⣀⣀⣀⣀⢀⡀⣀⣀⢀⡀⣀⢀⣀⣀⢀⢀⢀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣻⣻⣯⢷⢺⣻⣟⢻⣟⣻⡟⣯⢻⣾⢟⢿⢺⣟⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢽⣻⣵⠞⠌⣁⣬⣬⣬⣫⣮⣭⡯⡗⢭⢽⢻⣟⢯⢯⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣟⢟⡞⢀⣤⣾⣿⣿⣿⣿⠿⢿⣶⣹⢹⡽⡻⣹⢺⢽⣯⢾⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⢿⢇⣰⣷⣿⣿⡯⠩⠁⢀⠀⠀⠹⣿⣳⢪⣮⡗⢮⣯⢧⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣯⡽⡷⣿⣯⣿⣿⣿⡗⠂⣠⣀⣀⡀⣤⡤⣿⣮⣕⡖⢻⣟⣼⣿⣷⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⡿⣯⡷⣟⡟⣟⣿⣿⣿⣿⣿⡏⣿⣿⣿⠇⠈⠿⡛⢹⣿⡞⢮⢞⣺⣮⣾⣯⣾⢿⢿⣿⣿⣻⢯⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣟⣯⣿⣿⣿⣿⣿⢿⣽⣽⣿⣟⣿⣽⣯⣻⢯⡻⣟⢟⢟⣿⣿⣿⣿⣿⡣⠘⠊⠋⢀⣠⡀⠀⠀⣿⣯⣮⣟⣗⡽⡻⣞⢽⢻⣽⣽⣽⡿⣿⡟⣿⡷⣿⣿⣿⣻
⡮⡾⢽⡟⢻⡷⢻⣯⣯⣿⣯⢿⣟⣾⣻⣯⡧⡽⡯⣯⡽⡽⣟⣽⠿⣿⣿⣿⣿⢳⢣⢔⢀⢤⡦⢴⠂⢠⣿⣟⡖⢳⠚⣽⡽⡻⣻⢽⣾⢼⣾⢽⣷⢽⣯⣿⣻⢾⢟⣿
⡯⣟⣿⣟⣿⣿⣻⡷⣽⣼⣾⣽⣧⣯⣽⣼⣟⣯⢿⠗⡏⢯⣧⡞⢿⣿⣿⣿⣿⣿⣮⣱⡕⠵⠙⠋⠀⣼⣟⣟⣮⣯⣻⡳⣗⣟⢾⣽⣾⣽⣾⣽⣾⣿⣧⣯⣾⣽⣿⣷
⣟⣿⣽⣿⣾⣿⣿⣯⣿⣷⣻⣿⣿⣾⣾⣿⣿⢋⠥⣸⡔⡧⢲⡿⣻⣭⣽⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⡿⣻⡟⣻⣾⣽⢿⣟⡿⣿⣿⣾⣿⣻⣿⣾⣿⣿⣳⣾⣟⣿⣷
⣽⣯⣷⣿⣿⣿⣿⣿⣷⣿⣿⣟⣿⣾⣿⣟⢕⠹⣿⣧⣽⢯⣿⣽⡞⡲⣻⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣟⢿⣿⣿⣿⣿⣾⣿⣿⣿⣾⣿⣿⣿⢿⣿⣷⣿⣟
⣾⡽⣾⣿⣿⣿⣿⣯⣿⣿⣿⣿⣿⣿⣿⡯⢥⡗⣥⣫⡭⣟⢻⣿⣿⣟⣷⣧⢽⢾⣿⡿⣿⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣾⣿⣿⣾
⣿⢿⣿⣻⣿⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⡯⢕⢝⡞⣲⣹⡎⡯⣟⡿⢾⣯⢿⡽⡻⡽⣿⢸⣿⣿⣿⣿⣞⢳⢝⣻⢿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣷⣿⣿⣿⣿⣿⣿⡿⣻⣿⣿⣿⣿⣿⣿⡣⢬⣱⣎⡧⡕⡮⣕⣬⣽⣿⣽⣿⣟⣿⢻⡝⢺⣿⣟⣿⣯⢯⣮⣿⣧⢽⣼⣿⣿⣿⣿⣻⣿⣿⣿⣿⣿⣿⣿⣷⣽⣿⣿⣿
⣿⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣫⢬⡞⡺⣹⡳⡼⡧⣯⡟⣏⣻⣿⣿⣿⣿⡗⣿⣿⣿⣯⣟⢯⣿⣿⣿⣿⣧⢿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿
⣿⡿⣟⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣣⢻⢯⣾⣷⣽⣯⣷⡟⡿⡿⣟⣏⣻⣟⣟⢿⣿⣿⣷⣿⣟⣿⣿⣿⣟⣯⣵⣿⣿⣿⣿⣿⣿⣮⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣽⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⣞⣿⣻⣟⣞⣾⢷⣾⣿⣿⣿⣿⣿⣿⣿⣷⣷⣯⣼⣻⣿⣿⣿⣿⣿⣿⣿⣟⣽⣿⣿⣿⣿⣿⣿⣿⣮⠻⣿⣟⡻⣿⣿⣟
⣿⣟⣿⣿⣿⣿⣿⣿⣿⣿⡿⣻⣿⣿⣿⣿⣿⣿⣽⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣻⣝⡻⣻⣻⣷⣻
⣿⣿⣿⣿⣿⣿⣿⣿⣿⢏⣾⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⣛⣾⣽⣿⣼⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣝⢻⣿⣿
⣿⣿⣿⣿⣿⣿⣿⢿⣵⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣮⣷⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣬⢿
⣿⣿⣿⣿⣿⡿⣣⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷
⣿⣿⣿⡿⣫⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣫⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⠟⣴⠟⠛⢻⡿⠛⠻⣿⣿⠟⠛⢻⡟⡿⠽⠛⠛⠛⠛⠛⠛⢻⠿⠛⠛⠻⠟⠛⠛⠛⢛⠛⠛⠛⠛⠻⢿⠟⠛⠛⢿⡿⠛⠈⠻⣿⣿⠟⠛⠻⣻⣿⠟⠋⠻⣿⠛⠛⢿
⣿⡏⠀⠀⠾⠀⠀⢸⡿⠁⠀⠀⠐⡟⠁⠀⢠⣤⣤⡔⠀⠀⠀⠀⣠⠄⠀⠀⠤⠀⠠⠄⠀⠀⠤⠀⠀⠈⠀⠀⠀⠟⠁⠀⠀⠠⣿⠁⠀⠀⠀⣵⠅⠀⠀⠀⠊⠀⠠⣾
⣿⠧⢤⣆⡀⠴⢤⠟⠴⢤⠤⠤⠄⠠⠤⠤⡿⡿⡿⠣⠤⠤⡴⠰⣗⠤⠤⠠⠠⠔⠀⠴⠔⡔⠠⠆⢠⠥⠤⠀⠤⠤⠄⠰⠄⠆⠰⠴⠤⠴⠤⢽⠣⠴⣤⠤⠤⢦⣼⣿
⣗⡀⣐⣿⡃⣀⠀⢀⣠⣶⣾⣆⠀⣠⣀⣀⠀⡀⣀⢀⣀⣧⡀⡀⣘⣄⢀⣀⢀⢀⣆⣀⣸⣿⣀⡀⢈⣀⢀⣃⣀⢠⡆⣀⢀⠀⡀⠶⣶⡀⡀⢘⢀⣀⡿⣄⡀⢀⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠄⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿⠿"
(interactive "P")
(let ((hacker-path (or load-file-name buffer-file-name)))
(when (or (not hacker-path)
(not (equal (file-name-nondirectory hacker-path)
"hacker-typer.el")))
(setq hacker-path (locate-library "hacker-typer.el")))
(let ((hacker-file (expand-file-name "hackerman.png"
(file-name-directory hacker-path))))
;; Print file url to buffer and turn on iimage-mode
(with-output-to-temp-buffer "*hackerman*"
(princ (concat "file://" hacker-file)))
(with-current-buffer "*hackerman*"
(iimage-mode))
;; If prefix arg, delete other windows.
(when arg
(other-window 1)
(delete-other-windows)))))
;;;###autoload
(defalias 'hackerman 'hacker-typer-hackerman)
;;;###autoload
(defun hacker-typer-clear-cache ()
"Delete all data in `hacker-typer-data-dir'."
(interactive)
(dolist (f (directory-files hacker-typer-data-dir t))
(unless (file-directory-p f)
(delete-file f))))
;; utils
(defun hacker-typer--insert-contents (filename)
"Insert contents from FILENAME into ‘hacker-typer’ buffer."
(let* ((rate-range (apply 'number-sequence hacker-typer-random-range))
(inc-amount (if (eq hacker-typer-type-rate 'random)
(elt rate-range (random (length rate-range)))
hacker-typer-type-rate)))
(insert-file-contents filename nil hacker-typer--start hacker-typer--end)
(forward-char (- hacker-typer--end hacker-typer--start))
(setq hacker-typer--start (+ hacker-typer--end))
(setq hacker-typer--end (+ hacker-typer--start inc-amount))))
(defun hacker-typer--set-mode (buffer-name)
"Set major mode for ‘hacker-typer’ buffer named BUFFER-NAME based on buffer extension."
(let ((name buffer-name)
(mode nil))
;; Remove backup-suffixes from file name.
(setq name (file-name-sans-versions name))
;; Remove remote file name identification.
(while name
;; Find first matching alist entry.
(setq mode
;; Filesystem is case-sensitive.
(or
;; First match case-sensitively.
(let ((case-fold-search nil))
(assoc-default name auto-mode-alist
'string-match))
;; Fallback to case-insensitive match.
(and auto-mode-case-fold
(let ((case-fold-search t))
(assoc-default name auto-mode-alist
'string-match)))))
(if (and mode
(consp mode)
(cadr mode))
(setq mode (car mode)
name (substring name 0 (match-beginning 0)))
(setq name nil))
(when mode
(set-auto-mode-0 mode 'keep-mode-if-same)))))
(defun hacker-typer--choose-file (&optional filename)
"Randomly choose file from `hacker-typer-files'.
Downloads file and removes comments when necessary. If FILENAME
is provided, use that file instead."
(let* ((file-url (if filename
(concat "file://" (expand-file-name filename))
(elt hacker-typer-files
(random (length hacker-typer-files)))))
(base-name (file-name-nondirectory file-url))
(base-name-nc (concat "no-comment-" base-name))
(hacker-file (concat hacker-typer-data-dir base-name))
(hacker-file-nc (concat hacker-typer-data-dir base-name-nc)))
(make-directory hacker-typer-data-dir t)
;; If file doesn't exist, get it
(unless (or filename hacker-typer-remove-comments (file-exists-p hacker-file))
(url-copy-file file-url hacker-file t))
;; If remove-comments is on and uncommented file doesn't exist, get it.
(unless (or (not hacker-typer-remove-comments)
(file-exists-p hacker-file-nc))
(url-copy-file file-url hacker-file-nc t)
;; Remove comments.
(with-temp-file hacker-file-nc
(insert-file-contents hacker-file-nc)
(hacker-typer--set-mode hacker-file-nc)
(let (kill-ring)
(comment-kill (count-lines (point-min) (point-max))))
(goto-char (point-min))
(while (re-search-forward "\n\n+" nil t)
(replace-match "\n\n"))))
(if hacker-typer-remove-comments
hacker-file-nc
hacker-file)))
(provide 'hacker-typer)
;;; hacker-typer.el ends here