Klimi's new dotfiles with stow.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

161 řádky
5.8 KiB

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