unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to store list of variable values into another dynamically created variables
@ 2021-01-04 14:51 miles christopher
  2021-01-05 18:24 ` Leo Butler
  0 siblings, 1 reply; 6+ messages in thread
From: miles christopher @ 2021-01-04 14:51 UTC (permalink / raw)
  To: Emacs Help


I want to store some variables' value into another minor mode temporary variable
for later restore.

Here is my Elisp code, I don't know how to implement it in FIXME and TODO. Hope
some Elisp master help me.

#+begin_src emacs-lisp
(defvar mu4e-marker-icons-marker-list
  '(mu4e-headers-seen-mark
    mu4e-headers-new-mark
    mu4e-headers-unread-mark
    mu4e-headers-signed-mark
    mu4e-headers-encrypted-mark
    mu4e-headers-draft-mark
    mu4e-headers-attach-mark
    mu4e-headers-passed-mark
    mu4e-headers-flagged-mark
    mu4e-headers-replied-mark
    mu4e-headers-trashed-mark
    ;; thread prefix marks
    mu4e-headers-default-prefix
    mu4e-headers-has-child-prefix
    mu4e-headers-empty-parent-prefix
    mu4e-headers-first-child-prefix
    mu4e-headers-duplicate-prefix)
  "A list of markers used in mu4e.")

(defun mu4e-marker-icons--store ()
  "Store user old config."
  (mapcar
   (lambda (marker-variable)
     ;; FIXME:
     `(setq
       ,(intern (replace-regexp-in-string
                 "mu4e-headers-" "mu4e-marker-icons--"
                 (symbol-name marker-variable)))
       ,(symbol-value marker-variable))
     )
   mu4e-marker-icons-marker-list))

