unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Jari Aalto <jari.aalto@cante.net>
To: rms@gnu.org
Cc: 4382@emacsbugs.donarmstrong.com, gse@antisleep.com
Subject: bug#4382: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding C-x r N
Date: Thu, 10 Sep 2009 19:59:25 +0300	[thread overview]
Message-ID: <87hbvasple.fsf@jondo.cante.net> (raw)
In-Reply-To: <E1Mll7Z-0002eL-Lj@fencepost.gnu.org> (Richard Stallman's message of "Thu, 10 Sep 2009 10:59:37 -0400")

[-- Attachment #1: Type: text/plain, Size: 1569 bytes --]

Richard Stallman <rms@gnu.org> writes:

> What does this feature do?
> Where is the text to describe it?

The code implements:

    (define-key ctl-x-r-map "N" 'number-rectangle)

Where C-h f number-rectangle is described as:

    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

Demonstration, initial data:

     Item one
     Item two
     Item three

Put cursor at first line's character, draw region up till last line's
first character. Call M-x number-rectangle:

     First number [1] (default 1): <RET>
     Format: %i <RET>

Result:

     1 Item one
     2 Item two
     3 Item three

It the region would have been longer, say over 10 lines, the padding
would have taken in effect:

     01 Item one
     02 Item two
     ...

Jari

Here is slightly updated patch which corrects the name of the function
to `number-rectangle'.

2009-03-22  Jari Aalto  <jari.aalto@cante.net>

        * rect.el (number-rectangle): 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).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-rect.el-number-rectangle-New-user-key-binding-C.patch --]
[-- Type: text/x-diff, Size: 2625 bytes --]

From cdf6167d8c8975f74a42d943f48b6db99fe9c678 Mon Sep 17 00:00:00 2001
From: Jari Aalto <jari.aalto@cante.net>
Date: Thu, 10 Sep 2009 19:55:38 +0300
Subject: [PATCH] lisp/rect.el: (number-rectangle): 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


  reply	other threads:[~2009-09-10 16:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-09 10:04 bug#4382: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding C-x r N Jari Aalto
2009-09-10 14:59 ` Richard Stallman
2009-09-10 16:59   ` Jari Aalto [this message]
2009-09-11 21:00     ` Richard Stallman
     [not found] ` <handler.4382.B.125249070423585.ack@emacsbugs.donarmstrong.com>
2009-09-12 15:46   ` bug#4382: Acknowledgement ([PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding C-x r N) Jari Aalto
2009-09-16 10:52 ` bug#4382: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding C-x r N Dan Nicolaescu
  -- strict thread matches above, loose matches on Subject: below --
2010-12-03  9:49 bug#7538: [PATCH / wishlist] rect.el: (rectangle-number-lines) New user command " Jari Aalto
2010-12-15  2:56 ` bug#4382: [PATCH] lisp/rect.el: (rectangle-number-lines): New user key binding " Chong Yidong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hbvasple.fsf@jondo.cante.net \
    --to=jari.aalto@cante.net \
    --cc=4382@emacsbugs.donarmstrong.com \
    --cc=gse@antisleep.com \
    --cc=rms@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).