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