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.

If you have any other cool things, helper functions etc. regarding ebuild writing in Emacs, let me know on the discussion page.. I will add that to this page then.

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:

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

For the pkgdev and pkgcheck commands you would need and.

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) }}

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 coherent with Doom Emacs:

For the tag line ebuild mode uses the variables  and. They are present in the default config of Doom Emacs. If you want to use a different name or address for ebuilds than for the rest of your Emacs life (e.g. you have a dedicated mail address for Gentoo related work) set the variables  and.

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.

Little helpers
Put that in  (for Doom that is  ).

Tabs instead of spaces in ebuild-mode
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:

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

This makes sure that a comment sign is only put into that tagline when a comment sign is known. Before when the line was inserted into a .patch-file, it would put  at the start, because .patch-files do not have comment signs. (instead only lines with a space in front are not comments.) You could also use  and   here instead of their ebuild-mode counterparts. For me those are the same so it does not really matter.

I bound that to.

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. In dired we mark files with  then call. Or, if we have a patch-file open in the currently open buffer, just.

Go to build directory of the current ebuild
Sometimes I run into the problem that I want to go to the $WORKDIR of a build. Normally we would either need to type that long path to it or we could run the ebuild and produce an error somehow, than use "ebuild-run-mode" from above to go from that build error to the file. But what I the build has no error or we just want to run the prepare phase to begin writing patches for the package. Therefore I wrote this little function. When it is called with an ebuild in the current buffer, it will open helm-find-file (or ivy or vertico or whatever is used) in the $WORKDIR of that ebuild. Of course at least the unpack-phase must have ran before. Be aware that this one only works in Doom, because we use the Doom specific function  but if your Emacs has a similar function, tinker around with that function it a bit. Also it is assumed that portages tempdir is.

I use that so often, I bound it to a key in ebuild mode:

= See also =