User:Schievel/emacs-as-an-ebuild-IDE

= (Doom) Emacs as an Ebuild-IDE = is a very powerful and flexible editor. It is ideal for customizing it into an 'IDE' for Ebuilds. I use Doom Emacs, the Doom in Doom Emacs is a configuration framework for Emacs. It sets certain variables to sane defaults, provides coherent keybindings across packages and has it's own sets of packages that belong together bundled and installable with one command. Read more about Doom Emacs here. .

ebuild-mode
Ebuild mode is the center package of ebuild development on Gentoo. It sits on top of sh mode.

Installation
Ebuild-mode is available on Gentoos main repository as, so install it like any other package with . Ebuild mode package brings in several other modes apart from the ebuild-mode core, which are devbook-mode, gentoo-newsitem-mode and glep-mode. I don't use them, and therefore this article will focus on ebuild-mode itself. Ebuild mode is then installed into, so we have to tell emacs to load elisp from there:

into the config. There are several different ways how emacs loads configs. For Doom this is usually.

Configuration
To make emacs start ebuild-mode every time an .ebuild-File is opened, we should add this to the config as well: {{CodeBox|lang=elisp|1= (add-to-list 'load-path "/usr/share/emacs/site-lisp/ebuild-mode") (autoload 'ebuild-mode "ebuild-mode" "Major mode for Portage .ebuild and .eclass files." t) (autoload 'ebuild-repo-mode "ebuild-mode"  "Minor mode for files in an ebuild repository." t) (autoload 'ebuild-repo-mode-maybe-enable "ebuild-mode") (autoload 'devbook-mode "devbook-mode" "Major mode for editing the Gentoo Devmanual." t) (autoload 'gentoo-newsitem-mode "gentoo-newsitem-mode"  "Major mode for Gentoo GLEP 42 news items." t) (autoload 'glep-mode "glep-mode"  "Major mode for Gentoo Linux Enhancement Proposals." t) (add-to-list 'auto-mode-alist '("\\.\\(ebuild\\|eclass\\)\\'". ebuild-mode)) (add-to-list 'auto-mode-alist '("*.ebuild". ebuild-mode)) (add-to-list 'auto-mode-alist '("/devmanual.*\\.xml\\'". devbook-mode)) (add-to-list 'auto-mode-alist      '("/[0-9]\\{4\\}-[01][0-9]-[0-3][0-9]-.+\\.[a-z]\\{2\\}\\.txt\\'" . gentoo-newsitem-mode)) (add-to-list 'auto-mode-alist '("/glep.*\\.rst\\'". glep-mode)) (add-to-list 'auto-mode-alist      '("/\\(package\\.\\(mask\\|unmask\\{{!}}use\\{{!}}env\ \\{{!}}license\\{{!}}properties\\{{!}}accept_\\(keywords\\{{!}}restrict\\)\\)\ \\{{!}}\\(package\\.\\)?use.\\(stable\\.\\)?\\(force\\{{!}}mask\\)\\)\\'" . conf-space-mode)) (add-to-list 'auto-mode-alist      '("/make\\.\\(conf\\{{!}}}defaults\\)\\'". conf-unix-mode)) (add-to-list 'interpreter-mode-alist '("openrc-run". sh-mode)) (add-to-list 'interpreter-mode-alist '("runscript". sh-mode)) (add-hook 'find-file-hook #'ebuild-repo-mode-maybe-enable) (modify-coding-system-alist 'file "\\.\\(ebuild\\{{!}}eclass\\)\\'" 'utf-8) }}

By convention ebuilds use Tabs, not spaces, for indentation. This is important, because pkgcheck will get upset when we use spaces or indentation. I usually use spaces in shell-Scripts, and since ebuilds are shell-scripts (for emacs at least they are), emacs would insert spaces when I press tab all the time. So I also put this into the config:

Keybindings
By default ebuild mode uses conventional emacs keybindings, you can look them up by pressing  while being in ebuild-mode.

I remapped them so that they are more canonical with Doom Emacs:

For the tag line ebuild mode uses the variables  and. They are present in the default config of Doom Emacs.

ebuild-run-mode
Ebuild run mode provides the functionality to jump directly from an error in the output of  to the location of the code snippet that produced the error.

Installation
Ebuild-run-mode is available in akaters ebuild repository. So activate that using } (if using eselect repository, or run the corresponding layman command) then and install ebuild-run-mode with .

Ebuild-run-mode is installed into, so we need to tell emacs to load elisp from that path:

Ebuild-run-mode sits on top of ebuild-mode, so only run that after ebuild-mode is loaded:

Now, when an ebuild was run using  a buffer with the output will pop up. When there is an error, place the point (the cursor) on it and press return (bound to compile-goto-error) to jump to the line that produces the error in the packages code.

Tag line everywere
Because this is really useful even outside of ebuilds (in patches for example) I bound the  to a key that is always available instead of binding it to a key in ebuild-mode-map. (here it is bound to SPC T)

Environment variables
To test ebuilds we often need to set environment variables like  and   for example. To set them in Emacs we use. To make things a little easier we can define little functions to set sets of environment variables and bind them to keys in ebuild-mode-map or call them with. E.g. for Clang16 bugs I made this:

Call scrub-patch from emacs
According to the Gentoo Dev Manual patches should meet certain QA conditions as well. Therefore we have a neat little program called  which is part of, so we should install that using This thing scrubs the patch thoroughly and all we have to do is insert a short description what this patch is for, references to bugs and our tag. I wrote a function for Emacs to call  on the current buffer or on the marked file in dired, so we don't have to go to the shell that often.

Put that in  (for Doom that is  ). In dired we mark files with  then call. Or, if we have a patch-file open in the currently open buffer, just. After running that function on a currently open patch, reload it from disk. (In Doom that is bound to )

= See also =