> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/LadybirdBrowser/ladybird/llms.txt
> Use this file to discover all available pages before exploring further.

# Emacs configuration

> Configure Emacs for Ladybird development with lsp-mode, clangd, and clang-format

Emacs can be configured with `lsp-mode` and `clangd` to work well.

## clangd

The official clangd extension can be used for C++ comprehension.

<Note>
  Run cmake (`Meta/ladybird.py run ladybird` or similar) at least once for clangd to work, as doing so will generate the `Build/release/compile_commands.json` that is needed by `clangd`.
</Note>

## lsp-mode

Configure `lsp-mode` with the following settings:

```lisp theme={null}
(use-package lsp-mode
  :hook ((c++-mode) . lsp-deferred)
  :commands lsp
  :config
  (setq lsp-clients-clangd-args '("-j=4" "-background-index" "--log=error" "--clang-tidy" "--enable-config"))
  ;; Optionally, set the location of clangd -- See notes below for options.
  (setq lsp-clangd-binary-path "/usr/bin/clangd"))
```

### clangd installation options

There are a few different ways to specify which clangd to use:

<Steps>
  <Step title="System clangd (default)">
    By default, without configuration `lsp-mode` will try to find and use your system `clangd`. This is the easiest solution, but your system clangd might be out of date.
  </Step>

  <Step title="Manual path specification">
    You can manually specify any `clangd` binary with `lsp-clangd-binary-path`, as shown in the use-package example above.
  </Step>

  <Step title="lsp-mode managed installation">
    You can have `lsp-mode` manage your `clangd` installation with emacs' `lsp-install-server`. This will install a `clangd` binary for you.
  </Step>

  <Step title="Build from Ladybird repository (advanced)">
    You can build the LLVM toolchain, including `clangd`, from Ladybird's repository. This is an advanced option that is not currently documented.
  </Step>
</Steps>

## clang-format

There are multiple packages to handle auto formatting with `clang-format`, within emacs. Choose what works best for your setup:

* [format-all-mode](https://github.com/lassik/emacs-format-all-the-code)
* [clang-format-plus](https://github.com/SavchenkoValeriy/emacs-clang-format-plus)

### Using lsp-mode for formatting

Alternatively, this can be done without additional packages, using `lsp-mode`. You can use the following `.dir-locals.el` file placed in the project root:

```lisp theme={null}
((c++-mode
  (eval add-hook 'before-save-hook #'lsp-format-buffer nil t)))
```

<Tip>
  This configuration will automatically format your code on save using the Ladybird project's `.clang-format` style file.
</Tip>
