bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#21648: 25.0.50; [PATCH] Add ability to specify radix for the yanked


From: Kaushal Modi
Subject: bug#21648: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank
Date: Thu, 8 Oct 2015 11:07:38 -0400

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 <kaushal.modi@gmail.com>
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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]