|
| 1 | +;;; haskell-hoogle.el --- Look up Haskell documentation via hoogle or hayoo -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2015 Steve Purcell |
| 4 | + |
| 5 | +;; Author: Steve Purcell <[email protected]> |
| 6 | +;; Keywords: docs |
| 7 | + |
| 8 | +;; This program is free software; you can redistribute it and/or modify |
| 9 | +;; it under the terms of the GNU General Public License as published by |
| 10 | +;; the Free Software Foundation, either version 3 of the License, or |
| 11 | +;; (at your option) any later version. |
| 12 | + |
| 13 | +;; This program is distributed in the hope that it will be useful, |
| 14 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +;; GNU General Public License for more details. |
| 17 | + |
| 18 | +;; You should have received a copy of the GNU General Public License |
| 19 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +;;; Commentary: |
| 22 | + |
| 23 | +;; Functions for looking up documentation with hayoo or hoogle, via |
| 24 | +;; either local or remote servers. |
| 25 | + |
| 26 | +;;; Code: |
| 27 | + |
| 28 | +(require 'ansi-color) |
| 29 | +(require 'haskell-mode) |
| 30 | + |
| 31 | + |
| 32 | +;;;###autoload |
| 33 | +(defcustom haskell-hoogle-command |
| 34 | + (if (executable-find "hoogle") "hoogle") |
| 35 | + "Name of the command to use to query Hoogle. |
| 36 | +If nil, use the Hoogle web-site." |
| 37 | + :group 'haskell |
| 38 | + :type '(choice (const :tag "Use Web-site" nil) |
| 39 | + string)) |
| 40 | + |
| 41 | +;;;###autoload |
| 42 | +(defcustom haskell-hoogle-url "http://haskell.org/hoogle/?q=%s" |
| 43 | + "Default value for hoogle web site." |
| 44 | + :group 'haskell |
| 45 | + :type '(choice |
| 46 | + (const :tag "haskell-org" "http://haskell.org/hoogle/?q=%s") |
| 47 | + (const :tag "fp-complete" "https://www.stackage.org/lts/hoogle?q=%s") |
| 48 | + string)) |
| 49 | + |
| 50 | +;;;###autoload |
| 51 | +(defun haskell-hoogle (query &optional info) |
| 52 | + "Do a Hoogle search for QUERY. |
| 53 | +When `haskell-hoogle-command' is non-nil, this command runs |
| 54 | +that. Otherwise, it opens a hoogle search result in the browser. |
| 55 | +
|
| 56 | +If prefix argument INFO is given, then `haskell-hoogle-command' |
| 57 | +is asked to show extra info for the items matching QUERY.." |
| 58 | + (interactive |
| 59 | + (let ((def (haskell-ident-at-point))) |
| 60 | + (if (and def (symbolp def)) (setq def (symbol-name def))) |
| 61 | + (list (read-string (if def |
| 62 | + (format "Hoogle query (default %s): " def) |
| 63 | + "Hoogle query: ") |
| 64 | + nil nil def) |
| 65 | + current-prefix-arg))) |
| 66 | + (if (null haskell-hoogle-command) |
| 67 | + (browse-url (format haskell-hoogle-url (url-hexify-string query))) |
| 68 | + (with-help-window "*hoogle*" |
| 69 | + (with-current-buffer standard-output |
| 70 | + (insert (shell-command-to-string |
| 71 | + (concat haskell-hoogle-command |
| 72 | + (if info " -i " "") |
| 73 | + " --color " (shell-quote-argument query)))) |
| 74 | + (ansi-color-apply-on-region (point-min) (point-max)))))) |
| 75 | + |
| 76 | +;;;###autoload |
| 77 | +(defalias 'hoogle 'haskell-hoogle) |
| 78 | + |
| 79 | +(defvar haskell-hoogle-server-process-name "emacs-local-hoogle") |
| 80 | +(defvar haskell-hoogle-server-buffer-name (format "*%s*" haskell-hoogle-server-process-name)) |
| 81 | +(defvar haskell-hoogle-port-number 49513 "Port number.") |
| 82 | +(defvar haskell-hoogle-server-process nil "The process handle of the local hoogle server.") |
| 83 | + |
| 84 | +(defun haskell-hoogle-start-server () |
| 85 | + "Start hoogle local server." |
| 86 | + (interactive) |
| 87 | + (if (executable-find "hoogle") |
| 88 | + (unless (haskell-hoogle-server-live-p) |
| 89 | + (set 'haskell-hoogle-server-process |
| 90 | + (start-process |
| 91 | + haskell-hoogle-server-process-name |
| 92 | + (get-buffer-create haskell-hoogle-server-buffer-name) |
| 93 | + "hoogle" "server" "-p" (number-to-string haskell-hoogle-port-number)))) |
| 94 | + (error "\"hoogle\" executable not found"))) |
| 95 | + |
| 96 | +(defun haskell-hoogle-server-live-p () |
| 97 | + "Whether the hoogle server process is live." |
| 98 | + (condition-case _err |
| 99 | + (process-live-p haskell-hoogle-server-process) |
| 100 | + (error nil))) |
| 101 | + |
| 102 | +(defun haskell-hoogle-kill-server () |
| 103 | + "Kill the hoogle server if it is live." |
| 104 | + (interactive) |
| 105 | + (when (haskell-hoogle-server-live-p) |
| 106 | + (kill-process (get-buffer-create haskell-hoogle-server-buffer-name)) |
| 107 | + (set 'haskell-hoogle-server-process nil))) |
| 108 | + |
| 109 | +;;;###autoload |
| 110 | +(defun haskell-hoogle-lookup-from-local () |
| 111 | + "Lookup by local hoogle." |
| 112 | + (interactive) |
| 113 | + (if (haskell-hoogle-server-live-p) |
| 114 | + (browse-url (format "http://localhost:%i/?hoogle=%s" |
| 115 | + haskell-hoogle-port-number |
| 116 | + (read-string "hoogle: " (haskell-ident-at-point)))) |
| 117 | + (when (y-or-n-p "Hoogle server not running, start hoogle server? ") |
| 118 | + (haskell-hoogle-start-server)))) |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +;;;###autoload |
| 123 | +(defcustom haskell-hayoo-url "http://hayoo.fh-wedel.de/?query=%s" |
| 124 | + "Default value for hayoo web site." |
| 125 | + :group 'haskell |
| 126 | + :type '(choice |
| 127 | + (const :tag "fh-wedel.de" "http://hayoo.fh-wedel.de/?query=%s") |
| 128 | + string)) |
| 129 | + |
| 130 | +;;;###autoload |
| 131 | +(defun haskell-hayoo (query) |
| 132 | + "Do a Hayoo search for QUERY." |
| 133 | + (interactive |
| 134 | + (let ((def (haskell-ident-at-point))) |
| 135 | + (if (and def (symbolp def)) (setq def (symbol-name def))) |
| 136 | + (list (read-string (if def |
| 137 | + (format "Hayoo query (default %s): " def) |
| 138 | + "Hayoo query: ") |
| 139 | + nil nil def)))) |
| 140 | + (browse-url (format haskell-hayoo-url (url-hexify-string query)))) |
| 141 | + |
| 142 | +;;;###autoload |
| 143 | +(defalias 'hayoo 'haskell-hayoo) |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +(provide 'haskell-hoogle) |
| 149 | +;;; haskell-hoogle.el ends here |
0 commit comments