reddit / emacs

Sharing my first emacs extension: csharpto.el

kromobrn · 2022-02-18 20:03
Hello everybody! I wrote my first emacs extension: [csharpto.el](https://github.com/mabo3n/csharpto.el) Basically it introduces [Evil](https://github.com/emacs-evil/evil) text objects for functions (and now generic "scopes") of the CSharp language. They still don't support the count argument and the current region, but they work fine for the trivial cases and are already saving me lots of keystrokes. Hope they are useful to someone else! Oh, and as I was learning and doing at the same time, I'm pretty sure lots of things can be improved. I'm open for your feedback and suggestions! Thanks. ## A bit of background About 2.5y ago I was using C# and Visual Studio at work, and after playing around with Vim for a few months I got to know this Evil mode thing and decided to give Emacs a try (or more specifically, Spacemacs). Since then, I've been learning how to, and using it in my daily activities. One thing that I always missed is the ability to select a whole function in C#, but I thought I just didn't know the right package or it was something trivial to do ad-hoc. Surprisingly, up to these days I still couldn't find anything. I bumped into the [expand-region](https://github.com/magnars/expand-region.el) package at some point, which is awesome, but still something was missing. After going through the [Emacs Lisp Intro](https://www.gnu.org/software/emacs/manual/eintr.html) tutorial, I learned a bit how to search things in a buffer, and I was also editing csharp files every day, so I decided to try to implement the functions myself. Fast forward some weeks, here I am, with a smile on my face :) ## What I learned I could put my hands on several things I wanted to learn: - Doing things the emacs way: using the standard functions, playing with plists/alists, macros and backquotes, keybindings and keymaps, etc. - Using the edebug: this is essential. I should have learned earlier, would have saved me some headaches. - Learning the rx syntax for regexps: this is surprisingly good! I was reluctant to learn the syntax at first, but now I use it whenever I can. No more backslash hell. But also ended up letting some things aside that I was interested in: - ert: I was going to start writing tests with it, but I skipped the info pages and decide to write a custom, rudimentary test framework for my use cases instead. - Packaging and distributing: I need to learn how to set a CI environment to run tests, and how one publishes a package. - Getting to know more how Evil works: I certainly would have done things differently if I dedicated time to learn Evil better before started coding (but it wouldn't be as funny). - Faces and Overlays: I see these everywhere but don't know how to use them. Maybe next time... Overall I'm very satisfied with my progress, and now I feel much more confident in trying to hack things I want with elisp. Hopefully in the future I will be able to each time make more meaningful contributions to the community.
oantolin · 2022-02-19 14:05
> One thing that I always missed is the ability to select a whole function in C# Emacs has a command called `mark-defun` which is supposed to mark a function. Does it not work in C# mode? Or maybe it does work, but evil has no corresponding text object? EDIT: I installed `csharp-mode` and tried out `mark-defun` and it did mark a method definition. I didn't do extensive testing, but it seems to work fine.
sebhoagie · 2022-02-19 15:43
Hello fellow C#er. For expand-region I had to manually set the expansions of a C-like language or else it worked a bit wonky. https://github.com/sebasmonia/dotfiles/blob/dc5da285048dde44726e0e602153366ba03f6bcc/.emacs/init.el#L246 I've been using more M-h to mark the defun/method/paragraph lately, though. It does work in csharp-mode.
kromobrn · 2022-02-21 03:56
Hey just tested this tonight and it seems to work fine! For functions declared as lambda expressions `=>` it doesn't work though, but there's this `c-mark-function` that seems to address this problem. I saw this before and assumed it didn't work for C#, and now I know why: because Spacemacs automatic rebinds C-M-h to this `c-mark-function` in C# files, but for some reason it is not working in my machine "Cannot find any declaration". Anyway, I'll try to add something to the README soon about this. Thanks for pointing out!
kromobrn · 2022-02-21 04:05
Hum. Interesting, I think Spacemacs' default csharp config does this by default, because the expansions work very "cleverly" for me, it just takes a couple of expansions to get to the whole function when you're under complex statements. THanks for the heads up.