summaryrefslogtreecommitdiff
path: root/lisp/rect.el
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>2010-12-15 10:56:22 +0800
committerChong Yidong <cyd@stupidchicken.com>2010-12-15 10:56:22 +0800
commit99f053cfb94763b5d31bcf0802e4b9233da2bea4 (patch)
treea1edd0b7cae51cc71a25b30157c49f17b2e61a7d /lisp/rect.el
parent5614fd56080d50dc3e1b32382a39d2c21fe060eb (diff)
downloademacs-99f053cfb94763b5d31bcf0802e4b9233da2bea4.tar.gz
New command rectangle-number-lines (Bug#4382).
* rect.el (rectange--default-line-number-format) (rectangle-number-line-callback): New functions. (rectangle-number-lines): New command, bound to C-x r N.
Diffstat (limited to 'lisp/rect.el')
-rw-r--r--lisp/rect.el40
1 files changed, 40 insertions, 0 deletions
diff --git a/lisp/rect.el b/lisp/rect.el
index aedddebe4fa..e24331ebbb7 100644
--- a/lisp/rect.el
+++ b/lisp/rect.el
@@ -38,6 +38,7 @@
;;;###autoload (define-key ctl-x-r-map "y" 'yank-rectangle)
;;;###autoload (define-key ctl-x-r-map "o" 'open-rectangle)
;;;###autoload (define-key ctl-x-r-map "t" 'string-rectangle)
+;;;###autoload (define-key ctl-x-r-map "N" 'rectangle-number-lines)
;;; Code:
@@ -370,6 +371,45 @@ rectangle which were empty."
(delete-region pt (point))
(indent-to endcol)))))
+;; Line numbers for `rectangle-number-line-callback'.
+(defvar rectangle-number-line-counter)
+
+(defun rectangle-number-line-callback (start end format-string)
+ (move-to-column start t)
+ (insert (format format-string rectangle-number-line-counter))
+ (setq rectangle-number-line-counter
+ (1+ rectangle-number-line-counter)))
+
+(defun rectange--default-line-number-format (start end start-at)
+ (concat "%"
+ (int-to-string (length (int-to-string (+ (count-lines start end)
+ start-at))))
+ "d "))
+
+;;;###autoload
+(defun rectangle-number-lines (start end start-at &optional format)
+ "Insert numbers in front of the region-rectangle.
+
+START-AT, if non-nil, should be a number from which to begin
+counting. FORMAT, if non-nil, should be a format string to pass
+to `format' along with the line count. When called interactively
+with a prefix argument, prompt for START-AT and FORMAT."
+ (interactive
+ (if current-prefix-arg
+ (let* ((start (region-beginning))
+ (end (region-end))
+ (start-at (read-number "Number to count from: " 1)))
+ (list start end start-at
+ (read-string "Format string: "
+ (rectange--default-line-number-format
+ start end start-at))))
+ (list (region-beginning) (region-end) 1 nil)))
+ (unless format
+ (setq format (rectange--default-line-number-format start end start-at)))
+ (let ((rectangle-number-line-counter start-at))
+ (apply-on-rectangle 'rectangle-number-line-callback
+ start end format)))
+
(provide 'rect)
;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16