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.
This commit is contained in:
Jari Aalto 2010-12-15 10:56:22 +08:00 committed by Chong Yidong
parent 5614fd5608
commit 99f053cfb9
3 changed files with 51 additions and 0 deletions

View file

@ -303,6 +303,10 @@ set `x-select-enable-clipboard' to nil.
*** 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

View file

@ -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):

View file

@ -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