[Top][All Lists]

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

Re: Embedded list selection with ido-completing-read.

From: Emanuel Berg
Subject: Re: Embedded list selection with ido-completing-read.
Date: Tue, 26 Oct 2021 13:23:21 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Hongyi Zhao wrote:

> I tried for some time and got the following code snippet,
> but it doesn't seem to be able to complete the work
> discussed here by the original code snippet:
> (defun atomic-position (prog flag1 flag2)
>   (interactive
>    (list
>     (completing-read "prog: " '("neb" "pw" "cp") nil t)
>     (completing-read "flag1: " '("alat" "angstrom" "bohr" "crystal"
> "crystal_sg") nil t)
>     (completing-read "flag2: " '("alat" "angstrom" "bohr" "crystal") nil t)
>     )
>    )
>   (cond ((or (equal prog "neb")
>          (equal prog "pw"))
>      (insert (format "Atomic position: %s\n" flag1)))
>     ((equal prog "cp")
>      (insert (format "Atomic position: %s\n" flag2)))
>     ))

Badness 1635247083: Don't add several arguments for the
different way the function can execute. If program "neb" takes
one flag and program "cp" one as well then "prog" and "flag"
is enough.

I changed that and see what, then the whole `cond' thing
became redundant since it then did the same thing ... so
normally we would here think, "good", and just remove it and
perhaps think about how good code leads to even better
code ...

But I said in another thread to not use cond but instead
`cl-case' or `pcase' so I wanted to show you that, for that
reason I changed the output a little bit so it would,
technically at least, be correct to use either one of these
three including cond, hopefully you'll see the improvement
with pcase tho.

Note that with interactive use, we don't get weird indata
because of REQUIRE-MATCH, and from Lisp, because of the
"matches anything" pcase (the last one, the underscore) we
catch bogus programs. But from Lisp it is possible to send
bogus flags! Or brilliant ones for that matter ... so you
omitted that, perhaps realizing that possibility and potential
among future users ...

But regardless of whatever, one should be aware of it.

(defun atomic-position (prog flag)
    (completing-read "program: " '("cp"   "neb"      "pw")                      
    nil t)
    (completing-read    "flag: " '("alat" "angstrom" "bohr" "crystal" 
"crystal_sg") nil t) ))
  (let ((pstr (pcase prog
                ((or "neb" "pw") (format "[neb/pw, here %s] %s\n" prog flag))
                ("cp"            (format "[%s] %s" prog flag))
                (_               (error "No such program")) )))
    (insert "Atomic Kitten " pstr) ))

;; test:
;;   (atomic-position "neb" "bohr")
;;   (call-interactively #'atomic-position)

Now do this:

And read this post again ... and sloowly this time!

underground experts united

reply via email to

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