reddit / emacs

How to build a language mode from scratch?

Lassogoblin · 2022-04-22 10:29
Hello /r/emacs, I've been an emacs users for a few years now. I know basic elisp, which means I wrote my own config and I can read and modify elisp snippets. I never created a package though. As the title suggest I'd like to write my own major-mode for a programming language, but I don't know anything about this topic, and I don't know where to start either. ​ Can you recommend any resources or guides on writing a language mode from 0? Additionally maybe some simple (few lines of code) but quality codebases for language modes and examples worth studying?
mrprofessor007 · 2022-04-22 10:40
I am sorry I didn't understand your question. Do you want to make a emacs package or a whole new language?
Lassogoblin · 2022-04-22 10:47
Sorry, my question could use some editing. I'm talking about an emacs package for a language. Like 'go-mode', 'haskell-mode' etc.
Salieri_ · 2022-04-22 10:49
There's [this blog post](https://www.omarpolo.com/post/writing-a-major-mode.html), but it's kind of an hassle so maybe using [generic mode](https://www.emacswiki.org/emacs/GenericMode) will be better for you. Also http://emacs-fu.blogspot.com/2010/04/creating-custom-modes-easy-way-with.html
Mal_Dun · 2022-04-22 10:52
I built a language mode once for the programming language Code\_Aster which is derived from Python. See here for the code: [https://github.com/maldun/aster-mode/blob/master/aster-mode.el](https://github.com/maldun/aster-mode/blob/master/aster-mode.el) maybe it gives you an idea how this works as it is a rather simple mode. I personally had just a look at other modes of custom languages. It helps a lot to get some examples.
NoisyFrequency · 2022-04-22 10:57
Because you'll want to parse the language, you will need to find the tooling to do that first. For example in Python, you have the [Abstract syntax tree (ast)](https://docs.python.org/3/library/ast.html) available as a module to parse Python expressions. There's a [cool video by mCoding](https://www.youtube.com/watch?v=OjPT15y2EpE) about using the AST for custom linting. Depending on the language's tools is annoying though, so lots of people have been working on parsers, the most efficient and up-to-date to my knowledge is tree-sitter, [which has Emacs bindings](https://emacs-tree-sitter.github.io/). The other alternative of course is to use regex and it is probably what most modes do, it's easier, doesn't need to use the tools of the language or an external parser (like tree-sitter) and since you "only" need to highlight the relevant information (i.e keywords), you don't need to actually parse the language. For a simple mode, this is most likely the easier route. For example, [zig-mode](https://github.com/ziglang/zig-mode/blob/master/zig-mode.el) which is the mode for the Zig language uses regexes to act on the fonts' faces and highlight the language. So for an Emacs mode, using regular expressions is probably the way to go. If you want to parse a language entirely though, regex is not sufficient and you'll find [lot of reasons](https://stackoverflow.com/questions/5389244/building-a-regex-based-parser) why. N.B: If you look up "building and emacs major mode" you'll find a couple blogs about how to do that as well.
7890yuiop · 2022-04-22 11:05
> examples worth studying? A great many examples are supplied as part of Emacs -- look under `lisp/progmodes/`. If these are not suitable, you should provide details as to why that is, because it's the natural place to start.
NoisyFrequency · 2022-04-22 11:07
I didn't know about generic mode! Thanks for the find
Lassogoblin · 2022-04-22 12:56
Didn't even think about this yet, thanks!
Domse007 · 2022-04-22 12:59
I personally haven't done that, but a very simple example that I can think of is this one: [porth-mode.el](https://gitlab.com/tsoding/porth/-/blob/master/editor/porth-mode.el). The major-mode doesen't do much, but maybe it helps understand the basic concepts.
ieure · 2022-04-22 15:26
> examples worth studying? I created two modes for some obsolete proprietary programming languages. Since the languages are very simple, they make a good study. [9lc-mode](https://github.com/ieure/9lc-mode/blob/master/9lc-mode.el) and [tl1-mode](https://github.com/ieure/tl1-mode/blob/master/tl1-mode.el).