Narrowing the Author column in MagitMon Dec 18, 2017
The Org and Worg git repos can be committed to only via ssh protocol (and not https), and for technical reasons, I cannot commit to those repos via ssh from my primary development machine. So I ended up with a flow that involves pushing commits to those repos using my Nexus 6p phone.
Magit is how I git, and I love to primarily work from the
∗magit-log∗ buffer (
M-x magit-status, l b). But the default
column widths were not optimal on a Nexus 6p 5.7" screen. And that’s
what inspired this tweak, which looks great on a regular desktop
Before I jump to the code, you can see in the below figure how the author and commit age columns took up roughly half the screen width on my phone before the tweak (left), and how much easier it is to read the commit messages after the tweak (right).
So the Magit Log columns tweaks are basically:
- Abbreviate the authors’ first names to just their initials.
- Abbreviate the commit ages to 1-character time units.
To implement those tweaks, I started digging through
and found that I would need to customize (i) the
variable and (ii)
magit-log-marginvariable allows me to use the abbreviated age instead of the verbose age strings (minute→m, hour→h, day→d, week→w, month→M, year→Y). It’s also where I specify how wide I want the author column to be.. I can now reduce that column width from the default value of 18 to 11 as I am abbreviating the author name too.
- Reviewing the
magit-log.elcode, I realized that I also need to advise the
magit-log-format-marginfunction to implement the author name abbreviation.
(use-package magit-log :init (progn ;; (setq magit-log-margin '(t age magit-log-margin-width t 18)) ;Default value (setq magit-log-margin '(t age-abbreviated magit-log-margin-width :author 11))) :config (progn ;; Abbreviate author name. I added this so that I can view Magit log without ;; too much commit message truncation even on narrow screens (like on phone). (defun modi/magit-log--abbreviate-author (&rest args) "The first arg is AUTHOR, abbreviate it. First Last -> F Last First.Last -> F Last Last, First -> F Last First -> First (no change). It is assumed that the author has only one or two names." ;; ARGS -> '((AUTHOR DATE)) ;; (car ARGS) -> '(AUTHOR DATE) ;; (car (car ARGS)) -> AUTHOR (let* ((author (car (car args))) (author-abbr (if (string-match-p "," author) ;; Last, First -> F Last (replace-regexp-in-string "\\(.*?\\), *\\(.\\).*" "\\2 \\1" author) ;; First Last -> F Last (replace-regexp-in-string "\\(.\\).*?[. ]+\\(.*\\)" "\\1 \\2" author)))) (setf (car (car args)) author-abbr)) (car args)) ;'(AUTHOR-ABBR DATE) (advice-add 'magit-log-format-margin :filter-args #'modi/magit-log--abbreviate-author)))
- I need to set the
magit-log-marginvalue in the
:initblock because that variable dynamically sets many other variables in
magit-logat the time of loading. So we cannot wait for the whole
magit-logto load before setting that variable. I wouldn’t need to worry about this if I were using the Emacs Customize interface to set this variable.
- I use
modi/magit-log--abbreviate-authorto add a
magit-log-format-margin. The signature of the advised function is
(magit-log-format-margin AUTHOR DATE). So the advice basically replaces the
AUTHORarg with its abbreviated form as explained in the
- Feel free to ask for more explanation in comments in the case you don’t use