annotate lisp/emacs-lisp/smie.el @ 111643:f167a2ce369f

faces.el (glyphless-char): Define value for `pc'.
author Eli Zaretskii <eliz@gnu.org>
date Sat, 20 Nov 2010 13:43:01 +0200
parents b72ff43b041f
children 3655cc4062e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1 ;;; smie.el --- Simple Minded Indentation Engine
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
2
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
4
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
6 ;; Keywords: languages, lisp, internal, parsing, indentation
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
7
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
9
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
12 ;; the Free Software Foundation, either version 3 of the License, or
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
13 ;; (at your option) any later version.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
14
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
18 ;; GNU General Public License for more details.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
19
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
22
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
23 ;;; Commentary:
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
24
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
25 ;; While working on the SML indentation code, the idea grew that maybe
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
26 ;; I could write something generic to do the same thing, and at the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
27 ;; end of working on the SML code, I had a pretty good idea of what it
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
28 ;; could look like. That idea grew stronger after working on
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
29 ;; LaTeX indentation.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
30 ;;
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
31 ;; So at some point I decided to try it out, by writing a new
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
32 ;; indentation code for Coq while trying to keep most of the code
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
33 ;; "table driven", where only the tables are Coq-specific. The result
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
34 ;; (which was used for Beluga-mode as well) turned out to be based on
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
35 ;; something pretty close to an operator precedence parser.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
36
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
37 ;; So here is another rewrite, this time following the actual principles of
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
38 ;; operator precedence grammars. Why OPG? Even though they're among the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
39 ;; weakest kinds of parsers, these parsers have some very desirable properties
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
40 ;; for Emacs:
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
41 ;; - most importantly for indentation, they work equally well in either
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
42 ;; direction, so you can use them to parse backward from the indentation
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
43 ;; point to learn the syntactic context;
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
44 ;; - they work locally, so there's no need to keep a cache of
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
45 ;; the parser's state;
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
46 ;; - because of that locality, indentation also works just fine when earlier
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
47 ;; parts of the buffer are syntactically incorrect since the indentation
108912
c7e85ff4bca6 * lisp/emacs-lisp/smie.el (smie-indent-keyword): Remove special case that
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108897
diff changeset
48 ;; looks at "as little as possible" of the buffer to make an indentation
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
49 ;; decision.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
50 ;; - they typically have no error handling and can't even detect a parsing
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
51 ;; error, so we don't have to worry about what to do in case of a syntax
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
52 ;; error because the parser just automatically does something. Better yet,
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
53 ;; we can afford to use a sloppy grammar.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
54
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
55 ;; A good background to understand the development (especially the parts
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
56 ;; building the 2D precedence tables and then computing the precedence levels
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
57 ;; from it) can be found in pages 187-194 of "Parsing techniques" by Dick Grune
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
58 ;; and Ceriel Jacobs (BookBody.pdf available at
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
59 ;; http://www.cs.vu.nl/~dick/PTAPG.html).
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
60 ;;
108912
c7e85ff4bca6 * lisp/emacs-lisp/smie.el (smie-indent-keyword): Remove special case that
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108897
diff changeset
61 ;; OTOH we had to kill many chickens, read many coffee grounds, and practice
c7e85ff4bca6 * lisp/emacs-lisp/smie.el (smie-indent-keyword): Remove special case that
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108897
diff changeset
62 ;; untold numbers of black magic spells, to come up with the indentation code.
c7e85ff4bca6 * lisp/emacs-lisp/smie.el (smie-indent-keyword): Remove special case that
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108897
diff changeset
63 ;; Since then, some of that code has been beaten into submission, but the
c7e85ff4bca6 * lisp/emacs-lisp/smie.el (smie-indent-keyword): Remove special case that
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108897
diff changeset
64 ;; smie-indent-keyword is still pretty obscure.
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
65
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
66 ;;; Code:
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
67
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
68 ;; FIXME: I think the behavior on empty lines is wrong. It shouldn't
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
69 ;; look at the next token on subsequent lines.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
70
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
71 (eval-when-compile (require 'cl))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
72
111304
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
73 (defgroup smie nil
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
74 "Simple Minded Indentation Engine."
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
75 :group 'languages)
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
76
108878
c3cbf94d10f1 * emacs-lisp/smie.el (comment-continue): Declare for byte-compiler.
Juanma Barranquero <lekktu@gmail.com>
parents: 108865
diff changeset
77 (defvar comment-continue)
108923
5173ad363d4b * emacs-lisp/smie.el (comment-string-strip): Declare function.
Juanma Barranquero <lekktu@gmail.com>
parents: 108912
diff changeset
78 (declare-function comment-string-strip "newcomment" (str beforep afterp))
108878
c3cbf94d10f1 * emacs-lisp/smie.el (comment-continue): Declare for byte-compiler.
Juanma Barranquero <lekktu@gmail.com>
parents: 108865
diff changeset
79
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
80 ;;; Building precedence level tables from BNF specs.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
81
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
82 ;; We have 4 different representations of a "grammar":
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
83 ;; - a BNF table, which is a list of BNF rules of the form
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
84 ;; (NONTERM RHS1 ... RHSn) where each RHS is a list of terminals (tokens)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
85 ;; or nonterminals. Any element in these lists which does not appear as
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
86 ;; the `car' of a BNF rule is taken to be a terminal.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
87 ;; - A list of precedences (key word "precs"), is a list, sorted
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
88 ;; from lowest to highest precedence, of precedence classes that
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
89 ;; have the form (ASSOCIATIVITY TERMINAL1 .. TERMINALn), where
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
90 ;; ASSOCIATIVITY can be `assoc', `left', `right' or `nonassoc'.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
91 ;; - a 2 dimensional precedence table (key word "prec2"), is a 2D
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
92 ;; table recording the precedence relation (can be `<', `=', `>', or
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
93 ;; nil) between each pair of tokens.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
94 ;; - a precedence-level table (key word "grammar"), which is a alist
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
95 ;; giving for each token its left and right precedence level (a
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
96 ;; number or nil). This is used in `smie-grammar'.
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
97 ;; The prec2 tables are only intermediate data structures: the source
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
98 ;; code normally provides a mix of BNF and precs tables, and then
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
99 ;; turns them into a levels table, which is what's used by the rest of
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
100 ;; the SMIE code.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
101
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
102 (defun smie-set-prec2tab (table x y val &optional override)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
103 (assert (and x y))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
104 (let* ((key (cons x y))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
105 (old (gethash key table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
106 (if (and old (not (eq old val)))
108766
2f547b05b620 * emacs-lisp/smie.el (smie-set-prec2tab): Check override before use.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108652
diff changeset
107 (if (and override (gethash key override))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
108 ;; FIXME: The override is meant to resolve ambiguities,
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
109 ;; but it also hides real conflicts. It would be great to
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
110 ;; be able to distinguish the two cases so that overrides
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
111 ;; don't hide real conflicts.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
112 (puthash key (gethash key override) table)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
113 (display-warning 'smie (format "Conflict: %s %s/%s %s" x old val y)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
114 (puthash key val table))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
115
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
116 (put 'smie-precs->prec2 'pure t)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
117 (defun smie-precs->prec2 (precs)
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
118 "Compute a 2D precedence table from a list of precedences.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
119 PRECS should be a list, sorted by precedence (e.g. \"+\" will
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
120 come before \"*\"), of elements of the form \(left OP ...)
108923
5173ad363d4b * emacs-lisp/smie.el (comment-string-strip): Declare function.
Juanma Barranquero <lekktu@gmail.com>
parents: 108912
diff changeset
121 or (right OP ...) or (nonassoc OP ...) or (assoc OP ...). All operators in
108642
66f3c521df14 * emacs-lisp/smie.el: Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 108632
diff changeset
122 one of those elements share the same precedence level and associativity."
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
123 (let ((prec2-table (make-hash-table :test 'equal)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
124 (dolist (prec precs)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
125 (dolist (op (cdr prec))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
126 (let ((selfrule (cdr (assq (car prec)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
127 '((left . >) (right . <) (assoc . =))))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
128 (when selfrule
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
129 (dolist (other-op (cdr prec))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
130 (smie-set-prec2tab prec2-table op other-op selfrule))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
131 (let ((op1 '<) (op2 '>))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
132 (dolist (other-prec precs)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
133 (if (eq prec other-prec)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
134 (setq op1 '> op2 '<)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
135 (dolist (other-op (cdr other-prec))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
136 (smie-set-prec2tab prec2-table op other-op op2)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
137 (smie-set-prec2tab prec2-table other-op op op1)))))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
138 prec2-table))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
139
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
140 (put 'smie-merge-prec2s 'pure t)
108766
2f547b05b620 * emacs-lisp/smie.el (smie-set-prec2tab): Check override before use.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108652
diff changeset
141 (defun smie-merge-prec2s (&rest tables)
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
142 (if (null (cdr tables))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
143 (car tables)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
144 (let ((prec2 (make-hash-table :test 'equal)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
145 (dolist (table tables)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
146 (maphash (lambda (k v)
110802
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
147 (if (consp k)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
148 (smie-set-prec2tab prec2 (car k) (cdr k) v)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
149 (if (and (gethash k prec2)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
150 (not (equal (gethash k prec2) v)))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
151 (error "Conflicting values for %s property" k)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
152 (puthash k v prec2))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
153 table))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
154 prec2)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
155
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
156 (put 'smie-bnf->prec2 'pure t)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
157 (defun smie-bnf->prec2 (bnf &rest precs)
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
158 (let ((nts (mapcar 'car bnf)) ;Non-terminals
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
159 (first-ops-table ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
160 (last-ops-table ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
161 (first-nts-table ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
162 (last-nts-table ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
163 (prec2 (make-hash-table :test 'equal))
108766
2f547b05b620 * emacs-lisp/smie.el (smie-set-prec2tab): Check override before use.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108652
diff changeset
164 (override (apply 'smie-merge-prec2s
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
165 (mapcar 'smie-precs->prec2 precs)))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
166 again)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
167 (dolist (rules bnf)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
168 (let ((nt (car rules))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
169 (last-ops ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
170 (first-ops ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
171 (last-nts ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
172 (first-nts ()))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
173 (dolist (rhs (cdr rules))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
174 (unless (consp rhs)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
175 (signal 'wrong-type-argument `(consp ,rhs)))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
176 (if (not (member (car rhs) nts))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
177 (pushnew (car rhs) first-ops)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
178 (pushnew (car rhs) first-nts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
179 (when (consp (cdr rhs))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
180 ;; If the first is not an OP we add the second (which
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
181 ;; should be an OP if BNF is an "operator grammar").
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
182 ;; Strictly speaking, this should only be done if the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
183 ;; first is a non-terminal which can expand to a phrase
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
184 ;; without any OP in it, but checking doesn't seem worth
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
185 ;; the trouble, and it lets the writer of the BNF
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
186 ;; be a bit more sloppy by skipping uninteresting base
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
187 ;; cases which are terminals but not OPs.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
188 (assert (not (member (cadr rhs) nts)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
189 (pushnew (cadr rhs) first-ops)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
190 (let ((shr (reverse rhs)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
191 (if (not (member (car shr) nts))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
192 (pushnew (car shr) last-ops)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
193 (pushnew (car shr) last-nts)
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
194 (when (consp (cdr shr))
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
195 (assert (not (member (cadr shr) nts)))
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
196 (pushnew (cadr shr) last-ops)))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
197 (push (cons nt first-ops) first-ops-table)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
198 (push (cons nt last-ops) last-ops-table)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
199 (push (cons nt first-nts) first-nts-table)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
200 (push (cons nt last-nts) last-nts-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
201 ;; Compute all first-ops by propagating the initial ones we have
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
202 ;; now, according to first-nts.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
203 (setq again t)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
204 (while (prog1 again (setq again nil))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
205 (dolist (first-nts first-nts-table)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
206 (let* ((nt (pop first-nts))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
207 (first-ops (assoc nt first-ops-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
208 (dolist (first-nt first-nts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
209 (dolist (op (cdr (assoc first-nt first-ops-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
210 (unless (member op first-ops)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
211 (setq again t)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
212 (push op (cdr first-ops))))))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
213 ;; Same thing for last-ops.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
214 (setq again t)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
215 (while (prog1 again (setq again nil))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
216 (dolist (last-nts last-nts-table)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
217 (let* ((nt (pop last-nts))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
218 (last-ops (assoc nt last-ops-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
219 (dolist (last-nt last-nts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
220 (dolist (op (cdr (assoc last-nt last-ops-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
221 (unless (member op last-ops)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
222 (setq again t)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
223 (push op (cdr last-ops))))))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
224 ;; Now generate the 2D precedence table.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
225 (dolist (rules bnf)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
226 (dolist (rhs (cdr rules))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
227 (while (cdr rhs)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
228 (cond
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
229 ((member (car rhs) nts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
230 (dolist (last (cdr (assoc (car rhs) last-ops-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
231 (smie-set-prec2tab prec2 last (cadr rhs) '> override)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
232 ((member (cadr rhs) nts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
233 (dolist (first (cdr (assoc (cadr rhs) first-ops-table)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
234 (smie-set-prec2tab prec2 (car rhs) first '< override))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
235 (if (and (cddr rhs) (not (member (car (cddr rhs)) nts)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
236 (smie-set-prec2tab prec2 (car rhs) (car (cddr rhs))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
237 '= override)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
238 (t (smie-set-prec2tab prec2 (car rhs) (cadr rhs) '= override)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
239 (setq rhs (cdr rhs)))))
110802
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
240 ;; Keep track of which tokens are openers/closer, so they can get a nil
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
241 ;; precedence in smie-prec2->grammar.
110802
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
242 (puthash :smie-open/close-alist (smie-bnf-classify bnf) prec2)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
243 (puthash :smie-closer-alist (smie-bnf-closer-alist bnf) prec2)
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
244 prec2))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
245
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
246 ;; (defun smie-prec2-closer-alist (prec2 include-inners)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
247 ;; "Build a closer-alist from a PREC2 table.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
248 ;; The return value is in the same form as `smie-closer-alist'.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
249 ;; INCLUDE-INNERS if non-nil means that inner keywords will be included
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
250 ;; in the table, e.g. the table will include things like (\"if\" . \"else\")."
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
251 ;; (let* ((non-openers '())
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
252 ;; (non-closers '())
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
253 ;; ;; For each keyword, this gives the matching openers, if any.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
254 ;; (openers (make-hash-table :test 'equal))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
255 ;; (closers '())
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
256 ;; (done nil))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
257 ;; ;; First, find the non-openers and non-closers.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
258 ;; (maphash (lambda (k v)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
259 ;; (unless (or (eq v '<) (member (cdr k) non-openers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
260 ;; (push (cdr k) non-openers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
261 ;; (unless (or (eq v '>) (member (car k) non-closers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
262 ;; (push (car k) non-closers)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
263 ;; prec2)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
264 ;; ;; Then find the openers and closers.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
265 ;; (maphash (lambda (k _)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
266 ;; (unless (member (car k) non-openers)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
267 ;; (puthash (car k) (list (car k)) openers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
268 ;; (unless (or (member (cdr k) non-closers)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
269 ;; (member (cdr k) closers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
270 ;; (push (cdr k) closers)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
271 ;; prec2)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
272 ;; ;; Then collect the matching elements.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
273 ;; (while (not done)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
274 ;; (setq done t)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
275 ;; (maphash (lambda (k v)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
276 ;; (when (eq v '=)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
277 ;; (let ((aopeners (gethash (car k) openers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
278 ;; (dopeners (gethash (cdr k) openers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
279 ;; (new nil))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
280 ;; (dolist (o aopeners)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
281 ;; (unless (member o dopeners)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
282 ;; (setq new t)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
283 ;; (push o dopeners)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
284 ;; (when new
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
285 ;; (setq done nil)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
286 ;; (puthash (cdr k) dopeners openers)))))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
287 ;; prec2))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
288 ;; ;; Finally, dump the resulting table.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
289 ;; (let ((alist '()))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
290 ;; (maphash (lambda (k v)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
291 ;; (when (or include-inners (member k closers))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
292 ;; (dolist (opener v)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
293 ;; (unless (equal opener k)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
294 ;; (push (cons opener k) alist)))))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
295 ;; openers)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
296 ;; alist)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
297
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
298 (defun smie-bnf-closer-alist (bnf &optional no-inners)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
299 ;; We can also build this closer-alist table from a prec2 table,
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
300 ;; but it takes more work, and the order is unpredictable, which
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
301 ;; is a problem for smie-close-block.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
302 ;; More convenient would be to build it from a levels table since we
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
303 ;; always have this table (contrary to the BNF), but it has all the
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
304 ;; disadvantages of the prec2 case plus the disadvantage that the levels
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
305 ;; table has lost some info which would result in extra invalid pairs.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
306 "Build a closer-alist from a BNF table.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
307 The return value is in the same form as `smie-closer-alist'.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
308 NO-INNERS if non-nil means that inner keywords will be excluded
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
309 from the table, e.g. the table will not include things like (\"if\" . \"else\")."
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
310 (let ((nts (mapcar #'car bnf)) ;non terminals.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
311 (alist '()))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
312 (dolist (nt bnf)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
313 (dolist (rhs (cdr nt))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
314 (unless (or (< (length rhs) 2) (member (car rhs) nts))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
315 (if no-inners
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
316 (let ((last (car (last rhs))))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
317 (unless (member last nts)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
318 (pushnew (cons (car rhs) last) alist :test #'equal)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
319 ;; Reverse so that the "real" closer gets there first,
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
320 ;; which is important for smie-close-block.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
321 (dolist (term (reverse (cdr rhs)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
322 (unless (member term nts)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
323 (pushnew (cons (car rhs) term) alist :test #'equal)))))))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
324 (nreverse alist)))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
325
110802
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
326 (defun smie-bnf-classify (bnf)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
327 "Return a table classifying terminals.
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
328 Each terminal can either be an `opener', a `closer', or neither."
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
329 (let ((table (make-hash-table :test #'equal))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
330 (alist '()))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
331 (dolist (category bnf)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
332 (puthash (car category) 'neither table) ;Remove non-terminals.
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
333 (dolist (rhs (cdr category))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
334 (if (null (cdr rhs))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
335 (puthash (pop rhs) 'neither table)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
336 (let ((first (pop rhs)))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
337 (puthash first
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
338 (if (memq (gethash first table) '(nil opener))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
339 'opener 'neither)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
340 table))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
341 (while (cdr rhs)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
342 (puthash (pop rhs) 'neither table)) ;Remove internals.
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
343 (let ((last (pop rhs)))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
344 (puthash last
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
345 (if (memq (gethash last table) '(nil closer))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
346 'closer 'neither)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
347 table)))))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
348 (maphash (lambda (tok v)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
349 (when (memq v '(closer opener))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
350 (push (cons tok v) alist)))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
351 table)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
352 alist))
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
353
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
354 (defun smie-debug--prec2-cycle (csts)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
355 "Return a cycle in CSTS, assuming there's one.
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
356 CSTS is a list of pairs representing arcs in a graph."
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
357 ;; A PATH is of the form (START . REST) where REST is a reverse
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
358 ;; list of nodes through which the path goes.
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
359 (let ((paths (mapcar (lambda (pair) (list (car pair) (cdr pair))) csts))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
360 (cycle nil))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
361 (while (null cycle)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
362 (dolist (path (prog1 paths (setq paths nil)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
363 (dolist (cst csts)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
364 (when (eq (car cst) (nth 1 path))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
365 (if (eq (cdr cst) (car path))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
366 (setq cycle path)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
367 (push (cons (car path) (cons (cdr cst) (cdr path)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
368 paths))))))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
369 (cons (car cycle) (nreverse (cdr cycle)))))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
370
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
371 (defun smie-debug--describe-cycle (table cycle)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
372 (let ((names
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
373 (mapcar (lambda (val)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
374 (let ((res nil))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
375 (dolist (elem table)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
376 (if (eq (cdr elem) val)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
377 (push (concat "." (car elem)) res))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
378 (if (eq (cddr elem) val)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
379 (push (concat (car elem) ".") res)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
380 (assert res)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
381 res))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
382 cycle)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
383 (mapconcat
110464
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
384 (lambda (elems) (mapconcat 'identity elems "="))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
385 (append names (list (car names)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
386 " < ")))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
387
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
388 (put 'smie-prec2->grammar 'pure t)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
389 (defun smie-prec2->grammar (prec2)
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
390 "Take a 2D precedence table and turn it into an alist of precedence levels.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
391 PREC2 is a table as returned by `smie-precs->prec2' or
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
392 `smie-bnf->prec2'."
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
393 ;; For each operator, we create two "variables" (corresponding to
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
394 ;; the left and right precedence level), which are represented by
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
395 ;; cons cells. Those are the very cons cells that appear in the
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
396 ;; final `table'. The value of each "variable" is kept in the `car'.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
397 (let ((table ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
398 (csts ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
399 (eqs ())
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
400 tmp x y)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
401 ;; From `prec2' we construct a list of constraints between
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
402 ;; variables (aka "precedence levels"). These can be either
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
403 ;; equality constraints (in `eqs') or `<' constraints (in `csts').
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
404 (maphash (lambda (k v)
110802
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
405 (when (consp k)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
406 (if (setq tmp (assoc (car k) table))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
407 (setq x (cddr tmp))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
408 (setq x (cons nil nil))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
409 (push (cons (car k) (cons nil x)) table))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
410 (if (setq tmp (assoc (cdr k) table))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
411 (setq y (cdr tmp))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
412 (setq y (cons nil (cons nil nil)))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
413 (push (cons (cdr k) y) table))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
414 (ecase v
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
415 (= (push (cons x y) eqs))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
416 (< (push (cons x y) csts))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
417 (> (push (cons y x) csts)))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
418 prec2)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
419 ;; First process the equality constraints.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
420 (let ((eqs eqs))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
421 (while eqs
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
422 (let ((from (caar eqs))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
423 (to (cdar eqs)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
424 (setq eqs (cdr eqs))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
425 (if (eq to from)
108766
2f547b05b620 * emacs-lisp/smie.el (smie-set-prec2tab): Check override before use.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108652
diff changeset
426 nil ;Nothing to do.
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
427 (dolist (other-eq eqs)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
428 (if (eq from (cdr other-eq)) (setcdr other-eq to))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
429 (when (eq from (car other-eq))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
430 ;; This can happen because of `assoc' settings in precs
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
431 ;; or because of a rhs like ("op" foo "op").
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
432 (setcar other-eq to)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
433 (dolist (cst csts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
434 (if (eq from (cdr cst)) (setcdr cst to))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
435 (if (eq from (car cst)) (setcar cst to)))))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
436 ;; Then eliminate trivial constraints iteratively.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
437 (let ((i 0))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
438 (while csts
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
439 (let ((rhvs (mapcar 'cdr csts))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
440 (progress nil))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
441 (dolist (cst csts)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
442 (unless (memq (car cst) rhvs)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
443 (setq progress t)
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
444 ;; We could give each var in a given iteration the same value,
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
445 ;; but we can also give them arbitrarily different values.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
446 ;; Basically, these are vars between which there is no
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
447 ;; constraint (neither equality nor inequality), so
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
448 ;; anything will do.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
449 ;; We give them arbitrary values, which means that we
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
450 ;; replace the "no constraint" case with either > or <
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
451 ;; but not =. The reason we do that is so as to try and
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
452 ;; distinguish associative operators (which will have
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
453 ;; left = right).
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
454 (unless (caar cst)
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
455 (setcar (car cst) i)
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
456 (incf i))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
457 (setq csts (delq cst csts))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
458 (unless progress
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
459 (error "Can't resolve the precedence cycle: %s"
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
460 (smie-debug--describe-cycle
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
461 table (smie-debug--prec2-cycle csts)))))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
462 (incf i 10))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
463 ;; Propagate equalities back to their source.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
464 (dolist (eq (nreverse eqs))
108766
2f547b05b620 * emacs-lisp/smie.el (smie-set-prec2tab): Check override before use.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108652
diff changeset
465 (assert (or (null (caar eq)) (eq (car eq) (cdr eq))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
466 (setcar (car eq) (cadr eq)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
467 ;; Finally, fill in the remaining vars (which only appeared on the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
468 ;; right side of the < constraints).
110802
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
469 (let ((classification-table (gethash :smie-open/close-alist prec2)))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
470 (dolist (x table)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
471 ;; When both sides are nil, it means this operator binds very
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
472 ;; very tight, but it's still just an operator, so we give it
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
473 ;; the highest precedence.
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
474 ;; OTOH if only one side is nil, it usually means it's like an
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
475 ;; open-paren, which is very important for indentation purposes,
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
476 ;; so we keep it nil if so, to make it easier to recognize.
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
477 (unless (or (nth 1 x)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
478 (eq 'opener (cdr (assoc (car x) classification-table))))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
479 (setf (nth 1 x) i)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
480 (incf i)) ;See other (incf i) above.
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
481 (unless (or (nth 2 x)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
482 (eq 'closer (cdr (assoc (car x) classification-table))))
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
483 (setf (nth 2 x) i)
e544f6cc2447 SMIE: Reliably distinguish openers/closers in smie-prec2-levels
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110464
diff changeset
484 (incf i))))) ;See other (incf i) above.
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
485 (let ((ca (gethash :smie-closer-alist prec2)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
486 (when ca (push (cons :smie-closer-alist ca) table)))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
487 table))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
488
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
489 ;;; Parsing using a precedence level table.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
490
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
491 (defvar smie-grammar 'unset
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
492 "List of token parsing info.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
493 This list is normally built by `smie-prec2->grammar'.
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
494 Each element is of the form (TOKEN LEFT-LEVEL RIGHT-LEVEL).
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
495 Parsing is done using an operator precedence parser.
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
496 LEFT-LEVEL and RIGHT-LEVEL can be either numbers or nil, where nil
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
497 means that this operator does not bind on the corresponding side,
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
498 i.e. a LEFT-LEVEL of nil means this is a token that behaves somewhat like
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
499 an open-paren, whereas a RIGHT-LEVEL of nil would correspond to something
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
500 like a close-paren.")
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
501
108797
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
502 (defvar smie-forward-token-function 'smie-default-forward-token
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
503 "Function to scan forward for the next token.
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
504 Called with no argument should return a token and move to its end.
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
505 If no token is found, return nil or the empty string.
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
506 It can return nil when bumping into a parenthesis, which lets SMIE
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
507 use syntax-tables to handle them in efficient C code.")
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
508
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
509 (defvar smie-backward-token-function 'smie-default-backward-token
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
510 "Function to scan backward the previous token.
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
511 Same calling convention as `smie-forward-token-function' except
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
512 it should move backward to the beginning of the previous token.")
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
513
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
514 (defalias 'smie-op-left 'car)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
515 (defalias 'smie-op-right 'cadr)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
516
108797
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
517 (defun smie-default-backward-token ()
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
518 (forward-comment (- (point)))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
519 (buffer-substring-no-properties
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
520 (point)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
521 (progn (if (zerop (skip-syntax-backward "."))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
522 (skip-syntax-backward "w_'"))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
523 (point))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
524
108797
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
525 (defun smie-default-forward-token ()
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
526 (forward-comment (point-max))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
527 (buffer-substring-no-properties
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
528 (point)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
529 (progn (if (zerop (skip-syntax-forward "."))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
530 (skip-syntax-forward "w_'"))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
531 (point))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
532
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
533 (defun smie--associative-p (toklevels)
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
534 ;; in "a + b + c" we want to stop at each +, but in
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
535 ;; "if a then b elsif c then d else c" we don't want to stop at each keyword.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
536 ;; To distinguish the two cases, we made smie-prec2->grammar choose
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
537 ;; different levels for each part of "if a then b else c", so that
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
538 ;; by checking if the left-level is equal to the right level, we can
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
539 ;; figure out that it's an associative operator.
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
540 ;; This is not 100% foolproof, tho, since the "elsif" will have to have
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
541 ;; equal left and right levels (since it's optional), so smie-next-sexp
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
542 ;; has to be careful to distinguish those different cases.
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
543 (eq (smie-op-left toklevels) (smie-op-right toklevels)))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
544
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
545 (defun smie-next-sexp (next-token next-sexp op-forw op-back halfsexp)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
546 "Skip over one sexp.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
547 NEXT-TOKEN is a function of no argument that moves forward by one
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
548 token (after skipping comments if needed) and returns it.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
549 NEXT-SEXP is a lower-level function to skip one sexp.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
550 OP-FORW is the accessor to the forward level of the level data.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
551 OP-BACK is the accessor to the backward level of the level data.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
552 HALFSEXP if non-nil, means skip over a partial sexp if needed. I.e. if the
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
553 first token we see is an operator, skip over its left-hand-side argument.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
554 Possible return values:
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
555 (FORW-LEVEL POS TOKEN): we couldn't skip TOKEN because its back-level
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
556 is too high. FORW-LEVEL is the forw-level of TOKEN,
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
557 POS is its start position in the buffer.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
558 (t POS TOKEN): same thing when we bump on the wrong side of a paren.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
559 (nil POS TOKEN): we skipped over a paren-like pair.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
560 nil: we skipped over an identifier, matched parentheses, ..."
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
561 (catch 'return
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
562 (let ((levels ()))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
563 (while
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
564 (let* ((pos (point))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
565 (token (funcall next-token))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
566 (toklevels (cdr (assoc token smie-grammar))))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
567 (cond
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
568 ((null toklevels)
108797
d674108e8753 Provide hooks to use a different tokenizer in SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108766
diff changeset
569 (when (zerop (length token))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
570 (condition-case err
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
571 (progn (goto-char pos) (funcall next-sexp 1) nil)
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
572 (scan-error (throw 'return
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
573 (list t (caddr err)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
574 (buffer-substring-no-properties
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
575 (caddr err)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
576 (+ (caddr err)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
577 (if (< (point) (caddr err))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
578 -1 1)))))))
108652
79ce86edba9f * emacs-lisp/smie.el (smie-next-sexp): Break inf-loop at BOB.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108649
diff changeset
579 (if (eq pos (point))
79ce86edba9f * emacs-lisp/smie.el (smie-next-sexp): Break inf-loop at BOB.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108649
diff changeset
580 ;; We did not move, so let's abort the loop.
79ce86edba9f * emacs-lisp/smie.el (smie-next-sexp): Break inf-loop at BOB.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108649
diff changeset
581 (throw 'return (list t (point))))))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
582 ((null (funcall op-back toklevels))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
583 ;; A token like a paren-close.
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
584 (assert (funcall op-forw toklevels)) ;Otherwise, why mention it?
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
585 (push toklevels levels))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
586 (t
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
587 (while (and levels (< (funcall op-back toklevels)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
588 (funcall op-forw (car levels))))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
589 (setq levels (cdr levels)))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
590 (cond
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
591 ((null levels)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
592 (if (and halfsexp (funcall op-forw toklevels))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
593 (push toklevels levels)
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
594 (throw 'return
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
595 (prog1 (list (or (car toklevels) t) (point) token)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
596 (goto-char pos)))))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
597 (t
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
598 (let ((lastlevels levels))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
599 (if (and levels (= (funcall op-back toklevels)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
600 (funcall op-forw (car levels))))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
601 (setq levels (cdr levels)))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
602 ;; We may have found a match for the previously pending
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
603 ;; operator. Is this the end?
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
604 (cond
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
605 ;; Keep looking as long as we haven't matched the
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
606 ;; topmost operator.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
607 (levels
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
608 (if (funcall op-forw toklevels)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
609 (push toklevels levels)))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
610 ;; We matched the topmost operator. If the new operator
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
611 ;; is the last in the corresponding BNF rule, we're done.
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
612 ((null (funcall op-forw toklevels))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
613 ;; It is the last element, let's stop here.
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
614 (throw 'return (list nil (point) token)))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
615 ;; If the new operator is not the last in the BNF rule,
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
616 ;; ans is not associative, it's one of the inner operators
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
617 ;; (like the "in" in "let .. in .. end"), so keep looking.
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
618 ((not (smie--associative-p toklevels))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
619 (push toklevels levels))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
620 ;; The new operator is associative. Two cases:
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
621 ;; - it's really just an associative operator (like + or ;)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
622 ;; in which case we should have stopped right before.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
623 ((and lastlevels
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
624 (smie--associative-p (car lastlevels)))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
625 (throw 'return
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
626 (prog1 (list (or (car toklevels) t) (point) token)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
627 (goto-char pos))))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
628 ;; - it's an associative operator within a larger construct
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
629 ;; (e.g. an "elsif"), so we should just ignore it and keep
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
630 ;; looking for the closing element.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
631 (t (setq levels lastlevels))))))))
108649
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
632 levels)
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
633 (setq halfsexp nil)))))
c9e786f54683 Fix handling of non-associative equal levels.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108642
diff changeset
634
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
635 (defun smie-backward-sexp (&optional halfsexp)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
636 "Skip over one sexp.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
637 HALFSEXP if non-nil, means skip over a partial sexp if needed. I.e. if the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
638 first token we see is an operator, skip over its left-hand-side argument.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
639 Possible return values:
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
640 (LEFT-LEVEL POS TOKEN): we couldn't skip TOKEN because its right-level
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
641 is too high. LEFT-LEVEL is the left-level of TOKEN,
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
642 POS is its start position in the buffer.
108642
66f3c521df14 * emacs-lisp/smie.el: Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 108632
diff changeset
643 (t POS TOKEN): same thing but for an open-paren or the beginning of buffer.
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
644 (nil POS TOKEN): we skipped over a paren-like pair.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
645 nil: we skipped over an identifier, matched parentheses, ..."
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
646 (smie-next-sexp
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
647 (indirect-function smie-backward-token-function)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
648 (indirect-function 'backward-sexp)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
649 (indirect-function 'smie-op-left)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
650 (indirect-function 'smie-op-right)
108652
79ce86edba9f * emacs-lisp/smie.el (smie-next-sexp): Break inf-loop at BOB.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108649
diff changeset
651 halfsexp))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
652
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
653 (defun smie-forward-sexp (&optional halfsexp)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
654 "Skip over one sexp.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
655 HALFSEXP if non-nil, means skip over a partial sexp if needed. I.e. if the
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
656 first token we see is an operator, skip over its left-hand-side argument.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
657 Possible return values:
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
658 (RIGHT-LEVEL POS TOKEN): we couldn't skip TOKEN because its left-level
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
659 is too high. RIGHT-LEVEL is the right-level of TOKEN,
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
660 POS is its end position in the buffer.
108642
66f3c521df14 * emacs-lisp/smie.el: Fix typos in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 108632
diff changeset
661 (t POS TOKEN): same thing but for an open-paren or the beginning of buffer.
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
662 (nil POS TOKEN): we skipped over a paren-like pair.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
663 nil: we skipped over an identifier, matched parentheses, ..."
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
664 (smie-next-sexp
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
665 (indirect-function smie-forward-token-function)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
666 (indirect-function 'forward-sexp)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
667 (indirect-function 'smie-op-right)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
668 (indirect-function 'smie-op-left)
108652
79ce86edba9f * emacs-lisp/smie.el (smie-next-sexp): Break inf-loop at BOB.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108649
diff changeset
669 halfsexp))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
670
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
671 ;;; Miscellanous commands using the precedence parser.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
672
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
673 (defun smie-backward-sexp-command (&optional n)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
674 "Move backward through N logical elements."
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
675 (interactive "^p")
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
676 (smie-forward-sexp-command (- n)))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
677
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
678 (defun smie-forward-sexp-command (&optional n)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
679 "Move forward through N logical elements."
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
680 (interactive "^p")
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
681 (let ((forw (> n 0))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
682 (forward-sexp-function nil))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
683 (while (/= n 0)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
684 (setq n (- n (if forw 1 -1)))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
685 (let ((pos (point))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
686 (res (if forw
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
687 (smie-forward-sexp 'halfsexp)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
688 (smie-backward-sexp 'halfsexp))))
109840
51f5fd9b5515 * smie.el (smie-forward-sexp-command): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109838
diff changeset
689 (if (and (car res) (= pos (point)) (not (if forw (eobp) (bobp))))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
690 (signal 'scan-error
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
691 (list "Containing expression ends prematurely"
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
692 (cadr res) (cadr res)))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
693 nil)))))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
694
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
695 (defvar smie-closer-alist nil
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
696 "Alist giving the closer corresponding to an opener.")
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
697
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
698 (defun smie-close-block ()
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
699 "Close the closest surrounding block."
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
700 (interactive)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
701 (let ((closer
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
702 (save-excursion
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
703 (backward-up-list 1)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
704 (if (looking-at "\\s(")
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
705 (string (cdr (syntax-after (point))))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
706 (let* ((open (funcall smie-forward-token-function))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
707 (closer (cdr (assoc open smie-closer-alist)))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
708 (levels (list (assoc open smie-grammar)))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
709 (seen '())
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
710 (found '()))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
711 (cond
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
712 ;; Even if we improve the auto-computation of closers,
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
713 ;; there are still cases where we need manual
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
714 ;; intervention, e.g. for Octave's use of `until'
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
715 ;; as a pseudo-closer of `do'.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
716 (closer)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
717 ((or (equal levels '(nil)) (nth 1 (car levels)))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
718 (error "Doesn't look like a block"))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
719 (t
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
720 ;; Now that smie-setup automatically sets smie-closer-alist
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
721 ;; from the BNF, this is not really needed any more.
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
722 (while levels
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
723 (let ((level (pop levels)))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
724 (dolist (other smie-grammar)
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
725 (when (and (eq (nth 2 level) (nth 1 other))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
726 (not (memq other seen)))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
727 (push other seen)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
728 (if (nth 2 other)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
729 (push other levels)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
730 (push (car other) found))))))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
731 (cond
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
732 ((null found) (error "No known closer for opener %s" open))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
733 ;; FIXME: what should we do if there are various closers?
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
734 (t (car found))))))))))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
735 (unless (save-excursion (skip-chars-backward " \t") (bolp))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
736 (newline))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
737 (insert closer)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
738 (if (save-excursion (skip-chars-forward " \t") (eolp))
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
739 (indent-according-to-mode)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
740 (reindent-then-newline-and-indent))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
741
110067
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
742 (defun smie-down-list (&optional arg)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
743 "Move forward down one level paren-like blocks. Like `down-list'.
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
744 With argument ARG, do this that many times.
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
745 A negative argument means move backward but still go down a level.
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
746 This command assumes point is not in a string or comment."
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
747 (interactive "p")
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
748 (let ((start (point))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
749 (inc (if (< arg 0) -1 1))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
750 (offset (if (< arg 0) 1 0))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
751 (next-token (if (< arg 0)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
752 smie-backward-token-function
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
753 smie-forward-token-function)))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
754 (while (/= arg 0)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
755 (setq arg (- arg inc))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
756 (while
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
757 (let* ((pos (point))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
758 (token (funcall next-token))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
759 (levels (assoc token smie-grammar)))
110067
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
760 (cond
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
761 ((zerop (length token))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
762 (if (if (< inc 0) (looking-back "\\s(\\|\\s)" (1- (point)))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
763 (looking-at "\\s(\\|\\s)"))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
764 ;; Go back to `start' in case of an error. This presumes
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
765 ;; none of the token we've found until now include a ( or ).
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
766 (progn (goto-char start) (down-list inc) nil)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
767 (forward-sexp inc)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
768 (/= (point) pos)))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
769 ((and levels (null (nth (+ 1 offset) levels))) nil)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
770 ((and levels (null (nth (- 2 offset) levels)))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
771 (let ((end (point)))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
772 (goto-char start)
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
773 (signal 'scan-error
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
774 (list "Containing expression ends prematurely"
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
775 pos end))))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
776 (t)))))))
5cab4c4229ff * lisp/emacs-lisp/smie.el (smie-down-list): New command.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110036
diff changeset
777
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
778 (defvar smie-blink-matching-triggers '(?\s ?\n)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
779 "Chars which might trigger `blink-matching-open'.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
780 These can include the final chars of end-tokens, or chars that are
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
781 typically inserted right after an end token.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
782 I.e. a good choice can be:
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
783 (delete-dups
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
784 (mapcar (lambda (kw) (aref (cdr kw) (1- (length (cdr kw)))))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
785 smie-closer-alist))")
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
786
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
787 (defcustom smie-blink-matching-inners t
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
788 "Whether SMIE should blink to matching opener for inner keywords.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
789 If non-nil, it will blink not only for \"begin..end\" but also for \"if...else\"."
111304
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
790 :type 'boolean
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
791 :group 'smie)
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
792
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
793 (defun smie-blink-matching-check (start end)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
794 (save-excursion
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
795 (goto-char end)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
796 (let ((ender (funcall smie-backward-token-function)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
797 (cond
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
798 ((not (and ender (rassoc ender smie-closer-alist)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
799 ;; This not is one of the begin..end we know how to check.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
800 (blink-matching-check-mismatch start end))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
801 ((not start) t)
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
802 ((eq t (car (rassoc ender smie-closer-alist))) nil)
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
803 (t
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
804 (goto-char start)
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
805 (let ((starter (funcall smie-forward-token-function)))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
806 (not (member (cons starter ender) smie-closer-alist))))))))
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
807
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
808 (defun smie-blink-matching-open ()
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
809 "Blink the matching opener when applicable.
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
810 This uses SMIE's tables and is expected to be placed on `post-self-insert-hook'."
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
811 (let ((pos (point)) ;Position after the close token.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
812 token)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
813 (when (and blink-matching-paren
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
814 smie-closer-alist ; Optimization.
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
815 (or (eq (char-before) last-command-event) ;; Sanity check.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
816 (save-excursion
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
817 (or (progn (skip-chars-backward " \t")
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
818 (setq pos (point))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
819 (eq (char-before) last-command-event))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
820 (progn (skip-chars-backward " \n\t")
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
821 (setq pos (point))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
822 (eq (char-before) last-command-event)))))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
823 (memq last-command-event smie-blink-matching-triggers)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
824 (not (nth 8 (syntax-ppss))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
825 (save-excursion
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
826 (setq token (funcall smie-backward-token-function))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
827 (when (and (eq (point) (1- pos))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
828 (= 1 (length token))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
829 (not (rassoc token smie-closer-alist)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
830 ;; The trigger char is itself a token but is not one of the
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
831 ;; closers (e.g. ?\; in Octave mode), so go back to the
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
832 ;; previous token.
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
833 (setq pos (point))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
834 (setq token (funcall smie-backward-token-function)))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
835 (when (rassoc token smie-closer-alist)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
836 ;; We're after a close token. Let's still make sure we
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
837 ;; didn't skip a comment to find that token.
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
838 (funcall smie-forward-token-function)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
839 (when (and (save-excursion
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
840 ;; Skip the trigger char, if applicable.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
841 (if (eq (char-after) last-command-event)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
842 (forward-char 1))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
843 (if (eq ?\n last-command-event)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
844 ;; Skip any auto-indentation, if applicable.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
845 (skip-chars-forward " \t"))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
846 (>= (point) pos))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
847 ;; If token ends with a trigger char, don't blink for
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
848 ;; anything else than this trigger char, lest we'd blink
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
849 ;; both when inserting the trigger char and when
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
850 ;; inserting a subsequent trigger char like SPC.
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
851 (or (eq (point) pos)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
852 (not (memq (char-before)
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
853 smie-blink-matching-triggers)))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
854 (or smie-blink-matching-inners
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
855 (null (nth 2 (assoc token smie-grammar)))))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
856 ;; The major mode might set blink-matching-check-function
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
857 ;; buffer-locally so that interactive calls to
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
858 ;; blink-matching-open work right, but let's not presume
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
859 ;; that's the case.
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
860 (let ((blink-matching-check-function #'smie-blink-matching-check))
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
861 (blink-matching-open))))))))
110161
eb977e012180 Provide blink-matching support to SMIE.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110067
diff changeset
862
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
863 ;;; The indentation engine.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
864
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
865 (defcustom smie-indent-basic 4
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
866 "Basic amount of indentation."
111304
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
867 :type 'integer
0c36e585b866 * lisp/emacs-lisp/smie.el (smie): New custom group.
Glenn Morris <rgm@gnu.org>
parents: 111232
diff changeset
868 :group 'smie)
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
869
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
870 (defvar smie-rules-function 'ignore
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
871 "Function providing the indentation rules.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
872 It takes two arguments METHOD and ARG where the meaning of ARG
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
873 and the expected return value depends on METHOD.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
874 METHOD can be:
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
875 - :after, in which case ARG is a token and the function should return the
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
876 OFFSET to use for indentation after ARG.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
877 - :before, in which case ARG is a token and the function should return the
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
878 OFFSET to use to indent ARG itself.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
879 - :elem, in which case the function should return either:
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
880 - the offset to use to indent function arguments (ARG = `arg')
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
881 - the basic indentation step (ARG = `basic').
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
882 - :list-intro, in which case ARG is a token and the function should return
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
883 non-nil if TOKEN is followed by a list of expressions (not separated by any
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
884 token) rather than an expression.
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
885
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
886 When ARG is a token, the function is called with point just before that token.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
887 A return value of nil always means to fallback on the default behavior, so the
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
888 function should return nil for arguments it does not expect.
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
889
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
890 OFFSET can be:
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
891 nil use the default indentation rule.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
892 `(column . COLUMN) indent to column COLUMN.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
893 NUMBER offset by NUMBER, relative to a base token
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
894 which is the current token for :after and
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
895 its parent for :before.
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
896
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
897 The functions whose name starts with \"smie-rule-\" are helper functions
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
898 designed specifically for use in this function.")
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
899
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
900 (defalias 'smie-rule-hanging-p 'smie-indent--hanging-p)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
901 (defun smie-indent--hanging-p ()
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
902 "Return non-nil if the current token is \"hanging\".
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
903 A hanging keyword is one that's at the end of a line except it's not at
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
904 the beginning of a line."
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
905 (and (not (smie-indent--bolp))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
906 (save-excursion
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
907 (<= (line-end-position)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
908 (progn
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
909 (when (zerop (length (funcall smie-forward-token-function)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
910 ;; Could be an open-paren.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
911 (forward-char 1))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
912 (skip-chars-forward " \t")
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
913 (or (eolp)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
914 (and (looking-at comment-start-skip)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
915 (forward-comment (point-max))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
916 (point))))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
917
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
918 (defalias 'smie-rule-bolp 'smie-indent--bolp)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
919 (defun smie-indent--bolp ()
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
920 "Return non-nil if the current token is the first on the line."
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
921 (save-excursion (skip-chars-backward " \t") (bolp)))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
922
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
923 ;; Dynamically scoped.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
924 (defvar smie--parent) (defvar smie--after) (defvar smie--token)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
925
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
926 (defun smie-indent--parent ()
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
927 (or smie--parent
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
928 (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
929 (let* ((pos (point))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
930 (tok (funcall smie-forward-token-function)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
931 (unless (cadr (assoc tok smie-grammar))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
932 (goto-char pos))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
933 (setq smie--parent
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
934 (smie-backward-sexp 'halfsexp))))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
935
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
936 (defun smie-rule-parent-p (&rest parents)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
937 "Return non-nil if the current token's parent is among PARENTS.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
938 Only meaningful when called from within `smie-rules-function'."
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
939 (member (nth 2 (smie-indent--parent)) parents))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
940
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
941 (defun smie-rule-next-p (&rest tokens)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
942 "Return non-nil if the next token is among TOKENS.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
943 Only meaningful when called from within `smie-rules-function'."
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
944 (let ((next
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
945 (save-excursion
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
946 (unless smie--after
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
947 (smie-indent-forward-token) (setq smie--after (point)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
948 (goto-char smie--after)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
949 (smie-indent-forward-token))))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
950 (member (car next) tokens)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
951
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
952 (defun smie-rule-prev-p (&rest tokens)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
953 "Return non-nil if the previous token is among TOKENS."
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
954 (let ((prev (save-excursion
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
955 (smie-indent-backward-token))))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
956 (member (car prev) tokens)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
957
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
958 (defun smie-rule-sibling-p ()
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
959 "Return non-nil if the parent is actually a sibling.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
960 Only meaningful when called from within `smie-rules-function'."
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
961 (eq (car (smie-indent--parent))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
962 (cadr (assoc smie--token smie-grammar))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
963
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
964 (defun smie-rule-parent (&optional offset)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
965 "Align with parent.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
966 If non-nil, OFFSET should be an integer giving an additional offset to apply.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
967 Only meaningful when called from within `smie-rules-function'."
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
968 (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
969 (goto-char (cadr (smie-indent--parent)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
970 (cons 'column
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
971 (+ (or offset 0)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
972 (if (smie-indent--hanging-p)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
973 (smie-indent-virtual) (current-column))))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
974
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
975 (defvar smie-rule-separator-outdent 2)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
976
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
977 (defun smie-indent--separator-outdent ()
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
978 ;; FIXME: Here we actually have several reasonable behaviors.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
979 ;; E.g. for a parent token of "FOO" and a separator ";" we may want to:
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
980 ;; 1- left-align ; with FOO.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
981 ;; 2- right-align ; with FOO.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
982 ;; 3- align content after ; with content after FOO.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
983 ;; 4- align content plus add/remove spaces so as to align ; with FOO.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
984 ;; Currently, we try to align the contents (option 3) which actually behaves
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
985 ;; just like option 2 (if the number of spaces after FOO and ; is equal).
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
986 (let ((afterpos (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
987 (let ((tok (funcall smie-forward-token-function)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
988 (unless tok
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
989 (with-demoted-errors
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
990 (error "smie-rule-separator: can't skip token %s"
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
991 smie--token))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
992 (skip-chars-forward " ")
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
993 (unless (eolp) (point)))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
994 (or (and afterpos
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
995 ;; This should always be true, unless
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
996 ;; smie-forward-token-function skipped a \n.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
997 (< afterpos (line-end-position))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
998 (- afterpos (point)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
999 smie-rule-separator-outdent)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1000
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1001 (defun smie-rule-separator (method)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1002 "Indent current token as a \"separator\".
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1003 By \"separator\", we mean here a token whose sole purpose is to separate
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1004 various elements within some enclosing syntactic construct, and which does
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1005 not have any semantic significance in itself (i.e. it would typically no exist
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1006 as a node in an abstract syntax tree).
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1007 Such a token is expected to have an associative syntax and be closely tied
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1008 to its syntactic parent. Typical examples are \",\" in lists of arguments
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1009 \(enclosed inside parentheses), or \";\" in sequences of instructions (enclosed
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1010 in a {..} or begin..end block).
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1011 METHOD should be the method name that was passed to `smie-rules-function'.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1012 Only meaningful when called from within `smie-rules-function'."
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1013 ;; FIXME: The code below works OK for cases where the separators
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1014 ;; are placed consistently always at beginning or always at the end,
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1015 ;; but not if some are at the beginning and others are at the end.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1016 ;; I.e. it gets confused in cases such as:
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1017 ;; ( a
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1018 ;; , a,
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1019 ;; b
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1020 ;; , c,
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1021 ;; d
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1022 ;; )
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1023 ;;
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1024 ;; Assuming token is associative, the default rule for associative
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1025 ;; tokens (which assumes an infix operator) works fine for many cases.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1026 ;; We mostly need to take care of the case where token is at beginning of
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1027 ;; line, in which case we want to align it with its enclosing parent.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1028 (cond
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1029 ((and (eq method :before) (smie-rule-bolp) (not (smie-rule-sibling-p)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1030 ;; FIXME: Rather than consult the number of spaces, we could *set* the
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1031 ;; number of spaces so as to align the separator with the close-paren
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1032 ;; while aligning the content with the rest.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1033 (let ((parent-col
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1034 (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1035 (goto-char (cadr smie--parent))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1036 (if (smie-indent--hanging-p)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1037 (smie-indent-virtual) (current-column))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1038 (parent-pos-col ;FIXME: we knew this when computing smie--parent.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1039 (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1040 (goto-char (cadr smie--parent))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1041 (smie-indent-forward-token)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1042 (forward-comment (point-max))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1043 (current-column))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1044 (cons 'column
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1045 (max parent-col
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1046 (min parent-pos-col
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1047 (- parent-pos-col (smie-indent--separator-outdent)))))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1048 ((and (eq method :after) (smie-indent--bolp))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1049 (smie-indent--separator-outdent))))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1050
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
1051 (defun smie-indent--offset (elem)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1052 (or (funcall smie-rules-function :elem elem)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1053 (if (not (eq elem 'basic))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1054 (funcall smie-rules-function :elem 'basic))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1055 smie-indent-basic))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1056
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1057 (defun smie-indent--rule (method token
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1058 ;; FIXME: Too many parameters.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1059 &optional after parent base-pos)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1060 "Compute indentation column according to `indent-rule-functions'.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1061 METHOD and TOKEN are passed to `indent-rule-functions'.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1062 AFTER is the position after TOKEN, if known.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1063 PARENT is the parent info returned by `smie-backward-sexp', if known.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1064 BASE-POS is the position relative to which offsets should be applied."
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1065 ;; This is currently called in 3 cases:
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1066 ;; - :before opener, where rest=nil but base-pos could as well be parent.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1067 ;; - :before other, where
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1068 ;; ; after=nil
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1069 ;; ; parent is set
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1070 ;; ; base-pos=parent
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1071 ;; - :after tok, where
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1072 ;; ; after is set; parent=nil; base-pos=point;
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1073 (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1074 (let ((offset
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1075 (let ((smie--parent parent)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1076 (smie--token token)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1077 (smie--after after))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1078 (funcall smie-rules-function method token))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1079 (cond
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1080 ((not offset) nil)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1081 ((eq (car-safe offset) 'column) (cdr offset))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1082 ((integerp offset)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1083 (+ offset
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1084 (if (null base-pos) 0
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1085 (goto-char base-pos)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1086 (if (smie-indent--hanging-p)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1087 (smie-indent-virtual) (current-column)))))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1088 (t (error "Unknown indentation offset %s" offset))))))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1089
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1090 (defun smie-indent-forward-token ()
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1091 "Skip token forward and return it, along with its levels."
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1092 (let ((tok (funcall smie-forward-token-function)))
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1093 (cond
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1094 ((< 0 (length tok)) (assoc tok smie-grammar))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1095 ((looking-at "\\s(\\|\\s)\\(\\)")
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1096 (forward-char 1)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1097 (cons (buffer-substring (1- (point)) (point))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1098 (if (match-end 1) '(0 nil) '(nil 0)))))))
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1099
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1100 (defun smie-indent-backward-token ()
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1101 "Skip token backward and return it, along with its levels."
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1102 (let ((tok (funcall smie-backward-token-function))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1103 class)
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1104 (cond
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1105 ((< 0 (length tok)) (assoc tok smie-grammar))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1106 ;; 4 == open paren syntax, 5 == close.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1107 ((memq (setq class (syntax-class (syntax-after (1- (point))))) '(4 5))
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1108 (forward-char -1)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1109 (cons (buffer-substring (point) (1+ (point)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1110 (if (eq class 4) '(nil 0) '(0 nil)))))))
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1111
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1112 (defun smie-indent-virtual ()
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1113 ;; We used to take an optional arg (with value :not-hanging) to specify that
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1114 ;; we should only use (smie-indent-calculate) if we're looking at a hanging
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1115 ;; keyword. This was a bad idea, because the virtual indent of a position
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1116 ;; should not depend on the caller, since it leads to situations where two
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1117 ;; dependent indentations get indented differently.
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1118 "Compute the virtual indentation to use for point.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1119 This is used when we're not trying to indent point but just
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1120 need to compute the column at which point should be indented
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1121 in order to figure out the indentation of some other (further down) point."
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1122 ;; Trust pre-existing indentation on other lines.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1123 (if (smie-indent--bolp) (current-column) (smie-indent-calculate)))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1124
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1125 (defun smie-indent-fixindent ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1126 ;; Obey the `fixindent' special comment.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1127 (and (smie-indent--bolp)
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1128 (save-excursion
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1129 (comment-normalize-vars)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1130 (re-search-forward (concat comment-start-skip
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1131 "fixindent"
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1132 comment-end-skip)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1133 ;; 1+ to account for the \n comment termination.
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1134 (1+ (line-end-position)) t))
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1135 (current-column)))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1136
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1137 (defun smie-indent-bob ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1138 ;; Start the file at column 0.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1139 (save-excursion
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1140 (forward-comment (- (point)))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1141 (if (bobp) 0)))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1142
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1143 (defun smie-indent-close ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1144 ;; Align close paren with opening paren.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1145 (save-excursion
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1146 ;; (forward-comment (point-max))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1147 (when (looking-at "\\s)")
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1148 (while (not (zerop (skip-syntax-forward ")")))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1149 (skip-chars-forward " \t"))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1150 (condition-case nil
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1151 (progn
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1152 (backward-sexp 1)
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1153 (smie-indent-virtual)) ;:not-hanging
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1154 (scan-error nil)))))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1155
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1156 (defun smie-indent-keyword ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1157 ;; Align closing token with the corresponding opening one.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1158 ;; (e.g. "of" with "case", or "in" with "let").
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1159 (save-excursion
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1160 (let* ((pos (point))
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1161 (toklevels (smie-indent-forward-token))
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1162 (token (pop toklevels)))
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1163 (if (null (car toklevels))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1164 (save-excursion
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1165 (goto-char pos)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1166 ;; Different cases:
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1167 ;; - smie-indent--bolp: "indent according to others".
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1168 ;; - common hanging: "indent according to others".
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1169 ;; - SML-let hanging: "indent like parent".
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1170 ;; - if-after-else: "indent-like parent".
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1171 ;; - middle-of-line: "trust current position".
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1172 (cond
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1173 ((null (cdr toklevels)) nil) ;Not a keyword.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1174 ((smie-indent--bolp)
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1175 ;; For an open-paren-like thingy at BOL, always indent only
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1176 ;; based on other rules (typically smie-indent-after-keyword).
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1177 nil)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1178 ;; We're only ever here for virtual-indent.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1179 ((smie-indent--rule :before token))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1180 (t
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1181 ;; By default use point unless we're hanging.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1182 (unless (smie-indent--hanging-p) (current-column)))))
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1183
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1184 ;; FIXME: This still looks too much like black magic!!
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1185 (let* ((parent (smie-backward-sexp 'halfsexp)))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1186 ;; Different behaviors:
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1187 ;; - align with parent.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1188 ;; - parent + offset.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1189 ;; - after parent's column + offset (actually, after or before
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1190 ;; depending on where backward-sexp stopped).
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1191 ;; ? let it drop to some other indentation function (almost never).
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1192 ;; ? parent + offset + parent's own offset.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1193 ;; Different cases:
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1194 ;; - bump into a same-level operator.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1195 ;; - bump into a specific known parent.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1196 ;; - find a matching open-paren thingy.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1197 ;; - bump into some random parent.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1198 ;; ? borderline case (almost never).
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1199 ;; ? bump immediately into a parent.
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1200 (cond
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1201 ((not (or (< (point) pos)
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1202 (and (cadr parent) (< (cadr parent) pos))))
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1203 ;; If we didn't move at all, that means we didn't really skip
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1204 ;; what we wanted. Should almost never happen, other than
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1205 ;; maybe when an infix or close-paren is at the beginning
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1206 ;; of a buffer.
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1207 nil)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1208 ((save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1209 (goto-char pos)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1210 (smie-indent--rule :before token nil parent (cadr parent))))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1211 ((eq (car parent) (car toklevels))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1212 ;; We bumped into a same-level operator; align with it.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1213 (if (and (smie-indent--bolp) (/= (point) pos)
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1214 (save-excursion
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1215 (goto-char (goto-char (cadr parent)))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1216 (not (smie-indent--bolp))))
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1217 ;; If the parent is at EOL and its children are indented like
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1218 ;; itself, then we can just obey the indentation chosen for the
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1219 ;; child.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1220 ;; This is important for operators like ";" which
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1221 ;; are usually at EOL (and have an offset of 0): otherwise we'd
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1222 ;; always go back over all the statements, which is
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1223 ;; a performance problem and would also mean that fixindents
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1224 ;; in the middle of such a sequence would be ignored.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1225 ;;
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1226 ;; This is a delicate point!
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1227 ;; Even if the offset is not 0, we could follow the same logic
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1228 ;; and subtract the offset from the child's indentation.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1229 ;; But that would more often be a bad idea: OT1H we generally
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1230 ;; want to reuse the closest similar indentation point, so that
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1231 ;; the user's choice (or the fixindents) are obeyed. But OTOH
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1232 ;; we don't want this to affect "unrelated" parts of the code.
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1233 ;; E.g. a fixindent in the body of a "begin..end" should not
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1234 ;; affect the indentation of the "end".
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1235 (current-column)
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1236 (goto-char (cadr parent))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1237 ;; Don't use (smie-indent-virtual :not-hanging) here, because we
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1238 ;; want to jump back over a sequence of same-level ops such as
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1239 ;; a -> b -> c
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1240 ;; -> d
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1241 ;; So as to align with the earliest appropriate place.
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1242 (smie-indent-virtual)))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1243 (t
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1244 (if (and (= (point) pos) (smie-indent--bolp))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1245 ;; Since we started at BOL, we're not computing a virtual
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1246 ;; indentation, and we're still at the starting point, so
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1247 ;; we can't use `current-column' which would cause
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1248 ;; indentation to depend on itself and we can't use
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1249 ;; smie-indent-virtual since that would be an inf-loop.
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1250 nil
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1251 ;; In indent-keyword, if we're indenting `then' wrt `if', we
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1252 ;; want to use indent-virtual rather than use just
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1253 ;; current-column, so that we can apply the (:before . "if")
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1254 ;; rule which does the "else if" dance in SML. But in other
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1255 ;; cases, we do not want to use indent-virtual (e.g. indentation
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1256 ;; of "*" w.r.t "+", or ";" wrt "("). We could just always use
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1257 ;; indent-virtual and then have indent-rules say explicitly to
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1258 ;; use `point' after things like "(" or "+" when they're not at
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1259 ;; EOL, but you'd end up with lots of those rules.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1260 ;; So we use a heuristic here, which is that we only use virtual
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1261 ;; if the parent is tightly linked to the child token (they're
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1262 ;; part of the same BNF rule).
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1263 (if (car parent) (current-column) (smie-indent-virtual))))))))))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1264
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1265 (defun smie-indent-comment ()
109838
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1266 "Compute indentation of a comment."
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1267 ;; Don't do it for virtual indentations. We should normally never be "in
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1268 ;; front of a comment" when doing virtual-indentation anyway. And if we are
64936c70841e Try and remove some of SMIE's black magic by generalizing some rules.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108923
diff changeset
1269 ;; (as can happen in octave-mode), moving forward can lead to inf-loops.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1270 (and (smie-indent--bolp)
110464
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1271 (let ((pos (point)))
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1272 (save-excursion
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1273 (beginning-of-line)
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1274 (and (re-search-forward comment-start-skip (line-end-position) t)
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1275 (eq pos (or (match-end 1) (match-beginning 0))))))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1276 (save-excursion
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1277 (forward-comment (point-max))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1278 (skip-chars-forward " \t\r\n")
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1279 (smie-indent-calculate))))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1280
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1281 (defun smie-indent-comment-continue ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1282 ;; indentation of comment-continue lines.
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1283 (let ((continue (and comment-continue
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1284 (comment-string-strip comment-continue t t))))
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1285 (and (< 0 (length continue))
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1286 (looking-at (regexp-quote continue)) (nth 4 (syntax-ppss))
109841
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1287 (let ((ppss (syntax-ppss)))
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1288 (save-excursion
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1289 (forward-line -1)
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1290 (if (<= (point) (nth 8 ppss))
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1291 (progn (goto-char (1+ (nth 8 ppss))) (current-column))
3a11454ef7ca Reindent smie.el
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109840
diff changeset
1292 (skip-chars-forward " \t")
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1293 (if (looking-at (regexp-quote continue))
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1294 (current-column))))))))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1295
110464
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1296 (defun smie-indent-comment-close ()
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1297 (and (boundp 'comment-end-skip)
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1298 comment-end-skip
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1299 (not (looking-at " \t*$")) ;Not just a \n comment-closer.
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1300 (looking-at comment-end-skip)
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1301 (nth 4 (syntax-ppss))
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1302 (save-excursion
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1303 (goto-char (nth 8 (syntax-ppss)))
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1304 (current-column))))
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1305
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1306 (defun smie-indent-comment-inside ()
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1307 (and (nth 4 (syntax-ppss))
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1308 'noindent))
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1309
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1310 (defun smie-indent-after-keyword ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1311 ;; Indentation right after a special keyword.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1312 (save-excursion
108894
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1313 (let* ((pos (point))
ca3bfaa18e56 Make (after keyword) indent-rules more flexible.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108878
diff changeset
1314 (toklevel (smie-indent-backward-token))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1315 (tok (car toklevel)))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1316 (cond
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1317 ((null toklevel) nil)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1318 ((smie-indent--rule :after tok pos nil (point)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1319 ;; The default indentation after a keyword/operator is
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1320 ;; 0 for infix, t for prefix, and use another rule
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1321 ;; for postfix.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1322 ((null (nth 2 toklevel)) nil) ;A closer.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1323 ((or (null (nth 1 toklevel)) ;An opener.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1324 (rassoc tok smie-closer-alist)) ;An inner.
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1325 (+ (smie-indent-virtual) (smie-indent--offset 'basic))) ;
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1326 (t (smie-indent-virtual)))))) ;An infix.
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1327
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1328 (defun smie-indent-exps ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1329 ;; Indentation of sequences of simple expressions without
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1330 ;; intervening keywords or operators. E.g. "a b c" or "g (balbla) f".
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1331 ;; Can be a list of expressions or a function call.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1332 ;; If it's a function call, the first element is special (it's the
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1333 ;; function). We distinguish function calls from mere lists of
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1334 ;; expressions based on whether the preceding token is listed in
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1335 ;; the `list-intro' entry of smie-indent-rules.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1336 ;;
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1337 ;; TODO: to indent Lisp code, we should add a way to specify
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1338 ;; particular indentation for particular args depending on the
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1339 ;; function (which would require always skipping back until the
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1340 ;; function).
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1341 ;; TODO: to indent C code, such as "if (...) {...}" we might need
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1342 ;; to add similar indentation hooks for particular positions, but
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1343 ;; based on the preceding token rather than based on the first exp.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1344 (save-excursion
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1345 (let ((positions nil)
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1346 arg)
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1347 (while (and (null (car (smie-backward-sexp)))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1348 (push (point) positions)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1349 (not (smie-indent--bolp))))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1350 (save-excursion
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1351 ;; Figure out if the atom we just skipped is an argument rather
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1352 ;; than a function.
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1353 (setq arg
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1354 (or (null (car (smie-backward-sexp)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1355 (funcall smie-rules-function :list-intro
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1356 (funcall smie-backward-token-function)))))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1357 (cond
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1358 ((null positions)
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1359 ;; We're the first expression of the list. In that case, the
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1360 ;; indentation should be (have been) determined by its context.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1361 nil)
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1362 (arg
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1363 ;; There's a previous element, and it's not special (it's not
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1364 ;; the function), so let's just align with that one.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1365 (goto-char (car positions))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1366 (current-column))
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1367 ((cdr positions)
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1368 ;; We skipped some args plus the function and bumped into something.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1369 ;; Align with the first arg.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1370 (goto-char (cadr positions))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1371 (current-column))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1372 (positions
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1373 ;; We're the first arg.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1374 (goto-char (car positions))
110429
06323f4d421b * lisp/emacs-lisp/smie.el (smie-bnf-precedence-table): Improve error message.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110161
diff changeset
1375 (+ (smie-indent--offset 'args)
108897
68586a267c40 * lisp/emacs-lisp/smie.el (smie-indent-offset-rule): Rename from
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108894
diff changeset
1376 ;; We used to use (smie-indent-virtual), but that
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1377 ;; doesn't seem right since it might then indent args less than
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1378 ;; the function itself.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1379 (current-column)))))))
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1380
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1381 (defvar smie-indent-functions
110464
27917ce150c1 * lisp/emacs-lisp/smie.el (smie-debug--describe-cycle): Fix typo.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110432
diff changeset
1382 '(smie-indent-fixindent smie-indent-bob smie-indent-close
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1383 smie-indent-comment smie-indent-comment-continue smie-indent-comment-close
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1384 smie-indent-comment-inside smie-indent-keyword smie-indent-after-keyword
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1385 smie-indent-exps)
108865
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1386 "Functions to compute the indentation.
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1387 Each function is called with no argument, shouldn't move point, and should
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1388 return either nil if it has no opinion, or an integer representing the column
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1389 to which that point should be aligned, if we were to reindent it.")
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1390
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1391 (defun smie-indent-calculate ()
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1392 "Compute the indentation to use for point."
9d8fb76d6857 Split smie-indent-calculate into more manageable chunks.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 108864
diff changeset
1393 (run-hook-with-args-until-success 'smie-indent-functions))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1394
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1395 (defun smie-indent-line ()
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1396 "Indent current line using the SMIE indentation engine."
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1397 (interactive)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1398 (let* ((savep (point))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1399 (indent (or (with-demoted-errors
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1400 (save-excursion
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1401 (forward-line 0)
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1402 (skip-chars-forward " \t")
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1403 (if (>= (point) savep) (setq savep nil))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1404 (or (smie-indent-calculate) 0)))
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1405 0)))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1406 (if (not (numberp indent))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1407 ;; If something funny is used (e.g. `noindent'), return it.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1408 indent
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1409 (if (< indent 0) (setq indent 0)) ;Just in case.
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1410 (if savep
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1411 (save-excursion (indent-line-to indent))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1412 (indent-line-to indent)))))
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1413
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1414 (defun smie-setup (grammar rules-function &rest keywords)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1415 "Setup SMIE navigation and indentation.
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1416 GRAMMAR is a grammar table generated by `smie-prec2->grammar'.
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1417 RULES-FUNCTION is a set of indentation rules for use on `smie-rules-function'.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1418 KEYWORDS are additional arguments, which can use the following keywords:
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1419 - :forward-token FUN
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1420 - :backward-token FUN"
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1421 (set (make-local-variable 'smie-rules-function) rules-function)
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1422 (set (make-local-variable 'smie-grammar) grammar)
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1423 (set (make-local-variable 'indent-line-function) 'smie-indent-line)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1424 (set (make-local-variable 'forward-sexp-function)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1425 'smie-forward-sexp-command)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1426 (while keywords
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1427 (let ((k (pop keywords))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1428 (v (pop keywords)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1429 (case k
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1430 (:forward-token
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1431 (set (make-local-variable 'smie-forward-token-function) v))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1432 (:backward-token
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1433 (set (make-local-variable 'smie-backward-token-function) v))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1434 (t (message "smie-setup: ignoring unknown keyword %s" k)))))
111440
b72ff43b041f * lisp/emacs-lisp/smie.el: Simplify the smie-rules-function return values.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 111304
diff changeset
1435 (let ((ca (cdr (assq :smie-closer-alist grammar))))
111232
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1436 (when ca
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1437 (set (make-local-variable 'smie-closer-alist) ca)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1438 ;; Only needed for interactive calls to blink-matching-open.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1439 (set (make-local-variable 'blink-matching-check-function)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1440 #'smie-blink-matching-check)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1441 (add-hook 'post-self-insert-hook
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1442 #'smie-blink-matching-open 'append 'local)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1443 (set (make-local-variable 'smie-blink-matching-triggers)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1444 (append smie-blink-matching-triggers
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1445 ;; Rather than wait for SPC to blink, try to blink as
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1446 ;; soon as we type the last char of a block ender.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1447 (let ((closers (sort (mapcar #'cdr smie-closer-alist)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1448 #'string-lessp))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1449 (triggers ())
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1450 closer)
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1451 (while (setq closer (pop closers))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1452 (unless (and closers
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1453 ;; FIXME: this eliminates prefixes of other
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1454 ;; closers, but we should probably elimnate
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1455 ;; prefixes of other keywords as well.
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1456 (string-prefix-p closer (car closers)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1457 (push (aref closer (1- (length closer))) triggers)))
a9904c1962db SMIE: change indent rules format, improve smie-setup.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110802
diff changeset
1458 (delete-dups triggers)))))))
108632
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1459
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1460
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1461 (provide 'smie)
d38b0dd2bdbe Provide a simple generic indentation engine and use it for Prolog.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
1462 ;;; smie.el ends here