[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#4382: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key b
From: |
Jari Aalto |
Subject: |
bug#4382: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding C-x r N |
Date: |
Wed, 09 Sep 2009 13:04:57 +0300 |
Scott has just informaed me that his FSF assignment papers have been
processed and cleared. Based on his work at
http://www.antisleep.com/elisp/gse-number-rect.el I propose following
new feature to be added to the rect.el package.
2009-03-22 Jari Aalto <jari.aalto@cante.net>
* rect.el (rectangle-number-lines): New user function.
(rectangle-number-line-callback): New function.
(rectangle-number-line-format-history): New variable.
(rectangle-number-line-counter): New variable.
(define-key ctl-x-r-map "N" 'number-rectangle): New
user key binding. The basis of the rectangle numbering code was
lifted from gse-number-rect.el, orignally developed by Scott Evans
<gse@antisleep.com>. Used by permission (GPL).
>From 3ea550b85ded33fae27305bca8710e8f7ce0893b Mon Sep 17 00:00:00 2001
From: Jari Aalto <jari.aalto@cante.net>
Date: Sun, 22 Mar 2009 17:27:42 +0200
Subject: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding
C-x r N
Signed-off-by: Jari Aalto <jari.aalto@cante.net>
---
lisp/rect.el | 40 ++++++++++++++++++++++++++++++++++++++++
1 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/lisp/rect.el b/lisp/rect.el
index eb188fc..6b63c81 100644
--- a/lisp/rect.el
+++ b/lisp/rect.el
@@ -39,6 +39,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" 'number-rectangle)
;;; Code:
@@ -395,6 +396,45 @@ rectangle which were empty."
(delete-region pt (point))
(indent-to endcol)))))
+(defvar rectangle-number-line-counter nil
+ "Variable holding line numbering information.
+Initial value is set in function `rectangle-number-lines'
+and incremented for each line in `rectangle-number-line-callback'.")
+
+(defvar rectangle-number-line-format-history nil
+ "History variable that records previous format strings.")
+
+(defun rectangle-number-line-callback (start end format-string)
+ (move-to-column start t)
+ (setq rectangle-number-line-counter (+ rectangle-number-line-counter 1))
+ (insert (format format-string rectangle-number-line-counter)))
+
+(defun rectangle-number-lines (beg end start-at format &optional
no-zero-padding)
+ "Insert numbers in front of lines in rectangle BEG END.
+
+START-AT specifices the first number to start at.
+FORMAT is format string where %i denotes inserted number.
+
+If prefix arg NO-ZERO-PADDING is non-nil, do not padd numbers
+with leading zeroes."
+ (interactive
+ (list
+ (region-beginning)
+ (region-end)
+ (if (functionp 'read-number)
+ (read-number "First number [1]: " 1)
+ (string-to-int (read-string "First number [1]: " nil nil "1")))
+ (read-string "Format: " "%i " 'rectangle-number-line-format-history)
+ current-prefix-arg))
+ (setq start-at (- start-at 1))
+ (unless no-zero-padding
+ (let* ((max (+ (count-lines beg end) start-at))
+ (longest (length (int-to-string (+ 1 max))))
+ (fmt (concat "%0" (int-to-string longest) "i")))
+ (setq format (replace-regexp-in-string "%i" fmt format))))
+ (setq rectangle-number-line-counter start-at)
+ (apply-on-rectangle 'rectangle-number-line-callback beg end format))
+
(provide 'rect)
;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
--
1.6.3.3