aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaushal Modi2015-10-10 18:36:51 -0500
committerJay Belanger2015-10-10 18:36:51 -0500
commitec0d4d24fd11b5040de9f7657b486c3b1e743071 (patch)
treebcda0e92e9ef618205b4ddd77e224957bffec5be
parent89f2c79868e7bcc2fc5436796f063d1e903dea41 (diff)
downloademacs-ec0d4d24fd11b5040de9f7657b486c3b1e743071.tar.gz
emacs-ec0d4d24fd11b5040de9f7657b486c3b1e743071.zip
Allow numbers with different radixes to be yanked.
* lisp/calc/calc-yank.el (calc-yank): Allow radixes besides the default base 10.
-rw-r--r--lisp/calc/calc-prog.el4
-rw-r--r--lisp/calc/calc-yank.el106
2 files changed, 93 insertions, 17 deletions
diff --git a/lisp/calc/calc-prog.el b/lisp/calc/calc-prog.el
index c5a837d3260..8d97bc69a2d 100644
--- a/lisp/calc/calc-prog.el
+++ b/lisp/calc/calc-prog.el
@@ -1287,7 +1287,7 @@ Redefine the corresponding command."
1287 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000)) 1287 (setq rpt-count (if rpt-count (prefix-numeric-value rpt-count) 1000000))
1288 (let* ((count 0) 1288 (let* ((count 0)
1289 (parts nil) 1289 (parts nil)
1290 (body (vector) ) 1290 (body (vector))
1291 (open last-command-event) 1291 (open last-command-event)
1292 (counter initial) 1292 (counter initial)
1293 ch) 1293 ch)
@@ -1300,7 +1300,7 @@ Redefine the corresponding command."
1300 (if (eq ch ?Z) 1300 (if (eq ch ?Z)
1301 (progn 1301 (progn
1302 (setq ch (read-event) 1302 (setq ch (read-event)
1303 body (vconcat body (vector ?Z ch) )) 1303 body (vconcat body (vector ?Z ch)))
1304 (cond ((memq ch '(?\< ?\( ?\{)) 1304 (cond ((memq ch '(?\< ?\( ?\{))
1305 (setq count (1+ count))) 1305 (setq count (1+ count)))
1306 ((memq ch '(?\> ?\) ?\})) 1306 ((memq ch '(?\> ?\) ?\}))
diff --git a/lisp/calc/calc-yank.el b/lisp/calc/calc-yank.el
index 5694a4e56ae..c93b64b6436 100644
--- a/lisp/calc/calc-yank.el
+++ b/lisp/calc/calc-yank.el
@@ -111,25 +111,101 @@
111;; otherwise it just parses the yanked string. 111;; otherwise it just parses the yanked string.
112;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96 112;; Modified to use Emacs 19 extended concept of kill-ring. -- daveg 12/15/96
113;;;###autoload 113;;;###autoload
114(defun calc-yank () 114(defun calc-yank (radix)
115 (interactive) 115 "Yank a value into the Calculator buffer.
116
117Valid numeric prefixes for RADIX: 0, 2, 6, 8
118No radix notation is prepended for any other numeric prefix.
119
120If RADIX is 2, prepend \"2#\" - Binary.
121If RADIX is 8, prepend \"8#\" - Octal.
122If RADIX is 0, prepend \"10#\" - Decimal.
123If RADIX is 6, prepend \"16#\" - Hexadecimal.
124
125If RADIX is a non-nil list (created using \\[universal-argument]), the user
126will be prompted to enter the radix in the minibuffer.
127
128If RADIX is nil or if the yanked string already has a calc radix prefix, the
129yanked string will be passed on directly to the Calculator buffer without any
130alteration."
131 (interactive "P")
116 (calc-wrapper 132 (calc-wrapper
117 (calc-pop-push-record-list 133 (calc-pop-push-record-list
118 0 "yank" 134 0 "yank"
119 (let ((thing (if (fboundp 'current-kill) 135 (let* (radix-num
120 (current-kill 0 t) 136 radix-notation
121 (car kill-ring-yank-pointer)))) 137 valid-num-regexp
138 (thing-raw
139 (if (fboundp 'current-kill)
140 (current-kill 0 t)
141 (car kill-ring-yank-pointer)))
142 (thing
143 (if (or (null radix)
144 ;; Match examples: -2#10, 10\n(10#10,01)
145 (string-match-p "^[-(]*[0-9]\\{1,2\\}#" thing-raw))
146 thing-raw
147 (progn
148 (if (listp radix)
149 (progn
150 (setq radix-num
151 (read-number
152 "Set radix for yanked content (2-36): "))
153 (when (not (and (integerp radix-num)
154 (<= 2 radix-num)
155 (>= 36 radix-num)))
156 (error (concat "The radix has to be an "
157 "integer between 2 and 36."))))
158 (setq radix-num
159 (cond ((eq radix 2) 2)
160 ((eq radix 8) 8)
161 ((eq radix 0) 10)
162 ((eq radix 6) 16)
163 (t (message
164 (concat "No radix prepended "
165 "for invalid *numeric* "
166 "prefix %0d.")
167 radix)
168 nil))))
169 (if radix-num
170 (progn
171 (setq radix-notation
172 (concat (number-to-string radix-num) "#"))
173 (setq valid-num-regexp
174 (cond
175 ;; radix 2 to 10
176 ((and (<= 2 radix-num)
177 (>= 10 radix-num))
178 (concat "[0-"
179 (number-to-string (1- radix-num))
180 "]+"))
181 ;; radix 11
182 ((= 11 radix-num) "[0-9aA]+")
183 ;; radix 12+
184 (t
185 (concat "[0-9"
186 "a-" (format "%c" (+ (- ?a 11) radix-num))
187 "A-" (format "%c" (+ (- ?A 11) radix-num))
188 "]+"))))
189 ;; Ensure that the radix-notation is prefixed
190 ;; correctly even for multi-line yanks like below,
191 ;; 111
192 ;; 1111
193 (replace-regexp-in-string
194 valid-num-regexp
195 (concat radix-notation "\\&")
196 thing-raw))
197 thing-raw)))))
122 (if (eq (car-safe calc-last-kill) thing) 198 (if (eq (car-safe calc-last-kill) thing)
123 (cdr calc-last-kill) 199 (cdr calc-last-kill)
124 (if (stringp thing) 200 (if (stringp thing)
125 (let ((val (math-read-exprs (calc-clean-newlines thing)))) 201 (let ((val (math-read-exprs (calc-clean-newlines thing))))
126 (if (eq (car-safe val) 'error) 202 (if (eq (car-safe val) 'error)
127 (progn 203 (progn
128 (setq val (math-read-exprs thing)) 204 (setq val (math-read-exprs thing))
129 (if (eq (car-safe val) 'error) 205 (if (eq (car-safe val) 'error)
130 (error "Bad format in yanked data") 206 (error "Bad format in yanked data")
131 val)) 207 val))
132 val)))))))) 208 val))))))))
133 209
134;;; The Calc set- and get-register commands are modified versions of functions 210;;; The Calc set- and get-register commands are modified versions of functions
135;;; in register.el 211;;; in register.el