1 ;;; align --- align text to a specific column, by regexp
3 ;; Copyright (C) 1999, 2000 Free Sofware Foundation
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Keywords: convenience languages lisp
7 ;; X-URL: http://www.emacs.org/~johnw/emacs.html
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This mode allows you to align regions in a context-sensitive fashion.
29 ;; The classic use is to align assignments:
43 ;; There are several variables which define how certain "categories"
44 ;; of syntax are to be treated. These variables go by the name
45 ;; `align-CATEGORY-modes'. For example, "c++" is such a category.
46 ;; There are several rules which apply to c++, but since several other
47 ;; languages have a syntax similar to c++ (e.g., c, java, etc), these
48 ;; modes are treated as belonging to the same category.
50 ;; If you want to add a new mode under a certain category, just
51 ;; customize that list, or add the new mode manually. For example, to
52 ;; make jde-mode a c++ category mode, use this code in your .emacs
55 ;; (setq align-c++-modes (cons 'jde-mode align-c++-modes))
57 ;; In some programming modes, it's useful to have the aligner run only
58 ;; after indentation is performed. To achieve this, customize or set
59 ;; the variable `align-indent-before-aligning' to t.
63 ;; In order to incorporate align's functionality into your own
64 ;; modules, there are only a few steps you have to follow.
66 ;; 1. Require or load in the align.el library.
68 ;; 2. Define your alignment and exclusion rules lists, either
69 ;; customizable or not.
71 ;; 3. In your mode function, set the variables
72 ;; `align-mode-rules-list' and `align-mode-exclude-rules-list'
73 ;; to your own rules lists.
75 ;; If there is any need to add your mode name to one of the
76 ;; align-?-modes variables (for example, `align-dq-string-modes'), use
77 ;; `add-to-list', or some similar function which checks first to see
78 ;; if the value is already there. Since the user may customize that
79 ;; mode list, and then write your mode name into their .emacs file,
80 ;; causing the symbol already to be present the next time they load
87 ;; (defcustom my-align-rules-list
89 ;; (regexp . "Sample")))
90 ;; :type align-rules-list-type
91 ;; :group 'my-package)
93 ;; (put 'my-align-rules-list 'risky-local-variable t)
95 ;; (add-to-list 'align-dq-string-modes 'my-package-mode)
96 ;; (add-to-list 'align-open-comment-modes 'my-package-mode)
100 ;; (setq align-mode-rules-list my-align-rules-list))
102 ;; Note that if you need to install your own exclusion rules, then you
103 ;; will also need to reproduce any double-quoted string, or open
104 ;; comment exclusion rules that are defined in the standard
105 ;; `align-exclude-rules-list'. At the moment there is no convenient
106 ;; way to mix both mode-local and global rules lists.
110 ;; Version 1.0 was created in the earlier part of 1996, using a very
111 ;; simple algorithm that understand only basic regular expressions.
112 ;; Parts of the code were broken up and included in vhdl-mode.el
113 ;; around this time. After several comments from users, and a need to
114 ;; find a more robust, performant algorithm, 2.0 was born in late
115 ;; 1998. Many different approaches were taken (mostly due to the
116 ;; complexity of TeX tables), but finally a scheme was discovered
117 ;; which worked fairly well for most common usage cases. Development
118 ;; beyond version 2.8 is not planned, except for problems that users
124 "Align text to a specific column, by regexp."
129 (defcustom align-load-hook nil
130 "*Hook that gets run after the aligner has been loaded."
134 (defcustom align-indent-before-aligning nil
135 "*If non-nil, indent the marked region before aligning it."
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
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
)
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."
165 (defcustom align-highlight-change-face
'highlight
166 "*The face to highlight with if changes are necessary."
170 (defcustom align-highlight-nochange-face
'secondary-selection
171 "*The face to highlight with if no changes are necessary."
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."
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
)
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
)
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
)
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
)
203 (defcustom align-text-modes
'(text-mode outline-mode
)
204 "*A list of modes whose content is plain text."
205 :type
'(repeat symbol
)
208 (defcustom align-dq-string-modes
209 (append align-lisp-modes align-c
++-modes align-perl-modes
211 "*A list of modes where double quoted strings should be excluded."
212 :type
'(repeat symbol
)
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
)
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
)
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 */
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
252 int alpha = 1; /* one */
254 long gamma; /* ten */
256 unsigned int delta = 1; /* one */
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 */
267 long gamma; /* ten */
269 unsigned int delta = 1; /* one */
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 */
279 long gamma; /* ten */
281 unsigned int delta = 1; /* one */
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
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
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
315 (const :tag
"Entire region is one section" entire
)
316 (const :tag
"Align by contiguous groups" group
)
318 (regexp :tag
"Regexp defines section boundaries")
319 (function :tag
"Function defines section boundaries"))
322 (put 'align-region-separate
'risky-local-variable t
)
324 (defvar align-rules-list-type
327 :tag
"Alignment rule"
328 (symbol :tag
"Title")
329 (cons :tag
"Required attributes"
331 (const :tag
"(Regular expression to match)" regexp
)
332 (choice :value
"\\(\\s-+\\)" regexp function
))
334 :tag
"Optional attributes"
337 (const :tag
"(Repeat this rule throughout line)"
340 (cons :tag
"Paren group"
341 (const :tag
"(Parenthesis group to use)" group
)
343 integer
(repeat integer
)))
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)"
351 (cons :tag
"To Tab Stop"
352 (const :tag
"(Should rule align to tab stops)"
354 (boolean :value nil
))
356 (const :tag
"(Return non-nil if rule is valid)"
360 (const :tag
"(Return non-nil if rule should run)"
364 (const :tag
"(Column to fix alignment at)" column
)
365 (choice :value comment-column
368 (const :tag
"(Amount of spacing to use)" spacing
)
371 (const :tag
"(Should text be right justified)"
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)"
379 (choice :value
"^\\s-*$"
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
391 (regexp .
"\\(^\\s-+[^( \t\n]\\|(\\(\\S-+\\)\\s-+\\)\\S-+\\(\\s-+\\)")
393 (modes . align-lisp-modes
)
394 (run-if .
,(function (lambda () current-prefix-arg
))))
397 (regexp .
"\\(\\s-*\\)\\.\\(\\s-*\\)")
399 (modes . align-lisp-modes
))
403 (lambda (end reverse
)
404 (funcall (if reverse
're-search-backward
406 (concat "[^ \t\n\\\\]"
407 (regexp-quote comment-start
)
408 "\\(.+\\)$") end t
))))
409 (modes . align-open-comment-modes
))
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-*$\\)"))
421 (modes . align-c
++-modes
)
426 (not (or (save-excursion
427 (goto-char (match-beginning 1))
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
))))))))
437 (regexp .
,(concat "[^-=!^&*+<>/| \t\n]\\(\\s-*[-=!^&*+<>/|]*\\)"
438 "=\\(\\s-*\\)\\([^= \t\n]\\|$\\)"))
440 (modes . align-c
++-modes
)
445 (regexp .
,(concat "[^=!^&*-+<>/| \t\n]\\(\\s-*\\)=[~>]?"
446 "\\(\\s-*\\)\\([^>= \t\n]\\|$\\)"))
448 (modes . align-perl-modes
)
452 (regexp .
,(concat "[^=!<> \t\n]\\(\\s-*\\)="
453 "\\(\\s-*\\)\\([^>= \t\n]\\|$\\)"))
455 (modes .
'(python-mode))
459 (regexp .
"^\\s-*\\w+\\(\\s-*\\):?=\\(\\s-*\\)\\([^\t\n \\\\]\\|$\\)")
461 (modes .
'(makefile-mode))
465 (regexp .
",\\(\\s-*\\)[^/ \t\n]")
467 (modes . align-c
++-modes
)
468 (run-if .
,(function (lambda () current-prefix-arg
))))
472 ; (memq (caar (c-guess-basic-syntax))
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
481 (basic-comma-delimiter
482 (regexp .
",\\(\\s-*\\)[^# \t\n]")
484 (modes .
(append align-perl-modes
'(python-mode)))
485 (run-if .
,(function (lambda () current-prefix-arg
))))
488 (regexp .
"\\(\\s-*\\)\\(//.*\\|/\\*.*\\*/\\s-*\\)$")
489 (modes . align-c
++-modes
)
490 (column . comment-column
)
494 (goto-char (match-beginning 1))
498 (regexp .
"\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
499 (modes . align-c
++-modes
)
503 (goto-char (match-end 2))
504 (looking-at "\\s-*\\(/[*/]\\|$\\)"))))))
507 (regexp .
"\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
508 (modes . align-perl-modes
)
512 (goto-char (match-end 2))
513 (looking-at "\\s-*\\(#\\|$\\)"))))))
516 (regexp .
"\\(\\s-*\\)\\(\\<and\\>\\|\\<or\\>\\)")
517 (modes .
'(python-mode))
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
))
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
540 (lambda (end reverse
)
541 (align-match-tex-pattern "&" end reverse
))))
543 (modes . align-tex-modes
)
546 (tex-tabbing-separator
548 (lambda (end reverse
)
549 (align-match-tex-pattern "\\\\[=>]" end reverse
))))
551 (modes . align-tex-modes
)
555 (eq major-mode
'latex-mode
)))))
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.
564 (regexp .
"\\(^\\|\\S-\\)\\(\\s-+\\)\\(\\S-\\|$\\)")
566 (modes . align-text-modes
)
570 (and current-prefix-arg
571 (not (eq '- current-prefix-arg
)))))))
573 ;; With a negative prefix argument, lists of dollar figures will
576 (regexp .
"\\$?\\(\\s-+[0-9]+\\)\\.")
577 (modes . align-text-modes
)
581 (eq '- current-prefix-arg
))))))
582 "*An list describing all of the available alignment rules.
586 (ATTRIBUTE . VALUE) ...)
589 The following attributes are meaningful:
591 `regexp' This required attribute must be either a string describing
592 a regular expression, or a function (described below).
593 For every line within the section that this regular
594 expression matches, the given rule will be applied to that
595 line. The exclusion rules denote which part(s) of the
596 line should not be modified; the alignment rules cause the
597 identified whitespace group to be contracted/expanded such
598 that the \"alignment character\" (the character
599 immediately following the identified parenthesis group),
600 occurs in the same column for every line within the
601 alignment section (see `align-region-separate' for a
602 description of how the region is broken up into alignment
605 The `regexp' attribute describes how the text should be
606 treated. Within this regexp, there must be at least one
607 group of characters (typically whitespace) identified by
608 the special opening and closing parens used in regexp
609 expressions (`\\\\(' and `\\\\)') (see the Emacs manual on
610 the syntax of regular expressions for more info).
612 If `regexp' is a function, it will be called as a
613 replacement for `re-search-forward'. This means that it
614 should return nil if nothing is found to match the rule,
615 or it should set the match data appropriately, move point
616 to the end of the match, and return the value of point.
618 `group' For exclusion rules, the group identifies the range of
619 characters that should be ignored. For alignment rules,
620 these are the characters that will be deleted/expanded for
621 the purposes of alignment. The \"alignment character\" is
622 always the first character immediately following this
623 parenthesis group. This attribute may also be a list of
624 integer, in which case multiple alignment characters will
625 be aligned, with the list of integer identifying the
626 whitespace groups which precede them. The default for
629 `modes' The `modes' attribute, if set, should name a list of
630 major modes -- or evaluate to such a value -- in which the
631 rule is valid. If not set, the rule will apply to all
634 `case-fold' If `regexp' is an ordinary regular expression string
635 containing alphabetic character, sometimes you may want
636 the search to proceed case-insensitively (for languages
637 that ignore case, such as pascal for example). In that
638 case, set `case-fold' to nil, and the regular expression
639 search will ignore case. If `regexp' is set to a
640 function, that function must handle the job of ignoring
643 `tab-stop' If the `tab-stop' attribute is set, and non-nil, the
644 alignment character will always fall on a tab stop
645 (whether it uses tabs to get there or not depends on the
646 value of `indent-tabs-mode'). If the `tab-stop' attribute
647 is set to nil, tab stops will never be used. Otherwise,
648 the value of `align-to-tab-stop' determines whether or not
649 to align to a tab stop. The `tab-stop' attribute may also
650 be a list of t or nil values, corresponding to the number
651 of parenthesis groups specified by the `group' attribute.
653 `repeat' If the `repeat' attribute is present, and non-nil, the
654 rule will be applied to the line continuously until no
655 further matches are found.
657 `valid' If the `valid' attribute is set, it will be used to
658 determine whether the rule should be invoked. This form
659 is evaluated after the regular expression match has been
660 performed, so that it is possible to use the results of
661 that match to determine whether the alignment should be
662 performed. The buffer should not be modified during the
663 evaluation of this form.
665 `run-if' Like `valid', the `run-if' attribute tests whether the
666 rule should be run at all -- even before any searches are
667 done to determine if the rule applies to the alignment
668 region. This can save time, since `run-if' will only be
669 run once for each rule. If it returns nil, the rule will
672 `column' For alignment rules, if the `column' attribute is set --
673 which must be an integer, or a symbol whose value is an
674 integer -- it will be used as the column in which to align
675 the alignment character. If the text on a particular line
676 happens to overrun that column, a single space character,
677 or tab stop (see `align-to-tab-stop') will be added
678 between the last text character and the alignment
681 `spacing' Alignment rules may also override the amount of spacing
682 that would normally be used by providing a `spacing'
683 attribute. This must be an integer, or a list of integers
684 corresponding to the number of parenthesis groups matched
685 by the `group' attribute. If a list of value is used, and
686 any of those values is nil, `align-default-spacing' will
687 be used for that subgroup. See `align-default-spacing'
688 for more details on spacing, tab stops, and how to
689 indicate how much spacing should be used. If TAB-STOP is
690 present, it will override the value of `align-to-tab-stop'
693 `justify' It is possible with `regexp' and `group' to identify a
694 character group that contains more than just whitespace
695 characters. By default, any non-whitespace characters in
696 that group will also be deleted while aligning the
697 alignment character. However, if the `justify' attribute
698 is set to a non-nil value, only the initial whitespace
699 characters within that group will be deleted. This has
700 the effect of right-justifying the characters that remain,
701 and can be used for outdenting or just plain old right-
704 `separate' Each rule can define its own section separator, which
705 describes how to identify the separation of \"sections\"
706 within the region to be aligned. Setting the `separate'
707 attribute overrides the value of `align-region-separate'
708 (see the documentation of that variable for possible
709 values), and any separation argument passed to `align'."
710 :type align-rules-list-type
713 (put 'align-rules-list
'risky-local-variable t
)
715 (defvar align-exclude-rules-list-type
718 :tag
"Exclusion rule"
719 (symbol :tag
"Title")
720 (cons :tag
"Required attributes"
722 (const :tag
"(Regular expression to match)" regexp
)
723 (choice :value
"\\(\\s-+\\)" regexp function
))
725 :tag
"Optional attributes"
728 (const :tag
"(Repeat this rule throughout line)"
731 (cons :tag
"Paren group"
732 (const :tag
"(Parenthesis group to use)" group
)
734 integer
(repeat integer
)))
736 (const :tag
"(Modes where this rule applies)" modes
)
737 (sexp :value
(text-mode)))
738 (cons :tag
"Case-fold"
739 (const :tag
"(Should case be ignored for this rule)"
741 (boolean :value t
)))))))
742 "The `type' form for any `align-exclude-rules-list' variable.")
744 (defcustom align-exclude-rules-list
746 (regexp .
"\"\\([^\"\n]+\\)\"")
748 (modes . align-dq-string-modes
))
751 (regexp .
"'\\([^'\n]+\\)'")
753 (modes . align-sq-string-modes
))
758 (lambda (end reverse
)
759 (funcall (if reverse
're-search-backward
761 (concat "[^ \t\n\\\\]"
762 (regexp-quote comment-start
)
763 "\\(.+\\)$") end t
))))
764 (modes . align-open-comment-modes
))
767 (regexp .
"/\\*\\(.+\\)\\*/")
769 (modes . align-c
++-modes
))
772 (regexp .
"(\\([^)\n]+\\))")
774 (modes . align-c
++-modes
))
777 (regexp .
"^\\s-*#\\s-*\\(if\\w*\\|endif\\)\\(.*\\)$")
779 (modes . align-c
++-modes
)))
780 "*An list describing text that should be excluded from alignment.
781 See the documentation for `align-rules-list' for more info."
782 :type align-exclude-rules-list-type
785 (put 'align-exclude-rules-list
'risky-local-variable t
)
787 ;;; Internal Variables:
789 (defvar align-mode-rules-list nil
790 "Alignment rules specific to the current major mode.
791 See the variable `align-rules-list' for more details.")
793 (make-variable-buffer-local 'align-mode-rules-list
)
795 (defvar align-mode-exclude-rules-list nil
796 "Alignment exclusion rules specific to the current major mode.
797 See the variable `align-exclude-rules-list' for more details.")
799 (make-variable-buffer-local 'align-mode-exclude-rules-list
)
801 (defvar align-highlight-overlays nil
802 "The current overlays highlighting the text matched by a rule.")
804 ;; Sample extension rule set, for vhdl-mode. This should properly be
805 ;; in vhdl-mode.el itself.
807 (defcustom align-vhdl-rules-list
809 (regexp .
"\\(signal\\|variable\\|constant\\)\\(\\s-+\\)\\S-")
813 (regexp .
"\\(others\\|[^ \t\n=<]\\)\\(\\s-*\\)=>\\(\\s-*\\)\\S-")
818 (not (string= (downcase (match-string 1))
822 (regexp .
"[^ \t\n:]\\(\\s-*\\):\\(\\s-*\\)[^=\n]")
826 (regexp .
":\\s-*\\(in\\|out\\|inout\\|buffer\\)\\(\\s-*\\)")
830 (regexp .
"[^ \t\n=<]\\(\\s-*\\)<=\\(\\s-*\\)\\S-")
834 (regexp .
"[^ \t\n:]\\(\\s-*\\):="))
837 (regexp .
"\\(\\s-+\\)use\\s-+entity")))
838 "*Alignment rules for `vhdl-mode'. See `align-rules-list' for more info."
839 :type align-rules-list-type
842 (put 'align-vhdl-rules-list
'risky-local-variable t
)
844 (defun align-set-vhdl-rules ()
845 "Setup the `align-mode-rules-list' variable for `vhdl-mode'."
846 (setq align-mode-rules-list align-vhdl-rules-list
))
848 (add-hook 'vhdl-mode-hook
'align-set-vhdl-rules
)
850 (add-to-list 'align-dq-string-modes
'vhdl-mode
)
851 (add-to-list 'align-open-comment-modes
'vhdl-mode
)
856 (defun align (beg end
&optional separate rules exclude-rules
)
857 "Attempt to align a region based on a set of alignment rules.
858 BEG and END mark the region. If BEG and END are specifically set to
859 nil (this can only be done programmatically), the beginning and end of
860 the current alignment section will be calculated based on the location
861 of point, and the value of `align-region-separate' (or possibly each
862 rule's `separate' attribute).
864 If SEPARATE is non-nil, it overrides the value of
865 `align-region-separate' for all rules, except those that have their
866 `separate' attribute set.
868 RULES and EXCLUDE-RULES, if either is non-nil, will replace the
869 default rule lists defined in `align-rules-list' and
870 `align-exclude-rules-list'. See `align-rules-list' for more details
871 on the format of these lists."
875 (if (symbolp align-region-separate
)
876 (symbol-value align-region-separate
)
877 align-region-separate
)
879 (if (not (or ;(eq separator 'largest)
880 (and (functionp separator
)
881 (not (funcall separator nil nil
)))))
882 (align-region beg end separator
883 (or rules align-mode-rules-list align-rules-list
)
884 (or exclude-rules align-mode-exclude-rules-list
885 align-exclude-rules-list
))
886 (let ((sec-first end
)
888 (align-region beg end
890 align-mode-exclude-rules-list
891 align-exclude-rules-list
) nil
895 (when (and mode
(listp mode
))
896 (setq sec-first
(min sec-first b
)
897 sec-last
(max sec-last e
))))))
898 (if (< sec-first sec-last
)
899 (align-region sec-first sec-last
'entire
900 (or rules align-mode-rules-list align-rules-list
)
901 (or exclude-rules align-mode-exclude-rules-list
902 align-exclude-rules-list
)))))))
905 (defun align-regexp (beg end regexp
&optional group spacing repeat
)
906 "Align the current region using an ad-hoc rule read from the minibuffer.
907 BEG and END mark the limits of the region. This function will prompt
908 for the REGEXP to align with. If no prefix arg was specified, you
909 only need to supply the characters to be lined up and any preceding
910 whitespace is replaced. If a prefix arg was specified, the full
911 regexp with parenthesized whitespace should be supplied; it will also
912 prompt for which parenthesis GROUP within REGEXP to modify, the amount
913 of SPACING to use, and whether or not to REPEAT the rule throughout
914 the line. See `align-rules-list' for more information about these
917 For example, let's say you had a list of phone numbers, and wanted to
918 align them so that the opening parentheses would line up:
922 Mary-Anne (123) 456-7890
925 There is no predefined rule to handle this, but you could easily do it
926 using a REGEXP like \"(\". All you would have to do is to mark the
927 region, call `align-regexp' and type in that regular expression."
930 (list (min (point) (mark))
931 (max (point) (mark)))
932 (if current-prefix-arg
933 (list (read-string "Complex align using regexp: "
937 "Parenthesis group to modify (justify if negative): " "1"))
939 (read-string "Amount of spacing (or column if negative): "
940 (number-to-string align-default-spacing
)))
941 (y-or-n-p "Repeat throughout line? "))
942 (list (concat "\\(\\s-*\\)"
943 (read-string "Align regexp: "))
944 1 align-default-spacing nil
))))
946 (list (list nil
(cons 'regexp regexp
)
947 (cons 'group
(abs group
))
952 (cons 'spacing spacing
)
953 (cons 'column
(abs spacing
)))
954 (cons 'repeat repeat
)))))
955 (align-region beg end
'entire rule nil nil
)))
958 (defun align-entire (beg end
&optional rules exclude-rules
)
959 "Align the selected region as if it were one alignment section.
960 BEG and END mark the extent of the region. If RULES or EXCLUDE-RULES
961 is set to a list of rules (see `align-rules-list'), it can be used to
962 override the default alignment rules that would have been used to
965 (align beg end
'entire rules exclude-rules
))
968 (defun align-current (&optional rules exclude-rules
)
969 "Call `align' on the current alignment section.
970 This function assumes you want to align only the current section, and
971 so saves you from having to specify the region. If RULES or
972 EXCLUDE-RULES is set to a list of rules (see `align-rules-list'), it
973 can be used to override the default alignment rules that would have
974 been used to align that section."
976 (align nil nil nil rules exclude-rules
))
979 (defun align-highlight-rule (beg end title
&optional rules exclude-rules
)
980 "Highlight the whitespace which a given rule would have modified.
981 BEG and END mark the extent of the region. TITLE identifies the rule
982 that should be highlighted. If RULES or EXCLUDE-RULES is set to a
983 list of rules (see `align-rules-list'), it can be used to override the
984 default alignment rules that would have been used to identify the text
987 (list (min (mark) (point))
990 "Title of rule to highlight: "
994 (list (symbol-name (car rule
)))))
995 (append (or align-mode-rules-list align-rules-list
)
996 (or align-mode-exclude-rules-list
997 align-exclude-rules-list
))) nil t
)))
998 (let ((ex-rule (assq (intern title
)
999 (or align-mode-exclude-rules-list
1000 align-exclude-rules-list
)))
1002 (align-unhighlight-rule)
1005 (or rules
(if ex-rule
1006 (or exclude-rules align-mode-exclude-rules-list
1007 align-exclude-rules-list
)
1008 (or align-mode-rules-list align-rules-list
)))
1009 (unless ex-rule
(or exclude-rules align-mode-exclude-rules-list
1010 align-exclude-rules-list
))
1013 (if (and mode
(listp mode
))
1014 (if (equal (symbol-name (car mode
)) title
)
1015 (setq face
(cons align-highlight-change-face
1016 align-highlight-nochange-face
))
1019 (let ((overlay (make-overlay b e
)))
1020 (setq align-highlight-overlays
1021 (cons overlay align-highlight-overlays
))
1022 (overlay-put overlay
'face
1025 (cdr face
)))))))))))
1028 (defun align-unhighlight-rule ()
1029 "Remove any highlighting that was added by `align-highlight-rule'."
1031 (while align-highlight-overlays
1032 (delete-overlay (car align-highlight-overlays
))
1033 (setq align-highlight-overlays
1034 (cdr align-highlight-overlays
))))
1037 (defun align-newline-and-indent ()
1038 "A replacement function for `newline-and-indent', aligning as it goes."
1040 (let ((separate (or (if (symbolp align-region-separate
)
1041 (symbol-value align-region-separate
)
1042 align-region-separate
)
1045 (call-interactively 'newline-and-indent
)
1048 (while (not (or (bobp)
1049 (align-new-section-p (point) end separate
)))
1051 (align (point) end
))))
1053 ;;; Internal Functions:
1055 (defun align-match-tex-pattern (regexp end
&optional reverse
)
1056 "Match REGEXP in TeX mode, counting backslashes appropriately.
1057 END denotes the end of the region to be searched, while REVERSE, if
1058 non-nil, indicates that the search should proceed backward from the
1064 (if reverse
're-search-backward
1066 (concat "\\(\\s-*\\)" regexp
1067 "\\(\\s-*\\)") end t
))
1068 (let ((pos (match-end 1))
1070 (while (and (> pos
(point-min))
1071 (eq (char-before pos
) ?
\\))
1072 (setq count
(1+ count
) pos
(1- pos
)))
1073 (eq (mod count
2) 1))
1074 (goto-char (match-beginning 2))))
1077 (defun align-new-section-p (beg end separator
)
1078 "Is there a section divider between BEG and END?
1079 SEPARATOR specifies how to look for the section divider. See the
1080 documentation for `align-region-separate' for more details."
1081 (cond ((or (not separator
)
1082 (eq separator
'entire
))
1084 ((eq separator
'group
)
1090 (> (count-lines beg end
) amount
)))
1091 ((stringp separator
)
1094 (re-search-forward separator end t
)))
1095 ((functionp separator
)
1096 (funcall separator beg end
))
1098 (let ((seps separator
) yes
)
1100 (if (and (>= (car seps
) beg
)
1101 (<= (car seps
) end
))
1102 (setq yes t seps nil
)
1103 (setq seps
(cdr seps
))))
1106 (defun align-adjust-col-for-rule (column rule spacing tab-stop
)
1107 "Adjust COLUMN according to the given RULE.
1108 SPACING specifies how much spacing to use.
1109 TAB-STOP specifies whether SPACING refers to tab-stop boundaries."
1111 (setq spacing align-default-spacing
))
1116 (let ((stops tab-stop-list
))
1118 (if (and (> (car stops
) column
)
1119 (= (setq spacing
(1- spacing
)) 0))
1120 (setq column
(car stops
)
1122 (setq stops
(cdr stops
)))))
1125 (defsubst align-column
(pos)
1126 "Given a position in the buffer, state what column it's in.
1127 POS is the position whose column will be taken. Note that this
1128 function will change the location of point."
1132 (defsubst align-regions
(regions props rule func
)
1133 "Align the regions specified in REGIONS, a list of cons cells.
1134 PROPS describes formatting features specific to the given regions.
1135 RULE specifies exactly how to perform the alignments.
1136 If FUNC is specified, it will be called with each region that would
1137 have been aligned, rather than modifying the text."
1140 (align-areas (car regions
) (car props
) rule func
))
1141 (setq regions
(cdr regions
)
1142 props
(cdr props
))))
1144 (defun align-areas (areas props rule func
)
1145 "Given a list of AREAS and formatting PROPS, align according to RULE.
1146 AREAS should be a list of cons cells containing beginning and ending
1147 markers. This function sweeps through all of the beginning markers,
1148 finds out which one starts in the furthermost column, and then deletes
1149 and inserts text such that all of the ending markers occur in the same
1152 If FUNC is non-nil, it will be called for each text region that would
1153 have been aligned. No changes will be made to the buffer."
1154 (let* ((column (cdr (assq 'column rule
)))
1155 (fixed (if (symbolp column
)
1156 (symbol-value column
)
1158 (justify (cdr (assq 'justify rule
)))
1163 ;; Determine the alignment column.
1167 (setq col
(max col
(align-column (caar a
)))))
1169 (goto-char (cdar a
))
1171 (if (/= ecol
(current-column))
1173 (setq ecol
(current-column))))
1175 (goto-char (caar a
))
1176 (if (and (re-search-forward "\\s-*" (cdar a
) t
)
1177 (/= (point) (cdar a
)))
1178 (let ((bcol (current-column)))
1179 (setcdr (car a
) (cons (point-marker) (cdar a
)))
1180 (goto-char (cdr (cdar a
)))
1181 (setq width
(max width
(- (current-column) bcol
))))))
1185 (setq col
(+ (align-adjust-col-for-rule
1186 col rule
(car props
) (cdr props
)) width
)))
1188 ;; Make all ending positions to occur in the goal column. Since
1189 ;; the whitespace to be modified was already deleted by
1190 ;; `align-region', all we have to do here is indent.
1193 (setq change
(and ecol
(/= col ecol
))))
1195 (when (or func change
)
1197 (let ((area (car areas
))
1201 (funcall func
(car area
) (cdr area
) change
)
1202 (if (not (and justify
1203 (consp (cdr area
))))
1204 (goto-char (cdr area
))
1205 (goto-char (cddr area
))
1206 (let ((ecol (current-column)))
1207 (goto-char (cadr area
))
1208 (setq gocol
(- col
(- ecol
(current-column))))))
1209 (setq cur
(current-column))
1210 (cond ((< gocol
0) t
) ; don't do anything
1211 ((= cur gocol
) t
) ; don't need to
1212 ((< cur gocol
) ; just add space
1215 ;; This code works around an oddity in the
1216 ;; FORCE argument of `move-to-column', which
1217 ;; tends to screw up markers if there is any
1219 (let ((endcol (align-column
1225 (align-column (car area
)))))
1227 (goto-char (car area
))
1228 (move-to-column gocol t
))
1229 (let ((here (point)))
1230 (move-to-column endcol t
)
1231 (delete-region here
(point))
1233 (indent-to (align-adjust-col-for-rule
1234 (current-column) rule
1235 (car props
) (cdr props
)))))))))))
1236 (setq areas
(cdr areas
))))))
1238 (defun align-region (beg end separate rules exclude-rules
1240 "Align a region based on a given set of alignment rules.
1241 BEG and END specify the region to be aligned. Either may be nil, in
1242 which case the range will stop at the nearest section division (see
1243 `align-region-separate', and `align-region-heuristic' for more
1246 The region will be divided into separate alignment sections based on
1247 the value of SEPARATE.
1249 RULES and EXCLUDE-RULES are a pair of lists describing how to align
1250 the region, and which text areas within it should be excluded from
1251 alignment. See the `align-rules-list' for more information on the
1252 required format of these two lists.
1254 If FUNC is specified, no text will be modified. What `align-region'
1255 will do with the rules is to search for the alignment areas, as it
1256 regularly would, taking account for exclusions, and then call FUNC,
1257 first with the beginning and ending of the region to be aligned
1258 according to that rule (this can be different for each rule, if BEG
1259 and END were nil), and then with the beginning and ending of each
1260 text region that the rule would have applied to.
1262 The signature of FUNC should thus be:
1264 (defun my-align-function (beg end mode)
1265 \"If MODE is a rule (a list), return t if BEG to END are to be searched.
1266 Otherwise BEG to END will be a region of text that matches the rule's
1267 definition, and MODE will be non-nil if any changes are necessary.\"
1268 (unless (and mode (listp mode))
1269 (message \"Would have aligned from %d to %d...\" beg end)))
1271 This feature (of passing a FUNC) is used internally to locate the
1272 position of exclusion areas, but could also be used for any other
1273 purpose where you might want to know where the regions that the
1274 aligner would have dealt with are."
1275 (let ((end-mark (and end
(copy-marker end t
)))
1278 (report (and (not func
) align-large-region beg end
1279 (>= (- end beg
) align-large-region
)))
1281 (rule-count (length rules
)))
1282 (if (and align-indent-before-aligning real-beg end-mark
)
1283 (indent-region real-beg end-mark nil
))
1285 (let* ((rule (car rules
))
1286 (run-if (assq 'run-if rule
))
1287 (modes (assq 'modes rule
)))
1288 ;; unless the `run-if' form tells us not to, look for the
1290 (unless (or (and modes
(not (memq major-mode
1291 (eval (cdr modes
)))))
1292 (and run-if
(not (funcall (cdr run-if
)))))
1293 (let* ((current-case-fold case-fold-search
)
1294 (case-fold (assq 'case-fold rule
))
1295 (regexp (cdr (assq 'regexp rule
)))
1296 (regfunc (and (functionp regexp
) regexp
))
1297 (rulesep (assq 'separate rule
))
1298 (thissep (if rulesep
(cdr rulesep
) separate
))
1312 ;; if beg and end were not given, figure out what the
1313 ;; current alignment region should be. Depending on the
1314 ;; value of `align-region-separate' it's possible for
1315 ;; each rule to have its own definition of what that
1316 ;; current alignment section is.
1319 (if (or (not thissep
) (eq thissep
'entire
))
1320 (error "Cannot determine alignment region for '%s'"
1321 (symbol-name (cdr (assq 'title rule
)))))
1323 (while (and (not (eobp))
1324 (looking-at "^\\s-*$"))
1326 (let* ((here (point))
1330 (and align-region-heuristic
1332 align-region-heuristic
))))
1334 (funcall regfunc terminus t
)
1335 (re-search-backward regexp
1337 (if (align-new-section-p (point) here thissep
)
1340 (setq here
(point))))
1349 (and align-region-heuristic
1351 align-region-heuristic
))))
1353 (funcall regfunc terminus nil
)
1354 (re-search-forward regexp terminus t
))))
1355 (if (align-new-section-p here
(point) thissep
)
1358 (setq here
(point))))
1363 end-mark
(copy-marker end t
))
1366 ;; If we have a region to align, and `func' is set and
1367 ;; reports back that the region is ok, then align it.
1368 (when (or (not func
)
1369 (funcall func beg end rule
))
1371 (let (exclude-areas)
1372 ;; determine first of all where the exclusions
1373 ;; lie in this region
1375 ;; guard against a problem with recursion and
1376 ;; dynamic binding vs. lexical binding, since
1377 ;; the call to `align-region' below will
1378 ;; re-enter this function, and rebind
1380 (set (setq exclude-areas
1381 (make-symbol "align-exclude-areas"))
1387 (or (and mode
(listp mode
))
1388 (set (quote ,exclude-areas
)
1392 (sort (symbol-value exclude-areas
)
1395 (>= (car l
) (car r
)))))))
1397 ;; set `case-fold-search' according to the
1398 ;; (optional) `case-fold' property
1400 (setq case-fold-search
(cdr case-fold
)))
1402 ;; while we can find the rule in the alignment
1404 (while (and (< (point) end-mark
)
1406 (funcall regfunc end-mark nil
)
1407 (re-search-forward regexp
1410 ;; give the user some indication of where we
1411 ;; are, if it's a very large region being
1414 (let ((name (symbol-name (car rule
))))
1417 "Aligning `%s' (rule %d of %d) %d%%..."
1418 rule-index rule-count
1419 (/ (* (- (point) real-beg
) 100)
1420 (- end-mark real-beg
)))
1423 (/ (* (- (point) real-beg
) 100)
1424 (- end-mark real-beg
))))))
1426 ;; if the search ended us on the beginning of
1427 ;; the next line, move back to the end of the
1432 ;; lookup the `group' attribute the first time
1435 (setq group
(or (cdr (assq 'group rule
)) 1))
1437 (setq first
(car group
))
1438 (setq first group group
(list group
)))
1442 (setq spacing
(cdr (assq 'spacing rule
))
1447 (let ((rule-ts (assq 'tab-stop rule
)))
1450 (if (symbolp align-to-tab-stop
)
1451 (symbol-value align-to-tab-stop
)
1452 align-to-tab-stop
)))
1455 ;; test whether we have found a match on the same
1456 ;; line as a previous match
1463 ;; lookup the `repeat' attribute the first time
1465 (setq repeat
(cdr (assq 'repeat rule
))
1468 ;; lookup the `valid' attribute the first time
1470 (setq valid
(assq 'valid rule
)
1473 ;; remember the beginning position of this rule
1474 ;; match, and save the match-data, since either
1475 ;; the `valid' form, or the code that searches for
1476 ;; section separation, might alter it
1477 (setq b
(match-beginning first
)
1478 save-match-data
(match-data))
1480 ;; unless the `valid' attribute is set, and tells
1481 ;; us that the rule is not valid at this point in
1483 (unless (and valid
(not (funcall (cdr valid
))))
1485 ;; look to see if this match begins a new
1486 ;; section. If so, we should align what we've
1487 ;; collected so far, and then begin collecting
1488 ;; anew for the next alignment section
1490 (align-new-section-p last-point b
1493 (align-regions regions align-props
1495 (setq last-point
(copy-marker b t
)
1498 (setq last-point
(copy-marker b t
)))
1500 ;; restore the match data
1501 (set-match-data save-match-data
)
1503 ;; check whether the region to be aligned
1504 ;; straddles an exclusion area
1505 (let ((excls exclude-areas
))
1506 (setq exclude-p nil
)
1508 (if (and (< (match-beginning (car group
))
1510 (> (match-end (car (last group
)))
1514 (setq excls
(cdr excls
)))))
1516 ;; go through the list of parenthesis groups
1517 ;; matching whitespace text to be
1518 ;; contracted/expanded (or possibly
1519 ;; justified, if the `justify' attribute was
1525 ;; we have to use markers, since
1526 ;; `align-areas' may modify the buffer
1527 (setq b
(copy-marker
1528 (match-beginning (car g
)) t
)
1529 e
(copy-marker (match-end (car g
)) t
))
1531 ;; record this text region for alignment
1532 (setq index
(if same
(1+ index
) 0))
1533 (let ((region (cons b e
))
1538 (if (listp tab-stop
)
1541 (if (nth index regions
)
1542 (setcar (nthcdr index regions
)
1544 (nth index regions
)))
1548 (list (list region
)))
1549 (nconc align-props
(list props
)))
1551 (list (list region
)))
1552 (setq align-props
(list props
)))))
1554 ;; if any further rule matches are
1555 ;; found before `eol', then they are
1556 ;; on the same line as this one; this
1557 ;; can only happen if the `repeat'
1558 ;; attribute is non-nil
1560 (setq spacing
(cdr spacing
)))
1561 (if (listp tab-stop
)
1562 (setq tab-stop
(cdr tab-stop
)))
1563 (setq same t g
(cdr g
))))
1565 ;; if `repeat' has not been set, move to
1566 ;; the next line; don't bother searching
1567 ;; anymore on this one
1568 (if (and (not repeat
) (not (bolp)))
1571 ;; when they are no more matches for this rule,
1572 ;; align whatever was left over
1574 (align-regions regions align-props rule func
)))
1576 (setq case-fold-search current-case-fold
)))))))
1577 (setq rules
(cdr rules
)
1578 rule-index
(1+ rule-index
)))
1581 (message "Aligning...done"))))
1587 (run-hooks 'align-load-hook
)
1589 ;;; align.el ends here