changeset 111970:9570b2539308

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.
author Chong Yidong <cyd@stupidchicken.com>
date Wed, 15 Dec 2010 10:56:22 +0800
parents 0889087d741a
children ca22802893d9
files etc/NEWS lisp/ChangeLog lisp/rect.el
diffstat 3 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/etc/NEWS	Wed Dec 15 10:05:38 2010 +0800
+++ b/etc/NEWS	Wed Dec 15 10:56:22 2010 +0800
@@ -303,6 +303,10 @@
 
 *** Support for X cut buffers has been removed.
 
+** New command `rectangle-number-lines', bound to `C-x r N', numbers
+the lines in the current rectangle.  With an prefix argument, this
+prompts for a number to count from and for a format string.
+
 
 * Changes in Specialized Modes and Packages in Emacs 24.1
 
--- a/lisp/ChangeLog	Wed Dec 15 10:05:38 2010 +0800
+++ b/lisp/ChangeLog	Wed Dec 15 10:56:22 2010 +0800
@@ -1,3 +1,10 @@
+2010-12-15  Jari Aalto  <jari.aalto@cante.net>
+            Scott Evans <gse@antisleep.com>
+
+	* rect.el (rectange--default-line-number-format)
+	(rectangle-number-line-callback): New functions.
+	(rectangle-number-lines): New command, bound to C-x r N (Bug#4382).
+
 2010-12-15  Chong Yidong  <cyd@stupidchicken.com>
 
 	* rect.el (operate-on-rectangle-lines, string-rectangle-string):
--- a/lisp/rect.el	Wed Dec 15 10:05:38 2010 +0800
+++ b/lisp/rect.el	Wed Dec 15 10:56:22 2010 +0800
@@ -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 @@
 	(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