all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to get the current column regardless of the content of the current line?
@ 2022-12-15 19:22 Rodrigo Morales
  2022-12-15 21:37 ` Gregory Heytings
  2022-12-16  5:12 ` Jean Louis
  0 siblings, 2 replies; 15+ messages in thread
From: Rodrigo Morales @ 2022-12-15 19:22 UTC (permalink / raw)
  To: help-gnu-emacs

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>


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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 19:22 How to get the current column regardless of the content of the current line? Rodrigo Morales
@ 2022-12-15 21:37 ` Gregory Heytings
  2022-12-15 21:41   ` Gregory Heytings
  2022-12-16  5:12 ` Jean Louis
  1 sibling, 1 reply; 15+ messages in thread
From: Gregory Heytings @ 2022-12-15 21:37 UTC (permalink / raw)
  To: Rodrigo Morales; +Cc: help-gnu-emacs


>
> Which function could I use to obtain the column of the current position 
> that would work for any line regardless of its content?
>

(defun my/current-column ()
   (- (point) (progn (save-excursion (beginning-of-line) (point)))))




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 21:37 ` Gregory Heytings
@ 2022-12-15 21:41   ` Gregory Heytings
  2022-12-15 21:56     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-12-16  3:28     ` Rodrigo Morales
  0 siblings, 2 replies; 15+ messages in thread
From: Gregory Heytings @ 2022-12-15 21:41 UTC (permalink / raw)
  To: Rodrigo Morales; +Cc: help-gnu-emacs


>> Which function could I use to obtain the column of the current position 
>> that would work for any line regardless of its content?
>
> (defun my/current-column ()
>   (- (point) (progn (save-excursion (beginning-of-line) (point)))))
>

Sorry, the progn is unnecessary:

(defun my/current-column ()
   (- (point) (save-excursion (beginning-of-line) (point))))




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 21:41   ` Gregory Heytings
@ 2022-12-15 21:56     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-12-15 22:00       ` Emanuel Berg
  2022-12-15 22:02       ` Gregory Heytings
  2022-12-16  3:28     ` Rodrigo Morales
  1 sibling, 2 replies; 15+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-12-15 21:56 UTC (permalink / raw)
  To: help-gnu-emacs

> (defun my/current-column ()
>   (- (point) (save-excursion (beginning-of-line) (point))))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               (line-beginning-position)

-- Stefan




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 21:56     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-12-15 22:00       ` Emanuel Berg
  2022-12-15 22:02       ` Gregory Heytings
  1 sibling, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2022-12-15 22:00 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier via Users list for the GNU Emacs text editor wrote:

>> (defun my/current-column ()
>>   (- (point) (save-excursion (beginning-of-line) (point))))
>                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>                (line-beginning-position)

(pos-bol)?

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 21:56     ` Stefan Monnier via Users list for the GNU Emacs text editor
  2022-12-15 22:00       ` Emanuel Berg
@ 2022-12-15 22:02       ` Gregory Heytings
  2022-12-16  7:10         ` Eli Zaretskii
  1 sibling, 1 reply; 15+ messages in thread
From: Gregory Heytings @ 2022-12-15 22:02 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


>> (defun my/current-column ()
>>   (- (point) (save-excursion (beginning-of-line) (point))))
>               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>               (line-beginning-position)
>
> -- Stefan
>

Indeed.  Or even 'pos-bol':

(defun my/current-column ()
   (- (point) (pos-bol)))




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 21:41   ` Gregory Heytings
  2022-12-15 21:56     ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2022-12-16  3:28     ` Rodrigo Morales
  1 sibling, 0 replies; 15+ messages in thread
From: Rodrigo Morales @ 2022-12-16  3:28 UTC (permalink / raw)
  To: Gregory Heytings; +Cc: help-gnu-emacs

Thanks! That works well.

On Thu, Dec 15, 2022 at 4:41 PM Gregory Heytings <gregory@heytings.org>
wrote:

>
> >> Which function could I use to obtain the column of the current position
> >> that would work for any line regardless of its content?
> >
> > (defun my/current-column ()
> >   (- (point) (progn (save-excursion (beginning-of-line) (point)))))
> >
>
> Sorry, the progn is unnecessary:
>
> (defun my/current-column ()
>    (- (point) (save-excursion (beginning-of-line) (point))))
>
>


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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 19:22 How to get the current column regardless of the content of the current line? Rodrigo Morales
  2022-12-15 21:37 ` Gregory Heytings
@ 2022-12-16  5:12 ` Jean Louis
  2022-12-16 17:44   ` Rodrigo Morales
  1 sibling, 1 reply; 15+ messages in thread
From: Jean Louis @ 2022-12-16  5:12 UTC (permalink / raw)
  To: help-gnu-emacs, Rodrigo Morales

Tell me how did you format your email with table of contents?

Jean



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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-15 22:02       ` Gregory Heytings
@ 2022-12-16  7:10         ` Eli Zaretskii
  2022-12-16  8:52           ` Gregory Heytings
  0 siblings, 1 reply; 15+ messages in thread
From: Eli Zaretskii @ 2022-12-16  7:10 UTC (permalink / raw)
  To: Gregory Heytings; +Cc: monnier, help-gnu-emacs

