-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsmart-tab.el
140 lines (116 loc) · 4.4 KB
/
smart-tab.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;; smart-tab.el --- Intelligent tab completion and indentation.
;; Copyright (C) 2009 Sebastien Rocca Serra,
;; Daniel Hackney
;; Author: Sebastien Rocca Serra <[email protected]>
;; Daniel Hackney <[email protected]>
;; Maintainer: Daniel Hackney <[email protected]>
;; Keywords: convenience abbrev
;; Created: 24 May 2009
;; URL: http://github.com/chrono325/smart-tab/tree/master
;; Version: 0.3
;; Features that might be required by this library:
;;
;; `easy-mmmode'
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation; either version 3, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.
;; You should have received a copy of the GNU General Public License along with
;; this program; see the file COPYING. If not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; From http://www.emacswiki.org/cgi-bin/wiki/TabCompletion#toc2. There are a
;; number of available customizations on that page.
;;
;; To activate, add:
;; (require 'smart-tab)
;; (global-smart-tab-mode 1)
;;
;; to your .emacs file, or set `global-smart-tab-mode' to non-nil with
;; customize.
;;; Code:
(require 'easy-mmode)
(defgroup smart-tab nil
"Options for `smart-tab-mode'.")
(defcustom smart-tab-using-hippie-expand nil
"Use `hippie-expand' to expand text.
Use either `hippie-expand' or `dabbrev-expand' for expanding text
when we don't have to indent."
:type '(choice
(const :tag "hippie-expand" t)
(const :tag "dabbrev-expand" nil))
:group 'smart-tab)
;;;###autoload
(defun smart-tab (prefix)
"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 (smart-tab-must-expand prefix)
(if smart-tab-using-hippie-expand
(hippie-expand nil)
(dabbrev-expand nil))
(smart-tab-default)))
(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)])))))
(defun smart-tab-must-expand (&optional prefix)
"If PREFIX is \\[universal-argument] or the mark is active, do not expand.
Otherwise, uses `hippie-expand' or `dabbrev-expand' to expand the text at point.."
(unless (or (consp prefix)
mark-active)
(looking-at "\\_>")))
;;;###autoload
(defun smart-tab-mode-on ()
"Turn on `smart-tab-mode'."
(smart-tab-mode 1))
(defun smart-tab-mode-off ()
"Turn off `smart-tab-mode'."
(smart-tab-mode -1))
;;;###autoload
(define-minor-mode smart-tab-mode
"Enable `smart-tab' to be used in place of tab.
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"
:group 'smart-tab
:require 'smart-tab
:keymap '(("\t" . smart-tab)
([(tab)] . smart-tab))
(if smart-tab-mode
(progn
;; Don't start `smart-tab-mode' when in the minibuffer or a read-only
;; buffer.
(when (or (minibufferp)
buffer-read-only)
(smart-tab-mode-off)))))
;;;###autoload
(define-globalized-minor-mode global-smart-tab-mode
smart-tab-mode
smart-tab-mode-on
:group 'smart-tab)
(provide 'smart-tab)
;;; smart-tab.el ends here