Skip to content

Instantly share code, notes, and snippets.

@amotoki
Created September 29, 2013 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amotoki/6753733 to your computer and use it in GitHub Desktop.
Save amotoki/6753733 to your computer and use it in GitHub Desktop.
Insert a decimal or hex incremental/decremental number into a region
;; Copyright (C) 2013 Akihiro Motoki <amotoki@gmail.com>
;; Author: Akihiro Motoki <amotoki@gmail.com>
;; Keywords: convenience rectangle
;; This file 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, or (at your option)
;; any later version.
;; This file 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., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Installation
;; (require 'rectangle)
;; (global-set-key "\C-xrp" 'replace-rectangle)
;; (global-set-key "\C-xrn" 'insert-number-rectangle)
;;; Code:
(defun replace-rectangle (&optional start end string)
(interactive "r\nsReplace To: ")
(save-excursion
(let ((region-min (min start end))
(num-of-lines (count-lines start end)))
(kill-rectangle start end)
(string-rectangle region-min
(progn
(goto-char region-min)
(next-line (1- num-of-lines))
(point))
string))))
(defun insert-number-rectangle (&optional start end number)
"Insert incremental number into each left edges of rectangle's line"
(interactive "r\nnFirst Number: ")
(save-excursion
(let* ((incp (= (point) end))
(end-marker (set-mark (if incp end start))))
(goto-char (if incp start end))
(string-rectangle start end "")
(while (if incp (<= (point) (marker-position end-marker))
(>= (point) (marker-position end-marker)))
(insert (int-to-string number))
(backward-char (length (int-to-string number)))
(setq number (1+ number))
(next-line (if incp 1 -1))))))
(defun insert-hex-rectangle (start end number width &optional reverse)
"Insert incremental hexadecimal number into each left edges of rectangle's line"
(interactive "r\nnFirst Number: \nnWidth: \nP")
(save-excursion
(let* ((incp (= (point) end))
(end-marker (set-mark (if incp end start)))
(fmt (format "%%0%dX" width)))
(goto-char (if incp start end))
(string-rectangle start end "")
(while (if incp (<= (point) (marker-position end-marker))
(>= (point) (marker-position end-marker)))
(insert (format fmt number))
(backward-char (length (format fmt number)))
(setq number (if reverse (1- number) (1+ number)))
(next-line (if incp 1 -1))
))))
(defun upcase-rectangle (beg end &optional lower)
"Convert string in the rectangle to upper case.
If lower is t, convert string to lower case."
(interactive "r")
(let (cb ce rb re func)
(if lower
(setq func 'downcase-region)
(setq func 'upcase-region))
(save-excursion
(goto-char end)
(setq ce (current-column))
(goto-char beg)
(setq cb (current-column))
(while (< (point) end)
(move-to-column cb)
(setq rb (point))
(move-to-column ce)
(funcall func rb (point))
;;(upcase-region rb (point))
(forward-line 1)))))
(defun downcase-rectangle (beg end)
(interactive "r")
(upcase-rectangle beg end t))
(provide 'rectangle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment