├── README.md └── rustfmt.el /README.md: -------------------------------------------------------------------------------- 1 | # rustfmt.el - OBSOLETE 2 | 3 | [![MELPA][badge-melpa]](http://melpa.org/#/rustfmt) 4 | 5 | **This package has been merged into rust-mode. Please use that instead.** 6 | 7 | Format rust code in emacs using [rustfmt][]. 8 | 9 | ## Install 10 | 11 | Ensure you are using MELPA: 12 | 13 | ```lisp 14 | (require 'package) 15 | (add-to-list 'package-archives 16 | '("melpa" . "https://melpa.org/packages/") t) 17 | (package-initialize) 18 | ``` 19 | 20 | Then install the "rustfmt" package: 21 | 22 | * `M-x package-list-packages` 23 | * Find the "rustfmt" and install with `I` 24 | 25 | ## Usage 26 | 27 | Run M-x rustfmt-format-buffer to format the current buffer. 28 | 29 | For convenience, you may bind it to a key, such as: 30 | ```el 31 | (define-key rust-mode-map (kbd "C-c C-f") #'rustfmt-format-buffer) 32 | ``` 33 | 34 | Alternatively, run rustfmt before saving rust buffers: 35 | ```el 36 | (add-hook 'rust-mode-hook #'rustfmt-enable-on-save) 37 | ``` 38 | 39 | [rustfmt]: https://github.com/nrc/rustfmt 40 | [badge-melpa]: http://melpa.org/packages/rustfmt-badge.svg 41 | -------------------------------------------------------------------------------- /rustfmt.el: -------------------------------------------------------------------------------- 1 | ;;; rustfmt.el --- Format rust code using rustfmt -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2015 Fredrik Bergroth 4 | 5 | ;; Author: Fredrik Bergroth 6 | ;; URL: https://github.com/fbergroth/emacs-rustfmt 7 | ;; Keywords: convenience 8 | ;; Version: 0 9 | ;; Package-Requires: ((emacs "24")) 10 | 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 | 16 | ;; This program is distributed in the hope that it will be useful, 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | ;; GNU General Public License for more details. 20 | 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with this program. If not, see . 23 | 24 | ;;; Commentary: 25 | 26 | ;; *** OBSOLETE PACKAGE - This has been merged into rust-mode *** 27 | 28 | ;; Call `rustfmt-format-buffer' to format the current buffer using rustfmt. It is 29 | ;; convenient to bind it to a key, such as: 30 | ;; 31 | ;; (define-key rust-mode-map (kbd "C-c C-f") #'rustfmt-format-buffer) 32 | ;; 33 | ;; Alternatively, run rustfmt before saving rust buffers: 34 | ;; 35 | ;; (add-hook 'rust-mode-hook #'rustfmt-enable-on-save) 36 | ;; 37 | ;; Errors and warnings will be visible in the `*rustfmt*' buffer. 38 | 39 | ;;; Code: 40 | 41 | (defgroup rustfmt nil 42 | "Format rust buffers using rustfmt." 43 | :group 'convenience 44 | :prefix "rustfmt-") 45 | 46 | (defcustom rustfmt-bin "rustfmt" 47 | "Path to rustfmt executable." 48 | :type 'string) 49 | 50 | (defcustom rustfmt-popup-errors nil 51 | "Display error buffer when rustfmt fails." 52 | :type 'boolean) 53 | 54 | (defun rustfmt--call (buf) 55 | "Format BUF using rustfmt." 56 | (with-current-buffer (get-buffer-create "*rustfmt*") 57 | (erase-buffer) 58 | (insert-buffer-substring buf) 59 | (if (zerop (call-process-region (point-min) (point-max) rustfmt-bin t t nil)) 60 | (progn (copy-to-buffer buf (point-min) (point-max)) 61 | (kill-buffer)) 62 | (when rustfmt-popup-errors 63 | (display-buffer (current-buffer))) 64 | (error "Rustfmt failed, see *rustfmt* buffer for details")))) 65 | 66 | ;;;###autoload 67 | (defun rustfmt-format-buffer () 68 | "Format the current buffer using rustfmt." 69 | (interactive) 70 | (unless (executable-find rustfmt-bin) 71 | (error "Could not locate executable \"%s\"" rustfmt-bin)) 72 | 73 | (let ((cur-point (point)) 74 | (cur-win-start (window-start))) 75 | (rustfmt--call (current-buffer)) 76 | (goto-char cur-point) 77 | (set-window-start (selected-window) cur-win-start)) 78 | (message "Formatted buffer with rustfmt.")) 79 | 80 | ;;;###autoload 81 | (defun rustfmt-enable-on-save () 82 | "Run rustfmt when saving buffer." 83 | (interactive) 84 | (add-hook 'before-save-hook #'rustfmt-format-buffer nil t)) 85 | 86 | (provide 'rustfmt) 87 | ;;; rustfmt.el ends here 88 | --------------------------------------------------------------------------------