James Dyer and I both ran into the same workflow snag when fixing source indentation. He explains it best:
- You’re working in a file with inconsistent indentation
- You want to fix the entire buffer’s formatting
- You run C-x h (select all) followed by M-x indent-region
- Your mark is now at the beginning of the buffer, disrupting your workflow
Naturally, this is Emacs and were both able to patch our editor to smoothen things out.
While I ran into the same indent-region snag after selecting an entire buffer (via mark-whole-buffer), I also faced it with mark-defun and er/expand-region (awesome package btw).
With three cases in mind, I wanted a somewhat generic solution, so I built diverted.el, a little minor mode to identify these momentary diversions and try to bring the point back to the original location. Mind you, this was back in 2019 and until James's post, I hadn't heard of anyone else running into a similar snag. Since then, I kept the package as part of my config. James's post gave me the nudge I needed to move diverted.el out of my config and into its own GitHub repo.
By default, diverted.el recognizes both mark-defun and mark-whole-buffer as diversions, but you can also recognize the likes of er/expand-region via diverted-events. I'm hoping configuring is fairly self-explanatory. To date, I only have mark-whole-buffer, mark-defun, and er/expand-region as recognized diversions.
(defcustom diverted-events
(list
(make-diverted-event :from 'mark-defun
:to 'indent-for-tab-command
:breadcrumb (lambda ()
(diverted--pop-to-mark-command 2)))
(make-diverted-event :from 'mark-whole-buffer
:to 'indent-for-tab-command
:breadcrumb (lambda ()
(diverted--pop-to-mark-command 2))))
"Diversion events to look for.
For example:
(add-to-list 'diverted-events
(make-diverted-event
:from 'er/expand-region
:to 'indent-for-tab-command
:breadcrumb (lambda ()
(diverted--pop-to-mark-command 2))))"
:type '(repeat sexp)
:group 'diverted)
Read on for a little demo…
Notice how point is left at the top of the screen after pressing TAB to indent region.
Notice how point is left where it was prior to selecting the function and pressing TAB to indent region.
That's it for today. If you want to give diverted.el a try, head over to GitHub.
Reckon diverted-mode will be useful to you? Enjoying this blog or my projects? I am an 👉 indie dev 👈. Help make it sustainable by ✨sponsoring✨
Need a blog? I can help with that. Maybe buy my iOS apps too ;)
powered by LMNO.lol