Klimi's new dotfiles with stow.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

206 wiersze
7.9 KiB

5 lat temu
  1. ;;; cider-macroexpansion.el --- Macro expansion support -*- lexical-binding: t -*-
  2. ;; Copyright © 2012-2013 Tim King, Phil Hagelberg, Bozhidar Batsov
  3. ;; Copyright © 2013-2019 Bozhidar Batsov, Artur Malabarba and CIDER contributors
  4. ;;
  5. ;; Author: Tim King <kingtim@gmail.com>
  6. ;; Phil Hagelberg <technomancy@gmail.com>
  7. ;; Bozhidar Batsov <bozhidar@batsov.com>
  8. ;; Artur Malabarba <bruce.connor.am@gmail.com>
  9. ;; Hugo Duncan <hugo@hugoduncan.org>
  10. ;; Steve Purcell <steve@sanityinc.com>
  11. ;; This program is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; This program is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. ;; This file is not part of GNU Emacs.
  22. ;;; Commentary:
  23. ;; Macro expansion support.
  24. ;;; Code:
  25. (require 'cider-mode)
  26. (require 'subr-x)
  27. (require 'cider-compat)
  28. (defconst cider-macroexpansion-buffer "*cider-macroexpansion*")
  29. (defcustom cider-macroexpansion-display-namespaces 'tidy
  30. "Determines if namespaces are displayed in the macroexpansion buffer.
  31. Possible values are:
  32. 'qualified ;=> Vars are fully-qualified in the expansion
  33. 'none ;=> Vars are displayed without namespace qualification
  34. 'tidy ;=> Vars that are :refer-ed or defined in the current namespace are
  35. displayed with their simple name, non-referred vars from other
  36. namespaces are referred using the alias for that namespace (if
  37. defined), other vars are displayed fully qualified."
  38. :type '(choice (const :tag "Suppress namespaces" none)
  39. (const :tag "Show fully-qualified namespaces" qualified)
  40. (const :tag "Show namespace aliases" tidy))
  41. :group 'cider
  42. :package-version '(cider . "0.7.0"))
  43. (defcustom cider-macroexpansion-print-metadata nil
  44. "Determines if metadata is included in macroexpansion results."
  45. :type 'boolean
  46. :group 'cider
  47. :package-version '(cider . "0.9.0"))
  48. (defun cider-sync-request:macroexpand (expander expr &optional display-namespaces)
  49. "Macroexpand, using EXPANDER, the given EXPR.
  50. The default for DISPLAY-NAMESPACES is taken from
  51. `cider-macroexpansion-display-namespaces'."
  52. (cider-ensure-op-supported "macroexpand")
  53. (thread-first `("op" "macroexpand"
  54. "expander" ,expander
  55. "code" ,expr
  56. "ns" ,(cider-current-ns)
  57. "display-namespaces" ,(or display-namespaces
  58. (symbol-name cider-macroexpansion-display-namespaces)))
  59. (nconc (when cider-macroexpansion-print-metadata
  60. '("print-meta" "true")))
  61. (cider-nrepl-send-sync-request)
  62. (nrepl-dict-get "expansion")))
  63. (defun cider-macroexpand-undo (&optional arg)
  64. "Undo the last macroexpansion, using `undo-only'.
  65. ARG is passed along to `undo-only'."
  66. (interactive)
  67. (let ((inhibit-read-only t))
  68. (undo-only arg)))
  69. (defvar cider-last-macroexpand-expression nil
  70. "Specify the last macroexpansion preformed.
  71. This variable specifies both what was expanded and the expander.")
  72. (defun cider-macroexpand-expr (expander expr)
  73. "Macroexpand, use EXPANDER, the given EXPR."
  74. (when-let* ((expansion (cider-sync-request:macroexpand expander expr)))
  75. (setq cider-last-macroexpand-expression expr)
  76. (cider-initialize-macroexpansion-buffer expansion (cider-current-ns))))
  77. (defun cider-macroexpand-expr-inplace (expander)
  78. "Substitute the form preceding point with its macroexpansion using EXPANDER."
  79. (interactive)
  80. (let* ((expansion (cider-sync-request:macroexpand expander (cider-last-sexp)))
  81. (bounds (cons (save-excursion (clojure-backward-logical-sexp 1) (point)) (point))))
  82. (cider-redraw-macroexpansion-buffer
  83. expansion (current-buffer) (car bounds) (cdr bounds))))
  84. (defun cider-macroexpand-again ()
  85. "Repeat the last macroexpansion."
  86. (interactive)
  87. (cider-initialize-macroexpansion-buffer cider-last-macroexpand-expression (cider-current-ns)))
  88. ;;;###autoload
  89. (defun cider-macroexpand-1 (&optional prefix)
  90. "Invoke \\=`macroexpand-1\\=` on the expression preceding point.
  91. If invoked with a PREFIX argument, use \\=`macroexpand\\=` instead of
  92. \\=`macroexpand-1\\=`."
  93. (interactive "P")
  94. (let ((expander (if prefix "macroexpand" "macroexpand-1")))
  95. (cider-macroexpand-expr expander (cider-last-sexp))))
  96. (defun cider-macroexpand-1-inplace (&optional prefix)
  97. "Perform inplace \\=`macroexpand-1\\=` on the expression preceding point.
  98. If invoked with a PREFIX argument, use \\=`macroexpand\\=` instead of
  99. \\=`macroexpand-1\\=`."
  100. (interactive "P")
  101. (let ((expander (if prefix "macroexpand" "macroexpand-1")))
  102. (cider-macroexpand-expr-inplace expander)))
  103. ;;;###autoload
  104. (defun cider-macroexpand-all ()
  105. "Invoke \\=`macroexpand-all\\=` on the expression preceding point."
  106. (interactive)
  107. (cider-macroexpand-expr "macroexpand-all" (cider-last-sexp)))
  108. (defun cider-macroexpand-all-inplace ()
  109. "Perform inplace \\=`macroexpand-all\\=` on the expression preceding point."
  110. (interactive)
  111. (cider-macroexpand-expr-inplace "macroexpand-all"))
  112. (defun cider-initialize-macroexpansion-buffer (expansion ns)
  113. "Create a new Macroexpansion buffer with EXPANSION and namespace NS."
  114. (pop-to-buffer (cider-create-macroexpansion-buffer))
  115. (setq cider-buffer-ns ns)
  116. (setq buffer-undo-list nil)
  117. (let ((inhibit-read-only t)
  118. (buffer-undo-list t))
  119. (erase-buffer)
  120. (insert (format "%s" expansion))
  121. (goto-char (point-max))
  122. (cider--font-lock-ensure)))
  123. (defun cider-redraw-macroexpansion-buffer (expansion buffer start end)
  124. "Redraw the macroexpansion with new EXPANSION.
  125. Text in BUFFER from START to END is replaced with new expansion,
  126. and point is placed after the expanded form."
  127. (with-current-buffer buffer
  128. (let ((buffer-read-only nil))
  129. (goto-char start)
  130. (delete-region start end)
  131. (insert (format "%s" expansion))
  132. (goto-char start)
  133. (indent-sexp)
  134. (forward-sexp))))
  135. (declare-function cider-mode "cider-mode")
  136. (defun cider-create-macroexpansion-buffer ()
  137. "Create a new macroexpansion buffer."
  138. (with-current-buffer (cider-popup-buffer cider-macroexpansion-buffer 'select 'clojure-mode 'ancillary)
  139. (cider-mode -1)
  140. (cider-macroexpansion-mode 1)
  141. (current-buffer)))
  142. (declare-function cider-find-var "cider-find")
  143. (defvar cider-macroexpansion-mode-map
  144. (let ((map (make-sparse-keymap)))
  145. (define-key map (kbd "g") #'cider-macroexpand-again)
  146. (define-key map (kbd "q") #'cider-popup-buffer-quit-function)
  147. (define-key map (kbd "d") #'cider-doc)
  148. (define-key map (kbd "j") #'cider-javadoc)
  149. (define-key map (kbd ".") #'cider-find-var)
  150. (define-key map (kbd "m") #'cider-macroexpand-1-inplace)
  151. (define-key map (kbd "a") #'cider-macroexpand-all-inplace)
  152. (define-key map (kbd "u") #'cider-macroexpand-undo)
  153. (define-key map [remap undo] #'cider-macroexpand-undo)
  154. (easy-menu-define cider-macroexpansion-mode-menu map
  155. "Menu for CIDER's doc mode"
  156. '("Macroexpansion"
  157. ["Restart expansion" cider-macroexpand-again]
  158. ["Macroexpand-1" cider-macroexpand-1-inplace]
  159. ["Macroexpand-all" cider-macroexpand-all-inplace]
  160. ["Macroexpand-undo" cider-macroexpand-undo]
  161. ["Go to source" cider-find-var]
  162. ["Go to doc" cider-doc]
  163. ["Go to Javadoc" cider-docview-javadoc]
  164. ["Quit" cider-popup-buffer-quit-function]))
  165. map))
  166. (define-minor-mode cider-macroexpansion-mode
  167. "Minor mode for CIDER macroexpansion.
  168. \\{cider-macroexpansion-mode-map}"
  169. nil
  170. " Macroexpand"
  171. cider-macroexpansion-mode-map)
  172. (provide 'cider-macroexpansion)
  173. ;;; cider-macroexpansion.el ends here