Subject: bug#12217: [PATCH] Re: 24.2.50; append-to-register: Provide a convenient key binding

From: Jambunathan K <[email-masked]>
Date: Sat, 08 Sep 2012 23:56:20 +0530

Attaching modified patch.  Patch is generated against revno: 109935.

>> Once patch goes through, I will submit a second part which updates
>> regs.texi.
>
> I think the change is OK.  I would prefer a single `cond' form with the
> `error' in the last branch (instead of unless+if) so as to avoid
> a redundant `numberp' check (not as a matter of efficiency, of course,
> but maintenance).
> If you send such an updated patch with the corresponding regs.texi
> change, we'll install it.

Done.

>> I usually to use M-x append-to-register (and with this patch `C-x r +')
>> to collect sexps.  I think it will be convenient to have the collected
>> snippets separated by a suitable separator. Is it OK if a default "\n"
>> be used? WDYT.
>
> I think I'd rather not add any separator by default.  

> In many cases adding such a separator would render the command
> inconvenient, and in many others the user can easily make sure the
> text he appends includes an appropriate separator.

> I can see cases where adding a separator could be handy, but I'm not
> sure hardcoding "\n" would solve enough of those cases.  

> Maybe you could prompt for a separator if the user provided a C-u
> prefix?

Prefix is used as a `delete-flag' already ...

I have introduced an extra indirection via a `separator-register' (which
is nil by default) Now what gets used as a separator - a newline, a
double newline, a comma or a tab - is under user control.

There is already one user - that is me - who will find this feature
useful.

=== modified file 'doc/emacs/ChangeLog'
--- doc/emacs/ChangeLog	2012-09-07 10:27:11 +0000
+++ doc/emacs/ChangeLog	2012-09-08 18:04:28 +0000
@@ -1,3 +1,9 @@
+2012-09-08  Jambunathan K  <[email-masked]>
+
+	* regs.texi (Text Registers): `C-x r +' can now be used instead of
+	M-x append-to-register.  New option `separator-register'.
+	(Number Registers): Mention that `C-x r +' is polymorphic.
+
 2012-09-07  Chong Yidong  <[email-masked]>
 
 	* windows.texi (Window Choice): Don't mention obsolete

=== modified file 'doc/emacs/regs.texi'
--- doc/emacs/regs.texi	2012-05-27 01:25:06 +0000
+++ doc/emacs/regs.texi	2012-09-08 17:14:13 +0000
@@ -92,6 +92,13 @@
 Insert text from register @var{r} (@code{insert-register}).
 @item M-x append-to-register @key{RET} @var{r}
 Append region to text in register @var{r}.
+
+@kindex C-x r +
+When register @var{r} contains text, you can use @kbd{C-x r +}
+(@code{increment-register}) to append to that register.  Note that
+command @kbd{C-x r +} behaves differently if @var{r} contains a
+number.  @xref{Number Registers}.
+
 @item M-x prepend-to-register @key{RET} @var{r}
 Prepend region to text in register @var{r}.
 @end table
@@ -116,6 +123,19 @@
 the region text to the text in the register instead of
 @emph{appending} it.
 
+@vindex separator-register
+  When you are collecting text using @code{append-to-register} and
+@code{prepend-to-register}, you may want to separate individual
+collected pieces using a separator.  In that case, configure a
+@code{separator-register} and store the separator text in to that
+register.  For example, to get double newlines as text separator
+during the collection process, you can use the following setting.
+
+@example
+(setq separator-register ?+)
+(set-register separator-register "\n\n")
+@end example
+
 @kindex C-x r i
 @findex insert-register
   @kbd{C-x r i @var{r}} inserts in the buffer the text from register
@@ -191,8 +211,10 @@
 @item C-u @var{number} C-x r + @var{r}
 @kindex C-x r +
 @findex increment-register
-Increment the number in register @var{r} by @var{number}
-(@code{increment-register}).
+If @var{r} contains a number, increment the number in that register by
+@var{number}.  Note that command @kbd{C-x r +}
+(@code{increment-register}) behaves differently if @var{r} contains
+text.  @xref{Text Registers}.
 @item C-x r i @var{r}
 Insert the number from register @var{r} into the buffer.
 @end table

=== modified file 'etc/NEWS'
--- etc/NEWS	2012-09-07 10:27:11 +0000
+++ etc/NEWS	2012-09-08 17:31:09 +0000
@@ -182,6 +182,13 @@
 delete-trailing-whitespace command should delete trailing lines at the
 end of the buffer.  It defaults to t.
 
++++
+** `C-x r +' is now overloaded to invoke `append-to-register.
++++
+** New option `separator-register'.  Separator register stores
+separator text for use with M-x append-to-register and M-x
+prepend-to-register.  See manual for details.
+
 ** Search changes
 
 *** Global `M-s _' starts a symbol (identifier) incremental search,

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2012-09-07 20:14:55 +0000
+++ lisp/ChangeLog	2012-09-08 17:39:03 +0000
@@ -1,3 +1,13 @@
+2012-09-08  Jambunathan K  <[email-masked]>
+
+	* register.el (register): New group.
+	(separator-register): New user option.
+	(increment-register): Route it to `append-to-register', if
+	register contains text.  Implication is that `C-x r +' can now be
+	used for appending to a text register.
+	(append-to-register, prepend-to-register): Add separator based on
+	`separator-register.
+
 2012-09-07  Stefan Monnier  <[email-masked]>
 
 	* emacs-lisp/byte-run.el (defun): Tweak message.  Simplify code.

=== modified file 'lisp/register.el'
--- lisp/register.el	2012-07-29 04:45:48 +0000
+++ lisp/register.el	2012-09-08 14:12:42 +0000
@@ -76,6 +76,22 @@
 A list of the form (FRAME-CONFIGURATION POSITION)
  represents a saved frame configuration plus a saved value of point.")
 
+(defgroup register nil
+  "Register commands."
+  :group 'convenience
+  :version "24.2.50")
+
+(defcustom separator-register nil
+  "Use contents of this register to separate collected text.
+
+When collecting text with
+`append-to-register' (resp. `prepend-to-register') contents of
+this register is added to the beginning (resp. end) of the marked
+text."
+  :group 'register
+  :type '(choice (const :tag "None" nil)
+		 (character :tag "Use register" :value ?+)))
+
 (defun get-register (register)
   "Return contents of Emacs register named REGISTER, or nil if none."
   (cdr (assq register register-alist)))
