Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 103 lines (91 sloc) 3.272 kB
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
1 ;; Emacs smart tabs mode
2 ;; Intelligently indent with tabs, align with spaces!
3 ;;
24b6486 @jcsalomon Edit documentation
authored
4 ;; To use, save smart-tabs-mode.el to a a directory on your load-path
5 ;; (e.g., ~/.emacs.d/elisp), then add the following to your .emacs file:
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
6 ;;
a217800 @jacius Fixed documentation of smart-tabs-mode.el.
jacius authored
7 ;; (require 'smart-tabs-mode)
c0f7c82 @jcsalomon Document installation of smart-tabs-mode better
authored
8 ;;
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
9 ;; Then, for each language you want to use smart tabs, set up a hook
10 ;; and advice like so:
11 ;;
a217800 @jacius Fixed documentation of smart-tabs-mode.el.
jacius authored
12 ;; (add-hook 'MODE-HOOK 'smart-tabs-mode-enable)
13 ;; (smart-tabs-advice INDENT-FUNC TAB-WIDTH-VAR)
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
14 ;;
15 ;; Here are some specific examples for a few popular languages:
16 ;;
17 ;; ;; C/C++
18 ;; (add-hook 'c-mode-hook 'smart-tabs-mode-enable)
19 ;; (smart-tabs-advice c-indent-line c-basic-offset)
20 ;; (smart-tabs-advice c-indent-region c-basic-offset)
21 ;;
22 ;; ;; JavaScript
23 ;; (add-hook 'js2-mode-hook 'smart-tabs-mode-enable)
24 ;; (smart-tabs-advice js2-indent-line js2-basic-offset)
25 ;;
26 ;; ;; Perl (cperl-mode)
27 ;; (add-hook 'cperl-mode-hook 'smart-tabs-mode-enable)
28 ;; (smart-tabs-advice cperl-indent-line cperl-indent-level)
29 ;;
30 ;; ;; Python
31 ;; (add-hook 'python-mode-hook 'smart-tabs-mode-enable)
32 ;; (smart-tabs-advice python-indent-line-1 python-indent)
33 ;;
34 ;; ;; Ruby
35 ;; (add-hook 'ruby-mode-hook 'smart-tabs-mode-enable)
36 ;; (smart-tabs-advice ruby-indent-line ruby-indent-level)
37 ;;
38 ;;
39 ;; smart-tabs-mode.el is licensed under the GPLv2.
24b6486 @jcsalomon Edit documentation
authored
40 ;; It is derived from <http://www.emacswiki.org/emacs/SmartTabs>
41 ;; as modified by John Croisant (jacius), along with Julien Fontanet and
42 ;; Tomita Hiroshi (tomykaira).
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
43 ;;
24b6486 @jcsalomon Edit documentation
authored
44 ;; Modification history is at <https://github.com/jcsalomon/smarttabs>.
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
45
b21ee27 @jcsalomon Explicitly load Emacs advice
authored
46 (require 'advice)
8a09b47 @jacius Added smart-tabs-mode.el.
jacius authored
47
48 (defmacro smart-tabs-mode/no-tabs-mode-advice (function)
49 `(unless (ad-find-advice ',function 'around 'smart-tabs)
50 (defadvice ,function (around smart-tabs activate)
51 (if smart-tabs-mode
52 (let ((indent-tabs-mode nil)) ad-do-it)
53 ad-do-it))))
54
55
56 (define-minor-mode smart-tabs-mode
57 "Intelligently indent with tabs, align with spaces!"
58
59 (progn
60 (smart-tabs-mode/no-tabs-mode-advice align)
61 (smart-tabs-mode/no-tabs-mode-advice align-regexp)
62 (smart-tabs-mode/no-tabs-mode-advice indent-relative)
63
64 (unless
65 (ad-find-advice 'indent-according-to-mode 'around 'smart-tabs)
66 (defadvice indent-according-to-mode (around smart-tabs activate)
67 (if smart-tabs-mode
68 (let ((indent-tabs-mode indent-tabs-mode))
69 (if (memq indent-line-function
70 '(indent-relative
71 indent-relative-maybe))
72 (setq indent-tabs-mode nil))
73 ad-do-it)
74 ad-do-it)))
75 ))
76
77 (defun smart-tabs-mode-enable ()
78 "Enable smart-tabs-mode."
79 (smart-tabs-mode t))
80
81
82 (defmacro smart-tabs-advice (function offset)
83 `(progn
84 (defvaralias ',offset 'tab-width)
85 (defadvice ,function (around smart-tabs activate)
86 (cond
87 (smart-tabs-mode
88 (save-excursion
89 (beginning-of-line)
90 (while (looking-at "\t*\\( +\\)\t+")
91 (replace-match "" nil nil nil 1)))
92 (setq tab-width tab-width)
93 (let ((indent-tabs-mode t)
94 (tab-width fill-column)
95 (,offset fill-column))
96 (unwind-protect
97 (progn ad-do-it))))
98 (t
99 ad-do-it)))))
100
101
102 (provide 'smart-tabs-mode)
Something went wrong with that request. Please try again.