1 ;;; sm-c-mode.el --- Experimental C major mode based on SMIE -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is an experiment to see concretely where&how SMIE falls down when
27 ;; trying to handle a language like C.
28 ;; So, strictly speaking, this does provide "SMIE-based indentation for C" and
29 ;; might even do it OK for simple cases, but it really doesn't benefit much
31 ;; - it does a lot of its own parsing by hand.
32 ;; - its smie-rules-function also does a lot of indentation by hand.
33 ;; Hopefully at some point, someone will find a way to extend SMIE such that
34 ;; it can handle C without having to constantly work around SMIE, e.g.
35 ;; it'd be nice to hook sm-c--while-to-do, sm-c--else-to-if, sm-c--boi,
36 ;; sm-c--boe, ... into SMIE at some level.
38 ;; Note that this mode makes no attempt to try and handle sanely K&R style
39 ;; function definitions.
43 ;; This code can't be compared to CC-mode since its scope is much more limited
44 ;; (only tries to handle the kind of code found in Emacs's source code, for
45 ;; example; does not intend to be extensible to handle C++ or ObjC; does not
46 ;; offer the same kind of customizability of indentation style, ...).
47 ;; But in order to make sure it's doing a good enough job on the code for which
48 ;; it was tuned, I did run some quick benchmarks against CC-mode:
50 ;; Benchmarks: reindent emacs/src/*.[ch] (skipping macuvs.h and globals.h
51 ;; because CC-mode gets pathologically slow on them).
52 ;; (cd src/emacs/work/; git reset --hard; mv src/macuvs.h src/globals.h ./);
53 ;; files=($(echo ~/src/emacs/work/src/*.[ch]));
54 ;; (cd src/emacs/work/; mv macuvs.h globals.h src/);
55 ;; time make -j4 ${^${files}}.reindent EMACS="emacs24 -Q";
56 ;; (cd src/emacs/work/; git diff|wc)
57 ;; - Default settings:
58 ;; diff|wc => 86800 379362 2879534
59 ;; make -j4 191.57s user 1.77s system 334% cpu 57.78 total
60 ;; - With (setq sm-c-indent-cpp-basic 0)
61 ;; diff|wc => 59909 275415 2034045
62 ;; make -j4 177.88s user 1.70s system 340% cpu 52.80 total
63 ;; - For reference, CC-mode gets:
64 ;; diff|wc => 79164 490894 3428542
65 ;; make -j4 804.83s user 2.79s system 277% cpu 4:51.08 total
67 ;; Again: take this with a large grain of salt, since this is testing sm-c-mode
68 ;; in the most favorable light (IOW it's a very strongly biased benchmark).
69 ;; All this says, is that sm-c-mode's indentation might actually be usable if
70 ;; you use it on C code that is sufficiently similar to Emacs's.
74 ;; - We "use but don't use" SMIE.
75 ;; - CPP directives are treated as comments. To some extent this is OK, but in
76 ;; many other cases it isn't. See for instance the comment-only-p advice.
77 ;; - M-q in a comment doesn't do the right thing.
84 (defgroup sm-c-mode nil
85 "Major mode to edit C code, based on SMIE."
88 (defcustom sm-c-indent-basic 2
89 "Basic step of indentation.
90 Typically 2 for GNU style and `tab-width' for Linux style."
93 (defcustom sm-c-indent-braces t
94 "If nil, braces in if/while/... are aligned with the if/while/...
95 Else, they're indented by `sm-c-indent-basic' columns.
96 For braces placed at the end of lines (which SMIE calls \"hanging\"), it makes
100 ;;; Handling CPP directives.
102 (defsubst sm-c--cpp-inside-p (ppss)
106 (defconst sm-c--cpp-regexp "^[ \t]*\\(\\(#\\)[ \t]*\\([a-z]+\\)\\)"))
108 (defconst sm-c--cpp-syntax-table
109 (let ((st (make-syntax-table)))
110 (modify-syntax-entry ?/ ". 124" st)
111 (modify-syntax-entry ?* ". 23b" st)
112 (modify-syntax-entry ?\n ">" st)
115 (defun sm-c--cpp-goto-end (ppss &optional limit)
116 (cl-assert (sm-c--cpp-inside-p ppss))
119 (and (setq found (re-search-forward "\\(?:\\\\\\\\\\)*\n" limit 'move))
120 ;; We could also check (nth 5 ppss) to figure out if we're
121 ;; after a backslash, but this is a very common case, so it's good
122 ;; to avoid calling parse-partial-sexp for that.
123 (or (eq ?\\ (char-before (match-beginning 0)))
124 (with-syntax-table sm-c--cpp-syntax-table
125 (nth 4 (parse-partial-sexp (1+ (nth 8 ppss)) (point)))))))
128 (defun sm-c--cpp-fontify-syntactically (ppss)
129 ;; FIXME: ¡¡BIG UGLY HACK!!
130 ;; Copied from font-lock.el's font-lock-fontify-syntactically-region.
131 (cl-assert (> (point) (nth 8 ppss)))
134 (sm-c--cpp-goto-end ppss)
135 (narrow-to-region (1+ (nth 8 ppss)) (point))
136 ;; FIXME: We should add some "with-local-syntax-ppss" macro to
138 (let ((syntax-propertize-function nil)
139 (syntax-ppss-cache nil)
140 (syntax-ppss-last nil))
141 (font-lock-fontify-syntactically-region (point-min) (point-max))))))
143 (defun sm-c--cpp-syntax-propertize (end)
144 (let ((ppss (syntax-ppss))
146 (when (sm-c--cpp-inside-p ppss)
148 (and (setq found (re-search-forward "\\(\\\\\\\\\\)*\n" end 'move))
149 (or (eq ?\\ (char-before (match-beginning 0)))
150 (with-syntax-table sm-c--cpp-syntax-table
151 (nth 4 (parse-partial-sexp (1+ (nth 8 ppss)) (point)))))))
153 (put-text-property (1- (point)) (point)
154 'syntax-table (string-to-syntax "> c"))))))
156 ;;;; Indenting CPP directives.
158 (defcustom sm-c-indent-cpp-basic 1
159 "Indent step for CPP directives.
160 If non-zero, CPP directives are indented according to CPP depth.
161 E.g. a #define nested within 2 #ifs will be turned into \"# define\"."
164 (defun sm-c--cpp-prev (tok)
167 (when (re-search-backward sm-c--cpp-regexp nil t)
168 (pcase (cons tok (match-string 3))
169 (`(,_ . "endif") (sm-c--cpp-prev "endif"))
170 ((or `(,(or "endif" "else" "elif") . ,(or "if" "ifdef" "ifndef"))
171 `(,(or "else" "elif") . "elif"))
173 (`(,(or "endif" "else" "elif") . ,_) nil)
174 (`(,_ . ,(or "if" "ifdef" "ifndef" "elif" "else"))
175 (setq offset sm-c-indent-cpp-basic))
179 (goto-char (match-beginning 3))
180 (+ offset (current-column)))))
183 (defun sm-c--cpp-indent-line (&optional _arg)
184 ;; FIXME: Also align the terminating \, if any.
185 (when (> sm-c-indent-cpp-basic 0)
186 (let* ((pos (point-marker))
191 (when (looking-at sm-c--cpp-regexp)
192 (setq beg (match-beginning 3))
193 (or (sm-c--cpp-prev (match-string 3)) 0)))))
195 (let ((before (<= pos beg)))
197 (unless (= (current-column) indent)
198 (skip-chars-backward " \t")
199 (delete-region (point)
200 (progn (skip-chars-forward " \t") (point)))
202 (unless before (goto-char pos)))))))
204 ;;;; Indenting inside CPP #define.
206 (defconst sm-c--cpp-smie-indent-functions
207 ;; FIXME: Don't just align line after #define with the "d"!
211 ((eq f #'smie-indent-comment-inside) #'sm-c--cpp-indent-comment-inside)
212 ;; ((eq f #'smie-indent-exps) #'sm-c--cpp-indent-exps)
214 (default-value 'smie-indent-functions)))
216 (defun sm-c--cpp-indent-comment-inside ()
217 (let ((ppss (syntax-ppss)))
219 ;; Indicate where's the comment start.
220 `(noindent . ,(nth 8 ppss)))))
222 (defun sm-c--cpp-smie-indent ()
223 (let ((ppss (syntax-ppss)))
225 ((sm-c--cpp-inside-p ppss)
227 (narrow-to-region (nth 8 ppss) (point-max))
229 (let ((smie-indent-functions sm-c--cpp-smie-indent-functions)
230 (syntax-ppss-cache nil)
231 (syntax-ppss-last nil)
232 (parse-sexp-lookup-properties nil))
233 (smie-indent-calculate))))
234 (if (not (eq 'noindent (car-safe indent)))
235 (if (integerp indent)
236 (max (funcall smie-rules-function :elem 'basic) indent)
238 ;; We can't just return `noindent' if we're inside a comment,
239 ;; because the indent.el code would then be similarly confused,
240 ;; thinking the `noindent' is because we're inside the cpp
241 ;; pseudo-comment, and would hence align the code with the content
242 ;; of the psuedo-comment rather than the nested real comment!
244 ;; FIXME: Copy&paste from indent--default-inside-comment.
245 ;; FIXME: This will always re-indent inside these comments, even
246 ;; during indent-region.
249 (skip-chars-forward " \t")
250 (when (< (1- (point)) (cdr indent) (line-end-position))
251 (goto-char (cdr indent))
252 (when (looking-at comment-start-skip)
253 (goto-char (match-end 0))))
254 (current-column))))))
256 ((equal (syntax-after (point)) (string-to-syntax "< c")) 0)
261 (defvar sm-c-mode-syntax-table
262 (let ((st (make-syntax-table)))
263 (modify-syntax-entry ?/ ". 124" st)
264 (modify-syntax-entry ?* ". 23b" st)
265 (modify-syntax-entry ?\n ">" st)
266 (modify-syntax-entry ?\" "\"" st)
267 (modify-syntax-entry ?\' "\"" st)
268 (modify-syntax-entry ?= "." st)
269 (modify-syntax-entry ?< "." st)
270 (modify-syntax-entry ?> "." st)
273 (defun sm-c-syntax-propertize (start end)
275 (sm-c--cpp-syntax-propertize end)
277 (syntax-propertize-rules
280 (when (and (equal (match-string 3) "include")
281 (looking-at "[ \t]*\\(<\\)[^>\n]*\\(>\\)"))
282 (put-text-property (match-beginning 1) (match-end 1)
283 'syntax-table (string-to-syntax "|"))
284 (put-text-property (match-beginning 2) (match-end 2)
285 'syntax-table (string-to-syntax "|")))
286 (sm-c--cpp-syntax-propertize end)))))
289 (defun sm-c-syntactic-face-function (ppss)
290 (if (sm-c--cpp-inside-p ppss)
291 (prog1 nil (sm-c--cpp-fontify-syntactically ppss))
292 (funcall (default-value 'font-lock-syntactic-face-function) ppss)))
296 (defconst sm-c-paren-block-keywords '("if" "while" "for" "switch"))
298 (defconst sm-c-smie-precedence-table
300 ;; Compiled from https://en.wikipedia.org/wiki/Operators_in_C_and_C++.
302 ;; (nonassoc "throw")
303 (nonassoc "=" "+=" "-=" "*=" "/=" "%=" "<<=" ">>=" "&=" "^=" "|=") ;2
304 ;; (nonassoc "?" ":") ;; Better handle it in the BNF.
309 ;; (assoc "&") ;; Binary and. Confused with address-of.
310 (nonassoc "==" "!=") ;7
311 (nonassoc "<" "<=" ">" ">=") ;8
312 (nonassoc "<<" ">>") ;9
314 (assoc "/" "* mult" "%") ;11
315 ;; (nonassoc ".*" "->*") ;12 ;; Only C++
316 ;; (nonassoc "++" "--" "+" "-" "!" "~" "(type)" "*" "&"
317 ;; "sizeof" "new" "delete");13 ;; All prefix.
318 (left "." "->") ;; "++" "--" suffixes, "()", "[]", "typeid", "*_cast". ;14
319 ;; (noassoc "::") ;; Only C++
322 (defconst sm-c-smie-grammar
323 ;; `((:smie-closer-alist ("{" . "}")) ("{" (39) 0) ("}" 0 (40)) ("else" 27 26) ("," 38 38) ("do" (41) 22) ("while" (42) 23) ("for" (43) 24) (";" 11 11) ("if" (44) 25))
328 '((decls ("typedef" decl) ("extern" decl)
332 (insts ("{" insts "}")
337 ("case" subexp ": case")
339 (exp-if ("if" exp) ("do" exp) ("while" exp) ("switch" exp) ("for" exp)
341 (exp ("(" exp ")") (exp "," exp) (subexp "?" exp ":" exp))
342 (subexp (subexp "||" subexp))
343 ;; Some of the precedence table deals with pre/postfixes, which
344 ;; smie-precs->prec2 can't handle, so handle it here instead.
345 (exp11 (exp12) (exp11 "/" exp11))
346 (exp12 (exp13)) ;C++ only.
347 (exp13 (exp14) ("++ prefix" exp13) ("-- prefix" exp13)
348 ("!" exp13) ("~" exp13) ("&" exp13) ("* deref" exp13))
349 (exp14 (id) (exp14 "++ postfix") (exp14 "-- postfix")
350 (exp14 "->" id) (exp14 "." id)))
351 '((assoc ";") (assoc ",") (nonassoc "?" ":"))
352 sm-c-smie-precedence-table)
353 (smie-precs->prec2 sm-c-smie-precedence-table)
354 (smie-precs->prec2 '((nonassoc ";") (nonassoc ":")))))))
355 ;; SMIE gives (":label" 261 262), but really this could just as well be
356 ;; (":label" nil nil) because labels don't have any argument to their left
357 ;; or right. They're like both openers and closers at the same time.
359 (if (equal (car-safe x) ":label")
360 ;; Rather than (":label" (n1) (n2)) we use
361 ;; (":label" (n1) n2) because SMIE otherwise complains:
362 ;; cl--assertion-failed((numberp (funcall op-forw toklevels)))
363 ;; in smie-next-sexp.
364 `(,(nth 0 x) (,(nth 1 x)) ,(nth 2 x)) x))
367 ;; (defun sm-c--:-discriminate ()
369 ;; (and (null (smie-backward-sexp))
370 ;; (let ((prev (smie-indent-backward-token)))
372 ;; ((equal prev "case" ) ": case")
373 ;; ((member prev '(";" "{" "}")) ":-label")
376 (defconst sm-c-smie-operator-regexp
378 (pcase-dolist (`(,token . ,_) sm-c-smie-grammar)
379 (when (and (stringp token) (string-match "\\`[^ [:alnum:](){}]+" token))
380 (push (match-string 0 token) ops)))
383 (defun sm-c-smie-forward-token ()
384 (forward-comment (point-max))
385 (let ((tok (if (looking-at sm-c-smie-operator-regexp)
386 (progn (goto-char (match-end 0)) (match-string 0))
387 (smie-default-forward-token))))
389 ((and (equal tok "") (looking-at "\\\\\n"))
390 (goto-char (match-end 0))
391 (sm-c-smie-forward-token))
392 ((member tok '(":" "*"))
393 (save-excursion (sm-c-smie-backward-token)))
394 ((looking-at "[ \t]*:")
395 (if (not (equal (save-excursion (sm-c-smie-forward-token)) ":label"))
397 (looking-at "[ \t]*:")
398 (goto-char (match-end 0)) ":label"))
402 (defun sm-c-smie-backward-token ()
403 (forward-comment (- (point)))
404 (let ((tok (if (looking-back sm-c-smie-operator-regexp (- (point) 3) t)
405 (progn (goto-char (match-beginning 0)) (match-string 0))
406 (smie-default-backward-token))))
408 ((and (equal tok "") (looking-at "\n"))
410 (if (not (= 0 (mod (skip-chars-backward "\\\\") 2)))
411 (sm-c-smie-backward-token)
414 ((equal tok "*") (sm-c-smie--*-token))
417 (prev (sm-c-smie-backward-token)))
418 (if (zerop (length prev))
419 (progn (goto-char pos1) tok)
420 (let ((pos2 (point)))
421 (pcase (car (smie-indent-backward-token))
422 ("case" (goto-char pos1) ": case")
423 ((or ";" "{" "}") (goto-char pos2) ":label")
424 (_ (goto-char pos1) tok))))))
427 (defun sm-c--prev-token ()
428 (car (smie-indent-backward-token)))
430 (defun sm-c--else-to-if ()
432 (unless (equal (sm-c--prev-token) ";")
435 (pcase (smie-backward-sexp)
436 (`(,_ ,pos "if") (goto-char pos) nil) ;Found it!
437 (`(,_ ,_ ";") nil) ;Can't find it!
438 (`(,_ ,pos "else") (goto-char pos) (sm-c--else-to-if) t)
440 (goto-char pos) (unless (sm-c--while-to-do) (goto-char pos)) t)
441 (`(t . ,_) nil) ;Can't find it!
442 (`(,_ ,pos . ,_) (goto-char pos) t)
445 (defun sm-c--while-to-do ()
446 "Jump to the matching `do' and return non-nil, if any. Return nil otherwise."
447 (pcase (sm-c--prev-token)
450 (forward-char 1) (backward-sexp 1)
451 (equal (sm-c--prev-token) "do"))
453 (let ((found-do nil))
455 (pcase (smie-backward-sexp)
456 (`(,_ ,pos "do") (goto-char pos) (setq found-do t) nil)
457 (`(,_ ,_ ";") nil) ;Can't find it!
458 (`(,_ ,pos "else") (goto-char pos) (sm-c--else-to-if) t)
460 (goto-char pos) (unless (sm-c--while-to-do) (goto-char pos)) t)
461 (`(t . ,_) nil) ;Can't find it!
462 (`(,_ ,pos . ,_) (goto-char pos) t)
463 (`nil (or (not (looking-at "{"))
464 (smie-rule-prev-p "=")))))
467 (defun sm-c--skip-labels (max)
469 (let ((start (point)))
470 (pcase (sm-c-smie-forward-token)
472 (smie-forward-sexp "case")
473 (forward-comment (point-max))
474 (if (>= (point) max) (progn (goto-char start) nil)
477 (forward-comment (point-max))
478 (if (>= (point) max) (progn (goto-char start) nil)
480 (_ (goto-char start) nil)))))
482 (defun sm-c--boi (&optional inner)
483 "Jump to the beginning-of-instruction.
484 By default for things like nested ifs, it jumps to the outer if, but
485 if INNER is non-nil, it stops at the innermost one."
488 (pcase (smie-backward-sexp)
489 (`(,_ ,_ ";") nil) ;Found it!
490 (`(,_ ,pos "else") (goto-char pos) (sm-c--else-to-if) t)
492 (goto-char pos) (unless (sm-c--while-to-do) (goto-char pos)) t)
493 (`(,(pred numberp) ,pos . ,_) (goto-char pos) t)
494 ((or `nil `(nil . ,_))
495 (if (and (or (not (looking-at "{"))
496 (smie-rule-prev-p "="))
499 (goto-char pos) nil))
500 (`(,_ ,_ ,(or "(" "{" "[")) nil) ;Found it!
503 (or (member tok sm-c-paren-block-keywords)
504 (equal tok "do"))))))
505 (goto-char pos) nil) ;Found it!
506 (`(t ,(pred (eq (point-min))) . ,_) nil)
507 (`(,_ ,pos . ,_) (goto-char pos) t)))))
509 ;; (defun sm-c--if-tail-to-head ()
510 ;; (pcase (sm-c--prev-token)
512 ;; (forward-char 1) (backward-sexp 1)
513 ;; (pcase (sm-c--prev-token)
515 ;; ((or "while" "for") (sm-c--if-tail-to-head))))
516 ;; ("do" (sm-c--if-tail-to-head))))
518 (defun sm-c--boe (tok)
519 (let ((start (point))
520 (res (smie-backward-sexp tok)))
521 (when (member (nth 2 res) '("if" "while" "do" "for" "else"))
522 (when (member (nth 2 res) '("if" "for"))
523 (let ((forward-sexp-function nil))
525 (forward-comment (point-max)))
526 (when (looking-at "{")
527 (let ((forward-sexp-function nil))
529 (forward-comment (point-max)))
530 (if (> (point) start) (goto-char start)))))
532 (defun sm-c-smie--*-token ()
535 (pcase (car (smie-indent-backward-token))
537 ;; Can be a multiplication (as in "(a+b)*c"), or a deref
538 ;; (as in "if (stop) *a = 0;")
539 (if (and (goto-char (nth 1 (syntax-ppss)))
540 (eq ?\( (char-after))
541 (member (smie-default-backward-token) '("if" "for")))
544 ("]" "* mult") ;Multiplication.
545 ((or "(" "[" "{" "}") "* deref")
549 (while (not (or res (bobp)))
550 (pcase (smie-backward-sexp)
551 (`(,_ ,_ ,(or ";" "{")) (setq res "* deref"))
552 ((and `nil (guard (looking-at "{"))) (setq res "* deref"))
554 (if (and (numberp left)
555 (numberp (nth 2 (assoc op smie-grammar)))
556 (< (nth 2 (assoc op smie-grammar))
557 (nth 1 (assoc "* mult" smie-grammar))))
558 (smie-backward-sexp 'halfsexp)
559 (setq res "* mult")))))
563 (defun sm-c-smie-hanging-eolp ()
564 (let ((start (point))
565 (prev (smie-indent-backward-token)))
566 (if (and (not (numberp (nth 1 prev)))
567 (save-excursion (equal (sm-c-smie-backward-token) ";")))
568 ;; Treat instructions that start after ";" as always "hanging".
571 (skip-chars-forward " \t")
573 (forward-comment (point-max))
574 (and (looking-at "\\\\\n")
575 (goto-char (match-end 0)))))
577 (defvar sm-c-smie--inhibit-case/label-rule nil)
579 (defun sm-c--smie-virtual ()
580 (if (and (smie-indent--bolp)
582 (member (sm-c-smie-forward-token)
583 '("case" ":label")))))
585 (let ((sm-c-smie--inhibit-case/label-rule t))
586 (smie-indent-calculate))))
588 (defun sm-c-smie-rules (kind token)
589 (pcase (cons kind token)
590 (`(:elem . basic) sm-c-indent-basic)
591 (`(:list-intro . ";")
594 (if (and (null (smie-forward-sexp))
595 ;; FIXME: Handle \\\n as well!
596 (progn (forward-comment (point-max))
603 `(column . ,(smie-indent-virtual))))
604 (`(:before . "while")
606 (when (sm-c--while-to-do)
607 `(column . ,(smie-indent-virtual)))))
608 (`(:before . ,(or "=" "+=" "-=" "*=" "/=" "%=" "<<=" ">>=" "&=" "^=" "|="))
611 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
612 (smie-indent-virtual)))))
614 (when (and (not (smie-rule-bolp)) (smie-rule-prev-p "else"))
616 (smie-indent-backward-token)
617 `(column . ,(sm-c--smie-virtual)))))
618 ;; (`(:after . ,(or "=" "+=" "-=" "*=" "/=" "%=" "<<=" ">>=" "&=" "^=" "|="))
619 ;; (funcall smie-rules-function :elem 'basic))
622 ((smie-rule-prev-p "=") nil) ;Not a block of instructions!
625 (sm-c--boi 'inner) (sm-c--skip-labels (point-max))
626 (let ((tok (save-excursion (sm-c-smie-forward-token))))
628 ((or (equal tok "typedef")
629 (and (member tok '("enum" "struct"))
630 ;; Make sure that the {...} is about this struct/enum,
631 ;; as opposed to "struct foo *get_foo () {...}"!
633 (smie-indent-forward-token)
634 (smie-indent-forward-token)
635 (forward-comment (point-max))
637 `(column . ,(+ (if (save-excursion
639 (smie-rule-hanging-p))
641 (funcall smie-rules-function :elem 'basic))
642 (smie-indent-virtual))))
643 ((and (member tok '("enum" "struct"))
644 ;; Make sure that the {...} is about this struct/enum, as
645 ;; opposed to "struct foo *get_foo () {...}"!
647 (smie-indent-forward-token)
648 (smie-indent-forward-token)
649 (forward-comment (point-max))
651 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
652 (smie-indent-virtual))))
653 ((or (member tok sm-c-paren-block-keywords)
658 (when (and (> (car (syntax-ppss)) 0)
659 (equal ")" (car (smie-indent-backward-token))))
661 `(column . ,(sm-c--smie-virtual)))))
662 (t `(column . ,(smie-indent-virtual))))))))
663 ((smie-rule-hanging-p)
665 ((smie-rule-prev-p "do" "else")
666 (smie-indent-backward-token))
667 ((smie-rule-prev-p ")")
669 (smie-indent-backward-token))
670 (t (sm-c--boi 'inner)))
671 `(column . ,(sm-c--smie-virtual)))
674 (pcase (sm-c--prev-token)
678 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
679 (smie-indent-virtual))))))
681 (_ (goto-char pos) (sm-c--boi)
683 `(column . ,(sm-c--smie-virtual)))))))))
686 (let ((res (smie-backward-sexp)))
690 ;; (unless (save-excursion
691 ;; (member (sm-c-smie-backward-token)
692 ;; sm-c-paren-block-keywords))
693 ;; `(column . ,(sm-c--smie-virtual)))
695 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
696 (sm-c--smie-virtual)))))))))
699 (funcall smie-rules-function :elem 'basic)))
702 (let ((_ (progn (forward-char 1) (backward-sexp 1)))
704 (prev (sm-c-smie-backward-token)))
706 ((member prev sm-c-paren-block-keywords)
707 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
708 (smie-indent-virtual))))
709 ((and (looking-at "[[:alnum:]_]+(")
712 (and (bobp) (looking-at sm-c--cpp-regexp))))
713 ;; Will be bumped up presumably by the "max" in
714 ;; sm-c--cpp-smie-indent.
716 (t (goto-char pos) `(column . ,(sm-c--smie-virtual)))))))
719 (forward-char 1) (backward-sexp 1)
721 `(column . ,(sm-c--smie-virtual))))
725 `(column . ,(sm-c--smie-virtual))))
726 (`(:after . ":label")
728 `(column . ,(sm-c--smie-virtual)))
729 (`(:after . ": case")
732 (smie-backward-sexp ": case")
733 `(column . ,(sm-c--smie-virtual))))
734 (`(:after . "* deref") `(column . ,(sm-c--smie-virtual)))
735 ((and `(:before . ":label") (guard (not sm-c-smie--inhibit-case/label-rule)))
736 (let ((ppss (syntax-ppss)))
739 (goto-char (nth 1 ppss))
740 `(column . ,(smie-indent-virtual))))))
741 ((and `(:before . "case") (guard (not sm-c-smie--inhibit-case/label-rule)))
743 (dolist (pos (reverse (nth 9 (syntax-ppss))))
746 (and (looking-at "{")
747 (null (car-safe (smie-backward-sexp)))
748 (equal "switch" (sm-c-smie-backward-token))
750 (throw 'found `(column . ,(smie-indent-virtual))))))))))
752 ;;; Backslash alignment
754 (defvar-local sm-c--bs-changed nil)
756 (defun sm-c--bs-after-change (beg end _len)
757 (unless undo-in-progress
758 (if (null sm-c--bs-changed)
759 (setq sm-c--bs-changed (cons beg end))
760 (cl-callf (lambda (x) (min x beg)) (car sm-c--bs-changed))
761 (cl-callf (lambda (x) (max x end)) (cdr sm-c--bs-changed)))))
763 (defun sm-c--bs-realign ()
764 (when sm-c--bs-changed
765 (sm-c--bs-realign-1 (car sm-c--bs-changed) (cdr sm-c--bs-changed))
766 (setq sm-c--bs-changed nil)))
768 (defun sm-c--bs-realign-1 (from to)
772 (unless (zerop (mod (skip-chars-backward "\\\\") 2))
773 (skip-chars-backward " \t")
775 (let ((col (current-column))
778 (progn (setq start (point))
780 (and (< (point) start)
781 (not (zerop (mod (skip-chars-backward "\\\\") 2)))))
782 (skip-chars-backward " \t")
783 (setq col (max (current-column) col)))
786 (progn (setq end (point))
788 (and (> (line-beginning-position) end)
789 (not (zerop (mod (skip-chars-backward "\\\\") 2)))))
790 (skip-chars-backward " \t")
791 (setq col (max (current-column) col)))
794 (unless (or (> (point) end) ;Don't realign if we changed outside!
795 (<= end start)) ;A lone \
797 (setq col (1+ col)) ;Add a space before the backslashes.
800 (while (>= (point) start)
801 (cl-assert (eq (char-before) ?\\))
803 (let ((curcol (current-column)))
805 ((> col curcol) (indent-to col))
807 (move-to-column col t)
808 (delete-region (point)
809 (progn (skip-chars-forward " \t") (point))))))
810 (end-of-line 0)))))))
812 ;;; Font-lock support
814 (defconst sm-c--comment-regexp
815 "/\\(?:/.*\n\\|\\*\\(?:[^*]+\\(?:\\*+[^/*]\\)*\\)*\\*/\\)")
817 (defconst sm-c--defun-regexp
818 (let* ((spc0 (concat "\\(?:\n?[ \t]\\|" sm-c--comment-regexp "\\)*"))
819 (spc1 (concat "\n?[ \t]" spc0))
820 (id "\\(?:\\sw\\|\\s_\\)+"))
821 (cl-flet ((repeat (repetition &rest res)
822 (concat "\\(?:" (apply #'concat res) "\\)"
824 ((pred symbolp) (symbol-name repetition))
828 (repeat '* "\\*" spc0)
829 (repeat '* id (repeat 1 spc1 "\\|" spc0 "\\*" spc0))
830 "\\(" id "\\)[ \t\n]*("
832 "[ \t]*#[ \t]*define[ \t]+\\(?1:" id "\\)("
835 (defconst sm-c-font-lock-keywords
836 `((,sm-c--cpp-regexp (1 font-lock-preprocessor-face))
837 ("\\_<\\(?:true\\|false\\)\\_>" (0 font-lock-constant-face))
838 ("\\_<\\(case\\)\\_>[ \t]*\\([^: \t]+\\)"
839 (1 font-lock-keyword-face)
840 (2 font-lock-constant-face))
841 ("\\(?:[{};]\\(\\)\\|^\\)[ \t]*\\([[:alpha:]_][[:alnum:]_]*\\)[ \t]*:"
842 (2 (if (or (match-beginning 1)
843 (save-excursion (equal ":label" (sm-c-smie-backward-token))))
844 font-lock-constant-face)))
845 (,(let ((kws (delq nil (mapcar (lambda (x)
848 (string-match "\\`[a-z]" x)
850 sm-c-smie-grammar))))
851 (concat "\\_<" (regexp-opt
853 ;; Elements not in SMIE's grammar. Either because
854 ;; they're uninteresting from a parsing point of view,
855 ;; or because SMIE's parsing engine can't handle them
857 '("break" "continue" "struct" "enum" "union" "static")
858 ;; "case" already handled above.
859 (delete "case" kws)))
861 (0 font-lock-keyword-face))
864 (prog1 font-lock-function-name-face
865 (if (< (match-beginning 0) (line-beginning-position))
866 (put-text-property (match-beginning 0) (match-end 0)
867 'font-lock-multiline t)))))))
869 (defconst sm-c--def-regexp
870 (let ((spc0 (concat "\\(?:[ \t\n]\\|" sm-c--comment-regexp "\\)*"))
871 (id "\\(?:\\sw\\|\\s_\\)+"))
872 (concat sm-c--defun-regexp
874 "\\_<\\(?1:\\(?:struct\\|enum\\)[ \t]+" id "\\)" spc0 "{")))
877 (define-derived-mode sm-c-mode prog-mode "smC"
878 "C editing mode based on SMIE."
879 ;; (setq-local font-lock-support-mode nil) ;; To help debugging.
880 (setq-local comment-start "/* ")
881 (setq-local comment-end " */")
882 (setq-local parse-sexp-lookup-properties t)
883 (setq-local open-paren-in-column-0-is-defun-start nil)
884 (setq-local syntax-propertize-function #'sm-c-syntax-propertize)
885 (setq-local font-lock-defaults '(sm-c-font-lock-keywords))
886 (setq-local font-lock-syntactic-face-function #'sm-c-syntactic-face-function)
887 (smie-setup sm-c-smie-grammar #'sm-c-smie-rules
888 :backward-token #'sm-c-smie-backward-token
889 :forward-token #'sm-c-smie-forward-token)
890 ;; FIXME: The stock SMIE forward-sexp-function is not good enough here, since
891 ;; our grammar is much too poor. We should setup another function instead
892 ;; (or ideally teach SMIE to use it).
893 (kill-local-variable 'forward-sexp-function)
894 (add-hook 'smie-indent-functions #'sm-c--cpp-smie-indent nil t)
895 (add-function :after (local 'indent-line-function)
896 #'sm-c--cpp-indent-line)
897 (setq-local smie--hanging-eolp-function #'sm-c-smie-hanging-eolp)
898 ;; Backslash auto-realign.
899 (add-hook 'after-change-functions #'sm-c--bs-after-change nil t)
900 (add-hook 'post-command-hook #'sm-c--bs-realign nil t)
901 (setq-local add-log-current-defun-header-regexp sm-c--def-regexp)
902 (setq-local imenu-generic-expression `((nil ,sm-c--def-regexp 1))))
904 (defun sm-c--cpp-is-not-really-a-comment (&rest args)
905 ;; Without this, placing the region around a CPP directive and hitting
906 ;; M-; would just strip the leading "#" instead of commenting things out.
907 (if (not (derived-mode-p 'sm-c-mode))
909 (let ((parse-sexp-lookup-properties nil))
912 ;; FIXME: Maybe we should change newcomment.el instead; or maybe CPP directives
913 ;; should not be defined as comments, or at least "not always"!
914 (advice-add 'comment-only-p :around #'sm-c--cpp-is-not-really-a-comment)
917 ;;; sm-c-mode.el ends here