@@ -192,13 +208,24 @@
 			(string-to-number (match-string 0)))
 		    0))))
 
-(defun increment-register (number register)
-  "Add NUMBER to the contents of register REGISTER.
-Interactively, NUMBER is the prefix arg."
-  (interactive "p\ncIncrement register: ")
-  (or (numberp (get-register register))
-      (error "Register does not contain a number"))
-  (set-register register (+ number (get-register register))))
+(defun increment-register (prefix register)
+  "Augment contents of REGISTER.
+Interactively, PREFIX is in raw form.
+
+If REGISTER contains a number, add `prefix-numeric-value' of
+PREFIX to it.
+
+If REGISTER is empty or if it contains text, call
+`append-to-register' with `delete-flag' set to PREFIX."
+  (interactive "P\ncIncrement register: ")
+  (let ((register-val (get-register register)))
+    (cond
+     ((numberp register-val)
+      (let ((number (prefix-numeric-value prefix)))
+	(set-register register (+ number register-val))))
+     ((or (not register-val) (stringp register-val))
+      (append-to-register register (region-beginning) (region-end) prefix))
+     (t (error "Register does not contain a number or text")))))
 
 (defun view-register (register)
   "Display what is contained in register named REGISTER.
@@ -349,10 +376,11 @@
 START and END are buffer positions indicating what to append."
   (interactive "cAppend to register: \nr\nP")
   (let ((reg (get-register register))
-        (text (filter-buffer-substring start end)))
+        (text (filter-buffer-substring start end))
+	(separator (and separator-register (get-register separator-register))))
     (set-register
      register (cond ((not reg) text)
-                    ((stringp reg) (concat reg text))
+                    ((stringp reg) (concat reg separator text))
                     (t (error "Register does not contain text")))))
   (cond (delete-flag
 	 (delete-region start end))
@@ -366,10 +394,11 @@
 START and END are buffer positions indicating what to prepend."
   (interactive "cPrepend to register: \nr\nP")
   (let ((reg (get-register register))
-        (text (filter-buffer-substring start end)))
+        (text (filter-buffer-substring start end))
+	(separator (and separator-register (get-register separator-register))))
     (set-register
      register (cond ((not reg) text)
-                    ((stringp reg) (concat text reg))
+                    ((stringp reg) (concat text separator reg))
                     (t (error "Register does not contain text")))))
   (cond (delete-flag
 	 (delete-region start end))