502
|
1 ;;; behavior.el --- consistent interface onto behaviors
|
|
2
|
800
|
3 ;; Copyright (C) 2000, 2001, 2002 Ben Wing.
|
502
|
4
|
|
5 ;; Author: Ben Wing
|
|
6 ;; Maintainer: XEmacs Development Team
|
|
7 ;; Keywords: internal, dumped
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Synched up with: Not in FSF.
|
|
27
|
|
28 ;;; Authorship:
|
|
29
|
|
30 ;; Created July 2000 by Ben Wing.
|
|
31
|
|
32 ;;; Commentary:
|
|
33
|
|
34 ;; This file will be dumped with XEmacs.
|
|
35
|
|
36 ;;; Code:
|
|
37
|
800
|
38 ;; Hash table mapping behavior names to property lists, with entries for
|
|
39 ;; :short-doc, :require, :enable, and :disable.
|
|
40 (defconst behavior-hash-table (make-hash-table))
|
|
41
|
|
42 (defvar within-behavior-enabling-disabling nil)
|
|
43
|
|
44 (defgroup behaviors nil
|
|
45 "Behaviors -- high-level functionality interface.")
|
|
46
|
|
47 ;; List of enabled behaviors.
|
|
48 (defcustom enabled-behavior-list nil
|
|
49 "List of currently enabled behaviors.
|
|
50 Normally, don't set it directly; use `enable-behavior' or `disable-behavior'."
|
|
51 :initialize #'set-default
|
|
52 :set #'(lambda (sym val)
|
|
53 (if within-behavior-enabling-disabling
|
|
54 (set sym val)
|
|
55 (let* ((old-val enabled-behavior-list)
|
|
56 (disable-list (set-difference old-val val))
|
|
57 (enable-list (set-difference val old-val)))
|
|
58 (dolist (b disable-list)
|
|
59 (disable-behavior b t))
|
|
60 (dolist (b enable-list)
|
|
61 (enable-behavior b t))
|
897
|
62 (assert (equal (sort (copy-sequence enabled-behavior-list) 'string-lessp)
|
|
63 (sort (copy-sequence val) 'string-lessp))))))
|
800
|
64 :type '(repeat (symbol :tag "Behavior"))
|
|
65 :group 'behaviors)
|
|
66
|
502
|
67
|
|
68 (defvar behavior-history nil
|
|
69 "History of entered behaviors.")
|
|
70
|
|
71 (defun define-behavior (name doc-string &rest cl-keys)
|
|
72 "Define a behavior named NAME.
|
|
73 DOC-STRING must be specified, a description of what the behavior does
|
|
74 when it's enabled and how to further control it (typically through
|
|
75 custom variables). Accepted keywords are
|
|
76
|
800
|
77 :short-doc A \"pretty\" version of the name, for use in menus. If omitted
|
|
78 a prettified name will be generated.
|
|
79 :require A single symbol or a list of such symbols, which need to be
|
|
80 present at enable time, or will be loaded using `require'.
|
|
81 :enable A function of no variables, which turns the behavior on.
|
|
82 :disable A function of no variables, which turns the behavior off.
|
502
|
83
|
|
84 Behaviors are assumed to be global, and to take effect immediately; if
|
|
85 the underlying package is per-buffer, it may have to scan all existing
|
|
86 buffers and frob them. When a behavior is disabled, it should completely
|
|
87 go away *everywhere*, as if it were never invoked at all.
|
|
88
|
|
89 The :disable keywords can be missing, although this is considered bad
|
|
90 practice. In such a case, attempting to disable the behavior will signal
|
|
91 an error unless you use the `force' option."
|
|
92 (cl-parsing-keywords
|
800
|
93 ((:short-doc (capitalize-string-as-title (replace-in-string
|
|
94 (symbol-name name) "-" " ")))
|
502
|
95 :require
|
|
96 :enable
|
|
97 :disable)
|
|
98 ()
|
800
|
99 (let ((entry (list :short-doc cl-short-doc :require cl-require
|
502
|
100 :enable cl-enable :disable cl-disable)))
|
|
101 (puthash name entry behavior-hash-table))))
|
|
102
|
|
103 (defun read-behavior (prompt &optional must-match initial-contents history
|
|
104 default-value)
|
|
105 "Return a behavior symbol from the minibuffer, prompting with string PROMPT.
|
|
106 If non-nil, optional second arg INITIAL-CONTENTS is a string to insert
|
|
107 in the minibuffer before reading.
|
|
108 Third arg HISTORY, if non-nil, specifies a history list. (It defaults to
|
|
109 `behavior-history'.)
|
|
110 Fourth arg DEFAULT-VALUE is the default value. If non-nil, it is used
|
|
111 for history command, and as the value to return if the user enters the
|
|
112 empty string."
|
|
113 (let ((result
|
|
114 (completing-read
|
|
115 prompt
|
|
116 (let ((table (let (lis)
|
|
117 (maphash #'(lambda (key val)
|
|
118 (push (cons key val) lis))
|
|
119 behavior-hash-table)
|
|
120 (nreverse lis))))
|
|
121 (mapc #'(lambda (aentry)
|
|
122 (setcar aentry (symbol-name
|
|
123 (car aentry))))
|
|
124 table)
|
|
125 table)
|
|
126 nil must-match initial-contents
|
|
127 (or history 'behavior-history)
|
|
128 default-value)))
|
|
129 (if (and result (stringp result))
|
|
130 (intern result)
|
|
131 result)))
|
|
132
|
800
|
133 (defun behavior-enabled-p (behavior)
|
|
134 "Non-nil if BEHAVIOR (a symbol) if currently enabled."
|
|
135 (memq behavior enabled-behavior-list))
|
502
|
136
|
|
137 (defun enable-behavior (behavior &optional force)
|
|
138 "Enable the specified behavior."
|
|
139 (interactive (list (read-behavior "Enable Behavior: " t) current-prefix-arg))
|
|
140 (let ((plist (gethash behavior behavior-hash-table)))
|
|
141 (or plist (error 'invalid-argument "Not a behavior" behavior))
|
800
|
142 (or force (not (memq behavior enabled-behavior-list))
|
|
143 (error 'invalid-change "Behavior already enabled" behavior))
|
502
|
144 (let ((require (getf plist :require))
|
|
145 (enable (getf plist :enable)))
|
|
146 (cond ((listp require)
|
|
147 (mapc #'(lambda (sym) (require sym)) require))
|
|
148 ((symbolp require)
|
|
149 (require require))
|
|
150 ((null require))
|
|
151 (t (error 'invalid-argument "Invalid :require spec" require)))
|
800
|
152 (message "Enabling behavior %s..." behavior)
|
|
153 (if enable (funcall enable))
|
|
154 (message "Enabling behavior %s...done" behavior)
|
|
155 (let ((within-behavior-enabling-disabling t))
|
|
156 (customize-set-variable 'enabled-behavior-list
|
|
157 (cons behavior enabled-behavior-list))))))
|
502
|
158
|
|
159 (defun disable-behavior (behavior &optional force)
|
|
160 "Disable the specified behavior."
|
|
161 (interactive (list (read-behavior "Disable Behavior: " t)
|
|
162 current-prefix-arg))
|
|
163 (let ((plist (gethash behavior behavior-hash-table)))
|
|
164 (or plist (error 'invalid-argument "Not a behavior" behavior))
|
800
|
165 (or force (memq behavior enabled-behavior-list)
|
|
166 (error 'invalid-change "Behavior not enabled" behavior))
|
502
|
167 (let ((require (getf plist :require))
|
|
168 (disable (getf plist :disable)))
|
|
169 (cond ((listp require)
|
|
170 (mapc #'(lambda (sym) (require sym)) require))
|
|
171 ((symbolp require)
|
|
172 (require require))
|
|
173 ((null require))
|
|
174 (t (error 'invalid-argument "Invalid :require spec" require)))
|
800
|
175 (message "Disabling behavior %s..." behavior)
|
|
176 (if disable (funcall disable))
|
|
177 (message "Disabling behavior %s...done" behavior)
|
|
178 (let ((within-behavior-enabling-disabling t))
|
|
179 (customize-set-variable 'enabled-behavior-list
|
|
180 (delq behavior enabled-behavior-list))))))
|
502
|
181
|
|
182 (provide 'behavior)
|
|
183
|
|
184 ;;; finder-inf.el ends here
|