8

Is there some extension that can make calc autocomplete units while value entered as algebraic input? e.g.

' 1 kn<tab>

should be completed to "1 knot".

gordon-quad
  • 179
  • 3
  • I very much doubt it, but it's an interesting idea, and, probably not too difficult to implement... – wvxvw Oct 23 '15 at 09:16
  • @gordon-quad I have never used calc for stuff that would need units. Can you update the question with an example where you would want the auto-completion to happen? – Kaushal Modi Oct 23 '15 at 12:57
  • well, as I understnand units are just variables, so I can reformulate question "Is there autocompletion for variable names?" – gordon-quad Oct 23 '15 at 15:00
  • Perhaps, `abbrev` or `yas-snippet` work fine. I personally (and frequently) use `yas-snippet` to write elements into 3x3 matrices. (if it's in the `calc-edit` with `'\``) – Garid Mar 16 '23 at 19:13

2 Answers2

0

Most unit descriptions in calc are usually very short. You will find them when typing u v. In my opinion a completion is here unnecessary, just type a ' (e. g. after typing a number) and specify your unit together with the quantity in algebraic mode.

Dieter.Wilhelm
  • 1,836
  • 14
  • 25
  • 1
    I want to convert database which is provided with "units" calculator so there can be quite long names, like 'solarmass' or 'solarluminosity' – gordon-quad Oct 23 '15 at 14:58
  • @gordon-quad: I don't understand your use case. Could you please show this "database"? – Dieter.Wilhelm Oct 24 '15 at 16:54
  • no problem. Here is url of calculator: https://www.gnu.org/software/units/units.html here is example of database: https://github.com/ryantenney/gnu-units/blob/master/units.dat Convertion of this db to calc units is matter of simple sed script (maybe throwing away some invalid for ecalc stuff like unicode units or functions) and as you can see it has pretty long unit names and constants. – gordon-quad Oct 24 '15 at 17:47
0

This is Emacs. So the answer to such questions is almost always "yes".

The extension (not as a package but as code):

(require 'calc)
(require 'calc-aent)
(require 'calc-units)

(defun calc-do-alg-entry-completion (fun &rest args)
  "Call `calc-do-alg-entry' as FUN with ARGS.
Wrapp FUN with `minibuffer-completion-table' bound
to a completion table of calc-units and variables."
  ;; make sure that newly introduced units
  ;; and variables are completed:
  (math-build-units-table)
  (let ((minibuffer-completion-table
     (append
      (mapcar
       (lambda (unit)
         (setq unit (symbol-name (car unit))))
       math-units-table)
      (let (ret)
        (obarray-map
         (lambda (var)
           (let ((var-name (symbol-name var)))
         (when (string-match "\\`var-\\(.*\\)\\'" var-name)
           (push (match-string 1 var-name) ret))))
         obarray)))))
    (apply fun args)))

(defun calc-aent-complete-word ()
  "Complete word at point with entries from `minibuffer-completion-table'."
  (interactive)
  (when-let ((word-bounds (bounds-of-thing-at-point 'word)))
    (completion-in-region (car word-bounds) (cdr word-bounds) minibuffer-completion-table)))

(define-key calc-alg-ent-map "\C-i" #'calc-aent-complete-word)

(advice-add 'calc-do-alg-entry :around #'calc-do-alg-entry-completion)
Tobias
  • 32,569
  • 1
  • 34
  • 75