Note that there are some explanatory texts on larger screens.

plurals
  1. POFirst elisp attempt - minor mode for tab key not being invoked on tab?
    text
    copied!<p>I've decided to get my toes wet with a bit of lisp, since I want to make emacs behave a little better when I hit <kbd>TAB</kbd>. My command works fine. It just performs <code>indent-for-tab-command</code> and if nothing happens, it performs <code>tab-to-tab-stop</code>, on the assumption that it's unlikely I was hitting <kbd>TAB</kbd> just to have the point refuse to budge when I'm inside a multi-line string or some such. After the first <kbd>TAB</kbd> press, it continues to do <code>tab-to-tab-stop</code> until either editing resumes, or the point is moved elsewhere. AFAIK, my logic is ok, though my lisp code probably isn't!</p> <p>Originally I just hacked this into my emacs dot files by doing <code>(local-set-key (kbd "TAB") 'tab-dwim)</code> for major modes where I wanted this behaviour. That worked as expected.</p> <p>Then I decided that what I was doing was basically a minor mode, so I tried to move the key-binding into a minor-mode. For some reason, even though the minor mode is enabled (as indicated in the mode line, and just from toggling it on and off), my <code>tab-dwim</code> function isn't being invoked when I hit the <kbd>TAB</kbd> key. I can still invoke it with <kbd>M-x</kbd> as expected.</p> <p>What am I doing wrong with my minor mode's <code>:keymap</code>?</p> <pre><code>;;; ;; TAB DWIM ; buffer-local before/after point tracking (setq point-before-tab nil) (setq point-after-tab nil) (make-local-variable 'point-before-tab) (make-local-variable 'point-after-tab) (defun tab-dwim () "Indents normally once, then switches to tab-to-tab-stop if invoked again. tab-dwim will always perform tab-to-tab-stop if the first TAB press does not cause the point to move." (interactive) (print "in tab-dwim now") ; THIS LINE IS NEVER INVOKED ON TAB? (setq point-before-tab (point)) (if (eq point-before-tab point-after-tab) ; pressed TAB again (tab-to-tab-stop) (indent-for-tab-command)) (if (eq (point) point-before-tab) ; point didn't move (tab-to-tab-stop)) (setq point-after-tab (point))) (define-minor-mode tab-dwim-mode "Toggle tab-dwim-mode. With a non-nil argument, turns on tab-dwim-mode. With a nil argument, turns it off. When tab-dwim-mode is enabled, pressing the TAB key once will behave as normal, but pressing it subsequent times, will continue to indent, using tab-to-tab-stop. If tab-dwim determines that the first TAB key press resulted in no movement of the point, it will indent according to tab-to-tab-stop instead." :init-value nil :lighter " DWIM" :keymap '(([TAB] . tab-dwim))) (provide 'tab-dwim) </code></pre> <p>Cheers,</p> <p>Chris</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload