all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Embedded list selection with ido-completing-read.
@ 2021-10-23  7:33 Hongyi Zhao
  2021-10-23  8:43 ` Hongyi Zhao
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-23  7:33 UTC (permalink / raw)
  To: help-gnu-emacs

I've written the following code snippet:

(require 'ido)
(defun ATOMIC_POSITIONS ()
  (interactive)
  (let ((prog '("neb" "pw" "cp")))
    (cond
     ((or (equal (ido-completing-read "prog: " prog) "neb")
      (equal (ido-completing-read "prog: " prog) "pw"))
      (let ((flag '("alat" "bohr" "angstrom" "crystal" "crystal_sg")))
    (insert "ATOMIC_POSITIONS " (ido-completing-read "flag: " flag)))
      )
     ((equal (ido-completing-read "prog: " prog) "cp")
      (let ((flag '("alat" "bohr" "angstrom" "crystal")))
    (insert "ATOMIC_POSITIONS " (ido-completing-read "flag: " flag)))
      )
     ))

  (newline 1))


The purpose is do the following:

If the user selects "neb" or "pw", then insert one entry coming from
'("alat" "bohr" "angstrom" "crystal" "crystal_sg").

If the user selects "cp", then insert one entry coming from '("alat"
"bohr" "angstrom" "crystal").


But based on tries, the above code snippet only works for selection of
"neb", while for the other two selections, sometimes it works,
sometimes it doesn’t output results.

Any hints for this problem?

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-23  8:43 UTC (permalink / raw)
  To: help-gnu-emacs

On Sat, Oct 23, 2021 at 3:33 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> I've written the following code snippet:
>
> (require 'ido)
> (defun ATOMIC_POSITIONS ()
>   (interactive)
>   (let ((prog '("neb" "pw" "cp")))
>     (cond
>      ((or (equal (ido-completing-read "prog: " prog) "neb")
>       (equal (ido-completing-read "prog: " prog) "pw"))

Got it. The ido-completing-read command should be run only once for
one call. The correct usage should be as follows:

1. By ido-completing-read:

 (require 'ido)
(defun ido-ATOMIC_POSITIONS ()
  (interactive)
  (let* ((prog '("neb" "pw" "cp"))
     (prog-read (ido-completing-read "prog: " prog))
     )
    (cond
     ((or (equal prog-read "neb")
      (equal prog-read "pw"))
      (let ((flag '("alat" "bohr" "angstrom" "crystal" "crystal_sg")))
    (insert "ATOMIC_POSITIONS " (ido-completing-read "flag: " flag))))
     ((equal prog-read "cp")
      (let ((flag '("alat" "bohr" "angstrom" "crystal")))
    (insert "ATOMIC_POSITIONS " (ido-completing-read "flag: " flag))))
     ))

  (newline 1))

2. By completing-read:

(defun ATOMIC_POSITIONS ()
  (interactive
   (let* ((prog '("neb" "pw" "cp"))
      (prog-read (completing-read "prog: " prog))
      )

     (cond ((or (equal prog-read "neb")
        (equal prog-read "pw"))
        (insert "ATOMIC_POSITIONS "
            (completing-read "flag: "
                     '("alat" "bohr" "angstrom" "crystal" "crystal_sg"))))
       ((equal prog-read "cp")
        (insert "ATOMIC_POSITIONS "
            (completing-read "flag: "
                     '("alat" "bohr" "angstrom" "crystal")))))
     ))
  (newline 1))




>       (let ((flag '("alat" "bohr" "angstrom" "crystal" "crystal_sg")))
>     (insert "ATOMIC_POSITIONS " (ido-completing-read "flag: " flag)))
>       )
>      ((equal (ido-completing-read "prog: " prog) "cp")
>       (let ((flag '("alat" "bohr" "angstrom" "crystal")))
>     (insert "ATOMIC_POSITIONS " (ido-completing-read "flag: " flag)))
>       )
>      ))
>
>   (newline 1))
>
>
> The purpose is do the following:
>
> If the user selects "neb" or "pw", then insert one entry coming from
> '("alat" "bohr" "angstrom" "crystal" "crystal_sg").
>
> If the user selects "cp", then insert one entry coming from '("alat"
> "bohr" "angstrom" "crystal").
>
>
> But based on tries, the above code snippet only works for selection of
> "neb", while for the other two selections, sometimes it works,
> sometimes it doesn’t output results.
>
> Any hints for this problem?
>
> Regards
> --
> Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
> Theory and Simulation of Materials
> Hebei Vocational University of Technology and Engineering
> No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-23 19:29 UTC (permalink / raw)
  To: help-gnu-emacs

See if you can use `cl-case' (or `pcase') instead of `cond'.

In the `interactive' form, only assign values to the formal
parameters (see my previous post).

You can evaluate these to try:

(interactive "ssay hi: ")

(interactive (list (read-string "say hi: ")))

