all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Insert one of the flags automatically with tab completion.
@ 2021-10-23  5:07 Hongyi Zhao
  2021-10-23  6:10 ` Hongyi Zhao
  0 siblings, 1 reply; 9+ messages in thread
From: Hongyi Zhao @ 2021-10-23  5:07 UTC (permalink / raw)
  To: help-gnu-emacs

I noticed the following function defined here[1]:

(defun pw-ATOMIC_POSITIONS ()
  (interactive)
  (let ((flag (read-string "Flags: { alat | bohr | angstrom | crystal
| crystal_sg } ")))
    (insert "ATOMIC_POSITIONS " flag))
  (newline 1)
  )

The above function can only prompt the user to insert one of the flags
manually. But I want to insert one of the flags automatically with tab
completion. Any hints for achieving this enhancement?

[1] https://github.com/QEF/q-e/blob/03a7fa640903173662b24e4c30e520c9beaba16d/GUI/QE-modes/qe-modes/qe-funcs.el#L1966

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] 9+ messages in thread

* Re: Insert one of the flags automatically with tab completion.
  2021-10-23  5:07 Insert one of the flags automatically with tab completion Hongyi Zhao
@ 2021-10-23  6:10 ` Hongyi Zhao
  2021-10-23 19:23   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 9+ messages in thread
From: Hongyi Zhao @ 2021-10-23  6:10 UTC (permalink / raw)
  To: help-gnu-emacs

On Sat, Oct 23, 2021 at 1:07 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> I noticed the following function defined here[1]:
>
> (defun pw-ATOMIC_POSITIONS ()
>   (interactive)
>   (let ((flag (read-string "Flags: { alat | bohr | angstrom | crystal
> | crystal_sg } ")))
>     (insert "ATOMIC_POSITIONS " flag))
>   (newline 1)
>   )
>
> The above function can only prompt the user to insert one of the flags
> manually. But I want to insert one of the flags automatically with tab
> completion. Any hints for achieving this enhancement?
>
> [1] https://github.com/QEF/q-e/blob/03a7fa640903173662b24e4c30e520c9beaba16d/GUI/QE-modes/qe-modes/qe-funcs.el#L1966

I came up with two solutions:

1. by ido-completing-read

;; http://ergoemacs.org/emacs/elisp_idioms_prompting_input.html
(require 'ido)

(defun my-pw-ATOMIC_POSITIONS1 ()
  (interactive)
  (let ((flag '("alat" "bohr" "angstrom" "crystal" "crystal_sg")))
    (insert "ATOMIC_POSITIONS " (ido-completing-read "Select the flag: " flag)))
  (newline 1))


2. by completing-read

;; https://stackoverflow.com/questions/19772394/elisp-function-select-argument-from-list
(defun my-pw-ATOMIC_POSITIONS2 ()
  (interactive
   (list
    (insert "ATOMIC_POSITIONS "
        (completing-read
         "Select the flag: "
         '(("alat" 1)
           ("bohr" 2)
           ("angstrom" 3)
           ("crystal" 4)
           ("crystal_sg" 5)
           )
         nil t ""))))
  (newline 1))

HZ



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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-23  6:10 ` Hongyi Zhao
@ 2021-10-23 19:23   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-24 14:50     ` Hongyi Zhao
  2021-10-24 14:53     ` Hongyi Zhao
  0 siblings, 2 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-23 19:23 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> (defun my-pw-ATOMIC_POSITIONS2 ()
>   (interactive
>    (list
>     (insert "ATOMIC_POSITIONS "
>         (completing-read
>          "Select the flag: "
>          '(("alat" 1)
>            ("bohr" 2)
>            ("angstrom" 3)
>            ("crystal" 4)
>            ("crystal_sg" 5)
>            )
>          nil t ""))))
>   (newline 1))

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

Note that interactive and non-interactive use are not
identical in one aspect ...

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




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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-23 19:23   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-10-24 14:50     ` Hongyi Zhao
  2021-10-24 15:09       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-24 14:53     ` Hongyi Zhao
  1 sibling, 1 reply; 9+ messages in thread
From: Hongyi Zhao @ 2021-10-24 14:50 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 3:23 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > (defun my-pw-ATOMIC_POSITIONS2 ()
> >   (interactive
> >    (list
> >     (insert "ATOMIC_POSITIONS "
> >         (completing-read
> >          "Select the flag: "
> >          '(("alat" 1)
> >            ("bohr" 2)
> >            ("angstrom" 3)
> >            ("crystal" 4)
> >            ("crystal_sg" 5)
> >            )
> >          nil t ""))))
> >   (newline 1))
>
> (defun atomic-position (flag)
>   (interactive
>    (list
>     (completing-read
>      "flag: " '("alat" "angstrom" "bohr" "crystal" "crystal_sg") nil t) ))
>   (insert (format "Atomic position: %s\n" flag)) )

Thank you for your idiomatic coding style correction and logic enhancement.

> Note that interactive and non-interactive use are not
> identical in one aspect ...

Interactive is interacting with user as a command.
Non-interactive is called from within lisp code.



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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-23 19:23   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-24 14:50     ` Hongyi Zhao
@ 2021-10-24 14:53     ` Hongyi Zhao
  1 sibling, 0 replies; 9+ messages in thread
From: Hongyi Zhao @ 2021-10-24 14:53 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 3:23 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > (defun my-pw-ATOMIC_POSITIONS2 ()
> >   (interactive
> >    (list
> >     (insert "ATOMIC_POSITIONS "
> >         (completing-read
> >          "Select the flag: "
> >          '(("alat" 1)
> >            ("bohr" 2)
> >            ("angstrom" 3)
> >            ("crystal" 4)
> >            ("crystal_sg" 5)
> >            )
> >          nil t ""))))
> >   (newline 1))
>
> (defun atomic-position (flag)
>   (interactive
>    (list
>     (completing-read
>      "flag: " '("alat" "angstrom" "bohr" "crystal" "crystal_sg") nil t) ))

I can't find the real effect of `nil t` here, so I omitted it:

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


>   (insert (format "Atomic position: %s\n" flag)) )
>
> Note that interactive and non-interactive use are not
> identical in one aspect ...
>
> --
> underground experts united
> https://dataswamp.org/~incal



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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-24 14:50     ` Hongyi Zhao
@ 2021-10-24 15:09       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-26  2:38         ` Hongyi Zhao
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-24 15:09 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> Note that interactive and non-interactive use are not
>> identical in one aspect ...
>
> Interactive is interacting with user as a command.
> Non-interactive is called from within lisp code.

Yes, interactive is a command, M-x CMD RET, or a keybinding.

Non-interactive is from Lisp ...

(`call-interactively' is the exception that proves the rule,
and it should only be used when ... there is a reason to :))

But that's not what I refered to actually, if you study the
code you see the "nil t" that you asked about in the
other thread -

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

- and if you take a look at the docstring of `completion-read'
you see that these arguments are for the

  &optional PREDICATE REQUIRE-MATCH

so the first nil says PREDICATE should be nil, it is already
nil by default but we want to say that REQUIRE-MATCH should be
t, so we say that PREDICATE is nil just to get to
REQUIRE-MATCH if you follow, then we say it is t.

And this is the difference from non-interactive use, because
there is no such thing in the non-interactive part of the
function. You can send any "flag" argument from Lisp and it
will be processed.

(atomic-position "darn")

But M-x atomic-position RET darn RET ... try it.

Read (or look at) the docstrings, especially the
interface/prototype part, to all functions you use ...

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




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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-24 15:09       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-10-26  2:38         ` Hongyi Zhao
  2021-10-26  5:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 9+ messages in thread
From: Hongyi Zhao @ 2021-10-26  2:38 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Sun, Oct 24, 2021 at 11:09 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> >> Note that interactive and non-interactive use are not
> >> identical in one aspect ...
> >
> > Interactive is interacting with user as a command.
> > Non-interactive is called from within lisp code.
>
> Yes, interactive is a command, M-x CMD RET, or a keybinding.
>
> Non-interactive is from Lisp ...
>
> (`call-interactively' is the exception that proves the rule,
> and it should only be used when ... there is a reason to :))
>
> But that's not what I refered to actually, if you study the
> code you see the "nil t" that you asked about in the
> other thread -
>
> (defun atomic-position (flag)
>   (interactive
>    (list
>     (completing-read
>      "flag: " '("alat" "angstrom" "bohr" "crystal" "crystal_sg") nil t) ))
>   (insert (format "Atomic position: %s\n" flag)) )
>
> - and if you take a look at the docstring of `completion-read'
> you see that these arguments are for the
>
>   &optional PREDICATE REQUIRE-MATCH
>
> so the first nil says PREDICATE should be nil, it is already
> nil by default

What exactly does nil mean here?

The document says the following:

PREDICATE limits completion to a subset of COLLECTION.


> but we want to say that REQUIRE-MATCH should be
> t, so we say that PREDICATE is nil just to get to
> REQUIRE-MATCH if you follow, then we say it is t.
>
> And this is the difference from non-interactive use, because
> there is no such thing in the non-interactive part of the
> function. You can send any "flag" argument from Lisp and it
> will be processed.
>
> (atomic-position "darn")

(atomic-position "darn") ;; Atomic position: darn

> But M-x atomic-position RET darn RET ... try it.

0 flag (match required): darn

> Read (or look at) the docstrings, especially the
> interface/prototype part, to all functions you use ...

The lisp documentation is very obscure, and I often cannot grasp them
the first time I read it.

HZ



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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-26  2:38         ` Hongyi Zhao
@ 2021-10-26  5:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-26  5:22             ` Hongyi Zhao
  0 siblings, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-26  5:14 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> PREDICATE limits completion to a subset of COLLECTION.

nil predicate => no limits to the completion, complete to all
of COLLECTION.

Or the sky is the limit ...

https://dataswamp.org/~incal/blog/tree-house/tree-house-pulley.html

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




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

* Re: Insert one of the flags automatically with tab completion.
  2021-10-26  5:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-10-26  5:22             ` Hongyi Zhao
  0 siblings, 0 replies; 9+ messages in thread
From: Hongyi Zhao @ 2021-10-26  5:22 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Tue, Oct 26, 2021 at 1:15 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > PREDICATE limits completion to a subset of COLLECTION.
>
> nil predicate => no limits to the completion, complete to all
> of COLLECTION.

Got it.

> Or the sky is the limit ...
>
> https://dataswamp.org/~incal/blog/tree-house/tree-house-pulley.html

What's the relationship between this and the topic in question?

HZ



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

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

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-10-23  5:07 Insert one of the flags automatically with tab completion Hongyi Zhao
2021-10-23  6:10 ` Hongyi Zhao
2021-10-23 19:23   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-24 14:50     ` Hongyi Zhao
2021-10-24 15:09       ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26  2:38         ` Hongyi Zhao
2021-10-26  5:14           ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26  5:22             ` Hongyi Zhao
2021-10-24 14:53     ` Hongyi Zhao

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.