forked from eschulte/emacs-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheat.el
275 lines (230 loc) · 9.26 KB
/
cheat.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
;; cheat.el
;; Time-stamp: <2007-08-22 10:00:04 sjs>
;;
;; Copyright (c) 2007 Sami Samhuri <[email protected]>
;;
;; See http://sami.samhuri.net/2007/08/10/cheat-from-emacs for updates.
;;
;; 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 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, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;
;;
;; Provide a handy interface to cheat.
;; See http://cheat.errtheblog.com for details on cheat itself.
;;
;; sjs 2007.08.21
;; * Cache the list of cheat sheets, update it once a day (configurable).
;; * Strictly complete cheat sheet names.
;;
;; TODO: make sure all functions are namespaced under cheat-
;;
(defvar *cheat-host* "cheat.errtheblog.com")
(defvar *cheat-port* "80")
(defvar *cheat-uri* (concat *cheat-host* ":" *cheat-port*))
(defvar *cheat-directory* "~/.cheat")
(defvar *cheat-sheets-cache-file* (concat *cheat-directory* "/sheets"))
(defvar *cheat-last-sheet* nil
"Name of the most recently viewed cheat sheet.")
(defvar *cheat-sheet-history* nil
"List of the most recently viewed cheat sheets.")
(defconst +seconds-per-day+ 86400)
(defvar *cheat-cache-ttl* +seconds-per-day+
"The minimum age of a stale cache file, in seconds.")
;;; interactive functions
;;;###autoload
(defun cheat (name &optional silent)
"Show the specified cheat sheet.
If SILENT is non-nil then do not print any output, but return it
as a string instead."
(interactive (list (cheat-read-sheet-name)))
(if silent
(cheat-command-silent name)
(cheat-command name)))
(defun cheat-sheets ()
"List all cheat sheets."
(interactive)
(cheat-command "sheets"))
(defun cheat-recent ()
"Show recently added cheat sheets."
(interactive)
(cheat-command "recent"))
(defun cheat-clear-cache ()
"Clear the local cheat cache, located in ~/.cheat."
(interactive)
(cheat-command "--clear-cache")
(make-directory *cheat-directory*))
(defun cheat-versions (name)
"Version history of the specified cheat sheet."
(interactive (list (cheat-read-sheet-name)))
(cheat-command name "--versions"))
(defun cheat-diff (name version)
"Show the diff between the given version and the current version of the named
cheat.
If VERSION is of the form m:n then show the diff between versions m and n."
(interactive (list (cheat-read-sheet-name)
(read-string "Cheat version(s): ")))
(cheat-command name "--diff" version))
(defun cheat-add-current-buffer (name)
"Add a new cheat with the specified name and the current buffer as the body."
(interactive "sCheat name: \n")
(post-cheat name (buffer-string) t)
(if (interactive-p)
(print (concat "Cheat added (" name ")"))))
(defun cheat-edit (name)
"Fetch the named cheat and open a buffer containing its body.
The cheat can be saved with `cheat-save-current-buffer'."
(interactive (list (cheat-read-sheet-name)))
(cheat-clear-cache name) ; make sure we're working with the latest version
(switch-to-buffer (get-buffer-create (cheat->buffer name)))
(insert (cheat-body name))
(if (interactive-p)
(print "Run `cheat-save-current-buffer' when you're done editing.")))
(defun cheat-save-current-buffer ()
"Save the current buffer using the buffer name for the title and the contents
as the body."
(interactive)
(let ((name (buffer->cheat (buffer-name (current-buffer)))))
(post-cheat name (buffer-string))
;; TODO check for errors and kill the buffer on success
(if (interactive-p)
(print (concat "Cheat saved (" name ")")))
(cheat-clear-cache name)
(cheat name)))
;;; helpers
;; this is from rails-lib.el in the emacs-rails package
(defun string-join (separator strings)
"Join all STRINGS using SEPARATOR."
(mapconcat 'identity strings separator))
(defun blank (thing)
"Return T if THING is nil or an empty string, otherwise nil."
(or (null thing)
(and (stringp thing)
(= 0 (length thing)))))
(defun cheat-command (&rest rest)
"Run the cheat command with the given arguments, display the output."
(interactive "sArguments for cheat: \n")
(let* ((cmd (string-join " " rest))
(buffer (get-buffer-create
(concat "*Cheat: " cmd "*"))))
(shell-command (concat "cheat " cmd) buffer)))
(defun cheat-command-to-string (&rest rest)
"Run the cheat command with the given arguments and return the output as a
string. Display nothing."
(shell-command-to-string (concat "cheat " (string-join " " rest))))
(defalias 'cheat-command-silent 'cheat-command-to-string)
(defun cheat-read-sheet-name (&optional prompt)
"Get the name of an existing cheat sheet, prompting with completion and
history.
The name of the sheet read is stored in *cheat-last-sheet* unless it was blank."
(let* ((default (when (blank prompt) *cheat-last-sheet*))
(prompt (or prompt
(if (not (blank default))
(concat "Cheat name (default: " default "): ")
"Cheat name: ")))
(name (completing-read prompt
(cheat-sheets-list t)
nil
t
nil
'*cheat-sheet-history*
default)))
(when (not (blank name))
(setq *cheat-last-sheet* name))
name))
(defun cheat-sheets-list (&optional fetch-if-missing-or-stale)
"Get a list of all cheat sheets.
Return the cached list in *cheat-sheets-cache-file* if it's
readable and `cheat-cache-stale-p' returns nil.
When there is no cache or a stale cache, and
FETCH-IF-MISSING-OR-STALE is non-nil, cache the list and then
return it.
Otherwise return nil."
(cond ((and (file-readable-p *cheat-sheets-cache-file*)
(not (cheat-cache-stale-p)))
(save-excursion
(let* ((buffer (find-file *cheat-sheets-cache-file*))
(sheets (split-string (buffer-string))))
(kill-buffer buffer)
sheets)))
(fetch-if-missing-or-stale
(cheat-cache-list)
(cheat-sheets-list))
(t nil)))
(defun cheat-fetch-list ()
"Fetch a fresh list of all cheat sheets."
(nthcdr 3 (split-string (cheat-command-to-string "sheets"))))
(defun cheat-cache-list ()
"Cache the list of cheat sheets in *cheat-sheets-cache-file*. Return the
list."
(when (not (file-exists-p *cheat-directory*))
(make-directory *cheat-directory*))
(save-excursion
(let ((buffer (find-file *cheat-sheets-cache-file*))
(sheets (cheat-fetch-list)))
(insert (string-join "\n" sheets))
(basic-save-buffer)
(kill-buffer buffer)
sheets)))
(defun cheat-cache-stale-p ()
"Non-nil if the cache in *cheat-sheets-cache-file* is more than
*cheat-cache-ttl* seconds old.q
If the cache file does not exist then it is considered stale.
Also see `cheat-cache-sheets'."
(or (null (file-exists-p *cheat-sheets-cache-file*))
(let* ((now (float-time (current-time)))
(last-mod (float-time (sixth (file-attributes
*cheat-sheets-cache-file*))))
(age (- now last-mod)))
(> age *cheat-cache-ttl*))))
(defun cheat-body (name)
"Call out to Ruby to load the YAML and return just the body."
(shell-command-to-string
(concat "ruby -ryaml -e '"
"puts YAML.load_file(File.expand_path(\"~/.cheat/"
name ".yml\")).to_a[0][-1]'")))
(defun url-http-post (url args)
"Send ARGS to URL as a POST request."
(let ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data
(concat (mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&")
"\r\n")))
;; `kill-url-buffer' to discard the result
;; `switch-to-url-buffer' to view the results (debugging).
(url-retrieve url 'kill-url-buffer)))
(defun kill-url-buffer (status)
"Kill the buffer returned by `url-retrieve'."
(kill-buffer (current-buffer)))
(defun switch-to-url-buffer (status)
"Switch to the buffer returned by `url-retreive'.
The buffer contains the raw HTTP response sent by the server."
(switch-to-buffer (current-buffer)))
(defun post-cheat (title body &optional new)
(let ((uri (concat "http://" *cheat-uri* "/w/" (if new "" title))))
(url-http-post uri `(("sheet_title" . ,title)
("sheet_body" . ,body)
("from_gem" . "1")))))
(defun buffer->cheat (name)
(substring name 7 (- (length name) 1)))
(defun cheat->buffer (name)
(concat "*cheat-" name "*"))
(provide 'cheat)