aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2021-12-12 08:43:32 +0100
committerLars Ingebrigtsen2021-12-12 08:43:32 +0100
commit39ca2b45f06c95e5a9c07cb87a0fa8dfbe050bd6 (patch)
tree5387f1b31cbc241991e87685c7a935d2ec87d37f
parent1b2a8e08e29a83481314e7fe2e6a4d60175a1a5d (diff)
downloademacs-39ca2b45f06c95e5a9c07cb87a0fa8dfbe050bd6.tar.gz
emacs-39ca2b45f06c95e5a9c07cb87a0fa8dfbe050bd6.zip
Document multisession variables
-rw-r--r--doc/lispref/elisp.texi1
-rw-r--r--doc/lispref/variables.texi88
-rw-r--r--lisp/emacs-lisp/multisession.el2
3 files changed, 90 insertions, 1 deletions
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index b773ba8fb9e..7bb209103db 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -526,6 +526,7 @@ Variables
526* Variables with Restricted Values:: Non-constant variables whose value can 526* Variables with Restricted Values:: Non-constant variables whose value can
527 @emph{not} be an arbitrary Lisp object. 527 @emph{not} be an arbitrary Lisp object.
528* Generalized Variables:: Extending the concept of variables. 528* Generalized Variables:: Extending the concept of variables.
529* Multisession Variables:: Variables that survive restarting Emacs.
529 530
530Scoping Rules for Variable Bindings 531Scoping Rules for Variable Bindings
531 532
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index abef0b3d0c6..844d1253a61 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -44,6 +44,7 @@ representing the variable.
44* Variables with Restricted Values:: Non-constant variables whose value can 44* Variables with Restricted Values:: Non-constant variables whose value can
45 @emph{not} be an arbitrary Lisp object. 45 @emph{not} be an arbitrary Lisp object.
46* Generalized Variables:: Extending the concept of variables. 46* Generalized Variables:: Extending the concept of variables.
47* Multisession Variables:: Variables that survive restarting Emacs.
47@end menu 48@end menu
48 49
49@node Global Variables 50@node Global Variables
@@ -2752,3 +2753,90 @@ form that has not already had an appropriate expansion defined. In
2752Common Lisp, this is not an error since the function @code{(setf 2753Common Lisp, this is not an error since the function @code{(setf
2753@var{func})} might be defined later. 2754@var{func})} might be defined later.
2754@end quotation 2755@end quotation
2756
2757@node Multisession Variables
2758@section Multisession Variables
2759
2760@cindex multisession variable
2761 When you set a variable to a value and then close Emacs and restart
2762Emacs, this value won't be automatically restored. Users usually set
2763normal variables in their startup files, or use Customize to set a
2764user option permanently, and various packages have various files that
2765they store the data in (Gnus stores this in @file{.newsrc.eld} and the
2766URL library stores cookies in @file{~/.emacs.d/url/cookies}.
2767
2768For things in between these two extremes (i.e., configuration which
2769goes in the startup file, and massive application state that goes into
2770separate files), Emacs provides a facility to replicate data between
2771sessions called @dfn{multisession variables}. To give you an idea of
2772how these are meant to be used, here's a small example:
2773
2774@lisp
2775(define-multisession-variable foo-var 0)
2776(defun my-adder (num)
2777 (interactive "nAdd number: ")
2778 (setf (multisession-value foo)
2779 (+ (multisession-value foo) num))
2780 (message "The new number is: %s" (multisession-value foo)))
2781@end lisp
2782
2783This defines the variable @code{foo-var} and binds it to a special
2784multisession object which is initialized with the value @samp{0} (if
2785it doesn't already exist from a previous session). The
2786@code{my-adder} function queries the user for a number, adds this to
2787the old (possibly saved value), and then saves the new value.
2788
2789This facility isn't meant to be used for huge data structures, but
2790should be reasonably performant for most values.
2791
2792@defmac define-multisession-variable name initial-value &optional doc &rest args
2793This macro defines @var{name} as a multisession variable, with using
2794@var{initial-value} is this variable hasn't been stored earlier.
2795@var{doc} is the doc string, and some keyword arguments are possible:
2796
2797@table @code
2798@item :package symbol
2799This keyword says what package a multisession variable belongs to.
2800The combination of @var{package} and @var{name} has to be unique. If
2801@var{package} isn't given, this will default to the first ``section''
2802of the @var{name} symbol name. For instance, if @var{name} is
2803@code{foo-var} and @var{package} isn't given, @var{package} will
2804default to @code{foo}.
2805
2806@item :synchronized bool
2807By default, multisession variables are @dfn{synchronized}. This means
2808that if there's two concurrent Emacs instances running, and the other
2809Emacs changes the multisession variable @code{foo-var}, the current
2810Emacs instance will retrieve that data when accessing the value. If
2811@var{synchronized} is @code{nil}, this won't happen, and the variable
2812in all Emacs sessions will be independent.
2813@end table
2814@end defmac
2815
2816@defun multisession-value variable
2817This function returns the current value of @var{variable}. If this
2818variable hasn't been accessed before in this Emacs session, or if it's
2819changed externally, it will be read in from external storage. If not,
2820the current value in this session is returned as is.
2821
2822Values retrieved via @code{multisession-value} may or may not be
2823@code{eq} to each other, but they will always be @code{equal}.
2824
2825This is a generalized variable (@pxref{Generalized Variables}), so the
2826way to update a variable is to say, for instance:
2827
2828@lisp
2829(setf (multisession-value foo-bar) 'zot)
2830@end lisp
2831
2832If the multisession variable is synchronized, setting it may update
2833the value. For instance:
2834
2835@lisp
2836(incf (multisession-value foo-bar))
2837@end lisp
2838
2839This will first check whether the value has changed in a different
2840Emacs instance, retrieve that value, and then add 1 to that value, and
2841then store it.
2842@end defun
diff --git a/lisp/emacs-lisp/multisession.el b/lisp/emacs-lisp/multisession.el
index eb13c2ef53c..9fefb8afd2c 100644
--- a/lisp/emacs-lisp/multisession.el
+++ b/lisp/emacs-lisp/multisession.el
@@ -28,7 +28,7 @@
28(require 'sqlite) 28(require 'sqlite)
29 29
30(defcustom multisession-database-file 30(defcustom multisession-database-file
31 (expand-file-name "multisession.sqlite3" user-emacs-directory) 31 (expand-file-name "multisession.sqlite" user-emacs-directory)
32 "File to store multisession variables." 32 "File to store multisession variables."
33 :type 'file 33 :type 'file
34 :version "29.1" 34 :version "29.1"