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