-
Notifications
You must be signed in to change notification settings - Fork 1
/
my-random.el
106 lines (88 loc) · 3.25 KB
/
my-random.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
;;; my-random --- Random snippets of elisp
;;
;; Copyright (C) 2014 Alex Bennée
;;
;; Author: Alex Bennée <[email protected]>
;;
;; This file is not part of GNU Emacs.
;;
;; This file 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, or (at your option)
;; any later version.
;;
;;; Commentary:
;;
;; This is not currently loaded into Emacs on normal startup. However
;;it does provide a handy dumping ground for things.
;;
;;; Code:
;; Require prerequisites
(eval-when-compile (require 'cl-lib))
;; Variables
;; Code
;; hmm doesn't work like it should
(defun my-periodical-process-pause ()
"Periodically pause the current process."
(interactive)
(lexical-let ((proc (get-buffer-process (current-buffer))))
(when proc
(lexical-let ((old-filter (process-filter proc)))
(set-process-filter proc t)
(run-with-idle-timer
1 nil
#'(lambda()
(set-process-filter proc old-filter)))))))
;; Lets try a macro
(require 'async)
(defmacro my-async-org-function (async-form &optional post-form)
"Expands `ASYNC-FORM' as an asynchronus org-bable function.
If executed inside an org file will insert the results into the src
blocks results. Otherwise the result will be echoed to the Message
buffer. An optional `POST-FORM' can concatenate results to the async
forms."
;; All the my-async-* values we need to make available to the
;; asynchronus call. They need to be pure strings as the async
;; process will have no information about the parents internal
;; structures.
(let ((my-async-buffer (buffer-name))
(my-async-org-name (nth 4 (org-babel-get-src-block-info)))
(my-async-sexp async-form))
`(async-start
;; The async code runs in the inferior process so will not have
;; any of the current emacs environment. The results will be
;; echoed back to the handler function when done.
(lambda ()
,(async-inject-variables "my-async-")
(list
(cons 'buffer my-async-buffer)
(cons 'name my-async-org-name)
(cons 'result (eval my-async-sexp))))
;; This code runs in the current emacs process.
(lambda (result)
(let ((buf (cdr (assoc 'buffer result)))
(name (cdr (assoc 'name result)))
(res (cdr (assoc 'result result))))
;; Do we have a post-form to execute?
(when ,post-form
(setq res (append
(if (listp res)
res
(list (cons 'async-form res)))
(list (cons 'post-form (eval ,post-form))))))
;; Send the results somewhere
(if name
(save-excursion
(with-current-buffer buf
(org-babel-goto-named-result name)
(next-line)
(goto-char (org-babel-result-end))
(org-babel-insert-result (format "%s" res))))
(message (pp (format "async-result: %s" res)))))))))
;; Example calls
(my-async-org-function
(shell-command-to-string
"dd status=none count=8192 bs=8192 if=/dev/urandom | md5sum")
(format "and this was on the return"))
(provide 'my-random)
;;; my-random.el ends here