diff options
| author | Lars Ingebrigtsen | 2021-12-12 08:43:32 +0100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2021-12-12 08:43:32 +0100 |
| commit | 39ca2b45f06c95e5a9c07cb87a0fa8dfbe050bd6 (patch) | |
| tree | 5387f1b31cbc241991e87685c7a935d2ec87d37f | |
| parent | 1b2a8e08e29a83481314e7fe2e6a4d60175a1a5d (diff) | |
| download | emacs-39ca2b45f06c95e5a9c07cb87a0fa8dfbe050bd6.tar.gz emacs-39ca2b45f06c95e5a9c07cb87a0fa8dfbe050bd6.zip | |
Document multisession variables
| -rw-r--r-- | doc/lispref/elisp.texi | 1 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 88 | ||||
| -rw-r--r-- | lisp/emacs-lisp/multisession.el | 2 |
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 | ||
| 530 | Scoping Rules for Variable Bindings | 531 | Scoping 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 | |||
| 2752 | Common Lisp, this is not an error since the function @code{(setf | 2753 | Common 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 | ||
| 2762 | Emacs, this value won't be automatically restored. Users usually set | ||
| 2763 | normal variables in their startup files, or use Customize to set a | ||
| 2764 | user option permanently, and various packages have various files that | ||
| 2765 | they store the data in (Gnus stores this in @file{.newsrc.eld} and the | ||
| 2766 | URL library stores cookies in @file{~/.emacs.d/url/cookies}. | ||
| 2767 | |||
| 2768 | For things in between these two extremes (i.e., configuration which | ||
| 2769 | goes in the startup file, and massive application state that goes into | ||
| 2770 | separate files), Emacs provides a facility to replicate data between | ||
| 2771 | sessions called @dfn{multisession variables}. To give you an idea of | ||
| 2772 | how 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 | |||
| 2783 | This defines the variable @code{foo-var} and binds it to a special | ||
| 2784 | multisession object which is initialized with the value @samp{0} (if | ||
| 2785 | it doesn't already exist from a previous session). The | ||
| 2786 | @code{my-adder} function queries the user for a number, adds this to | ||
| 2787 | the old (possibly saved value), and then saves the new value. | ||
| 2788 | |||
| 2789 | This facility isn't meant to be used for huge data structures, but | ||
| 2790 | should be reasonably performant for most values. | ||
| 2791 | |||
| 2792 | @defmac define-multisession-variable name initial-value &optional doc &rest args | ||
| 2793 | This 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 | ||
| 2799 | This keyword says what package a multisession variable belongs to. | ||
| 2800 | The 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'' | ||
| 2802 | of the @var{name} symbol name. For instance, if @var{name} is | ||
| 2803 | @code{foo-var} and @var{package} isn't given, @var{package} will | ||
| 2804 | default to @code{foo}. | ||
| 2805 | |||
| 2806 | @item :synchronized bool | ||
| 2807 | By default, multisession variables are @dfn{synchronized}. This means | ||
| 2808 | that if there's two concurrent Emacs instances running, and the other | ||
| 2809 | Emacs changes the multisession variable @code{foo-var}, the current | ||
| 2810 | Emacs instance will retrieve that data when accessing the value. If | ||
| 2811 | @var{synchronized} is @code{nil}, this won't happen, and the variable | ||
| 2812 | in all Emacs sessions will be independent. | ||
| 2813 | @end table | ||
| 2814 | @end defmac | ||
| 2815 | |||
| 2816 | @defun multisession-value variable | ||
| 2817 | This function returns the current value of @var{variable}. If this | ||
| 2818 | variable hasn't been accessed before in this Emacs session, or if it's | ||
| 2819 | changed externally, it will be read in from external storage. If not, | ||
| 2820 | the current value in this session is returned as is. | ||
| 2821 | |||
| 2822 | Values retrieved via @code{multisession-value} may or may not be | ||
| 2823 | @code{eq} to each other, but they will always be @code{equal}. | ||
| 2824 | |||
| 2825 | This is a generalized variable (@pxref{Generalized Variables}), so the | ||
| 2826 | way to update a variable is to say, for instance: | ||
| 2827 | |||
| 2828 | @lisp | ||
| 2829 | (setf (multisession-value foo-bar) 'zot) | ||
| 2830 | @end lisp | ||
| 2831 | |||
| 2832 | If the multisession variable is synchronized, setting it may update | ||
| 2833 | the value. For instance: | ||
| 2834 | |||
| 2835 | @lisp | ||
| 2836 | (incf (multisession-value foo-bar)) | ||
| 2837 | @end lisp | ||
| 2838 | |||
| 2839 | This will first check whether the value has changed in a different | ||
| 2840 | Emacs instance, retrieve that value, and then add 1 to that value, and | ||
| 2841 | then 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" |