From: Adrian A. <ad...@xe...> - 2002-10-06 14:05:15
|
>>>>> "Alex" == Alex Schroeder <al...@em...> writes: Alex> Is there a define-minor-mode in the latest XEmacs, or the lastest Alex> fsf-compat package? If not, I have a simple (untested) proposal that Hi Alex, I can find it in easy-mmode.el, which is part of the xemacs-base package. Adrian Alex> should do enough to be usable for ERC, and IRC client for Emacs and Alex> XEmacs. Alex> Here it is: Alex> (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body) Alex> "Define a minor mode like in Emacs." Alex> `(progn Alex> (devar ,mode ,init-value "Non-nil if the corresponding mode is enabled.") Alex> (defun ,mode (&optional arg) Alex> ,doc Alex> (setq ,mode (if arg Alex> (> (prefix-numeric-value arg) 0) Alex> (not foo))) Alex> ,@body Alex> ,mode) Alex> (add-minor-mode ,mode ,lighter ,keymap))) Alex> Here is the result: Alex> (pp (macroexpand '(define-minor-mode foo "doc" 'init " f" 'map (goo) (gaa))) Alex> (current-buffer)) Alex> (progn Alex> (devar foo Alex> 'init "Non-nil if the corresponding mode is enabled.") Alex> (defun foo Alex> (&optional arg) Alex> "doc" Alex> (setq foo Alex> (if arg Alex> (> Alex> (prefix-numeric-value arg) Alex> 0) Alex> (not foo))) Alex> (goo) Alex> (gaa) Alex> foo) Alex> (add-minor-mode foo " f" Alex> 'map)) Alex> Here is the result for Emacs. Note that it does hooks, and keymap Alex> stuff, which we currently do not need, so I did not bother. Alex> (pp (macroexpand '(define-minor-mode foo "doc" 'init " f" 'map (goo) (gaa))) Alex> (current-buffer)) Alex> (progn Alex> (progn Alex> (defvar foo 'init "Non-nil if foo mode is enabled.\nUse the command `foo' to change this variable.") Alex> (make-variable-buffer-local 'foo)) Alex> (defun foo Alex> (&optional arg) Alex> "doc" Alex> (interactive) Alex> (setq foo Alex> (if arg Alex> (> Alex> (prefix-numeric-value arg) Alex> 0) Alex> (not foo))) Alex> (goo) Alex> (gaa) Alex> (run-hooks 'foo-hook Alex> (if foo 'foo-on-hook 'foo-off-hook)) Alex> (if Alex> (interactive-p) Alex> (message "foo mode %sabled" Alex> (if foo "en" "dis"))) Alex> (force-mode-line-update) Alex> foo) Alex> :autoload-end Alex> (defcustom foo-hook nil "Hook run at the end of function `foo'." :group 'foo :type 'hook) Alex> (defvar foo-map Alex> (let Alex> ((m 'map)) Alex> (cond Alex> ((keymapp m) Alex> m) Alex> ((listp m) Alex> (easy-mmode-define-keymap m)) Alex> (t Alex> (error "Invalid keymap %S" 'map)))) Alex> "Keymap for `foo'.") Alex> (add-minor-mode 'foo Alex> '#(" f" 0 2 Alex> (local-map Alex> (keymap Alex> (header-line keymap Alex> (down-mouse-3 . mode-line-mode-menu-1)) Alex> (mode-line keymap Alex> (down-mouse-3 . mode-line-mode-menu-1))) Alex> help-echo "mouse-3: minor mode menu")) Alex> foo-map) Alex> nil) Alex> And the doc string, in case you are interested: Alex> define-minor-mode is a Lisp macro in `easy-mmode'. Alex> (define-minor-mode MODE DOC &optional INIT-VALUE LIGHTER KEYMAP &rest BODY) Alex> Define a new minor mode MODE. Alex> This function defines the associated control variable MODE, keymap MODE-map, Alex> toggle command MODE, and hook MODE-hook. Alex> DOC is the documentation for the mode toggle command. Alex> Optional INIT-VALUE is the initial value of the mode's variable. Alex> Optional LIGHTER is displayed in the modeline when the mode is on. Alex> Optional KEYMAP is the default (defvar) keymap bound to the mode keymap. Alex> If it is a list, it is passed to `easy-mmode-define-keymap' Alex> in order to build a valid keymap. It's generally better to use Alex> a separate MODE-map variable than to use this argument. Alex> The above three arguments can be skipped if keyword arguments are Alex> used (see below). Alex> BODY contains code that will be executed each time the mode is (dis)activated. Alex> It will be executed after any toggling but before running the hooks. Alex> BODY can start with a list of CL-style keys specifying additional arguments. Alex> The following keyword arguments are supported: Alex> :group Followed by the group name to use for any generated `defcustom'. Alex> :global If non-nil specifies that the minor mode is not meant to be Alex> buffer-local. By default, the variable is made buffer-local. Alex> :init-value Same as the INIT-VALUE argument. Alex> :lighter Same as the LIGHTER argument. -- Adrian Aichner mailto:ad...@xe... http://www.xemacs.org/ |