forked from avendael/emacs-geeknote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeeknote.el
438 lines (384 loc) · 15 KB
/
geeknote.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
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
;;; geeknote.el --- Use Evernote in Emacs through geeknote
;; Copyright (C) 2015 Evan Dale Aromin
;; Author: Evan Dale Aromin
;; Modifications: David A. Shamma
;; Version: 0.3
;; Package-Version: 20150223.815
;; Keywords: evernote, geeknote, note, emacs-evernote, evernote-mode
;; Package-Requires: ((emacs "24"))
;; URL: http://github.com/avendael/emacs-geeknote
;;; 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:
;; This package wraps common geeknote commands into elisp. With this, geeknote
;; can be interacted with within emacs instead of through a shell.
;;
;; The command `geeknote' is expected to be present on the user's `$PATH'.
;; Please follow the geeknote installation instructions to obtain this command.
;;; Code:
(defgroup geeknote nil
"Interact with evernote through emacs."
:group 'tools
:group 'convenience)
(defcustom geeknote-command "geeknote"
"The geeknote command.
It's either a path to the geeknote script as an argument to python, or simply
`geeknote` if the command is already on your PATH."
:group 'geeknote
:type 'string)
(defconst geeknote--expect-script
(concat "expect -c 'spawn "
geeknote-command
" %s; set pid $spawn_id; set timeout 10; set count 5; while { $count > 0 } { expect \"^\\-\\- More \\-\\-\"; if {[catch {send -i $pid \" \"} err]} { exit } else { set count [expr $count-1]} }'"))
(defvar geeknote-mode-hook nil)
(defvar geeknote-mode-map
(let ((map (make-keymap)))
(define-key map "q" 'kill-this-buffer)
(define-key map "j" 'next-line)
(define-key map "k" 'previous-line)
map)
"Keymap for Geeknote major mode")
(regexp-opt '("Total found") t)
(defconst geeknote-font-lock-keywords-1
(list
'("\\(Total found:\\)" . font-lock-constant-face))
"Minimal highlighting keywords for geeknote mode")
(defconst geeknote-font-lock-keywords-2
(append geeknote-font-lock-keywords-1
(list '("\\(^\s-+[0-9]+\\)" . font-lock-keyword-face)))
"Additional Keywords to highlight in geeknote mode")
(defconst geeknote-font-lock-keywords-3
(append geeknote-font-lock-keywords-2
(list '(" : \\(.+\\)$" . font-lock-builting-face)))
"Additional Keywords to highlight in geeknote mode")
(defvar geeknote-font-lock-keywords geeknote-font-lock-keywords-3
"Default highlighting expressions for geeknote mode")
(defun geeknote-mode ()
"Major mode for navigation Geeknote mode listings."
(kill-all-local-variables)
(use-local-map geeknote-mode-map)
(set (make-local-variable 'font-lock-defaults) '(geeknote-font-lock-keywords))
(setq major-mode 'geeknote-mode)
(setq mode-name "geeknote")
(run-hooks 'geeknote-mode-hook))
(provide 'geeknote-mode)
;;;###autoload
(defun geeknote-setup ()
"Setup geeknote."
(interactive)
(message (concat "geeknote: "
(shell-command-to-string
(concat geeknote-command
" settings --editor emacsclient")))))
;;;###autoload
(defun geeknote-create (title)
"Create a new note with the given title.
TITLE the title of the new note to be created."
(interactive "sName: ")
(message (format "geeknote creating note: %s" title))
(let* ((note-title (geeknote--parse-title title))
(note-tags (geeknote--parse-tags title))
(message (format "New note tags: %s" (or note-tags "")))
(note-notebook (geeknote--parse-notebook title)))
(async-shell-command
(format (concat geeknote-command " create --content WRITE --title %s %s"
(when note-notebook " --notebook %s"))
(shell-quote-argument note-title)
(if (equal note-tags "")
""
(mapconcat 'identity
(mapcar (lambda (x) (concat "--tag " x))
(mapcar 'shell-quote-argument (split-string note-tags ",")))
" ")
)
(shell-quote-argument (or note-notebook "")))
)))
;;;###autoload
(defun geeknote-show (title)
"Open an existing note.
TITLE the title of the note to show."
(interactive "sName: ")
(message (format "geeknote showing note: %s" title))
(let* ((note (shell-command-to-string
(format (concat geeknote-command " show %s")
(shell-quote-argument title))))
(lines (split-string note "\n"))
(name (cadr lines))
(buf-name (format "*Geeknote: %s*" name)))
(with-current-buffer (get-buffer-create buf-name)
(display-buffer buf-name)
(read-only-mode 0)
(erase-buffer)
(insert note)
(read-only-mode t)
(markdown-mode))
(other-window 1)))
;;;###autoload
(defun geeknote-edit (title)
"Open up an existing note for editing.
TITLE the title of the note to edit."
(interactive "sName: ")
(message (format "Editing note: %s" title))
(async-shell-command
(format (concat geeknote-command " edit --note %s")
(shell-quote-argument title))))
;;;###autoload
(defun geeknote-remove (title)
"Delete an existing note.
TITLE the title of the note to delete."
(interactive "sName: ")
(message (format "geeknote deleting note: %s" title))
(message (concat "geeknote: "
(shell-command-to-string
(format (concat geeknote-command
" remove --note %s --force")
(shell-quote-argument title))))))
;;;###autoload
(defun geeknote-find (keyword)
"Search for a note with the given keyword.
KEYWORD the keyword to search the notes with."
(interactive "skeyword: ")
(geeknote--find-with-args
(format
(concat geeknote-command
" find --search %s --count 20 --content-search")
(shell-quote-argument keyword))
keyword))
(defun geeknote-find-in-notebook (notebook keyword)
"Search for a note with the given keyword.
KEYWORD the keyword to search the notes with."
(interactive "sNotebook: \nsKeyword: ")
(geeknote--find-with-args
(format
(concat geeknote-command
" find --search %s --count 20 --content-search --notebook %s")
(shell-quote-argument keyword)
(shell-quote-argument notebook))
keyword))
(defun geeknote--find-with-notebook (notebook)
(let* ((m "Search notebook '%s' with: ")
(p (format m notebook))
(keyword (read-from-minibuffer p)))
(geeknote--find-with-args
(format
(concat geeknote-command
" find --search %s --count 20 --content-search --notebook %s")
(shell-quote-argument keyword)
(shell-quote-argument notebook))
keyword)))
(defun geeknote-find-tags (tags)
"Search for a note with the given keyword.
TAGS the tags to search the notes with."
(interactive "stags: ")
(geeknote--find-with-args
(format
(concat geeknote-command
" find %s --count 20")
(mapconcat 'identity (mapcar (lambda (x) (concat "--tag " x)) (split-string tags)) " ")
)
tags)
)
(defun geeknote--find-with-args (command keyword)
"Search for a note with the given arg string.
COMMAND basically the full geeknote command to exec.
KEYWORD is used for display and buffer title only."
(let* ((notes (shell-command-to-string command))
(lines (split-string notes "\n"))
(buf-name (format "*Geeknote Find: %s*" keyword)))
(with-current-buffer (get-buffer-create buf-name)
(display-buffer buf-name)
(read-only-mode 0)
(erase-buffer)
(dotimes (i 2)
(insert (concat (car lines) "\n"))
(setq lines (cdr lines)))
(while lines
(let ((l (car lines)))
(insert-button l
'follow-link t
'help-echo "Edit this note."
'action (lambda (x)
(geeknote-edit
(car (split-string (button-get x 'name) " : "))))
'name l)
(insert "\n"))
(setq lines (cdr lines)))
(read-only-mode t)
(geeknote-mode))
(other-window 1)))
;;;###autoload
(defun geeknote-tag-list ()
"Show the list of existing tags in your Evernote."
(interactive)
(let* ((tags (shell-command-to-string
(format geeknote--expect-script "tag-list")))
(lines (split-string tags "\n"))
(buf-name "*Geeknote Tag List*"))
(with-current-buffer (get-buffer-create buf-name)
(display-buffer buf-name)
(read-only-mode 0)
(erase-buffer)
(setq lines (cdr lines))
(insert (replace-regexp-in-string
"\^M" ""
(concat "Total found: "
(cadr (split-string (car lines) "Total found: "))
"\n")))
(setq lines (cdr lines))
(while lines
(let ((l
(geeknote--chomp-end (replace-regexp-in-string
"\^M" ""
(replace-regexp-in-string "^.*\^M\s+\^M" ""
(car lines))))))
(unless (zerop (length (geeknote--chomp l)))
(insert-button l
'follow-link t
'help-echo "Find notes with this tag."
'action (lambda (x)
(geeknote-find-tags
(cadr (split-string (button-get x 'name) " : "))))
'name l)
(insert "\n")))
(setq lines (cdr lines)))
(read-only-mode t)
(geeknote-mode))
(other-window 1)))
;;;###autoload
(defun geeknote-notebook-list ()
"Show the list of existing notebooks in your Evernote."
(interactive)
(let* ((books (shell-command-to-string
(format geeknote--expect-script "notebook-list")))
(lines (split-string books "\n")))
(with-current-buffer (get-buffer-create "*Geeknote Notebook List*")
(display-buffer "*Geeknote Notebook List*")
(read-only-mode 0)
(erase-buffer)
(setq lines (cdr lines))
(insert (replace-regexp-in-string
"\^M" ""
(concat "Total found: "
(cadr (split-string (car lines) "Total found: "))
"\n")))
(setq lines (cdr lines))
(while lines
(let ((l
(geeknote--chomp-end (replace-regexp-in-string
"\^M" ""
(replace-regexp-in-string "^.*\^M\s+\^M" ""
(car lines))))))
(unless (zerop (length (geeknote--chomp l)))
(insert-button l
'follow-link t
'help-echo "Search in this notebook."
'action (lambda (x)
(geeknote--find-with-notebook
(cadr (split-string (button-get x 'name) " : "))))
'name l)
(insert "\n")))
(setq lines (cdr lines)))
(read-only-mode t)
(geeknote-mode))
(other-window 1)))
;;;###autoload
(defun geeknote--notebook-edit-with-oldtitle (oldtitle)
"Rename an existing notebook with a target.
TITLE the title of the notebook to rename."
(let* ((m "Rename notebook '%s' to: ")
(p (format m oldtitle))
(newtitle (read-from-minibuffer p)))
(message (format "Renaming notebook: %s to %s." oldtitle newtitle))
(geeknote-notebook-edit oldtitle newtitle)))
;;;###autoload
(defun geeknote-notebook-edit (oldtitle newtitle)
"Rename an existing notebook.
TITLE the title of the notebook to rename."
(interactive "sRename existing notebook: \nsTo new notebook name: ")
(message (format "Renaming notebook: %s to %s." oldtitle newtitle))
(message (shell-command-to-string
(format (concat geeknote-command
" notebook-edit --notebook %s --title %s")
(shell-quote-argument oldtitle)
(shell-quote-argument newtitle)))))
;;;###autoload
(defun geeknote-user ()
"Show information about active user."
(interactive)
(with-output-to-temp-buffer "*Geeknote User Info*"
(princ (shell-command-to-string
(format (concat geeknote-command " user")))))
(other-window 1))
;;;###autoload
(defun geeknote-move (note notebook)
"Move a NOTE to a different NOTEBOOK. If the provided NOTEBOOK is
non-existent, it will be created.
NOTE the title of the note to move.
NOTEBOOK the title of the notebook where NOTE should be moved."
(interactive "sName: \nsMove note %s to notebook: ")
(message (format "Moving note %s to notebook %s..." note notebook))
(async-shell-command
(format (concat geeknote-command " edit --note %s --notebook %s")
(shell-quote-argument note)
(shell-quote-argument notebook))))
(defun geeknote--parse-title (title)
"Rerieve the title from the provided string. Filters out @notebooks and #tags.
TITLE is the input given when asked for a new note title."
(let ((wordlist (split-string title)))
(mapconcat (lambda (s) s)
(delq nil
(mapcar (lambda (str)
(cond
((string-prefix-p "@" str) nil)
((string-prefix-p "#" str) nil)
(t str)))
wordlist))
" ")))
(defun geeknote--parse-notebook (title)
"Rerieve the @notebook from the provided string. Returns nil if none.
TITLE is the input given when asked for a new note title."
(let ((wordlist (split-string title)))
(elt
(delq nil
(mapcar (lambda (str)
(cond
((string-prefix-p "@" str) (substring str 1))
(t nil)))
wordlist))
0)))
(defun geeknote--parse-tags (title)
"Rerieve the #tags from the provided string. Returns nil if none.
TITLE is the input given when asked for a new note title."
(let ((wordlist (split-string title)))
(mapconcat (lambda (s) s)
(delq nil
(mapcar (lambda (str)
(cond
((string-prefix-p "#" str) (substring str 1))
(t nil)))
wordlist))
", ")))
(defun geeknote--chomp-end (str)
"Chomp tailing whitespace from STR."
(replace-regexp-in-string (rx (* (any " \t\n")) eos)
""
str))
(defun geeknote--chomp (str)
"Chomp leading and tailing whitespace from STR."
(replace-regexp-in-string (rx (or (: bos (* (any " \t\n")))
(: (* (any " \t\n")) eos)))
""
str))
(provide 'geeknote)
;;; geeknote.el ends here