all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Robert Thorpe <rt@robertthorpeconsulting.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: [SOLVED with `eval']: Why I cannot use this variable in macro call from function?
Date: Fri, 11 Jun 2021 10:03:25 +0300	[thread overview]
Message-ID: <YMMKvTNo+wBIQR/V@protected.localdomain> (raw)
In-Reply-To: <87lf7g9ahl.fsf@robertthorpeconsulting.com>

* Robert Thorpe <rt@robertthorpeconsulting.com> [2021-06-11 09:34]:
> Jean Louis <bugs@gnu.support> writes:
> 
> > Definitely good idea and now I was thinking there is some description
> > pointing to it and have looked into `completing-read' and
> > `read-from-minibuffer' and found nothing, I have to provide a variable
> > or cons with variable to those functions. It is good idea definitely,
> > but show me the practical way how to use alist or hash in those
> > functions.
> 
> AFAICT, What you need to do is to populate a variable from the
> alist.  Then pass that variable to to `completing-read' or
> `read-from-minibuffer'.

I got the idea. But then... is that history just temporary or retained
over sessions? Common history variables are retained in
~/.emacs.d/history 

> I'm not sure what you mean here.  In one global alist you could put lots
> and lots of information.  There could be an entry for each history list
> that you need.  I don't see why you need to pass around functions
> here.

I do understand the idea that one history pre-variable would be used
to keep history for the generated on the fly modified history variable
that is used in `read-from-minibuffer', but then it will probably not
be retained and it would be vague, unclear to where such history
belongs. Additionally I would need to find way to keep the alist
variable somewhere stored as well.

Idea is understandable, but way too complicated.

We better conclude that the issue is solved and it works well without
problems. Using `eval' is there for a reason.

The snippet below solves it. Shorter solution is useful, if you have
one: 

(defun rcd-symbol-if-not-exist (variable &optional value description)
  "Return symbol for VARIABLE string. 

It will generate new VARIABLE if it does not exist."
  (let ((rcd-symbol (intern variable))
	(description (or description (format "Generated variable `%s'" variable))))
    (if (boundp rcd-symbol)
	rcd-symbol
      (eval (list 'defvar rcd-symbol value description)))))

(rcd-symbol-if-not-exist 
 "my-new-symbol" "Value here" "Some description")

⇒ my-new-symbol ⇒ "Value here"

(setq my-new-symbol 10) ⇒ 10

my-new-symbol ⇒ 10

It is handy that it will not change the existing value of the
variable, just return a symbol or generate it:

(rcd-symbol-if-not-exist 
 "my-new-symbol" "Value here" "Some description") ⇒ my-new-symbol ⇒ 10

for history it is then very useful to dynamically generate
history variables:

(let* ((name "Streets")
       (history (concat name "-history")))
  (read-from-minibuffer "Street: " nil nil nil (rcd-symbol-if-not-exist history)))

Streets-history ⇒ ("Elm Street")

It will be saved in ~/.emacs.d/history as:

(setq Streets-history '("Elm Street"))

User has to think of how such history variables should be called
as not to collide with others.


> > It works this way:
> >
> > (let ((history '("One" "Two" "Three")))
> >   (read-from-minibuffer "Tell me: " nil nil nil 'history))
> >
> > Or this way:
> >
> > (let ((history '("One" "Two" "Three")))
> >   (read-from-minibuffer "Tell me: " nil nil nil '(history . 1)))
> 
> None of those three work for me.  That is, none give a history list.

On my side these below work:

(let ((history '("One" "Two" "Three")))
  (read-from-minibuffer "Tell me: " nil nil nil 'history))

(let ((history '("One" "Two" "Three" "Four")))
  (read-from-minibuffer "Tell me: " nil nil nil '(history . 1)))

Press arrow up.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



      reply	other threads:[~2021-06-11  7:03 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-08 13:10 Why I cannot use this variable in macro call from function? Jean Louis
2021-06-08 13:41 ` [External] : " Drew Adams
2021-06-08 14:47   ` Jean Louis
2021-06-08 17:14   ` Jean Louis
2021-06-08 18:31     ` tomas
2021-06-08 18:37       ` Oops function? tomas
2021-06-08 19:27       ` [SOLVED with `eval']: Why I cannot use this variable in macro call from function? Jean Louis
2021-06-08 20:03         ` tomas
2021-06-08 20:06           ` Sorry again tomas
2021-06-08 20:12           ` [SOLVED with `eval']: Why I cannot use this variable in macro call from function? Jean Louis
2021-06-08 20:23             ` tomas
2021-06-08 20:38               ` Jean Louis
2021-06-08 20:47               ` Jean Louis
2021-06-09  6:09                 ` tomas
2021-06-09  6:42                   ` Jean Louis
2021-06-09  6:51                     ` tomas
2021-06-09  7:03                       ` Jean Louis
2021-06-09  7:39                         ` tomas
2021-06-09  8:22                           ` Jean Louis
2021-06-09  8:54                             ` tomas
2021-06-09 10:56                               ` Jean Louis
2021-06-09 11:33                                 ` tomas
2021-06-09 14:39                                   ` Jean Louis
2021-06-09 16:41                                     ` tomas
2021-06-10  2:10                                     ` Robert Thorpe
2021-06-10  6:56                                       ` Jean Louis
2021-06-11  6:33                                         ` Robert Thorpe
2021-06-11  7:03                                           ` Jean Louis [this message]

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

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

  git send-email \
    --in-reply-to=YMMKvTNo+wBIQR/V@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=rt@robertthorpeconsulting.com \
    /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.
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.