about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/nix-mode-20180908.2240/nix-search.el
blob: 85a1e837f299a291a9cd9392d38c225673f7c5fd (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
;;; nix-search.el -- run nix commands in Emacs -*- lexical-binding: t -*-

;; Author: Matthew Bauer <mjbauer95@gmail.com>
;; Homepage: https://github.com/NixOS/nix-mode
;; Keywords: nix

;; This file is NOT part of GNU Emacs.

;;; Commentary:

;;; Code:

(require 'nix)
(require 'nix-instantiate)
(require 'nix-shell)
(require 'json)

;;;###autoload
(defun nix-search (&optional search file)
  "Run nix search.
SEARCH a search term to use.
FILE a Nix expression to search in."
  (interactive)
  (unless search (setq search ""))
  (unless file (nix-read-file))

  (let ((stdout (generate-new-buffer "nix search"))
	result)
    (call-process nix-executable nil (list stdout nil) nil
		  "search" "--json" "-f" file search)
    (with-current-buffer stdout
      (when (eq (buffer-size) 0)
	(error "Error: nix search %s failed to produce any output" search))
      (goto-char (point-min))
      (setq result (json-read)))
    (kill-buffer stdout)
    (when (called-interactively-p 'any)
      (let ((display (generate-new-buffer "*nix search*")))
	(with-current-buffer display
	  (dolist (entry result)
	    (widget-insert
	     (format "attr: %s\nname: %s\nversion: %s\ndescription: %s\n\n"
                     (car entry)
		     (alist-get 'pkgName (cdr entry))
		     (alist-get 'version (cdr entry))
		     (alist-get 'description (cdr entry)))))
	  )
	(display-buffer display 'display-buffer-pop-up-window)))
    (kill-buffer stdout)
    result))

(defun nix-search-read-attr (file)
  "Read from a list of attributes.
FILE the nix file to look in."
  (let ((collection
	 (sort (mapcar (lambda (x) (symbol-name (car x)))
		       (nix-search "" file))
	       'string<))
	(read (cond ((fboundp 'ivy-read) 'ivy-read)
		    (t 'completing-read))))
    (funcall read "Attribute: " collection)))

(provide 'nix-search)
;;; nix-search.el ends here