emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/crux 3088be2 012/112: Add a way to make commands operate o


From: ELPA Syncer
Subject: [nongnu] elpa/crux 3088be2 012/112: Add a way to make commands operate on either a region or a buffer
Date: Wed, 11 Aug 2021 09:57:44 -0400 (EDT)

branch: elpa/crux
commit 3088be246ee18c5dccaf70afbf6f33a137654639
Author: Bozhidar Batsov <bozhidar@batsov.com>
Commit: Bozhidar Batsov <bozhidar@batsov.com>

    Add a way to make commands operate on either a region or a buffer
---
 README.md | 15 +++++++++++++++
 crux.el   | 11 +++++++++++
 2 files changed, 26 insertions(+)

diff --git a/README.md b/README.md
index a7a141e..3eecb43 100644
--- a/README.md
+++ b/README.md
@@ -61,6 +61,21 @@ Command                                     | Suggested 
Keybinding(s)         |
 `crux-kill-whole-line`                      | <kbd>Super-k</kbd> | Kill whole 
line
 `crux-kill-line-backwards`                  | <kbd>C-Backspace</kbd> | Kill 
line backwards
 
+## Using the bundled advices
+
+crux ships with some handy advises that can enhance the operation of existing 
commands. 
+
+### Make a function operate on a region or the entire buffer
+
+You can use `crux-with-region-or-buffer` to make a command acting
+normally on a region to operate on the entire buffer in the absense of
+a region. Here are a few examples you can stuff in your config:
+
+```el
+(crux-with-region-or-buffer indent-region)
+(crux-with-region-or-buffer untabify)
+```
+
 ## License
 
 Copyright © 2015 Bozhidar Batsov and [contributors][].
diff --git a/crux.el b/crux.el
index 57a015a..d099438 100644
--- a/crux.el
+++ b/crux.el
@@ -348,5 +348,16 @@ Doesn't mess with special buffers."
                            (t (error "Unknown shell")))))
     (find-file-other-window (expand-file-name shell-init-file (getenv 
"HOME")))))
 
+(defmacro crux-with-region-or-buffer (func)
+  "When called with no active region, call FUNC on current buffer.
+
+Use to make commands like `indent-region' work on both the region
+and the entire buffer (in the absense of a region)."
+  `(defadvice ,func (before with-region-or-buffer activate compile)
+     (interactive
+      (if mark-active
+          (list (region-beginning) (region-end))
+        (list (point-min) (point-max))))))
+
 (provide 'crux)
 ;;; crux.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]