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

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

[nongnu] elpa/evil-numbers 221ceb6177 001/145: Initial commit.


From: ELPA Syncer
Subject: [nongnu] elpa/evil-numbers 221ceb6177 001/145: Initial commit.
Date: Thu, 6 Jan 2022 03:00:13 -0500 (EST)

branch: elpa/evil-numbers
commit 221ceb6177a97029d28bd1a245211e5f7f58ca63
Author: Michael Markert <markert.michael@googlemail.com>
Commit: Michael Markert <markert.michael@googlemail.com>

    Initial commit.
    
    Adapted from gist.
    
    Signed-off-by: Michael Markert <markert.michael@googlemail.com>
---
 .gitignore      |   1 +
 evil-numbers.el | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 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/evil-numbers.el b/evil-numbers.el
new file mode 100644
index 0000000000..d59e9da48f
--- /dev/null
+++ b/evil-numbers.el
@@ -0,0 +1,103 @@
+;; evil-numbers -- increment/decrement numbers like in vim
+
+;; Copyright (C) 2011 by Michael Markert
+;; Author: 2011 Michael Markert <markert.michael@googlemail.com>
+;; Created: 2011-09-02
+;; Version: 0.
+;; Keywords: numbers increment decrement octal hex binary
+
+;; This file is not part of GNU Emacs.
+
+;; 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 2 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 GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;; Known Bugs:
+;; See http://github.com/cofi/evil-numbers/issues
+
+;; Install:
+
+;; Usage:
+
+;; Homepage: http://github.com/cofi/evil-numbers
+;; Git-Repository: git://github.com/cofi/evil-numbers.git
+
+(defun evil-numbers/inc-at-pt (amount)
+  "Increment the number at point by `amount'"
+  (interactive "p*")
+  (save-match-data
+    (or
+     ;; find binary literals
+     (when (looking-back "0[bB][01]*")
+       ;; already ensured there's only one -
+       (skip-chars-backward "01")
+       (search-forward-regexp "[01]*")
+       (replace-match
+        (evil-numbers/format-binary (+ amount (string-to-number (match-string 
0) 2))
+                                    (- (match-end 0) (match-beginning 0))))
+       t)
+
+     ;; find octal literals
+     (when (looking-back "0[oO]-?[0-7]*")
+       ;; already ensured there's only one -
+       (skip-chars-backward "-01234567")
+       (search-forward-regexp "-?\\([0-7]+\\)")
+       (replace-match
+        (format (format "%%0%do" (- (match-end 1) (match-beginning 1)))
+                (+ amount (string-to-number (match-string 0) 8))))
+       t)
+
+     ;; find hex literals
+     (when (looking-back "0[xX]-?[0-9a-fA-F]*")
+       ;; already ensured there's only one -
+       (skip-chars-backward "-0123456789abcdefABCDEF")
+       (search-forward-regexp "-?\\([0-9a-fA-F]+\\)")
+       (replace-match
+        (format (format "%%0%dX" (- (match-end 1) (match-beginning 1)))
+                (+ amount (string-to-number (match-string 0) 16))))
+       t)
+
+     ;; find decimal literals
+     (progn
+       (skip-chars-backward "0123456789")
+       (skip-chars-backward "-")
+       (when (looking-at "-?\\([0-9]+\\)")
+         (replace-match
+          (format (format "%%0%dd" (- (match-end 1) (match-beginning 1)))
+                  (+ amount (string-to-number (match-string 0) 10))))
+         t))
+     (error "No number at point"))))
+
+(defun evil-numbers/format-binary (number &optional width fillchar)
+  "Format `NUMBER' as binary.
+Fill up to `WIDTH' with `FILLCHAR' (defaults to ?0) if binary
+representation of `NUMBER' is smaller."
+  (let (nums
+        (fillchar (or fillchar ?0)))
+    (do ((num number (truncate num 2)))
+        ((= num 0))
+      (push (number-to-string (% num 2)) nums))
+    (let ((len (length nums)))
+      (apply #'concat
+             (if (and width (< len width))
+                 (make-string (- width len) fillchar)
+               "")
+             nums))))
+
+(defun evil-numbers/dec-at-pt (amount)
+  "Decrement the number at point by `amount'"
+  (interactive "p*")
+  (evil-numbers/inc-at-pt (- amount)))
+
+(provide evil-numbers)



reply via email to

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