From 6c20cda0221ce97f5f2c1b09bc6c96a914b09a17 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Mon, 5 Dec 2022 10:45:12 -0600 Subject: [PATCH] fixing c++ and eglot systems --- README.org | 33 +++++++++++++++++++++++++++++++-- init.el | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/README.org b/README.org index 5f446d20..052d271b 100644 --- a/README.org +++ b/README.org @@ -2002,7 +2002,36 @@ I'm gonna try this to speed up emacs and make it nicer *** C++ In c++ I just want to make sure lsp is called when enter c++-mode #+BEGIN_SRC emacs-lisp -(add-hook 'c++-mode-hook 'eglot) +(defun astyle-this-buffer () + "Use astyle command to auto format c/c++ code." + (interactive "r") + (let* ((original-point (point))) ;; save original point before processing, thanks to @Scony + (progn + (if (executable-find "astyle") + (shell-command-on-region + (point-min) (point-max) + (concat + "astyle" + " --style=" cc-mode-code-style + " --indent=spaces=" (number-to-string c-basic-offset) + " --pad-oper" + " --pad-header" + " --break-blocks" + " --delete-empty-lines" + " --align-pointer=type" + " --align-reference=name") + (current-buffer) t + (get-buffer-create "*Astyle Errors*") t) + (message "Cannot find binary \"astyle\", please install first.")) + (goto-char original-point)))) ;; restore original point + +(defun astyle-before-save () + "Auto styling before saving." + (interactive) + (when (member major-mode '(cc-mode c++-mode c-mode)) + (astyle-this-buffer))) + +(add-hook 'c-mode-common-hook (lambda () (add-hook 'before-save-hook 'astyle-before-save))) #+END_SRC *** Web @@ -2094,7 +2123,7 @@ Let's give eglot a try. (use-package eglot :commands eglot :hook - (c++-mode . eglot)) + (c++-mode . eglot-ensure)) #+end_src #+begin_src emacs-lisp diff --git a/init.el b/init.el index 5b349cff..ac585915 100644 --- a/init.el +++ b/init.el @@ -1374,7 +1374,36 @@ targets." ) (use-package tree-sitter-langs) -(add-hook 'c++-mode-hook 'eglot) +(defun astyle-this-buffer () + "Use astyle command to auto format c/c++ code." + (interactive "r") + (let* ((original-point (point))) ;; save original point before processing, thanks to @Scony + (progn + (if (executable-find "astyle") + (shell-command-on-region + (point-min) (point-max) + (concat + "astyle" + " --style=" cc-mode-code-style + " --indent=spaces=" (number-to-string c-basic-offset) + " --pad-oper" + " --pad-header" + " --break-blocks" + " --delete-empty-lines" + " --align-pointer=type" + " --align-reference=name") + (current-buffer) t + (get-buffer-create "*Astyle Errors*") t) + (message "Cannot find binary \"astyle\", please install first.")) + (goto-char original-point)))) ;; restore original point + +(defun astyle-before-save () + "Auto styling before saving." + (interactive) + (when (member major-mode '(cc-mode c++-mode c-mode)) + (astyle-this-buffer))) + +(add-hook 'c-mode-common-hook (lambda () (add-hook 'before-save-hook 'astyle-before-save))) (use-package web-mode :config @@ -1408,7 +1437,7 @@ targets." (use-package eglot :commands eglot :hook - (c++-mode . eglot)) + (c++-mode . eglot-ensure)) (use-package consult-eglot)