X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/7e76d0968317ff83b6310e9fed4d10de0fd05403..2670a396603bf44935e8ff1d3d2dead43c9e4c3c:/packages/sm-c-mode/sm-c-mode.el diff --git a/packages/sm-c-mode/sm-c-mode.el b/packages/sm-c-mode/sm-c-mode.el index 72cd75ef0..54b9a54ee 100644 --- a/packages/sm-c-mode/sm-c-mode.el +++ b/packages/sm-c-mode/sm-c-mode.el @@ -640,6 +640,16 @@ if INNER is non-nil, it stops at the innermost one." 0 (funcall smie-rules-function :elem 'basic)) (smie-indent-virtual)))) + ((and (member tok '("enum" "struct")) + ;; Make sure that the {...} is about this struct/enum, as + ;; opposed to "struct foo *get_foo () {...}"! + (save-excursion + (smie-indent-forward-token) + (smie-indent-forward-token) + (forward-comment (point-max)) + (>= (point) pos))) + `(column . ,(+ (funcall smie-rules-function :elem 'basic) + (smie-indent-virtual)))) ((or (member tok sm-c-paren-block-keywords) (equal tok "do")) nil) @@ -801,6 +811,27 @@ if INNER is non-nil, it stops at the innermost one." ;;; Font-lock support +(defconst sm-c--comment-regexp + "/\\(?:/.*\n\\|\\*\\(?:[^*]+\\(?:\\*+[^/*]\\)*\\)*\\*/\\)") + +(defconst sm-c--defun-regexp + (let* ((spc0 (concat "\\(?:\n?[ \t]\\|" sm-c--comment-regexp "\\)*")) + (spc1 (concat "\n?[ \t]" spc0)) + (id "\\(?:\\sw\\|\\s_\\)+")) + (cl-flet ((repeat (repetition &rest res) + (concat "\\(?:" (apply #'concat res) "\\)" + (pcase repetition + ((pred symbolp) (symbol-name repetition)) + (1 ""))))) + (concat + "^\\(?:" + (repeat '* "\\*" spc0) + (repeat '* id (repeat 1 spc1 "\\|" spc0 "\\*" spc0)) + "\\(" id "\\)[ \t\n]*(" + "\\|" + "[ \t]*#[ \t]*define[ \t]+\\(?1:" id "\\)(" + "\\)")))) + (defconst sm-c-font-lock-keywords `((,sm-c--cpp-regexp (1 font-lock-preprocessor-face)) ("\\_<\\(?:true\\|false\\)\\_>" (0 font-lock-constant-face)) @@ -828,25 +859,19 @@ if INNER is non-nil, it stops at the innermost one." (delete "case" kws))) "\\_>")) (0 font-lock-keyword-face)) - (,(let* ((spc0 "\\(?:\n?[ \t]\\|/\\*.*?\\*/\\)*") - (spc1 (concat "\n?[ \t]" spc0)) - (id "\\(?:\\sw\\|\\s_\\)+")) - (cl-flet ((repeat (repetition &rest res) - (concat "\\(?:" (apply #'concat res) "\\)" - (pcase repetition - ((pred symbolp) (symbol-name repetition)) - (1 ""))))) - (concat - "^" - (repeat '* "\\*" spc0) - (repeat '* id (repeat 1 spc1 "\\|" spc0 "\\*" spc0)) - "\\(" id "\\)[ \t\n]*("))) + (,sm-c--defun-regexp (1 (prog1 font-lock-function-name-face (if (< (match-beginning 0) (line-beginning-position)) (put-text-property (match-beginning 0) (match-end 0) 'font-lock-multiline t))))))) +(defconst sm-c--def-regexp + (let ((spc0 (concat "\\(?:[ \t\n]\\|" sm-c--comment-regexp "\\)*")) + (id "\\(?:\\sw\\|\\s_\\)+")) + (concat sm-c--defun-regexp + "\\|" + "\\_<\\(?1:\\(?:struct\\|enum\\)[ \t]+" id "\\)" spc0 "{"))) ;;;###autoload (define-derived-mode sm-c-mode prog-mode "smC" @@ -872,7 +897,9 @@ if INNER is non-nil, it stops at the innermost one." (setq-local smie--hanging-eolp-function #'sm-c-smie-hanging-eolp) ;; Backslash auto-realign. (add-hook 'after-change-functions #'sm-c--bs-after-change nil t) - (add-hook 'post-command-hook #'sm-c--bs-realign nil t)) + (add-hook 'post-command-hook #'sm-c--bs-realign nil t) + (setq-local add-log-current-defun-header-regexp sm-c--def-regexp) + (setq-local imenu-generic-expression `((nil ,sm-c--def-regexp 1)))) (defun sm-c--cpp-is-not-really-a-comment (&rest args) ;; Without this, placing the region around a CPP directive and hitting