forked from jcsalomon/smarttabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-tabs-mode.el
322 lines (273 loc) · 11.6 KB
/
smart-tabs-mode.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
;;; smart-tabs-mode.el --- Intelligently indent with tabs, align with spaces!
;; Copyright © 2011 John Croisant <jacius@gmail.com>
;; Copyright © 2011 Joel C. Salomon <joelcsalomon@gmail.com>
;; Copyright © 2012 Alan Pearce <alan@alanpearce.co.uk>
;; Copyright © 2012 Daniel Dehennin <daniel.dehennin@baby-gnu.org>
;; Copyright © 2013 Matt Renaud <mrenaud92@gmail.com>
;; Author: John Croisant <jacius@gmail.com>
;; Alan Pearce <alan@alanpearce.co.uk>
;; Daniel Dehennin <daniel.dehennin@baby-gnu.org>
;; Matt Renaud <mrenaud92@gmail.com>
;; Maintainer: Joel C. Salomon <joelcsalomon@gmail.com>
;; URL: http://www.emacswiki.org/emacs/SmartTabs
;; Created: 19 Sep 2011
;; Version: 1.0
;; Keywords: languages
;; This file is not part of GNU Emacs.
;;; License:
;; This file 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 2 of the License, or
;; (at your option) any later version.
;;
;; This file 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 file. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provide a semantic way of using tab characters in
;; source code: tabs for indentation, spaces for alignment.
;;
;; It is derived from <http://www.emacswiki.org/emacs/SmartTabs>
;; with modifications by the various authors listed above.
;;
;; Modification history is at <https://github.com/jcsalomon/smarttabs>.
;;; Installation:
;; The easiest and preferred way to install smart-tabs-mode is to use
;; the package available on MELPA.
;;
;; Manual installation:
;;
;; Save smart-tabs-mode.el to a a directory on your load-path
;; (e.g., ~/.emacs.d/elisp), then add the following to your .emacs file:
;;
;; (autoload 'smart-tabs-mode "smart-tabs-mode"
;; "Intelligently indent with tabs, align with spaces!")
;; (autoload 'smart-tabs-mode-enable "smart-tabs-mode")
;; (autoload 'smart-tabs-advice "smart-tabs-mode")
;; (autoload 'smart-tabs-insinuate "smart-tabs-mode")
;;
;;; Enabling smart-tabs-mode within language modes:
;; As of version 1.0 of this package, the easiest and preferred way to
;; enable smart-tabs-mode is with the smart-tabs-insinuate function;
;; for example,
;;
;; (smart-tabs-insinuate 'c 'c++ 'java 'javascript 'cperl 'python
;; 'ruby 'nxml)
;;
;; will enable smart-tabs-mode with all supported language modes.
;;
;; (See below for instructions on adding additional language support.)
;;
;; The old method of manually enabling smart-tabs-mode is still
;; available, but is no longer recommended; smart-tabs-insinuate
;; wraps the functionality below in a convenient manner.
;;
;; For reference, the basic manual method looks like this:
;;
;; (add-hook 'MODE-HOOK 'smart-tabs-mode-enable)
;; (smart-tabs-advice INDENT-FUNC TAB-WIDTH-VAR)
;;
;; Note that it might be preferable to delay calling smart-tabs-advice
;; until after the major mode is loaded and evaluated, so the lines
;; above would be better written like this:
;;
;; (add-hook 'MODE-HOOK (lambda ()
;; (smart-tabs-mode-enable)
;; (smart-tabs-advice INDENT-FUNC TAB-WIDTH-VAR)))
;;
;; Here are some specific examples for a few popular languages:
;;
;; ;; C
;; (add-hook 'c-mode-hook 'smart-tabs-mode-enable)
;; (smart-tabs-advice c-indent-line c-basic-offset)
;; (smart-tabs-advice c-indent-region c-basic-offset)
;;
;; ;; JavaScript
;; (add-hook 'js2-mode-hook 'smart-tabs-mode-enable)
;; (smart-tabs-advice js2-indent-line js2-basic-offset)
;;
;; ;; Perl (cperl-mode)
;; (add-hook 'cperl-mode-hook 'smart-tabs-mode-enable)
;; (smart-tabs-advice cperl-indent-line cperl-indent-level)
;;
;; ;; Python
;; (add-hook 'python-mode-hook 'smart-tabs-mode-enable)
;; (smart-tabs-advice python-indent-line-1 python-indent)
;;; Adding language support
;; Language support can be added through the use of the macro
;; `smart-tabs-add-language-support'. Pass in the symbol you wish
;; to use to identify the language, the mode hook, and a list
;; of cons cells containing (indent-line-function . offset-variable).
;; For example, if C++ mode was not provided by default it could be
;; added as follows:
;;
;; (smart-tabs-add-language-support c++ c++-mode-hook
;; ((c-indent-line . c-basic-offset)
;; (c-indent-region . c-basic-offset)))
;;
;; NOTE: All language support must be added before the call to
;; `smart-tabs-insinuate'.
;;; Code:
(require 'advice)
(require 'cl-lib)
(defvar smart-tabs-mode nil
"Define if smart-tabs-mode is enabled")
;;;###autoload
(defmacro smart-tabs-when (condition advice-list)
(declare (indent 1))
`(when ,condition
,@(smart-tabs-create-advice-list advice-list)))
;;;###autoload
(defmacro smart-tabs-create-advice-list (advice-list)
`(cl-loop for (func . offset) in ,advice-list
collect `(smart-tabs-advice ,func ,offset)))
;;;###autoload
(defmacro smart-tabs-create-language-advice (lang mode-hook advice-list &rest body)
"Create a cons cell containing the actions to take to enable
`smart-tabs-mode' for the language LANG. This usually involved enabling
`smart-tabs-mode' through `smart-tabs-mode-enable' and adding a lambda
function to the MODE-HOOK for the specified language. This macro
simplifies the creation of such a cons cell."
(declare (indent 2))
`'(,lang . (lambda ()
(add-hook ',mode-hook
(lambda ()
(smart-tabs-mode-enable)
,@(smart-tabs-create-advice-list advice-list)
,@(cl-loop for form in body
collect (macroexpand form)))))))
(defvar smart-tabs-insinuate-alist
`(,(smart-tabs-create-language-advice c c-mode-hook
((c-indent-line . c-basic-offset)
(c-indent-region . c-basic-offset)))
,(smart-tabs-create-language-advice c++ c++-mode-hook
((c-indent-line . c-basic-offset)
(c-indent-region . c-basic-offset)))
,(smart-tabs-create-language-advice java java-mode-hook
((c-indent-line . c-basic-offset)
(c-indent-region . c-basic-offset)))
,(smart-tabs-create-language-advice javascript js2-mode-hook
((js2-indent-line . js2-basic-offset)))
,(smart-tabs-create-language-advice cperl cperl-mode-hook
((cperl-indent-line . cperl-indent-level)))
,(smart-tabs-create-language-advice python python-mode-hook
((python-indent-line . python-indent-offset)
(python-indent-region . python-indent-offset))
(smart-tabs-when (featurep 'python-mode)
((py-indent-line . py-indent-offset)
(py-newline-and-indent . py-indent-offset)
(py-indent-region . py-indent-offset))))
,(smart-tabs-create-language-advice ruby ruby-mode-hook
((ruby-indent-line . ruby-indent-level)))
,(smart-tabs-create-language-advice nxml nxml-mode-hook
((nxml-indent-line . nxml-child-indent))))
"Alist of language name and their activation code.
Smarttabs is enabled in mode hook.")
(defmacro smart-tabs-mode/no-tabs-mode-advice (function)
`(unless (ad-find-advice ',function 'around 'smart-tabs)
(defadvice ,function (around smart-tabs activate)
(if smart-tabs-mode
(let ((indent-tabs-mode nil)) ad-do-it)
ad-do-it))))
;;;###autoload
(define-minor-mode smart-tabs-mode
"Intelligently indent with tabs, align with spaces!"
:init-value nil
(progn
(smart-tabs-mode/no-tabs-mode-advice align)
(smart-tabs-mode/no-tabs-mode-advice align-regexp)
(smart-tabs-mode/no-tabs-mode-advice indent-relative)
(smart-tabs-mode/no-tabs-mode-advice comment-dwim)
(smart-tabs-mode/no-tabs-mode-advice comment-box)
(smart-tabs-mode/no-tabs-mode-advice comment-indent)
(unless
(ad-find-advice 'indent-according-to-mode 'around 'smart-tabs)
(defadvice indent-according-to-mode (around smart-tabs activate)
(if smart-tabs-mode
(let ((indent-tabs-mode indent-tabs-mode))
(if (memq indent-line-function
'(indent-relative
indent-relative-maybe))
(setq indent-tabs-mode nil))
ad-do-it)
ad-do-it)))
))
;;;###autoload
(defun smart-tabs-mode-enable ()
"Enable smart-tabs-mode."
(smart-tabs-mode t))
;;;###autoload
(defmacro smart-tabs-advice (function offset)
`(progn
(defadvice ,function (around smart-tabs activate)
(cond
(smart-tabs-mode
(save-excursion
(beginning-of-line)
(while (looking-at "\t*\\( +\\)\t+")
(replace-match "" nil nil nil 1)))
(setq tab-width tab-width)
(let ((indent-tabs-mode t)
(tab-width fill-column)
(,offset fill-column))
(unwind-protect
(progn ad-do-it))))
(t
ad-do-it)))))
;;;###autoload
(defun smart-tabs-insinuate (&rest languages)
"Enable smart-tabs-mode for LANGUAGES.
LANGUAGES is a list of SYMBOL or NAME as defined in
'smart-tabs-insinuate-alist' alist or a language using standard named
indent function and indent level.
"
(mapc (lambda (lang)
(let ((lang-map (assoc lang smart-tabs-insinuate-alist))
(lang-param (smart-tabs-get-standard-language lang)))
(cond ((and (null lang-map)
(not (null (car lang-param)))
(not (null (nth 1 lang-param)))
(not (null (nth 2 lang-param))))
(smart-tabs-guess-insinuate lang-param))
((null lang-map)
(error (format "Unknown smart-tab-mode capable language '%s'" lang)))
(t (funcall (cdr lang-map))))))
languages))
;;;###autoload
(defmacro smart-tabs-add-language-support (lang mode-hook advice-list &rest body)
"Add support for a language not already in the `smart-tabs-insinuate-alist'."
(declare (indent 2))
`(add-to-list
'smart-tabs-insinuate-alist
(smart-tabs-create-language-advice ,lang ,mode-hook
,advice-list ,@body)))
(defun smart-tabs-guess-insinuate (lang-param)
"Enable smart-tabs-mode if language respect standard naming.
Several languages define a '<LANGUAGE>-indent-line' function and
'<LANGUAGE>-indent-level' variable to control indentation.
LANG-PARAM is a list of HOOK INDENT-FUNCTION INDENT-LEVEL, if
thoses are defined, we use them."
(let ((lang-hook (car lang-param))
(indent-function (nth 1 lang-param))
(indent-level (nth 2 lang-param)))
(if (and (not (null lang-hook))
(and (not (null indent-function))
(fboundp indent-function))
(and (not (null indent-level))
(boundp indent-level)))
(add-hook lang-hook
`(lambda ()
(smart-tabs-mode-enable)
(smart-tabs-advice ,indent-function ,indent-level))))))
(defun smart-tabs-get-standard-language (language)
"Return a list of HOOK INDENT-FUNCTION INDENT-LEVEL for a language."
(let ((indent-function (intern-soft (concat (symbol-name language) "-indent-line")))
(indent-level (intern-soft (concat (symbol-name language) "-indent-level")))
(lang-hook (intern-soft (concat (symbol-name language) "-mode-hook"))))
(list lang-hook indent-function indent-level)))
(provide 'smart-tabs-mode)
;;; smart-tabs-mode.el ends here