reddit / emacs

Guide/manual to understand Emacs source-code written in C

NDcodes · 2022-10-17 22:30
I am trying to better understand (and possibly, edit) some of the C files in the latest version of Emacs. Does anybody know of any good resources that go over what the general design patterns are, best practices, and tips on debugging? (Search engines haven't been much help as most combinations of 'emacs' and 'C code' lead to results about configuring Emacs as an IDE for C) I am pretty familiar with Emacs-Lisp, and I can write and understand C pretty well, but I would like to get a good understanding of all the Emacs-specific things I'm seeing in the C code, such as how to go from C variables and structs to Lisp objects, the DEFUN macro, etc. The motivation behind this is that, as noted in [a previous post](https://www.reddit.com/r/emacs/comments/y39uau/xwidgetwebkit_is_a_great_feature_but_it_needs/), I've noticed that the xwidget-webkit feature is (partially) broken in the latest version for MacOS. I would like to try out some possible fixes: it looks like most issues are simply due to the fact that many functions are only defined for the GTK framework, and not Mac's NextStep/Cocoa framework. Also, if anybody wants to help out with this effort, please let me know. ​ UPDATE: 1. if you're interested in understanding Emacs C source-code, take a look at this [section of the official manual](https://www.gnu.org/software/emacs/manual/html_node/elisp/GNU-Emacs-Internals.html): I found the part about [Writing Emacs Primitives](https://www.gnu.org/software/emacs/manual/html_node/elisp/Writing-Emacs-Primitives.html) especially helpful. Many thanks to u/[7890yuiop](https://www.reddit.com/user/7890yuiop/)!!! 2. If you're interested in the xwidget patches I'm working on, I've created [this fork of the emacs mirror](https://github.com/andyjda/emacs/tree/xwidget-patches). I don't know if I'll be able to fix everything, but it's looking good and I've already implemented the estimated-load-progress function
nnreddit-user · 2022-10-18 01:20
Open-source development is a lot like graduate school except there's even less funding and less recognition. Experts, like thesis advisors, can critique -- we don't handhold. I would accept payment for fixing the Cocoa business except having to purchase a Mac is a bridge too far for this cheapskate.
7890yuiop · 2022-10-18 05:12
AFAIK there is no such official guide. Quoting the CONTRIBUTE file: > ** Understanding Emacs internals > > The best way to understand Emacs internals is to read the code. Some > source files, such as xdisp.c, have extensive comments describing the > design and implementation. The following resources may also help: > > * https://www.gnu.org/software/emacs/manual/html_node/elisp/Tips.html > * https://www.gnu.org/software/emacs/manual/html_node/elisp/GNU-Emacs-Internals.html > > or run `(info "(elisp)Tips")` or `(info "(elisp)GNU Emacs Internals")`. > > The file etc/DEBUG describes how to debug Emacs bugs. The latter info node/tree is almost certainly the closest thing to what you're asking about. There's also an etc/NEXTSTEP file with some brief history which you should check, but I'm not sure that's going to help. If you were to continue with your efforts and document things you found important as a newcomer, that could be very valuable as the basis for some kind of primer document, or for improving the info manual.
NDcodes · 2022-10-18 05:37
Thanks a lot! This looks very helpful. I just got started with tinkering around the code, I will take a better look later and see if I can put together a primer document like you suggested
arthurno1 · 2022-10-18 14:58
> I am pretty familiar with Emacs-Lisp, and I can write and understand C pretty well, but I would like to get a good understanding of all the Emacs-specific things I'm seeing in the C code, such as how to go from C variables and structs to Lisp objects, the DEFUN macro, etc. https://emacs-lsp.github.io/lsp-mode/tutorials/CPP-guide/ Once you have configured Emacs to work with Emacs source code, you can jump around and see definitions of macros, functions, what they do etc. If you ar familiar with C, it is not very difficult to understand what things do for the most part, and for the part you don't, look at a similar example if there is one, or ask for the clarification/help on emacs-devel. There is some guide in the manual, which you have already linked to. Pay attention to the "C dialect". If you are familiar with Emacs Lisp, it shouldn't be difficult to understand their macro language. Most of the things do what you think they do. Pay attention on how to call Lisp from C, and C from Lisp, and about data types. Once you have that, rest is just plain old c, more or less. The C sources are relatively well commented, at least important concepts, read them carefully and pay attention to the details. Yes, they have done in Emacs C core what every older textbook on C advises not to do with macros; defined their own language, and cluttered it with endless spaghetti of if-defs, which are of-course not impenetrable, but unfortunately make Emacs C sources more tedious to work with than what they maybe should have been. Once you get used to it, and get the feeling on how the sources are organized and how Emacs works, it is not so bad as is looks at the first sight.
[deleted] · 2022-10-25 20:35
[deleted]