I’m wondering if there’s any way to make a variable automatically save its old value, so that I can, e.g., write a minor mode that will automatically restore the old values of variables when it’s deactivated (without having to declare a bunch of -old-value variables).

  • 7890yuiop@alien.topB
    link
    fedilink
    English
    arrow-up
    2
    ·
    10 months ago

    You don’t have to declare a new variable each time you want to remember an old value. You could, e.g., put a custom symbol property on the variable symbol with the old value (or list of old values) you wanted to store. Or maintain a single variable mapping all your variable symbols to their old values.

  • Qudit314159@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    10 months ago

    The function add-variable-watcher registers a callback that is invoked whenever the variable’s value changes. You could use it to save old values in a list or other data structure.

  • JDRiverRun@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    10 months ago

    A variable watcher maybe.

        (add-variable-watcher
         'myvar
         (lambda (sym val op buf)
           (when (eq op 'set)
             (push val (get sym 'prior-values)))))
    
  • arthurno1@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    10 months ago

    There is no built-in automatic way; you will have to backup your old values any way you want; create a bunch of new symbols, put them into a hash table, a list, or whichever else strategy you prefer.