When done test the function both from Lisp and
interactively.

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  0:28 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 3:30 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> See if you can use `cl-case' (or `pcase') instead of `cond'.
>
> In the `interactive' form, only assign values to the formal
> parameters (see my previous post).
>
> You can evaluate these to try:
>
> (interactive "ssay hi: ")
>
> (interactive (list (read-string "say hi: ")))
>
> When done test the function both from Lisp and
> interactively.

I still can't figure out how to use your suggestions above to further
simplify my implementation below:

(defun my-pw-ATOMIC_POSITIONS2 ()
  (interactive
   (list
    (insert "ATOMIC_POSITIONS "
            (completing-read
             "flag: "
             '(("alat")
               ("bohr")
               ("angstrom")
               ("crystal")
               ("crystal_sg")
               )
             )))))

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
                           ` (2 more replies)
  0 siblings, 3 replies; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24  1:16 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> I still can't figure out how to use your suggestions above
> to further simplify my implementation below:
>
> (defun my-pw-ATOMIC_POSITIONS2 ()
>   (interactive
>    (list
>     (insert "ATOMIC_POSITIONS "
>             (completing-read
>              "flag: "
>              '(("alat")
>                ("bohr")
>                ("angstrom")
>                ("crystal")
>                ("crystal_sg")
>                )
>              )))))

Same as I said before ...

1) Use a formal parameter.

2) Don't do anything in the `interactive' form except
   get/assign the argument(s).

3) Test interactively and non-interactively (i.e., from Lisp).

(defun atomic-position (flag)
  (interactive
   (list (completing-read
          "flag: "
          '("alat" "angstrom" "bohr" "crystal" "crystal_sg") )))
  (insert (format "Atomic Kitten: %s" flag) ))

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

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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  5:37           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-24  5:36         ` Hongyi Zhao
  2021-10-24 15:21         ` Hongyi Zhao
  2 siblings, 2 replies; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  2:28 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 9:17 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > I still can't figure out how to use your suggestions above
> > to further simplify my implementation below:
> >
> > (defun my-pw-ATOMIC_POSITIONS2 ()
> >   (interactive
> >    (list
> >     (insert "ATOMIC_POSITIONS "
> >             (completing-read
> >              "flag: "
> >              '(("alat")
> >                ("bohr")
> >                ("angstrom")
> >                ("crystal")
> >                ("crystal_sg")
> >                )
> >              )))))
>
> Same as I said before ...
>
> 1) Use a formal parameter.

This seems to make the function usable in more situations.

> 2) Don't do anything in the `interactive' form except
>    get/assign the argument(s).

Got it. These jobs are really belongs to things done in `interactive'
manner. So, your idea is: when coding, do the right thing in the right
place.

> 3) Test interactively and non-interactively (i.e., from Lisp).

For portability and usability again.

> (defun atomic-position (flag)
>   (interactive
>    (list (completing-read
>           "flag: "
>           '("alat" "angstrom" "bohr" "crystal" "crystal_sg") )))
>   (insert (format "Atomic Kitten: %s" flag) ))

Got it. Thank you.

> ;; test:
> ;;   (call-interactively #'atomic-position)

Interactively test, equivalent to `M-x atomic-position RET'

> ;;   (atomic-position "bohr")

Test it non-interactively to make sure it works in other Lisp code
blocks to further take advantage of it.


BTW, my ultimate purpose it to implement the in-buffer company-mode
[1] completion support for QE-modes [2], which is the main reason why
I ask this question. And I’ve got some useful advice here [3-5].

[1] https://github.com/company-mode/company-mode
[2] http://pwtk.ijs.si/qe-modes.html
[3] https://github.com/company-mode/company-mode/discussions/1247#discussioncomment-1525332
[4] https://github.com/company-mode/company-mode/blob/master/CONTRIBUTING.md#guidelines-for-third-party-packages
[5] https://github.com/company-mode/company-mode/blob/master/CONTRIBUTING.md#backend-integration

It seems that I still have a long way to go to achieve this goal.

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  1 sibling, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  3:11 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 10:28 AM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> BTW, my ultimate purpose it to implement the in-buffer company-mode
> [1] completion support for QE-modes [2], which is the main reason why
> I ask this question. And I’ve got some useful advice here [3-5].
>
> [1] https://github.com/company-mode/company-mode
> [2] http://pwtk.ijs.si/qe-modes.html
> [3] https://github.com/company-mode/company-mode/discussions/1247#discussioncomment-1525332
> [4] https://github.com/company-mode/company-mode/blob/master/CONTRIBUTING.md#guidelines-for-third-party-packages
> [5] https://github.com/company-mode/company-mode/blob/master/CONTRIBUTING.md#backend-integration
>
> It seems that I still have a long way to go to achieve this goal.

I describe the outline of my final goal as follows [1]:

