all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* csv-mode: header does not align when line numbers are displayed
@ 2022-04-29  7:47 Joost
  2022-04-29 10:40 ` Eli Zaretskii
  0 siblings, 1 reply; 3+ messages in thread
From: Joost @ 2022-04-29  7:47 UTC (permalink / raw)
  To: emacs-devel

Hi list,

Not sure if this should be reported as a bug or not; let me know if it should.

`csv-mode` has the option to display a header line so you can make column names permanently visible, which is a very nice option. However, when `display-line-numbers-mode` is active, the column names do not line up with the actual columns, because the header line does not take into account the space taken up by the line numbers.

The following patch seems to be a quick fix:

```
diff --git a/csv-mode.el b/csv-mode.el
index 10ce166052..33118aa2e8 100644
--- a/csv-mode.el
+++ b/csv-mode.el
@@ -1382,6 +1382,10 @@ If there is already a header line, then unset the header line."
       (move-to-column (or csv--header-hscroll 0))
       (let ((str (replace-regexp-in-string
                  "%" "%%" (buffer-substring (point) (line-end-position))))
+            (line-number-width (if (and (bound-and-true-p display-line-numbers-mode)
+                                        display-line-numbers-width)
+                                   (+ 2 display-line-numbers-width)
+                                 0))
             (i 0))
         (while (and i (< i (length str)))
           (let ((prop (get-text-property i 'display str)))
@@ -1392,13 +1396,13 @@ If there is already a header line, then unset the header line."
                         (newprop
                          `(space :align-to
                                  ,(if (numberp x)
-                                      (- x (or csv--header-hscroll 0))
-                                    `(- ,x csv--header-hscroll)))))
+                                      (+ line-number-width (- x (or csv--header-hscroll 0)))
+                                    `(+ line-number-width (- ,x csv--header-hscroll))))))
                    (put-text-property i (or nexti (length str))
                                       'display newprop str)
                    (setq i nexti))))
           (setq i (next-single-property-change i 'display str)))
-        (concat (propertize " " 'display '((space :align-to 0))) str)))))
+        (concat (propertize " " 'display `((space :align-to ,line-number-width))) str)))))
 
 ;;; Auto-alignment
 
```

Though it doesn't automatically adjust the header line when `display-line-numbers-mode` is toggled or if `display-line-number-width` changes, so it's probably not the ideal solution.

Thanks,

-- 
Joost Kremers
Life has its moments



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-12 15:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-29  7:47 csv-mode: header does not align when line numbers are displayed Joost
2022-04-29 10:40 ` Eli Zaretskii
2022-05-12 15:40   ` Joost Kremers

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.