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
)
Related
Once in a while I manually set the font-family and size different from the default, and I use buffer-face-mode to do it. (To be exact I use the mouse & pick one from the dialog box.) Once I set it, I'd like it to stay set for that buffer, even if I change modes, so I tried a customization. The idea was to add a change-major-mode-hook (which runs just before buffer-locals get killed) that would save the buffer face, if it is set, in a function to be called later- that much seems to work. But then that function seems to be called too soon, and when the mode change is over, buffer-face-mode is not active.
Here's the customization I cam up with so far
(defun my-preserve-bufface-cmmh ()
"Keep the state of buffer-face-mode between major-mode changes"
(if (and (local-variable-p 'buffer-face-mode) buffer-face-mode)
(delay-mode-hooks
(message "face is %s" buffer-face-mode-face) ; Just to show me it has the right face
(let ((my-inner-face buffer-face-mode-face))
(run-mode-hooks
(message "inner %s" my-inner-face) ; it still has the right face here
(setq buffer-face-mode-face my-inner-face)
(buffer-face-mode))))))
(add-hook 'change-major-mode-hook
'my-preserve-bufface-cmmh)
The messages both run and show a custom face, as they should, when I'm changing major-mode in a buffer with the minor-mode buffer-face-mode set. I had thought the combination of delay-mode-hooks ... run-mode-hooks would make setq buffer-face-mode-face ... (buffer-face-mode) run after the new mode was set up, but apparently not.
Is this customization "close"/salvageable for my wants? Is there a cleaner way?
The first thing is that delayed-mode-hooks is itself a local variable.
That means, if you set it by (delay-mode-hooks (run-mode-hooks ...)) in change-major-mode-hook
this will have no effect since it is killed instantaneously.
The second thing is that the stuff within your run-mode-hooks is
evaluated within my-preserve-bufface-cmmh. It should be defined as a back-quoted hook function
`(lambda () ...) where you splice in the values you want to keep.
The alternative would be to use lexical binding (which google).
The 2nd thing is demonstrated in the following example (to be evaluated step-by-step):
(defun test (str)
(let ((mytest (concat "hello " str)))
(add-hook 'my-own-hook `(lambda () (message "mytest:%S" ,mytest)))))
(test "you")
(run-hooks 'my-own-hook)
(test "world")
(run-hooks 'my-own-hook)
(put :myface 'test)
If you want to keep the font buffer-local you have to use a local variable that survives kill-all-local-variables such as buffer-file-name. You can hook a property there:
Edit: Even if the symbol has a buffer local value its properties are not buffer local. Thus, the previous approach did not work. Better: Create your own permanent buffer-local variable:
(defvar-local my-preserve-bufface nil
"Keep the state of buffer-face-mode between major-mode changes")
(put 'my-preserve-bufface 'permanent-local t)
(defun my-preserve-bufface-put ()
"Keep the state of buffer-face-mode between major-mode changes"
(and (local-variable-p 'buffer-face-mode)
buffer-face-mode
(setq my-preserve-bufface buffer-face-mode-face)))
(defun my-preserve-bufface-get ()
"Keep the state of buffer-face-mode between major-mode changes"
(and my-preserve-bufface
(setq buffer-face-mode-face my-preserve-bufface)
(buffer-face-mode)))
(add-hook 'change-major-mode-hook 'my-preserve-bufface-put)
(add-hook 'after-change-major-mode-hook 'my-preserve-bufface-get)
Thanks for the educational comments and especially the answer/example from #user2708138, which I am going to accept because it does answer the question.
Yet I am also going to answer my own question, since I did come up with working code that is a more general solution. I went down this path after finding I also wanted my font-size changes maintained and that they were from text-scale-mode, one more minor-mode to keep. This code reads a list of minor-modes to preserve, without my having to figure out which variables they use. (It isn't too hard for a human to figure them out, but I wanted to try having emacs do it).
Alas there is no function I know of to retrieve the variables used by a minor-mode, but the modes I'm interested in use the convention minor-mode-var-name, so this code just filters buffer-local variables for that pattern.
; Save & restore minor modes I wish to be "permanent" if set
(setq my-preserve-minor-modes '(buffer-face-mode text-scale-mode))
(defun my-preserve-minor-modes-cmmh ()
"Keep the state of desired-permanent minor modes between major-mode changes. Assumes that associated buffer-local minor-mode variables to save begin with `minor-mode-'"
(setq my-restore-minor-modes-acmmh nil)
(dolist (mm my-preserve-minor-modes)
(when (and (local-variable-p mm) (symbol-value mm))
(push mm my-restore-minor-modes-acmmh)))
(when my-restore-minor-modes-acmmh
(add-hook 'after-change-major-mode-hook 'my-restore-minor-modes-acmmh)
; Predicate-list showing if symbol starts with a preserved mode
(let ((mm-p-l `(lambda (locvar-nm)
(or ,#(mapcar (lambda (mm)
`(and (< ,(length (symbol-name mm))
(length locvar-nm))
(string-prefix-p ,(symbol-name mm)
locvar-nm)))
my-restore-minor-modes-acmmh)))))
; For each found minor mode, create fn to restore its buf-local variables
(dolist (locvar (buffer-local-variables))
(if (and (listp locvar) (funcall mm-p-l (symbol-name (car locvar))))
(push `(lambda()(setq ,(car locvar) ',(cdr locvar)))
my-restore-minor-modes-acmmh))))))
(defun my-restore-minor-modes-acmmh ()
"After major-mode change, restore minor-mode state, and remove self from hook. It restores state by calling the function stored in the variable my-restore-minor-modes-acmmh."
(remove-hook 'after-change-major-mode-hook 'my-restore-minor-modes-acmmh)
(dolist (restore-f my-restore-minor-modes-acmmh) (funcall restore-f)))
(add-hook 'change-major-mode-hook 'my-preserve-minor-modes-cmmh)
I did know about the permanent-local property but I wasn't sure about any unwanted side-effects... probably unwarranted paranoia on my part!
My answer could be improved if there is ever a way to get a list of variables for a minor mode, or by having the user specify an alist of variables per minor mode- in either case we wouldn't have to loop over buffer-local-variables anymore, and maybe simply making those all permanent-local would be all we need, simplifying the code quite a bit. Anyway, figuring all this out (with your help & looking at the fine manual) was quite educational.
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)))
Here is a screenshot of what is going wrong:
As you can see, the whitespace characters are getting in the way of auto-complete's pop-up text and making things look really terrible.
When really, I'd like it to look like this:
Is there anyone out there who has been able to use whitespace-mode but eliminate it from popping up in the auto-complete stuff?
There is an issue about compatibility between auto-complete and whitespace-mode in the Prelude issue tracker which has the following workaround in its comments (improved a bit from the original):
(defvar my-prev-whitespace-mode nil)
(make-variable-buffer-local 'my-prev-whitespace-mode)
(defadvice popup-draw (before my-turn-off-whitespace activate compile)
"Turn off whitespace mode before showing autocomplete box"
(if whitespace-mode
(progn
(setq my-prev-whitespace-mode t)
(prelude-turn-off-whitespace))
(setq my-prev-whitespace-mode nil)))
(defadvice popup-delete (after my-restore-whitespace activate compile)
"Restore previous whitespace mode when deleting autocomplete box"
(if my-prev-whitespace-mode
(prelude-turn-on-whitespace)))
Essentially this disables whitespace mode for the whole buffer while a popup is shown.
The issue is also reported in the popup.el issue tracker.
Reworked example to make it work (partially).
There are following issues that prevent #lunaryorn's example from working
double activating the popup-draw advice would cause my-prev-whitespace-mode to lose stored information
activating the popup-delete advice before popup-draw would just switch-off whitespace mode because default value for my-prev-whitespace-mode is nil.
So I evolve the original workaround further to address the two difficulties.
(defun my:force-modes (rule-mode &rest modes)
"switch on/off several modes depending of state of
the controlling minor mode
"
(let ((rule-state (if rule-mode 1 -1)
))
(mapcar (lambda (k) (funcall k rule-state)) modes)
)
)
(require 'whitespace)
(defvar my:prev-whitespace-mode nil)
(make-variable-buffer-local 'my:prev-whitespace-mode)
(defvar my:prev-whitespace-pushed nil)
(make-variable-buffer-local 'my:prev-whitespace-pushed)
(defun my:push-whitespace (&rest skip)
(if my:prev-whitespace-pushed () (progn
(setq my:prev-whitespace-mode whitespace-mode)
(setq my:prev-whitespace-pushed t)
(my:force-modes nil 'whitespace-mode)
))
)
(defun my:pop-whitespace (&rest skip)
(if my:prev-whitespace-pushed (progn
(setq my:prev-whitespace-pushed nil)
(my:force-modes my:prev-whitespace-mode 'whitespace-mode)
))
)
(require 'popup)
(advice-add 'popup-draw :before #'my:push-whitespace)
(advice-add 'popup-delete :after #'my:pop-whitespace)
This solution still has drawbacks. Whitespace-mode is disabled only when a menu is shown. While there is only single candidate for substitution the whitespace mode continues to disrupt screen.
The issue is most likely with bad choice for functions to dirty hack with advices. There should be more appropriate that are called exactly when disabling whitespace-mode is desirable. But no-one except the developer can name them.
I'm trying to turn auto-complete in the minibuffer:
(add-hook 'minibuffer-setup-hook 'auto-complete-mode)
What I get is auto-complete working in the first instance of minibuffer, but no longer. That is the full minibuffer-setup-hook after loading:
(auto-complete-mode turn-on-visual-line-mode ido-minibuffer-setup rfn-eshadow-setup-minibuffer minibuffer-history-isearch-setup minibuffer-history-initialize)
How to turn auto-complete on persistently?
You rarely ever want to add a function symbol to a hook variable if that function acts as a toggle (which will be the case for most minor modes).
minibuffer-setup-hook runs "just after entry to minibuffer", which means that you would be enabling auto complete mode the first time you enter the minibuffer; disabling it the second time; enabling it the third time; etc...
Typically you would either look to see if there's a pre-defined turn-on-autocomplete-mode type of function, or define your own:
(defun my-turn-on-auto-complete-mode ()
(auto-complete-mode 1)) ;; an argument of 1 will enable most modes
(add-hook 'minibuffer-setup-hook 'my-turn-on-auto-complete-mode)
I can't test that, because you haven't linked to the autocomplete-mode you are using.
The creator of "auto-complete-mode" explicitly excludes the minibuffer for use with auto completion. The definition for the minor mode is:
(define-global-minor-mode global-auto-complete-mode
auto-complete-mode auto-complete-mode-maybe
:group 'auto-complete)
so the "turn mode on" function is "auto-complete-mode-maybe" - the definition of that function is:
(defun auto-complete-mode-maybe ()
"What buffer `auto-complete-mode' prefers."
(if (and (not (minibufferp (current-buffer)))
(memq major-mode ac-modes))
(auto-complete-mode 1)))
This function explicitly tests in the if statement if the current-buffer is the minibuffer and doesn't turn on the auto-complete-mode if it is.
If you want to use auto-complete-mode in the minibuffer, you should probably contact the maintainer of the mode and ask him why he excluded the minibuffer and what programming changes he feels are necessary to enable the mode in the minibuffer.
Zev called to my attention auto-complete-mode-maybe, and that is the required modifications (file auto-complete.el, all changes have comments):
;; Add this variable
(defcustom ac-in-minibuffer t
"Non-nil means expand in minibuffer."
:type 'boolean
:group 'auto-complete)
...
(defun ac-handle-post-command ()
(condition-case var
(when (and ac-triggered
(not (ido-active)) ;; Disable auto pop-up in ido mode
(or ac-auto-start
ac-completing)
(not isearch-mode))
(setq ac-last-point (point))
(ac-start :requires (unless ac-completing ac-auto-start))
(ac-inline-update))
(error (ac-error var))))
...
(defun auto-complete-mode-maybe ()
"What buffer `auto-complete-mode' prefers."
(if (or (and (minibufferp (current-buffer)) ac-in-minibuffer) ;; Changed
(memq major-mode ac-modes))
(auto-complete-mode 1)))
And .emacs:
(add-hook 'minibuffer-setup-hook 'auto-complete-mode)
Certainly, there are binding conflicts but it is possible to solve them.
I have a minor mode. If that mode is active and the user hits DEL, I
want to do some action, but only if some condition holds. If the
condition holds and the action is executed I want to do nothing more
after that. But if the condition fails, I don't want to do anything
and let the default DEL action execute.
Not sure how I could solve this. But I guess I could do it in two ways:
1)
I could rebind the DEL key to a function in the minor mode and then
check if the conditions holds ot not. But then how do I know what the
default command to DEL is?
2)
I could add a pre command hook like this. Execute the command and then
break the chain. But how do I break the chain?
(add-hook 'pre-command-hook
(lambda()
(when (equal last-input-event 'backspace)
;; Do something and then stop (do not execute the
;; command that backspace is bound to)
)))
In what way would you solve it? Thanks!
The way to do this is to temporarily disable your minor mode, then look up the key binding.
Pretend that you've bound 'do-thingy to DEL. Then this would do the trick (assuming the condition you want to trigger off is (equal last-input-event 'backspace):
(defun do-thingy ()
"Do something, unless last event was backspace."
(interactive)
(if (equal last-input-event 'backspace)
(let* ((my-minor-mode nil)
(original-func (key-binding (kbd "DEL"))))
;; original-func is whatever DEL would be if
;; my-minor-mode were disabled
(call-interactively original-func))
(message "Here's my minor mode behavior!")))
Note: This behavior assumes you have set up your key bindings the standard way a minor-mode would. Specifically, you should add your keymap to the variable minor-mode-map-alist by adding an element (my-minor-mode . my-minor-mode-keymap). That's how the above let statement works, it looks up the binding you want with your mode temporarily disabled.
If you use define-minor-mode to define your minor mode, the keymap gets set up the "right way" automatically.
This is what I use for my smart-tab package which does exactly that.
(defun smart-tab-default ()
"Indents region if mark is active, or current line otherwise."
(interactive)
(if mark-active
(indent-region (region-beginning)
(region-end))
(call-interactively
(or
;; Minor mode maps for tab (without smart-tab-mode)
(cdar (assq-delete-all 'smart-tab-mode (minor-mode-key-binding "\t")))
(cdar (assq-delete-all 'smart-tab-mode (minor-mode-key-binding [(tab)])))
(local-key-binding "\t")
(local-key-binding [(tab)])
(global-key-binding "\t")
(global-key-binding [(tab)])))))
And in the command smart-tab (which is the one bound to tab in the minor
mode), it has the following:
(if (smart-tab-must-expand prefix)
;; use smart tab
(smart-tab-default))
It first checks if there are any minor mode bindings for tab (not including
smart-tab-mode), then local, and finally global keybindings.
There doesn't seem to be a way to do what you want reliably. If your new command is bound to DEL, then whatever was bound to DEL before in the current keymap isn't there anymore. The other approach you proposed won't work because pre-command-hooks don't prevent the following action from taking place. You might also think to interrupt further execution with ^G (Keyboard-Quit), but that's an uncontrolled interrupt that might stop more things than you want.
Even if you make the process of setting up the new binding a little more sophisticated than just re-bind, and remember what was bound there before, so you can call it afterwards, you don't really have what your looking for. If someone wants to rebind the "default" action, they have to do it by modifying to your function rather than replacing the key binding.
What you want to do doesn't fit Emacs' model of how key binding works.