27327
|
1 ;;; align --- align text to a specific column, by regexp
|
|
2
|
|
3 ;; Copyright (C) 1999, 2000 Free Sofware Foundation
|
|
4
|
|
5 ;; Author: John Wiegley <johnw@gnu.org>
|
|
6 ;; Keywords: convenience languages lisp
|
|
7 ;; X-URL: http://www.emacs.org/~johnw/emacs.html
|
|
8
|
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
12 ;; it under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 ;; GNU General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; This mode allows you to align regions in a context-sensitive fashion.
|
|
29 ;; The classic use is to align assignments:
|
|
30 ;;
|
|
31 ;; int a = 1;
|
|
32 ;; short foo = 2;
|
|
33 ;; double blah = 4;
|
|
34 ;;
|
|
35 ;; becomes
|
|
36 ;;
|
|
37 ;; int a = 1;
|
|
38 ;; short foo = 2;
|
|
39 ;; double blah = 4;
|
|
40
|
|
41 ;;; Usage:
|
|
42
|
|
43 ;; There are several variables which define how certain "categories"
|
|
44 ;; of syntax are to be treated. These variables go by the name
|
|
45 ;; `align-CATEGORY-modes'. For example, "c++" is such a category.
|
|
46 ;; There are several rules which apply to c++, but since several other
|
|
47 ;; languages have a syntax similar to c++ (e.g., c, java, etc), these
|
|
48 ;; modes are treated as belonging to the same category.
|
|
49 ;;
|
|
50 ;; If you want to add a new mode under a certain category, just
|
|
51 ;; customize that list, or add the new mode manually. For example, to
|
|
52 ;; make jde-mode a c++ category mode, use this code in your .emacs
|
|
53 ;; file:
|
|
54 ;;
|
|
55 ;; (setq align-c++-modes (cons 'jde-mode align-c++-modes))
|
|
56
|
|
57 ;; In some programming modes, it's useful to have the aligner run only
|
|
58 ;; after indentation is performed. To achieve this, customize or set
|
|
59 ;; the variable `align-indent-before-aligning' to t.
|
|
60
|
|
61 ;;; Module Authors:
|
|
62
|
|
63 ;; In order to incorporate align's functionality into your own
|
|
64 ;; modules, there are only a few steps you have to follow.
|
|
65
|
|
66 ;; 1. Require or load in the align.el library.
|
|
67 ;;
|
|
68 ;; 2. Define your alignment and exclusion rules lists, either
|
|
69 ;; customizable or not.
|
|
70 ;;
|
|
71 ;; 3. In your mode function, set the variables
|
|
72 ;; `align-mode-rules-list' and `align-mode-exclude-rules-list'
|
|
73 ;; to your own rules lists.
|
|
74
|
|
75 ;; If there is any need to add your mode name to one of the
|
|
76 ;; align-?-modes variables (for example, `align-dq-string-modes'), use
|
|
77 ;; `add-to-list', or some similar function which checks first to see
|
|
78 ;; if the value is already there. Since the user may customize that
|
|
79 ;; mode list, and then write your mode name into their .emacs file,
|
|
80 ;; causing the symbol already to be present the next time they load
|
|
81 ;; your package.
|
|
82
|
|
83 ;; Example:
|
|
84 ;;
|
|
85 ;; (require 'align)
|
|
86 ;;
|
|
87 ;; (defcustom my-align-rules-list
|
|
88 ;; '((my-rule
|
|
89 ;; (regexp . "Sample")))
|
|
90 ;; :type align-rules-list-type
|
|
91 ;; :group 'my-package)
|
|
92 ;;
|
|
93 ;; (put 'my-align-rules-list 'risky-local-variable t)
|
|
94 ;;
|
|
95 ;; (add-to-list 'align-dq-string-modes 'my-package-mode)
|
|
96 ;; (add-to-list 'align-open-comment-modes 'my-package-mode)
|
|
97 ;;
|
|
98 ;; (defun my-mode ()
|
|
99 ;; ...
|
|
100 ;; (setq align-mode-rules-list my-align-rules-list))
|
|
101 ;;
|
|
102 ;; Note that if you need to install your own exclusion rules, then you
|
|
103 ;; will also need to reproduce any double-quoted string, or open
|
|
104 ;; comment exclusion rules that are defined in the standard
|
|
105 ;; `align-exclude-rules-list'. At the moment there is no convenient
|
|
106 ;; way to mix both mode-local and global rules lists.
|
|
107
|
|
108 ;;; History:
|
|
109
|
|
110 ;; Version 1.0 was created in the earlier part of 1996, using a very
|
|
111 ;; simple algorithm that understand only basic regular expressions.
|
|
112 ;; Parts of the code were broken up and included in vhdl-mode.el
|
|
113 ;; around this time. After several comments from users, and a need to
|
|
114 ;; find a more robust, performant algorithm, 2.0 was born in late
|
|
115 ;; 1998. Many different approaches were taken (mostly due to the
|
|
116 ;; complexity of TeX tables), but finally a scheme was discovered
|
|
117 ;; which worked fairly well for most common usage cases. Development
|
|
118 ;; beyond version 2.8 is not planned, except for problems that users
|
|
119 ;; might encounter.
|
|
120
|
|
121 ;;; Code:
|
|
122
|
|
123 (defgroup align nil
|
|
124 "Align text to a specific column, by regexp."
|
|
125 :group 'fill)
|
|
126
|
|
127 ;;; User Variables:
|
|
128
|
|
129 (defcustom align-load-hook nil
|
|
130 "*Hook that gets run after the aligner has been loaded."
|
|
131 :type 'hook
|
|
132 :group 'align)
|
|
133
|
|
134 (defcustom align-indent-before-aligning nil
|
|
135 "*If non-nil, indent the marked region before aligning it."
|
|
136 :type 'boolean
|
|
137 :group 'align)
|
|
138
|
|
139 (defcustom align-default-spacing 1
|
|
140 "*An integer that represents the default amount of padding to use.
|
|
141 If `align-to-tab-stop' is non-nil, this will represent the number of
|
|
142 tab stops to use for alignment, rather than the number of spaces.
|
|
143 Each alignment rule can optionally override both this variable. See
|
|
144 `align-mode-alist'."
|
|
145 :type 'integer
|
|
146 :group 'align)
|
|
147
|
|
148 (defcustom align-to-tab-stop 'indent-tabs-mode
|
|
149 "*If non-nil, alignments will always fall on a tab boundary.
|
|
150 It may also be a symbol, whose value will be taken."
|
|
151 :type '(choice (const nil) symbol)
|
|
152 :group 'align)
|
|
153
|
|
154 (defcustom align-region-heuristic 500
|
|
155 "*If non-nil, used as a heuristic by `align-current'.
|
|
156 Since each alignment rule can possibly have its own set of alignment
|
|
157 sections (whenever `align-region-separate' is non-nil, and not a
|
|
158 string), this heuristic is used to determine how far before and after
|
|
159 point we should search in looking for a region separator. Larger
|
|
160 values can mean slower perform in large files, although smaller values
|
|
161 may cause unexpected behavior at times."
|
|
162 :type 'integer
|
|
163 :group 'align)
|
|
164
|
|
165 (defcustom align-highlight-change-face 'highlight
|
|
166 "*The face to highlight with if changes are necessary."
|
|
167 :type 'face
|
|
168 :group 'align)
|
|
169
|
|
170 (defcustom align-highlight-nochange-face 'secondary-selection
|
|
171 "*The face to highlight with if no changes are necessary."
|
|
172 :type 'face
|
|
173 :group 'align)
|
|
174
|
|
175 (defcustom align-large-region 10000
|
|
176 "*If an integer, defines what constitutes a \"large\" region.
|
|
177 If nil,then no messages will ever be printed to the minibuffer."
|
|
178 :type 'integer
|
|
179 :group 'align)
|
|
180
|
|
181 (defcustom align-c++-modes '(c++-mode c-mode java-mode)
|
|
182 "*A list of modes whose syntax resembles C/C++."
|
|
183 :type '(repeat symbol)
|
|
184 :group 'align)
|
|
185
|
|
186 (defcustom align-perl-modes '(perl-mode cperl-mode)
|
|
187 "*A list of modes where perl syntax is to be seen."
|
|
188 :type '(repeat symbol)
|
|
189 :group 'align)
|
|
190
|
|
191 (defcustom align-lisp-modes
|
|
192 '(emacs-lisp-mode lisp-interaction-mode lisp-mode scheme-mode)
|
|
193 "*A list of modes whose syntax resembles Lisp."
|
|
194 :type '(repeat symbol)
|
|
195 :group 'align)
|
|
196
|
|
197 (defcustom align-tex-modes
|
|
198 '(tex-mode plain-tex-mode latex-mode slitex-mode)
|
|
199 "*A list of modes whose syntax resembles TeX (and family)."
|
|
200 :type '(repeat symbol)
|
|
201 :group 'align)
|
|
202
|
|
203 (defcustom align-text-modes '(text-mode outline-mode)
|
|
204 "*A list of modes whose content is plain text."
|
|
205 :type '(repeat symbol)
|
|
206 :group 'align)
|
|
207
|
|
208 (defcustom align-dq-string-modes (append align-lisp-modes
|
|
209 align-c++-modes
|
|
210 align-perl-modes)
|
|
211 "*A list of modes where double quoted strings should be excluded."
|
|
212 :type '(repeat symbol)
|
|
213 :group 'align)
|
|
214
|
|
215 (defcustom align-sq-string-modes align-perl-modes
|
|
216 "*A list of modes where single quoted strings should be excluded."
|
|
217 :type '(repeat symbol)
|
|
218 :group 'align)
|
|
219
|
|
220 (defcustom align-open-comment-modes (append align-lisp-modes
|
|
221 align-c++-modes
|
|
222 align-perl-modes
|
|
223 '(makefile-mode))
|
|
224 "*A list of modes with a single-line comment syntax.
|
|
225 These are comments as in Lisp, which have a beginning but, end with
|
|
226 the line (i.e., `comment-end' is an empty string)."
|
|
227 :type '(repeat symbol)
|
|
228 :group 'align)
|
|
229
|
|
230 (defcustom align-region-separate "^\\s-*[{}]?\\s-*$"
|
|
231 "*Select the method by which alignment sections will be separated.
|
|
232 If this is a symbol, that symbol's value will be used.
|
|
233
|
|
234 For the sake of clarification, consider the following example, which
|
|
235 will be referred to in the descriptions below.
|
|
236
|
|
237 int alpha = 1; /* one */
|
|
238 double beta = 2.0;
|
|
239 long gamma; /* ten */
|
|
240
|
|
241 unsigned int delta = 1; /* one */
|
|
242 long double epsilon = 3.0;
|
|
243 long long omega; /* ten */
|
|
244
|
|
245 The possible settings for `align-region-separate' are:
|
|
246
|
|
247 `entire' The entire region being aligned will be considered as a
|
|
248 single alignment section. Assuming that comments were not
|
|
249 being aligned to a particular column, the example would
|
|
250 become:
|
|
251
|
|
252 int alpha = 1; /* one */
|
|
253 double beta = 2.0;
|
|
254 long gamma; /* ten */
|
|
255
|
|
256 unsigned int delta = 1; /* one */
|
|
257 long double epsilon;
|
|
258 long long chi = 10; /* ten */
|
|
259
|
|
260 `group' Each contiguous set of lines where a specific alignment
|
|
261 occurs is considered a section for that alignment rule.
|
|
262 Note that each rule will may have any entirely different
|
|
263 set of section divisions than another.
|
|
264
|
|
265 int alpha = 1; /* one */
|
|
266 double beta = 2.0;
|
|
267 long gamma; /* ten */
|
|
268
|
|
269 unsigned int delta = 1; /* one */
|
|
270 long double epsilon;
|
|
271 long long chi = 10; /* ten */
|
|
272
|
|
273 `largest' When contiguous rule sets overlap, the largest section
|
|
274 described will be taken as the alignment section for each
|
|
275 rule touched by that section.
|
|
276
|
|
277 int alpha = 1; /* one */
|
|
278 double beta = 2.0;
|
|
279 long gamma; /* ten */
|
|
280
|
|
281 unsigned int delta = 1; /* one */
|
|
282 long double epsilon;
|
|
283 long long chi = 10; /* ten */
|
|
284
|
|
285 NOTE: This option is not supported yet, due to algorithmic
|
|
286 issues which haven't been satisfactorily resolved. There
|
|
287 are ways to do it, but they're both ugly and resource
|
|
288 consumptive.
|
|
289
|
|
290 regexp A regular expression string which defines the section
|
|
291 divider. If the mode you're in has a consistent divider
|
|
292 between sections, the behavior will be very similar to
|
|
293 `largest', and faster. But if the mode does not use clear
|
|
294 separators (for example, if you collapse your braces onto
|
|
295 the preceding statement in C or perl), `largest' is
|
|
296 probably the better alternative.
|
|
297
|
|
298 function A function that will be passed the beginning and ending
|
|
299 locations of the region in which to look for the section
|
|
300 separator. At the very beginning of the attempt to align,
|
|
301 both of these parameters will be nil, in which case the
|
|
302 function should return non-nil if it wants each rule to
|
|
303 define its own section, or nil if it wants the largest
|
|
304 section found to be used as the common section for all rules
|
|
305 that occur there.
|
|
306
|
|
307 list A list of markers within the buffer that represent where
|
|
308 the section dividers lie. Be certain to use markers! For
|
|
309 when the aligning begins, the ensuing contract/expanding of
|
|
310 whitespace will throw off any non-marker positions.
|
|
311
|
|
312 This method is intended for use in Lisp programs, and not
|
|
313 by the user."
|
|
314 :type '(choice
|
|
315 (const :tag "Entire region is one section" entire)
|
|
316 (const :tag "Align by contiguous groups" group)
|
|
317 ; (const largest)
|
|
318 (regexp :tag "Regexp defines section boundaries")
|
|
319 (function :tag "Function defines section boundaries"))
|
|
320 :group 'align)
|
|
321
|
|
322 (put 'align-region-separate 'risky-local-variable t)
|
|
323
|
|
324 (defvar align-rules-list-type
|
|
325 '(repeat
|
|
326 (cons
|
|
327 :tag "Alignment rule"
|
|
328 (symbol :tag "Title")
|
|
329 (cons :tag "Required attributes"
|
|
330 (cons :tag "Regexp"
|
|
331 (const :tag "(Regular expression to match)" regexp)
|
|
332 (choice :value "\\(\\s-+\\)" regexp function))
|
|
333 (repeat
|
|
334 :tag "Optional attributes"
|
|
335 (choice
|
|
336 (cons :tag "Repeat"
|
|
337 (const :tag "(Repeat this rule throughout line)"
|
|
338 repeat)
|
|
339 (boolean :value t))
|
|
340 (cons :tag "Paren group"
|
|
341 (const :tag "(Parenthesis group to use)" group)
|
|
342 (choice :value 2
|
|
343 integer (repeat integer)))
|
|
344 (cons :tag "Modes"
|
|
345 (const :tag "(Modes where this rule applies)" modes)
|
|
346 (sexp :value (text-mode)))
|
|
347 (cons :tag "Case-fold"
|
|
348 (const :tag "(Should case be ignored for this rule)"
|
|
349 case-fold)
|
|
350 (boolean :value t))
|
|
351 (cons :tag "To Tab Stop"
|
|
352 (const :tag "(Should rule align to tab stops)"
|
|
353 tab-stop)
|
|
354 (boolean :value nil))
|
|
355 (cons :tag "Valid"
|
|
356 (const :tag "(Return non-nil if rule is valid)"
|
|
357 valid)
|
|
358 (function :value t))
|
|
359 (cons :tag "Run If"
|
|
360 (const :tag "(Return non-nil if rule should run)"
|
|
361 run-if)
|
|
362 (function :value t))
|
|
363 (cons :tag "Column"
|
|
364 (const :tag "(Column to fix alignment at)" column)
|
|
365 (choice :value comment-column
|
|
366 integer symbol))
|
|
367 (cons :tag "Spacing"
|
|
368 (const :tag "(Amount of spacing to use)" spacing)
|
|
369 (integer :value 1))
|
|
370 (cons :tag "Justify"
|
|
371 (const :tag "(Should text be right justified)"
|
|
372 justify)
|
|
373 (boolean :value t))
|
|
374 ;; make sure this stays up-to-date with any changes
|
|
375 ;; in `align-region-separate'
|
|
376 (cons :tag "Separate"
|
|
377 (const :tag "(Separation to use for this rule)"
|
|
378 separate)
|
|
379 (choice :value "^\\s-*$"
|
|
380 (const entire)
|
|
381 (const group)
|
|
382 ; (const largest)
|
|
383 regexp function)))))))
|
|
384 "The `type' form for any `align-rules-list' variable.")
|
|
385
|
|
386 (unless (functionp 'c-guess-basic-syntax)
|
|
387 (autoload 'c-guess-basic-syntax "cc-engine"))
|
|
388
|
|
389 (defcustom align-rules-list
|
|
390 `((lisp-second-arg
|
|
391 (regexp . "\\(^\\s-+[^( \t\n]\\|(\\(\\S-+\\)\\s-+\\)\\S-+\\(\\s-+\\)")
|
|
392 (group . 3)
|
|
393 (modes . align-lisp-modes)
|
|
394 (run-if . ,(function (lambda () current-prefix-arg))))
|
|
395
|
|
396 (lisp-alist-dot
|
|
397 (regexp . "\\(\\s-*\\)\\.\\(\\s-*\\)")
|
|
398 (group . (1 2))
|
|
399 (modes . align-lisp-modes))
|
|
400
|
|
401 (open-comment
|
|
402 (regexp . ,(function
|
|
403 (lambda (end reverse)
|
|
404 (funcall (if reverse 're-search-backward
|
|
405 're-search-forward)
|
|
406 (concat "[^ \t\n\\\\]"
|
|
407 (regexp-quote comment-start)
|
|
408 "\\(.+\\)$") end t))))
|
|
409 (modes . align-open-comment-modes))
|
|
410
|
|
411 (c-macro-definition
|
|
412 (regexp . "^\\s-*#\\s-*define\\s-+\\S-+\\(\\s-+\\)")
|
|
413 (modes . align-c++-modes))
|
|
414
|
|
415 (c-variable-declaration
|
|
416 (regexp . ,(concat "[*&0-9A-Za-z_]>?[&*]*\\(\\s-+[*&]*\\)"
|
|
417 "[A-Za-z_][0-9A-Za-z:_]*\\s-*\\(\\()\\|"
|
|
418 "=[^=\n].*\\|(.*)\\|\\(\\[.*\\]\\)*\\)?"
|
|
419 "\\s-*[;,]\\|)\\s-*$\\)"))
|
|
420 (group . 1)
|
|
421 (modes . align-c++-modes)
|
|
422 (justify . t)
|
|
423 (valid
|
|
424 . ,(function
|
|
425 (lambda ()
|
|
426 (not (or (save-excursion
|
|
427 (goto-char (match-beginning 1))
|
|
428 (backward-word 1)
|
|
429 (looking-at
|
|
430 "\\(goto\\|return\\|new\\|delete\\|throw\\)"))
|
|
431 (if (and (boundp 'font-lock-mode) font-lock-mode)
|
|
432 (eq (cadr (memq 'face (text-properties-at (point))))
|
|
433 'font-lock-comment-face)
|
|
434 (eq (caar (c-guess-basic-syntax)) 'c))))))))
|
|
435
|
|
436 (c-assignment
|
|
437 (regexp . ,(concat "[^-=!^&*+<>/| \t\n]\\(\\s-*[-=!^&*+<>/|]*\\)"
|
|
438 "=\\(\\s-*\\)\\([^= \t\n]\\|$\\)"))
|
|
439 (group . (1 2))
|
|
440 (modes . align-c++-modes)
|
|
441 (justify . t)
|
|
442 (tab-stop . nil))
|
|
443
|
|
444 (perl-assignment
|
|
445 (regexp . ,(concat "[^=!^&*-+<>/| \t\n]\\(\\s-*\\)=[~>]?"
|
|
446 "\\(\\s-*\\)\\([^>= \t\n]\\|$\\)"))
|
|
447 (group . (1 2))
|
|
448 (modes . align-perl-modes)
|
|
449 (tab-stop . nil))
|
|
450
|
|
451 (make-assignment
|
|
452 (regexp . "^\\s-*\\w+\\(\\s-*\\):?=\\(\\s-*\\)\\([^\t\n \\\\]\\|$\\)")
|
|
453 (group . (1 2))
|
|
454 (modes . '(makefile-mode))
|
|
455 (tab-stop . nil))
|
|
456
|
|
457 (c-comma-delimiter
|
|
458 (regexp . ",\\(\\s-*\\)[^/ \t\n]")
|
|
459 (repeat . t)
|
|
460 (modes . align-c++-modes)
|
|
461 (run-if . ,(function (lambda () current-prefix-arg))))
|
|
462 ; (valid
|
|
463 ; . ,(function
|
|
464 ; (lambda ()
|
|
465 ; (memq (caar (c-guess-basic-syntax))
|
|
466 ; '(brace-list-intro
|
|
467 ; brace-list-entry
|
|
468 ; brace-entry-open))))))
|
|
469
|
|
470 ;; With a prefix argument, comma delimiter will be aligned. Since
|
|
471 ;; perl-mode doesn't give us enough syntactic information (and we
|
|
472 ;; don't do our own parsing yet), this rule is too destructive to
|
|
473 ;; run normally.
|
|
474 (perl-comma-delimiter
|
|
475 (regexp . ",\\(\\s-*\\)[^# \t\n]")
|
|
476 (repeat . t)
|
|
477 (modes . align-perl-modes)
|
|
478 (run-if . ,(function (lambda () current-prefix-arg))))
|
|
479
|
|
480 (c++-comment
|
|
481 (regexp . "\\(\\s-*\\)\\(//.*\\|/\\*.*\\*/\\s-*\\)$")
|
|
482 (modes . align-c++-modes)
|
|
483 (column . comment-column)
|
|
484 (valid . ,(function
|
|
485 (lambda ()
|
|
486 (save-excursion
|
|
487 (goto-char (match-beginning 1))
|
|
488 (not (bolp)))))))
|
|
489
|
|
490 (c-macro-line-continuation
|
|
491 (regexp . "\\(\\s-*\\)\\\\$")
|
|
492 (modes . (append align-c++-modes '(makefile-mode)))
|
|
493 (column . c-backslash-column))
|
|
494 ; (valid
|
|
495 ; . ,(function
|
|
496 ; (lambda ()
|
|
497 ; (memq (caar (c-guess-basic-syntax))
|
|
498 ; '(cpp-macro cpp-macro-cont))))))
|
|
499
|
|
500 (c-chain-logic
|
|
501 (regexp . "\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
|
|
502 (modes . align-c++-modes)
|
|
503 (valid . ,(function
|
|
504 (lambda ()
|
|
505 (save-excursion
|
|
506 (goto-char (match-end 2))
|
|
507 (looking-at "\\s-*\\(/[*/]\\|$\\)"))))))
|
|
508
|
|
509 (perl-chain-logic
|
|
510 (regexp . "\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
|
|
511 (modes . align-perl-modes)
|
|
512 (valid . ,(function
|
|
513 (lambda ()
|
|
514 (save-excursion
|
|
515 (goto-char (match-end 2))
|
|
516 (looking-at "\\s-*\\(#\\|$\\)"))))))
|
|
517
|
|
518 (tex-record-separator
|
|
519 (regexp . ,(function
|
|
520 (lambda (end reverse)
|
|
521 (align-match-tex-pattern "&" end reverse))))
|
|
522 (group . (1 2))
|
|
523 (modes . align-tex-modes)
|
|
524 (repeat . t))
|
|
525
|
|
526 (tex-tabbing-separator
|
|
527 (regexp . ,(function
|
|
528 (lambda (end reverse)
|
|
529 (align-match-tex-pattern "\\\\[=>]" end reverse))))
|
|
530 (group . (1 2))
|
|
531 (modes . align-tex-modes)
|
|
532 (repeat . t)
|
|
533 (run-if . ,(function
|
|
534 (lambda ()
|
|
535 (eq major-mode 'latex-mode)))))
|
|
536
|
|
537 (tex-record-break
|
|
538 (regexp . "\\(\\s-*\\)\\\\\\\\")
|
|
539 (modes . align-tex-modes))
|
|
540
|
|
541 ;; With a numeric prefix argument, or C-u, space delimited text
|
|
542 ;; tables will be aligned.
|
|
543 (text-column
|
|
544 (regexp . "\\(^\\|\\S-\\)\\(\\s-+\\)\\(\\S-\\|$\\)")
|
|
545 (group . 2)
|
|
546 (modes . align-text-modes)
|
|
547 (repeat . t)
|
|
548 (run-if . ,(function
|
|
549 (lambda ()
|
|
550 (and current-prefix-arg
|
|
551 (not (eq '- current-prefix-arg)))))))
|
|
552
|
|
553 ;; With a negative prefix argument, lists of dollar figures will
|
|
554 ;; be aligned.
|
|
555 (text-dollar-figure
|
|
556 (regexp . "\\$?\\(\\s-+[0-9]+\\)\\.")
|
|
557 (modes . align-text-modes)
|
|
558 (justify . t)
|
|
559 (run-if . ,(function
|
|
560 (lambda ()
|
|
561 (eq '- current-prefix-arg))))))
|
|
562 "*An list describing all of the available alignment rules.
|
|
563 The format is:
|
|
564
|
|
565 ((TITLE
|
|
566 (ATTRIBUTE . VALUE) ...)
|
|
567 ...)
|
|
568
|
|
569 The following attributes are meaningful:
|
|
570
|
|
571 `regexp' This required attribute must be either a string describing
|
|
572 a regular expression, or a function (described below).
|
|
573 For every line within the section that this regular
|
|
574 expression matches, the given rule will be applied to that
|
|
575 line. The exclusion rules denote which part(s) of the
|
|
576 line should not be modified; the alignment rules cause the
|
|
577 identified whitespace group to be contracted/expanded such
|
|
578 that the \"alignment character\" (the character
|
|
579 immediately following the identified parenthesis group),
|
|
580 occurs in the same column for every line within the
|
|
581 alignment section (see `align-region-separate' for a
|
|
582 description of how the region is broken up into alignment
|
|
583 sections).
|
|
584
|
|
585 The `regexp' attribute describes how the text should be
|
|
586 treated. Within this regexp, there must be at least one
|
|
587 group of characters (typically whitespace) identified by
|
|
588 the special opening and closing parens used in regexp
|
|
589 expressions (`\\\\(' and `\\\\)') (see the Emacs manual on
|
|
590 the syntax of regular expressions for more info).
|
|
591
|
|
592 If `regexp' is a function, it will be called as a
|
|
593 replacement for `re-search-forward'. This means that it
|
|
594 should return nil if nothing is found to match the rule,
|
|
595 or it should set the match data appropriately, move point
|
|
596 to the end of the match, and return the value of point.
|
|
597
|
|
598 `group' For exclusion rules, the group identifies the range of
|
|
599 characters that should be ignored. For alignment rules,
|
|
600 these are the characters that will be deleted/expanded for
|
|
601 the purposes of alignment. The \"alignment character\" is
|
|
602 always the first character immediately following this
|
|
603 parenthesis group. This attribute may also be a list of
|
|
604 integer, in which case multiple alignment characters will
|
|
605 be aligned, with the list of integer identifying the
|
|
606 whitespace groups which precede them. The default for
|
|
607 this attribute is 1.
|
|
608
|
|
609 `modes' The `modes' attribute, if set, should name a list of
|
|
610 major modes -- or evaluate to such a value -- in which the
|
|
611 rule is valid. If not set, the rule will apply to all
|
|
612 modes.
|
|
613
|
|
614 `case-fold' If `regexp' is an ordinary regular expression string
|
|
615 containing alphabetic character, sometimes you may want
|
|
616 the search to proceed case-insensitively (for languages
|
|
617 that ignore case, such as pascal for example). In that
|
|
618 case, set `case-fold' to nil, and the regular expression
|
|
619 search will ignore case. If `regexp' is set to a
|
|
620 function, that function must handle the job of ignoring
|
|
621 case by itself.
|
|
622
|
|
623 `tab-stop' If the `tab-stop' attribute is set, and non-nil, the
|
|
624 alignment character will always fall on a tab stop
|
|
625 (whether it uses tabs to get there or not depends on the
|
|
626 value of `indent-tabs-mode'). If the `tab-stop' attribute
|
|
627 is set to nil, tab stops will never be used. Otherwise,
|
|
628 the value of `align-to-tab-stop' determines whether or not
|
|
629 to align to a tab stop. The `tab-stop' attribute may also
|
|
630 be a list of t or nil values, corresponding to the number
|
|
631 of parenthesis groups specified by the `group' attribute.
|
|
632
|
|
633 `repeat' If the `repeat' attribute is present, and non-nil, the
|
|
634 rule will be applied to the line continuously until no
|
|
635 further matches are found.
|
|
636
|
|
637 `valid' If the `valid' attribute is set, it will be used to
|
|
638 determine whether the rule should be invoked. This form
|
|
639 is evaluated after the regular expression match has been
|
|
640 performed, so that it is possible to use the results of
|
|
641 that match to determine whether the alignment should be
|
|
642 performed. The buffer should not be modified during the
|
|
643 evaluation of this form.
|
|
644
|
|
645 `run-if' Like `valid', the `run-if' attribute tests whether the
|
|
646 rule should be run at all -- even before any searches are
|
|
647 done to determine if the rule applies to the alignment
|
|
648 region. This can save time, since `run-if' will only be
|
|
649 run once for each rule. If it returns nil, the rule will
|
|
650 not be attempted.
|
|
651
|
|
652 `column' For alignment rules, if the `column' attribute is set --
|
|
653 which must be an integer, or a symbol whose value is an
|
|
654 integer -- it will be used as the column in which to align
|
|
655 the alignment character. If the text on a particular line
|
|
656 happens to overrun that column, a single space character,
|
|
657 or tab stop (see `align-to-tab-stop') will be added
|
|
658 between the last text character and the alignment
|
|
659 character.
|
|
660
|
|
661 `spacing' Alignment rules may also override the amount of spacing
|
|
662 that would normally be used by providing a `spacing'
|
|
663 attribute. This must be an integer, or a list of integers
|
|
664 corresponding to the number of parenthesis groups matched
|
|
665 by the `group' attribute. If a list of value is used, and
|
|
666 any of those values is nil, `align-default-spacing' will
|
|
667 be used for that subgroup. See `align-default-spacing'
|
|
668 for more details on spacing, tab stops, and how to
|
|
669 indicate how much spacing should be used. If TAB-STOP is
|
|
670 present, it will override the value of `align-to-tab-stop'
|
|
671 for that rule.
|
|
672
|
|
673 `justify' It is possible with `regexp' and `group' to identify a
|
|
674 character group that contains more than just whitespace
|
|
675 characters. By default, any non-whitespace characters in
|
|
676 that group will also be deleted while aligning the
|
|
677 alignment character. However, if the `justify' attribute
|
|
678 is set to a non-nil value, only the initial whitespace
|
|
679 characters within that group will be deleted. This has
|
|
680 the effect of right-justifying the characters that remain,
|
|
681 and can be used for outdenting or just plain old right-
|
|
682 justification.
|
|
683
|
|
684 `separate' Each rule can define its own section separator, which
|
|
685 describes how to identify the separation of \"sections\"
|
|
686 within the region to be aligned. Setting the `separate'
|
|
687 attribute overrides the value of `align-region-separate'
|
|
688 (see the documentation of that variable for possible
|
|
689 values), and any separation argument passed to `align'."
|
|
690 :type align-rules-list-type
|
|
691 :group 'align)
|
|
692
|
|
693 (put 'align-rules-list 'risky-local-variable t)
|
|
694
|
|
695 (defvar align-exclude-rules-list-type
|
|
696 '(repeat
|
|
697 (cons
|
|
698 :tag "Exclusion rule"
|
|
699 (symbol :tag "Title")
|
|
700 (cons :tag "Required attributes"
|
|
701 (cons :tag "Regexp"
|
|
702 (const :tag "(Regular expression to match)" regexp)
|
|
703 (choice :value "\\(\\s-+\\)" regexp function))
|
|
704 (repeat
|
|
705 :tag "Optional attributes"
|
|
706 (choice
|
|
707 (cons :tag "Repeat"
|
|
708 (const :tag "(Repeat this rule throughout line)"
|
|
709 repeat)
|
|
710 (boolean :value t))
|
|
711 (cons :tag "Paren group"
|
|
712 (const :tag "(Parenthesis group to use)" group)
|
|
713 (choice :value 2
|
|
714 integer (repeat integer)))
|
|
715 (cons :tag "Modes"
|
|
716 (const :tag "(Modes where this rule applies)" modes)
|
|
717 (sexp :value (text-mode)))
|
|
718 (cons :tag "Case-fold"
|
|
719 (const :tag "(Should case be ignored for this rule)"
|
|
720 case-fold)
|
|
721 (boolean :value t)))))))
|
|
722 "The `type' form for any `align-exclude-rules-list' variable.")
|
|
723
|
|
724 (defcustom align-exclude-rules-list
|
|
725 `((exc-dq-string
|
|
726 (regexp . "\"\\([^\"\n]+\\)\"")
|
|
727 (repeat . t)
|
|
728 (modes . align-dq-string-modes))
|
|
729
|
|
730 (exc-sq-string
|
|
731 (regexp . "'\\([^'\n]+\\)'")
|
|
732 (repeat . t)
|
|
733 (modes . align-sq-string-modes))
|
|
734
|
|
735 (exc-open-comment
|
|
736 (regexp
|
|
737 . ,(function
|
|
738 (lambda (end reverse)
|
|
739 (funcall (if reverse 're-search-backward
|
|
740 're-search-forward)
|
|
741 (concat "[^ \t\n\\\\]"
|
|
742 (regexp-quote comment-start)
|
|
743 "\\(.+\\)$") end t))))
|
|
744 (modes . align-open-comment-modes))
|
|
745
|
|
746 (exc-c-comment
|
|
747 (regexp . "/\\*\\(.+\\)\\*/")
|
|
748 (repeat . t)
|
|
749 (modes . align-c++-modes))
|
|
750
|
|
751 (exc-c-func-params
|
|
752 (regexp . "(\\([^)\n]+\\))")
|
|
753 (repeat . t)
|
|
754 (modes . align-c++-modes))
|
|
755
|
|
756 (exc-c-macro
|
|
757 (regexp . "^\\s-*#\\s-*\\(if\\w*\\|endif\\)\\(.*\\)$")
|
|
758 (group . 2)
|
|
759 (modes . align-c++-modes)))
|
|
760 "*An list describing text that should be excluded from alignment.
|
|
761 See the documentation for `align-rules-list' for more info."
|
|
762 :type align-exclude-rules-list-type
|
|
763 :group 'align)
|
|
764
|
|
765 (put 'align-exclude-rules-list 'risky-local-variable t)
|
|
766
|
|
767 ;;; Internal Variables:
|
|
768
|
|
769 (defvar align-mode-rules-list nil
|
|
770 "Alignment rules specific to the current major mode.
|
|
771 See the variable `align-rules-list' for more details.")
|
|
772
|
|
773 (make-variable-buffer-local 'align-mode-rules-list)
|
|
774
|
|
775 (defvar align-mode-exclude-rules-list nil
|
|
776 "Alignment exclusion rules specific to the current major mode.
|
|
777 See the variable `align-exclude-rules-list' for more details.")
|
|
778
|
|
779 (make-variable-buffer-local 'align-mode-exclude-rules-list)
|
|
780
|
|
781 (defvar align-highlight-overlays nil
|
|
782 "The current overlays highlighting the text matched by a rule.")
|
|
783
|
|
784 ;; Sample extension rule set, for vhdl-mode. This should properly be
|
|
785 ;; in vhdl-mode.el itself.
|
|
786
|
|
787 (defcustom align-vhdl-rules-list
|
|
788 `((vhdl-declaration
|
|
789 (regexp . "\\(signal\\|variable\\|constant\\)\\(\\s-+\\)\\S-")
|
|
790 (group . 2))
|
|
791
|
|
792 (vhdl-case
|
|
793 (regexp . "\\(others\\|[^ \t\n=<]\\)\\(\\s-*\\)=>\\(\\s-*\\)\\S-")
|
|
794 (group . (2 3))
|
|
795 (valid
|
|
796 . ,(function
|
|
797 (lambda ()
|
|
798 (not (string= (downcase (match-string 1))
|
|
799 "others"))))))
|
|
800
|
|
801 (vhdl-colon
|
|
802 (regexp . "[^ \t\n:]\\(\\s-*\\):\\(\\s-*\\)[^=\n]")
|
|
803 (group . (1 2)))
|
|
804
|
|
805 (direction
|
|
806 (regexp . ":\\s-*\\(in\\|out\\|inout\\|buffer\\)\\(\\s-*\\)")
|
|
807 (group . 2))
|
|
808
|
|
809 (sig-assign
|
|
810 (regexp . "[^ \t\n=<]\\(\\s-*\\)<=\\(\\s-*\\)\\S-")
|
|
811 (group . (1 2)))
|
|
812
|
|
813 (var-assign
|
|
814 (regexp . "[^ \t\n:]\\(\\s-*\\):="))
|
|
815
|
|
816 (use-entity
|
|
817 (regexp . "\\(\\s-+\\)use\\s-+entity")))
|
|
818 "*Alignment rules for `vhdl-mode'. See `align-rules-list' for more info."
|
|
819 :type align-rules-list-type
|
|
820 :group 'align)
|
|
821
|
|
822 (put 'align-vhdl-rules-list 'risky-local-variable t)
|
|
823
|
|
824 (defun align-set-vhdl-rules ()
|
|
825 "Setup the `align-mode-rules-list' variable for `vhdl-mode'."
|
|
826 (setq align-mode-rules-list align-vhdl-rules-list))
|
|
827
|
|
828 (add-hook 'vhdl-mode-hook 'align-set-vhdl-rules)
|
|
829
|
|
830 (add-to-list 'align-dq-string-modes 'vhdl-mode)
|
|
831 (add-to-list 'align-open-comment-modes 'vhdl-mode)
|
|
832
|
|
833 ;;; User Functions:
|
|
834
|
|
835 ;;;###autoload
|
|
836 (defun align (beg end &optional separate rules exclude-rules)
|
|
837 "Attempt to align a region based on a set of alignment rules.
|
|
838 BEG and END mark the region. If BEG and END are specifically set to
|
|
839 nil (this can only be done programmatically), the beginning and end of
|
|
840 the current alignment section will be calculated based on the location
|
|
841 of point, and the value of `align-region-separate' (or possibly each
|
|
842 rule's `separate' attribute).
|
|
843
|
|
844 If SEPARATE is non-nil, it overrides the value of
|
|
845 `align-region-separate' for all rules, except those that have their
|
|
846 `separate' attribute set.
|
|
847
|
|
848 RULES and EXCLUDE-RULES, if either is non-nil, will replace the
|
|
849 default rule lists defined in `align-rules-list' and
|
|
850 `align-exclude-rules-list'. See `align-rules-list' for more details
|
|
851 on the format of these lists."
|
|
852 (interactive "r")
|
|
853 (let ((separator
|
|
854 (or separate
|
|
855 (if (symbolp align-region-separate)
|
|
856 (symbol-value align-region-separate)
|
|
857 align-region-separate)
|
|
858 'entire)))
|
|
859 (if (not (or ;(eq separator 'largest)
|
|
860 (and (functionp separator)
|
|
861 (not (funcall separator nil nil)))))
|
|
862 (align-region beg end separator
|
|
863 (or rules align-mode-rules-list align-rules-list)
|
|
864 (or exclude-rules align-mode-exclude-rules-list
|
|
865 align-exclude-rules-list))
|
|
866 (let ((sec-first end)
|
|
867 (sec-last beg))
|
|
868 (align-region beg end
|
|
869 (or exclude-rules
|
|
870 align-mode-exclude-rules-list
|
|
871 align-exclude-rules-list) nil
|
|
872 separator
|
|
873 (function
|
|
874 (lambda (b e mode)
|
|
875 (when (and mode (listp mode))
|
|
876 (setq sec-first (min sec-first b)
|
|
877 sec-last (max sec-last e))))))
|
|
878 (if (< sec-first sec-last)
|
|
879 (align-region sec-first sec-last 'entire
|
|
880 (or rules align-mode-rules-list align-rules-list)
|
|
881 (or exclude-rules align-mode-exclude-rules-list
|
|
882 align-exclude-rules-list)))))))
|
|
883
|
|
884 ;;;###autoload
|
|
885 (defun align-regexp (beg end regexp &optional group spacing repeat)
|
|
886 "Align the current region using an ad-hoc rule read from the minibuffer.
|
|
887 BEG and END mark the limits of the region. This function will prompt
|
|
888 for the REGEXP to align with. If no prefix arg was specified, you
|
|
889 only need to supply the characters to be lined up and any preceding
|
|
890 whitespace is replaced. If a prefix arg was specified, the full
|
|
891 regexp with parenthesized whitespace should be supplied; it will also
|
|
892 prompt for which parenthesis GROUP within REGEXP to modify, the amount
|
|
893 of SPACING to use, and whether or not to REPEAT the rule throughout
|
|
894 the line. See `align-rules-list' for more information about these
|
|
895 options.
|
|
896
|
|
897 For example, let's say you had a list of phone numbers, and wanted to
|
|
898 align them so that the opening parentheses would line up:
|
|
899
|
|
900 Fred (123) 456-7890
|
|
901 Alice (123) 456-7890
|
|
902 Mary-Anne (123) 456-7890
|
|
903 Joe (123) 456-7890
|
|
904
|
|
905 There is no predefined rule to handle this, but you could easily do it
|
|
906 using a REGEXP like \"(\". All you would have to do is to mark the
|
|
907 region, call `align-regexp' and type in that regular expression."
|
|
908 (interactive
|
|
909 (append
|
|
910 (list (min (point) (mark))
|
|
911 (max (point) (mark)))
|
|
912 (if current-prefix-arg
|
|
913 (list (read-string "Complex align using regexp: "
|
|
914 "\\(\\s-*\\)")
|
|
915 (string-to-int
|
|
916 (read-string
|
|
917 "Parenthesis group to modify (justify if negative): " "1"))
|
|
918 (string-to-int
|
|
919 (read-string "Amount of spacing (or column if negative): "
|
|
920 (number-to-string align-default-spacing)))
|
|
921 (y-or-n-p "Repeat throughout line? "))
|
|
922 (list (concat "\\(\\s-*\\)"
|
|
923 (read-string "Align regexp: "))
|
|
924 1 align-default-spacing nil))))
|
|
925 (let ((rule
|
|
926 (list (list nil (cons 'regexp regexp)
|
|
927 (cons 'group (abs group))
|
|
928 (if (< group 0)
|
|
929 (cons 'justify t)
|
|
930 (cons 'bogus nil))
|
|
931 (if (>= spacing 0)
|
|
932 (cons 'spacing spacing)
|
|
933 (cons 'column (abs spacing)))
|
|
934 (cons 'repeat repeat)))))
|
|
935 (align-region beg end 'entire rule nil nil)))
|
|
936
|
|
937 ;;;###autoload
|
|
938 (defun align-entire (beg end &optional rules exclude-rules)
|
|
939 "Align the selected region as if it were one alignment section.
|
|
940 BEG and END mark the extent of the region. If RULES or EXCLUDE-RULES
|
|
941 is set to a list of rules (see `align-rules-list'), it can be used to
|
|
942 override the default alignment rules that would have been used to
|
|
943 align that section."
|
|
944 (interactive "r")
|
|
945 (align beg end 'entire rules exclude-rules))
|
|
946
|
|
947 ;;;###autoload
|
|
948 (defun align-current (&optional rules exclude-rules)
|
|
949 "Call `align' on the current alignment section.
|
|
950 This function assumes you want to align only the current section, and
|
|
951 so saves you from having to specify the region. If RULES or
|
|
952 EXCLUDE-RULES is set to a list of rules (see `align-rules-list'), it
|
|
953 can be used to override the default alignment rules that would have
|
|
954 been used to align that section."
|
|
955 (interactive)
|
|
956 (align nil nil nil rules exclude-rules))
|
|
957
|
|
958 ;;;###autoload
|
|
959 (defun align-highlight-rule (beg end title &optional rules exclude-rules)
|
|
960 "Highlight the whitespace which a given rule would have modified.
|
|
961 BEG and END mark the extent of the region. TITLE identifies the rule
|
|
962 that should be highlighted. If RULES or EXCLUDE-RULES is set to a
|
|
963 list of rules (see `align-rules-list'), it can be used to override the
|
|
964 default alignment rules that would have been used to identify the text
|
|
965 to be colored."
|
|
966 (interactive
|
|
967 (list (min (mark) (point))
|
|
968 (max (mark) (point))
|
|
969 (completing-read
|
|
970 "Title of rule to highlight: "
|
|
971 (mapcar
|
|
972 (function
|
|
973 (lambda (rule)
|
|
974 (list (symbol-name (car rule)))))
|
|
975 (append (or align-mode-rules-list align-rules-list)
|
|
976 (or align-mode-exclude-rules-list
|
|
977 align-exclude-rules-list))) nil t)))
|
|
978 (let ((ex-rule (assq (intern title)
|
|
979 (or align-mode-exclude-rules-list
|
|
980 align-exclude-rules-list)))
|
|
981 face)
|
|
982 (align-unhighlight-rule)
|
|
983 (align-region
|
|
984 beg end 'entire
|
|
985 (or rules (if ex-rule
|
|
986 (or exclude-rules align-mode-exclude-rules-list
|
|
987 align-exclude-rules-list)
|
|
988 (or align-mode-rules-list align-rules-list)))
|
|
989 (unless ex-rule (or exclude-rules align-mode-exclude-rules-list
|
|
990 align-exclude-rules-list))
|
|
991 (function
|
|
992 (lambda (b e mode)
|
|
993 (if (and mode (listp mode))
|
|
994 (if (equal (symbol-name (car mode)) title)
|
|
995 (setq face (cons align-highlight-change-face
|
|
996 align-highlight-nochange-face))
|
|
997 (setq face nil))
|
|
998 (when face
|
|
999 (let ((overlay (make-overlay b e)))
|
|
1000 (setq align-highlight-overlays
|
|
1001 (cons overlay align-highlight-overlays))
|
|
1002 (overlay-put overlay 'face
|
|
1003 (if mode
|
|
1004 (car face)
|
|
1005 (cdr face)))))))))))
|
|
1006
|
|
1007 ;;;###autoload
|
|
1008 (defun align-unhighlight-rule ()
|
|
1009 "Remove any highlighting that was added by `align-highlight-rule'."
|
|
1010 (interactive)
|
|
1011 (while align-highlight-overlays
|
|
1012 (delete-overlay (car align-highlight-overlays))
|
|
1013 (setq align-highlight-overlays
|
|
1014 (cdr align-highlight-overlays))))
|
|
1015
|
|
1016 ;;; Internal Functions:
|
|
1017
|
|
1018 (defun align-match-tex-pattern (regexp end &optional reverse)
|
|
1019 "Match REGEXP in TeX mode, counting backslashes appropriately.
|
|
1020 END denotes the end of the region to be searched, while REVERSE, if
|
|
1021 non-nil, indicates that the search should proceed backward from the
|
|
1022 current position."
|
|
1023 (let (result)
|
|
1024 (while
|
|
1025 (and (setq result
|
|
1026 (funcall
|
|
1027 (if reverse 're-search-backward
|
|
1028 're-search-forward)
|
|
1029 (concat "\\(\\s-*\\)" regexp
|
|
1030 "\\(\\s-*\\)") end t))
|
|
1031 (let ((pos (match-end 1))
|
|
1032 (count 0))
|
|
1033 (while (and (> pos (point-min))
|
|
1034 (eq (char-before pos) ?\\))
|
|
1035 (setq count (1+ count) pos (1- pos)))
|
|
1036 (eq (mod count 2) 1))
|
|
1037 (goto-char (match-beginning 2))))
|
|
1038 result))
|
|
1039
|
|
1040 (defun align-new-section-p (beg end separator)
|
|
1041 "Is there a section divider between BEG and END?
|
|
1042 SEPARATOR specifies how to look for the section divider. See the
|
|
1043 documentation for `align-region-separate' for more details."
|
|
1044 (cond ((or (not separator)
|
|
1045 (eq separator 'entire))
|
|
1046 nil)
|
|
1047 ((eq separator 'group)
|
|
1048 (let ((amount 2))
|
|
1049 (save-excursion
|
|
1050 (goto-char end)
|
|
1051 (if (bolp)
|
|
1052 (setq amount 1)))
|
|
1053 (> (count-lines beg end) amount)))
|
|
1054 ((stringp separator)
|
|
1055 (save-excursion
|
|
1056 (goto-char beg)
|
|
1057 (re-search-forward separator end t)))
|
|
1058 ((functionp separator)
|
|
1059 (funcall separator beg end))
|
|
1060 ((listp separator)
|
|
1061 (let ((seps separator) yes)
|
|
1062 (while seps
|
|
1063 (if (and (>= (car seps) beg)
|
|
1064 (<= (car seps) end))
|
|
1065 (setq yes t seps nil)
|
|
1066 (setq seps (cdr seps))))
|
|
1067 yes))))
|
|
1068
|
|
1069 (defun align-adjust-col-for-rule (column rule spacing tab-stop)
|
|
1070 "Adjust COLUMN according to the given RULE.
|
|
1071 SPACING specifies how much spacing to use.
|
|
1072 TAB-STOP specifies whether SPACING refers to tab-stop boundaries."
|
|
1073 (unless spacing
|
|
1074 (setq spacing align-default-spacing))
|
|
1075 (if (<= spacing 0)
|
|
1076 column
|
|
1077 (if (not tab-stop)
|
|
1078 (+ column spacing)
|
|
1079 (let ((stops tab-stop-list))
|
|
1080 (while stops
|
|
1081 (if (and (> (car stops) column)
|
|
1082 (= (setq spacing (1- spacing)) 0))
|
|
1083 (setq column (car stops)
|
|
1084 stops nil)
|
|
1085 (setq stops (cdr stops)))))
|
|
1086 column)))
|
|
1087
|
|
1088 (defsubst align-column (pos)
|
|
1089 "Given a position in the buffer, state what column it's in.
|
|
1090 POS is the position whose column will be taken. Note that this
|
|
1091 function will change the location of point."
|
|
1092 (goto-char pos)
|
|
1093 (current-column))
|
|
1094
|
|
1095 (defsubst align-regions (regions props rule func)
|
|
1096 "Align the regions specified in REGIONS, a list of cons cells.
|
|
1097 PROPS describes formatting features specific to the given regions.
|
|
1098 RULE specifies exactly how to perform the alignments.
|
|
1099 If FUNC is specified, it will be called with each region that would
|
|
1100 have been aligned, rather than modifying the text."
|
|
1101 (while regions
|
|
1102 (save-excursion
|
|
1103 (align-areas (car regions) (car props) rule func))
|
|
1104 (setq regions (cdr regions)
|
|
1105 props (cdr props))))
|
|
1106
|
|
1107 (defun align-areas (areas props rule func)
|
|
1108 "Given a list of AREAS and formatting PROPS, align according to RULE.
|
|
1109 AREAS should be a list of cons cells containing beginning and ending
|
|
1110 markers. This function sweeps through all of the beginning markers,
|
|
1111 finds out which one starts in the furthermost column, and then deletes
|
|
1112 and inserts text such that all of the ending markers occur in the same
|
|
1113 column.
|
|
1114
|
|
1115 If FUNC is non-nil, it will be called for each text region that would
|
|
1116 have been aligned. No changes will be made to the buffer."
|
|
1117 (let* ((column (cdr (assq 'column rule)))
|
|
1118 (fixed (if (symbolp column)
|
|
1119 (symbol-value column)
|
|
1120 column))
|
|
1121 (justify (cdr (assq 'justify rule)))
|
|
1122 (col (or fixed 0))
|
|
1123 (width 0)
|
|
1124 ecol change look)
|
|
1125
|
|
1126 ;; Determine the alignment column.
|
|
1127 (let ((a areas))
|
|
1128 (while a
|
|
1129 (unless fixed
|
|
1130 (setq col (max col (align-column (caar a)))))
|
|
1131 (unless change
|
|
1132 (goto-char (cdar a))
|
|
1133 (if ecol
|
|
1134 (if (not (= ecol (current-column)))
|
|
1135 (setq change t))
|
|
1136 (setq ecol (current-column))))
|
|
1137 (when justify
|
|
1138 (goto-char (caar a))
|
|
1139 (if (and (re-search-forward "\\s-*" (cdar a) t)
|
|
1140 (not (= (point) (cdar a))))
|
|
1141 (let ((bcol (current-column)))
|
|
1142 (setcdr (car a) (cons (point-marker) (cdar a)))
|
|
1143 (goto-char (cdr (cdar a)))
|
|
1144 (setq width (max width (- (current-column) bcol))))))
|
|
1145 (setq a (cdr a))))
|
|
1146
|
|
1147 (unless fixed
|
|
1148 (setq col (+ (align-adjust-col-for-rule
|
|
1149 col rule (car props) (cdr props)) width)))
|
|
1150
|
|
1151 ;; Make all ending positions to occur in the goal column. Since
|
|
1152 ;; the whitespace to be modified was already deleted by
|
|
1153 ;; `align-region', all we have to do here is indent.
|
|
1154
|
|
1155 (unless change
|
|
1156 (setq change (and ecol (not (= col ecol)))))
|
|
1157
|
|
1158 (when (or func change)
|
|
1159 (while areas
|
|
1160 (let ((area (car areas))
|
|
1161 (gocol col) cur)
|
|
1162 (when area
|
|
1163 (if func
|
|
1164 (funcall func (car area) (cdr area) change)
|
|
1165 (if (not (and justify
|
|
1166 (consp (cdr area))))
|
|
1167 (goto-char (cdr area))
|
|
1168 (goto-char (cddr area))
|
|
1169 (let ((ecol (current-column)))
|
|
1170 (goto-char (cadr area))
|
|
1171 (setq gocol (- col (- ecol (current-column))))))
|
|
1172 (setq cur (current-column))
|
|
1173 (cond ((< gocol 0) t) ; don't do anything
|
|
1174 ((= cur gocol) t) ; don't need to
|
|
1175 ((< cur gocol) ; just add space
|
|
1176 (indent-to gocol))
|
|
1177 (t
|
|
1178 ;; This code works around an oddity in the
|
|
1179 ;; FORCE argument of `move-to-column', which
|
|
1180 ;; tends to screw up markers if there is any
|
|
1181 ;; tabbing.
|
|
1182 (let ((endcol (align-column
|
|
1183 (if (and justify
|
|
1184 (consp (cdr area)))
|
|
1185 (cadr area)
|
|
1186 (cdr area))))
|
|
1187 (abuts (<= gocol
|
|
1188 (align-column (car area)))))
|
|
1189 (if abuts
|
|
1190 (goto-char (car area))
|
|
1191 (move-to-column gocol t))
|
|
1192 (let ((here (point)))
|
|
1193 (move-to-column endcol t)
|
|
1194 (delete-region here (point))
|
|
1195 (if abuts
|
|
1196 (indent-to (align-adjust-col-for-rule
|
|
1197 (current-column) rule
|
|
1198 (car props) (cdr props)))))))))))
|
|
1199 (setq areas (cdr areas))))))
|
|
1200
|
|
1201 (defun align-region (beg end separate rules exclude-rules
|
|
1202 &optional func)
|
|
1203 "Align a region based on a given set of alignment rules.
|
|
1204 BEG and END specify the region to be aligned. Either may be nil, in
|
|
1205 which case the range will stop at the nearest section division (see
|
|
1206 `align-region-separate', and `align-region-heuristic' for more
|
|
1207 information').
|
|
1208
|
|
1209 The region will be divided into separate alignment sections based on
|
|
1210 the value of SEPARATE.
|
|
1211
|
|
1212 RULES and EXCLUDE-RULES are a pair of lists describing how to align
|
|
1213 the region, and which text areas within it should be excluded from
|
|
1214 alignment. See the `align-rules-list' for more information on the
|
|
1215 required format of these two lists.
|
|
1216
|
|
1217 If FUNC is specified, no text will be modified. What `align-region'
|
|
1218 will do with the rules is to search for the alignment areas, as it
|
|
1219 regularly would, taking account for exclusions, and then call FUNC,
|
|
1220 first with the beginning and ending of the region to be aligned
|
|
1221 according to that rule (this can be different for each rule, if BEG
|
|
1222 and END were nil), and then with the beginning and ending of each
|
|
1223 text region that the rule would have applied to.
|
|
1224
|
|
1225 The signature of FUNC should thus be:
|
|
1226
|
|
1227 (defun my-align-function (beg end mode)
|
|
1228 \"If MODE is a rule (a list), return t if BEG to END are to be searched.
|
|
1229 Otherwise BEG to END will be a region of text that matches the rule's
|
|
1230 definition, and MODE will be non-nil if any changes are necessary.\"
|
|
1231 (unless (and mode (listp mode))
|
|
1232 (message \"Would have aligned from %d to %d...\" beg end)))
|
|
1233
|
|
1234 This feature (of passing a FUNC) is used internally to locate the
|
|
1235 position of exclusion areas, but could also be used for any other
|
|
1236 purpose where you might want to know where the regions that the
|
|
1237 aligner would have dealt with are."
|
|
1238 (let ((end-mark (and end (copy-marker end t)))
|
|
1239 (real-beg beg)
|
|
1240 (real-end end)
|
|
1241 (report (and (not func) align-large-region beg end
|
|
1242 (>= (- end beg) align-large-region)))
|
|
1243 (rule-index 1)
|
|
1244 (rule-count (length rules)))
|
|
1245 (if (and align-indent-before-aligning real-beg end-mark)
|
|
1246 (indent-region real-beg end-mark nil))
|
|
1247 (while rules
|
|
1248 (let* ((rule (car rules))
|
|
1249 (run-if (assq 'run-if rule))
|
|
1250 (modes (assq 'modes rule)))
|
|
1251 ;; unless the `run-if' form tells us not to, look for the
|
|
1252 ;; rule..
|
|
1253 (unless (or (and modes (not (memq major-mode
|
|
1254 (eval (cdr modes)))))
|
|
1255 (and run-if (not (funcall (cdr run-if)))))
|
|
1256 (let* ((current-case-fold case-fold-search)
|
|
1257 (case-fold (assq 'case-fold rule))
|
|
1258 (regexp (cdr (assq 'regexp rule)))
|
|
1259 (regfunc (and (functionp regexp) regexp))
|
|
1260 (rulesep (assq 'separate rule))
|
|
1261 (thissep (if rulesep (cdr rulesep) separate))
|
|
1262 same (eol 0)
|
|
1263 group group-c
|
|
1264 spacing spacing-c
|
|
1265 tab-stop tab-stop-c
|
|
1266 repeat repeat-c
|
|
1267 valid valid-c
|
|
1268 pos-list first
|
|
1269 regions index
|
|
1270 last-point b e
|
|
1271 save-match-data
|
|
1272 exclude-p
|
|
1273 align-props)
|
|
1274 (save-excursion
|
|
1275 ;; if beg and end were not given, figure out what the
|
|
1276 ;; current alignment region should be. Depending on the
|
|
1277 ;; value of `align-region-separate' it's possible for
|
|
1278 ;; each rule to have its own definition of what that
|
|
1279 ;; current alignment section is.
|
|
1280 (if real-beg
|
|
1281 (goto-char beg)
|
|
1282 (if (or (not thissep) (eq thissep 'entire))
|
|
1283 (error "Cannot determine alignment region for '%s'"
|
|
1284 (symbol-name (cdr (assq 'title rule)))))
|
|
1285 (beginning-of-line)
|
|
1286 (while (and (not (eobp))
|
|
1287 (looking-at "^\\s-*$"))
|
|
1288 (forward-line))
|
|
1289 (let* ((here (point))
|
|
1290 (start here))
|
|
1291 (while (and here
|
|
1292 (let ((terminus
|
|
1293 (and align-region-heuristic
|
|
1294 (- (point)
|
|
1295 align-region-heuristic))))
|
|
1296 (if regfunc
|
|
1297 (funcall regfunc terminus t)
|
|
1298 (re-search-backward regexp
|
|
1299 terminus t))))
|
|
1300 (if (align-new-section-p (point) here thissep)
|
|
1301 (setq beg here
|
|
1302 here nil)
|
|
1303 (setq here (point))))
|
|
1304 (if (not here)
|
|
1305 (goto-char beg))
|
|
1306 (beginning-of-line)
|
|
1307 (setq beg (point))
|
|
1308 (goto-char start)
|
|
1309 (setq here (point))
|
|
1310 (while (and here
|
|
1311 (let ((terminus
|
|
1312 (and align-region-heuristic
|
|
1313 (+ (point)
|
|
1314 align-region-heuristic))))
|
|
1315 (if regfunc
|
|
1316 (funcall regfunc terminus nil)
|
|
1317 (re-search-forward regexp terminus t))))
|
|
1318 (if (align-new-section-p here (point) thissep)
|
|
1319 (setq end here
|
|
1320 here nil)
|
|
1321 (setq here (point))))
|
|
1322 (if (not here)
|
|
1323 (goto-char end))
|
|
1324 (forward-line)
|
|
1325 (setq end (point)
|
|
1326 end-mark (copy-marker end t))
|
|
1327 (goto-char beg)))
|
|
1328
|
|
1329 ;; If we have a region to align, and `func' is set and
|
|
1330 ;; reports back that the region is ok, then align it.
|
|
1331 (when (or (not func)
|
|
1332 (funcall func beg end rule))
|
|
1333 (unwind-protect
|
|
1334 (let (exclude-areas)
|
|
1335 ;; determine first of all where the exclusions
|
|
1336 ;; lie in this region
|
|
1337 (when exclude-rules
|
|
1338 ;; guard against a problem with recursion and
|
|
1339 ;; dynamic binding vs. lexical binding, since
|
|
1340 ;; the call to `align-region' below will
|
|
1341 ;; re-enter this function, and rebind
|
|
1342 ;; `exclude-areas'
|
|
1343 (set (setq exclude-areas
|
|
1344 (make-symbol "align-exclude-areas"))
|
|
1345 nil)
|
|
1346 (align-region
|
|
1347 beg end 'entire
|
|
1348 exclude-rules nil
|
|
1349 `(lambda (b e mode)
|
|
1350 (or (and mode (listp mode))
|
|
1351 (set (quote ,exclude-areas)
|
|
1352 (cons (cons b e)
|
|
1353 ,exclude-areas)))))
|
|
1354 (setq exclude-areas
|
|
1355 (sort (symbol-value exclude-areas)
|
|
1356 (function
|
|
1357 (lambda (l r)
|
|
1358 (>= (car l) (car r)))))))
|
|
1359
|
|
1360 ;; set `case-fold-search' according to the
|
|
1361 ;; (optional) `case-fold' property
|
|
1362 (and case-fold
|
|
1363 (setq case-fold-search (cdr case-fold)))
|
|
1364
|
|
1365 ;; while we can find the rule in the alignment
|
|
1366 ;; region..
|
|
1367 (while (and (< (point) end-mark)
|
|
1368 (if regfunc
|
|
1369 (funcall regfunc end-mark nil)
|
|
1370 (re-search-forward regexp
|
|
1371 end-mark t)))
|
|
1372
|
|
1373 ;; give the user some indication of where we
|
|
1374 ;; are, if it's a very large region being
|
|
1375 ;; aligned
|
|
1376 (if report
|
|
1377 (message
|
|
1378 "Aligning `%s' [rule %d of %d] (%d%%)..."
|
|
1379 (symbol-name (car rule))
|
|
1380 rule-index rule-count
|
|
1381 (/ (* (- (point) real-beg) 100)
|
|
1382 (- end-mark real-beg))))
|
|
1383
|
|
1384 ;; if the search ended us on the beginning of
|
|
1385 ;; the next line, move back to the end of the
|
|
1386 ;; previous line.
|
|
1387 (if (bolp)
|
|
1388 (forward-char -1))
|
|
1389
|
|
1390 ;; lookup the `group' attribute the first time
|
|
1391 ;; that we need it
|
|
1392 (unless group-c
|
|
1393 (setq group (or (cdr (assq 'group rule)) 1))
|
|
1394 (if (listp group)
|
|
1395 (setq first (car group))
|
|
1396 (setq first group group (list group)))
|
|
1397 (setq group-c t))
|
|
1398
|
|
1399 (unless spacing-c
|
|
1400 (setq spacing (cdr (assq 'spacing rule))
|
|
1401 spacing-c t))
|
|
1402
|
|
1403 (unless tab-stop-c
|
|
1404 (setq tab-stop
|
|
1405 (let ((rule-ts (assq 'tab-stop rule)))
|
|
1406 (if rule-ts
|
|
1407 (cdr rule-ts)
|
|
1408 (if (symbolp align-to-tab-stop)
|
|
1409 (symbol-value align-to-tab-stop)
|
|
1410 align-to-tab-stop)))
|
|
1411 tab-stop-c t))
|
|
1412
|
|
1413 ;; test whether we have found a match on the same
|
|
1414 ;; line as a previous match
|
|
1415 (if (> (point) eol)
|
|
1416 (setq same nil
|
|
1417 eol (save-excursion
|
|
1418 (end-of-line)
|
|
1419 (point-marker))))
|
|
1420
|
|
1421 ;; lookup the `repeat' attribute the first time
|
|
1422 (or repeat-c
|
|
1423 (setq repeat (cdr (assq 'repeat rule))
|
|
1424 repeat-c t))
|
|
1425
|
|
1426 ;; lookup the `valid' attribute the first time
|
|
1427 (or valid-c
|
|
1428 (setq valid (assq 'valid rule)
|
|
1429 valid-c t))
|
|
1430
|
|
1431 ;; remember the beginning position of this rule
|
|
1432 ;; match, and save the match-data, since either
|
|
1433 ;; the `valid' form, or the code that searches for
|
|
1434 ;; section separation, might alter it
|
|
1435 (setq b (match-beginning first)
|
|
1436 save-match-data (match-data))
|
|
1437
|
|
1438 ;; unless the `valid' attribute is set, and tells
|
|
1439 ;; us that the rule is not valid at this point in
|
|
1440 ;; the code..
|
|
1441 (unless (and valid (not (funcall (cdr valid))))
|
|
1442
|
|
1443 ;; look to see if this match begins a new
|
|
1444 ;; section. If so, we should align what we've
|
|
1445 ;; collected so far, and then begin collecting
|
|
1446 ;; anew for the next alignment section
|
|
1447 (if (and last-point
|
|
1448 (align-new-section-p last-point b
|
|
1449 thissep))
|
|
1450 (progn
|
|
1451 (align-regions regions align-props
|
|
1452 rule func)
|
|
1453 (setq last-point (copy-marker b t)
|
|
1454 regions nil
|
|
1455 align-props nil))
|
|
1456 (setq last-point (copy-marker b t)))
|
|
1457
|
|
1458 ;; restore the match data
|
|
1459 (set-match-data save-match-data)
|
|
1460
|
|
1461 ;; check whether the region to be aligned
|
|
1462 ;; straddles an exclusion area
|
|
1463 (let ((excls exclude-areas))
|
|
1464 (setq exclude-p nil)
|
|
1465 (while excls
|
|
1466 (if (and (< (match-beginning (car group))
|
|
1467 (cdar excls))
|
|
1468 (> (match-end (car (last group)))
|
|
1469 (caar excls)))
|
|
1470 (setq exclude-p t
|
|
1471 excls nil)
|
|
1472 (setq excls (cdr excls)))))
|
|
1473
|
|
1474 ;; go through the list of parenthesis groups
|
|
1475 ;; matching whitespace text to be
|
|
1476 ;; contracted/expanded (or possibly
|
|
1477 ;; justified, if the `justify' attribute was
|
|
1478 ;; set)
|
|
1479 (unless exclude-p
|
|
1480 (let ((g group))
|
|
1481 (while g
|
|
1482
|
|
1483 ;; we have to use markers, since
|
|
1484 ;; `align-areas' may modify the buffer
|
|
1485 (setq b (copy-marker
|
|
1486 (match-beginning (car g)) t)
|
|
1487 e (copy-marker (match-end (car g)) t))
|
|
1488
|
|
1489 ;; record this text region for alignment
|
|
1490 (setq index (if same (1+ index) 0))
|
|
1491 (let ((region (cons b e))
|
|
1492 (props (cons
|
|
1493 (if (listp spacing)
|
|
1494 (car spacing)
|
|
1495 spacing)
|
|
1496 (if (listp tab-stop)
|
|
1497 (car tab-stop)
|
|
1498 tab-stop))))
|
|
1499 (if (nth index regions)
|
|
1500 (setcar (nthcdr index regions)
|
|
1501 (cons region
|
|
1502 (nth index regions)))
|
|
1503 (if regions
|
|
1504 (progn
|
|
1505 (nconc regions
|
|
1506 (list (list region)))
|
|
1507 (nconc align-props (list props)))
|
|
1508 (setq regions
|
|
1509 (list (list region)))
|
|
1510 (setq align-props (list props)))))
|
|
1511
|
|
1512 ;; if any further rule matches are
|
|
1513 ;; found before `eol', then they are
|
|
1514 ;; on the same line as this one; this
|
|
1515 ;; can only happen if the `repeat'
|
|
1516 ;; attribute is non-nil
|
|
1517 (if (listp spacing)
|
|
1518 (setq spacing (cdr spacing)))
|
|
1519 (if (listp tab-stop)
|
|
1520 (setq tab-stop (cdr tab-stop)))
|
|
1521 (setq same t g (cdr g))))
|
|
1522
|
|
1523 ;; if `repeat' has not been set, move to
|
|
1524 ;; the next line; don't bother searching
|
|
1525 ;; anymore on this one
|
|
1526 (if (and (not repeat) (not (bolp)))
|
|
1527 (forward-line)))))
|
|
1528
|
|
1529 ;; when they are no more matches for this rule,
|
|
1530 ;; align whatever was left over
|
|
1531 (if regions
|
|
1532 (align-regions regions align-props rule func)))
|
|
1533
|
|
1534 (setq case-fold-search current-case-fold)))))))
|
|
1535 (setq rules (cdr rules)
|
|
1536 rule-index (1+ rule-index)))
|
|
1537
|
|
1538 (if report
|
|
1539 (message "Aligning...done"))))
|
|
1540
|
|
1541 ;; Provide:
|
|
1542
|
|
1543 (provide 'align)
|
|
1544
|
|
1545 (run-hooks 'align-load-hook)
|
|
1546
|
|
1547 ;;; align.el ends here
|