[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Persistence of variables

From: Marcin Borkowski
Subject: Re: Persistence of variables
Date: Wed, 21 Mar 2018 10:13:11 +0100
User-agent: mu4e 1.1.0; emacs 27.0.50

On 2018-03-21, at 08:25, Marcin Borkowski <> wrote:

> On 2018-03-21, at 07:55, Eli Zaretskii <> wrote:
>>> From: Marcin Borkowski <>
>>> Date: Wed, 21 Mar 2018 07:08:44 +0100
>>> I want to make a variable persistent across Emacs sessions.  How do I do
>>> it?
>> Did you consider desktop.el?
> No, I did not remember about its existence.
> But no, way too heavy.  I want _one variable_ to persist, and nothing
> more.

OK, how about this?

--8<---------------cut here---------------start------------->8---
(defun make-variable-persistent (variable-name)
  "Save VARIABLE-NAME to `custom-file' or `init-file'."
  (when (y-or-n-p (format "I am going to save variable `%s' in %s.  Should I 
proceed? " variable-name user-init-file))
    (with-temp-file user-init-file
      (let ((sentinel-text (format "\n;; Persistent variable `%s'.  Do not edit 
manually!\n" variable-name)))
        (insert-file-contents user-init-file)
        (if (search-forward sentinel-text nil t)
            (if (not (looking-at-p (format "^(setq %s" variable-name)))
                (error "Broken init file.")
          (goto-char (point-max))
          (insert sentinel-text)))
      (insert (format "(setq %s %S)" variable-name (symbol-value 
--8<---------------cut here---------------end--------------->8---

Any thoughts/suggestions?

Marcin Borkowski

reply via email to

[Prev in Thread] Current Thread [Next in Thread]