I'd like to write minor mode that switches several other modes when run. Some kind of umbrella mode.
Now I'm stack with the simplest task - create mode that enables single other mode.
I'v wrote
(require 'whitespace)
(define-minor-mode
myspace-mode
"some doc"
nil
nil
(if myspace-mode
(whitespace-mode 1)
(whitespace-mode -1)
)
)
When I toggle this mode from M-x nothing happens. But when I evaluate directly (whitespace-mode ±1) it works as expected.
What do I miss?
There is one parameter missing from your definition. For this reason, your (if ...) form is actually interpreted as the keymap parameter.
Try this:
(define-minor-mode
myspace-mode
"some doc"
nil
nil
nil
(if myspace-mode
(whitespace-mode 1)
(whitespace-mode -1)))
Related
I am confused about how font lock mode gets engaged. I do not have a statement that launches font lock mode in my init.el, but apparently it always runs as a minor mode. Furthermore I have the following function:
(defun testregexfunc ()
(interactive)
(make-variable-buffer-local 'font-lock-extra-managed-props)
(add-to-list 'font-lock-extra-managed-props 'invisible)
(font-lock-add-keywords nil
'(("\\(\\[\\)\\([a-zA-Z0-9_]+\\)\\(\\]\\)"
(1 '(face nil invisible t))
(3 '(face nil invisible t))))))
It uses font-lock specific things. But it only takes effect once I do M-x testregexfunc followed by M-x font-lock-mode twice. First time disables font lock mode second time starts it. But it is not running as a major mode now, as the buffer still displays whatever mode the buffer was in before. Okay, so I guess the function sets some values and only take effect once the mode restarts. I figured maybe I need to add a hook to font lock mode like this:
(add-hook
'font-lock-mode
'testregexfunc)
No... does not do anything. What do I need to do to not have to restart font lock mode for the function to work?
I got that function from here and modified it some. I don't understand most of its definition and the documentation on font lock does not really help me much:
https://emacs.stackexchange.com/questions/28154/using-font-lock-regexp-groups
I think the functions you are looking for are font-lock-flush and font-lock-ensure which together declare the buffer's font-locking out-of-date and then refontify it. So, you could alter your function as follows,
(defun testregexfunc (arg)
"Fontify buffer with new rules. With prefix arg restore default fontification."
(interactive "P")
(if arg
(font-lock-refresh-defaults) ;restore the defaults for the buffer
(make-variable-buffer-local 'font-lock-extra-managed-props)
(add-to-list 'font-lock-extra-managed-props 'invisible)
(font-lock-add-keywords nil ;make the "[" and "]" invisible
'(("\\(\\[\\)\\([a-zA-Z0-9_]+\\)\\(\\]\\)"
(1 '(face nil invisible t))
(3 '(face nil invisible t)))))
(font-lock-flush) ;declare the fontification out-of-date
(font-lock-ensure))) ;fontify the buffer using new rules
I am programming my own Emacs minor mode using the idioms and macros similar to this minimal example:
(define-minor-mode foo-mode
"Toggle Foo mode."
:init-value nil
:lighter " Foo"
:group 'foo
(if foo-mode
(do-enable-foo)
(do-disable-foo))
)
(defun do-enable-foo ()
"Enable foo minor mode"
(message "Enabling foo...")
(if test-something
(message "Foo enabled after disabling was canceled!")
(message "Foo enabled from scratch"))
)
(defun do-disable-foo ()
"Disable foo minor mode"
(message "Disabling foo...")
(if (not certain-situation)
(message "... done.") ; finish disabling foo
;; else forms:
(message "Need to cancel disabling foo!")
(foo-mode 1)) ; turning foo mode on again
)
During the toggling off of the minor mode, a 'certain-situation may arise when I have to cancel toggling off. Currently, I am thinking since I am using the define-minor-mode macro, that I cannot bail out but just have to programmatically turn the mode on again using (foo-mode 1) as seen in the code.
If I go this route, I will have to handle this differently in the enabling function do-enable-foo -- so my first question is how to detect this case using the placeholder 'test-something in the code above?
Or, is there a cleaner way to achieve the cancel, for example, by signaling an error instead of (foo-mode 1) to prevent the mode toggle from going through?
If you want to cancel the "turn off", rather than call (foo-mode 1), you can just (setq foo-mode t).
If for some reason you really want to call (foo-mode 1) recursively to (re)enable the mode, then you can do it in the following way:
(defvar foo-mode--reenabling nil)
....
(defun do-enable-foo ()
(if foo-mode--reenabling
...
...))
...
(defun do-disable-foo ()
...
(let ((foo-mode--reenabling t))
(foo-mode 1)))
I would be very scared about preventing the user from turning off a minor mode. I mean, if the minor mode is broken, or misbehaving, you leave them with with a broken Emacs that must be killed. What is the condition that you are thinking off?
The opposite situation -- a mode which refuses to be turned on under certain circumstances -- already happens -- paredit-mode is a nice example. It just throws an error in the define-minor-mode body.
I ended up using Stefan's suggestion in the following way using the variable to signal to the minor mode definition that the mode was enabled as a result of cancelling the disable process (actually as a result of a user query). I didn't dare to use the idea of simply setting the mode variable to t but I cannot find the documentation anymore that cautioned against it. It might be worthwhile to see if using (setq foo-mode t) actually lets me eliminate the new variable canceled-foo-off.
(defvar canceled-foo-off nil "Set to `true' if user canceled toggling off foo.")
(make-variable-buffer-local 'canceled-foo-off)
(define-minor-mode foo-mode
...
(if foo-mode
(if canceled-foo-off
(setq canceled-foo-off nil) ; Mode was turned back on from cancelling
(do-enable-foo)) ; Mode was turned on from scratch
(do-disable-foo))
)
(defun do-disable-foo ()
"Disable foo minor mode"
(if (not certain-situation)
...
;; else forms:
(setq canceled-foo-off t) ; make sure mode starting procedure is not run!
(foo-mode 1)) ; turning foo mode on again
)
I've been working on an Emacs minor mode lately and part of its functionality was displaying images in separate buffers. So far I've been using a function like this:
(defun create-buffer-with-image (name)
(let ((buffer (generate-new-buffer name))
(image (get-svg-for-kanji-code name)))
(switch-to-buffer buffer)
(turn-on-iimage-mode)
(iimage-mode-buffer t)
(insert-image image)))
and it produces a buffer with the image passed as argument, but closing the buffer requires hitting C-x k and Return, which started to get cumbersome after a while. The way to simplify closing of such transient buffers would be to have a key binding for the kill-this-buffer function, but it would need to be buffer-specific, so as not to mess up anything else. The question is how to make such a binding with the creation of a buffer.
From EmacsWiki: https://www.emacswiki.org/emacs/BufferLocalKeys
For buffer-local keys, you cannot use local-set-key, unless you want to modify the keymap of the entire major-mode in question: local-set-key is local to a major-mode, not to a buffer.
For buffer-local modifications, use this instead:
(use-local-map (copy-keymap foo-mode-map))
(local-set-key "d" 'some-function)
Written by: TiagoSaboga
To inspect the change, type C-h b aka M-x describe-bindings
I'd suggest you add a call to special-mode after the call to switch-to-buffer.
In the longer run, you'll want to use your own major mode, so you'd do:
(define-derived-mode my-image-mode special-mode "MyImage"
"My own major mode to display images."
;; We could add more things here
)
(defun create-buffer-with-image (name)
(with-current-buffer (generate-new-buffer name)
(my-image-mode)
(let ((image (get-svg-for-kanji-code name)))
(turn-on-iimage-mode)
(iimage-mode-buffer t)
(insert-image image)
(pop-to-bffer (current-buffer)))))
I was a bit mislead by some posts on the web suggesting the use of local-key-binding, but somehow it did not work for me - when the image was displayed and I examined the key bindings, my choice of q was not in effect. After some experimentation and digging through elisp references I found that I needed to use local-set-key. So now my function looks like this:
(defun create-buffer-with-image (name)
(let ((buffer (generate-new-buffer name))
(image (get-svg-for-kanji-code name)))
(switch-to-buffer buffer)
(local-set-key (kbd "q") 'kill-this-buffer)
(turn-on-iimage-mode)
(iimage-mode-buffer t)
(insert-image image)))
and the newly created image buffer can easily be closed by pressing q.
Create a minor mode:
(define-minor-mode my-mode "my doc" nil nil (make-sparse-keymap))
Then you can use this mode's my-mode-map to define your keybindings. Activate the mode with (my-mode).
I'm trying to write a custom tab completion implementation which tries a bunch of different completions depending on where the point is. However, if none of the conditions for completions are met I would like tab to do what ever the current mode originally intended it to do.
Something like this:
(defun my-custom-tab-completion ()
(interactive)
(cond
(some-condition
(do-something))
(some-other-condition
(do-something-else))
(t
(do-whatever-tab-is-supposed-to-do-in-the-current-mode))) ;; How do I do this?
Currently I'm checking for specific modes and doing the right thing for that mode, but I really would like a solution that just does the right thing without me having to explicitly add a condition for that specific mode.
Any ideas of how to do this?
Thanks! /Erik
BTW, here is another solution:
(define-key <map> <key>
`(menu-item "" <my-cmd> :filter ,(lambda (cmd) (if <my-predicate> cmd))))
Here is a macro I wrote based on Emacs key binding fallback to define a keybinding conditionally. It adds the keybinding to the specified minor mode but if the condition is not true, the previously assigned action is executed:
(defmacro define-key-with-fallback (keymap key def condition &optional mode)
"Define key with fallback. Binds KEY to definition DEF in keymap KEYMAP,
the binding is active when the CONDITION is true. Otherwise turns MODE off
and re-enables previous definition for KEY. If MODE is nil, tries to recover
it by stripping off \"-map\" from KEYMAP name."
`(define-key ,keymap ,key
(lambda () (interactive)
(if ,condition ,def
(let* ((,(if mode mode
(let* ((keymap-str (symbol-name keymap))
(mode-name-end (- (string-width keymap-str) 4)))
(if (string= "-map" (substring keymap-str mode-name-end))
(intern (substring keymap-str 0 mode-name-end))
(error "Could not deduce mode name from keymap name (\"-map\" missing?)"))))
nil)
(original-func (key-binding ,key)))
(call-interactively original-func))))))
Then I can do things like the following to use the special binding for TAB only when I am on a header in outline-minor-mode. Otherwise my default action (I have both indent and yasnippets) is executed:
(define-key-with-fallback outline-minor-mode-map (kbd "TAB")
(outline-cycle 1) (outline-on-heading-p))
You could use functions such as key-binding (or its more specific variants global-key-binding, minor-mode-key-binding and local-key-binding) to probe active keymaps for bindings.
For example:
(call-interactively (key-binding (kbd "TAB")))
;; in an emacs-lisp-mode buffer:
;; --> indent-for-tab-command
;;
;; in a c++-mode buffer with yas/minor-mode:
;; --> yas/expand
One way to avoid infinite loops if your command is bound to TAB could be to put your binding in a minor mode, and temporarily disable its keymap while looking for the TAB binding:
(define-minor-mode my-complete-mode
"Smart completion"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") 'my-complete)
map))
(defun my-complete ()
(interactive)
(if (my-condition)
(message "my-complete")
(let ((my-complete-mode nil))
(call-interactively (key-binding (kbd "TAB"))))))
It's possible that you could achieve this without any special workarounds at all. In most modes TAB just does indentation by default, but if you set the global variable tab-always-indent to 'complete it will try to do completion first, and indent if no completion is possible. This usually works really well, although if TAB is bound to another command in one of your major modes you might be out of luck.
If that works in the modes you need, you'll just need to add your custom completion function to the front of the list completion-at-point-functions in all applicable buffers (maybe using a mode hook). The completion-at-point command calls each function listed in completion-at-point-functions until one of them returns non-nil, so all you need to do to have your custom completion function "fall through" to the existing behavior is return nil from it.
This isn't a 100% answer to the question, but if the major modes you're working with are written according to the normal guidelines it might be the cleanest way.
define-key can accept quoted string or interactive lambdas like in this example.
;Static
(define-key evil-normal-state-mapr "m" 'evil-motion-state)
;Conditional
(define-key evil-normal-state-map "m"
(lambda () (interactive) (message "%s" major-mode)))
Lambda's can be replaced with named functions like my-tab-completion and used more effectively.
From define-key's docstring (Emacs 25)
DEF is anything that can be a key's definition:
nil (means key is undefined in this keymap),
a command (a Lisp function suitable for interactive calling),
a string (treated as a keyboard macro),
a keymap (to define a prefix key),
a symbol (when the key is looked up, the symbol will stand for its
function definition, which should at that time be one of the above,
or another symbol whose function definition is used, etc.),
a cons (STRING . DEFN), meaning that DEFN is the definition
(DEFN should be a valid definition in its own right),
or a cons (MAP . CHAR), meaning use definition of CHAR in keymap MAP,
or an extended menu item definition.
(See info node `(elisp)Extended Menu Items'.)
I'm trying to write a custom tab completion implementation which tries a bunch of different completions depending on where the point is. However, if none of the conditions for completions are met I would like tab to do what ever the current mode originally intended it to do.
Something like this:
(defun my-custom-tab-completion ()
(interactive)
(cond
(some-condition
(do-something))
(some-other-condition
(do-something-else))
(t
(do-whatever-tab-is-supposed-to-do-in-the-current-mode))) ;; How do I do this?
Currently I'm checking for specific modes and doing the right thing for that mode, but I really would like a solution that just does the right thing without me having to explicitly add a condition for that specific mode.
Any ideas of how to do this?
Thanks! /Erik
BTW, here is another solution:
(define-key <map> <key>
`(menu-item "" <my-cmd> :filter ,(lambda (cmd) (if <my-predicate> cmd))))
Here is a macro I wrote based on Emacs key binding fallback to define a keybinding conditionally. It adds the keybinding to the specified minor mode but if the condition is not true, the previously assigned action is executed:
(defmacro define-key-with-fallback (keymap key def condition &optional mode)
"Define key with fallback. Binds KEY to definition DEF in keymap KEYMAP,
the binding is active when the CONDITION is true. Otherwise turns MODE off
and re-enables previous definition for KEY. If MODE is nil, tries to recover
it by stripping off \"-map\" from KEYMAP name."
`(define-key ,keymap ,key
(lambda () (interactive)
(if ,condition ,def
(let* ((,(if mode mode
(let* ((keymap-str (symbol-name keymap))
(mode-name-end (- (string-width keymap-str) 4)))
(if (string= "-map" (substring keymap-str mode-name-end))
(intern (substring keymap-str 0 mode-name-end))
(error "Could not deduce mode name from keymap name (\"-map\" missing?)"))))
nil)
(original-func (key-binding ,key)))
(call-interactively original-func))))))
Then I can do things like the following to use the special binding for TAB only when I am on a header in outline-minor-mode. Otherwise my default action (I have both indent and yasnippets) is executed:
(define-key-with-fallback outline-minor-mode-map (kbd "TAB")
(outline-cycle 1) (outline-on-heading-p))
You could use functions such as key-binding (or its more specific variants global-key-binding, minor-mode-key-binding and local-key-binding) to probe active keymaps for bindings.
For example:
(call-interactively (key-binding (kbd "TAB")))
;; in an emacs-lisp-mode buffer:
;; --> indent-for-tab-command
;;
;; in a c++-mode buffer with yas/minor-mode:
;; --> yas/expand
One way to avoid infinite loops if your command is bound to TAB could be to put your binding in a minor mode, and temporarily disable its keymap while looking for the TAB binding:
(define-minor-mode my-complete-mode
"Smart completion"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") 'my-complete)
map))
(defun my-complete ()
(interactive)
(if (my-condition)
(message "my-complete")
(let ((my-complete-mode nil))
(call-interactively (key-binding (kbd "TAB"))))))
It's possible that you could achieve this without any special workarounds at all. In most modes TAB just does indentation by default, but if you set the global variable tab-always-indent to 'complete it will try to do completion first, and indent if no completion is possible. This usually works really well, although if TAB is bound to another command in one of your major modes you might be out of luck.
If that works in the modes you need, you'll just need to add your custom completion function to the front of the list completion-at-point-functions in all applicable buffers (maybe using a mode hook). The completion-at-point command calls each function listed in completion-at-point-functions until one of them returns non-nil, so all you need to do to have your custom completion function "fall through" to the existing behavior is return nil from it.
This isn't a 100% answer to the question, but if the major modes you're working with are written according to the normal guidelines it might be the cleanest way.
define-key can accept quoted string or interactive lambdas like in this example.
;Static
(define-key evil-normal-state-mapr "m" 'evil-motion-state)
;Conditional
(define-key evil-normal-state-map "m"
(lambda () (interactive) (message "%s" major-mode)))
Lambda's can be replaced with named functions like my-tab-completion and used more effectively.
From define-key's docstring (Emacs 25)
DEF is anything that can be a key's definition:
nil (means key is undefined in this keymap),
a command (a Lisp function suitable for interactive calling),
a string (treated as a keyboard macro),
a keymap (to define a prefix key),
a symbol (when the key is looked up, the symbol will stand for its
function definition, which should at that time be one of the above,
or another symbol whose function definition is used, etc.),
a cons (STRING . DEFN), meaning that DEFN is the definition
(DEFN should be a valid definition in its own right),
or a cons (MAP . CHAR), meaning use definition of CHAR in keymap MAP,
or an extended menu item definition.
(See info node `(elisp)Extended Menu Items'.)