The ultimate purpose of [`QE-modes`](http://pwtk.ijs.si/qe-modes.html)
package is to facilitate the generation of [`Quantum
ESPRESSO`](http://www.quantum-espresso.org/) input files. Therefore,
the content to be automatically completed here may not be the same as
ordinary automatic completion. Let me use [the following
example](https://github.com/QEF/q-e/blob/master/test-suite/cp_h2o/h2o-mt-blyp-1.in)
to illustrate some of the technical difficulties that need to be
resolved.

```
&control
   title = ' Water Molecule ',
   calculation = 'cp',
   restart_mode = 'from_scratch',
   ndr = 51,
   ndw = 51,
   nstep  = 100,
   iprint = 100,
   isave  = 100,
   tstress = .TRUE.,
   tprnfor = .TRUE.,
   dt    = 5.0d0,
   etot_conv_thr = 1.d-9,
   ekin_conv_thr = 1.d-4,
   prefix = 'h2o'
   verbosity = 'medium'
/
&system
   ibrav = 14,
   ce
   celldm(1) = 12.0,
   celldm(2) = 1.0,
   celldm(3) = 1.0,
   celldm(4) = 0.0,
   celldm(5) = 0.0,
   celldm(6) = 0.0,
   nat  = 3,
   ntyp = 2,
   nbnd = 4,
   ecutwfc = 80.0,
/
&electrons
   emass = 400.d0,
   emass_cutoff = 2.5d0,
   electron_dynamics = 'damp',
   electron_damping = 0.2
/
&ions
   orthogonalization = 'ortho',
   ion_dynamics = 'none',
   ion_radius(1) = 0.8d0,
   ion_radius(2) = 0.8d0,
/
ATOMIC_SPECIES
   O 16.0d0 O.blyp-mt.UPF
   H 1.00d0 H.blyp-vbc.UPF
ATOMIC_POSITIONS (bohr)
   O     0.0099    0.0099    0.0000  0 0 0
   H     1.8325   -0.2243   -0.0001  1 1 1
   H    -0.2243    1.8325    0.0002  1 1 1
```
The input file specification of the above example is located
[here](https://www.quantum-espresso.org/Doc/INPUT_CP.html). The main
problems I want to solve are:

1. When I try to input one namelist, hinting all possible namelists at
the point with drop-down frame; once I select the namelist, say,
`&CONTROL`, then insert it corresponding to the specification as
follows, and put the point in the middle of it, denoted by `|`:

```
&control
|
/
```
2. When I try to input one CARD, hinting all possible CARDs at the
point with drop-down frame; once I select the CARD, say,
`ATOMIC_SPECIES`, then insert it corresponding to the specification as
follows, and put the point in the middle of it, denoted by `|`:

```
ATOMIC_SPECIES
|
```
3. When I try to input one variable with the point located in certain
namelist, hinting all possible variables belonging to the given
namelist at the point with drop-down frame. Say, for the namelist
`&control`, only [the following
variables](https://www.quantum-espresso.org/Doc/INPUT_CP.html#idm4)
should be used to construct the candidates list:

`     calculation | title | verbosity | isave | restart_mode | nstep |
iprint | tstress | tprnfor | dt | outdir | saverho | prefix | ndr |
ndw | tabps | max_seconds | etot_conv_thr | forc_conv_thr |
ekin_conv_thr | disk_io | memory | pseudo_dir | tefield `

4. After I have completed an input of one CARD, inserting an example
usage of the CARD, say, the following one:

```
ATOMIC_POSITIONS (bohr)
   O     0.0099    0.0099    0.0000  0 0 0
```


[1] https://github.com/company-mode/company-mode/discussions/1247#discussioncomment-1526576

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-24  3:11           ` Hongyi Zhao
@ 2021-10-24  3:21             ` Hongyi Zhao
  0 siblings, 0 replies; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  3:21 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 11:11 AM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Sun, Oct 24, 2021 at 10:28 AM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> > BTW, my ultimate purpose it to implement the in-buffer company-mode
> > [1] completion support for QE-modes [2], which is the main reason why
> > I ask this question. And I’ve got some useful advice here [3-5].
> >
> > [1] https://github.com/company-mode/company-mode
> > [2] http://pwtk.ijs.si/qe-modes.html
> > [3] https://github.com/company-mode/company-mode/discussions/1247#discussioncomment-1525332
> > [4] https://github.com/company-mode/company-mode/blob/master/CONTRIBUTING.md#guidelines-for-third-party-packages
> > [5] https://github.com/company-mode/company-mode/blob/master/CONTRIBUTING.md#backend-integration
> >
> > It seems that I still have a long way to go to achieve this goal.
>
> I describe the outline of my final goal as follows [1]:
>
> The ultimate purpose of [`QE-modes`](http://pwtk.ijs.si/qe-modes.html)
> package is to facilitate the generation of [`Quantum
> ESPRESSO`](http://www.quantum-espresso.org/) input files. Therefore,
> the content to be automatically completed here may not be the same as
> ordinary automatic completion. Let me use [the following
> example](https://github.com/QEF/q-e/blob/master/test-suite/cp_h2o/h2o-mt-blyp-1.in)
> to illustrate some of the technical difficulties that need to be
> resolved.
>
> ```
> &control
>    title = ' Water Molecule ',
>    calculation = 'cp',
>    restart_mode = 'from_scratch',
>    ndr = 51,
>    ndw = 51,
>    nstep  = 100,
>    iprint = 100,
>    isave  = 100,
>    tstress = .TRUE.,
>    tprnfor = .TRUE.,
>    dt    = 5.0d0,
>    etot_conv_thr = 1.d-9,
>    ekin_conv_thr = 1.d-4,
>    prefix = 'h2o'
>    verbosity = 'medium'
> /
> &system
>    ibrav = 14,
>    ce
>    celldm(1) = 12.0,
>    celldm(2) = 1.0,
>    celldm(3) = 1.0,
>    celldm(4) = 0.0,
>    celldm(5) = 0.0,
>    celldm(6) = 0.0,
>    nat  = 3,
>    ntyp = 2,
>    nbnd = 4,
>    ecutwfc = 80.0,
> /
> &electrons
>    emass = 400.d0,
>    emass_cutoff = 2.5d0,
>    electron_dynamics = 'damp',
>    electron_damping = 0.2
> /
> &ions
>    orthogonalization = 'ortho',
>    ion_dynamics = 'none',
>    ion_radius(1) = 0.8d0,
>    ion_radius(2) = 0.8d0,
> /
> ATOMIC_SPECIES
>    O 16.0d0 O.blyp-mt.UPF
>    H 1.00d0 H.blyp-vbc.UPF
> ATOMIC_POSITIONS (bohr)
>    O     0.0099    0.0099    0.0000  0 0 0
>    H     1.8325   -0.2243   -0.0001  1 1 1
>    H    -0.2243    1.8325    0.0002  1 1 1
> ```
> The input file specification of the above example is located
> [here](https://www.quantum-espresso.org/Doc/INPUT_CP.html). The main
> problems I want to solve are:
>
> 1. When I try to input one namelist, hinting all possible namelists at
> the point with drop-down frame; once I select the namelist, say,
> `&CONTROL`, then insert it corresponding to the specification as
> follows, and put the point in the middle of it, denoted by `|`:
>
> ```
> &control
> |
> /
> ```
> 2. When I try to input one CARD, hinting all possible CARDs at the
> point with drop-down frame; once I select the CARD, say,
> `ATOMIC_SPECIES`, then insert it corresponding to the specification as
> follows, and put the point in the middle of it, denoted by `|`:

put the point on the line below it.


> ```
> ATOMIC_SPECIES
> |
> ```
> 3. When I try to input one variable with the point located in certain
> namelist, hinting all possible variables belonging to the given
> namelist at the point with drop-down frame. Say, for the namelist
> `&control`, only [the following
> variables](https://www.quantum-espresso.org/Doc/INPUT_CP.html#idm4)
> should be used to construct the candidates list:
>
> `     calculation | title | verbosity | isave | restart_mode | nstep |
> iprint | tstress | tprnfor | dt | outdir | saverho | prefix | ndr |
> ndw | tabps | max_seconds | etot_conv_thr | forc_conv_thr |
> ekin_conv_thr | disk_io | memory | pseudo_dir | tefield `
>
> 4. After I have completed an input of one CARD, inserting an example
> usage of the CARD, say, the following one:
>
> ```
> ATOMIC_POSITIONS (bohr)
>    O     0.0099    0.0099    0.0000  0 0 0
> ```
>
>
> [1] https://github.com/company-mode/company-mode/discussions/1247#discussioncomment-1526576
>
> HZ



-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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  5:36         ` Hongyi Zhao
  2021-10-24  5:43           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-24 15:21         ` Hongyi Zhao
  2 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  5:36 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 9:17 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > I still can't figure out how to use your suggestions above
> > to further simplify my implementation below:
> >
> > (defun my-pw-ATOMIC_POSITIONS2 ()
> >   (interactive
> >    (list
> >     (insert "ATOMIC_POSITIONS "
> >             (completing-read
> >              "flag: "
> >              '(("alat")
> >                ("bohr")
> >                ("angstrom")
> >                ("crystal")
> >                ("crystal_sg")
> >                )
> >              )))))
>
> Same as I said before ...
>
> 1) Use a formal parameter.
>
> 2) Don't do anything in the `interactive' form except
>    get/assign the argument(s).
>
> 3) Test interactively and non-interactively (i.e., from Lisp).
>
> (defun atomic-position (flag)
>   (interactive
>    (list (completing-read
>           "flag: "
>           '("alat" "angstrom" "bohr" "crystal" "crystal_sg") )))
>   (insert (format "Atomic Kitten: %s" flag) ))
>
> ;; test:
> ;;   (call-interactively #'atomic-position)

The following also works:

(call-interactively 'atomic-position)

> ;;   (atomic-position "bohr")
>
> --
> underground experts united
> https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-24  2:28         ` Hongyi Zhao
  2021-10-24  3:11           ` Hongyi Zhao
@ 2021-10-24  5:37           ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24  5:37 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> 1) Use a formal parameter.
>
> This seems to make the function usable in more situations.
>
>> 2) Don't do anything in the `interactive' form except
>>    get/assign the argument(s).
>
> Got it. These jobs are really belongs to things done in
> `interactive' manner.

It also makes it much easier to write the code since then you
can just refer to it as a variable.
-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24  5:43 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> The following also works:
>
> (call-interactively 'atomic-position)

Byte-compile #'xyzabc and 'abcxyz to see the difference ...


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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 2 replies; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  6:57 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 1:46 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > The following also works:
> >
> > (call-interactively 'atomic-position)
>
> Byte-compile #'xyzabc and 'abcxyz to see the difference ...

By evaluating them, the same results are given:

(byte-compile #'xyzabc) ;;  #[nil "\300\207" [nil] 1]
(byte-compile 'abcxyz) ;;  #[nil "\300\207" [nil] 1]

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  1 sibling, 0 replies; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24  7:00 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 2:57 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Sun, Oct 24, 2021 at 1:46 PM Emanuel Berg via Users list for the
> GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> >
> > Hongyi Zhao wrote:
> >
> > > The following also works:
> > >
> > > (call-interactively 'atomic-position)
> >
> > Byte-compile #'xyzabc and 'abcxyz to see the difference ...
>
> By evaluating them, the same results are given:
>
> (byte-compile #'xyzabc) ;;  #[nil "\300\207" [nil] 1]
> (byte-compile 'abcxyz) ;;  #[nil "\300\207" [nil] 1]

I still can't figure out the difference. Maybe I misunderstood you
again. Sorry for my ignorance on Emacs and ELISP.

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  1 sibling, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24  9:42 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> Byte-compile #'xyzabc and 'abcxyz to see the difference ...
>
> By evaluating them, the same results are given:
>
> (byte-compile #'xyzabc) ;;  #[nil "\300\207" [nil] 1]
> (byte-compile 'abcxyz) ;;  #[nil "\300\207" [nil] 1]

Byte-compile as in line 59 here:

  https://dataswamp.org/~incal/emacs-init/Makefile

Always byte-compile your code BTW.

Faster, better code. Ask here only when it byte compiles and
you have 0 warnings and 0 errors - unless, of course, that's
what your question is about ;)

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24 15:05 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 5:46 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> >> Byte-compile #'xyzabc and 'abcxyz to see the difference ...
> >
> > By evaluating them, the same results are given:
> >
> > (byte-compile #'xyzabc) ;;  #[nil "\300\207" [nil] 1]
> > (byte-compile 'abcxyz) ;;  #[nil "\300\207" [nil] 1]
>
> Byte-compile as in line 59 here:
>
>   https://dataswamp.org/~incal/emacs-init/Makefile
>
> Always byte-compile your code BTW.
>
> Faster, better code. Ask here only when it byte compiles and
> you have 0 warnings and 0 errors - unless, of course, that's
> what your question is about ;)

My Emacs was compiled with the `--with-native-compilation` option, as
suggested here [1], so I think the byte-compile will be done
automatically on my Emacs machine. BTW, here are the detailed git
master Emacs compilation steps used by me:

$ ./autogen.sh
$ ./configure CFLAGS="-g3 -O2" --without-m17n-flt --with-native-compilation
$ make -j $(nproc) bootstrap
$ make -j $(nproc) NATIVE_FULL_AOT=1
$ make tags
$ sudo make install

[1] https://ddavis.io/posts/emacs-native-centos7/

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24 15:12 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> My Emacs was compiled with the `--with-native-compilation`
> option, as suggested here [1], so I think the byte-compile
> will be done automatically on my Emacs machine. BTW, here
> are the detailed git master Emacs compilation steps used by
> me:
>
> $ ./autogen.sh
> $ ./configure CFLAGS="-g3 -O2" --without-m17n-flt --with-native-compilation
> $ make -j $(nproc) bootstrap
> $ make -j $(nproc) NATIVE_FULL_AOT=1
> $ make tags
> $ sudo make install
>
> [1] https://ddavis.io/posts/emacs-native-centos7/

Byte-compile Elisp != build Emacs ... see lines 59-62:

  https://dataswamp.org/~incal/emacs-init/Makefile

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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:51                       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24 15:18 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 11:15 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > My Emacs was compiled with the `--with-native-compilation`
> > option, as suggested here [1], so I think the byte-compile
> > will be done automatically on my Emacs machine. BTW, here
> > are the detailed git master Emacs compilation steps used by
> > me:
> >
> > $ ./autogen.sh
> > $ ./configure CFLAGS="-g3 -O2" --without-m17n-flt --with-native-compilation
> > $ make -j $(nproc) bootstrap
> > $ make -j $(nproc) NATIVE_FULL_AOT=1
> > $ make tags
> > $ sudo make install
> >
> > [1] https://ddavis.io/posts/emacs-native-centos7/
>
> Byte-compile Elisp != build Emacs ... see lines 59-62:
>
>   https://dataswamp.org/~incal/emacs-init/Makefile

byte-compile  = $(emacs)                                     \
    --batch                                                   \
    --eval "(setq load-path (append load-path '($(packs)))))" \
    -f batch-byte-compile

TBF, I really have never used Emacs like this ;-(

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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  5:36         ` Hongyi Zhao
@ 2021-10-24 15:21         ` Hongyi Zhao
  2021-10-24 16:23           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24 15:21 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 9:17 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > I still can't figure out how to use your suggestions above
> > to further simplify my implementation below:
> >
> > (defun my-pw-ATOMIC_POSITIONS2 ()
> >   (interactive
> >    (list
> >     (insert "ATOMIC_POSITIONS "
> >             (completing-read
> >              "flag: "
> >              '(("alat")
> >                ("bohr")
> >                ("angstrom")
> >                ("crystal")
> >                ("crystal_sg")
> >                )
> >              )))))
>
> Same as I said before ...
>
> 1) Use a formal parameter.
>
> 2) Don't do anything in the `interactive' form except
>    get/assign the argument(s).
>
> 3) Test interactively and non-interactively (i.e., from Lisp).
>
> (defun atomic-position (flag)
>   (interactive
>    (list (completing-read
>           "flag: "
>           '("alat" "angstrom" "bohr" "crystal" "crystal_sg") )))
>   (insert (format "Atomic Kitten: %s" flag) ))

I've tried to rewrite the following more complex function written by
me by your above rules/guidelines, but still failed to do the trick:

(defun ATOMIC_POSITIONS ()
  (interactive
   (let* ((prog '("neb" "pw" "cp"))
      (prog-read (completing-read "prog: " prog))
      )

     (cond ((or (equal prog-read "neb")
        (equal prog-read "pw"))
        (insert "ATOMIC_POSITIONS "
            (completing-read "flag: "
                     '("alat" "bohr" "angstrom" "crystal" "crystal_sg"))))
       ((equal prog-read "cp")
        (insert "ATOMIC_POSITIONS "
            (completing-read "flag: "
                     '("alat" "bohr" "angstrom" "crystal")))))
     ))
  (newline 1))


> ;; test:
> ;;   (call-interactively #'atomic-position)
> ;;   (atomic-position "bohr")
>
> --
> underground experts united
> https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  1 sibling, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-24 15:25 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 11:18 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Sun, Oct 24, 2021 at 11:15 PM Emanuel Berg via Users list for the
> GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> >
> > Hongyi Zhao wrote:
> >
> > > My Emacs was compiled with the `--with-native-compilation`
> > > option, as suggested here [1], so I think the byte-compile
> > > will be done automatically on my Emacs machine. BTW, here
> > > are the detailed git master Emacs compilation steps used by
> > > me:
> > >
> > > $ ./autogen.sh
> > > $ ./configure CFLAGS="-g3 -O2" --without-m17n-flt --with-native-compilation
> > > $ make -j $(nproc) bootstrap
> > > $ make -j $(nproc) NATIVE_FULL_AOT=1
> > > $ make tags
> > > $ sudo make install
> > >
> > > [1] https://ddavis.io/posts/emacs-native-centos7/
> >
> > Byte-compile Elisp != build Emacs ... see lines 59-62:
> >
> >   https://dataswamp.org/~incal/emacs-init/Makefile
>
> byte-compile  = $(emacs)                                     \
>     --batch                                                   \
>     --eval "(setq load-path (append load-path '($(packs)))))" \
>     -f batch-byte-compile
>
> TBF, I really have never used Emacs like this ;-(

It seems that we are talking about the thing noted here [1]:

At a quick glance it looks like there is some unconventional packaging
going on in their repo.
They probably shouldn't be distributing compiled elisp files. Those
are not guaranteed to be bytecode compatible between Emacs versions
and should be generated by the user's Emacs.

[1] https://github.com/raxod502/straight.el/issues/871#issuecomment-950265097



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24 16:23 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> 1) Use a formal parameter.
>>
>> 2) Don't do anything in the `interactive' form except
>>    get/assign the argument(s).
>>
>> 3) Test interactively and non-interactively (i.e., from Lisp).
>>
>> (defun atomic-position (flag)
>>   (interactive
>>    (list (completing-read
>>           "flag: "
>>           '("alat" "angstrom" "bohr" "crystal" "crystal_sg") )))
>>   (insert (format "Atomic Kitten: %s" flag) ))
>
> I've tried to rewrite the following more complex function
> written by me by your above rules/guidelines, but still
> failed to do the trick

... ? Is this for real?

Do 1-2, then 3.

> (defun ATOMIC_POSITIONS ()
>   (interactive
>    (let* ((prog '("neb" "pw" "cp"))
>       (prog-read (completing-read "prog: " prog))
>       )
>
>      (cond ((or (equal prog-read "neb")
>         (equal prog-read "pw"))
>         (insert "ATOMIC_POSITIONS "
>             (completing-read "flag: "
>                      '("alat" "bohr" "angstrom" "crystal" "crystal_sg"))))
>        ((equal prog-read "cp")
>         (insert "ATOMIC_POSITIONS "
>             (completing-read "flag: "
>                      '("alat" "bohr" "angstrom" "crystal")))))
>      ))
>   (newline 1))

...

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-24 15:18                     ` Hongyi Zhao
  2021-10-24 15:25                       ` Hongyi Zhao
@ 2021-10-24 17:51                       ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24 17:51 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> byte-compile  = $(emacs)                                      \
>     --batch                                                   \
>     --eval "(setq load-path (append load-path '($(packs)))))" \
>     -f batch-byte-compile
>
> TBF, I really have never used Emacs like this ;-(

Well, start today ... That is an ordinary Makefile the same
way you'd write it for whatever.

But that command looks more polished/complicated than it is or
needs to be, to make it work for just one basic Elisp file it
should be enough to do

$ emacs --batch -f batch-byte-compile EL-FILE

"do it today, in a different way"

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-24 15:25                       ` Hongyi Zhao
@ 2021-10-24 17:53                         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24 17:53 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> It seems that we are talking about the thing noted here [1]

Indeed,

[1] $ emacs --batch -f batch-byte-compile EL-FILE

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-26  2:52 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Mon, Oct 25, 2021 at 12:23 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> >> 1) Use a formal parameter.
> >>
> >> 2) Don't do anything in the `interactive' form except
> >>    get/assign the argument(s).
> >>
> >> 3) Test interactively and non-interactively (i.e., from Lisp).
> >>
> >> (defun atomic-position (flag)
> >>   (interactive
> >>    (list (completing-read
> >>           "flag: "
> >>           '("alat" "angstrom" "bohr" "crystal" "crystal_sg") )))
> >>   (insert (format "Atomic Kitten: %s" flag) ))
> >
> > I've tried to rewrite the following more complex function
> > written by me by your above rules/guidelines, but still
> > failed to do the trick
>
> ... ? Is this for real?
>
> Do 1-2, then 3.

But the formal parameter isn't used in the `interactive' part of your code:

(defun atomic-position (flag)
  (interactive
   (list
    (completing-read "flag: " '("alat" "angstrom" "bohr" "crystal"
"crystal_sg") nil t)))
  (insert (format "Atomic position: %s\n" flag)))

This makes it difficult for me to adapt the above code to a
multi-parameter situation.


> > (defun ATOMIC_POSITIONS ()
> >   (interactive
> >    (let* ((prog '("neb" "pw" "cp"))
> >       (prog-read (completing-read "prog: " prog))
> >       )
> >
> >      (cond ((or (equal prog-read "neb")
> >         (equal prog-read "pw"))
> >         (insert "ATOMIC_POSITIONS "
> >             (completing-read "flag: "
> >                      '("alat" "bohr" "angstrom" "crystal" "crystal_sg"))))
> >        ((equal prog-read "cp")
> >         (insert "ATOMIC_POSITIONS "
> >             (completing-read "flag: "
> >                      '("alat" "bohr" "angstrom" "crystal")))))
> >      ))
> >   (newline 1))
>
> ...
>
> --
> underground experts united
> https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26  5:19 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> But the formal parameter isn't used in the `interactive'
> part of your code:

The order if there are many. If therer are parameters
A B C then the interactive form should read (a b c) and
parameter A will be assigned a and so on ...

> (defun atomic-position (flag)
>   (interactive
>    (list
>     (completing-read "flag: " '("alat" "angstrom" "bohr" "crystal"
> "crystal_sg") nil t)))
>   (insert (format "Atomic position: %s\n" flag)))
>
> This makes it difficult for me to adapt the above code to
> a multi-parameter situation.
>
>> > (defun ATOMIC_POSITIONS ()
>> >   (interactive
>> >    (let* ((prog '("neb" "pw" "cp"))
>> >       (prog-read (completing-read "prog: " prog))
>> >       )
>> >
>> >      (cond ((or (equal prog-read "neb")
>> >         (equal prog-read "pw"))
>> >         (insert "ATOMIC_POSITIONS "
>> >             (completing-read "flag: "
>> >                      '("alat" "bohr" "angstrom" "crystal" "crystal_sg"))))
>> >        ((equal prog-read "cp")
>> >         (insert "ATOMIC_POSITIONS "
>> >             (completing-read "flag: "
>> >                      '("alat" "bohr" "angstrom" "crystal")))))
>> >      ))
>> >   (newline 1))

(list (completing-read ... )
      (completing-read ... )
      ;; as many you like/have said should be there )

Remove everything that isn't doing the list. Then see if and
in what state you get a list by evaluating the
interactive form.

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  2021-10-26 14:04                   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-26  8:44 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Tue, Oct 26, 2021 at 1:20 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > But the formal parameter isn't used in the `interactive'
> > part of your code:
>
> The order if there are many. If therer are parameters
> A B C then the interactive form should read (a b c) and
> parameter A will be assigned a and so on ...
>
> > (defun atomic-position (flag)
> >   (interactive
> >    (list
> >     (completing-read "flag: " '("alat" "angstrom" "bohr" "crystal"
> > "crystal_sg") nil t)))
> >   (insert (format "Atomic position: %s\n" flag)))
> >
> > This makes it difficult for me to adapt the above code to
> > a multi-parameter situation.
> >
> >> > (defun ATOMIC_POSITIONS ()
> >> >   (interactive
> >> >    (let* ((prog '("neb" "pw" "cp"))
> >> >       (prog-read (completing-read "prog: " prog))
> >> >       )
> >> >
> >> >      (cond ((or (equal prog-read "neb")
> >> >         (equal prog-read "pw"))
> >> >         (insert "ATOMIC_POSITIONS "
> >> >             (completing-read "flag: "
> >> >                      '("alat" "bohr" "angstrom" "crystal" "crystal_sg"))))
> >> >        ((equal prog-read "cp")
> >> >         (insert "ATOMIC_POSITIONS "
> >> >             (completing-read "flag: "
> >> >                      '("alat" "bohr" "angstrom" "crystal")))))
> >> >      ))
> >> >   (newline 1))
>
> (list (completing-read ... )
>       (completing-read ... )
>       ;; as many you like/have said should be there )
>
> Remove everything that isn't doing the list. Then see if and
> in what state you get a list by evaluating the
> interactive form.

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)))
    ))

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-26  8:44                 ` Hongyi Zhao
@ 2021-10-26 11:23                   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-26 12:31                     ` Hongyi Zhao
  2021-10-26 14:04                   ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26 11:23 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-26 11:23                   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-10-26 12:31                     ` Hongyi Zhao
  2021-10-26 12:40                       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-26 12:31 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Tue, Oct 26, 2021 at 7:26 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> (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) ))

Thank you for your wonderful code snippets. But there is a small
problem that makes the above code not meet my requirements, as
described below. In my original but ugly code, it does the following:

(or
 (equal prog "neb")
 (equal prog "pw"))

flag is chosen from: '("alat" "angstrom" "bohr" "crystal" "crystal_sg")

(equal prog "cp")

flag is chosen from: '("alat" "angstrom" "bohr" "crystal")

And this is why I try to use two flags.

> ;; 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



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26 12:40 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> flag is chosen from: '("alat" "angstrom" "bohr" "crystal" "crystal_sg")
>
> (equal prog "cp")
>
> flag is chosen from: '("alat" "angstrom" "bohr" "crystal")

Okay, absolutely, you can have two lists as you have above,
but you still only need and want one flag.

You can see this in the way you put it above BTW!

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-26 12:55 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Tue, Oct 26, 2021 at 8:41 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > flag is chosen from: '("alat" "angstrom" "bohr" "crystal" "crystal_sg")
> >
> > (equal prog "cp")
> >
> > flag is chosen from: '("alat" "angstrom" "bohr" "crystal")
>
> Okay, absolutely, you can have two lists as you have above,
> but you still only need and want one flag.
>
> You can see this in the way you put it above BTW!

But how to adjust your code to meet my above requirements? I have
tried for a while, but still no idea.



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-26 12:55                         ` Hongyi Zhao
@ 2021-10-26 13:04                           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26 13:04 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> But how to adjust your code to meet my above requirements?
> I have tried for a while, but still no idea

Do two functions, the first is the interactive interface, it
takes one argument which is the program, then set possible
flags based on that and read the flag, then send both the
program and the flag to a non-interactive function which looks
pretty much like your did.

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-26  8:44                 ` Hongyi Zhao
  2021-10-26 11:23                   ` 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
  1 sibling, 1 reply; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26 14:04 UTC (permalink / raw)
  To: help-gnu-emacs

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)))
>     ))

Ha, so what's up with all this?

You fooled me, I admit it ... good one :)

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  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
  0 siblings, 1 reply; 33+ messages in thread
From: Hongyi Zhao @ 2021-10-26 15:07 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Tue, Oct 26, 2021 at 10:05 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> 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)))
> >     ))
>
> Ha, so what's up with all this?

The above code has a problem: only one of the completing-read command
corresponding to flag1 or flag2 should be called in one run, but it
runs both at the same call.

> You fooled me, I admit it ... good one :)

I’m serious and have never done anything flashy.

HZ



^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: Embedded list selection with ido-completing-read.
  2021-10-26 15:07                     ` Hongyi Zhao
@ 2021-10-26 22:14                       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 33+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26 22:14 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> Ha, so what's up with all this?
>
> The above code has a problem: only one of the
> completing-read command corresponding to flag1 or flag2
> should be called in one run, but it runs both at the
> same call.

I have already suggested and shown what you should do.
Without attempting to do any of that, you refer to some
package or URL to some programming resource or repository, say
it doesn't work in a way that is wordy yet often boils down to
just that, then you post the same old code again with all the
issues I've told you about in exactly the same, untouched form
and places as before. So either this is some elaborate
trolling or you are not helped by my messages anyway, so
why bother?

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




^ permalink raw reply	[flat|nested] 33+ messages in thread

end of thread, other threads:[~2021-10-26 22:14 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.