all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Reading text properties from a yanked text
@ 2022-11-26 21:35 Nicolas P. Rougier (inria)
  2022-11-26 23:03 ` [External] : " Drew Adams
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Nicolas P. Rougier (inria) @ 2022-11-26 21:35 UTC (permalink / raw)
  To: emacs-devel


Hello,

I'm trying to read the properties of a yanked string and I do not 
understand why I get a nil result:

1. I evaluate the line below to get a bold "Hello" and I copy the 
result in the kill ring.

(insert (propertize "Hello" 'face 'bold))

2. If I try to get the properties of the yanked text, I get 
nil. However, the text is displayed bold and a (text-properties-at 
(point)) returns '(face bold) when point is on the H letter.

(text-properties-at 0 "Hello") 

3. This version works as expected (but this is not what I need):
(text-properties-at 0 (propertize "Hello" 'face 'bold))

Why do I get a nil result in case 2 (using Emacs 28.2) even though 
the text is displayed bold?

Nicolas

-- 
Nicolas P. Rougier —— www.labri.fr/perso/nrougier
Institute of Neurodegenerative Diseases, Bordeaux



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

* RE: [External] : Reading text properties from a yanked text
  2022-11-26 21:35 Reading text properties from a yanked text Nicolas P. Rougier (inria)
@ 2022-11-26 23:03 ` Drew Adams
  2022-11-27  6:07   ` Nicolas P. Rougier (inria)
  2022-11-26 23:26 ` Gregory Heytings
  2022-11-27  6:14 ` Eli Zaretskii
  2 siblings, 1 reply; 13+ messages in thread
From: Drew Adams @ 2022-11-26 23:03 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria), emacs-devel@gnu.org

> I'm trying to read the properties of a yanked string and I do not
> understand why I get a nil result:
> 
> 1. I evaluate the line below to get a bold "Hello" and I copy the
> result in the kill ring.
> 
> (insert (propertize "Hello" 'face 'bold))
> 
> 2. If I try to get the properties of the yanked text, I get
> nil. However, the text is displayed bold and a (text-properties-at
> (point)) returns '(face bold) when point is on the H letter.
> 
> (text-properties-at 0 "Hello")
> 
> 3. This version works as expected (but this is not what I need):
> (text-properties-at 0 (propertize "Hello" 'face 'bold))
> 
> Why do I get a nil result in case 2 (using Emacs 28.2) even though
> the text is displayed bold?

Not sure what you're asking.
But do either of these user options help?

 `yank-excluded-properties'
 `yank-handled-properties'

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

* Re: Reading text properties from a yanked text
  2022-11-26 21:35 Reading text properties from a yanked text Nicolas P. Rougier (inria)
  2022-11-26 23:03 ` [External] : " Drew Adams
@ 2022-11-26 23:26 ` Gregory Heytings
  2022-11-27  6:08   ` Nicolas P. Rougier (inria)
  2022-11-27  6:14 ` Eli Zaretskii
  2 siblings, 1 reply; 13+ messages in thread
From: Gregory Heytings @ 2022-11-26 23:26 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: emacs-devel


>
> 1. I evaluate the line below to get a bold "Hello" and I copy the result 
> in the kill ring.
>
> (insert (propertize "Hello" 'face 'bold))
>
> 2. If I try to get the properties of the yanked text, I get nil. 
> However, the text is displayed bold and a (text-properties-at (point)) 
> returns '(face bold) when point is on the H letter.
>
> (text-properties-at 0 "Hello")
>
> 3. This version works as expected (but this is not what I need): 
> (text-properties-at 0 (propertize "Hello" 'face 'bold))
>
> Why do I get a nil result in case 2 (using Emacs 28.2) even though the 
> text is displayed bold?
>

I think I see what you mean (but I'm not sure).

Is your recipe the following one?

emacs -Q
M-x text-mode RET
M-: (insert (propertize "Hello" 'face 'bold)) RET
C-SPC
M-b
M-w
M-: (text-properties-at 0 "<press C-y>") RET

in which you see the text in bold in the minibuffer before pressing RET? 
(Instead of M-: you could also type (text-properties-at 0 "<press C-y>") 
followed by C-x C-e somewhere else.)



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

* Re: [External] : Reading text properties from a yanked text
  2022-11-26 23:03 ` [External] : " Drew Adams
@ 2022-11-27  6:07   ` Nicolas P. Rougier (inria)
  2022-11-27  7:08     ` Stefan Monnier
  0 siblings, 1 reply; 13+ messages in thread
