1 ;;; symbols.el --- functions for working with symbols and symbol values
3 ;; Copyright (C) 1996 Ben Wing.
5 ;; Maintainer: XEmacs Development Team
8 ;; This file is part of XEmacs.
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Synched up with: Not in FSF.
29 ;; Not yet dumped into XEmacs.
31 ;; The idea behind magic variables is that you can specify arbitrary
32 ;; behavior to happen when setting or retrieving a variable's value. The
33 ;; purpose of this is to make it possible to cleanly provide support for
34 ;; obsolete variables (e.g. unread-command-event, which is obsolete for
35 ;; unread-command-events) and variable compatibility
36 ;; (e.g. suggest-key-bindings, the FSF equivalent of
37 ;; teach-extended-commands-p and teach-extended-commands-timeout).
39 ;; There are a large number of functions pertaining to a variable's
49 ;; set-default / setq-default
50 ;; make-variable-buffer-local
51 ;; make-local-variable
52 ;; kill-local-variable
53 ;; kill-console-local-variable
54 ;; symbol-value-in-buffer
55 ;; symbol-value-in-console
56 ;; local-variable-p / local-variable-if-set-p
58 ;; Plus some "meta-functions":
64 ;; I wanted an implementation that:
66 ;; -- would work with all the above functions, but (a) didn't require
67 ;; a separate handler for every function, and (b) would work OK
68 ;; even if more functions are added (e.g. `set-symbol-value-in-buffer'
69 ;; or `makunbound-default') or if more arguments are added to a
71 ;; -- avoided consing if at all possible.
72 ;; -- didn't slow down operations on non-magic variables (therefore,
73 ;; storing the magic information using `put' is ruled out).
78 ;; perhaps this should check whether the functions are bound, so that
79 ;; some handlers can be unspecified. That requires that all functions
80 ;; are defined before `define-magic-variable-handlers' is called,
83 ;; perhaps there should be something that combines
84 ;; `define-magic-variable-handlers' with `defvaralias'.
86 (defun define-magic-variable-handlers (variable handler-class harg)
87 "Set the magic variable handles for VARIABLE to those in HANDLER-CLASS.
88 HANDLER-CLASS should be a symbol. The handlers are constructed by adding
89 the handler type to HANDLER-CLASS. HARG is passed as the HARG value for
90 each of the handlers."
93 (set-magic-variable-handler variable htype
94 (intern (concat (symbol-value handler-class)
96 (symbol-value htype)))
98 '(get-value set-value other-predicate other-action)))
100 ;; unread-command-event
102 (defun mvh-first-of-list-get-value (sym fun args harg)
103 (car (apply fun harg args)))
105 (defun mvh-first-of-list-set-value (sym value setfun getfun args harg)
106 (apply setfun harg (cons value (apply getfun harg args)) args))
108 (defun mvh-first-of-list-other-predicate (sym fun args harg)
109 (apply fun harg args))
111 (defun mvh-first-of-list-other-action (sym fun args harg)
112 (apply fun harg args))
114 (define-magic-variable-handlers 'unread-command-event
116 'unread-command-events)
118 ;; last-command-char, last-input-char, unread-command-char
120 (defun mvh-char-to-event-get-value (sym fun args harg)
121 (event-to-character (apply fun harg args)))
123 (defun mvh-char-to-event-set-value (sym value setfun getfun args harg)
124 (let ((event (apply getfun harg args)))
125 (if (event-live-p event)
127 (setq event (make-event))
128 (apply setfun harg event args))
129 (character-to-event value event)))
131 (defun mvh-char-to-event-other-predicate (sym fun args harg)
132 (apply fun harg args))
134 (defun mvh-char-to-event-other-action (sym fun args harg)
135 (apply fun harg args))
137 (define-magic-variable-handlers 'last-command-char
141 (define-magic-variable-handlers 'last-input-char
145 (define-magic-variable-handlers 'unread-command-char
147 'unread-command-event)
149 ;; suggest-key-bindings
151 (set-magic-variable-handler
152 'suggest-key-bindings 'get-value
153 #'(lambda (sym fun args harg)
154 (and (apply fun 'teach-extended-commands-p args)
155 (apply fun 'teach-extended-commands-timeout args))))
157 (set-magic-variable-handler
158 'suggest-key-bindings 'set-value
159 #'(lambda (sym value setfun getfun args harg)
160 (apply setfun 'teach-extended-commands-p (not (null value)) args)
162 (apply 'teach-extended-commands-timeout
163 (if (numberp value) value 2) args))))
165 (set-magic-variable-handler
166 'suggest-key-bindings 'other-action
167 #'(lambda (sym fun args harg)
168 (apply fun 'teach-extended-commands-p args)
169 (apply fun 'teach-extended-commands-timeout args)))
171 (set-magic-variable-handler
172 'suggest-key-bindings 'other-predicate
173 #'(lambda (sym fun args harg)
174 (and (apply fun 'teach-extended-commands-p args)
175 (apply fun 'teach-extended-commands-timeout args))))
177 ;;; symbols.el ends here