Skip to content

Commit

Permalink
Set the keybinding globally rather than in a minor mode.
Browse files Browse the repository at this point in the history
This allows org-mode's `org-cycle' command to fall-back to `smart-tab' if it
decides not to do any "org-mode-y" functions. Rather than have `smart-tab' try
to deal with what it should do if it is called within a the minibuffer, prevent
`smart-tab-mode' from starting in a minibuffer.

This is a better way of organizing things, since if a major or minor mode wants
to override the function of tab, then it should have priority over
`smart-tab'. If that mode wants to provide a fallback (like org-mode does if it
cannot cycle at the current point), it can use `global-key-binding' to call
`smart-tab'.

Signed-off-by: Dan Hackney <chrono325@gmail.com>
  • Loading branch information
Dan Hackney committed May 27, 2009
1 parent 25ff08e commit fc84764
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions smart-tab.el
Expand Up @@ -11,7 +11,7 @@
;; URL: http://github.com/chrono325/smart-tab/tree/master
;; Version: 0.2
;; Features that might be required by this library:
;;
;;
;; `easy-mmmode'


Expand Down Expand Up @@ -60,35 +60,21 @@ when we don't have to indent."

;;;###autoload
(defun smart-tab (prefix)
"Try to 'do the right thing' when tab is pressed.
`smart-tab' attempts to expand the text before the point, indent
the current line or selection, or complete a string (when in the
minibuffer).
If tab is pressed while in the minibuffer, then
`minibuffer-complete' is called, unless an `ido-completing-read'
is in progress, in which case `ido-complete' is called. This lets
`smart-tab' work correctly both when completing with ido and when
using the default completion.
"Try to 'do the smart thing' when tab is pressed.
`smart-tab' attempts to expand the text before the point or
indent the current line or selection.
In a regular buffer, `smart-tab' will attempt to expand with
either `hippie-expand' or `dabbrev-expand', depending on the
value of `smart-tab-using-hippie-expand'. If the mark is active,
or PREFIX is \\[universal-argument], then `smart-tab' will indent
the region or the current line (if the mark is not active)."
(interactive "P")
(if (minibufferp)
;; If completing with ido, need to use `ido-complete' to continue
;; completing, not `minibuffer-complete'
(if (and (functionp 'ido-active)
(ido-active))
(ido-complete)
(minibuffer-complete))
(if (smart-tab-must-expand prefix)
(if smart-tab-using-hippie-expand
(hippie-expand nil)
(dabbrev-expand nil))
(smart-indent))))
(if (smart-tab-must-expand prefix)
(if smart-tab-using-hippie-expand
(hippie-expand nil)
(dabbrev-expand nil))
(smart-indent)))

(defun smart-indent ()
"Indents region if mark is active, or current line otherwise."
Expand All @@ -108,8 +94,11 @@ Otherwise, uses `hippie-expand' or `dabbrev-expand' to expand the text at point.
;;;###autoload
(defun smart-tab-mode-on ()
"Turn on `smart-tab-mode'."
(unless (minibufferp)
(smart-tab-mode 1)))
(smart-tab-mode 1))

;; Save the old definition of tab.
(defvar smart-tab-previous-tab-binding (global-key-binding "\t"))
(make-variable-buffer-local 'smart-tab-previous-tab-binding)

;;;###autoload
(define-minor-mode smart-tab-mode
Expand All @@ -121,9 +110,23 @@ With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode."
:lighter " Smrt"
:keymap '(([tab] . smart-tab))
:group 'smart-tab
:require 'smart-tab)
:require 'smart-tab
(let ((prev-bind (global-key-binding "\t")))
(if smart-tab-mode
(progn
(unless (eq prev-bind 'smart-tab)
(setq smart-tab-previous-tab-binding prev-bind))

;; Don't start `smart-tab-mode' when in the minibuffer or a read-only
;; buffer.
(when (or (minibufferp)
buffer-read-only)
(smart-tab-mode -1))
(global-set-key "\t" 'smart-tab))

;; Restore the old binding of tab.
(global-set-key "\t" smart-tab-previous-tab-binding))))

;;;###autoload
(define-globalized-minor-mode global-smart-tab-mode
Expand Down

0 comments on commit fc84764

Please sign in to comment.