1;;; alchemist-macroexpand.el --- Macro expansion support -*- lexical-binding: t -*- 2 3;; Copyright © 2014-2015 Samuel Tonini 4 5;; Author: Samuel Tonini <tonini.samuel@gmail.com 6 7;; This file is not part of GNU Emacs. 8 9;; This program is free software: you can redistribute it and/or modify 10;; it under the terms of the GNU General Public License as published by 11;; the Free Software Foundation, either version 3 of the License, or 12;; (at your option) any later version. 13 14;; This program is distributed in the hope that it will be useful, 15;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17;; GNU General Public License for more details. 18 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 22;;; Commentary: 23 24;; Macro expansion support 25 26;;; Code: 27 28(require 'alchemist-server) 29(require 'alchemist-interact) 30 31(defgroup alchemist-macroexpand nil 32 "Macro expansion support." 33 :prefix "alchemist-macroexpand-" 34 :group 'alchemist) 35 36(defvar alchemist-macroexpand-filter-output nil) 37 38(defconst alchemist-macroexpand-buffer-name "*alchemist macroexpand*" 39 "Name of the Elixir Macro expand buffer.") 40 41(defun alchemist-macroexpand-filter (_process output) 42 (setq alchemist-macroexpand-filter-output (cons output alchemist-macroexpand-filter-output)) 43 (when (alchemist-server-contains-end-marker-p output) 44 (alchemist-interact-create-popup alchemist-macroexpand-buffer-name 45 (alchemist-server-prepare-filter-output alchemist-macroexpand-filter-output) 46 #'(lambda () 47 (elixir-mode) 48 (alchemist-macroexpand-mode))) 49 (setq alchemist-macroexpand-filter-output nil))) 50 51(defun alchemist-macroexpand-insert-filter (_process output) 52 (setq alchemist-macroexpand-filter-output (cons output alchemist-macroexpand-filter-output)) 53 (when (alchemist-server-contains-end-marker-p output) 54 (alchemist-interact-insert-as-comment 55 (alchemist-server-prepare-filter-output alchemist-macroexpand-filter-output)) 56 (setq alchemist-macroexpand-filter-output nil))) 57 58(defun alchemist-macroexpand-expand-request (expr) 59 (let ((file (make-temp-file "alchemist-expand" nil ".exs"))) 60 (with-temp-file file 61 (insert expr)) 62 (alchemist-server-eval (format "{ :expand, '%s' }" file) #'alchemist-macroexpand-filter))) 63 64(defun alchemist-macroexpand-expand-and-print-request (expr) 65 (let ((file (make-temp-file "alchemist-expand" nil ".exs"))) 66 (with-temp-file file 67 (insert expr)) 68 (alchemist-server-eval (format "{ :expand, '%s' }" file) #'alchemist-macroexpand-insert-filter))) 69 70(defun alchemist-macroexpand-expand-once-request (expr) 71 (let ((file (make-temp-file "alchemist-expand-once" nil ".exs"))) 72 (with-temp-file file 73 (insert expr)) 74 (alchemist-server-eval (format "{ :expand_once, '%s' }" file) #'alchemist-macroexpand-filter))) 75 76(defun alchemist-macroexpand-expand-once-and-print-request (expr) 77 (let ((file (make-temp-file "alchemist-expand-once" nil ".exs"))) 78 (with-temp-file file 79 (insert expr)) 80 (alchemist-server-eval (format "{ :expand_once, '%s' }" file) #'alchemist-macroexpand-insert-filter))) 81 82(defun alchemist-macroexpand-current-line () 83 "Macro expand the Elixir code on the current line." 84 (interactive) 85 (alchemist-macroexpand-expand-request (thing-at-point 'line))) 86 87(defun alchemist-macroexpand-print-current-line () 88 "Macro expand the Elixir code on the current line and insert the result." 89 (interactive) 90 (alchemist-macroexpand-expand-and-print-request (thing-at-point 'line))) 91 92(defun alchemist-macroexpand-once-current-line () 93 "Macro expand the Elixir code on the current line." 94 (interactive) 95 (alchemist-macroexpand-expand-once-request (thing-at-point 'line))) 96 97(defun alchemist-macroexpand-once-print-current-line () 98 "Macro expand the Elixir code on the current line and insert the result." 99 (interactive) 100 (alchemist-macroexpand-expand-once-and-print-request (thing-at-point 'line))) 101 102(defun alchemist-macroexpand-print-region (beg end) 103 "Macro expand the Elixir code on marked region and insert the result." 104 (interactive (list (point) (mark))) 105 (unless (and beg end) 106 (error "The mark is not set now, so there is no region")) 107 (let ((string (buffer-substring-no-properties beg end))) 108 (when (> end beg) 109 (exchange-point-and-mark)) 110 (alchemist-macroexpand-expand-and-print-request string))) 111 112(defun alchemist-macroexpand-region (beg end) 113 "Macro expand the Elixir code on marked region." 114 (interactive (list (point) (mark))) 115 (unless (and beg end) 116 (error "The mark is not set now, so there is no region")) 117 (let ((string (buffer-substring-no-properties beg end))) 118 (alchemist-macroexpand-expand-request string))) 119 120(defun alchemist-macroexpand-once-print-region (beg end) 121 "Macro expand the Elixir code on marked region once and insert the result." 122 (interactive "r") 123 (let ((string (buffer-substring-no-properties beg end))) 124 (when (> end beg) 125 (exchange-point-and-mark)) 126 (alchemist-macroexpand-expand-once-and-print-request string))) 127 128(defun alchemist-macroexpand-once-region (beg end) 129 "Macro expand the Elixir code on marked region once." 130 (interactive (list (point) (mark))) 131 (unless (and beg end) 132 (error "The mark is not set now, so there is no region")) 133 (let ((string (buffer-substring-no-properties beg end))) 134 (alchemist-macroexpand-expand-once-request string))) 135 136(defun alchemist-macroexpand-close-popup () 137 "Quit the macroexpand buffer window." 138 (interactive) 139 (quit-windows-on alchemist-macroexpand-buffer-name)) 140 141(defvar alchemist-macroexpand-mode-map 142 (let ((map (make-sparse-keymap))) 143 (define-key map (kbd "q") #'quit-window) 144 map) 145 "Keymap for `alchemist-macroexpand-mode'.") 146 147(define-minor-mode alchemist-macroexpand-mode 148 "Minor mode for Alchemist Elixir macroexpand functionality. 149 150\\{alchemist-macroexpand-mode-map}" 151 nil 152 alchemist-macroexpand-mode-map) 153 154(provide 'alchemist-macroexpand) 155 156;;; alchemist-macroexpand.el ends here 157