[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/reformatter 947411170d 18/81: Generate both -buffer and -r
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/reformatter 947411170d 18/81: Generate both -buffer and -region commands by default |
Date: |
Tue, 5 Sep 2023 04:03:33 -0400 (EDT) |
branch: elpa/reformatter
commit 947411170d5da2de8ff248a27e20c875e7fd9644
Author: Steve Purcell <steve@sanityinc.com>
Commit: Steve Purcell <steve@sanityinc.com>
Generate both -buffer and -region commands by default
---
README.md | 16 ++++++------
reformatter.el | 77 +++++++++++++++++++++++++++++++++++-----------------------
2 files changed, 55 insertions(+), 38 deletions(-)
diff --git a/README.md b/README.md
index 321feeb005..52467e0220 100644
--- a/README.md
+++ b/README.md
@@ -25,12 +25,13 @@ dhall executable:
:lighter 'DF)
```
-The `reformatter-define` macro expands to code which generates both
-a `dhall-format` interactive command and a local minor mode called
-`dhall-format-on-save-mode`. The `:args` and `:program` expressions
-will be evaluated at runtime, so they can refer to variables that may
-(later) have a buffer-local value. A custom variable will be generated
-for the mode lighter, with the supplied value becoming the default.
+The `reformatter-define` macro expands to code which generates
+`dhall-format-buffer` and `dhall-format-region` interactive commands,
+and a local minor mode called `dhall-format-on-save-mode`. The `:args`
+and `:program` expressions will be evaluated at runtime, so they can
+refer to variables that may (later) have a buffer-local value. A
+custom variable will be generated for the mode lighter, with the
+supplied value becoming the default.
The generated minor mode allows idiomatic per-directory or per-file
customisation, via the "modes" support baked into Emacs' file-local
@@ -50,7 +51,8 @@ Library authors might like to provide autoloads for the
generated
code, e.g.:
```el
-;;;###autoload (autoload 'dhall-format "current-file" nil t)
+;;;###autoload (autoload 'dhall-format-buffer "current-file" nil t)
+;;;###autoload (autoload 'dhall-format-region "current-file" nil t)
;;;###autoload (autoload 'dhall-format-on-save-mode "current-file" nil t)
```
diff --git a/reformatter.el b/reformatter.el
index c4fceeed1d..60cd9895e6 100644
--- a/reformatter.el
+++ b/reformatter.el
@@ -41,13 +41,14 @@
;; :program dhall-command
;; :args '("format"))
-;; The `reformatter-define' macro expands to code which generates both
-;; a `dhall-format' interactive command and a local minor mode
-;; called `dhall-format-on-save-mode'. The :args" and :program
-;; expressions will be evaluated at runtime, so they can refer to
-;; variables that may (later) have a buffer-local value. A custom
-;; variable will be generated for the mode lighter, with the supplied
-;; value becoming the default.
+;; The `reformatter-define' macro expands to code which generates
+;; `dhall-format-buffer' and `dhall-format-region' interactive
+;; commands, and a local minor mode called
+;; `dhall-format-on-save-mode'. The :args" and :program expressions
+;; will be evaluated at runtime, so they can refer to variables that
+;; may (later) have a buffer-local value. A custom variable will be
+;; generated for the mode lighter, with the supplied value becoming
+;; the default.
;; The generated minor mode allows idiomatic per-directory or per-file
;; customisation, via the "modes" support baked into Emacs' file-local
@@ -64,7 +65,8 @@
;; Library authors might like to provide autoloads for the generated
;; code, e.g.:
-;; ;;;###autoload (autoload 'dhall-format "current-file" nil t)
+;; ;;;###autoload (autoload 'dhall-format-buffer "current-file" nil t)
+;; ;;;###autoload (autoload 'dhall-format-region "current-file" nil t)
;; ;;;###autoload (autoload 'dhall-format-on-save-mode "current-file" nil
t)
;;; Code:
@@ -125,17 +127,19 @@ The macro accepts the following keyword arguments:
(cl-assert program)
;; Note: we skip using `gensym' here because the macro arguments are only
;; referred to once below, but this may have to change later.
- (let ((minor-mode-form
- (when mode
- (let ((on-save-mode-name (intern (format "%s-on-save-mode" name)))
- (lighter-name (intern (format "%s-on-save-mode-lighter"
name))))
- `(progn
- (defcustom ,lighter-name ,lighter
- ,(format "Mode lighter for `%s'." on-save-mode-name)
- :group ,group
- :type 'string)
- (define-minor-mode ,on-save-mode-name
- ,(format "When enabled, call `%s' when this buffer is saved.
+ (let* ((buffer-fn-name (intern (format "%s-buffer" name)))
+ (region-fn-name (intern (format "%s-region" name)))
+ (minor-mode-form
+ (when mode
+ (let ((on-save-mode-name (intern (format "%s-on-save-mode" name)))
+ (lighter-name (intern (format "%s-on-save-mode-lighter"
name))))
+ `(progn
+ (defcustom ,lighter-name ,lighter
+ ,(format "Mode lighter for `%s'." on-save-mode-name)
+ :group ,group
+ :type 'string)
+ (define-minor-mode ,on-save-mode-name
+ ,(format "When enabled, call `%s' when this buffer is saved.
To enable this unconditionally in a major mode, add this mode
to the major mode's hook. To enable it in specific files or directories,
@@ -145,20 +149,19 @@ might use
((some-major-mode
(mode . %s-on-save)))
" name name) nil
- :global nil
- :lighter ,lighter-name
- :keymap ,keymap
- :group ,group
- (if ,on-save-mode-name
- (add-hook 'before-save-hook ',name nil t)
- (remove-hook 'before-save-hook ',name t))))))))
+ :global nil
+ :lighter ,lighter-name
+ :keymap ,keymap
+ :group ,group
+ (if ,on-save-mode-name
+ (add-hook 'before-save-hook ',buffer-fn-name nil t)
+ (remove-hook 'before-save-hook ',buffer-fn-name t))))))))
`(progn
- (defun ,name (&optional display-errors)
- "Reformats the current buffer.
+ (defun ,region-fn-name (beg end &optional display-errors)
+ "Reformats the region from BEG to END.
When called interactively, or with prefix argument
DISPLAY-ERRORS, shows a buffer if the formatting fails."
- (interactive "p")
- (message "Formatting buffer")
+ (interactive "rp")
(let* ((err-file (make-temp-file ,(symbol-name name)))
(out-file (make-temp-file ,(symbol-name name)))
(coding-system-for-read 'utf-8)
@@ -166,7 +169,7 @@ DISPLAY-ERRORS, shows a buffer if the formatting fails."
(unwind-protect
(let* ((error-buffer (get-buffer-create ,(format "*%s errors*"
name)))
(retcode
- (apply 'call-process-region (point-min) (point-max)
,program
+ (apply 'call-process-region beg end ,program
nil (list (list :file out-file) err-file)
nil
,args)))
@@ -186,6 +189,18 @@ DISPLAY-ERRORS, shows a buffer if the formatting fails."
(message ,(concat (symbol-name name) " failed: see %s")
(buffer-name error-buffer)))))
(delete-file err-file)
(delete-file out-file))))
+
+ (defun ,buffer-fn-name (&optional display-errors)
+ "Reformats the current buffer.
+When called interactively, or with prefix argument
+DISPLAY-ERRORS, shows a buffer if the formatting fails."
+ (interactive "p")
+ (message "Formatting buffer")
+ (,region-fn-name (point-min) (point-max) display-errors))
+
+ ;; This alias will be removed in a future version
+ (defalias ',name ',buffer-fn-name)
+
,minor-mode-form)))
- [nongnu] elpa/reformatter cb9a593854 61/81: Rename repo, (continued)
- [nongnu] elpa/reformatter cb9a593854 61/81: Rename repo, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter e02a9ea942 63/81: Merge pull request #31 from wyuenho/patch-1, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter cc2bb75f21 22/81: Add missing colon in docstring, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter b29fdd346d 35/81: Add note about brokenness of replace-buffer-contents, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter b44ee754b7 29/81: Fix homepage link, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 58943b47b3 26/81: Start an FAQ section in the README, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 885a7f5547 02/81: Mark up code as elisp, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 3f081e4cd2 14/81: Provide more setup hints in the -on-save-mode docstring, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 7b336cab51 20/81: Link to sqlformat as another example of real-world usage, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter c2466f745a 21/81: Mention "needed by" reverse dependencies list on MELPA page, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 947411170d 18/81: Generate both -buffer and -region commands by default,
ELPA Syncer <=
- [nongnu] elpa/reformatter 8eec55689a 13/81: Doc clarification, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 8372cc4259 39/81: Prefer "elisp" for code blocks, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 466203594c 36/81: Fix broken link to example, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 8423ad9e83 47/81: Merge pull request #21 from erickgnavar/close-error-buffer-with-no-errors, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter c6919a6549 38/81: Merge pull request #17 from bradyt/patch-1, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 9d277200c1 52/81: Add Emacs 27.1 to CI matrix, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 0c24778dfc 50/81: Fix grammar, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter f5fbed9e7d 46/81: Close error window when there is no errors to be shown, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter e8f70b20ca 45/81: No longer forcibly call delete-trailing-whitespace, ELPA Syncer, 2023/09/05
- [nongnu] elpa/reformatter 3833c13dd9 51/81: Clarify how args are evaluated, and what to avoid, ELPA Syncer, 2023/09/05