]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/ada-wisi-opentoken.el
1812a497285e8d55d0c54f3da1f5397c92856b31
[gnu-emacs-elpa] / packages / ada-mode / ada-wisi-opentoken.el
1 ;;; ada-wisi-opentoken.el --- An indentation function for ada-wisi that indents OpenToken
2 ;; grammar statements nicely.
3
4 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20
21
22 ;;; Commentary:
23
24 ;; This is an example of a user-added indentation rule.
25 ;;
26 ;; In ~/.emacs (or project-specific config):
27 ;; (require 'ada-wisi-opentoken)
28 ;;
29 ;; In each file that declares OpenToken grammars:
30 ;;
31 ;; Local Variables:
32 ;; ada-indent-opentoken: t
33 ;; End:
34
35 ;;; Code:
36
37 (require 'ada-mode)
38 (require 'wisi)
39
40 (defcustom ada-indent-opentoken nil
41 "If non-nil, apply `ada-wisi-opentoken' indentation rule."
42 :type 'boolean
43 :group 'ada-indentation
44 :safe 'booleanp)
45 (make-variable-buffer-local 'ada-indent-opentoken)
46
47 (defun ada-wisi-opentoken ()
48 "Return appropriate indentation (an integer column) for continuation lines in an OpenToken grammar statement."
49 ;; We don't do any checking to see if we actually are in an
50 ;; OpenToken grammar statement, since this rule should only be
51 ;; included in package specs that exist solely to define OpenToken
52 ;; grammar fragments.
53 (when ada-indent-opentoken
54 (save-excursion
55 (let ((token-text (nth 1 (wisi-backward-token))))
56 (cond
57 ((equal token-text "<=")
58 (back-to-indentation)
59 (+ (current-column) ada-indent-broken))
60
61 ((member token-text '("+" "&"))
62 (while (not (equal "<=" (nth 1 (wisi-backward-token)))))
63 (back-to-indentation)
64 (+ (current-column) ada-indent-broken))
65 )))))
66
67 (defun ada-wisi-opentoken-setup ()
68 (add-to-list 'wisi-indent-calculate-functions 'ada-wisi-opentoken))
69
70 ;; This must be after ada-wisi-setup on ada-mode-hook, because
71 ;; ada-wisi-setup resets wisi-indent-calculate-functions
72 (add-hook 'ada-mode-hook 'ada-wisi-opentoken-setup t)
73
74 (add-to-list 'ada-align-rules
75 '(ada-opentoken
76 (regexp . "[^=]\\(\\s-*\\)<=")
77 (valid . (lambda() (not (ada-in-comment-p))))
78 (modes . '(ada-mode))))
79
80 (provide 'ada-wisi-opentoken)
81 ;; end of file