This video demonstrates how to enhance Emacs for C++ development by integrating Language Server Protocol (LSP) features, transforming it into a more modern and functional Integrated Development Environment (IDE). The tutorial focuses on using Eglot as the LSP client (built-in Emacs 29+) and Clangd as the C++ language server. It covers the installation and activation of these tools, then showcases key features such as real-time error and warning diagnostics ("red/yellow squigglies"), intelligent code formatting, automatic inclusion of header files, global symbol renaming, and contextual documentation. The video also provides practical advice on configuring Emacs with custom key bindings and hooks for automatic Eglot activation to improve workflow efficiency.
Summarizing Emacs as a C++ IDE with Eglot and Clangd
0:00 Default Emacs Limitations: Emacs' built-in CC mode offers basic features like syntax highlighting but lacks advanced IDE functionalities such as real-time error diagnostics ("red squigglies").
0:29 Introducing Language Servers & LSP: Language servers like Clangd provide features such as code completion, syntax highlighting, and error diagnostics. The Language Server Protocol (LSP) enables communication between these servers and text editors like Emacs.
0:47 Installing Clangd: The first step is to install the Clangd language server for your operating system. For Fedora, it's available via the clang-tools-extra package.
1:02 Activating Eglot in Emacs: Eglot, the LSP client for Emacs (built-in for Emacs 29+, otherwise manual install), is activated by running M-x eglot in a C++ file. This connects Emacs to the Clangd server.
1:53 Eglot vs. LSP Mode: Eglot is preferred for its lean integration of existing Emacs tools and its default inclusion in Emacs 29, while LSP Mode is heavier and more feature-rich than necessary for many users.
2:41 Real-time Error Diagnostics: Once Eglot is connected, Clangd provides immediate feedback on code errors, such as undefined identifiers, shown as "red squigglies" with detailed messages in the mini-buffer.
3:16 Implicit Conversion Warnings: Clangd also highlights potential issues like implicit type conversions (e.g., double to int) with "yellow squigglies," helping to prevent unintended data loss.
5:02 Enabling Unused Variable Warnings: To receive warnings for unused variables, a .clangd configuration file needs to be created in the project root with the compile-flags: add: -Wunused-variable setting. Eglot must then be reconnected (M-x eglot-reconnect) for changes to take effect.
6:13 Renaming Symbols (Global Refactoring): The eglot-rename command allows for global renaming of symbols (e.g., function names). It automatically updates all occurrences of the symbol across relevant files.
7:36 Automatic Include Directives:eglot-code-actions can automatically suggest and insert missing include directives when functions from other files are used, placing them in alphabetical order.
8:30 Code Formatting: The eglot-format command can automatically correct formatting errors, such as ensuring include directives are sorted alphabetically.
8:41 Contextual Documentation: Eglot displays relevant documentation for functions or variables directly in the Emacs mini-buffer when the cursor hovers over them, providing quick reference.
9:06 Setting Key Bindings: To streamline workflow, custom key bindings can be set in the Emacs init file for frequently used Eglot commands like eglot-code-actions, eglot-rename, and eglot-format.
11:35 Basic Code Completion: Eglot offers code completion suggestions (activated with Alt-Tab), although the default interface is described as clunky and warrants a dedicated video for optimization.
12:44 Auto-Starting Eglot with Hooks: A hook can be added to the Emacs init file to automatically start Eglot whenever a C++ file is opened, ensuring an enhanced development environment by default.