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-ruled-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 the sm-c--while-to-do, sm-c--else-to-if, and sm-c--boi
36 ;; functions into SMIE at some level.
39 ;; - M-; mistakes # for a comment in CPP directives!
40 ;; Ha! As if this was the only/main problem!
47 (defgroup sm-c-mode nil
48 "Major mode to edit C code, based on SMIE."
51 (defcustom sm-c-indent-basic 2
52 "Basic step of indentation.
53 Typically 2 for GNU style and `tab-width' for Linux style."
56 (defcustom sm-c-indent-braces t
57 "If non-nil, braces in if/while/... are indented."
60 ;;; Handling CPP directives.
62 (defsubst sm-c--cpp-inside-p (ppss)
66 (defconst sm-c--cpp-regexp "^[ \t]*\\(\\(#\\)[ \t]*\\([a-z]+\\)\\)"))
68 (defconst sm-c--cpp-syntax-table
69 (let ((st (make-syntax-table)))
70 (modify-syntax-entry ?/ ". 124" st)
71 (modify-syntax-entry ?* ". 23b" st)
72 (modify-syntax-entry ?\n ">" st)
75 (defun sm-c--cpp-goto-end (ppss &optional limit)
76 (cl-assert (sm-c--cpp-inside-p ppss))
79 (and (setq found (re-search-forward "\\(?:\\\\\\\\\\)*\n" limit 'move))
80 ;; We could also check (nth 5 ppss) to figure out if we're
81 ;; after a backslash, but this is a very common case, so it's good
82 ;; to avoid calling parse-partial-sexp for that.
83 (or (eq ?\\ (char-before (match-beginning 0)))
84 (with-syntax-table sm-c--cpp-syntax-table
85 (nth 4 (parse-partial-sexp (1+ (nth 8 ppss)) (point)))))))
88 (defun sm-c--cpp-fontify-syntactically (ppss)
89 ;; FIXME: ¡¡BIG UGLY HACK!!
90 ;; Copied from font-lock.el's font-lock-fontify-syntactically-region.
91 (cl-assert (> (point) (nth 8 ppss)))
94 (sm-c--cpp-goto-end ppss)
95 (narrow-to-region (1+ (nth 8 ppss)) (point))
96 ;; FIXME: We should add some "with-local-syntax-ppss" macro to
98 (let ((syntax-propertize-function nil)
99 (syntax-ppss-cache nil)
100 (syntax-ppss-last nil))
101 (font-lock-fontify-syntactically-region (point-min) (point-max))))))
103 (defun sm-c--cpp-syntax-propertize (end)
104 (let ((ppss (syntax-ppss))
106 (when (sm-c--cpp-inside-p ppss)
108 (and (setq found (re-search-forward "\\(\\\\\\\\\\)*\n" end 'move))
109 (or (eq ?\\ (char-before (match-beginning 0)))
110 (with-syntax-table sm-c--cpp-syntax-table
111 (nth 4 (parse-partial-sexp (1+ (nth 8 ppss)) (point)))))))
113 (put-text-property (1- (point)) (point)
114 'syntax-table (string-to-syntax "> c"))))))
116 ;;;; Indenting CPP directives.
118 (defcustom sm-c-indent-cpp-basic 1
119 "Indent step for CPP directives."
122 (defun sm-c--cpp-prev (tok)
125 (when (re-search-backward sm-c--cpp-regexp nil t)
126 (pcase (cons tok (match-string 3))
127 (`(,_ . "endif") (sm-c--cpp-prev "endif"))
128 ((or `(,(or "endif" "else" "elif") . ,(or "if" "ifdef" "ifndef"))
129 `(,(or "else" "elif") . "elif"))
131 (`(,(or "endif" "else" "elif") . ,_) nil)
132 (`(,_ . ,(or "if" "ifdef" "ifndef" "elif" "else"))
133 (setq offset sm-c-indent-cpp-basic))
137 (goto-char (match-beginning 3))
138 (+ offset (current-column)))))
141 (defun sm-c--cpp-indent-line (&optional _arg)
142 ;; FIXME: Also align the terminating \, if any.
143 (when (> sm-c-indent-cpp-basic 0)
144 (let* ((pos (point-marker))
149 (when (looking-at sm-c--cpp-regexp)
150 (setq beg (match-beginning 3))
151 (or (sm-c--cpp-prev (match-string 3)) 0)))))
153 (let ((before (<= pos beg)))
155 (unless (= (current-column) indent)
156 (skip-chars-backward " \t")
157 (delete-region (point)
158 (progn (skip-chars-forward " \t") (point)))
160 (unless before (goto-char pos)))))))
162 ;;;; Indenting inside CPP #define.
164 (defconst sm-c--cpp-smie-indent-functions
165 ;; FIXME: Don't just align line after #define with the "d"!
169 ((eq f #'smie-indent-comment-inside) #'sm-c--cpp-indent-comment-inside)
170 ;; ((eq f #'smie-indent-exps) #'sm-c--cpp-indent-exps)
172 (default-value 'smie-indent-functions)))
174 (defun sm-c--cpp-indent-comment-inside ()
175 (let ((ppss (syntax-ppss)))
177 ;; Indicate where's the comment start.
178 `(noindent . ,(nth 8 ppss)))))
180 (defun sm-c--cpp-smie-indent ()
181 (let ((ppss (syntax-ppss)))
183 ((sm-c--cpp-inside-p ppss)
185 (narrow-to-region (nth 8 ppss) (point-max))
187 (let ((smie-indent-functions sm-c--cpp-smie-indent-functions)
188 (syntax-ppss-cache nil)
189 (syntax-ppss-last nil)
190 (parse-sexp-lookup-properties nil))
191 (smie-indent-calculate))))
192 (if (not (eq 'noindent (car-safe indent)))
193 (if (integerp indent)
194 (max (funcall smie-rules-function :elem 'basic) indent)
196 ;; We can't just return `noindent' if we're inside a comment,
197 ;; because the indent.el code would then be similarly confused,
198 ;; thinking the `noindent' is because we're inside the cpp
199 ;; pseudo-comment, and would hence align the code with the content
200 ;; of the psuedo-comment rather than the nested real comment!
202 ;; FIXME: Copy&paste from indent--default-inside-comment.
203 ;; FIXME: This will always re-indent inside these comments, even
204 ;; during indent-region.
207 (skip-chars-forward " \t")
208 (when (< (1- (point)) (cdr indent) (line-end-position))
209 (goto-char (cdr indent))
210 (when (looking-at comment-start-skip)
211 (goto-char (match-end 0))))
212 (current-column))))))
214 ((equal (syntax-after (point)) (string-to-syntax "< c")) 0)
219 (defvar sm-c-mode-syntax-table
220 (let ((st (make-syntax-table)))
221 (modify-syntax-entry ?/ ". 124" st)
222 (modify-syntax-entry ?* ". 23b" st)
223 (modify-syntax-entry ?\n ">" st)
224 (modify-syntax-entry ?\" "\"" st)
225 (modify-syntax-entry ?\' "\"" st)
226 (modify-syntax-entry ?= "." st)
227 (modify-syntax-entry ?< "." st)
228 (modify-syntax-entry ?> "." st)
231 (defun sm-c-syntax-propertize (start end)
233 (sm-c--cpp-syntax-propertize end)
235 (syntax-propertize-rules
236 (sm-c--cpp-regexp (2 (prog1 "< c" (sm-c--cpp-syntax-propertize end)))))
239 (defun sm-c-syntactic-face-function (ppss)
240 (if (sm-c--cpp-inside-p ppss)
241 (prog1 nil (sm-c--cpp-fontify-syntactically ppss))
242 (funcall (default-value 'font-lock-syntactic-face-function) ppss)))
246 (defconst sm-c-paren-block-keywords '("if" "while" "for" "switch"))
248 (defconst sm-c-smie-precedence-table
250 ;; Compiled from https://en.wikipedia.org/wiki/Operators_in_C_and_C++.
252 ;; (nonassoc "throw")
253 (nonassoc "=" "+=" "-=" "*=" "/=" "%=" "<<=" ">>=" "&=" "^=" "|=") ;2
254 ;; (nonassoc "?" ":") ;; Better handle it in the BNF.
259 ;; (assoc "&") ;; Binary and. Confused with address-of.
260 (nonassoc "==" "!=") ;7
261 (nonassoc "<" "<=" ">" ">=") ;8
262 (nonassoc "<<" ">>") ;9
264 (assoc "/" "* mult" "%") ;11
265 ;; (nonassoc ".*" "->*") ;12 ;; Only C++
266 ;; (nonassoc "++" "--" "+" "-" "!" "~" "(type)" "*" "&"
267 ;; "sizeof" "new" "delete");13 ;; All prefix.
268 (left "." "->") ;; "++" "--" suffixes, "()", "[]", "typeid", "*_cast". ;14
269 ;; (noassoc "::") ;; Only C++
272 (defconst sm-c-smie-grammar
273 ;; `((: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))
278 '((decls ("typedef" decl) ("extern" decl)
282 (insts ("{" insts "}")
287 ("case" subexp ": case")
289 (exp-if ("if" exp) ("do" exp) ("while" exp) ("switch" exp) ("for" exp)
291 (exp ("(" exp ")") (exp "," exp) (subexp "?" exp ":" exp))
292 (subexp (subexp "||" subexp))
293 ;; Some of the precedence table deals with pre/postfixes, which
294 ;; smie-precs->prec2 can't handle, so handle it here instead.
295 (exp11 (exp12) (exp11 "/" exp11))
296 (exp12 (exp13)) ;C++ only.
297 (exp13 (exp14) ("++ prefix" exp13) ("-- prefix" exp13)
298 ("!" exp13) ("~" exp13) ("&" exp13) ("* deref" exp13))
299 (exp14 (id) (exp14 "++ postfix") (exp14 "-- postfix")
300 (exp14 "->" id) (exp14 "." id)))
301 '((assoc ";") (assoc ",") (nonassoc "?" ":"))
302 sm-c-smie-precedence-table)
303 (smie-precs->prec2 sm-c-smie-precedence-table)
304 (smie-precs->prec2 '((nonassoc ";") (nonassoc ":")))))))
305 ;; SMIE gives (":label" 261 262), but really this could just as well be
306 ;; (":label" nil nil) because labels don't have any argument to their left
307 ;; or right. They're like both openers and closers at the same time.
309 (if (equal (car-safe x) ":label")
310 ;; Rather than (":label" (n1) (n2)) we use
311 ;; (":label" (n1) n2) because SMIE otherwise complains:
312 ;; cl--assertion-failed((numberp (funcall op-forw toklevels)))
313 ;; in smie-next-sexp.
314 `(,(nth 0 x) (,(nth 1 x)) ,(nth 2 x)) x))
317 ;; (defun sm-c--:-discriminate ()
319 ;; (and (null (smie-backward-sexp))
320 ;; (let ((prev (smie-indent-backward-token)))
322 ;; ((equal prev "case" ) ": case")
323 ;; ((member prev '(";" "{" "}")) ":-label")
326 (defconst sm-c-smie-operator-regexp
328 (pcase-dolist (`(,token . ,_) sm-c-smie-grammar)
329 (when (and (stringp token) (string-match "\\`[^ [:alnum:](){}]+" token))
330 (push (match-string 0 token) ops)))
333 (defun sm-c-smie-forward-token ()
334 (forward-comment (point-max))
335 (let ((tok (if (looking-at sm-c-smie-operator-regexp)
336 (progn (goto-char (match-end 0)) (match-string 0))
337 (smie-default-forward-token))))
339 ((and (equal tok "") (looking-at "\\\\\n"))
340 (goto-char (match-end 0))
341 (sm-c-smie-forward-token))
342 ((member tok '(":" "*"))
343 (save-excursion (sm-c-smie-backward-token)))
344 ((looking-at "[ \t]*:")
345 (if (not (equal (save-excursion (sm-c-smie-forward-token)) ":label"))
347 (looking-at "[ \t]*:")
348 (goto-char (match-end 0)) ":label"))
352 (defun sm-c-smie-backward-token ()
353 (forward-comment (- (point)))
354 (let ((tok (if (looking-back sm-c-smie-operator-regexp (- (point) 3) t)
355 (progn (goto-char (match-beginning 0)) (match-string 0))
356 (smie-default-backward-token))))
358 ((and (equal tok "") (looking-at "\n"))
360 (if (not (= 0 (mod (skip-chars-backward "\\\\") 2)))
361 (sm-c-smie-backward-token)
364 ((equal tok "*") (sm-c-smie--*-token))
367 (prev (sm-c-smie-backward-token)))
368 (if (zerop (length prev))
369 (progn (goto-char pos1) tok)
370 (let ((pos2 (point)))
371 (pcase (car (smie-indent-backward-token))
372 ("case" (goto-char pos1) ": case")
373 ((or ";" "{" "}") (goto-char pos2) ":label")
374 (_ (goto-char pos1) tok))))))
377 (defun sm-c--prev-token ()
378 (car (smie-indent-backward-token)))
380 (defun sm-c--else-to-if ()
382 (unless (equal (sm-c--prev-token) ";")
385 (pcase (smie-backward-sexp)
386 (`(,_ ,pos "if") (goto-char pos) nil) ;Found it!
387 (`(,_ ,_ ";") nil) ;Can't find it!
388 (`(,_ ,pos "else") (goto-char pos) (sm-c--else-to-if) t)
390 (goto-char pos) (unless (sm-c--while-to-do) (goto-char pos)) t)
391 (`(t . ,_) nil) ;Can't find it!
392 (`(,_ ,pos . ,_) (goto-char pos) t)
395 (defun sm-c--while-to-do ()
396 "Jump to the matching `do' and return non-nil, if any. Return nil otherwise."
397 (pcase (sm-c--prev-token)
400 (forward-char 1) (backward-sexp 1)
401 (equal (sm-c--prev-token) "do"))
403 (let ((found-do nil))
405 (pcase (smie-backward-sexp)
406 (`(,_ ,pos "do") (goto-char pos) (setq found-do t) nil)
407 (`(,_ ,_ ";") nil) ;Can't find it!
408 (`(,_ ,pos "else") (goto-char pos) (sm-c--else-to-if) t)
410 (goto-char pos) (unless (sm-c--while-to-do) (goto-char pos)) t)
411 (`(t . ,_) nil) ;Can't find it!
412 (`(,_ ,pos . ,_) (goto-char pos) t)
413 (`nil (or (not (looking-at "{"))
414 (smie-rule-prev-p "=")))))
417 (defun sm-c--skip-labels (max)
419 (let ((start (point)))
420 (pcase (sm-c-smie-forward-token)
422 (smie-forward-sexp "case")
423 (forward-comment (point-max))
424 (if (>= (point) max) (progn (goto-char start) nil)
427 (forward-comment (point-max))
428 (if (>= (point) max) (progn (goto-char start) nil)
430 (_ (goto-char start) nil)))))
432 (defun sm-c--boi (&optional inner)
433 "Jump to the beginning-of-instruction.
434 By default for things like nested ifs, it jumps to the outer if, but
435 if INNER is non-nil, it stops at the innermost one."
438 (pcase (smie-backward-sexp)
439 (`(,_ ,_ ";") nil) ;Found it!
440 (`(,_ ,pos "else") (goto-char pos) (sm-c--else-to-if) t)
442 (goto-char pos) (unless (sm-c--while-to-do) (goto-char pos)) t)
443 (`(,(pred numberp) ,pos . ,_) (goto-char pos) t)
444 ((or `nil `(nil . ,_))
445 (if (and (or (not (looking-at "{"))
446 (smie-rule-prev-p "="))
449 (goto-char pos) nil))
450 (`(,_ ,_ ,(or "(" "{" "[")) nil) ;Found it!
453 (or (member tok sm-c-paren-block-keywords)
454 (equal tok "do"))))))
455 (goto-char pos) nil) ;Found it!
456 (`(t ,(pred (eq (point-min))) . ,_) nil)
457 (`(,_ ,pos . ,_) (goto-char pos) t)))))
459 ;; (defun sm-c--if-tail-to-head ()
460 ;; (pcase (sm-c--prev-token)
462 ;; (forward-char 1) (backward-sexp 1)
463 ;; (pcase (sm-c--prev-token)
465 ;; ((or "while" "for") (sm-c--if-tail-to-head))))
466 ;; ("do" (sm-c--if-tail-to-head))))
468 (defun sm-c--boe (tok)
469 (let ((start (point))
470 (res (smie-backward-sexp tok)))
471 (when (member (nth 2 res) '("if" "while" "do" "for" "else"))
472 (when (member (nth 2 res) '("if" "for"))
473 (let ((forward-sexp-function nil))
475 (forward-comment (point-max)))
476 (when (looking-at "{")
477 (let ((forward-sexp-function nil))
479 (forward-comment (point-max)))
480 (if (> (point) start) (goto-char start)))))
482 (defun sm-c-smie--*-token ()
485 (pcase (car (smie-indent-backward-token))
487 ;; Can be a multiplication (as in "(a+b)*c"), or a deref
488 ;; (as in "if (stop) *a = 0;")
489 (if (and (goto-char (nth 1 (syntax-ppss)))
490 (eq ?\( (char-after))
491 (member (smie-default-backward-token) '("if" "for")))
494 ("]" "* mult") ;Multiplication.
495 ((or "(" "[" "{" "}") "* deref")
498 (pcase (smie-backward-sexp "* mult")
499 (`(,_ ,_ ,(or ";" "{")) "* deref")
503 (defun sm-c-smie-hanging-eolp ()
504 (let ((start (point))
505 (prev (smie-indent-backward-token)))
506 (if (and (not (numberp (nth 1 prev)))
507 (save-excursion (equal (sm-c-smie-backward-token) ";")))
508 ;; Treat instructions that start after ";" as always "hanging".
511 (skip-chars-forward " \t")
513 (forward-comment (point-max))
514 (and (looking-at "\\\\\n")
515 (goto-char (match-end 0)))))
517 (defvar sm-c-smie--inhibit-case/label-rule nil)
519 (defun sm-c--smie-virtual ()
520 (if (and (smie-indent--bolp)
522 (member (sm-c-smie-forward-token)
523 '("case" ":label")))))
525 (let ((sm-c-smie--inhibit-case/label-rule t))
526 (smie-indent-calculate))))
528 (defun sm-c-smie-rules (kind token)
529 (pcase (cons kind token)
530 (`(:elem . basic) sm-c-indent-basic)
531 (`(:list-intro . ";")
534 (if (and (null (smie-forward-sexp))
535 ;; FIXME: Handle \\\n as well!
536 (progn (forward-comment (point-max))
543 `(column . ,(smie-indent-virtual))))
544 (`(:before . "while")
546 (when (sm-c--while-to-do)
547 `(column . ,(smie-indent-virtual)))))
548 (`(:before . ,(or "=" "+=" "-=" "*=" "/=" "%=" "<<=" ">>=" "&=" "^=" "|="))
551 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
552 (smie-indent-virtual)))))
554 (when (and (not (smie-rule-bolp)) (smie-rule-prev-p "else"))
556 (smie-indent-backward-token)
557 `(column . ,(sm-c--smie-virtual)))))
558 ;; (`(:after . ,(or "=" "+=" "-=" "*=" "/=" "%=" "<<=" ">>=" "&=" "^=" "|="))
559 ;; (funcall smie-rules-function :elem 'basic))
562 ((smie-rule-prev-p "=") nil) ;Not a block of instructions!
565 (sm-c--boi 'inner) (sm-c--skip-labels (point-max))
566 (let ((tok (save-excursion (sm-c-smie-forward-token))))
568 ((member tok '("typedef")) ; "enum" "struct"
569 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
570 (smie-indent-virtual))))
571 ((or (member tok sm-c-paren-block-keywords)
576 (when (and (> (car (syntax-ppss)) 0)
577 (equal ")" (car (smie-indent-backward-token))))
579 `(column . ,(sm-c--smie-virtual)))))
580 (t `(column . ,(smie-indent-virtual))))))))
581 ((smie-rule-hanging-p)
583 ((smie-rule-prev-p "do" "else")
584 (smie-indent-backward-token))
585 ((smie-rule-prev-p ")")
587 (smie-indent-backward-token))
588 (t (sm-c--boi 'inner)))
589 `(column . ,(sm-c--smie-virtual)))
592 (pcase (sm-c--prev-token)
596 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
597 (smie-indent-virtual))))))
599 (_ (goto-char pos) (sm-c--boi)
601 `(column . ,(sm-c--smie-virtual)))))))))
604 (let ((res (smie-backward-sexp)))
608 ;; (unless (save-excursion
609 ;; (member (sm-c-smie-backward-token)
610 ;; sm-c-paren-block-keywords))
611 ;; `(column . ,(sm-c--smie-virtual)))
613 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
614 (sm-c--smie-virtual)))))))))
617 (funcall smie-rules-function :elem 'basic)))
620 (let ((_ (progn (forward-char 1) (backward-sexp 1)))
622 (prev (sm-c-smie-backward-token)))
624 ((member prev sm-c-paren-block-keywords)
625 `(column . ,(+ (funcall smie-rules-function :elem 'basic)
626 (smie-indent-virtual))))
627 ((and (looking-at "[[:alnum:]_]+(")
630 (and (bobp) (looking-at sm-c--cpp-regexp))))
631 ;; Will be bumped up presumably by the "max" in
632 ;; sm-c--cpp-smie-indent.
634 (t (goto-char pos) `(column . ,(sm-c--smie-virtual)))))))
637 (forward-char 1) (backward-sexp 1)
639 `(column . ,(sm-c--smie-virtual))))
643 `(column . ,(sm-c--smie-virtual))))
644 (`(:after . ":label")
646 `(column . ,(sm-c--smie-virtual)))
647 (`(:after . ": case")
650 (smie-backward-sexp ": case")
651 `(column . ,(sm-c--smie-virtual))))
652 (`(:after . "* deref") `(column . ,(sm-c--smie-virtual)))
653 ((and `(:before . ":label") (guard (not sm-c-smie--inhibit-case/label-rule)))
654 (let ((ppss (syntax-ppss)))
657 (goto-char (nth 1 ppss))
658 `(column . ,(smie-indent-virtual))))))
659 ((and `(:before . "case") (guard (not sm-c-smie--inhibit-case/label-rule)))
661 (dolist (pos (reverse (nth 9 (syntax-ppss))))
664 (and (looking-at "{")
665 (null (car-safe (smie-backward-sexp)))
666 (equal "switch" (sm-c-smie-backward-token))
668 (throw 'found `(column . ,(smie-indent-virtual))))))))))
670 ;;; Backslash alignment
672 (defvar-local sm-c--bs-changed nil)
674 (defun sm-c--bs-after-change (beg end _len)
675 (unless undo-in-progress
676 (if (null sm-c--bs-changed)
677 (setq sm-c--bs-changed (cons beg end))
678 (cl-callf (lambda (x) (min x beg)) (car sm-c--bs-changed))
679 (cl-callf (lambda (x) (max x end)) (cdr sm-c--bs-changed)))))
681 (defun sm-c--bs-realign ()
682 (when sm-c--bs-changed
683 (sm-c--bs-realign-1 (car sm-c--bs-changed) (cdr sm-c--bs-changed))
684 (setq sm-c--bs-changed nil)))
686 (defun sm-c--bs-realign-1 (from to)
690 (unless (zerop (mod (skip-chars-backward "\\\\") 2))
691 (skip-chars-backward " \t")
692 (let ((col (current-column))
695 (progn (setq start (point))
697 (and (< (point) start)
698 (not (zerop (mod (skip-chars-backward "\\\\") 2)))))
699 (skip-chars-backward " \t")
700 (setq col (max (current-column) col)))
703 (progn (setq end (point))
705 (and (> (point) start)
706 (not (zerop (mod (skip-chars-backward "\\\\") 2)))))
707 (skip-chars-backward " \t")
708 (setq col (max (current-column) col)))
711 (unless (or (> (point) end) ;Don't realign if we changed outside!
712 (< end start)) ;A lone \
714 (setq col (1+ col)) ;Add a space before the backslashes.
717 (while (>= (point) start)
718 (cl-assert (eq (char-before) ?\\))
720 (let ((curcol (current-column)))
722 ((> col curcol) (indent-to col))
724 (move-to-column col t)
725 (delete-region (point)
726 (progn (skip-chars-forward " \t") (point))))))
727 (end-of-line 0)))))))
729 ;;; Font-lock support
731 (defconst sm-c-font-lock-keywords
732 `((,sm-c--cpp-regexp (1 font-lock-preprocessor-face))
733 ("\\_<\\(?:true\\|false\\)\\_>" (0 font-lock-constant-face))
734 ("\\_<\\(case\\)\\_>[ \t]*\\([^: \t]+\\)"
735 (1 font-lock-keyword-face)
736 (2 font-lock-constant-face))
737 ("\\(?:[{};]\\(\\)\\|^\\)[ \t]*\\([[:alpha:]_][[:alnum:]_]*\\)[ \t]*:"
738 (2 (if (or (match-beginning 1)
739 (save-excursion (equal ":label" (sm-c-smie-backward-token))))
740 font-lock-constant-face)))
741 (,(let ((kws (delq nil (mapcar (lambda (x)
744 (string-match "\\`[a-z]" x)
746 sm-c-smie-grammar))))
747 (concat "\\_<" (regexp-opt
749 ;; Elements not in SMIE's grammar. Either because
750 ;; they're uninteresting from a parsing point of view,
751 ;; or because SMIE's parsing engine can't handle them
753 '("break" "continue" "struct" "enum" "union" "static")
754 ;; "case" already handled above.
755 (delete "case" kws)))
757 (0 font-lock-keyword-face))
758 (,(let* ((spc0 "\\(?:\n?[ \t]\\|/\\*.*?\\*/\\)*")
759 (spc1 (concat "\n?[ \t]" spc0))
760 (id "\\(?:\\sw\\|\\s_\\)+"))
761 (cl-flet ((repeat (repetition &rest res)
762 (concat "\\(?:" (apply #'concat res) "\\)"
764 ((pred symbolp) (symbol-name repetition))
768 (repeat '* "\\*" spc0)
769 (repeat '* id (repeat 1 spc1 "\\|" spc0 "\\*" spc0))
770 "\\(" id "\\)[ \t\n]*(")))
772 (prog1 font-lock-function-name-face
773 (if (< (match-beginning 0) (line-beginning-position))
774 (put-text-property (match-beginning 0) (match-end 0)
775 'font-lock-multiline t)))))))
779 (define-derived-mode sm-c-mode prog-mode "smC"
780 "C editing mode based on SMIE."
781 ;; (setq-local font-lock-support-mode nil) ;; To help debugging.
782 (setq-local comment-start "/* ")
783 (setq-local comment-end " */")
784 (setq-local parse-sexp-lookup-properties t)
785 (setq-local open-paren-in-column-0-is-defun-start nil)
786 (setq-local syntax-propertize-function #'sm-c-syntax-propertize)
787 (setq-local font-lock-defaults '(sm-c-font-lock-keywords))
788 (setq-local font-lock-syntactic-face-function #'sm-c-syntactic-face-function)
789 (smie-setup sm-c-smie-grammar #'sm-c-smie-rules
790 :backward-token #'sm-c-smie-backward-token
791 :forward-token #'sm-c-smie-forward-token)
792 ;; FIXME: The stock SMIE forward-sexp-function is not good enough here, since
793 ;; our grammar is much too poor. We should setup another function instead
794 ;; (or ideally teach SMIE to use it).
795 (kill-local-variable 'forward-sexp-function)
796 (add-hook 'smie-indent-functions #'sm-c--cpp-smie-indent nil t)
797 (add-function :after (local 'indent-line-function)
798 #'sm-c--cpp-indent-line)
799 (setq-local smie--hanging-eolp-function #'sm-c-smie-hanging-eolp)
800 ;; Backslash auto-realign.
801 (add-hook 'after-change-functions #'sm-c--bs-after-change nil t)
802 (add-hook 'post-command-hook #'sm-c--bs-realign nil t))
805 ;;; sm-c-mode.el ends here