[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
How to get the current column regardless of the content of the current l
From: |
Rodrigo Morales |
Subject: |
How to get the current column regardless of the content of the current line? |
Date: |
Thu, 15 Dec 2022 14:22:49 -0500 |
Table of Contents
_________________
1. The context
2. The question
3. The current workaround
4. Additional information
1 The context
=============
I'm aware that `current-column' returns the column of the cursor
position. See two minimal working examples below.
,----
| (with-temp-buffer
| (insert "foo")
| (end-of-line)
| (princ (current-column)))
`----
,----
| 1
`----
,----
| (with-temp-buffer
| (insert "foofoo")
| (end-of-line)
| (princ (current-column)))
`----
,----
| 6
`----
Sometimes, `current-column' considers one character as if it were
two. In the minimal working example below, you can see that the line
contains `你' (U+4F60) 3 times and `current-column' returns 6 when it
is at the end of the line.
,----
| (with-temp-buffer
| (insert "你你你")
| (end-of-line)
| (princ (current-column)))
`----
,----
| 6
`----
In the minimal working example below, you can see that even though the
line contains two characters, `current-column' returns `3'. This
happens because, as shown in the previous example, `你' is counted as
two characters by `current-column'.
,----
| (with-temp-buffer
| (insert "a你")
| (end-of-line)
| (princ (current-column)))
`----
,----
| 3
`----
2 The question
==============
Which function could I use to obtain the column of the current
position that would work for any line regardless of its content?
3 The current workaround
========================
I have written the following function to get the results that I
expect. However, I decided to open this thread just in case there's
another (preferably, shorter or built-in) function that any of you
might know.
,----
| (defun my/current-column ()
| (save-excursion
| ;; Save the current point
| (let ((orig (point))
| (counter 0))
| (beginning-of-line)
| ;; Count the number of characters from the beginning of the line
| ;; to the previously stored point
| (while (and (<= (point) orig)
| (not (eobp)))
| (setq counter (1+ counter))
| (forward-char))
| counter)))
`----
The following code blocks contains minimal working examples that show
the results of this function.
,----
| (with-temp-buffer
| (insert "你你你")
| (my/current-column))
`----
,----
| 3
`----
,----
| (with-temp-buffer
| (insert "a你")
| (my/current-column))
`----
,----
| 2
`----
4 Additional information
========================
I opened a thread in the bug-gnu-emacs mailing list, because I thought
that there was a bug in `current-column', and Eli Zaretskii explained
some details of this function. You can find the thread in this link at
the archives:
<https://lists.gnu.org/archive/html/bug-gnu-emacs/2022-12/msg01524.html>
- How to get the current column regardless of the content of the current line?,
Rodrigo Morales <=
- Re: How to get the current column regardless of the content of the current line?, Gregory Heytings, 2022/12/15
- Re: How to get the current column regardless of the content of the current line?, Gregory Heytings, 2022/12/15
- Re: How to get the current column regardless of the content of the current line?, Stefan Monnier, 2022/12/15
- Re: How to get the current column regardless of the content of the current line?, Gregory Heytings, 2022/12/15
- Re: How to get the current column regardless of the content of the current line?, Eli Zaretskii, 2022/12/16
- Re: How to get the current column regardless of the content of the current line?, Gregory Heytings, 2022/12/16
- Re: How to get the current column regardless of the content of the current line?, Emanuel Berg, 2022/12/17
- Re: How to get the current column regardless of the content of the current line?, Emanuel Berg, 2022/12/16
- Re: How to get the current column regardless of the content of the current line?, Rodrigo Morales, 2022/12/15
Re: How to get the current column regardless of the content of the current line?, Jean Louis, 2022/12/16