From d83ba452ef6e779adfebe8ba0c579adc7afc0b36 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 26 Feb 2019 13:23:42 -0500 Subject: [PATCH] Refactor +cc-c-c++-objc-mode Improve docstring and a minor correction to C++ detection regexp. --- modules/lang/cc/autoload.el | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/lang/cc/autoload.el b/modules/lang/cc/autoload.el index 0aba0b2dc..4eb371b1a 100644 --- a/modules/lang/cc/autoload.el +++ b/modules/lang/cc/autoload.el @@ -56,7 +56,16 @@ preceded by the opening brace or a comma (disregarding whitespace in between)." ;;;###autoload (defun +cc-c-c++-objc-mode () - "Sets either `c-mode', `objc-mode' or `c++-mode', whichever is appropriate." + "Uses heuristics to detect `c-mode', `objc-mode' or `c++-mode'. + +1. Checks if there are nearby cpp/cc/m/mm files with the same name. +2. Checks for ObjC and C++-specific keywords and libraries. +3. Falls back to `+cc-default-header-file-mode', if set. +4. Otherwise, activates `c-mode'. + +This is meant to replace `c-or-c++-mode' (introduced in Emacs 26.1), which +doesn't support specification of the fallback mode and whose heuristics are +simpler." (let ((base (file-name-sans-extension (buffer-file-name (buffer-base-buffer))))) (cond ((file-exists-p! (or (concat base ".cpp") (concat base ".cc"))) @@ -70,13 +79,11 @@ preceded by the opening brace or a comma (disregarding whitespace in between)." "\\|[-+] ([a-zA-Z0-9_]+)" "\\)"))) (objc-mode)) - ;; NOTE c-or-c++-mode was introduced in Emacs 26.1, but it doesn't - ;; support specification of a default/fallback mode ((+cc--re-search-for (let ((id "[a-zA-Z0-9_]+") (ws "[ \t\r]+") (ws-maybe "[ \t\r]*")) (concat "^" ws-maybe "\\(?:" - "using" ws "\\(?:namespace" ws "std;\\|std::\\)" - "\\|" "namespace" "\\(:?" ws id "\\)?" ws-maybe "{" + "using" ws "\\(?:namespace" ws "std;\\|std::\\)" + "\\|" "namespace" "\\(?:" ws id "\\)?" ws-maybe "{" "\\|" "class" ws id ws-maybe "[:{\n]" "\\|" "template" ws-maybe "<.*>" "\\|" "#include" ws-maybe "<\\(?:string\\|iostream\\|map\\)>"