From: Nicolas P. Rougier (inria) @ 2022-11-27  6:07 UTC (permalink / raw)
  To: Drew Adams; +Cc: emacs-devel@gnu.org


Drew Adams [2022-11-26 at 23:03] wrote:
> Not sure what you're asking.
> But do either of these user options help?
> 
>  `yank-excluded-properties'
>  `yank-handled-properties'

Thank for the pointer, I didn't you could configure yank 
properties. But in the present case it does not seem to make a 
difference.

Nicolas

-- 
Nicolas P. Rougier —— www.labri.fr/perso/nrougier
Institute of Neurodegenerative Diseases, Bordeaux



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

* Re: Reading text properties from a yanked text
  2022-11-26 23:26 ` Gregory Heytings
@ 2022-11-27  6:08   ` Nicolas P. Rougier (inria)
  0 siblings, 0 replies; 13+ messages in thread
From: Nicolas P. Rougier (inria) @ 2022-11-27  6:08 UTC (permalink / raw)
  To: Gregory Heytings; +Cc: emacs-devel


Gregory Heytings [2022-11-26 at 23:26] wrote:
> I think I see what you mean (but I'm not sure).
> Is your recipe the following one?
> 
> emacs -Q
> M-x text-mode RET
> M-: (insert (propertize "Hello" 'face 'bold)) RET
> C-SPC
> M-b
> M-w
> M-: (text-properties-at 0 "<press C-y>") RET
> 
> in which you see the text in bold in the minibuffer before 
> pressing
> RET? (Instead of M-: you could also type (text-properties-at 0 
> "<press
> C-y>") followed by C-x C-e somewhere else.)

Yes, I think this is equivalent to what I did:

Instead of using the M-x / M-:, I write the first line in the 
*scratch* buffer and evaluate it (eval-last-sexp). Then I copy the 
output and I write the second line using the killed text.

I can see the bold text in the buffer when I yank it but when I 
evaluate the second line (text-properties...), it says there is no 
properties. However, if I use M-x: describe-char on any of the 
yanked character, it says, it has a bold face.

Nicolas

-- 
Nicolas P. Rougier —— www.labri.fr/perso/nrougier
Institute of Neurodegenerative Diseases, Bordeaux



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

* Re: Reading text properties from a yanked text
  2022-11-26 21:35 Reading text properties from a yanked text Nicolas P. Rougier (inria)
  2022-11-26 23:03 ` [External] : " Drew Adams
  2022-11-26 23:26 ` Gregory Heytings
@ 2022-11-27  6:14 ` Eli Zaretskii
  2022-11-27  6:16   ` Nicolas P. Rougier (inria)
  2 siblings, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-11-27  6:14 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: emacs-devel

> From: "Nicolas P. Rougier (inria)" <nicolas.rougier@inria.fr>
> Date: Sat, 26 Nov 2022 22:35:13 +0100
> 
> I'm trying to read the properties of a yanked string and I do not 
> understand why I get a nil result:
> 
> 1. I evaluate the line below to get a bold "Hello" and I copy the 
> result in the kill ring.
> 
> (insert (propertize "Hello" 'face 'bold))
> 
> 2. If I try to get the properties of the yanked text, I get 
> nil. However, the text is displayed bold and a (text-properties-at 
> (point)) returns '(face bold) when point is on the H letter.
> 
> (text-properties-at 0 "Hello") 
> 
> 3. This version works as expected (but this is not what I need):
> (text-properties-at 0 (propertize "Hello" 'face 'bold))
> 
> Why do I get a nil result in case 2 (using Emacs 28.2) even though 
> the text is displayed bold?

Because "Hello" is just a string, with no properties.  The "Hello" that you
propertized and inserted is long gone by the time you evaluate case 2.  The
mere fact that both strings have the same text "Hello" doesn't mean they are
the same string object.  And text properties in Emacs are properties of
specific objects.

Try this instead:

  (let ((str (propertize "Hello" 'face 'bold)))
    (insert str)
    (text-properties-at 0 str))

This makes sure the same string that gets inserted is passed to
text-properties-at, and produces the results you expect.



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

* Re: Reading text properties from a yanked text
  2022-11-27  6:14 ` Eli Zaretskii
@ 2022-11-27  6:16   ` Nicolas P. Rougier (inria)
  2022-11-27  7:10     ` Eli Zaretskii
  2022-11-27  7:10     ` Gregory Heytings
  0 siblings, 2 replies; 13+ messages in thread
From: Nicolas P. Rougier (inria) @ 2022-11-27  6:16 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel


Eli Zaretskii [2022-11-27 at 08:14] wrote:
> Because "Hello" is just a string, with no properties.  The 
> "Hello" that you
> propertized and inserted is long gone by the time you evaluate 
> case 2.  The
> mere fact that both strings have the same text "Hello" doesn't 
> mean they are
> the same string object.  And text properties in Emacs are 
> properties of
> specific objects.
> 
> Try this instead:
> 
>   (let ((str (propertize "Hello" 'face 'bold)))
>     (insert str)
>     (text-properties-at 0 str))
> 
> This makes sure the same string that gets inserted is passed to
> text-properties-at, and produces the results you expect.

Thanks for the explanation. I think I get it (but it is still is a 
bit confusing to me).

For example this returns (face bold):

(with-temp-buffer
  (insert (propertize "Hello" 'face 'bold))
  (kill-region (point-min) (point-max))
  (yank)
  (text-properties-at 0
        (buffer-substring (point-min) (point-max))))

While this returns nil:

(with-temp-buffer
  (insert (propertize "Hello" 'face 'bold))
  (kill-region (point-min) (point-max))
  (insert (format "(text-properties-at 0 \"%s\")"
                  (current-kill 0)))
  (eval-buffer))


Nicolas

-- 
Nicolas P. Rougier —— www.labri.fr/perso/nrougier
Institute of Neurodegenerative Diseases, Bordeaux



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

* Re: [External] : Reading text properties from a yanked text
  2022-11-27  6:07   ` Nicolas P. Rougier (inria)
@ 2022-11-27  7:08     ` Stefan Monnier
  0 siblings, 0 replies; 13+ messages in thread
From: Stefan Monnier @ 2022-11-27  7:08 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: Drew Adams, emacs-devel@gnu.org

Nicolas P. Rougier (inria) [2022-11-27 07:07:30] wrote:
> Drew Adams [2022-11-26 at 23:03] wrote:
>> Not sure what you're asking.
>> But do either of these user options help?
>>  `yank-excluded-properties'
>>  `yank-handled-properties'
> Thank for the pointer, I didn't you could configure yank properties. But in
> the present case it does not seem to make a difference.

Evaluating code of (foo "bar" baz) only pays attention to the
characters in the code.  Not to the text-properties applied to
those characters.  IOW the face applied to `foo`, or to `bar`, or to
`baz` will have no effect on the return value of that piece of code.


        Stefan




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

* Re: Reading text properties from a yanked text
  2022-11-27  6:16   ` Nicolas P. Rougier (inria)
@ 2022-11-27  7:10     ` Eli Zaretskii
  2022-11-27  7:29       ` Nicolas P. Rougier (inria)
  2022-11-27  7:10     ` Gregory Heytings
  1 sibling, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-11-27  7:10 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: emacs-devel

> From: "Nicolas P. Rougier (inria)" <nicolas.rougier@inria.fr>
> Cc: emacs-devel@gnu.org
> Date: Sun, 27 Nov 2022 07:16:45 +0100
> 
> For example this returns (face bold):
> 
> (with-temp-buffer
>   (insert (propertize "Hello" 'face 'bold))
>   (kill-region (point-min) (point-max))
>   (yank)
>   (text-properties-at 0
>         (buffer-substring (point-min) (point-max))))
> 
> While this returns nil:
> 
> (with-temp-buffer
>   (insert (propertize "Hello" 'face 'bold))
>   (kill-region (point-min) (point-max))
>   (insert (format "(text-properties-at 0 \"%s\")"
>                   (current-kill 0)))
>   (eval-buffer))

Because once again what you pass to text-properties-at is a different
string.  You are confusing how a string looks on display when inserted into
a buffer with another string that just happens to have the same text.

Try this instead:

(with-temp-buffer
  (insert (propertize "Hello" 'face 'bold))
  (kill-region (point-min) (point-max))
  (insert (format "(text-properties-at 0 \"%s\")"
                  (current-kill 0)))
  (text-properties-at 0 (buffer-substring 24 29)))



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

* Re: Reading text properties from a yanked text
  2022-11-27  6:16   ` Nicolas P. Rougier (inria)
  2022-11-27  7:10     ` Eli Zaretskii
@ 2022-11-27  7:10     ` Gregory Heytings
  2022-11-27  7:14       ` Gregory Heytings
  1 sibling, 1 reply; 13+ messages in thread
From: Gregory Heytings @ 2022-11-27  7:10 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: Eli Zaretskii, emacs-devel


>
> For example this returns (face bold):
>
> (with-temp-buffer
> (insert (propertize "Hello" 'face 'bold))
> (kill-region (point-min) (point-max))
> (yank)
> (text-properties-at 0
>       (buffer-substring (point-min) (point-max))))
>
> While this returns nil:
>
> (with-temp-buffer
> (insert (propertize "Hello" 'face 'bold))
> (kill-region (point-min) (point-max))
> (insert (format "(text-properties-at 0 \"%s\")"
>                 (current-kill 0)))
> (eval-buffer))
>

The Lisp reader does not see the text properties of the text it reads. 
Unless you use the special "#(...)" construct, in which text properties 
are explicitly specified, and case you'll get them.  E.g. you can eval the 
following

(text-properties-at 0 #("Hello" 0 4 (face bold)))

to get what you want.



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

* Re: Reading text properties from a yanked text
  2022-11-27  7:10     ` Gregory Heytings
@ 2022-11-27  7:14       ` Gregory Heytings
  0 siblings, 0 replies; 13+ messages in thread
From: Gregory Heytings @ 2022-11-27  7:14 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: Eli Zaretskii, emacs-devel


>
> explicitly specified, and case you'll get them.
>

s/and/in which/



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

* Re: Reading text properties from a yanked text
  2022-11-27  7:10     ` Eli Zaretskii
@ 2022-11-27  7:29       ` Nicolas P. Rougier (inria)
  2022-12-01 23:25         ` Richard Stallman
  0 siblings, 1 reply; 13+ messages in thread
From: Nicolas P. Rougier (inria) @ 2022-11-27  7:29 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel


Eli Zaretskii [2022-11-27 at 09:10] wrote:
> Because once again what you pass to text-properties-at is a 
> different
> string.  You are confusing how a string looks on display when 
> inserted into
> a buffer with another string that just happens to have the same 
> text.
> 
> Try this instead:
> 
> (with-temp-buffer
>   (insert (propertize "Hello" 'face 'bold))
>   (kill-region (point-min) (point-max))
>   (insert (format "(text-properties-at 0 \"%s\")"
>                   (current-kill 0)))
>   (text-properties-at 0 (buffer-substring 24 29)))


Thanks to all of you. I think I'm understanding it now. My 
confusion comes from a more complex code that I'm trying to debug 
and I stumble on this specific case/confusion. Back to debug then.

Nicolas

-- 
Nicolas P. Rougier —— www.labri.fr/perso/nrougier
Institute of Neurodegenerative Diseases, Bordeaux



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

* Re: Reading text properties from a yanked text
  2022-11-27  7:29       ` Nicolas P. Rougier (inria)
@ 2022-12-01 23:25         ` Richard Stallman
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Stallman @ 2022-12-01 23:25 UTC (permalink / raw)
  To: Nicolas P. Rougier (inria); +Cc: eliz, emacs-devel

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > > Because once again what you pass to text-properties-at is a 
  > > different
  > > string.  You are confusing how a string looks on display when 
  > > inserted into
  > > a buffer with another string that just happens to have the same 
  > > text.

  > Thanks to all of you. I think I'm understanding it now. My 
  > confusion comes from a more complex code that I'm trying to debug 
  > and I stumble on this specific case/confusion. Back to debug then.

Is there anything in our documentation that we could change to
help people understand this point?

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





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

end of thread, other threads:[~2022-12-01 23:25 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-26 21:35 Reading text properties from a yanked text Nicolas P. Rougier (inria)
2022-11-26 23:03 ` [External] : " Drew Adams
2022-11-27  6:07   ` Nicolas P. Rougier (inria)
2022-11-27  7:08     ` Stefan Monnier
2022-11-26 23:26 ` Gregory Heytings
2022-11-27  6:08   ` Nicolas P. Rougier (inria)
2022-11-27  6:14 ` Eli Zaretskii
2022-11-27  6:16   ` Nicolas P. Rougier (inria)
2022-11-27  7:10     ` Eli Zaretskii
2022-11-27  7:29       ` Nicolas P. Rougier (inria)
2022-12-01 23:25         ` Richard Stallman
2022-11-27  7:10     ` Gregory Heytings
2022-11-27  7:14       ` Gregory Heytings

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.