unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Joost <joostkremers@fastmail.fm>
To: emacs-devel@gnu.org
Subject: csv-mode: header does not align when line numbers are displayed
Date: Fri, 29 Apr 2022 09:47:08 +0200	[thread overview]
Message-ID: <64bbe68b-f2b9-4cb5-93b8-07e5720e74fa@www.fastmail.com> (raw)

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



             reply	other threads:[~2022-04-29  7:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-29  7:47 Joost [this message]
2022-04-29 10:40 ` csv-mode: header does not align when line numbers are displayed Eli Zaretskii
2022-05-12 15:40   ` Joost Kremers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=64bbe68b-f2b9-4cb5-93b8-07e5720e74fa@www.fastmail.com \
    --to=joostkremers@fastmail.fm \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).