about summary refs log tree commit diff stats
path: root/.emacs.d/lisp/init-misc.el
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2024-08-16 12:20:18 +0100
committerDavid Morgan <djm_uk@protonmail.com>2024-08-16 12:20:18 +0100
commitf54e5ba11e5cc2c41c85ea79b80916c400b1c75f (patch)
tree30b18ba33e876053b0e1b36f68f4a1caa8fdaf99 /.emacs.d/lisp/init-misc.el
parent0f79b969c27476d5052ea06aaf1c5e38ee13ae04 (diff)
downloaddotfiles-f54e5ba11e5cc2c41c85ea79b80916c400b1c75f.tar.gz
Add restclient-copy-curl-command
Diffstat (limited to '.emacs.d/lisp/init-misc.el')
-rw-r--r--.emacs.d/lisp/init-misc.el19
1 files changed, 19 insertions, 0 deletions
diff --git a/.emacs.d/lisp/init-misc.el b/.emacs.d/lisp/init-misc.el
index d71c646..86ec023 100644
--- a/.emacs.d/lisp/init-misc.el
+++ b/.emacs.d/lisp/init-misc.el
@@ -44,6 +44,25 @@
                       (cdar restclient-saved-requests)
                     (alist-get (completing-read "Call: " (map-keys restclient-saved-requests)) restclient-saved-requests nil nil 'string-equal))))
         (apply 'restclient-http-do args))))
+
+  ;; https://github.com/pashky/restclient.el/issues/288#issuecomment-1775770753
+  (defun my/restclient-copy-curl-command ()
+    "Formats the request as a curl command and copies the command to the clipboard."
+    (interactive)
+    (restclient-http-parse-current-and-do
+     '(lambda (method url headers entity)
+        (let* ((header-args
+                (apply 'append
+                       (mapcar (lambda (header)
+                                 (list "-H" (format "\"%s: %s\"" (car header) (cdr header))))
+                               headers)))
+               (header-parsed (mapconcat 'identity header-args " "))
+               (method-arg (concat "-X" " " method))
+               (entity-arg (if (> 0 (string-width entity)) ""
+                             (format "-d \x27%s\x27" entity)))
+               (curl-command (format "curl %s %s %s %s" header-parsed method-arg url entity-arg)))
+          (kill-new curl-command)
+          (message "curl command copied to clipboard.")))))
   :bind
   ("C-c C-h" . restclient-call-saved-request)
   (:map restclient-mode-map ("C-c h" . restclient-save-current))