-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathorg-recipes.el
312 lines (280 loc) · 12.6 KB
/
org-recipes.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
;;; org-recipes.el --- A code snippet manager with Org and Helm
;;
;; Filename: org-recipes.el
;; Description: A code snippet manager with Org and Helm
;; Author: Tu, Do Hoang <[email protected]>
;; URL : https://github.com/tuhdo/semantic-refactor
;; Maintainer: Tu, Do Hoang
;; Created: March 11, 2017
;; Version: 0.1
;; Package-Requires: ((emacs "24.4") helm org)
;; Keywords: tools
;; Compatibility: GNU Emacs: 24.4+
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;; This package collects code snippets under the inner-most Org heading. It
;; provides the following features:
;;
;; - List all Org headings with at least a code snippet. Each entry can be
;; visited for viewing.
;;
;; - Insert a code snippet into the current buffer. The description text is
;; stripped and all code snippets are concantenated into a single snippet and
;; then it is insert into current buffer.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'subr-x)
(require 'thingatpt)
;; (defvar org-recipes-cache nil)
(defcustom org-recipes-directory "~/org-recipes"
"Source directory that org-recipes will search for code snippets in every Org files."
:type 'list
:group 'org-recipes)
;; (defun org-recipes-invalidate-cache ()
;; (interactive)
;; (setq org-recipes-cache nil))
(defmacro org-recipes--get-heading-face (headline)
`(intern-soft (concat "org-level-" (number-to-string (org-element-property :level ,headline)))))
(defmacro org-recipes--get-file (c)
`(nth 0 ,c))
(defmacro org-recipes--get-line (c)
`(nth 1 ,c))
(defmacro org-recipes--get-src-blocks (c)
`(nth 2 ,c))
(defmacro org-recipes--get-real (candidate)
`(cdr ,candidate))
(defmacro org-recipes--src-string (src-block)
`(org-element-property :value ,src-block))
(defun org-recipes ()
"docstring"
(interactive)
(helm :sources (org-recipes--build-source)
:buffer "*helm sync source*"))
(defun org-recipes--build-source ()
"docstring"
(interactive "P")
(helm-build-sync-source "Org Snippets"
:candidates (org-recipes--get-candidates)
:action '(("Jump to snippet" . org-recipes--persistent-view)
("Insert code" . org-recipes--insert))
:keymap org-recipes-map
:persistent-action 'org-recipes--persistent-view))
(setq org-recipes-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "C-c C-i") 'org-recipes-insert)
(define-key map (kbd "C-c i") 'org-recipes-insert)
(delq nil map)
map))
(defun org-recipes--persistent-view (c)
(find-file (org-recipes--get-file c))
(goto-line (org-recipes--get-line c))
(org-show-subtree))
(defun org-recipes-insert ()
(interactive)
(with-helm-alive-p
(helm-exit-and-execute-action 'org-recipes--insert)))
(defun org-recipes--insert (c)
(maphash (lambda (file src-str)
(let ((file-empty (string-equal file "empty")))
(with-current-buffer (if file-empty
(current-buffer)
(find-file-noselect file))
(save-excursion
(beginning-of-line)
(let ((start (point)))
(insert src-str)
(indent-region start (point))
(unless file-empty (save-buffer)))))))
(org-recipes--distribute-src-blocks-strings (org-recipes--get-src-blocks c))))
(defmacro org-recipes--src-data-get-file (s)
`(car ,s))
(defmacro org-recipes--src-data-get-ignore (s)
`(cadr ,s))
(defmacro org-recipes--src-data-pre-recipe-list (s)
`(caddr ,s))
(defmacro org-recipes--src-data-post-recipe-list (s)
`(cadddr ,s))
(defmacro org-recipes--src-data-get-str (s)
`(cadddr (cdr ,s)))
(defun org-recipes--distribute-src-blocks-strings (src-blocks)
(let* ((dist-table (make-hash-table :test #'equal)))
(mapcar (lambda (s)
(let* ((src-data (org-recipes--process-src-block s))
(pre-recipe-list (org-recipes--src-data-pre-recipe-list src-data))
(post-recipe-list (org-recipes--src-data-post-recipe-list src-data))
(file (org-recipes--src-data-get-file src-data))
(ignore (org-recipes--src-data-get-ignore src-data))
(new-str (org-recipes--src-data-get-str src-data))
(old-str (gethash file dist-table))
(pre-str (with-temp-buffer
(mapcar (lambda (r)
(when-let ((candidates (org-recipes--get-candidates r)))
(org-recipes--insert-candidates candidates)
(newline)))
pre-recipe-list)
(buffer-string)))
(post-str (with-temp-buffer
(mapcar (lambda (r)
(when-let ((candidates (org-recipes--get-candidates r)))
(newline)
(org-recipes--insert-candidates candidates)))
post-recipe-list)
(buffer-string))))
(unless ignore
(puthash file
(if old-str
(concat pre-str old-str new-str post-str)
(concat pre-str new-str post-str))
dist-table)
)))
src-blocks)
dist-table))
(defun org-recipes--process-src-block (s)
"docstring"
(let* ((src-parameters (org-recipes--filter-src-parameters
(org-babel-parse-header-arguments (org-element-property :parameters s))
'(:file :pre-recipe :post-recipe :ignore)))
(file (cdr (assoc :file src-parameters)))
(ignore (cdr (assoc :ignore src-parameters)))
(pre-recipe-list (cdr (assoc :pre-recipe src-parameters)))
(post-recipe-list (cdr (assoc :post-recipe src-parameters)))
(src-string (org-recipes--src-string s)))
(list (or file "empty") ignore pre-recipe-list post-recipe-list src-string)))
(defun org-recipes--filter-src-parameters (src-params key-list)
(delete-if (lambda (s)
(not (member (car s) key-list))) src-params))
(defun org-recipes--get-candidates (&optional recipe)
(-flatten-n
1
(delq
nil
(mapcar (lambda (f)
(org-recipes--collect-snippets f recipe))
(append (directory-files-recursively org-recipes-directory "")
(when (featurep 'org-wiki)
(mapcar (lambda (f)
(concat org-wiki-location "/" f))
(org-wiki--page-files))))))))
(defun org-recipes--collect-snippets (f &optional recipe)
(let* ((org-buf (find-file-noselect f))
(target-major-modes (org-recipes--get-target-major-modes org-buf))
(cur-major-mode major-mode)
(headline-major-modes))
(when (or (null target-major-modes)
(member cur-major-mode target-major-modes))
(with-current-buffer org-buf
(org-element-map (org-element-parse-buffer 'element) 'headline
(lambda (headline)
(setq headline-major-modes (mapcar #'org-recipes--string-to-mode
(org-recipes--split-mode-list (org-element-property :LANG headline))))
(unless headline-major-modes (setq headline-major-modes (list cur-major-mode)))
(when (member cur-major-mode headline-major-modes)
(let* ((src-blocks (delq nil (org-element-map headline 'src-block
(lambda (s)
(when (or headline-major-modes
(eq cur-major-mode
(org-recipes--string-to-mode (org-element-property :language s))))
s)))))
(symbol (org-element-property :SYMBOL headline))
(src-blocks-parent (org-element-map headline 'headline 'identity))
(linum (line-number-at-pos
(org-element-property :begin headline))))
(when (and src-blocks
(eq (length src-blocks-parent) 1)
(or (null recipe)
(equal symbol (symbol-name recipe))))
(cons (concat (concat (file-relative-name f org-recipes-directory) ":")
(concat (number-to-string linum) ":")
" "
(when symbol (propertize (concat "[" symbol "] ") 'face 'font-lock-type-face))
(org-recipes--get-parent-string headline)
;; (format "%s" (org-element-property :title headline))
(propertize (format "%s" (org-element-property :title headline)) 'face (org-recipes--get-heading-face headline)))
(list f
linum
src-blocks)))))
))
))))
(defun org-recipes--get-parent-string (headline)
(when-let ((parent (org-element-property :parent headline))
(parent-str (org-element-property :raw-value parent)))
(concat (org-recipes--get-parent-string parent)
(propertize parent-str 'face (org-recipes--get-heading-face parent))
" / ")))
(defun org-recipes-dwim ()
(interactive)
(if-let ((recipe-list (list-at-point)))
(org-recipes--process-recipes recipe-list)
(when-let ((symbol (symbol-at-point))
(candidates (org-recipes--get-candidates symbol)))
(org-recipes--delete-thing-at-point symbol)
(let ((start (point)))
(org-recipes--insert-candidates candidates)
(indent-region start (point))
(newline)))))
(defun org-recipes--process-recipes (recipe-list &optional deleted)
(let ((start (point))
(deleted deleted))
(mapcar (lambda (r)
(when-let ((candidates (org-recipes--get-candidates r)))
(unless deleted
(org-recipes--delete-thing-at-point recipe-list)
(setq deleted t))
(org-recipes--insert-candidates candidates)
(newline)))
recipe-list)
(indent-region start (point))))
(defun org-recipes--insert-candidates (candidates)
(mapcar (lambda (c)
(org-recipes--insert (org-recipes--get-real c)))
candidates))
(defun org-recipes--delete-thing-at-point (thing)
(cond ((listp thing)
(if (looking-at "\(")
(kill-sexp)
(backward-up-list)
(kill-sexp)))
((symbolp thing)
(condition-case nil
(beginning-of-sexp)
(error nil))
(mark-sexp)
(delete-region (region-beginning) (region-end)))))
(defun org-recipes--get-target-major-modes (buffer)
(with-current-buffer buffer
(save-excursion
(beginning-of-buffer)
(when (search-forward "#+MODE:" nil t)
(when-let ((mode-keyword (org-element-property :value (org-element-at-point))))
(beginning-of-line)
(mapcar #'org-recipes--string-to-mode
(org-recipes--split-mode-list (org-element-property :value (org-element-at-point)))))))))
(defun org-recipes--split-mode-list (mode-list)
(when mode-list
(split-string (replace-regexp-in-string "[ ]*" "" mode-list) ",")))
(defun org-recipes--string-to-mode (m)
(intern-soft (concat m "-mode")))
(provide 'org-recipes)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; org-recipes.el ends here
;; End: