all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kaushal Modi <kaushal.modi@gmail.com>
To: Jay Belanger <jay.p.belanger@gmail.com>
Cc: 21648@debbugs.gnu.org
Subject: bug#21648: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank
Date: Thu, 8 Oct 2015 16:35:26 -0400	[thread overview]
Message-ID: <CAFyQvY1UudfFMN7XzOngTQJwJRND77YgaruTDK6Ui2eKOevCJQ@mail.gmail.com> (raw)
In-Reply-To: <87wpux88mo.fsf@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 7643 bytes --]

Hi all,

Here the the new implementation of calc-yank. I can submit a PATCH once the
new definition is confirmed.

> If the copied number has a calc-style prefix, then the number should be
> interpreted in that base.  I can be entered onto the stack in the new
> base.  That's done now; if Calc is using base 5 and 2#111 is yanked,
> then 5#12 appears on the stack.

It does that now
​. I also tested with the 2#111 and few other examples.

​

​> ​
You'd probably want Calc to do the conversion.

​Correct, I am not doing any string manipulation.. I simply don't handle
the string if it begins with "\\`[0-9]+#".​

​> ​
Your method is a good approach.

​Thanks. I have not changed the way ​it works right now. Using the same
example of yanking "1000" in a calc buffer with radix set to hex ("d6"),
doing C-y will paste 16#3E8. But with the new calc-yank, C-6 C-y will paste
16#1000.

​> ​
Yes; it sounds like a nice approach.

​Done! I have implemented C-u C-y functionality which will prompt user to
enter radix between 2 and 36.​

​> ​
And perhaps some sort of message
​> ​
if the entered radix isn't a number from 2 to 36 (the allowed radixes).

Is that check needed in calc-yank? If I enter an invalid radix like 37, I
get the generic error "Bad format in yanked data".​

> ​Also, you probably want to make sure that calc-yank behaves
​> ​
appropriately when a number isn't being yanked.
​> ​
(Either applying the new base to all numbers in an expression or
​> ​
disregarding the radix when the expression being yanked isn't a pure
> number, for example.)

Ah! I wasn't aware that yanking non-numbers was possible!
Based on the current code, as long as the user does not provide ANY prefix,
calc-yank will work exactly as before. So the non-number yanking should
also behave the same way.

Can you please provide feedback on how to incorporate that based on the
below updated code?
Or can you provide some valid non-number examples for yanking?



==============================================================​

(defun calc-yank (radix)
          "Yank a value into the Calculator buffer.

Valid numeric prefixes for RADIX: 0, 2, 6, 8
No radix notation is prepended for any other numeric prefix.

If RADIX is 2, prepend \"2#\"  - Binary.
If RADIX is 8, prepend \"8#\"  - Octal.
If RADIX is 0, prepend \"10#\" - Decimal.
If RADIX is 6, prepend \"16#\" - Hexadecimal.

If RADIX is a non-nil list (created using \\[universal-argument]), the user
will be prompted to enter the radix in the minibuffer.

If RADIX is nil or if the yanked string already has a calc radix prefix, the
yanked string will be passed on directly to the Calculator buffer without
any
alteration."
          (interactive "P")
          (calc-wrapper
           (calc-pop-push-record-list
            0 "yank"
            (let* (radix-notation
                   (thing-raw (if (fboundp 'current-kill)
                                  (current-kill 0 t)
                                (car kill-ring-yank-pointer)))
                   (thing (if (or (null radix)
                                  (string-match-p "\\`[0-9]+#" thing-raw))
                              thing-raw
                            (progn
                              (setq radix-notation
                                    (if (listp radix)
                                        (concat (number-to-string
                                                 (read-number
                                                  "Set radix for yanked
number (2-36): "))
                                                "#")
                                      (cond ((eq radix 2) "2#")
                                            ((eq radix 8) "8#")
                                            ((eq radix 0) "10#")
                                            ((eq radix 6) "16#")
                                            (t (progn
                                                 (message
                                                  (concat "No radix is "
                                                          "prepended for
prefix "
                                                          "value of %0d.
Valid "
                                                          "numeric prefixes
are "
                                                          "0, 2, 6, 8.")
                                                  radix)
                                                 "")))))
                              (concat radix-notation thing-raw)))))
              (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))))))))


--
Kaushal Modi

