Subject: bug#21648: closed (Re: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank)

From: [email-masked] (GNU bug Tracking System)
Date: Mon, 12 Oct 2015 13:17:03 +0000

Your bug report

#21648: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
21648: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=21648
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank
From: Kaushal Modi <[email-masked]>
To: [email-masked], [email-masked]
Cc: Eli Zaretskii <[email-masked]>
Date: Mon, 12 Oct 2015 13:16:07 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Closing this debbugs as the patch is now commited to the trunk:
http://git.savannah.gnu.org/cgit/emacs.git/commit/?id=ec0d4d24fd11b5040de9f7657b486c3b1e743071

Many thanks to Jay for a very active feedback session, helping make the
final patch much more robust than the initial version.

Subject: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank
From: Kaushal Modi <[email-masked]>
To: "[email-masked]" <[email-masked]>, [email-masked]
Date: Thu, 8 Oct 2015 11:07:38 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

I read this question of emacs.stackexchange (
http://emacs.stackexchange.com/q/13451/115 ) where the user needed to
specify the radix of the number he was pasting in calc.

If the calc default radix is decimal and if a user pastes 1000, it will be
pasted as decimal 1000. But what if the user meant to paste binary 1000
(decimal 8)?

My patch below enables doing that using numeric prefixes.

Please advise if merging this patch to calc-yank is a good idea or if needs
improvement/bug fixes before the merging. I have used this modified
calc-yank function for few weeks and did not mess up anything else in my
calc usage.

======================================================

>From fa9c8f6c2cce1c9e69bd6f6c25f036a72e0b799b Mon Sep 17 00:00:00 2001
From: Kaushal Modi <[email-masked]>
Date: Thu, 8 Oct 2015 10:56:36 -0400
Subject: [PATCH] Prepend radix to yanked numbers using num prefix

If 1000 is the last element saved to the kill-ring.

Then,

C-2 C-y will paste 8 (2#1000),
C-8 C-y will paste 512 (8#1000),
C-0 C-y will paste 1000 (10#1000),
C-6 C-y will paste 4096 (16#1000)
.. and C-y will paste 1000 (1000).
---
 lisp/calc/calc-yank.el | 45 ++++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/lisp/calc/calc-yank.el b/lisp/calc/calc-yank.el
index 5694a4e..811b308 100644
--- a/lisp/calc/calc-yank.el
+++ b/lisp/calc/calc-yank.el
@@ -111,25 +111,40 @@ calc-copy-region-as-kill
 ;; otherwise it just parses the yanked string.
 ;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg
12/15/96
 ;;;###autoload
-(defun calc-yank ()
-  (interactive)
+(defun calc-yank (radix)
+  "Yank a value into the Calculator buffer.
+
+If RADIX is nil, do not prepend any radix notation.
+
+If RADIX is 2, prepend \"2#\" (The yanked number will be perceived as
binary.)
+If RADIX is 8, prepend \"8#\" (The yanked number will be perceived as
octal.)
+If RADIX is 0, prepend \"10#\" (The yanked number will be perceived as
decimal.)
+If RADIX is 16, prepend \"16#\" (The yanked number will be perceived as
hexadecimal.) "
+  (interactive "P")
   (calc-wrapper
    (calc-pop-push-record-list
     0 "yank"
-    (let ((thing (if (fboundp 'current-kill)
-     (current-kill 0 t)
-   (car kill-ring-yank-pointer))))
+    (let* ((radix-notation (cl-case radix
+                             (2 "2#")
+                             (8 "8#")
+                             (0 "10#")
+                             (6 "16#")
+                             (t "")))
+           (thing (concat radix-notation
+                          (if (fboundp 'current-kill)
+                              (current-kill 0 t)
+                            (car kill-ring-yank-pointer)))))
       (if (eq (car-safe calc-last-kill) thing)
-  (cdr calc-last-kill)
- (if (stringp thing)
-    (let ((val (math-read-exprs (calc-clean-newlines thing))))
-      (if (eq (car-safe val) 'error)
-  (progn
-    (setq val (math-read-exprs thing))
-    (if (eq (car-safe val) 'error)
- (error "Bad format in yanked data")
-      val))
- val))))))))
+          (cdr calc-last-kill)
+        (if (stringp thing)
+            (let ((val (math-read-exprs (calc-clean-newlines thing))))
+              (if (eq (car-safe val) 'error)
+                  (progn
+                    (setq val (math-read-exprs thing))
+                    (if (eq (car-safe val) 'error)
+                        (error "Bad format in yanked data")
+                      val))
+                val))))))))

 ;;; The Calc set- and get-register commands are modified versions of
functions
 ;;; in register.el
-- 
2.6.0.rc0.24.gec371ff

======================================================

PS: My copyright paperwork is on file (#1029578)

--
Kaushal Modi