Subject: bug#8898: add a function to change button label

From: Dmitry Kurochkin <[email-masked]>
Date: Mon, 20 Jun 2011 00:26:43 +0400

Package: emacs
Version: 24.0.50
Tags: patch

In some cases it is very useful to change button text label.  Please
consider applying the attached patch that implements `button-set-label'
function to change button label.

Regards,
  Dmitry
Add `button-set-label' function to change button label.

=== modified file 'doc/lispref/display.texi'
--- doc/lispref/display.texi	2011-05-29 22:41:06 +0000
+++ doc/lispref/display.texi	2011-06-19 20:04:46 +0000
@@ -5046,40 +5046,44 @@ Return the position at which @var{button
 
 @defun button-get button prop
 Get the property of button @var{button} named @var{prop}.
 @end defun
 
 @defun button-put button prop val
 Set @var{button}'s @var{prop} property to @var{val}.
 @end defun
 
 @defun button-activate button &optional use-mouse-action
 Call @var{button}'s @code{action} property (i.e., invoke it).  If
 @var{use-mouse-action} is non-@code{nil}, try to invoke the button's
 @code{mouse-action} property instead of @code{action}; if the button
 has no @code{mouse-action} property, use @code{action} as normal.
 @end defun
 
 @defun button-label button
 Return @var{button}'s text label.
 @end defun
 
+@defun button-set-label button label
+Change @var{button}'s text label to @var{label}.
+@end defun
+
 @defun button-type button
 Return @var{button}'s button-type.
 @end defun
 
 @defun button-has-type-p button type
 Return @code{t} if @var{button} has button-type @var{type}, or one of
 @var{type}'s subtypes.
 @end defun
 
 @defun button-at pos
 Return the button at position @var{pos} in the current buffer, or @code{nil}.
 @end defun
 
 @defun button-type-put type prop val
 Set the button-type @var{type}'s @var{prop} property to @var{val}.
 @end defun
 
 @defun button-type-get type prop
 Get the property of button-type @var{type} named @var{prop}.
 @end defun

=== modified file 'lisp/button.el'
--- lisp/button.el	2011-01-25 04:08:28 +0000
+++ lisp/button.el	2011-06-19 20:08:33 +0000
@@ -216,40 +216,52 @@ changes to a supertype are not reflected
      prop val)))
 
 (defsubst button-activate (button &optional use-mouse-action)
   "Call BUTTON's action property.
 If USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
 instead of its normal action; if the button has no mouse-action,
 the normal action is used instead."
   (let ((action (or (and use-mouse-action (button-get button 'mouse-action))
 		    (button-get button 'action))))
     (if (markerp action)
 	(save-selected-window
 	  (select-window (display-buffer (marker-buffer action)))
 	  (goto-char action)
 	  (recenter 0))
       (funcall action button))))
 
 (defun button-label (button)
   "Return BUTTON's text label."
   (buffer-substring-no-properties (button-start button) (button-end button)))
 
+(defun button-set-label (button label)
+  "Change BUTTON's text label to LABEL."
+  (save-excursion
+    (let ((old-start (button-start button))
+          (old-end (button-end button)))
+      (goto-char old-end)
+      (insert label)
+      (if (overlayp button)
+          (move-overlay button old-end (point))
+        (add-text-properties old-end (point) (text-properties-at button)))
+      (delete-region old-start old-end))))
+
 (defsubst button-type (button)
   "Return BUTTON's button-type."
   (button-get button 'type))
 
 (defun button-has-type-p (button type)
   "Return t if BUTTON has button-type TYPE, or one of TYPE's subtypes."
   (button-type-subtype-p (button-get button 'type) type))
 
 
 ;; Creating overlay buttons
 
 (defun make-button (beg end &rest properties)
   "Make a button from BEG to END in the current buffer.
 The remaining arguments form a sequence of PROPERTY VALUE pairs,
 specifying properties to add to the button.
 In addition, the keyword argument :type may be used to specify a
 button-type from which to inherit other properties; see
 `define-button-type'.
 
 Also see `make-text-button', `insert-button'."