emacs : a minor mode to show numbers in all kinds of radix

103 Views Asked by At

I'm looking for an Emacs package that can recognise numbers and their radix.

I would like it to be interactive (to give the value of the number selected or at point).

The returned values could be printed to the minibuffer (then messages buffer) or to a separate buffer.

Can someone explain how can I get access those values and what are the steps I need to take to create such a function?

1

There are 1 best solutions below

2
Drew On

I don't have a full, direct answer for you, but here are a couple of libraries that you can look at for ideas about realizing this yourself. Some of the question depends on just how you want to display/return the number.


If the text to recognize uses the standard Emacs form for radix and number in that radix then you can define a radix-number-at-point. That form is documented in the Elisp manual, node Integer Basics. For example, #13r90B3 is the same number as decimal integer 19919.

Here'a a quick definition. You might want to tweak it.

(defun radix-number-at-point ()
  "Return the radix number at point, or nil if none is found."
  (when (thing-at-point-looking-at "-?#[0-9][0-9]*r[a-zA-Z0-9]+\\.?[a-zA-Z0-9]*" 500)
    (read (buffer-substring-no-properties (match-beginning 0) (match-end 0)))))

You can also define a function that doesn't require the explicit #rRADIX part, for example automatically trying different radixes, in sequence and, say, using the lowest radix that returns a number.