Skip to content

Commit

Permalink
Fix compatibility with ivy-occur
Browse files Browse the repository at this point in the history
- Updates `ivy--old-cands‘ with the result of counsel-jq (Fixes #7)
- Configurable buffer name and binary
  • Loading branch information
Gleek committed Feb 19, 2021
1 parent 153d70b commit f31b238
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions counsel-jq.el
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
(require 'swiper)

(defcustom counsel-jq-json-buffer-mode 'js-mode
"Major mode for the resulting *jq-json* buffer."
"Major mode for the resulting `counsel-jq' buffer."
:type '(function)
:require 'counsel-jq
:group 'counsel-jq)

(defvar counsel-jq-command "jq"
"Command for `counsel-jq'.")

(defvar counsel-jq-buffer "*jq-json*"
"Buffer for the `counsel-jq' query results.")

(defun counsel-jq-json (&optional query)
"Call 'jq' with the QUERY with a default of '.'."
(with-current-buffer
Expand All @@ -26,33 +32,37 @@
(call-process-region
(point-min)
(point-max)
"jq"
counsel-jq-command
nil
"*jq-json*"
counsel-jq-buffer
nil
"-M"
(or query "."))))

(defun counsel-jq-query-function (input)
"Wrapper function passing INPUT over to `counsel-jq-json'."
(when (get-buffer "*jq-json*")
(with-current-buffer "*jq-json*"
(when (get-buffer counsel-jq-buffer)
(with-current-buffer counsel-jq-buffer
(funcall counsel-jq-json-buffer-mode)
(erase-buffer)))
(counsel-jq-json input)
(split-string
(with-current-buffer "*jq-json*"
(buffer-string)) "\n"))
(setq ivy--old-cands
(split-string
(replace-regexp-in-string
"\n$" ""
(with-current-buffer counsel-jq-buffer
(buffer-string))) "\n")))

;;;###autoload
(defun counsel-jq ()
"Counsel interface for dynamically querying jq. Whenever you're happy with the query, hit RET and the results will be displayed to you in the buffer *jq-json*."
"Counsel interface for dynamically querying jq.
Whenever you're happy with the query, hit RET and the results
will be displayed to you in the buffer in `counsel-jq-buffer'."
(interactive)
(ivy-read "jq query: " #'counsel-jq-query-function
:action #'(1
("s" (lambda (x)
(split-window-below)
(switch-to-buffer "*jq-json*"))
("s" (lambda (_)
(display-buffer counsel-jq-buffer))
"show"))
:initial-input "."
:dynamic-collection t
Expand Down

0 comments on commit f31b238

Please sign in to comment.