(save-completions-file-name): Fix typo in last change.
[emacs.git] / lisp / align.el
blob2cbc43e38d3d36f7bf93633f4fb4e18801df6b2a
1 ;;; align.el --- align text to a specific column, by regexp
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; Keywords: convenience languages lisp
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (defcustom align-rules-list
388 `((lisp-second-arg
389 (regexp . "\\(^\\s-+[^( \t\n]\\|(\\(\\S-+\\)\\s-+\\)\\S-+\\(\\s-+\\)")
390 (group . 3)
391 (modes . align-lisp-modes)
392 (run-if . ,(function (lambda () current-prefix-arg))))
394 (lisp-alist-dot
395 (regexp . "\\(\\s-*\\)\\.\\(\\s-*\\)")
396 (group . (1 2))
397 (modes . align-lisp-modes))
399 (open-comment
400 (regexp . ,(function
401 (lambda (end reverse)
402 (funcall (if reverse 're-search-backward
403 're-search-forward)
404 (concat "[^ \t\n\\\\]"
405 (regexp-quote comment-start)
406 "\\(.+\\)$") end t))))
407 (modes . align-open-comment-modes))
409 (c-macro-definition
410 (regexp . "^\\s-*#\\s-*define\\s-+\\S-+\\(\\s-+\\)")
411 (modes . align-c++-modes))
413 (c-variable-declaration
414 (regexp . ,(concat "[*&0-9A-Za-z_]>?[&*]*\\(\\s-+[*&]*\\)"
415 "[A-Za-z_][0-9A-Za-z:_]*\\s-*\\(\\()\\|"
416 "=[^=\n].*\\|(.*)\\|\\(\\[.*\\]\\)*\\)?"
417 "\\s-*[;,]\\|)\\s-*$\\)"))
418 (group . 1)
419 (modes . align-c++-modes)
420 (justify . t)
421 (valid
422 . ,(function
423 (lambda ()
424 (not (or (save-excursion
425 (goto-char (match-beginning 1))
426 (backward-word 1)
427 (looking-at
428 "\\(goto\\|return\\|new\\|delete\\|throw\\)"))
429 (if (and (boundp 'font-lock-mode) font-lock-mode)
430 (eq (get-text-property (point) 'face)
431 'font-lock-comment-face)
432 (eq (caar (c-guess-basic-syntax)) 'c))))))))
434 (c-assignment
435 (regexp . ,(concat "[^-=!^&*+<>/| \t\n]\\(\\s-*[-=!^&*+<>/|]*\\)"
436 "=\\(\\s-*\\)\\([^= \t\n]\\|$\\)"))
437 (group . (1 2))
438 (modes . align-c++-modes)
439 (justify . t)
440 (tab-stop . nil))
442 (perl-assignment
443 (regexp . ,(concat "[^=!^&*-+<>/| \t\n]\\(\\s-*\\)=[~>]?"
444 "\\(\\s-*\\)\\([^>= \t\n]\\|$\\)"))
445 (group . (1 2))
446 (modes . align-perl-modes)
447 (tab-stop . nil))
449 (python-assignment
450 (regexp . ,(concat "[^=!<> \t\n]\\(\\s-*\\)="
451 "\\(\\s-*\\)\\([^>= \t\n]\\|$\\)"))
452 (group . (1 2))
453 (modes . '(python-mode))
454 (tab-stop . nil))
456 (make-assignment
457 (regexp . "^\\s-*\\w+\\(\\s-*\\):?=\\(\\s-*\\)\\([^\t\n \\\\]\\|$\\)")
458 (group . (1 2))
459 (modes . '(makefile-mode))
460 (tab-stop . nil))
462 (c-comma-delimiter
463 (regexp . ",\\(\\s-*\\)[^/ \t\n]")
464 (repeat . t)
465 (modes . align-c++-modes)
466 (run-if . ,(function (lambda () current-prefix-arg))))
467 ; (valid
468 ; . ,(function
469 ; (lambda ()
470 ; (memq (caar (c-guess-basic-syntax))
471 ; '(brace-list-intro
472 ; brace-list-entry
473 ; brace-entry-open))))))
475 ;; With a prefix argument, comma delimiter will be aligned. Since
476 ;; perl-mode doesn't give us enough syntactic information (and we
477 ;; don't do our own parsing yet), this rule is too destructive to
478 ;; run normally.
479 (basic-comma-delimiter
480 (regexp . ",\\(\\s-*\\)[^# \t\n]")
481 (repeat . t)
482 (modes . (append align-perl-modes '(python-mode)))
483 (run-if . ,(function (lambda () current-prefix-arg))))
485 (c++-comment
486 (regexp . "\\(\\s-*\\)\\(//.*\\|/\\*.*\\*/\\s-*\\)$")
487 (modes . align-c++-modes)
488 (column . comment-column)
489 (valid . ,(function
490 (lambda ()
491 (save-excursion
492 (goto-char (match-beginning 1))
493 (not (bolp)))))))
495 (c-chain-logic
496 (regexp . "\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
497 (modes . align-c++-modes)
498 (valid . ,(function
499 (lambda ()
500 (save-excursion
501 (goto-char (match-end 2))
502 (looking-at "\\s-*\\(/[*/]\\|$\\)"))))))
504 (perl-chain-logic
505 (regexp . "\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
506 (modes . align-perl-modes)
507 (valid . ,(function
508 (lambda ()
509 (save-excursion
510 (goto-char (match-end 2))
511 (looking-at "\\s-*\\(#\\|$\\)"))))))
513 (python-chain-logic
514 (regexp . "\\(\\s-*\\)\\(\\<and\\>\\|\\<or\\>\\)")
515 (modes . '(python-mode))
516 (valid . ,(function
517 (lambda ()
518 (save-excursion
519 (goto-char (match-end 2))
520 (looking-at "\\s-*\\(#\\|$\\|\\\\\\)"))))))
522 (c-macro-line-continuation
523 (regexp . "\\(\\s-*\\)\\\\$")
524 (modes . align-c++-modes)
525 (column . c-backslash-column))
526 ; (valid
527 ; . ,(function
528 ; (lambda ()
529 ; (memq (caar (c-guess-basic-syntax))
530 ; '(cpp-macro cpp-macro-cont))))))
532 (basic-line-continuation
533 (regexp . "\\(\\s-*\\)\\\\$")
534 (modes . '(python-mode makefile-mode)))
536 (tex-record-separator
537 (regexp . ,(function
538 (lambda (end reverse)
539 (align-match-tex-pattern "&" end reverse))))
540 (group . (1 2))
541 (modes . align-tex-modes)
542 (repeat . t))
544 (tex-tabbing-separator
545 (regexp . ,(function
546 (lambda (end reverse)
547 (align-match-tex-pattern "\\\\[=>]" end reverse))))
548 (group . (1 2))
549 (modes . align-tex-modes)
550 (repeat . t)
551 (run-if . ,(function
552 (lambda ()
553 (eq major-mode 'latex-mode)))))
555 (tex-record-break
556 (regexp . "\\(\\s-*\\)\\\\\\\\")
557 (modes . align-tex-modes))
559 ;; With a numeric prefix argument, or C-u, space delimited text
560 ;; tables will be aligned.
561 (text-column
562 (regexp . "\\(^\\|\\S-\\)\\(\\s-+\\)\\(\\S-\\|$\\)")
563 (group . 2)
564 (modes . align-text-modes)
565 (repeat . t)
566 (run-if . ,(function
567 (lambda ()
568 (and current-prefix-arg
569 (not (eq '- current-prefix-arg)))))))
571 ;; With a negative prefix argument, lists of dollar figures will
572 ;; be aligned.
573 (text-dollar-figure
574 (regexp . "\\$?\\(\\s-+[0-9]+\\)\\.")
575 (modes . align-text-modes)
576 (justify . t)
577 (run-if . ,(function
578 (lambda ()
579 (eq '- current-prefix-arg)))))
581 (css-declaration
582 (regexp . "^\\s-*\\w+:\\(\\s-*\\).*;")
583 (group . (1))
584 (modes . '(css-mode html-mode))))
585 "*A list describing all of the available alignment rules.
586 The format is:
588 ((TITLE
589 (ATTRIBUTE . VALUE) ...)
590 ...)
592 The following attributes are meaningful:
594 `regexp' This required attribute must be either a string describing
595 a regular expression, or a function (described below).
596 For every line within the section that this regular
597 expression matches, the given rule will be applied to that
598 line. The exclusion rules denote which part(s) of the
599 line should not be modified; the alignment rules cause the
600 identified whitespace group to be contracted/expanded such
601 that the \"alignment character\" (the character
602 immediately following the identified parenthesis group),
603 occurs in the same column for every line within the
604 alignment section (see `align-region-separate' for a
605 description of how the region is broken up into alignment
606 sections).
608 The `regexp' attribute describes how the text should be
609 treated. Within this regexp, there must be at least one
610 group of characters (typically whitespace) identified by
611 the special opening and closing parens used in regexp
612 expressions (`\\\\(' and `\\\\)') (see the Emacs manual on
613 the syntax of regular expressions for more info).
615 If `regexp' is a function, it will be called as a
616 replacement for `re-search-forward'. This means that it
617 should return nil if nothing is found to match the rule,
618 or it should set the match data appropriately, move point
619 to the end of the match, and return the value of point.
621 `group' For exclusion rules, the group identifies the range of
622 characters that should be ignored. For alignment rules,
623 these are the characters that will be deleted/expanded for
624 the purposes of alignment. The \"alignment character\" is
625 always the first character immediately following this
626 parenthesis group. This attribute may also be a list of
627 integer, in which case multiple alignment characters will
628 be aligned, with the list of integer identifying the
629 whitespace groups which precede them. The default for
630 this attribute is 1.
632 `modes' The `modes' attribute, if set, should name a list of
633 major modes -- or evaluate to such a value -- in which the
634 rule is valid. If not set, the rule will apply to all
635 modes.
637 `case-fold' If `regexp' is an ordinary regular expression string
638 containing alphabetic character, sometimes you may want
639 the search to proceed case-insensitively (for languages
640 that ignore case, such as pascal for example). In that
641 case, set `case-fold' to a non-nil value, and the regular
642 expression search will ignore case. If `regexp' is set to
643 a function, that function must handle the job of ignoring
644 case by itself.
646 `tab-stop' If the `tab-stop' attribute is set, and non-nil, the
647 alignment character will always fall on a tab stop
648 (whether it uses tabs to get there or not depends on the
649 value of `indent-tabs-mode'). If the `tab-stop' attribute
650 is set to nil, tab stops will never be used. Otherwise,
651 the value of `align-to-tab-stop' determines whether or not
652 to align to a tab stop. The `tab-stop' attribute may also
653 be a list of t or nil values, corresponding to the number
654 of parenthesis groups specified by the `group' attribute.
656 `repeat' If the `repeat' attribute is present, and non-nil, the
657 rule will be applied to the line continuously until no
658 further matches are found.
660 `valid' If the `valid' attribute is set, it will be used to
661 determine whether the rule should be invoked. This form
662 is evaluated after the regular expression match has been
663 performed, so that it is possible to use the results of
664 that match to determine whether the alignment should be
665 performed. The buffer should not be modified during the
666 evaluation of this form.
668 `run-if' Like `valid', the `run-if' attribute tests whether the
669 rule should be run at all -- even before any searches are
670 done to determine if the rule applies to the alignment
671 region. This can save time, since `run-if' will only be
672 run once for each rule. If it returns nil, the rule will
673 not be attempted.
675 `column' For alignment rules, if the `column' attribute is set --
676 which must be an integer, or a symbol whose value is an
677 integer -- it will be used as the column in which to align
678 the alignment character. If the text on a particular line
679 happens to overrun that column, a single space character,
680 or tab stop (see `align-to-tab-stop') will be added
681 between the last text character and the alignment
682 character.
684 `spacing' Alignment rules may also override the amount of spacing
685 that would normally be used by providing a `spacing'
686 attribute. This must be an integer, or a list of integers
687 corresponding to the number of parenthesis groups matched
688 by the `group' attribute. If a list of value is used, and
689 any of those values is nil, `align-default-spacing' will
690 be used for that subgroup. See `align-default-spacing'
691 for more details on spacing, tab stops, and how to
692 indicate how much spacing should be used. If TAB-STOP is
693 present, it will override the value of `align-to-tab-stop'
694 for that rule.
696 `justify' It is possible with `regexp' and `group' to identify a
697 character group that contains more than just whitespace
698 characters. By default, any non-whitespace characters in
699 that group will also be deleted while aligning the
700 alignment character. However, if the `justify' attribute
701 is set to a non-nil value, only the initial whitespace
702 characters within that group will be deleted. This has
703 the effect of right-justifying the characters that remain,
704 and can be used for outdenting or just plain old right-
705 justification.
707 `separate' Each rule can define its own section separator, which
708 describes how to identify the separation of \"sections\"
709 within the region to be aligned. Setting the `separate'
710 attribute overrides the value of `align-region-separate'
711 (see the documentation of that variable for possible
712 values), and any separation argument passed to `align'."
713 :type align-rules-list-type
714 :group 'align)
716 (put 'align-rules-list 'risky-local-variable t)
718 (defvar align-exclude-rules-list-type
719 '(repeat
720 (cons
721 :tag "Exclusion rule"
722 (symbol :tag "Title")
723 (cons :tag "Required attributes"
724 (cons :tag "Regexp"
725 (const :tag "(Regular expression to match)" regexp)
726 (choice :value "\\(\\s-+\\)" regexp function))
727 (repeat
728 :tag "Optional attributes"
729 (choice
730 (cons :tag "Repeat"
731 (const :tag "(Repeat this rule throughout line)"
732 repeat)
733 (boolean :value t))
734 (cons :tag "Paren group"
735 (const :tag "(Parenthesis group to use)" group)
736 (choice :value 2
737 integer (repeat integer)))
738 (cons :tag "Modes"
739 (const :tag "(Modes where this rule applies)" modes)
740 (sexp :value (text-mode)))
741 (cons :tag "Case-fold"
742 (const :tag "(Should case be ignored for this rule)"
743 case-fold)
744 (boolean :value t)))))))
745 "The `type' form for any `align-exclude-rules-list' variable.")
747 (defcustom align-exclude-rules-list
748 `((exc-dq-string
749 (regexp . "\"\\([^\"\n]+\\)\"")
750 (repeat . t)
751 (modes . align-dq-string-modes))
753 (exc-sq-string
754 (regexp . "'\\([^'\n]+\\)'")
755 (repeat . t)
756 (modes . align-sq-string-modes))
758 (exc-open-comment
759 (regexp
760 . ,(function
761 (lambda (end reverse)
762 (funcall (if reverse 're-search-backward
763 're-search-forward)
764 (concat "[^ \t\n\\\\]"
765 (regexp-quote comment-start)
766 "\\(.+\\)$") end t))))
767 (modes . align-open-comment-modes))
769 (exc-c-comment
770 (regexp . "/\\*\\(.+\\)\\*/")
771 (repeat . t)
772 (modes . align-c++-modes))
774 (exc-c-func-params
775 (regexp . "(\\([^)\n]+\\))")
776 (repeat . t)
777 (modes . align-c++-modes))
779 (exc-c-macro
780 (regexp . "^\\s-*#\\s-*\\(if\\w*\\|endif\\)\\(.*\\)$")
781 (group . 2)
782 (modes . align-c++-modes)))
783 "*A list describing text that should be excluded from alignment.
784 See the documentation for `align-rules-list' for more info."
785 :type align-exclude-rules-list-type
786 :group 'align)
788 (put 'align-exclude-rules-list 'risky-local-variable t)
790 ;;; Internal Variables:
792 (defvar align-mode-rules-list nil
793 "Alignment rules specific to the current major mode.
794 See the variable `align-rules-list' for more details.")
796 (make-variable-buffer-local 'align-mode-rules-list)
798 (defvar align-mode-exclude-rules-list nil
799 "Alignment exclusion rules specific to the current major mode.
800 See the variable `align-exclude-rules-list' for more details.")
802 (make-variable-buffer-local 'align-mode-exclude-rules-list)
804 (defvar align-highlight-overlays nil
805 "The current overlays highlighting the text matched by a rule.")
807 ;; Sample extension rule set, for vhdl-mode. This should properly be
808 ;; in vhdl-mode.el itself.
810 (defcustom align-vhdl-rules-list
811 `((vhdl-declaration
812 (regexp . "\\(signal\\|variable\\|constant\\)\\(\\s-+\\)\\S-")
813 (group . 2))
815 (vhdl-case
816 (regexp . "\\(others\\|[^ \t\n=<]\\)\\(\\s-*\\)=>\\(\\s-*\\)\\S-")
817 (group . (2 3))
818 (valid
819 . ,(function
820 (lambda ()
821 (not (string= (downcase (match-string 1))
822 "others"))))))
824 (vhdl-colon
825 (regexp . "[^ \t\n:]\\(\\s-*\\):\\(\\s-*\\)[^=\n]")
826 (group . (1 2)))
828 (direction
829 (regexp . ":\\s-*\\(in\\|out\\|inout\\|buffer\\)\\(\\s-*\\)")
830 (group . 2))
832 (sig-assign
833 (regexp . "[^ \t\n=<]\\(\\s-*\\)<=\\(\\s-*\\)\\S-")
834 (group . (1 2)))
836 (var-assign
837 (regexp . "[^ \t\n:]\\(\\s-*\\):="))
839 (use-entity
840 (regexp . "\\(\\s-+\\)use\\s-+entity")))
841 "*Alignment rules for `vhdl-mode'. See `align-rules-list' for more info."
842 :type align-rules-list-type
843 :group 'align)
845 (put 'align-vhdl-rules-list 'risky-local-variable t)
847 (defun align-set-vhdl-rules ()
848 "Setup the `align-mode-rules-list' variable for `vhdl-mode'."
849 (setq align-mode-rules-list align-vhdl-rules-list))
851 (add-hook 'vhdl-mode-hook 'align-set-vhdl-rules)
853 (add-to-list 'align-dq-string-modes 'vhdl-mode)
854 (add-to-list 'align-open-comment-modes 'vhdl-mode)
856 ;;; User Functions:
858 ;;;###autoload
859 (defun align (beg end &optional separate rules exclude-rules)
860 "Attempt to align a region based on a set of alignment rules.
861 BEG and END mark the region. If BEG and END are specifically set to
862 nil (this can only be done programmatically), the beginning and end of
863 the current alignment section will be calculated based on the location
864 of point, and the value of `align-region-separate' (or possibly each
865 rule's `separate' attribute).
867 If SEPARATE is non-nil, it overrides the value of
868 `align-region-separate' for all rules, except those that have their
869 `separate' attribute set.
871 RULES and EXCLUDE-RULES, if either is non-nil, will replace the
872 default rule lists defined in `align-rules-list' and
873 `align-exclude-rules-list'. See `align-rules-list' for more details
874 on the format of these lists."
875 (interactive "r")
876 (let ((separator
877 (or separate
878 (if (and (symbolp align-region-separate)
879 (boundp align-region-separate))
880 (symbol-value align-region-separate)
881 align-region-separate)
882 'entire)))
883 (if (not (or ;(eq separator 'largest)
884 (and (functionp separator)
885 (not (funcall separator nil nil)))))
886 (align-region beg end separator
887 (or rules align-mode-rules-list align-rules-list)
888 (or exclude-rules align-mode-exclude-rules-list
889 align-exclude-rules-list))
890 (let ((sec-first end)
891 (sec-last beg))
892 (align-region beg end
893 (or exclude-rules
894 align-mode-exclude-rules-list
895 align-exclude-rules-list) nil
896 separator
897 (function
898 (lambda (b e mode)
899 (when (and mode (listp mode))
900 (setq sec-first (min sec-first b)
901 sec-last (max sec-last e))))))
902 (if (< sec-first sec-last)
903 (align-region sec-first sec-last 'entire
904 (or rules align-mode-rules-list align-rules-list)
905 (or exclude-rules align-mode-exclude-rules-list
906 align-exclude-rules-list)))))))
908 ;;;###autoload
909 (defun align-regexp (beg end regexp &optional group spacing repeat)
910 "Align the current region using an ad-hoc rule read from the minibuffer.
911 BEG and END mark the limits of the region. This function will prompt
912 for the REGEXP to align with. If no prefix arg was specified, you
913 only need to supply the characters to be lined up and any preceding
914 whitespace is replaced. If a prefix arg was specified, the full
915 regexp with parenthesized whitespace should be supplied; it will also
916 prompt for which parenthesis GROUP within REGEXP to modify, the amount
917 of SPACING to use, and whether or not to REPEAT the rule throughout
918 the line. See `align-rules-list' for more information about these
919 options.
921 For example, let's say you had a list of phone numbers, and wanted to
922 align them so that the opening parentheses would line up:
924 Fred (123) 456-7890
925 Alice (123) 456-7890
926 Mary-Anne (123) 456-7890
927 Joe (123) 456-7890
929 There is no predefined rule to handle this, but you could easily do it
930 using a REGEXP like \"(\". All you would have to do is to mark the
931 region, call `align-regexp' and type in that regular expression."
932 (interactive
933 (append
934 (list (min (point) (mark))
935 (max (point) (mark)))
936 (if current-prefix-arg
937 (list (read-string "Complex align using regexp: "
938 "\\(\\s-*\\)")
939 (string-to-number
940 (read-string
941 "Parenthesis group to modify (justify if negative): " "1"))
942 (string-to-number
943 (read-string "Amount of spacing (or column if negative): "
944 (number-to-string align-default-spacing)))
945 (y-or-n-p "Repeat throughout line? "))
946 (list (concat "\\(\\s-*\\)"
947 (read-string "Align regexp: "))
948 1 align-default-spacing nil))))
949 (let ((rule
950 (list (list nil (cons 'regexp regexp)
951 (cons 'group (abs group))
952 (if (< group 0)
953 (cons 'justify t)
954 (cons 'bogus nil))
955 (if (>= spacing 0)
956 (cons 'spacing spacing)
957 (cons 'column (abs spacing)))
958 (cons 'repeat repeat)))))
959 (align-region beg end 'entire rule nil nil)))
961 ;;;###autoload
962 (defun align-entire (beg end &optional rules exclude-rules)
963 "Align the selected region as if it were one alignment section.
964 BEG and END mark the extent of the region. If RULES or EXCLUDE-RULES
965 is set to a list of rules (see `align-rules-list'), it can be used to
966 override the default alignment rules that would have been used to
967 align that section."
968 (interactive "r")
969 (align beg end 'entire rules exclude-rules))
971 ;;;###autoload
972 (defun align-current (&optional rules exclude-rules)
973 "Call `align' on the current alignment section.
974 This function assumes you want to align only the current section, and
975 so saves you from having to specify the region. If RULES or
976 EXCLUDE-RULES is set to a list of rules (see `align-rules-list'), it
977 can be used to override the default alignment rules that would have
978 been used to align that section."
979 (interactive)
980 (align nil nil nil rules exclude-rules))
982 ;;;###autoload
983 (defun align-highlight-rule (beg end title &optional rules exclude-rules)
984 "Highlight the whitespace which a given rule would have modified.
985 BEG and END mark the extent of the region. TITLE identifies the rule
986 that should be highlighted. If RULES or EXCLUDE-RULES is set to a
987 list of rules (see `align-rules-list'), it can be used to override the
988 default alignment rules that would have been used to identify the text
989 to be colored."
990 (interactive
991 (list (min (mark) (point))
992 (max (mark) (point))
993 (completing-read
994 "Title of rule to highlight: "
995 (mapcar
996 (function
997 (lambda (rule)
998 (list (symbol-name (car rule)))))
999 (append (or align-mode-rules-list align-rules-list)
1000 (or align-mode-exclude-rules-list
1001 align-exclude-rules-list))) nil t)))
1002 (let ((ex-rule (assq (intern title)
1003 (or align-mode-exclude-rules-list
1004 align-exclude-rules-list)))
1005 face)
1006 (align-unhighlight-rule)
1007 (align-region
1008 beg end 'entire
1009 (or rules (if ex-rule
1010 (or exclude-rules align-mode-exclude-rules-list
1011 align-exclude-rules-list)
1012 (or align-mode-rules-list align-rules-list)))
1013 (unless ex-rule (or exclude-rules align-mode-exclude-rules-list
1014 align-exclude-rules-list))
1015 (function
1016 (lambda (b e mode)
1017 (if (and mode (listp mode))
1018 (if (equal (symbol-name (car mode)) title)
1019 (setq face (cons align-highlight-change-face
1020 align-highlight-nochange-face))
1021 (setq face nil))
1022 (when face
1023 (let ((overlay (make-overlay b e)))
1024 (setq align-highlight-overlays
1025 (cons overlay align-highlight-overlays))
1026 (overlay-put overlay 'face
1027 (if mode
1028 (car face)
1029 (cdr face)))))))))))
1031 ;;;###autoload
1032 (defun align-unhighlight-rule ()
1033 "Remove any highlighting that was added by `align-highlight-rule'."
1034 (interactive)
1035 (while align-highlight-overlays
1036 (delete-overlay (car align-highlight-overlays))
1037 (setq align-highlight-overlays
1038 (cdr align-highlight-overlays))))
1040 ;;;###autoload
1041 (defun align-newline-and-indent ()
1042 "A replacement function for `newline-and-indent', aligning as it goes."
1043 (interactive)
1044 (let ((separate (or (if (and (symbolp align-region-separate)
1045 (boundp align-region-separate))
1046 (symbol-value align-region-separate)
1047 align-region-separate)
1048 'entire))
1049 (end (point)))
1050 (call-interactively 'newline-and-indent)
1051 (save-excursion
1052 (forward-line -1)
1053 (while (not (or (bobp)
1054 (align-new-section-p (point) end separate)))
1055 (forward-line -1))
1056 (align (point) end))))
1058 ;;; Internal Functions:
1060 (defun align-match-tex-pattern (regexp end &optional reverse)
1061 "Match REGEXP in TeX mode, counting backslashes appropriately.
1062 END denotes the end of the region to be searched, while REVERSE, if
1063 non-nil, indicates that the search should proceed backward from the
1064 current position."
1065 (let (result)
1066 (while
1067 (and (setq result
1068 (funcall
1069 (if reverse 're-search-backward
1070 're-search-forward)
1071 (concat "\\(\\s-*\\)" regexp
1072 "\\(\\s-*\\)") end t))
1073 (let ((pos (match-end 1))
1074 (count 0))
1075 (while (and (> pos (point-min))
1076 (eq (char-before pos) ?\\))
1077 (setq count (1+ count) pos (1- pos)))
1078 (eq (mod count 2) 1))
1079 (goto-char (match-beginning 2))))
1080 result))
1082 (defun align-new-section-p (beg end separator)
1083 "Is there a section divider between BEG and END?
1084 SEPARATOR specifies how to look for the section divider. See the
1085 documentation for `align-region-separate' for more details."
1086 (cond ((or (not separator)
1087 (eq separator 'entire))
1088 nil)
1089 ((eq separator 'group)
1090 (let ((amount 2))
1091 (save-excursion
1092 (goto-char end)
1093 (if (bolp)
1094 (setq amount 1)))
1095 (> (count-lines beg end) amount)))
1096 ((stringp separator)
1097 (save-excursion
1098 (goto-char beg)
1099 (re-search-forward separator end t)))
1100 ((functionp separator)
1101 (funcall separator beg end))
1102 ((listp separator)
1103 (let ((seps separator) yes)
1104 (while seps
1105 (if (and (>= (car seps) beg)
1106 (<= (car seps) end))
1107 (setq yes t seps nil)
1108 (setq seps (cdr seps))))
1109 yes))))
1111 (defun align-adjust-col-for-rule (column rule spacing tab-stop)
1112 "Adjust COLUMN according to the given RULE.
1113 SPACING specifies how much spacing to use.
1114 TAB-STOP specifies whether SPACING refers to tab-stop boundaries."
1115 (unless spacing
1116 (setq spacing align-default-spacing))
1117 (if (<= spacing 0)
1118 column
1119 (if (not tab-stop)
1120 (+ column spacing)
1121 (let ((stops tab-stop-list))
1122 (while stops
1123 (if (and (> (car stops) column)
1124 (= (setq spacing (1- spacing)) 0))
1125 (setq column (car stops)
1126 stops nil)
1127 (setq stops (cdr stops)))))
1128 column)))
1130 (defsubst align-column (pos)
1131 "Given a position in the buffer, state what column it's in.
1132 POS is the position whose column will be taken. Note that this
1133 function will change the location of point."
1134 (goto-char pos)
1135 (current-column))
1137 (defsubst align-regions (regions props rule func)
1138 "Align the regions specified in REGIONS, a list of cons cells.
1139 PROPS describes formatting features specific to the given regions.
1140 RULE specifies exactly how to perform the alignments.
1141 If FUNC is specified, it will be called with each region that would
1142 have been aligned, rather than modifying the text."
1143 (while regions
1144 (save-excursion
1145 (align-areas (car regions) (car props) rule func))
1146 (setq regions (cdr regions)
1147 props (cdr props))))
1149 (defun align-areas (areas props rule func)
1150 "Given a list of AREAS and formatting PROPS, align according to RULE.
1151 AREAS should be a list of cons cells containing beginning and ending
1152 markers. This function sweeps through all of the beginning markers,
1153 finds out which one starts in the furthermost column, and then deletes
1154 and inserts text such that all of the ending markers occur in the same
1155 column.
1157 If FUNC is non-nil, it will be called for each text region that would
1158 have been aligned. No changes will be made to the buffer."
1159 (let* ((column (cdr (assq 'column rule)))
1160 (fixed (if (symbolp column)
1161 (symbol-value column)
1162 column))
1163 (justify (cdr (assq 'justify rule)))
1164 (col (or fixed 0))
1165 (width 0)
1166 ecol change look)
1168 ;; Determine the alignment column.
1169 (let ((a areas))
1170 (while a
1171 (unless fixed
1172 (setq col (max col (align-column (caar a)))))
1173 (unless change
1174 (goto-char (cdar a))
1175 (if ecol
1176 (if (/= ecol (current-column))
1177 (setq change t))
1178 (setq ecol (current-column))))
1179 (when justify
1180 (goto-char (caar a))
1181 (if (and (re-search-forward "\\s-*" (cdar a) t)
1182 (/= (point) (cdar a)))
1183 (let ((bcol (current-column)))
1184 (setcdr (car a) (cons (point-marker) (cdar a)))
1185 (goto-char (cdr (cdar a)))
1186 (setq width (max width (- (current-column) bcol))))))
1187 (setq a (cdr a))))
1189 (unless fixed
1190 (setq col (+ (align-adjust-col-for-rule
1191 col rule (car props) (cdr props)) width)))
1193 ;; Make all ending positions to occur in the goal column. Since
1194 ;; the whitespace to be modified was already deleted by
1195 ;; `align-region', all we have to do here is indent.
1197 (unless change
1198 (setq change (and ecol (/= col ecol))))
1200 (when (or func change)
1201 (while areas
1202 (let ((area (car areas))
1203 (gocol col) cur)
1204 (when area
1205 (if func
1206 (funcall func (car area) (cdr area) change)
1207 (if (not (and justify
1208 (consp (cdr area))))
1209 (goto-char (cdr area))
1210 (goto-char (cddr area))
1211 (let ((ecol (current-column)))
1212 (goto-char (cadr area))
1213 (setq gocol (- col (- ecol (current-column))))))
1214 (setq cur (current-column))
1215 (cond ((< gocol 0) t) ; don't do anything
1216 ((= cur gocol) t) ; don't need to
1217 ((< cur gocol) ; just add space
1218 ;; FIXME: It is stated above that "...the
1219 ;; whitespace to be modified was already
1220 ;; deleted by `align-region', all we have
1221 ;; to do here is indent." However, this
1222 ;; doesn't seem to be true, so we first
1223 ;; delete the whitespace to avoid tabs
1224 ;; after spaces.
1225 (delete-horizontal-space t)
1226 (indent-to gocol))
1228 ;; This code works around an oddity in the
1229 ;; FORCE argument of `move-to-column', which
1230 ;; tends to screw up markers if there is any
1231 ;; tabbing.
1232 (let ((endcol (align-column
1233 (if (and justify
1234 (consp (cdr area)))
1235 (cadr area)
1236 (cdr area))))
1237 (abuts (<= gocol
1238 (align-column (car area)))))
1239 (if abuts
1240 (goto-char (car area))
1241 (move-to-column gocol t))
1242 (let ((here (point)))
1243 (move-to-column endcol t)
1244 (delete-region here (point))
1245 (if abuts
1246 (indent-to (align-adjust-col-for-rule
1247 (current-column) rule
1248 (car props) (cdr props)))))))))))
1249 (setq areas (cdr areas))))))
1251 (defun align-region (beg end separate rules exclude-rules
1252 &optional func)
1253 "Align a region based on a given set of alignment rules.
1254 BEG and END specify the region to be aligned. Either may be nil, in
1255 which case the range will stop at the nearest section division (see
1256 `align-region-separate', and `align-region-heuristic' for more
1257 information').
1259 The region will be divided into separate alignment sections based on
1260 the value of SEPARATE.
1262 RULES and EXCLUDE-RULES are a pair of lists describing how to align
1263 the region, and which text areas within it should be excluded from
1264 alignment. See the `align-rules-list' for more information on the
1265 required format of these two lists.
1267 If FUNC is specified, no text will be modified. What `align-region'
1268 will do with the rules is to search for the alignment areas, as it
1269 regularly would, taking account for exclusions, and then call FUNC,
1270 first with the beginning and ending of the region to be aligned
1271 according to that rule (this can be different for each rule, if BEG
1272 and END were nil), and then with the beginning and ending of each
1273 text region that the rule would have applied to.
1275 The signature of FUNC should thus be:
1277 (defun my-align-function (beg end mode)
1278 \"If MODE is a rule (a list), return t if BEG to END are to be searched.
1279 Otherwise BEG to END will be a region of text that matches the rule's
1280 definition, and MODE will be non-nil if any changes are necessary.\"
1281 (unless (and mode (listp mode))
1282 (message \"Would have aligned from %d to %d...\" beg end)))
1284 This feature (of passing a FUNC) is used internally to locate the
1285 position of exclusion areas, but could also be used for any other
1286 purpose where you might want to know where the regions that the
1287 aligner would have dealt with are."
1288 (let ((end-mark (and end (copy-marker end t)))
1289 (real-beg beg)
1290 (real-end end)
1291 (report (and (not func) align-large-region beg end
1292 (>= (- end beg) align-large-region)))
1293 (rule-index 1)
1294 (rule-count (length rules)))
1295 (if (and align-indent-before-aligning real-beg end-mark)
1296 (indent-region real-beg end-mark nil))
1297 (while rules
1298 (let* ((rule (car rules))
1299 (run-if (assq 'run-if rule))
1300 (modes (assq 'modes rule)))
1301 ;; unless the `run-if' form tells us not to, look for the
1302 ;; rule..
1303 (unless (or (and modes (not (memq major-mode
1304 (eval (cdr modes)))))
1305 (and run-if (not (funcall (cdr run-if)))))
1306 (let* ((current-case-fold case-fold-search)
1307 (case-fold (assq 'case-fold rule))
1308 (regexp (cdr (assq 'regexp rule)))
1309 (regfunc (and (functionp regexp) regexp))
1310 (rulesep (assq 'separate rule))
1311 (thissep (if rulesep (cdr rulesep) separate))
1312 same (eol 0)
1313 group group-c
1314 spacing spacing-c
1315 tab-stop tab-stop-c
1316 repeat repeat-c
1317 valid valid-c
1318 pos-list first
1319 regions index
1320 last-point b e
1321 save-match-data
1322 exclude-p
1323 align-props)
1324 (save-excursion
1325 ;; if beg and end were not given, figure out what the
1326 ;; current alignment region should be. Depending on the
1327 ;; value of `align-region-separate' it's possible for
1328 ;; each rule to have its own definition of what that
1329 ;; current alignment section is.
1330 (if real-beg
1331 (goto-char beg)
1332 (if (or (not thissep) (eq thissep 'entire))
1333 (error "Cannot determine alignment region for '%s'"
1334 (symbol-name (cdr (assq 'title rule)))))
1335 (beginning-of-line)
1336 (while (and (not (eobp))
1337 (looking-at "^\\s-*$"))
1338 (forward-line))
1339 (let* ((here (point))
1340 (start here))
1341 (while (and here
1342 (let ((terminus
1343 (and align-region-heuristic
1344 (- (point)
1345 align-region-heuristic))))
1346 (if regfunc
1347 (funcall regfunc terminus t)
1348 (re-search-backward regexp
1349 terminus t))))
1350 (if (align-new-section-p (point) here thissep)
1351 (setq beg here
1352 here nil)
1353 (setq here (point))))
1354 (if (not here)
1355 (goto-char beg))
1356 (beginning-of-line)
1357 (setq beg (point))
1358 (goto-char start)
1359 (setq here (point))
1360 (while (and here
1361 (let ((terminus
1362 (and align-region-heuristic
1363 (+ (point)
1364 align-region-heuristic))))
1365 (if regfunc
1366 (funcall regfunc terminus nil)
1367 (re-search-forward regexp terminus t))))
1368 (if (align-new-section-p here (point) thissep)
1369 (setq end here
1370 here nil)
1371 (setq here (point))))
1372 (if (not here)
1373 (goto-char end))
1374 (forward-line)
1375 (setq end (point)
1376 end-mark (copy-marker end t))
1377 (goto-char beg)))
1379 ;; If we have a region to align, and `func' is set and
1380 ;; reports back that the region is ok, then align it.
1381 (when (or (not func)
1382 (funcall func beg end rule))
1383 (unwind-protect
1384 (let (exclude-areas)
1385 ;; determine first of all where the exclusions
1386 ;; lie in this region
1387 (when exclude-rules
1388 ;; guard against a problem with recursion and
1389 ;; dynamic binding vs. lexical binding, since
1390 ;; the call to `align-region' below will
1391 ;; re-enter this function, and rebind
1392 ;; `exclude-areas'
1393 (set (setq exclude-areas
1394 (make-symbol "align-exclude-areas"))
1395 nil)
1396 (align-region
1397 beg end 'entire
1398 exclude-rules nil
1399 `(lambda (b e mode)
1400 (or (and mode (listp mode))
1401 (set (quote ,exclude-areas)
1402 (cons (cons b e)
1403 ,exclude-areas)))))
1404 (setq exclude-areas
1405 (sort (symbol-value exclude-areas)
1406 (function
1407 (lambda (l r)
1408 (>= (car l) (car r)))))))
1410 ;; set `case-fold-search' according to the
1411 ;; (optional) `case-fold' property
1412 (and case-fold
1413 (setq case-fold-search (cdr case-fold)))
1415 ;; while we can find the rule in the alignment
1416 ;; region..
1417 (while (and (< (point) end-mark)
1418 (if regfunc
1419 (funcall regfunc end-mark nil)
1420 (re-search-forward regexp
1421 end-mark t)))
1423 ;; give the user some indication of where we
1424 ;; are, if it's a very large region being
1425 ;; aligned
1426 (if report
1427 (let ((symbol (car rule)))
1428 (if (and symbol (symbolp symbol))
1429 (message
1430 "Aligning `%s' (rule %d of %d) %d%%..."
1431 (symbol-name symbol) rule-index rule-count
1432 (/ (* (- (point) real-beg) 100)
1433 (- end-mark real-beg)))
1434 (message
1435 "Aligning %d%%..."
1436 (/ (* (- (point) real-beg) 100)
1437 (- end-mark real-beg))))))
1439 ;; if the search ended us on the beginning of
1440 ;; the next line, move back to the end of the
1441 ;; previous line.
1442 (if (bolp)
1443 (forward-char -1))
1445 ;; lookup the `group' attribute the first time
1446 ;; that we need it
1447 (unless group-c
1448 (setq group (or (cdr (assq 'group rule)) 1))
1449 (if (listp group)
1450 (setq first (car group))
1451 (setq first group group (list group)))
1452 (setq group-c t))
1454 (unless spacing-c
1455 (setq spacing (cdr (assq 'spacing rule))
1456 spacing-c t))
1458 (unless tab-stop-c
1459 (setq tab-stop
1460 (let ((rule-ts (assq 'tab-stop rule)))
1461 (if rule-ts
1462 (cdr rule-ts)
1463 (if (symbolp align-to-tab-stop)
1464 (symbol-value align-to-tab-stop)
1465 align-to-tab-stop)))
1466 tab-stop-c t))
1468 ;; test whether we have found a match on the same
1469 ;; line as a previous match
1470 (if (> (point) eol)
1471 (setq same nil
1472 eol (save-excursion
1473 (end-of-line)
1474 (point-marker))))
1476 ;; lookup the `repeat' attribute the first time
1477 (or repeat-c
1478 (setq repeat (cdr (assq 'repeat rule))
1479 repeat-c t))
1481 ;; lookup the `valid' attribute the first time
1482 (or valid-c
1483 (setq valid (assq 'valid rule)
1484 valid-c t))
1486 ;; remember the beginning position of this rule
1487 ;; match, and save the match-data, since either
1488 ;; the `valid' form, or the code that searches for
1489 ;; section separation, might alter it
1490 (setq b (match-beginning first)
1491 save-match-data (match-data))
1493 ;; unless the `valid' attribute is set, and tells
1494 ;; us that the rule is not valid at this point in
1495 ;; the code..
1496 (unless (and valid (not (funcall (cdr valid))))
1498 ;; look to see if this match begins a new
1499 ;; section. If so, we should align what we've
1500 ;; collected so far, and then begin collecting
1501 ;; anew for the next alignment section
1502 (if (and last-point
1503 (align-new-section-p last-point b
1504 thissep))
1505 (progn
1506 (align-regions regions align-props
1507 rule func)
1508 (setq last-point (copy-marker b t)
1509 regions nil
1510 align-props nil))
1511 (setq last-point (copy-marker b t)))
1513 ;; restore the match data
1514 (set-match-data save-match-data)
1516 ;; check whether the region to be aligned
1517 ;; straddles an exclusion area
1518 (let ((excls exclude-areas))
1519 (setq exclude-p nil)
1520 (while excls
1521 (if (and (< (match-beginning (car group))
1522 (cdar excls))
1523 (> (match-end (car (last group)))
1524 (caar excls)))
1525 (setq exclude-p t
1526 excls nil)
1527 (setq excls (cdr excls)))))
1529 ;; go through the list of parenthesis groups
1530 ;; matching whitespace text to be
1531 ;; contracted/expanded (or possibly
1532 ;; justified, if the `justify' attribute was
1533 ;; set)
1534 (unless exclude-p
1535 (let ((g group))
1536 (while g
1538 ;; we have to use markers, since
1539 ;; `align-areas' may modify the buffer
1540 (setq b (copy-marker
1541 (match-beginning (car g)) t)
1542 e (copy-marker (match-end (car g)) t))
1544 ;; record this text region for alignment
1545 (setq index (if same (1+ index) 0))
1546 (let ((region (cons b e))
1547 (props (cons
1548 (if (listp spacing)
1549 (car spacing)
1550 spacing)
1551 (if (listp tab-stop)
1552 (car tab-stop)
1553 tab-stop))))
1554 (if (nth index regions)
1555 (setcar (nthcdr index regions)
1556 (cons region
1557 (nth index regions)))
1558 (if regions
1559 (progn
1560 (nconc regions
1561 (list (list region)))
1562 (nconc align-props (list props)))
1563 (setq regions
1564 (list (list region)))
1565 (setq align-props (list props)))))
1567 ;; if any further rule matches are
1568 ;; found before `eol', then they are
1569 ;; on the same line as this one; this
1570 ;; can only happen if the `repeat'
1571 ;; attribute is non-nil
1572 (if (listp spacing)
1573 (setq spacing (cdr spacing)))
1574 (if (listp tab-stop)
1575 (setq tab-stop (cdr tab-stop)))
1576 (setq same t g (cdr g))))
1578 ;; if `repeat' has not been set, move to
1579 ;; the next line; don't bother searching
1580 ;; anymore on this one
1581 (if (and (not repeat) (not (bolp)))
1582 (forward-line)))))
1584 ;; when they are no more matches for this rule,
1585 ;; align whatever was left over
1586 (if regions
1587 (align-regions regions align-props rule func)))
1589 (setq case-fold-search current-case-fold)))))))
1590 (setq rules (cdr rules)
1591 rule-index (1+ rule-index)))
1593 (if report
1594 (message "Aligning...done"))))
1596 ;; Provide:
1598 (provide 'align)
1600 (run-hooks 'align-load-hook)
1602 ;;; arch-tag: ef79cccf-1db8-4888-a8a1-d7ce2d1532f7
1603 ;;; align.el ends here