-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathivy-hoogle.el
92 lines (77 loc) · 3.37 KB
/
ivy-hoogle.el
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
;;; ivy-hoogle.el --- hoogle search for ivy
;; Copyright (C) 2018 Sam Schweigel
;; Author: Sam Schweigel <[email protected]>
;; Keywords: lisp
;; Version: 0.0.1
;; Package-Requires: ((ivy "0.10.0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Adds a syntax-coloured hoogle search through ivy.
;;; Code:
(require 'ivy)
(require 's)
(defun ivy-hoogle--do-search (&optional request-prefix)
(let* ((pattern (or (and request-prefix
(concat request-prefix
" "))))
(lim 100)
(args (append (list "search" "-l")
(and lim (list "-n" (int-to-string lim)))
(list pattern))))
(let (candidates)
(with-temp-buffer
(apply #'call-process "hoogle" nil t nil args)
(goto-char (point-min))
(while (not (eobp))
(if (looking-at "\\(.+?\\) -- \\(.+\\)")
(push (propertize (match-string 1)
'hoogle-url (match-string-no-properties 2))
candidates))
(forward-line 1)))
(nreverse candidates))))
(defun ivy-hoogle--fontify-haskell (src)
(with-temp-buffer
(setq case-fold-search nil)
(insert src)
(goto-char (point-min))
(when (search-forward-regexp "\\(\\(?:[A-Z]\\w*\.?\\)+\\)" nil 'noerror)
(replace-match (propertize (match-string 1) 'face 'haskell-type-face)))
(when (search-forward-regexp "\\(\\w*\\)" nil 'noerror)
(replace-match (propertize (match-string 1) 'face 'haskell-definition-face)))
(while (search-forward-regexp "\\([A-Z]\\w*\\)" nil 'noerror)
(let ((ref (match-string 1)))
(replace-match (propertize ref 'face 'haskell-type-face))))
(goto-char (point-min))
(while (search-forward-regexp "\\([!#$%&*+./<=>?@\\^|-~:]\\)" nil 'noerror)
(let ((ref (match-string 1)))
(replace-match (propertize ref 'face 'haskell-operator-face))))
(goto-char (point-min))
(while (search-forward-regexp "\\(case\\|class\\|data\\|default\\|deriving\\|do\\|else\\|if\\|import\\|in\\|infix\\|infixl\\|infixr\\|instance\\|let\\|module\\|mdo\\|newtype\\|of\\|rec\\|pattern\\|proc\\|then\\|type\\|where\\|_\\|package\\)\\s-+" nil 'noerror)
(let ((ref (match-string 0)))
(replace-match (propertize ref 'face 'haskell-keyword-face))))
(buffer-string)))
(defun ivy-hoogle--function (str)
(mapcar #'ivy-hoogle--fontify-haskell (ivy-hoogle--do-search str)))
(defvar ivy-hoogle-history nil)
;;;###autoload
(defun ivy-hoogle ()
"Perform a hoogle search."
(interactive)
(ivy-read "Hoogle: "
#'ivy-hoogle--function
:dynamic-collection t
:history ivy-hoogle-history
:re-builder #'regexp-quote
:action (lambda (str)
(browse-url (get-text-property 0 'hoogle-url str)))))
(provide 'ivy-hoogle)
;;; ivy-hoogle.el ends here