about summary refs log tree commit diff stats
path: root/.emacs.d/lisp/init-misc.el
blob: 86ec023372e55bb0423e3263cbdcee33639e8a22 (plain) (blame)
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
;;; init-misc.el --- Miscellaneous Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(use-package exec-path-from-shell
  :defer 5
  :if (memq window-system '(mac ns))
  :custom
  (exec-path-from-shell-arguments '("-l"))
  :config
  (exec-path-from-shell-initialize))

(use-package envrc
  :diminish
  :hook (elpaca-after-init . envrc-global-mode))

(use-package restclient
  :config
  (defvar restclient-saved-requests nil)
  (defun restclient-save-current (label)
    "Save the current request as `label' (or use a default based on method, url and entity)."
    (interactive "sLabel: ")
    (restclient-http-parse-current-and-do
     `(lambda (method url headers entity)
        (let ((lab (if (string-empty-p ,label)
                       (format "%s %s (%s ...)" method url (substring entity 0 (min (length entity) 200)))
                     ,label)))
          (push (cons lab (list method url headers entity nil nil)) restclient-saved-requests)))))

  (defun restclient-delete-saved-request ()
    "Delete a saved request."
    (interactive)
    (if (= 0 (length restclient-saved-requests))
        (message "No saved restclient requests to delete.")
      (setq restclient-saved-requests
            (assoc-delete-all (completing-read "Delete: " (map-keys restclient-saved-requests)) restclient-saved-requests))))

  (defun restclient-call-saved-request ()
    "Call a saved request."
    (interactive)
    (if (= 0 (length restclient-saved-requests))
        (message "No saved restclient requests found.")
      (let ((args (if (= 1 (length restclient-saved-requests))
                      (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))
  :mode (("\\.http\\'" . restclient-mode)))

(use-package restclient-jq
  :after restclient
  :demand t)

(use-package es-mode
  :mode "\.es\'")

(use-package json-mode)
(use-package jq-format)

(use-package csv-mode
  :bind (:map csv-mode-map
              ("M-]" . csv-forward-field)
              ("M-[" . csv-backward-field)))

(use-package yaml-mode
  :diminish
  :hook
  (yaml-mode . whitespace-mode)
  (yaml-mode . subword-mode))

(provide 'init-misc)
;;; init-misc.el ends here