Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.7 KiB

5 years ago
  1. ;;; cider-grimoire.el --- Grimoire integration -*- lexical-binding: t -*-
  2. ;; Copyright © 2014-2019 Bozhidar Batsov and CIDER contributors
  3. ;;
  4. ;; Author: Bozhidar Batsov <bozhidar@batsov.com>
  5. ;; This program is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;; This file is not part of GNU Emacs.
  16. ;;; Commentary:
  17. ;; A few commands for Grimoire documentation lookup.
  18. ;;; Code:
  19. (require 'cider-client)
  20. (require 'cider-common)
  21. (require 'subr-x)
  22. (require 'cider-compat)
  23. (require 'cider-popup)
  24. (require 'nrepl-dict)
  25. (require 'url-vars)
  26. (declare-function markdown-mode "markdown-mode.el")
  27. (declare-function markdown-toggle-fontify-code-blocks-natively "markdown-mode.el")
  28. (defconst cider-grimoire-url "http://conj.io/")
  29. (defconst cider-grimoire-buffer "*cider-grimoire*")
  30. (defun cider-grimoire-replace-special (name)
  31. "Convert the dashes in NAME to a grimoire friendly format."
  32. (thread-last name
  33. (replace-regexp-in-string "\\?" "_QMARK_")
  34. (replace-regexp-in-string "\\." "_DOT_")
  35. (replace-regexp-in-string "\\/" "_SLASH_")
  36. (replace-regexp-in-string "\\(\\`_\\)\\|\\(_\\'\\)" "")))
  37. (defun cider-grimoire-url (name ns)
  38. "Generate a grimoire search v0 url from NAME, NS."
  39. (let ((base-url cider-grimoire-url))
  40. (when (and name ns)
  41. (concat base-url "search/v0/" ns "/" (cider-grimoire-replace-special name) "/"))))
  42. (defun cider-grimoire-web-lookup (symbol)
  43. "Open the grimoire documentation for SYMBOL in a web browser."
  44. (if-let* ((var-info (cider-var-info symbol)))
  45. (let ((name (nrepl-dict-get var-info "name"))
  46. (ns (nrepl-dict-get var-info "ns")))
  47. (browse-url (cider-grimoire-url name ns)))
  48. (error "Symbol %s not resolved" symbol)))
  49. ;;;###autoload
  50. (defun cider-grimoire-web (&optional arg)
  51. "Open grimoire documentation in the default web browser.
  52. Prompts for the symbol to use, or uses the symbol at point, depending on
  53. the value of `cider-prompt-for-symbol'. With prefix arg ARG, does the
  54. opposite of what that option dictates."
  55. (interactive "P")
  56. (funcall (cider-prompt-for-symbol-function arg)
  57. "Grimoire doc for"
  58. #'cider-grimoire-web-lookup))
  59. (defun cider-create-grimoire-buffer (content)
  60. "Create a new grimoire buffer with CONTENT."
  61. (with-current-buffer (cider-popup-buffer cider-grimoire-buffer t)
  62. (read-only-mode -1)
  63. (insert content)
  64. (when (require 'markdown-mode nil 'noerror)
  65. (markdown-mode)
  66. (cider-popup-buffer-mode 1)
  67. (when (fboundp 'markdown-toggle-fontify-code-blocks-natively)
  68. (markdown-toggle-fontify-code-blocks-natively 1)))
  69. (view-mode 1)
  70. (goto-char (point-min))
  71. (current-buffer)))
  72. (defun cider-grimoire-lookup (symbol)
  73. "Look up the grimoire documentation for SYMBOL.
  74. If SYMBOL is a special form, the clojure.core ns is used, as is
  75. Grimoire's convention."
  76. (if-let* ((var-info (cider-var-info symbol)))
  77. (let ((name (nrepl-dict-get var-info "name"))
  78. (ns (nrepl-dict-get var-info "ns" "clojure.core"))
  79. (url-request-method "GET")
  80. (url-request-extra-headers `(("Content-Type" . "text/plain"))))
  81. (url-retrieve (cider-grimoire-url name ns)
  82. (lambda (_status)
  83. ;; we need to strip the http header
  84. (goto-char (point-min))
  85. (re-search-forward "^$")
  86. (delete-region (point-min) (point))
  87. (delete-blank-lines)
  88. ;; and create a new buffer with whatever is left
  89. (pop-to-buffer (cider-create-grimoire-buffer (buffer-string))))))
  90. (error "Symbol %s not resolved" symbol)))
  91. ;;;###autoload
  92. (defun cider-grimoire (&optional arg)
  93. "Open grimoire documentation in a popup buffer.
  94. Prompts for the symbol to use, or uses the symbol at point, depending on
  95. the value of `cider-prompt-for-symbol'. With prefix arg ARG, does the
  96. opposite of what that option dictates."
  97. (interactive "P")
  98. (when (derived-mode-p 'clojurescript-mode)
  99. (user-error "`cider-grimoire' doesn't support ClojureScript"))
  100. (funcall (cider-prompt-for-symbol-function arg)
  101. "Grimoire doc for"
  102. #'cider-grimoire-lookup))
  103. (provide 'cider-grimoire)
  104. ;;; cider-grimoire.el ends here