This repository has been archived by the owner. It is now read-only.
forked from igrigorik/vimgolf
/
vimgolf.el
330 lines (263 loc) · 11.7 KB
/
vimgolf.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
;;; vimgolf.el --- VimGolf interface for the One True Editor
;; Copyright (C) never, by no one
;; Author: Tim Visher <tim.visher@gmail.com>
;; Maintainer: Tim Visher <tim.visher@gmail.com>
;; Created: 2011-11-02
;; Version: 0.1
;; Package-Version: 6a616502
;; Keywords: games vimgolf vim
;; This file is not part of GNU Emacs
;;; Commentary:
;; This is a simple package that allows Emacs users to compete on [VimGolf][1]
;; using the One True Editor. Competition can be commenced utilizing `M-x
;; vimgolf`. When finished with a challenge, `C-c C-v C-c` should finish your
;; editing, ensure correctness, and submit your score and keystrokes to
;; [VimGolf][1].
;;
;; On second thought, let's not go to Camelot. It's a silly place.
;;; Installation:
;; Use package.el. You'll need to add Marmalade to your archives:
;; (require 'package)
;; (add-to-list 'package-archives
;; '("marmalade" . "http://marmalade-repo.org/packages/"))
;; If you use a version of Emacs prior to 24 that doesn't include
;; package.el, you can get it from http://bit.ly/pkg-el23. If you have
;; an older package.el installed from tromey.com, you should upgrade
;; in order to support installation from multiple sources.
;;; License:
;; [CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/)
;;; Code:
(defgroup vimgolf nil
"Compete on VimGolf with the One True Editor."
:prefix "vimgolf-"
:group 'applications)
(defcustom vimgolf-key nil
"Your VimGolf API Key. Must be set in order to submit your solution."
:type 'string
:group 'vimgolf)
(defcustom vimgolf-mode-hook '((lambda () (whitespace-mode t)))
"A list of functions to call upon the initialization of vimgolf-mode."
:type 'hook
:group 'vimgolf)
(defvar vimgolf-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-v C-c") 'vimgolf-submit)
(define-key map (kbd "C-c C-v r") 'vimgolf-revert)
(define-key map (kbd "C-c C-v d") 'vimgolf-diff)
(define-key map (kbd "C-c C-v c") 'vimgolf-continue)
(define-key map (kbd "C-c C-v p") 'vimgolf-pause)
(define-key map (kbd "C-c C-v q") 'vimgolf-quit)
map))
(define-minor-mode vimgolf-mode
"Toggle VimGolf mode.
With no argument, this command toggles the mode. Non-null prefix
argument turns on the mode. Null prefix argument turns off the
mode.
When VimGolf mode is enabled, several key bindings are defined
with `C-c C-v` prefixes to help in playing VimGolf.
\\{vimgolf-mode-map}"
;; The initial value.
nil
;; The indicator for the mode line.
" VimGolf"
;; The minor mode bindings.
:keymap vimgolf-mode-map
:group 'vimgolf)
(defvar vimgolf-challenge nil)
(defvar vimgolf-prior-window-configuration nil)
(defvar vimgolf-working-window-configuration nil)
(defvar vimgolf-work-buffer-name "*vimgolf-work*")
(defvar vimgolf-start-buffer-name "*vimgolf-start*")
(defvar vimgolf-end-buffer-name "*vimgolf-end*")
(defvar vimgolf-keystrokes-buffer-name "*vimgolf-keystrokes*")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Keystroke logging
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro vimgolf-with-saved-command-environment (&rest body)
`(let ((deactivate-mark nil)
(this-command this-command)
(last-command last-command))
,@body))
(defun vimgolf-capturable-keystroke-p ()
(not (or executing-kbd-macro
(< 0 (recursion-depth))
(member this-command
'(digit-argument
negative-argument
universal-argument
universal-argument-other-key
universal-argument-minus
universal-argument-more))
(string-prefix-p "vimgolf-" (symbol-name this-command)))))
(defun vimgolf-capturable-dangling-keystroke-p ()
(member this-command
'(calc-dispatch)))
(defvar vimgolf-keystrokes nil
"A list of (keys-vector . command) pairs for the keystrokes entered.
Each entry is a cons cell containing a key sequence vector
suitable for use with `key-description', and a symbol for the
command that was executed as a result (which may be nil if an
unknown key sequence was entered).")
(defun vimgolf-maybe-capture-keystroke (pred)
"Store the keystrokes for `this-command' if result of calling function `PRED' is not nil."
(vimgolf-with-saved-command-environment
(when (funcall pred)
(setq vimgolf-keystrokes
(append vimgolf-keystrokes (list (cons (this-command-keys-vector)
this-command)))))))
(defun vimgolf-capture-keystroke ()
(vimgolf-maybe-capture-keystroke 'vimgolf-capturable-keystroke-p))
(defun vimgolf-capture-dangling-keystroke ()
(vimgolf-maybe-capture-keystroke 'vimgolf-capturable-dangling-keystroke-p))
(defun vimgolf-refresh-keystroke-log ()
"Refresh the contents of the keystrokes log buffer."
(when (not (< 0 (recursion-depth)))
(with-current-buffer (get-buffer-create vimgolf-keystrokes-buffer-name)
(erase-buffer)
(insert (format "Keystrokes (%d)\n\n" (vimgolf-count-keystrokes))
(mapconcat 'key-description (mapcar 'car vimgolf-keystrokes) " ")
"\n\nFull command log:\n\n")
(dolist (entry vimgolf-keystrokes)
(insert (key-description (car entry)))
(insert " ")
(princ (cdr entry) (current-buffer))
(insert "\n")))))
(defun vimgolf-enable-capture (enable)
"Enable keystroke logging if `ENABLE' is non-nil otherwise disable it."
(let ((f (if enable 'add-hook 'remove-hook)))
(funcall f 'pre-command-hook 'vimgolf-capture-keystroke)
(funcall f 'post-command-hook 'vimgolf-capture-dangling-keystroke)
(funcall f 'pre-command-hook 'vimgolf-refresh-keystroke-log)
(funcall f 'post-command-hook 'vimgolf-refresh-keystroke-log)))
(defun vimgolf-count-keystrokes ()
(apply '+ (mapcar 'length (mapcar 'car vimgolf-keystrokes))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Managing and scoring challenges
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun vimgolf-solution-correct-p ()
"Return t if the work text is identical to the solution, nil otherwise."
(let ((case-fold-search nil)
(work vimgolf-work-buffer-name)
(end vimgolf-end-buffer-name))
(flet ((point-min-in (buf) (with-current-buffer buf (point-min)))
(point-max-in (buf) (with-current-buffer buf (point-max))))
(zerop (compare-buffer-substrings
(get-buffer work) (point-min-in work) (point-max-in work)
(get-buffer end) (point-min-in end) (point-max-in end))))))
(defun vimgolf-wrong-solution ()
(message "Wrong!")
(vimgolf-diff))
(defun vimgolf-right-solution ()
(message "Hurray! You solved %s in %d keystrokes!" vimgolf-challenge (vimgolf-count-keystrokes)))
(defun vimgolf-submit ()
"Stop the challenge and attempt to submit the solution to VimGolf."
(interactive)
(vimgolf-enable-capture nil)
(if (vimgolf-solution-correct-p) (vimgolf-right-solution) (vimgolf-wrong-solution)))
(defun vimgolf-clear-keystrokes ()
(setq vimgolf-keystrokes nil))
(defun vimgolf-reset-work-buffer ()
"Reset the contents of the work buffer, and clear undo/macro history etc."
(with-current-buffer (get-buffer-create vimgolf-work-buffer-name)
(vimgolf-init-buffer (current-buffer)
(with-current-buffer vimgolf-start-buffer-name
(buffer-string)))
(when defining-kbd-macro
(end-kbd-macro))
(vimgolf-clear-keystrokes)
(setq buffer-undo-list nil)
(set-buffer-modified-p nil)))
(defun vimgolf-revert ()
"Revert the work buffer to it's original state and reset keystrokes."
(interactive)
(vimgolf-reset-work-buffer)
(set-window-configuration vimgolf-working-window-configuration)
(message "If at first you don't succeed, try, try again."))
(defun vimgolf-diff ()
"Pause the competition and view differences between the buffers."
(interactive)
(vimgolf-enable-capture nil)
(ediff-buffers (get-buffer-create vimgolf-work-buffer-name) (get-buffer-create vimgolf-end-buffer-name))
(message "Remember to `C-c C-v c` when you're done."))
(defun vimgolf-continue ()
"Restore work and end buffers and begin recording keystrokes again."
(interactive)
(vimgolf-enable-capture t)
(set-window-configuration vimgolf-working-window-configuration)
(message "Golf away!"))
(defun vimgolf-pause ()
"Stop recording keystrokes."
(interactive)
(vimgolf-enable-capture nil)
(message "Come `C-c C-v c` soon."))
(defun vimgolf-quit ()
"Cancel the competition."
(interactive)
(vimgolf-enable-capture nil)
(vimgolf-kill-existing-session)
(set-window-configuration vimgolf-prior-window-configuration)
(message "I declare you, n00b!"))
(defvar vimgolf-host "http://vimgolf.com/")
;; (setq vimgolf-host "http://vimgolf.local:8888/")
;; (setq vimgolf-host "http://vimgolf.com/")
;; Overall VimGolf Rank ID: 4d2fb20e63b08b08b0000075
;; Sort entries based on date ID: 4ea9bc988b36f70001000008
;; HTML to Haml ID: 4d3c51f1aabf526ed6000030
(defvar vimgolf-challenge-extension ".yaml")
(defun vimgolf-challenge-path (challenge-id)
(concat "challenges/" challenge-id))
(defun vimgolf-challenge-url (challenge-id)
(concat vimgolf-host (vimgolf-challenge-path challenge-id) vimgolf-challenge-extension))
(defun vimgolf-init-buffer (buffer text)
(with-current-buffer buffer
(erase-buffer)
(insert text)
(beginning-of-buffer)
(vimgolf-mode t)))
(defun vimgolf-kill-existing-session ()
"Kill any vimgolf-related buffers."
(dolist (buf (list vimgolf-start-buffer-name
vimgolf-work-buffer-name
vimgolf-end-buffer-name
vimgolf-keystrokes-buffer-name))
(when (get-buffer buf)
(kill-buffer buf))))
(defun vimgolf-read-next-data-chunk ()
"Return the next chunk of data as a string, leaving the point at the end of that chunk."
(let ((data-start-regexp " data: |\\+\\{0,1\\}\n")
(data-end-regexp "\\([ ]\\{4\\}\\|[ ]\\{0\\}\\)\n type: [-a-z]+"))
(unless (re-search-forward data-start-regexp nil t)
(error "Can't find data in response from vimgolf"))
(let ((start (point)))
(unless (re-search-forward data-end-regexp nil t)
(error "Unclosed data section in response from vimgolf"))
(let ((str (buffer-substring-no-properties start (match-beginning 0))))
(replace-regexp-in-string "^ " "" str)))))
;;;###autoload
(defun vimgolf (challenge-id)
"Open a VimGolf Challenge"
(interactive "sChallenge ID: ")
(vimgolf-clear-keystrokes)
(setq vimgolf-prior-window-configuration (current-window-configuration)
vimgolf-challenge challenge-id)
(let ((vimgolf-yaml-buffer (url-retrieve-synchronously (vimgolf-challenge-url challenge-id))))
(set-buffer vimgolf-yaml-buffer)
(beginning-of-buffer)
(let* ((start-text (vimgolf-read-next-data-chunk))
(end-text (vimgolf-read-next-data-chunk)))
(vimgolf-kill-existing-session)
(let ((vimgolf-start-buffer (get-buffer-create vimgolf-start-buffer-name))
(vimgolf-work-buffer (get-buffer-create vimgolf-work-buffer-name))
(vimgolf-end-buffer (get-buffer-create vimgolf-end-buffer-name)))
(vimgolf-init-buffer vimgolf-start-buffer start-text)
(vimgolf-init-buffer vimgolf-end-buffer end-text)
(vimgolf-reset-work-buffer)
;; Set up windows
(delete-other-windows)
(display-buffer vimgolf-end-buffer 'display-buffer-pop-up-window)
(set-window-buffer (selected-window) vimgolf-work-buffer)
(switch-to-buffer vimgolf-work-buffer)
(setq vimgolf-working-window-configuration (current-window-configuration))
(vimgolf-enable-capture t)))))
(provide 'vimgolf)
;;; vimgolf.el ends here