(defun mu4e-marker-icons--restore ()
  "Restore user old config."
  ;; TODO:
  (mapcar
   (lambda (marker-variable)
     ;; FIXME:
     `(setq
       marker-variable
       ,(symbol-value (intern (replace-regexp-in-string
                               "mu4e-headers-" "mu4e-marker-icons--"
                               (symbol-name marker-variable))))))
   mu4e-marker-icons-marker-list))
#+end_src

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



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

* Re: How to store list of variable values into another dynamically created variables
  2021-01-04 14:51 How to store list of variable values into another dynamically created variables miles christopher
@ 2021-01-05 18:24 ` Leo Butler
  2021-01-05 19:38   ` Stefan Monnier
  2021-01-06  2:44   ` miles christopher
  0 siblings, 2 replies; 6+ messages in thread
From: Leo Butler @ 2021-01-05 18:24 UTC (permalink / raw)
  To: Emacs Help

miles christopher <numbchild@gmail.com> writes:

> I want to store some variables' value into another minor mode temporary variable
> for later restore.
>
> Here is my Elisp code, I don't know how to implement it in FIXME and TODO. Hope
> some Elisp master help me.
>
> #+begin_src emacs-lisp
> (defvar mu4e-marker-icons-marker-list
>   '(mu4e-headers-seen-mark
>     mu4e-headers-new-mark
>     mu4e-headers-unread-mark
>     mu4e-headers-signed-mark
>     mu4e-headers-encrypted-mark
>     mu4e-headers-draft-mark
>     mu4e-headers-attach-mark
>     mu4e-headers-passed-mark
>     mu4e-headers-flagged-mark
>     mu4e-headers-replied-mark
>     mu4e-headers-trashed-mark
>     ;; thread prefix marks
>     mu4e-headers-default-prefix
>     mu4e-headers-has-child-prefix
>     mu4e-headers-empty-parent-prefix
>     mu4e-headers-first-child-prefix
>     mu4e-headers-duplicate-prefix)
>   "A list of markers used in mu4e.")
>
> (defun mu4e-marker-icons--store ()
>   "Store user old config."
>   (mapcar
>    (lambda (marker-variable)
>      ;; FIXME:
>      `(setq
>        ,(intern (replace-regexp-in-string
>                  "mu4e-headers-" "mu4e-marker-icons--"
>                  (symbol-name marker-variable)))
>        ,(symbol-value marker-variable))
>      )
>    mu4e-marker-icons-marker-list))
>
> (defun mu4e-marker-icons--restore ()
>   "Restore user old config."
>   ;; TODO:
>   (mapcar
>    (lambda (marker-variable)
>      ;; FIXME:
>      `(setq
>        marker-variable
>        ,(symbol-value (intern (replace-regexp-in-string
>                                "mu4e-headers-" "mu4e-marker-icons--"
>                                (symbol-name marker-variable))))))
>    mu4e-marker-icons-marker-list))
> #+end_src

Since you are trying to rename the variables, why not start with a list
of conses (old . new) rather than a list of variable names?

#+begin_src emacs-lisp
(defvar mu4e-marker-icons-marker-list
  '((mu4e-headers-seen-mark . mu4e-saved-headers-seen-mark)
    (mu4e-headers-new-mark . mu4e-saved-headers-new-mark)
    (mu4e-headers-unread-mark . mu4e-saved-headers-unread-mark)
    (mu4e-headers-signed-mark . mu4e-saved-headers-signed-mark)
    (mu4e-headers-encrypted-mark . mu4e-saved-headers-encrypted-mark)
    (mu4e-headers-draft-mark . mu4e-saved-headers-draft-mark)
    (mu4e-headers-attach-mark . mu4e-saved-headers-attach-mark)
    (mu4e-headers-passed-mark . mu4e-saved-headers-passed-mark)
    (mu4e-headers-flagged-mark . mu4e-saved-headers-flagged-mark)
    (mu4e-headers-replied-mark . mu4e-saved-headers-replied-mark)
    (mu4e-headers-trashed-mark . mu4e-saved-headers-trashed-mark)
    ;; thread prefix marks
    (mu4e-headers-default-prefix . mu4e-saved-headers-default-prefix)
    (mu4e-headers-has-child-prefix . mu4e-saved-headers-has-child-prefix)
    (mu4e-headers-empty-parent-prefix . mu4e-saved-headers-empty-parent-prefix)
    (mu4e-headers-first-child-prefix . mu4e-saved-headers-first-child-prefix)
    (mu4e-headers-duplicate-prefix . mu4e-saved-headers-duplicate-prefix))
  "A list of markers used in mu4e.")

#+end_src

Your save and restore functions are then dead easy:

#+begin_src emacs-lisp
(defun mu4e-save (l)
       (mapcar (lambda(x) (set (cdr x) (symbol-value (car x)))) l))

(defun mu4e-restore (l)
 (let ((lrev (mapcar (lambda(x) (cons (cdr x) (car x))) l)))
       (mu4e-save lrev)))

#+end_src

Leo



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

* Re: How to store list of variable values into another dynamically created variables
  2021-01-05 18:24 ` Leo Butler
@ 2021-01-05 19:38   ` Stefan Monnier
  2021-01-06  2:47     ` miles christopher
  2021-01-06  2:44   ` miles christopher
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2021-01-05 19:38 UTC (permalink / raw)
  To: help-gnu-emacs

> Your save and restore functions are then dead easy:
>
> #+begin_src emacs-lisp
> (defun mu4e-save (l)
>        (mapcar (lambda(x) (set (cdr x) (symbol-value (car x)))) l))
>
> (defun mu4e-restore (l)
>  (let ((lrev (mapcar (lambda(x) (cons (cdr x) (car x))) l)))
>        (mu4e-save lrev)))

FWIW, in most of the minor modes where I've needed such a thing, I found
it more convenient to use a single "save" variable, e.g. something like:

    (defvar FOO--saved-vals nil)

    (define-minor-mode FOO-mode
      "blabla FOO blabla"
      ;; Revert to original values.
      (dolist (x (prog1 FOO--saved-vals (setq FOO--saved-vals nil)))
        (if (consp x)
            (set (car x) (cdr x))
          (mkunbound x)))
      ;; Set minor mode's values.
      (when FOO-mode
        (setq FOO--saved-vals
              (mapcar (lambda (v)
                        (if (boundp v) (cons v (symbol-value v)) v))
                      '(mu4e-headers-seen-mark
                        ...
                        mu4e-headers-duplicate-prefix)))
        ..etc..))


