indent.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. ;;; indent.el --- indentation commands for Emacs
  2. ;; Copyright (C) 1985, 1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Package: emacs
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Commands for making and changing indentation in text. These are
  18. ;; described in the Emacs manual.
  19. ;;; Code:
  20. (defgroup indent nil
  21. "Indentation commands."
  22. :group 'editing)
  23. (defcustom standard-indent 4
  24. "Default number of columns for margin-changing functions to indent."
  25. :group 'indent
  26. :type 'integer)
  27. (defvar indent-line-function 'indent-relative
  28. "Function to indent the current line.
  29. This function will be called with no arguments.
  30. If it is called somewhere where auto-indentation cannot be done
  31. \(e.g. inside a string), the function should simply return `noindent'.
  32. Setting this function is all you need to make TAB indent appropriately.
  33. Don't rebind TAB unless you really need to.")
  34. (defcustom tab-always-indent t
  35. "Controls the operation of the TAB key.
  36. If t, hitting TAB always just indents the current line.
  37. If nil, hitting TAB indents the current line if point is at the left margin
  38. or in the line's indentation, otherwise it inserts a \"real\" TAB character.
  39. If `complete', TAB first tries to indent the current line, and if the line
  40. was already indented, then try to complete the thing at point.
  41. Some programming language modes have their own variable to control this,
  42. e.g., `c-tab-always-indent', and do not respect this variable."
  43. :group 'indent
  44. :type '(choice
  45. (const :tag "Always indent" t)
  46. (const :tag "Indent if inside indentation, else TAB" nil)
  47. (const :tag "Indent, or if already indented complete" complete)))
  48. (defun indent-according-to-mode ()
  49. "Indent line in proper way for current major mode.
  50. Normally, this is done by calling the function specified by the
  51. variable `indent-line-function'. However, if the value of that
  52. variable is `indent-relative' or `indent-relative-maybe', handle
  53. it specially (since those functions are used for tabbing); in
  54. that case, indent by aligning to the previous non-blank line."
  55. (interactive)
  56. (syntax-propertize (line-end-position))
  57. (if (memq indent-line-function
  58. '(indent-relative indent-relative-maybe))
  59. ;; These functions are used for tabbing, but can't be used for
  60. ;; indenting. Replace with something ad-hoc.
  61. (let ((column (save-excursion
  62. (beginning-of-line)
  63. (skip-chars-backward "\n \t")
  64. (beginning-of-line)
  65. (current-indentation))))
  66. (if (<= (current-column) (current-indentation))
  67. (indent-line-to column)
  68. (save-excursion (indent-line-to column))))
  69. ;; The normal case.
  70. (funcall indent-line-function)))
  71. (defun indent-for-tab-command (&optional arg)
  72. "Indent the current line or region, or insert a tab, as appropriate.
  73. This function either inserts a tab, or indents the current line,
  74. or performs symbol completion, depending on `tab-always-indent'.
  75. The function called to actually indent the line or insert a tab
  76. is given by the variable `indent-line-function'.
  77. If a prefix argument is given, after this function indents the
  78. current line or inserts a tab, it also rigidly indents the entire
  79. balanced expression which starts at the beginning of the current
  80. line, to reflect the current line's indentation.
  81. In most major modes, if point was in the current line's
  82. indentation, it is moved to the first non-whitespace character
  83. after indenting; otherwise it stays at the same position relative
  84. to the text.
  85. If `transient-mark-mode' is turned on and the region is active,
  86. this function instead calls `indent-region'. In this case, any
  87. prefix argument is ignored."
  88. (interactive "P")
  89. (cond
  90. ;; The region is active, indent it.
  91. ((use-region-p)
  92. (indent-region (region-beginning) (region-end)))
  93. ((or ;; indent-to-left-margin is only meant for indenting,
  94. ;; so we force it to always insert a tab here.
  95. (eq indent-line-function 'indent-to-left-margin)
  96. (and (not tab-always-indent)
  97. (or (> (current-column) (current-indentation))
  98. (eq this-command last-command))))
  99. (insert-tab arg))
  100. (t
  101. (let ((old-tick (buffer-chars-modified-tick))
  102. (old-point (point))
  103. (old-indent (current-indentation)))
  104. ;; Indent the line.
  105. (funcall indent-line-function)
  106. (cond
  107. ;; If the text was already indented right, try completion.
  108. ((and (eq tab-always-indent 'complete)
  109. (eq old-point (point))
  110. (eq old-tick (buffer-chars-modified-tick)))
  111. (completion-at-point))
  112. ;; If a prefix argument was given, rigidly indent the following
  113. ;; sexp to match the change in the current line's indentation.
  114. (arg
  115. (let ((end-marker
  116. (save-excursion
  117. (forward-line 0) (forward-sexp) (point-marker)))
  118. (indentation-change (- (current-indentation) old-indent)))
  119. (save-excursion
  120. (forward-line 1)
  121. (when (and (not (zerop indentation-change))
  122. (< (point) end-marker))
  123. (indent-rigidly (point) end-marker indentation-change))))))))))
  124. (defun insert-tab (&optional arg)
  125. (let ((count (prefix-numeric-value arg)))
  126. (if (and abbrev-mode
  127. (eq (char-syntax (preceding-char)) ?w))
  128. (expand-abbrev))
  129. (if indent-tabs-mode
  130. (insert-char ?\t count)
  131. (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
  132. (defun indent-rigidly (start end arg)
  133. "Indent all lines starting in the region sideways by ARG columns.
  134. Called from a program, takes three arguments, START, END and ARG.
  135. You can remove all indentation from a region by giving a large negative ARG."
  136. (interactive "r\np")
  137. (save-excursion
  138. (goto-char end)
  139. (setq end (point-marker))
  140. (goto-char start)
  141. (or (bolp) (forward-line 1))
  142. (while (< (point) end)
  143. (let ((indent (current-indentation))
  144. eol-flag)
  145. (save-excursion
  146. (skip-chars-forward " \t")
  147. (setq eol-flag (eolp)))
  148. (or eol-flag
  149. (indent-to (max 0 (+ indent arg)) 0))
  150. (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  151. (forward-line 1))
  152. (move-marker end nil)))
  153. (defun indent-line-to (column)
  154. "Indent current line to COLUMN.
  155. This function removes or adds spaces and tabs at beginning of line
  156. only if necessary. It leaves point at end of indentation."
  157. (back-to-indentation)
  158. (let ((cur-col (current-column)))
  159. (cond ((< cur-col column)
  160. (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
  161. (delete-region (point)
  162. (progn (skip-chars-backward " ") (point))))
  163. (indent-to column))
  164. ((> cur-col column) ; too far right (after tab?)
  165. (delete-region (progn (move-to-column column t) (point))
  166. (progn (back-to-indentation) (point)))))))
  167. (defun current-left-margin ()
  168. "Return the left margin to use for this line.
  169. This is the value of the buffer-local variable `left-margin' plus the value
  170. of the `left-margin' text-property at the start of the line."
  171. (save-excursion
  172. (back-to-indentation)
  173. (max 0
  174. (+ left-margin (or (get-text-property
  175. (if (and (eobp) (not (bobp)))
  176. (1- (point)) (point))
  177. 'left-margin) 0)))))
  178. (defun move-to-left-margin (&optional n force)
  179. "Move to the left margin of the current line.
  180. With optional argument, move forward N-1 lines first.
  181. The column moved to is the one given by the `current-left-margin' function.
  182. If the line's indentation appears to be wrong, and this command is called
  183. interactively or with optional argument FORCE, it will be fixed."
  184. (interactive (list (prefix-numeric-value current-prefix-arg) t))
  185. (beginning-of-line n)
  186. (skip-chars-forward " \t")
  187. (if (minibufferp (current-buffer))
  188. (if (save-excursion (beginning-of-line) (bobp))
  189. (goto-char (minibuffer-prompt-end))
  190. (beginning-of-line))
  191. (let ((lm (current-left-margin))
  192. (cc (current-column)))
  193. (cond ((> cc lm)
  194. (if (> (move-to-column lm force) lm)
  195. ;; If lm is in a tab and we are not forcing, move before tab
  196. (backward-char 1)))
  197. ((and force (< cc lm))
  198. (indent-to-left-margin))))))
  199. ;; This used to be the default indent-line-function,
  200. ;; used in Fundamental Mode, Text Mode, etc.
  201. (defun indent-to-left-margin ()
  202. "Indent current line to the column given by `current-left-margin'."
  203. (save-excursion (indent-line-to (current-left-margin)))
  204. ;; If we are within the indentation, move past it.
  205. (when (save-excursion
  206. (skip-chars-backward " \t")
  207. (bolp))
  208. (skip-chars-forward " \t")))
  209. (defun delete-to-left-margin (&optional from to)
  210. "Remove left margin indentation from a region.
  211. This deletes to the column given by `current-left-margin'.
  212. In no case will it delete non-whitespace.
  213. Args FROM and TO are optional; default is the whole buffer."
  214. (save-excursion
  215. (goto-char (or to (point-max)))
  216. (setq to (point-marker))
  217. (goto-char (or from (point-min)))
  218. (or (bolp) (forward-line 1))
  219. (while (< (point) to)
  220. (delete-region (point) (progn (move-to-left-margin nil t) (point)))
  221. (forward-line 1))
  222. (move-marker to nil)))
  223. (defun set-left-margin (from to width)
  224. "Set the left margin of the region to WIDTH.
  225. If `auto-fill-mode' is active, re-fill the region to fit the new margin.
  226. Interactively, WIDTH is the prefix argument, if specified.
  227. Without prefix argument, the command prompts for WIDTH."
  228. (interactive "r\nNSet left margin to column: ")
  229. (save-excursion
  230. ;; If inside indentation, start from BOL.
  231. (goto-char from)
  232. (skip-chars-backward " \t")
  233. (if (bolp) (setq from (point)))
  234. ;; Place end after whitespace
  235. (goto-char to)
  236. (skip-chars-forward " \t")
  237. (setq to (point-marker)))
  238. ;; Delete margin indentation first, but keep paragraph indentation.
  239. (delete-to-left-margin from to)
  240. (put-text-property from to 'left-margin width)
  241. (indent-rigidly from to width)
  242. (if auto-fill-function (save-excursion (fill-region from to nil t t)))
  243. (move-marker to nil))
  244. (defun set-right-margin (from to width)
  245. "Set the right margin of the region to WIDTH.
  246. If `auto-fill-mode' is active, re-fill the region to fit the new margin.
  247. Interactively, WIDTH is the prefix argument, if specified.
  248. Without prefix argument, the command prompts for WIDTH."
  249. (interactive "r\nNSet right margin to width: ")
  250. (save-excursion
  251. (goto-char from)
  252. (skip-chars-backward " \t")
  253. (if (bolp) (setq from (point))))
  254. (put-text-property from to 'right-margin width)
  255. (if auto-fill-function (save-excursion (fill-region from to nil t t))))
  256. (defun alter-text-property (from to prop func &optional object)
  257. "Programmatically change value of a text-property.
  258. For each region between FROM and TO that has a single value for PROPERTY,
  259. apply FUNCTION to that value and sets the property to the function's result.
  260. Optional fifth argument OBJECT specifies the string or buffer to operate on."
  261. (let ((begin from)
  262. end val)
  263. (while (setq val (get-text-property begin prop object)
  264. end (text-property-not-all begin to prop val object))
  265. (put-text-property begin end prop (funcall func val) object)
  266. (setq begin end))
  267. (if (< begin to)
  268. (put-text-property begin to prop (funcall func val) object))))
  269. (defun increase-left-margin (from to inc)
  270. "Increase or decrease the left-margin of the region.
  271. With no prefix argument, this adds `standard-indent' of indentation.
  272. A prefix arg (optional third arg INC noninteractively) specifies the amount
  273. to change the margin by, in characters.
  274. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  275. (interactive "*r\nP")
  276. (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  277. (save-excursion
  278. (goto-char from)
  279. (skip-chars-backward " \t")
  280. (if (bolp) (setq from (point)))
  281. (goto-char to)
  282. (setq to (point-marker)))
  283. (alter-text-property from to 'left-margin
  284. (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
  285. (indent-rigidly from to inc)
  286. (if auto-fill-function (save-excursion (fill-region from to nil t t)))
  287. (move-marker to nil))
  288. (defun decrease-left-margin (from to inc)
  289. "Make the left margin of the region smaller.
  290. With no prefix argument, decrease the indentation by `standard-indent'.
  291. A prefix arg (optional third arg INC noninteractively) specifies the amount
  292. to change the margin by, in characters.
  293. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  294. (interactive "*r\nP")
  295. (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  296. (increase-left-margin from to (- inc)))
  297. (defun increase-right-margin (from to inc)
  298. "Increase the right-margin of the region.
  299. With no prefix argument, increase the right margin by `standard-indent'.
  300. A prefix arg (optional third arg INC noninteractively) specifies the amount
  301. to change the margin by, in characters. A negative argument decreases
  302. the right margin width.
  303. If `auto-fill-mode' is active, re-fill the region to fit the new margin."
  304. (interactive "r\nP")
  305. (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  306. (save-excursion
  307. (alter-text-property from to 'right-margin
  308. (lambda (v) (+ inc (or v 0))))
  309. (if auto-fill-function
  310. (fill-region from to nil t t))))
  311. (defun decrease-right-margin (from to inc)
  312. "Make the right margin of the region smaller.
  313. With no prefix argument, decrease the right margin by `standard-indent'.
  314. A prefix arg (optional third arg INC noninteractively) specifies the amount
  315. of width to remove, in characters. A negative argument increases
  316. the right margin width.
  317. If `auto-fill-mode' is active, re-fills region to fit in new margin."
  318. (interactive "*r\nP")
  319. (setq inc (if inc (prefix-numeric-value inc) standard-indent))
  320. (increase-right-margin from to (- inc)))
  321. (defun beginning-of-line-text (&optional n)
  322. "Move to the beginning of the text on this line.
  323. With optional argument, move forward N-1 lines first.
  324. From the beginning of the line, moves past the left-margin indentation, the
  325. fill-prefix, and any indentation used for centering or right-justifying the
  326. line, but does not move past any whitespace that was explicitly inserted
  327. \(such as a tab used to indent the first line of a paragraph)."
  328. (interactive "p")
  329. (beginning-of-line n)
  330. (skip-chars-forward " \t")
  331. ;; Skip over fill-prefix.
  332. (if (and fill-prefix
  333. (not (string-equal fill-prefix "")))
  334. (if (equal fill-prefix
  335. (buffer-substring
  336. (point) (min (point-max) (+ (length fill-prefix) (point)))))
  337. (forward-char (length fill-prefix)))
  338. (if (and adaptive-fill-mode adaptive-fill-regexp
  339. (looking-at adaptive-fill-regexp))
  340. (goto-char (match-end 0))))
  341. ;; Skip centering or flushright indentation
  342. (if (memq (current-justification) '(center right))
  343. (skip-chars-forward " \t")))
  344. (defvar indent-region-function nil
  345. "Short cut function to indent region using `indent-according-to-mode'.
  346. A value of nil means really run `indent-according-to-mode' on each line.")
  347. (defun indent-region (start end &optional column)
  348. "Indent each nonblank line in the region.
  349. A numeric prefix argument specifies a column: indent each line to that column.
  350. With no prefix argument, the command chooses one of these methods and
  351. indents all the lines with it:
  352. 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
  353. beginning of each line in the region that does not already begin
  354. with it.
  355. 2) If `indent-region-function' is non-nil, call that function
  356. to indent the region.
  357. 3) Indent each line via `indent-according-to-mode'.
  358. Called from a program, START and END specify the region to indent.
  359. If the third argument COLUMN is an integer, it specifies the
  360. column to indent to; if it is nil, use one of the three methods above."
  361. (interactive "r\nP")
  362. (cond
  363. (column
  364. (setq column (prefix-numeric-value column))
  365. (save-excursion
  366. (goto-char end)
  367. (setq end (point-marker))
  368. (goto-char start)
  369. (or (bolp) (forward-line 1))
  370. (while (< (point) end)
  371. (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  372. (or (eolp)
  373. (indent-to column 0))
  374. (forward-line 1))
  375. (move-marker end nil)))
  376. (fill-prefix
  377. (save-excursion
  378. (goto-char end)
  379. (setq end (point-marker))
  380. (goto-char start)
  381. (let ((regexp (regexp-quote fill-prefix)))
  382. (while (< (point) end)
  383. (or (looking-at regexp)
  384. (and (bolp) (eolp))
  385. (insert fill-prefix))
  386. (forward-line 1)))))
  387. (indent-region-function
  388. (funcall indent-region-function start end))
  389. (t
  390. (save-excursion
  391. (setq end (copy-marker end))
  392. (goto-char start)
  393. (while (< (point) end)
  394. (or (and (bolp) (eolp))
  395. (indent-according-to-mode))
  396. (forward-line 1))
  397. (move-marker end nil))))
  398. ;; In most cases, reindenting modifies the buffer, but it may also
  399. ;; leave it unmodified, in which case we have to deactivate the mark
  400. ;; by hand.
  401. (deactivate-mark))
  402. (defun indent-relative-maybe ()
  403. "Indent a new line like previous nonblank line.
  404. If the previous nonblank line has no indent points beyond the
  405. column point starts at, this command does nothing.
  406. See also `indent-relative'."
  407. (interactive)
  408. (indent-relative t))
  409. (defun indent-relative (&optional unindented-ok)
  410. "Space out to under next indent point in previous nonblank line.
  411. An indent point is a non-whitespace character following whitespace.
  412. The following line shows the indentation points in this line.
  413. ^ ^ ^ ^ ^ ^ ^ ^ ^
  414. If the previous nonblank line has no indent points beyond the
  415. column point starts at, `tab-to-tab-stop' is done instead, unless
  416. this command is invoked with a numeric argument, in which case it
  417. does nothing.
  418. See also `indent-relative-maybe'."
  419. (interactive "P")
  420. (if (and abbrev-mode
  421. (eq (char-syntax (preceding-char)) ?w))
  422. (expand-abbrev))
  423. (let ((start-column (current-column))
  424. indent)
  425. (save-excursion
  426. (beginning-of-line)
  427. (if (re-search-backward "^[^\n]" nil t)
  428. (let ((end (save-excursion (forward-line 1) (point))))
  429. (move-to-column start-column)
  430. ;; Is start-column inside a tab on this line?
  431. (if (> (current-column) start-column)
  432. (backward-char 1))
  433. (or (looking-at "[ \t]")
  434. unindented-ok
  435. (skip-chars-forward "^ \t" end))
  436. (skip-chars-forward " \t" end)
  437. (or (= (point) end) (setq indent (current-column))))))
  438. (if indent
  439. (let ((opoint (point-marker)))
  440. (indent-to indent 0)
  441. (if (> opoint (point))
  442. (goto-char opoint))
  443. (move-marker opoint nil))
  444. (tab-to-tab-stop))))
  445. (defcustom tab-stop-list
  446. '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
  447. "List of tab stop positions used by `tab-to-tab-stop'.
  448. This should be a list of integers, ordered from smallest to largest."
  449. :group 'indent
  450. :type '(repeat integer))
  451. (put 'tab-stop-list 'safe-local-variable 'listp)
  452. (defvar edit-tab-stops-map
  453. (let ((map (make-sparse-keymap)))
  454. (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
  455. (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
  456. map)
  457. "Keymap used in `edit-tab-stops'.")
  458. (defvar edit-tab-stops-buffer nil
  459. "Buffer whose tab stops are being edited.
  460. This matters if the variable `tab-stop-list' is local in that buffer.")
  461. (defun edit-tab-stops ()
  462. "Edit the tab stops used by `tab-to-tab-stop'.
  463. Creates a buffer *Tab Stops* containing text describing the tab stops.
  464. A colon indicates a column where there is a tab stop.
  465. You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
  466. (interactive)
  467. (setq edit-tab-stops-buffer (current-buffer))
  468. (switch-to-buffer (get-buffer-create "*Tab Stops*"))
  469. (use-local-map edit-tab-stops-map)
  470. (make-local-variable 'indent-tabs-mode)
  471. (setq indent-tabs-mode nil)
  472. (overwrite-mode 1)
  473. (setq truncate-lines t)
  474. (erase-buffer)
  475. (let ((tabs tab-stop-list))
  476. (while tabs
  477. (indent-to (car tabs) 0)
  478. (insert ?:)
  479. (setq tabs (cdr tabs))))
  480. (let ((count 0))
  481. (insert ?\n)
  482. (while (< count 8)
  483. (insert (+ count ?0))
  484. (insert " ")
  485. (setq count (1+ count)))
  486. (insert ?\n)
  487. (while (> count 0)
  488. (insert "0123456789")
  489. (setq count (1- count))))
  490. (insert "\nTo install changes, type C-c C-c")
  491. (goto-char (point-min)))
  492. (defun edit-tab-stops-note-changes ()
  493. "Put edited tab stops into effect."
  494. (interactive)
  495. (let (tabs)
  496. (save-excursion
  497. (goto-char 1)
  498. (end-of-line)
  499. (while (search-backward ":" nil t)
  500. (setq tabs (cons (current-column) tabs))))
  501. (bury-buffer (prog1 (current-buffer)
  502. (switch-to-buffer edit-tab-stops-buffer)))
  503. (setq tab-stop-list tabs))
  504. (message "Tab stops installed"))
  505. (defun tab-to-tab-stop ()
  506. "Insert spaces or tabs to next defined tab-stop column.
  507. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  508. Use \\[edit-tab-stops] to edit them interactively."
  509. (interactive)
  510. (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
  511. (expand-abbrev))
  512. (let ((tabs tab-stop-list))
  513. (while (and tabs (>= (current-column) (car tabs)))
  514. (setq tabs (cdr tabs)))
  515. (if tabs
  516. (progn
  517. (delete-horizontal-space t)
  518. (indent-to (car tabs)))
  519. (insert ?\s))))
  520. (defun move-to-tab-stop ()
  521. "Move point to next defined tab-stop column.
  522. The variable `tab-stop-list' is a list of columns at which there are tab stops.
  523. Use \\[edit-tab-stops] to edit them interactively."
  524. (interactive)
  525. (let ((tabs tab-stop-list))
  526. (while (and tabs (>= (current-column) (car tabs)))
  527. (setq tabs (cdr tabs)))
  528. (if tabs
  529. (let ((before (point)))
  530. (move-to-column (car tabs) t)
  531. (save-excursion
  532. (goto-char before)
  533. ;; If we just added a tab, or moved over one,
  534. ;; delete any superfluous spaces before the old point.
  535. (if (and (eq (preceding-char) ?\s)
  536. (eq (following-char) ?\t))
  537. (let ((tabend (* (/ (current-column) tab-width) tab-width)))
  538. (while (and (> (current-column) tabend)
  539. (eq (preceding-char) ?\s))
  540. (forward-char -1))
  541. (delete-region (point) before))))))))
  542. (define-key global-map "\t" 'indent-for-tab-command)
  543. (define-key esc-map "\C-\\" 'indent-region)
  544. (define-key ctl-x-map "\t" 'indent-rigidly)
  545. (define-key esc-map "i" 'tab-to-tab-stop)
  546. ;;; indent.el ends here