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

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

[nongnu] elpa/evil-exchange 72bf29c9fa 01/46: initial commit


From: ELPA Syncer
Subject: [nongnu] elpa/evil-exchange 72bf29c9fa 01/46: initial commit
Date: Thu, 6 Jan 2022 03:59:50 -0500 (EST)

branch: elpa/evil-exchange
commit 72bf29c9fa3aa6b621183542977e46fbd49f3457
Author: Dewdrops <v_v_4474@126.com>
Commit: Dewdrops <v_v_4474@126.com>

    initial commit
---
 .gitignore       |   1 +
 README.md        |  40 +++++++++++++++++++
 evil-exchange.el | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..c531d9867f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.elc
diff --git a/README.md b/README.md
new file mode 100644
index 0000000000..55b68f741e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,40 @@
+evil-exchange
+============
+
+English is not my first language, so feel free to correct me of any mistake.
+
+Easy text exchange operator for Evil. This is the port of 
[vim-exchange](https://github.com/tommcdo/vim-exchange) by Tom McDonald.
+
+Default bindings
+--------
+
+`gx`
+
+On the first use, define the first {motion} to exchange. On the second use,
+define the second {motion} and perform the exchange.
+
+`gX`
+
+Clear any {motion} pending for exchange.
+
+### Notes
+
+* `gx` (and `gX`) can also be used from visual mode, which is sometimes easier 
than coming
+  up with the right {motion}
+* If you're using the same motion again (e.g. exchanging two words using
+  `gxiw`), you can use `.` (evil-repeat) the second time.
+
+Installation
+------------
+
+```lisp
+(require 'evil-exchange)
+;; if you want to change default key bindings
+;; (setq evil-exchange-key (kbd "zx"))
+(evil-exchange-install)
+```
+
+Customization
+-------
+
+You can change the default bindings by customizing `evil-exchange-key` and/or 
`evil-exchange-cancel-key` BEFORE  `evil-exchange-install` is called.
diff --git a/evil-exchange.el b/evil-exchange.el
new file mode 100644
index 0000000000..1cbdfe550d
--- /dev/null
+++ b/evil-exchange.el
@@ -0,0 +1,116 @@
+;;; evil-exchange.el --- Exchange text more easily within Evil
+
+;; Copyright (C) 2013 by Dewdrops
+
+;; Author: Dewdrops <v_v_4474@126.com>
+;; URL: http://github.com/Dewdrops/evil-exchange
+;; Version: 0.1
+;; Keywords: evil, plugin
+
+;; This file is NOT part of GNU Emacs.
+
+;;; License:
+;;
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Port of vim-exchange by Tom McDonald 
(https://github.com/tommcdo/vim-exchange)
+
+;; Installation:
+
+;; put evil-exchange.el into somewhere in your load-path and add these
+;; lines to your .emacs:
+;; (require 'evil-exchange)
+;; ;; if you want to change default key bindings, bind it to as you like HERE
+;; ;; (setq evil-exchange-key (kbd "zx"))
+;; (evil-exchange-install)
+
+;;; Code:
+
+(require 'evil)
+
+(defgroup evil-exchange nil
+  "Easy text exchange operator for Evil."
+  :prefix "evil-exchange"
+  :group 'evil)
+
+(defcustom evil-exchange-key (kbd "gx")
+  "Default binding for evil-exchange."
+  :type `,(if (get 'key-sequence 'widget-type)
+              'key-sequence
+            'sexp)
+  :group 'evil-exchange)
+
+(defcustom evil-exchange-cancel-key (kbd "gX")
+  "Default binding for evil-exchange-cancel."
+  :type `,(if (get 'key-sequence 'widget-type)
+              'key-sequence
+            'sexp)
+  :group 'evil-exchange)
+
+(defvar evil-exchange-position nil "Text position which will be exchanged")
+
+;;;###autoload
+(evil-define-operator evil-exchange (beg end type)
+  "Exchange two regions with evil motion."
+  :move-point nil
+  (interactive "<R>")
+  (let ((beg-marker (copy-marker beg t))
+        (end-marker (copy-marker end t)))
+    (if (null evil-exchange-position)
+        ;; call without evil-exchange-position set: store region
+        (setq evil-exchange-position (list beg-marker end-marker type))
+      ;; secondary call: do exchange
+      (cl-destructuring-bind
+          (orig-beg orig-end orig-type) evil-exchange-position
+        (cond
+         ;; exchange block region
+         ((and (eq orig-type 'block) (eq type 'block))
+          (let ((orig-rect (delete-extract-rectangle orig-beg orig-end))
+                (curr-rect (delete-extract-rectangle beg-marker end-marker)))
+            (save-excursion
+              (goto-char orig-beg)
+              (insert-rectangle curr-rect)
+              (goto-char beg-marker)
+              (insert-rectangle orig-rect)))
+          (setq evil-exchange-position nil))
+         ;; signal error if regions incompatible
+         ((or (eq orig-type 'block) (eq type 'block))
+          (error "Can't exchange block region with non-block region."))
+         ;; exchange normal region
+         (t
+          (transpose-regions orig-beg orig-end beg end)
+          (setq evil-exchange-position nil))))))
+  ;; place cursor on beginning of line
+  (when (and (evil-called-interactively-p) (eq type 'line))
+    (evil-first-non-blank)))
+
+;;;###autoload
+(defun evil-exchange-cancel ()
+  "Cancel current pending exchange."
+  (interactive)
+  (setq evil-exchange-position nil)
+  (message "Exchange cancelled"))
+
+;;;###autoload
+(defun evil-exchange-install ()
+  "Setting evil-exchange key bindings."
+  (define-key evil-normal-state-map evil-exchange-key 'evil-exchange)
+  (define-key evil-visual-state-map evil-exchange-key 'evil-exchange)
+  (define-key evil-normal-state-map evil-exchange-cancel-key 
'evil-exchange-cancel)
+  (define-key evil-visual-state-map evil-exchange-cancel-key 
'evil-exchange-cancel))
+
+(provide 'evil-exchange)
+;;; evil-exchange.el ends here



reply via email to

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