branch: externals/shift-number
commit 643a9316384eb4f606895e2565b2263e2a170626
Author: Alex Kost <[email protected]>
Commit: Alex Kost <[email protected]>

    Preserve leading zeros
    
    * shift-number.el (shift-number): Restore leading zeros if they existed
      in the original number.
    * README.org: Mention this feature.
---
 README.org      |  3 +++
 shift-number.el | 26 +++++++++++++++++---------
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/README.org b/README.org
index 93addad84a..91d7337eae 100644
--- a/README.org
+++ b/README.org
@@ -61,6 +61,9 @@ Comparing with them, =shift-number= has the following 
distinctions:
 
 - The point does not move anywhere when a number is modified.
 
+- If a number has leading zeros (for example =007=), they are preserved
+  during shifting.
+
 - It is simple: only shifting up/down is available, no multiplication or
   other more complex stuff.
 
diff --git a/shift-number.el b/shift-number.el
index 9029dfa7d2..34a2861227 100644
--- a/shift-number.el
+++ b/shift-number.el
@@ -1,6 +1,6 @@
 ;;; shift-number.el --- Increase/decrease the number at point
 
-;; Copyright © 2016 Alex Kost
+;; Copyright © 2016–2017 Alex Kost
 
 ;; Author: Alex Kost <[email protected]>
 ;; Created: 12 Apr 2016
@@ -12,12 +12,12 @@
 ;; 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/>.
 
@@ -82,13 +82,21 @@ the current line and change it."
     (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)))
+    (let* ((beg         (match-beginning 1))
+           (end         (match-end       1))
+           (old-num-str (buffer-substring-no-properties beg end))
+           (old-num     (string-to-number old-num-str))
+           (new-num     (+ old-num n))
+           (new-num-str (number-to-string new-num)))
       (delete-region beg end)
-      (insert (number-to-string new-num))
+      ;; If there are leading zeros, preserve them keeping the same
+      ;; length of the original number.
+      (when (string-match-p "\\`0" old-num-str)
+        (let ((len-diff (- (length old-num-str)
+                           (length new-num-str))))
+          (when (> len-diff 0)
+            (insert (make-string len-diff ?0)))))
+      (insert new-num-str)
       (goto-char old-pos)
       (when shift-number-display-message
         (message "Number %d has been changed to number %d."

Reply via email to