Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 157 lines (130 sloc) 5.212 kb
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
1 ;;; smart-tab.el --- Intelligent tab completion and indentation.
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
2
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
3 ;; Copyright (C) 2009 Sebastien Rocca Serra,
4 ;; Daniel Hackney
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
5
6 ;; Author: Sebastien Rocca Serra <sroccaserra@gmail.com>
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
7 ;; Daniel Hackney <dan@haxney.org>
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
8 ;; Maintainer: Daniel Hackney <dan@haxney.org>
9 ;; Keywords: convenience abbrev
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
10 ;; Created: 24 May 2009
11 ;; URL: http://github.com/chrono325/smart-tab/tree/master
a3ff634 Version bump to 0.3.
Dan Hackney authored
12 ;; Version: 0.3
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
13 ;; Features that might be required by this library:
1d249cf @genehack make completion functions an alist.
authored
14 ;;
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
15 ;; `easy-mmmode'
16
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
17
18 ;; This file is NOT part of GNU Emacs.
19
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
20 ;; This program is free software; you can redistribute it and/or modify it under
21 ;; the terms of the GNU General Public License as published by the Free Software
22 ;; Foundation; either version 3, or (at your option) any later version.
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
23
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
24 ;; This program is distributed in the hope that it will be useful, but WITHOUT
25 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26 ;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27 ;; details.
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
28
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
29 ;; You should have received a copy of the GNU General Public License along with
30 ;; this program; see the file COPYING. If not, write to the Free Software
31 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
32
33 ;;; Commentary:
34 ;;
35 ;; From http://www.emacswiki.org/cgi-bin/wiki/TabCompletion#toc2. There are a
36 ;; number of available customizations on that page.
37 ;;
38 ;; To activate, add:
39 ;; (require 'smart-tab)
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
40 ;; (global-smart-tab-mode 1)
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
41 ;;
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
42 ;; to your .emacs file, or set `global-smart-tab-mode' to non-nil with
43 ;; customize.
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
44
45 ;;; Code:
46
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
47 (require 'easy-mmode)
48
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
49 (defgroup smart-tab nil
50 "Options for `smart-tab-mode'.")
51
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
52 (defcustom smart-tab-using-hippie-expand nil
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
53 "Use `hippie-expand' to expand text.
54 Use either `hippie-expand' or `dabbrev-expand' for expanding text
55 when we don't have to indent."
93b6917 @haxney Fix incorrectly specified `defcustom'
haxney authored
56 :type '(choice
b5b4c5f Setting for hippie- vs dabbrev-expand was reversed.
Dan Hackney authored
57 (const :tag "hippie-expand" t)
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
58 (const :tag "dabbrev-expand" nil))
59 :group 'smart-tab)
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
60
1d249cf @genehack make completion functions an alist.
authored
61 (defcustom smart-tab-completion-functions-alist
62 '((emacs-lisp-mode . lisp-complete-symbol)
63 (text-mode . dabbrev-completion))
64 "A-list of major modes in which to use a mode specific completion function.
65 If current major mode is not found in this alist, fall back to
66 hippie-expand or dabbrev-expand, depending on the value of
67 smart-tab-using-hippie-expand"
68 :type '(alist :key-type (symbol :tag "Major mode")
69 :value-type (function :tag "Completion function to use in this mode"))
70 :group 'smart-tab)
71
909a27d @psanford Added an exceptions list for major modes that don't play well.
psanford authored
72 (defcustom smart-tab-disabled-major-modes '(term-mode)
73 "List of major modes that should not use smart-tab"
74 :type 'sexp
75 :group 'smart-tab)
76
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
77 ;;;###autoload
78 (defun smart-tab (prefix)
fc84764 Set the keybinding globally rather than in a minor mode.
Dan Hackney authored
79 "Try to 'do the smart thing' when tab is pressed.
80 `smart-tab' attempts to expand the text before the point or
81 indent the current line or selection.
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
82
83 In a regular buffer, `smart-tab' will attempt to expand with
84 either `hippie-expand' or `dabbrev-expand', depending on the
85 value of `smart-tab-using-hippie-expand'. If the mark is active,
86 or PREFIX is \\[universal-argument], then `smart-tab' will indent
87 the region or the current line (if the mark is not active)."
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
88 (interactive "P")
89 (if (smart-tab-must-expand prefix)
1816e1d @genehack fix up various broken things.
authored
90 (call-completion-function))
91 (smart-tab-default))
c887322 @genehack add in stuff from EmacsWiki user SebastianB
authored
92
1816e1d @genehack fix up various broken things.
authored
93 (defun call-completion-function()
c887322 @genehack add in stuff from EmacsWiki user SebastianB
authored
94 "Get a completion function according to current major mode."
95 (let ((completion-function
1d249cf @genehack make completion functions an alist.
authored
96 (cdr (assq major-mode smart-tab-completion-functions-alist))))
c887322 @genehack add in stuff from EmacsWiki user SebastianB
authored
97 (if (null completion-function)
1d249cf @genehack make completion functions an alist.
authored
98 (if smart-tab-using-hippie-expand
1816e1d @genehack fix up various broken things.
authored
99 (hippie-expand nil)
100 (dabbrev-expand nil))
101 (funcall completion-function))))
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
102
7c80c79 More intelligent fall back when not expanding.
Dan Hackney authored
103 (defun smart-tab-default ()
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
104 "Indents region if mark is active, or current line otherwise."
105 (interactive)
106 (if mark-active
107 (indent-region (region-beginning)
108 (region-end))
1816e1d @genehack fix up various broken things.
authored
109 (indent-for-tab-command)))
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
110
111 (defun smart-tab-must-expand (&optional prefix)
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
112 "If PREFIX is \\[universal-argument] or the mark is active, do not expand.
113 Otherwise, uses `hippie-expand' or `dabbrev-expand' to expand the text at point.."
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
114 (unless (or (consp prefix)
115 mark-active)
116 (looking-at "\\_>")))
117
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
118 ;;;###autoload
32861fa Convert smart-tab to a minor-mode.
Dan Hackney authored
119 (defun smart-tab-mode-on ()
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
120 "Turn on `smart-tab-mode'."
fc84764 Set the keybinding globally rather than in a minor mode.
Dan Hackney authored
121 (smart-tab-mode 1))
122
263b045 Add `turn-off-smart-tab-mode' function.
Dan Hackney authored
123 (defun smart-tab-mode-off ()
124 "Turn off `smart-tab-mode'."
125 (smart-tab-mode -1))
126
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
127 ;;;###autoload
32861fa Convert smart-tab to a minor-mode.
Dan Hackney authored
128 (define-minor-mode smart-tab-mode
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
129 "Enable `smart-tab' to be used in place of tab.
130
32861fa Convert smart-tab to a minor-mode.
Dan Hackney authored
131 With no argument, this command toggles the mode.
132 Non-null prefix argument turns on the mode.
133 Null prefix argument turns off the mode."
134 :lighter " Smrt"
25ff08e Prepare for initial upload to ELPA.
Dan Hackney authored
135 :group 'smart-tab
fc84764 Set the keybinding globally rather than in a minor mode.
Dan Hackney authored
136 :require 'smart-tab
8134a70 Remove global binding of smart-tab, use only minor-mode binding.
Dan Hackney authored
137 :keymap '(("\t" . smart-tab)
138 ([(tab)] . smart-tab))
139 (if smart-tab-mode
140 (progn
141 ;; Don't start `smart-tab-mode' when in the minibuffer or a read-only
142 ;; buffer.
143 (when (or (minibufferp)
909a27d @psanford Added an exceptions list for major modes that don't play well.
psanford authored
144 buffer-read-only
145 (member major-mode smart-tab-disabled-major-modes))
263b045 Add `turn-off-smart-tab-mode' function.
Dan Hackney authored
146 (smart-tab-mode-off)))))
32861fa Convert smart-tab to a minor-mode.
Dan Hackney authored
147
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
148 ;;;###autoload
32861fa Convert smart-tab to a minor-mode.
Dan Hackney authored
149 (define-globalized-minor-mode global-smart-tab-mode
150 smart-tab-mode
c8999bc Flesh out minor-mode definitions, loading from custom works.
Dan Hackney authored
151 smart-tab-mode-on
152 :group 'smart-tab)
32861fa Convert smart-tab to a minor-mode.
Dan Hackney authored
153
5c94d52 @haxney Initial version. Same as on EmacsWiki for now.
haxney authored
154 (provide 'smart-tab)
155
156 ;;; smart-tab.el ends here
Something went wrong with that request. Please try again.