dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

cider-macroexpansion.el (8070B)


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