Fork me on GitHub
#emacs
<
2022-07-13
>
Drew Verlee01:07:48

Things i like ~ how much functionality is jammed into emacs! things i don't like ~ hitting some keys by accident and having emacs take up 100% of my cpu trying to do i have no idea what.

😬 3
😄 2
teodorlu11:07:03

I feel you! 😊😄😂

lread12:07:05

Yeah me too! How'd we get here fingers? And how do we get outta here?

😂 1
ag04:07:34

> hitting some keys by accident and having emacs take up 100% of my cpu pkill -SIGUSR2 Emacs

teodorlu11:07:42

Does this just abort the execution of the current Emacs Lisp command?

ag16:07:24

it just forces Emacs to enter the debugger. See Info for debug-on-event https://www.gnu.org/software/emacs/manual/html_node/elisp/Error-Debugging.html#index-debug_002don_002devent

👍 1
ag04:07:43

I've been dreaming for someone (smarter than me) to port something like core.async to emacs-lisp.

Benjamin05:07:54

https://github.com/jwiegley/emacs-async @ag i guess you knew about this. I think the big draw back is you have to import code you want to use in the async process. So it isnt at all like core async. Consult for example uses it in searches

ag17:07:25

Oh I didn't know Consult uses emacs-async. I wanna dig through the source code when I get a chance. I'm curious.

teodorlu14:07:53

Hey! Quick question. With #babashka it's easy to transform some EDN:

$ echo '{:title "Good morning"}' | bb '(assoc *input* :title "Good afternoon")'
{:title "Good afternoon"}
I want to write an Emacs Lisp function that mutates the current buffer containing some EDN, and change it to some other EDN. Something like this:
;; Current buffer contains:
;;
;;    {:title "Good morning"}

(defun edn-good-afternoon ()
  (interactive)
  (shell-command-transform-buffer "bb" "(assoc *input* :title \"Good afternoon\")"))

;; run M-x edn-good-afternoon

;; Current buffer now contains:
;;
;;    {:title "Good afternoon"}
Is there a function I can use that roughly the same as the imaginary shell-command-transform-buffer? --- If I were to try right now, I would: 1. Read the content of the current buffer 2. Construct the same echo {,,,} | bb ... command above 3. Write that result back to the same buffer. I'm suspecting there's a better way. Pointers much welcome!

1
teodorlu14:07:27

closer:

(shell-command-on-region (point-min) (point-max) "echo lol" (current-buffer))

teodorlu14:07:28

duckie 🐧🎉

(defun teod-play-xx ()
  "Play."
  (interactive)
  (shell-command-on-region (point-min) (point-max) "bb '(assoc *input* :title \"Good afternoon\")'" (current-buffer)))

🚀 1
duckie 1
teodorlu15:07:07

I did a bit of more work. Problems with the solution above: 1. Can't edit structure inside text string easily 2. A bit of noise around point-min, point-max and current-buffer. So I created t-bb (macro) that takes an Emacs lisp form and turns it into a babashka shell invocation, and a simplified "run stuff on buffer. Here's the example above -- revised:

(defun teod-play-xx ()
  "Play."
  (interactive)
  (t-shell-command-buffer
   (t-bb (assoc *input*
                :title "Good afternoon"))))
Pasting the source of t-bb and t-shell-comamnd-buffer as well:
(defun t-str (&rest xs)
  "Join XS into string."
  (apply #'s-concat
         (mapcar (lambda (s)
                   (format "%s" s))
                 xs)))

(defun t-bb-fun (form)
  "Convert quoted FORM to Babashka shell invocation."
  (t-str "bb " "'" form "'"))

;; Example:
;;
;;   (t-bb-fun '(assoc *input* :message "Hello!"))

(defmacro t-bb (form)
  "Convert FORM to babashka shell invocation."
  (list 't-bb-fun (list 'quote form)))

;; Example:
;;
;;   (t-bb (assoc *input* :message "Hello!"))

(defun t-shell-command-buffer (command)
  "Run COMMAND on current buffer."
  (shell-command-on-region (point-min) (point-max)
                           command
                           (current-buffer)))

teodorlu15:07:05

t-bb-shell-command might be a better name than the plain t-bb.

Benjamin17:07:24

I recently crafted something with princ as the thing to pass into bb -e. I wrote t-bb in terms of it

(shell-command-to-string (format "bb -e '%s'" (princ '(+ 1 2 3))))

(defmacro t-bb (form)
  `(format "bb -e '%s'" (princ ',form)))

(shell-command-to-string (t-bb (+ 1 2 3)))

(defun eval-bb (form)
  (car
   (read-from-string
    (shell-command-to-string (format "bb -e '%s'" (princ form))))))

(+ (eval-bb '(+ 1 2 3)) 10)
16
I'm currently thinking that the last function is just good enough

💯 1
Benjamin17:07:58

for reference t-str can be written in terms of mapconcat

(defun t-str (&rest xs)
  "Join XS into string."
  (mapconcat (lambda (s) (format "%s" s)) xs ""))

💯 1
teodorlu18:07:39

https://clojurians.slack.com/archives/C099W16KZ/p1657734204817869?thread_ts=1657721213.601949&amp;cid=C099W16KZ Nice! That's bb directly in Emacs Lisp 😄 I guess it would crash if you tried to return a map or something though?

Benjamin18:07:36

Yea its naive and expects elisp readable output. You could either drop the read string part or return a string from bb if you want a string. Eg for insert

👍 1