-- Stefan




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

* Re: How to store list of variable values into another dynamically created variables
  2021-01-05 18:24 ` Leo Butler
  2021-01-05 19:38   ` Stefan Monnier
@ 2021-01-06  2:44   ` miles christopher
  1 sibling, 0 replies; 6+ messages in thread
From: miles christopher @ 2021-01-06  2:44 UTC (permalink / raw)
  To: Leo Butler; +Cc: help-gnu-emacs@gnu.org


This seems a good solution for my purpose. I'm also interested in Stefan's
implementation. I will try both.

Thanks Leo.

Leo Butler <leo.butler@umanitoba.ca> writes:

> miles christopher <numbchild@gmail.com> writes:
>
>> I want to store some variables' value into another minor mode temporary variable
>> for later restore.
>>
>> Here is my Elisp code, I don't know how to implement it in FIXME and TODO. Hope
>> some Elisp master help me.
>>
>> #+begin_src emacs-lisp
>> (defvar mu4e-marker-icons-marker-list
>>   '(mu4e-headers-seen-mark
>>     mu4e-headers-new-mark
>>     mu4e-headers-unread-mark
>>     mu4e-headers-signed-mark
>>     mu4e-headers-encrypted-mark
>>     mu4e-headers-draft-mark
>>     mu4e-headers-attach-mark
>>     mu4e-headers-passed-mark
>>     mu4e-headers-flagged-mark
>>     mu4e-headers-replied-mark
>>     mu4e-headers-trashed-mark
>>     ;; thread prefix marks
>>     mu4e-headers-default-prefix
>>     mu4e-headers-has-child-prefix
>>     mu4e-headers-empty-parent-prefix
>>     mu4e-headers-first-child-prefix
>>     mu4e-headers-duplicate-prefix)
>>   "A list of markers used in mu4e.")
>>
>> (defun mu4e-marker-icons--store ()
>>   "Store user old config."
>>   (mapcar
>>    (lambda (marker-variable)
>>      ;; FIXME:
>>      `(setq
>>        ,(intern (replace-regexp-in-string
>>                  "mu4e-headers-" "mu4e-marker-icons--"
>>                  (symbol-name marker-variable)))
>>        ,(symbol-value marker-variable))
>>      )
>>    mu4e-marker-icons-marker-list))
>>
>> (defun mu4e-marker-icons--restore ()
>>   "Restore user old config."
>>   ;; TODO:
>>   (mapcar
>>    (lambda (marker-variable)
>>      ;; FIXME:
>>      `(setq
>>        marker-variable
>>        ,(symbol-value (intern (replace-regexp-in-string
>>                                "mu4e-headers-" "mu4e-marker-icons--"
>>                                (symbol-name marker-variable))))))
>>    mu4e-marker-icons-marker-list))
>> #+end_src
>
> Since you are trying to rename the variables, why not start with a list
> of conses (old . new) rather than a list of variable names?
>
> #+begin_src emacs-lisp
> (defvar mu4e-marker-icons-marker-list
>   '((mu4e-headers-seen-mark . mu4e-saved-headers-seen-mark)
>     (mu4e-headers-new-mark . mu4e-saved-headers-new-mark)
>     (mu4e-headers-unread-mark . mu4e-saved-headers-unread-mark)
>     (mu4e-headers-signed-mark . mu4e-saved-headers-signed-mark)
>     (mu4e-headers-encrypted-mark . mu4e-saved-headers-encrypted-mark)
>     (mu4e-headers-draft-mark . mu4e-saved-headers-draft-mark)
>     (mu4e-headers-attach-mark . mu4e-saved-headers-attach-mark)
>     (mu4e-headers-passed-mark . mu4e-saved-headers-passed-mark)
>     (mu4e-headers-flagged-mark . mu4e-saved-headers-flagged-mark)
>     (mu4e-headers-replied-mark . mu4e-saved-headers-replied-mark)
>     (mu4e-headers-trashed-mark . mu4e-saved-headers-trashed-mark)
>     ;; thread prefix marks
>     (mu4e-headers-default-prefix . mu4e-saved-headers-default-prefix)
>     (mu4e-headers-has-child-prefix . mu4e-saved-headers-has-child-prefix)
>     (mu4e-headers-empty-parent-prefix . mu4e-saved-headers-empty-parent-prefix)
>     (mu4e-headers-first-child-prefix . mu4e-saved-headers-first-child-prefix)
>     (mu4e-headers-duplicate-prefix . mu4e-saved-headers-duplicate-prefix))
>   "A list of markers used in mu4e.")
>
> #+end_src
>
> Your save and restore functions are then dead easy:
>
> #+begin_src emacs-lisp
> (defun mu4e-save (l)
>        (mapcar (lambda(x) (set (cdr x) (symbol-value (car x)))) l))
>
> (defun mu4e-restore (l)
>  (let ((lrev (mapcar (lambda(x) (cons (cdr x) (car x))) l)))
>        (mu4e-save lrev)))
>
> #+end_src
>
> Leo


-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



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

* Re: How to store list of variable values into another dynamically created variables
  2021-01-05 19:38   ` Stefan Monnier
@ 2021-01-06  2:47     ` miles christopher
  2021-01-06  3:01       ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: miles christopher @ 2021-01-06  2:47 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs@gnu.org


Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Your save and restore functions are then dead easy:
>>
>> #+begin_src emacs-lisp
>> (defun mu4e-save (l)
>>        (mapcar (lambda(x) (set (cdr x) (symbol-value (car x)))) l))
>>
>> (defun mu4e-restore (l)
>>  (let ((lrev (mapcar (lambda(x) (cons (cdr x) (car x))) l)))
>>        (mu4e-save lrev)))
>
> FWIW, in most of the minor modes where I've needed such a thing, I found
> it more convenient to use a single "save" variable, e.g. something like:
>
>     (defvar FOO--saved-vals nil)
>
>     (define-minor-mode FOO-mode
>       "blabla FOO blabla"
>       ;; Revert to original values.
>       (dolist (x (prog1 FOO--saved-vals (setq FOO--saved-vals nil)))
>         (if (consp x)
>             (set (car x) (cdr x))
>           (mkunbound x)))
>       ;; Set minor mode's values.
>       (when FOO-mode
>         (setq FOO--saved-vals
>               (mapcar (lambda (v)
>                         (if (boundp v) (cons v (symbol-value v)) v))
>                       '(mu4e-headers-seen-mark
>                         ...
>                         mu4e-headers-duplicate-prefix)))
>         ..etc..))
>
>
> -- Stefan

Maybe can provide a simple package for this purpose? And include it in Emacs /
GNU ELPA will be available for some package developers. WDYT?

Indeed, because some minor modes are based on another mode. As a minor mode,
should provide a mechanism to store and restore original settings. Of course,
not provided is also acceptable.

-- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3



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

* Re: How to store list of variable values into another dynamically created variables
  2021-01-06  2:47     ` miles christopher
@ 2021-01-06  3:01       ` Stefan Monnier
  0 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2021-01-06  3:01 UTC (permalink / raw)
  To: help-gnu-emacs

> Maybe can provide a simple package for this purpose? And include it in Emacs /
> GNU ELPA will be available for some package developers. WDYT?

I won't be the one stopping you,


        Stefan




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

end of thread, other threads:[~2021-01-06  3:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-01-04 14:51 How to store list of variable values into another dynamically created variables miles christopher
2021-01-05 18:24 ` Leo Butler
2021-01-05 19:38   ` Stefan Monnier
2021-01-06  2:47     ` miles christopher
2021-01-06  3:01       ` Stefan Monnier
2021-01-06  2:44   ` miles christopher

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