branch: externals/shift-number
commit 7313bab013f907ac5ebd19e7951fd485cf3d089b
Author: Alex Kost <[email protected]>
Commit: Alex Kost <[email protected]>
Initial commit
---
.gitignore | 2 +
README.org | 54 +++++++++++++++++++++++++++
shift-number.el | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 167 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000..ff21375871
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+tmp
+*.elc
diff --git a/README.org b/README.org
new file mode 100644
index 0000000000..5091f5ce62
--- /dev/null
+++ b/README.org
@@ -0,0 +1,54 @@
+** About
+
+This Emacs package provides commands to increase and decrease the number
+at point (or the next number on the current line).
+
+[[https://raw.github.com/alezost/shift-number.el/master/demo.gif]]
+
+** Installation
+
+For the manual installation, clone the repo, add the directory to
+=load-path= and add autoloads for the interactive commands:
+
+#+BEGIN_SRC emacs-lisp
+(add-to-list 'load-path "/path/to/shift-number-dir")
+(autoload 'shift-number-up "shift-number" nil t)
+(autoload 'shift-number-down "shift-number" nil t)
+#+END_SRC
+
+** Usage
+
+As you can see in the gif demonstration:
+
+- =M-x shift-number-up= increases the current number. If there is no
+ number at point, the first number between the current position and the
+ end of line is increased. With a numeric prefix ARG, the number is
+ increased for this ARG.
+
+- =M-x shift-number-down= decreases the current number.
+
+You may bind some keys to these commands in a usual manner, for example:
+
+#+BEGIN_SRC emacs-lisp
+(global-set-key (kbd "M-+") 'shift-number-up)
+(global-set-key (kbd "M-_") 'shift-number-down)
+#+END_SRC
+
+** Similar packages
+
+There are other packages for the same task (modifying the number at
+point):
+
+- [[https://github.com/knu/operate-on-number.el][operate-on-number]]
+- [[https://github.com/chrisdone/number][number]]
+
+Comparing with them, =shift-number= has the following distinctions:
+
+- If there is no number at point, it operates on the next number on the
+ current line.
+
+- It is simple: only shifting up/down is available, no multiplication or
+ other more complex stuff.
+
+- It does not prompt for any additional input: you just press a key
+ bound to =shift-number-{up/down}= command and the number is changing.
diff --git a/shift-number.el b/shift-number.el
new file mode 100644
index 0000000000..9029dfa7d2
--- /dev/null
+++ b/shift-number.el
@@ -0,0 +1,111 @@
+;;; shift-number.el --- Increase/decrease the number at point
+
+;; Copyright © 2016 Alex Kost
+
+;; Author: Alex Kost <[email protected]>
+;; Created: 12 Apr 2016
+;; Version: 0.1
+;; URL: https://github.com/alezost/shift-number.el
+;; Keywords: convenience
+
+;; 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:
+
+;; Increase or decrease the number at point with `shift-number-up' and
+;; `shift-number-down' commands.
+
+;; To install the package manually, add the following to your init file:
+;;
+;; (add-to-list 'load-path "/path/to/shift-number-dir")
+;; (autoload 'shift-number-up "shift-number" nil t)
+;; (autoload 'shift-number-down "shift-number" nil t)
+
+;; For more verbose description and a gif demonstration, see
+;; <https://github.com/alezost/shift-number.el>.
+
+;;; Code:
+
+(defgroup shift-number nil
+ "Increase or decrease the number at point."
+ :group 'convenience)
+
+(defcustom shift-number-regexp
+ (rx (group (one-or-more num)))
+ "Regexp for `shift-number' function.
+The first parenthesized expression must match the number."
+ :type 'regexp
+ :group 'shift-number)
+
+(defcustom shift-number-display-message nil
+ "If non-nil, display a message after shifting the current number."
+ :type 'boolean
+ :group 'shift-number)
+
+(defun shift-number-in-regexp-p (regexp)
+ "Return non-nil, if point is inside REGEXP on the current line."
+ ;; The code originates from `org-at-regexp-p'.
+ (save-excursion
+ (let ((pos (point))
+ (end (line-end-position))
+ found exit)
+ (beginning-of-line)
+ (while (and (not (or exit found))
+ (re-search-forward regexp end t))
+ (cond
+ ((> (match-beginning 0) pos)
+ (setq exit t))
+ ((>= (match-end 0) pos)
+ (setq found t))))
+ found)))
+
+(defun shift-number (n)
+ "Change the number at point by N.
+If there is no number at point, search forward till the end of
+the current line and change it."
+ ;; The whole number is removed and a new number is inserted in its
+ ;; place, so `save-excursion' is not used, as it will put the point at
+ ;; the beginning of the number. Instead, the point is saved and
+ ;; restored later.
+ (let ((old-pos (point)))
+ (or (shift-number-in-regexp-p shift-number-regexp)
+ (re-search-forward shift-number-regexp (line-end-position) t)
+ (error "No number on the current line"))
+ (let* ((beg (match-beginning 1))
+ (end (match-end 1))
+ (old-num (string-to-number
+ (buffer-substring-no-properties beg end)))
+ (new-num (+ old-num n)))
+ (delete-region beg end)
+ (insert (number-to-string new-num))
+ (goto-char old-pos)
+ (when shift-number-display-message
+ (message "Number %d has been changed to number %d."
+ old-num new-num)))))
+
+;;;###autoload
+(defun shift-number-up (&optional arg)
+ "Increase the number at point (or on the current line) by ARG."
+ (interactive "p")
+ (shift-number arg))
+
+;;;###autoload
+(defun shift-number-down (&optional arg)
+ "Decrease the number at point (or on the current line) by ARG."
+ (interactive "p")
+ (shift-number (- arg)))
+
+(provide 'shift-number)
+
+;;; shift-number.el ends here