unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Embedded list selection with ido-completing-read.
Date: Tue, 26 Oct 2021 13:23:21 +0200	[thread overview]
Message-ID: <87bl3ckp1y.fsf@zoho.eu> (raw)
In-Reply-To: CAGP6POKymF+fgmTKuBWzu_Jga+PBt6RxDHg+oAr2w8ZWQNJJ4Q@mail.gmail.com

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)
  (interactive
   (list
    (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:
  https://www.youtube.com/watch?v=uQgB1DdwdMQ

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

-- 
underground experts united
https://dataswamp.org/~incal




  reply	other threads:[~2021-10-26 11:23 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-23  7:33 Embedded list selection with ido-completing-read Hongyi Zhao
2021-10-23  8:43 ` Hongyi Zhao
2021-10-23 19:29   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24  0:28     ` Hongyi Zhao
2021-10-24  1:16       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24  2:28         ` Hongyi Zhao
2021-10-24  3:11           ` Hongyi Zhao
2021-10-24  3:21             ` Hongyi Zhao
2021-10-24  5:37           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24  5:36         ` Hongyi Zhao
2021-10-24  5:43           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24  6:57             ` Hongyi Zhao
2021-10-24  7:00               ` Hongyi Zhao
2021-10-24  9:42               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24 15:05                 ` Hongyi Zhao
2021-10-24 15:12                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24 15:18                     ` Hongyi Zhao
2021-10-24 15:25                       ` Hongyi Zhao
2021-10-24 17:53                         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24 17:51                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24 15:21         ` Hongyi Zhao
2021-10-24 16:23           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26  2:52             ` Hongyi Zhao
2021-10-26  5:19               ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26  8:44                 ` Hongyi Zhao
2021-10-26 11:23                   ` Emanuel Berg via Users list for the GNU Emacs text editor [this message]
2021-10-26 12:31                     ` Hongyi Zhao
2021-10-26 12:40                       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26 12:55                         ` Hongyi Zhao
2021-10-26 13:04                           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26 14:04                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26 15:07                     ` Hongyi Zhao
2021-10-26 22:14                       ` Emanuel Berg via Users list for the GNU Emacs text editor

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87bl3ckp1y.fsf@zoho.eu \
    --to=help-gnu-emacs@gnu.org \
    --cc=moasenwood@zoho.eu \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).