Fork me on GitHub
#emacs
<
2023-06-04
>
pesterhazy10:06:49

Is there a command to "paste as quote JS/CLJS string" (or Java/Clojure string)? I find myself needing this all the time

pesterhazy10:06:26

It would paste one "two three" four as "one \"two three\" four"

Cora (she/her)20:06:57

I don't know how to do that in emacs but I usually cheat by opening a ruby repl and wrapping it in %{} so %{one "two three" four} which then is inspected and I get:

>> %{one "two three" four}
=> "one \"two three\" four"

Benjamin C21:06:46

Not sure if there's one already, but I've got this in my config:

(defun dc/yank-as-quoted-string
  "Insert the clipboard contents as a string with escaped double quotes."
  (interactive)
  (let ((string (current-kill 0 t)))
    (insert (concat "\"" (replace-regexp-in-string "\"" "\\\\\"" string) "\""))))

pesterhazy09:06:02

Thanks @U02CV2P4J6S - taking inspiration from your snippet, I came up with this, based on simpleclip:

(defun quote-js (s)
  (with-temp-buffer
       (insert s)
       (shell-command-on-region (point-min) (point-max) "python -c \"import sys,json; sys.stdout.write(json.dumps(sys.stdin.read()))\"" nil 'replace)
       (buffer-string)))

(defun paste-quoted ()
  (interactive "*")
  (let ((str-val (quote-js (simpleclip-get-contents))))
    (unless str-val
      (error "No content to paste"))
    (when (use-region-p)
      (delete-region (region-beginning) (region-end)))
    (push-mark (point) t)
    (insert-for-yank str-val)
    (when (and (not (minibufferp))
               (not simpleclip-less-feedback)
               (simpleclip-called-interactively-p 'interactive))
      (message "pasted quoted from clipboard"))))

pesterhazy10:06:37

Thanks to both (but wow - that Slack UX is confusing)

Benjamin C14:06:07

You'd think they'd sort user tags by closest context :P.

aisamu15:06:25

If you’re using separedit (which I heartily recommend):

(defun yank-quoted ()
  (interactive)
  (separedit-dwim)
  (yank)
  (separedit-commit))

👀 2
Benjamin C16:06:38

Thanks @U1UQEM078, separedit looks fantastic!

🎉 2
escherize22:06:09

I wrote a hackable clipboard transformer that can do that. Can you write clojure that does the transformation you want? like, is it just string replace " with \"?

escherize22:06:46

here’s what a config entry for it looks like:

{:re #"replace! (.+) with (.+) in (.+)"
  :export (fn [[_ search replace text]]
            (str/replace text (re-pattern search) replace))}

🧡 2
escherize22:06:20

then when you copy replace! a with ZZ in abcd your clipboard will contain ZZbcd

escherize22:06:59

the transformation configs are pluggable 🤓