|
| 1 | +;;; elixir-format.el --- Emacs plugin to mix format Elixir files |
| 2 | + |
| 3 | +;; Copyright 2017-2018 Anil Wadghule, Christian Kruse |
| 4 | + |
| 5 | +;; This file is NOT part of GNU Emacs. |
| 6 | + |
| 7 | +;; This program is free software; you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation; either version 2, or (at your option) |
| 10 | +;; any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;;; Commentary: |
| 18 | + |
| 19 | +;; The elixir-format function formats the elixir files with Elixir's `mix format` |
| 20 | +;; command |
| 21 | + |
| 22 | +;; e.g. |
| 23 | +;; |
| 24 | +;; (require 'elixir-format) |
| 25 | +;; M-x elixir-format |
| 26 | +;; |
| 27 | + |
| 28 | +(defcustom elixir-format-elixir-path "elixir" |
| 29 | + "Path to the Elixir interpreter." |
| 30 | + :type 'string |
| 31 | + :group 'elixir-format) |
| 32 | + |
| 33 | +(defcustom elixir-format-mix-path "/usr/bin/mix" |
| 34 | + "Path to the 'mix' executable." |
| 35 | + :type 'string |
| 36 | + :group 'elixir-format) |
| 37 | + |
| 38 | +(defcustom elixir-format-arguments nil |
| 39 | + "Additional arguments to 'mix format'" |
| 40 | + :type '(repeat string) |
| 41 | + :group 'elixir-format) |
| 42 | + |
| 43 | +(defcustom elixir-format-hook nil |
| 44 | + "Hook called by `elixir-format'." |
| 45 | + :type 'hook |
| 46 | + :group 'elixir-format) |
| 47 | + |
| 48 | + |
| 49 | +;;; Code |
| 50 | + |
| 51 | +;;;###autoload |
| 52 | +(defun elixir-format-before-save () |
| 53 | + "Add this to .emacs to run mix format on the current buffer when saving: |
| 54 | +\(add-hook 'before-save-hook 'elixir-format-before-save). |
| 55 | +
|
| 56 | +Note that this will cause ‘elixir-mode’ to get loaded the first time |
| 57 | +you save any file, kind of defeating the point of autoloading." |
| 58 | + |
| 59 | + (interactive) |
| 60 | + (when (eq major-mode 'elixir-mode) (elixir-format))) |
| 61 | + |
| 62 | + |
| 63 | +(defun elixir-format--goto-line (line) |
| 64 | + (goto-char (point-min)) |
| 65 | + (forward-line (1- line))) |
| 66 | + |
| 67 | +(defun elixir-format--delete-whole-line (&optional arg) |
| 68 | + "Delete the current line without putting it in the `kill-ring'. |
| 69 | +Derived from function `kill-whole-line'. ARG is defined as for that |
| 70 | +function. |
| 71 | +
|
| 72 | +Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)" |
| 73 | + (setq arg (or arg 1)) |
| 74 | + (if (and (> arg 0) |
| 75 | + (eobp) |
| 76 | + (save-excursion (forward-visible-line 0) (eobp))) |
| 77 | + (signal 'end-of-buffer nil)) |
| 78 | + (if (and (< arg 0) |
| 79 | + (bobp) |
| 80 | + (save-excursion (end-of-visible-line) (bobp))) |
| 81 | + (signal 'beginning-of-buffer nil)) |
| 82 | + (cond ((zerop arg) |
| 83 | + (delete-region (progn (forward-visible-line 0) (point)) |
| 84 | + (progn (end-of-visible-line) (point)))) |
| 85 | + ((< arg 0) |
| 86 | + (delete-region (progn (end-of-visible-line) (point)) |
| 87 | + (progn (forward-visible-line (1+ arg)) |
| 88 | + (unless (bobp) |
| 89 | + (backward-char)) |
| 90 | + (point)))) |
| 91 | + (t |
| 92 | + (delete-region (progn (forward-visible-line 0) (point)) |
| 93 | + (progn (forward-visible-line arg) (point)))))) |
| 94 | + |
| 95 | +(defun elixir-format--apply-rcs-patch (patch-buffer) |
| 96 | + "Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer. |
| 97 | +Shamelessly stolen from go-mode (https://github.com/dominikh/go-mode.el)" |
| 98 | + |
| 99 | + (let ((target-buffer (current-buffer)) |
| 100 | + ;; Relative offset between buffer line numbers and line numbers |
| 101 | + ;; in patch. |
| 102 | + ;; |
| 103 | + ;; Line numbers in the patch are based on the source file, so |
| 104 | + ;; we have to keep an offset when making changes to the |
| 105 | + ;; buffer. |
| 106 | + ;; |
| 107 | + ;; Appending lines decrements the offset (possibly making it |
| 108 | + ;; negative), deleting lines increments it. This order |
| 109 | + ;; simplifies the forward-line invocations. |
| 110 | + (line-offset 0)) |
| 111 | + (save-excursion |
| 112 | + (with-current-buffer patch-buffer |
| 113 | + (goto-char (point-min)) |
| 114 | + (while (not (eobp)) |
| 115 | + (unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)") |
| 116 | + (error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch")) |
| 117 | + (forward-line) |
| 118 | + (let ((action (match-string 1)) |
| 119 | + (from (string-to-number (match-string 2))) |
| 120 | + (len (string-to-number (match-string 3)))) |
| 121 | + (cond |
| 122 | + ((equal action "a") |
| 123 | + (let ((start (point))) |
| 124 | + (forward-line len) |
| 125 | + (let ((text (buffer-substring start (point)))) |
| 126 | + (with-current-buffer target-buffer |
| 127 | + (cl-decf line-offset len) |
| 128 | + (goto-char (point-min)) |
| 129 | + (forward-line (- from len line-offset)) |
| 130 | + (insert text))))) |
| 131 | + ((equal action "d") |
| 132 | + (with-current-buffer target-buffer |
| 133 | + (elixir-format--goto-line (- from line-offset)) |
| 134 | + (cl-incf line-offset len) |
| 135 | + (elixir-format--delete-whole-line len))) |
| 136 | + (t |
| 137 | + (error "Invalid rcs patch or internal error in elixir-format--apply-rcs-patch")))))))) |
| 138 | + ) |
| 139 | + |
| 140 | +;;;###autoload |
| 141 | +(defun elixir-format (&optional is-interactive) |
| 142 | + (interactive "p") |
| 143 | + |
| 144 | + (let ((outbuff (get-buffer-create "*elixir-format-output*")) |
| 145 | + (errbuff (get-buffer-create "*elixir-format-errors*")) |
| 146 | + (tmpfile (make-temp-file "elixir-format" nil ".ex")) |
| 147 | + (our-elixir-format-arguments (list elixir-format-mix-path "format")) |
| 148 | + (output nil)) |
| 149 | + |
| 150 | + (unwind-protect |
| 151 | + (save-restriction |
| 152 | + (with-current-buffer outbuff |
| 153 | + (erase-buffer)) |
| 154 | + |
| 155 | + (with-current-buffer errbuff |
| 156 | + (setq buffer-read-only nil) |
| 157 | + (erase-buffer)) |
| 158 | + |
| 159 | + (write-region nil nil tmpfile) |
| 160 | + |
| 161 | + (run-hooks 'elixir-format-hook) |
| 162 | + |
| 163 | + (when elixir-format-arguments |
| 164 | + (setq our-elixir-format-arguments (append our-elixir-format-arguments elixir-format-arguments))) |
| 165 | + (setq our-elixir-format-arguments (append our-elixir-format-arguments (list tmpfile))) |
| 166 | + |
| 167 | + (if (zerop (apply #'call-process elixir-format-elixir-path nil errbuff nil our-elixir-format-arguments)) |
| 168 | + (progn |
| 169 | + (if (zerop (call-process-region (point-min) (point-max) "diff" nil outbuff nil "-n" "-" tmpfile)) |
| 170 | + (message "File is already formatted") |
| 171 | + (progn |
| 172 | + (elixir-format--apply-rcs-patch outbuff) |
| 173 | + (message "mix format applied"))) |
| 174 | + (kill-buffer errbuff)) |
| 175 | + |
| 176 | + (progn |
| 177 | + (with-current-buffer errbuff |
| 178 | + (setq buffer-read-only t) |
| 179 | + (ansi-color-apply-on-region (point-min) (point-max)) |
| 180 | + (special-mode)) |
| 181 | + |
| 182 | + (if is-interactive |
| 183 | + (display-buffer errbuff) |
| 184 | + (error "elixir-format failed: see %s" (buffer-name errbuff))))) |
| 185 | + |
| 186 | + (delete-file tmpfile) |
| 187 | + (kill-buffer outbuff))))) |
| 188 | + |
| 189 | +(provide 'elixir-format) |
| 190 | + |
| 191 | +;;; elixir-format.el ends here |
0 commit comments