init-misc.el 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; init-misc.el --- Miscellaneous Configuration File -*- lexical-binding: t -*-
  2. ;;; Commentary:
  3. ;;; Code:
  4. (use-package exec-path-from-shell
  5. :defer 5
  6. :if (memq window-system '(mac ns))
  7. :custom
  8. (exec-path-from-shell-arguments '("-l"))
  9. :config
  10. (exec-path-from-shell-initialize))
  11. (use-package envrc
  12. :diminish
  13. :hook (elpaca-after-init . envrc-global-mode))
  14. (use-package restclient
  15. :config
  16. (defvar restclient-saved-requests nil)
  17. (defun restclient-save-current (label)
  18. "Save the current request as `label' (or use a default based on method, url and entity)."
  19. (interactive "sLabel: ")
  20. (restclient-http-parse-current-and-do
  21. `(lambda (method url headers entity)
  22. (let ((lab (if (string-empty-p ,label)
  23. (format "%s %s (%s ...)" method url (substring entity 0 (min (length entity) 200)))
  24. ,label)))
  25. (push (cons lab (list method url headers entity nil nil)) restclient-saved-requests)))))
  26. (defun restclient-delete-saved-request ()
  27. "Delete a saved request."
  28. (interactive)
  29. (if (= 0 (length restclient-saved-requests))
  30. (message "No saved restclient requests to delete.")
  31. (setq restclient-saved-requests
  32. (assoc-delete-all (completing-read "Delete: " (map-keys restclient-saved-requests)) restclient-saved-requests))))
  33. (defun restclient-call-saved-request ()
  34. "Call a saved request."
  35. (interactive)
  36. (if (= 0 (length restclient-saved-requests))
  37. (message "No saved restclient requests found.")
  38. (let ((args (if (= 1 (length restclient-saved-requests))
  39. (cdar restclient-saved-requests)
  40. (alist-get (completing-read "Call: " (map-keys restclient-saved-requests)) restclient-saved-requests nil nil 'string-equal))))
  41. (apply 'restclient-http-do args))))
  42. ;; https://github.com/pashky/restclient.el/issues/288#issuecomment-1775770753
  43. (defun my/restclient-copy-curl-command ()
  44. "Formats the request as a curl command and copies the command to the clipboard."
  45. (interactive)
  46. (restclient-http-parse-current-and-do
  47. '(lambda (method url headers entity)
  48. (let* ((header-args
  49. (apply 'append
  50. (mapcar (lambda (header)
  51. (list "-H" (format "\"%s: %s\"" (car header) (cdr header))))
  52. headers)))
  53. (header-parsed (mapconcat 'identity header-args " "))
  54. (method-arg (concat "-X" " " method))
  55. (entity-arg (if (> 0 (string-width entity)) ""
  56. (format "-d \x27%s\x27" entity)))
  57. (curl-command (format "curl %s %s %s %s" header-parsed method-arg url entity-arg)))
  58. (kill-new curl-command)
  59. (message "curl command copied to clipboard.")))))
  60. :bind
  61. ("C-c M-h" . restclient-call-saved-request)
  62. (:map restclient-mode-map ("C-c h" . restclient-save-current))
  63. :mode (("\\.http\\'" . restclient-mode)))
  64. (use-package restclient-jq
  65. :after restclient
  66. :demand t)
  67. (use-package es-mode
  68. :mode "\.es\'")
  69. (use-package json-mode)
  70. (use-package jq-format)
  71. (use-package csv-mode
  72. :bind (:map csv-mode-map
  73. ;; TODO find something less awkward
  74. ("C-M-)" . csv-forward-field)
  75. ("C-M-(" . csv-backward-field)))
  76. (use-package yaml-mode
  77. :diminish
  78. :hook
  79. (yaml-mode . whitespace-mode)
  80. (yaml-mode . subword-mode))
  81. (provide 'init-misc)
  82. ;;; init-misc.el ends here