> Date: Thu, 15 Dec 2022 22:02:59 +0000
> From: Gregory Heytings <gregory@heytings.org>
> cc: help-gnu-emacs@gnu.org
> 
> 
> >> (defun my/current-column ()
> >>   (- (point) (save-excursion (beginning-of-line) (point))))
> >               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >               (line-beginning-position)
> >
> > -- Stefan
> >
> 
> Indeed.  Or even 'pos-bol':
> 
> (defun my/current-column ()
>    (- (point) (pos-bol)))

It's not entirely correct to call this a "column", though...



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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-16  7:10         ` Eli Zaretskii
@ 2022-12-16  8:52           ` Gregory Heytings
  2022-12-16 21:43             ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: Gregory Heytings @ 2022-12-16  8:52 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: monnier, help-gnu-emacs


>> (defun my/current-column ()
>>    (- (point) (pos-bol)))
>
> It's not entirely correct to call this a "column", though...
>

That's what the OP wanted, indeed it's not a "column" in the visual sense 
anymore (e.g. a TAB counts for 1), but it's still a "column" in a "number 
of characters" sense.  Well, almost: when glyphs are composed, that 
function will not give the result the OP expects I think, e.g. with a line 
with only "🧙🏻" 2 is returned.


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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-16  5:12 ` Jean Louis
@ 2022-12-16 17:44   ` Rodrigo Morales
  2022-12-17  4:07     ` Jean Louis
  0 siblings, 1 reply; 15+ messages in thread
From: Rodrigo Morales @ 2022-12-16 17:44 UTC (permalink / raw)
  To: Jean Louis; +Cc: help-gnu-emacs

> Tell me how did you format your email with table of contents?

I wrote the email in an org-mode buffer, then

1. Press C-c C-e (by defualt, bound to org-export-dispatch)
2. Press "t" to choose "Export to plain text"
3. Press "A" to choose "As ASCII buffer"
4. Copy the content in the buffer "*Org ASCII Export*" in my mail
   client and send the email.

I also use Org Mode to write Markdown (i.e. issues in Gitlab, Github,
etc.). After I finish writing it in an Org Mode buffer, I export it to
Markdown and use the export as desired.

On Fri, Dec 16, 2022 at 12:12 AM Jean Louis <bugs@gnu.support> wrote:

> Tell me how did you format your email with table of contents?
>
> Jean
>


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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-16  8:52           ` Gregory Heytings
@ 2022-12-16 21:43             ` Emanuel Berg
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2022-12-16 21:43 UTC (permalink / raw)
  To: help-gnu-emacs

Gregory Heytings wrote:

>>> (defun my/current-column ()
>>>    (- (point) (pos-bol)))
>>
>> It's not entirely correct to call this a "column", though...
>
> That's what the OP wanted, indeed it's not a "column" in the
> visual sense anymore (e.g. a TAB counts for 1), but it's
> still a "column" in a "number of characters" sense.

One shouldn't use tabs anyway ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-16 17:44   ` Rodrigo Morales
@ 2022-12-17  4:07     ` Jean Louis
  2022-12-17  5:11       ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: Jean Louis @ 2022-12-17  4:07 UTC (permalink / raw)
  To: Rodrigo Morales; +Cc: help-gnu-emacs

* Rodrigo Morales <moralesrodrigo1100@gmail.com> [2022-12-16 20:45]:
> > Tell me how did you format your email with table of contents?
> 
> I wrote the email in an org-mode buffer, then
> 
> 1. Press C-c C-e (by defualt, bound to org-export-dispatch)
> 2. Press "t" to choose "Export to plain text"
> 3. Press "A" to choose "As ASCII buffer"
> 4. Copy the content in the buffer "*Org ASCII Export*" in my mail
>    client and send the email.
> 
> I also use Org Mode to write Markdown (i.e. issues in Gitlab, Github,
> etc.). After I finish writing it in an Org Mode buffer, I export it to
> Markdown and use the export as desired.

Ah, yes, that is Org type of export. Thanks. I was thinking there is
some other mode.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-17  4:07     ` Jean Louis
@ 2022-12-17  5:11       ` Emanuel Berg
  2022-12-17  5:53         ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg @ 2022-12-17  5:11 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> Ah, yes, that is Org type of export. Thanks. I was thinking
> there is some other mode.

No ToS for mails.

Send one mail per section instead, and put the section name as
the mail's Subject.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: How to get the current column regardless of the content of the current line?
  2022-12-17  5:11       ` Emanuel Berg
@ 2022-12-17  5:53         ` Emanuel Berg
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2022-12-17  5:53 UTC (permalink / raw)
  To: help-gnu-emacs

>> Ah, yes, that is Org type of export. Thanks. I was thinking
>> there is some other mode.
>
> No ToS for mails.

*ToC

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2022-12-17  5:53 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-15 19:22 How to get the current column regardless of the content of the current line? Rodrigo Morales
2022-12-15 21:37 ` Gregory Heytings
2022-12-15 21:41   ` Gregory Heytings
2022-12-15 21:56     ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-12-15 22:00       ` Emanuel Berg
2022-12-15 22:02       ` Gregory Heytings
2022-12-16  7:10         ` Eli Zaretskii
2022-12-16  8:52           ` Gregory Heytings
2022-12-16 21:43             ` Emanuel Berg
2022-12-16  3:28     ` Rodrigo Morales
2022-12-16  5:12 ` Jean Louis
2022-12-16 17:44   ` Rodrigo Morales
2022-12-17  4:07     ` Jean Louis
2022-12-17  5:11       ` Emanuel Berg
2022-12-17  5:53         ` Emanuel Berg

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.