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