On Thu, Oct 8, 2015 at 3:26 PM, Jay Belanger <jay.p.belanger@gmail.com>
wrote:
>
>
> >> With the patch, if the yanked number already has the radix prefix,
there
> >> is an error. It might make more sense to have Calc do an appropriate
> >> conversion.
> > I did not try that case as I meant to use the prefix only when the
copied number does
> > not have a calc-style prefix. But that's a valid point. If the copied
number has
> > calc-style prefix, what should have a higher priority?
>
> If the copied number has a calc-style prefix, then the number should be
> interpreted in that base.  I can be entered onto the stack in the new
> base.  That's done now; if Calc is using base 5 and 2#111 is yanked,
> then 5#12 appears on the stack.
>
> > Either way, it will be easy with some string manipulation.
>
> You'd probably want Calc to do the conversion.
>
> >> It might make more sense to have calc-yank use the current Calc's
current radix rather
> >> than a prefix radix.
> > I need some clarification for this point. Did you mean that if the
> > user has set the calc radix to hex by using "d6" and now if they yank
> > a number "1000" it gets yanked automatically as "16#1000".
>
> Yes; that's what I meant.
>
> > If yes, I believe it will cause a huge disturbance in the way people
> > might have already got used to yanking in calc.
>
> You are probably right.  I would prefer the yanking to pay attention to
> the current Calc setting, but I really don't like changing age-old
> behavior.  Your method is a good approach.
>
> >> Also, the number of radixes in the patch is less than Calc allows.
> > I assumed the use cases of only the common radixes used in programming.
How about I
> > display a prompt for the user to enter any radix they like (I believe
calc supports up
> > to radix 36) if the prefix is specifically '(4) ( a list prefix created
when user uses
> > C-u specifically for prefix).
> >
> > So now, C-2 C-y will prefix the yanked number with "2#". But C-u C-y
will show a prompt
> > where the user will enter radix. If the user entered 9, the prefix
appended will be
> > "9#". Does this option sound fair?
>
> Yes; it sounds like a nice approach.  And perhaps some sort of message
> if the entered radix isn't a number from 2 to 36 (the allowed radixes).
>
> Thanks again,
> Jay
>
>

[-- Attachment #2: Type: text/html, Size: 12596 bytes --]

  reply	other threads:[~2015-10-08 20:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-08 15:07 bug#21648: 25.0.50; [PATCH] Add ability to specify radix for the yanked number in calc-yank Kaushal Modi
2015-10-08 16:08 ` Jay Belanger
2015-10-08 16:19   ` Kaushal Modi
2015-10-08 16:59     ` Kaushal Modi
2015-10-08 19:26       ` Jay Belanger
2015-10-08 20:35         ` Kaushal Modi [this message]
2015-10-09  1:04           ` Jay Belanger
2015-10-09  2:56             ` Kaushal Modi
     [not found]               ` <8737xk7nge.fsf@gmail.com>
     [not found]                 ` <CAFyQvY3YBu1x4wcib++K_mTGfRNLqBfPBaj90rYJriZAuk-9Gg@mail.gmail.com>
     [not found]                   ` <CAFyQvY04LqeGAwyWMGFFDpHDqkb1TbW+vfH9drHruq6+yhVRpw@mail.gmail.com>
     [not found]                     ` <87twq0dkqd.fsf@gmail.com>
     [not found]                       ` <CAFyQvY1-yu9QQhzaQLVv9MLLkBQmOxY=usidbguSJnr9xwKp6g@mail.gmail.com>
     [not found]                         ` <87vbael6ph.fsf@gmail.com>
     [not found]                           ` <CAFyQvY07v3P2bjUQ+PAJbvUQu_nXV=k+umVW-JeKK2r=uWS=_Q@mail.gmail.com>
     [not found]                             ` <87twpyuz1q.fsf@gmail.com>
     [not found]                               ` <CAFyQvY3BbgGimetANnnZ-P13KXvH54_QHqcYF3sAiPWFaz=NFA@mail.gmail.com>
     [not found]                                 ` <87vbaeib5o.fsf@gmail.com>
2015-10-12 13:16                                   ` Kaushal Modi
2015-10-08 16:30   ` Eli Zaretskii
2015-10-08 18:11     ` Jay Belanger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFyQvY1UudfFMN7XzOngTQJwJRND77YgaruTDK6Ui2eKOevCJQ@mail.gmail.com \
    --to=kaushal.modi@gmail.com \
    --cc=21648@debbugs.gnu.org \
    --cc=jay.p.belanger@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.