Modify text appearance (put spaces after commas just for display)

classic Classic list List threaded Threaded
40 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Modify text appearance (put spaces after commas just for display)

Alberto Luaces
Hello, I have some inherited code where the style used was to squeeze
every single non-required white-space.  There are long lists of
comma-separated values with any spaces on them, rendering the code very
difficult to read.

I wanted to have something like glasses-mode, but unless I am mistaken,
it is not very configurable, so I cannot add new cases as commas
separated by spaces, which is mine.

Then I thought of overlays, but the manual says that if there are a big
number of substitutions, maybe it could be a better idea to use text
properties instead.

So, from the top of your head, what would be the less painful method to
change every comma to a comma+space, or at least change the color of the
commas to anything more distinguishable and attach it to the language
mode hook?

Thanks!

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Emanuel Berg-4
Alberto Luaces wrote:

> Hello, I have some inherited code where the
> style used was to squeeze every single
> non-required white-space. There are long
> lists of comma-separated values with any
> spaces on them, rendering the code very
> difficult to read.
>
> I wanted to have something like glasses-mode,
> but unless I am mistaken, it is not very
> configurable, so I cannot add new cases as
> commas separated by spaces, which is mine.
>
> Then I thought of overlays, but the manual
> says that if there are a big number of
> substitutions, maybe it could be a better
> idea to use text properties instead.
>
> So, from the top of your head, what would be
> the less painful method to change every comma
> to a comma+space, or at least change the
> color of the commas to anything more
> distinguishable and attach it to the language
> mode hook?

... you mean in code that is already written?

Do it by hand, or use `replace-regexp'.

(replace-regexp "," ", ")

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Alberto Luaces
Emanuel Berg writes:

> Alberto Luaces wrote:
>
>> Hello, I have some inherited code where the
>> style used was to squeeze every single
>> non-required white-space. There are long
>> lists of comma-separated values with any
>> spaces on them, rendering the code very
>> difficult to read.
>>
>> I wanted to have something like glasses-mode,
>> but unless I am mistaken, it is not very
>> configurable, so I cannot add new cases as
>> commas separated by spaces, which is mine.
>>
>> Then I thought of overlays, but the manual
>> says that if there are a big number of
>> substitutions, maybe it could be a better
>> idea to use text properties instead.
>>
>> So, from the top of your head, what would be
>> the less painful method to change every comma
>> to a comma+space, or at least change the
>> color of the commas to anything more
>> distinguishable and attach it to the language
>> mode hook?
>
> ... you mean in code that is already written?
>
> Do it by hand, or use `replace-regexp'.
>
> (replace-regexp "," ", ")

Yes, it is already written.

The point is not modifying the code, as in glasses-mode style, so
replace-regexp is not a solution.  Thanks anyway!

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Tomas Zerolo
In reply to this post by Emanuel Berg-4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Sep 26, 2017 at 11:28:35AM +0200, Emanuel Berg wrote:
> Alberto Luaces wrote:

[...]

> ... you mean in code that is already written?
>
> Do it by hand, or use `replace-regexp'.
>
> (replace-regexp "," ", ")

I think the OP didn't want to change the text itself, but just its
display/appearance. Like with display properties.

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlnKHscACgkQBcgs9XrR2kbLgQCeL5N7P1g2joPQouMs06G1ecJ6
qQEAn1GZFT/QVilhNhlAKYZVuEcNra0v
=UCKe
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Yuri Khan-2
In reply to this post by Alberto Luaces
On Tue, Sep 26, 2017 at 4:12 PM, Alberto Luaces <[hidden email]> wrote:

> So, from the top of your head, what would be the less painful method to
> change every comma to a comma+space, or at least change the color of the
> commas to anything more distinguishable and attach it to the language
> mode hook?

One imperfect approximation would be to use prettify-symbols-mode.

    $ emacs -Q

    (add-to-list 'prettify-symbols-alist '("," . (?, (Br . Bl) ?\s)))

    M-x prettify-symbols-mode RET

Limitations:

* Does not act on commas in comments or string literals.
* If a comma is followed by a space, you will see two spaces.

If you want to overcome these, you will either want to write some
custom predicates for prettify-symbols-mode, or dive a layer down and
work with ‘font-lock-add-keywords’ directly.

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Emanuel Berg-4
In reply to this post by Tomas Zerolo
tomás wrote:

> I think the OP didn't want to change the text
> itself, but just its display/appearance.

So it will look one way and be another?
Why would you do that?

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Tomas Zerolo
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Sep 26, 2017 at 12:14:05PM +0200, Emanuel Berg wrote:
> tomás wrote:
>
> > I think the OP didn't want to change the text
> > itself, but just its display/appearance.
>
> So it will look one way and be another?
> Why would you do that?

Why wouldn't you?

(yah, snotty reply, sorry. What I mean is... we do that all the time,
with syntax highlighting, moving the debugger "spot" around, Customize
(yes, we know you don't like that one). If you press me, hey, UTF-8.

What you see in your editor is already the result of layers upon layers
of representation and cheating (newline? tab? C'm on! ;)

The nice thing about Emacs is that it not only caters for my (or your)
needs :-)

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlnKLEAACgkQBcgs9XrR2kbc7QCdEJ4QpHh8YJ1DWh+koi+2vMYE
pTMAnj2neUW7hdsiIELAXtA3k9JCLbqH
=EPUc
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Alberto Luaces
In reply to this post by Yuri Khan-2
Yuri Khan writes:

> On Tue, Sep 26, 2017 at 4:12 PM, Alberto Luaces <[hidden email]> wrote:
>
>> So, from the top of your head, what would be the less painful method to
>> change every comma to a comma+space, or at least change the color of the
>> commas to anything more distinguishable and attach it to the language
>> mode hook?
>
> One imperfect approximation would be to use prettify-symbols-mode.
>
>     $ emacs -Q
>
>     (add-to-list 'prettify-symbols-alist '("," . (?, (Br . Bl) ?\s)))
>
>     M-x prettify-symbols-mode RET
>
> Limitations:
>
> * Does not act on commas in comments or string literals.
> * If a comma is followed by a space, you will see two spaces.
>

Nice solution, thanks!  The limitations are fine by me, since I do not
care about the comments, and having several spaces together does not
hinder my reading.

I like very much how emacs keeps considering the substituted character
as a whole block, so that eases the editing of the file since I know
which spaces are real and which ones are not.

> If you want to overcome these, you will either want to write some
> custom predicates for prettify-symbols-mode, or dive a layer down and
> work with ‘font-lock-add-keywords’ directly.
>

Nice to know.  Thanks again!

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Alberto Luaces
In reply to this post by Tomas Zerolo
tomás writes:

> On Tue, Sep 26, 2017 at 12:14:05PM +0200, Emanuel Berg wrote:
>> tomás wrote:
>>
>> > I think the OP didn't want to change the text
>> > itself, but just its display/appearance.
>>
>> So it will look one way and be another?
>> Why would you do that?
>
> Why wouldn't you?
>
> (yah, snotty reply, sorry. What I mean is... we do that all the time,
> with syntax highlighting, moving the debugger "spot" around, Customize
> (yes, we know you don't like that one). If you press me, hey, UTF-8.
>
> What you see in your editor is already the result of layers upon layers
> of representation and cheating (newline? tab? C'm on! ;)
>
> The nice thing about Emacs is that it not only caters for my (or your)
> needs :-)

Yes, tomás is right with his complete explanation.  I would just add
another hint:

glasses-mode

read about it and you will find the entire rationale :-)

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Marcin Borkowski-3
In reply to this post by Alberto Luaces

On 2017-09-26, at 13:05, Alberto Luaces <[hidden email]> wrote:

>> If you want to overcome these, you will either want to write some
>> custom predicates for prettify-symbols-mode, or dive a layer down and
>> work with ‘font-lock-add-keywords’ directly.
>
> Nice to know.  Thanks again!

How about this?

http://mbork.pl/2016-01-30_Dimming_out_tildes_in_AUCTeX

Hth,

--
Marcin Borkowski

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Nick Helm
In reply to this post by Alberto Luaces
Alberto Luaces <[hidden email]> writes:

> Hello, I have some inherited code where the style used was to squeeze
> every single non-required white-space.  There are long lists of
> comma-separated values with any spaces on them, rendering the code very
> difficult to read.
>
> So, from the top of your head, what would be the less painful method to
> change every comma to a comma+space, or at least change the color of the
> commas to anything more distinguishable and attach it to the language
> mode hook?

Another option is to alter the buffer's display table by repurposing
whitespace-mode. For example:

(add-hook 'text-mode-hook (lambda ()
  (set (make-local-variable 'whitespace-style) '(space-mark))
  (set (make-local-variable 'whitespace-display-mappings)
        '((space-mark 44 [44 32])))
  (whitespace-mode)))

Change the hook to the language mode you're using. M-x whitespace-mode
toggles the effect off and on, as usual.

--
Nick

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Sivaram Neelakantan
In reply to this post by Alberto Luaces
On Tue, Sep 26 2017,Alberto Luaces wrote:


[snipped 8 lines]

>
> Then I thought of overlays, but the manual says that if there are a big
> number of substitutions, maybe it could be a better idea to use text
> properties instead.
>
> So, from the top of your head, what would be the less painful method to
> change every comma to a comma+space, or at least change the color of the
> commas to anything more distinguishable and attach it to the language
> mode hook?
>

Does org-mode have a means of making columns out of comma separated
entries just for viewing?  I vaguely remember that Excel style columns
were produced on CSV rows when in org-mode


sivaram
--


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Alberto Luaces
In reply to this post by Nick Helm
Nick Helm writes:

> Alberto Luaces <[hidden email]> writes:
>
>> Hello, I have some inherited code where the style used was to squeeze
>> every single non-required white-space.  There are long lists of
>> comma-separated values with any spaces on them, rendering the code very
>> difficult to read.
>>
>> So, from the top of your head, what would be the less painful method to
>> change every comma to a comma+space, or at least change the color of the
>> commas to anything more distinguishable and attach it to the language
>> mode hook?
>
> Another option is to alter the buffer's display table by repurposing
> whitespace-mode. For example:
>
> (add-hook 'text-mode-hook (lambda ()
>   (set (make-local-variable 'whitespace-style) '(space-mark))
>   (set (make-local-variable 'whitespace-display-mappings)
>         '((space-mark 44 [44 32])))
>   (whitespace-mode)))
>
> Change the hook to the language mode you're using. M-x whitespace-mode
> toggles the effect off and on, as usual.

That is nice, Nick.  Besides that, I have learned that whitespace-mode
can be customized so deeply thanks to you!

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Alberto Luaces
In reply to this post by Marcin Borkowski-3
Marcin Borkowski writes:

> On 2017-09-26, at 13:05, Alberto Luaces <[hidden email]> wrote:
>
>>> If you want to overcome these, you will either want to write some
>>> custom predicates for prettify-symbols-mode, or dive a layer down and
>>> work with ‘font-lock-add-keywords’ directly.
>>
>> Nice to know.  Thanks again!
>
> How about this?
>
> http://mbork.pl/2016-01-30_Dimming_out_tildes_in_AUCTeX
>
> Hth,

Yes, I also like your solution, it is very elegant.  Thanks, Marcin!

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Alberto Luaces
In reply to this post by Sivaram Neelakantan
Sivaram Neelakantan writes:

> On Tue, Sep 26 2017,Alberto Luaces wrote:
>
>
> [snipped 8 lines]
>
>>
>> Then I thought of overlays, but the manual says that if there are a big
>> number of substitutions, maybe it could be a better idea to use text
>> properties instead.
>>
>> So, from the top of your head, what would be the less painful method to
>> change every comma to a comma+space, or at least change the color of the
>> commas to anything more distinguishable and attach it to the language
>> mode hook?
>>
>
> Does org-mode have a means of making columns out of comma separated
> entries just for viewing?  I vaguely remember that Excel style columns
> were produced on CSV rows when in org-mode

I think that you are right on this one.  Unfortunately, my documents are
source code, and I wanted not to have to transform them manually if
possible, but in an automatic way as some other posters have taught me.

Thanks anyway, Sivaram!

--
Alberto


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Emanuel Berg-4
In reply to this post by Tomas Zerolo
tomás wrote:

> Why wouldn't you?

Because it is confusing. Just looking at code
tells you a lot. What it is, if there are bugs.
And not just actively. It stores an image in
the brain for later use, if need be.

If you can't trust your eyes, or have to think
(translate in your brain) what it actually is,
that's either a huge disadvantage or an extra
effort which is error-prone and has no
advantage to it.

It also makes the "brain image storage"
much more difficult.

> (yah, snotty reply, sorry. What I mean is...
> we do that all the time, with syntax
> highlighting

The analogue situation with respect to syntax
highlighting would be syntax highlighting that
is inconsistent.

> moving the debugger "spot" around

Likewise, if the spot showed what isn't
"spotted", that'd be analogue.

> Customize (yes, we know you don't like that
> one). If you press me, hey, UTF-8.

Not following?

> What you see in your editor is already the
> result of layers upon layers of
> representation and cheating (newline? tab?
> C'm on! ;)

Newline are displayed as is. Again, the example
would rather be a displayed newline where there
isn't one, or a non-displayed one where there
is one.

Tabs shouldn't be used, the exception being
makefiles because they (tabs) are mandatory
there for reasons unknown to me.

> The nice thing about Emacs is that it not
> only caters for my (or your) needs :-)

... what do you mean?

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Marcin Borkowski-3

On 2017-09-26, at 23:29, Emanuel Berg <[hidden email]> wrote:

> Newline are displayed as is. Again, the example
> would rather be a displayed newline where there
> isn't one [...]

A common situation, see visual-line-mode.  And for a good reason.

> Tabs shouldn't be used [...]

Seriously?

Best,

--
Marcin Borkowski

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Emanuel Berg-4
Marcin Borkowski wrote:

>> Newline are displayed as is. Again, the
>> example would rather be a displayed newline
>> where there isn't one [...]
>
> A common situation, see visual-line-mode.
> And for a good reason.

Well, I have 72 columns in the ttys where - in
one of the instances - I have Emacs. Actually,
I can only type 69 chars before the annoying
dollar sign appears. In X and xterm, tho
I don't write anything there really, I have
even less - 71 columns. You can find this out
with

    $ echo $COLUMNS

So if I can do it, sure most guys could as they
most likely have much more, and no one (?)
has less.

The only scenario where I see your point is if
one was working on a joint project, and some
bozos were writing insanely long lines. If that
ever happened, the only thing to do would be to
assign the application a new background color,
and clone it.

>> Tabs shouldn't be used [...]
>
> Seriously?

What do you think this is for?

    (setq-default indent-tabs-mode nil)

    (untabify (point-min) (point-max))

Just so a bunch of bozos can put the tabs
back in?

--
underground experts united
http://user.it.uu.se/~embe8573


Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Tomas Zerolo
In reply to this post by Emanuel Berg-4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Sep 26, 2017 at 11:29:08PM +0200, Emanuel Berg wrote:
> tomás wrote:
>
> > Why wouldn't you?
>
> Because it is confusing.

Sorry. Wrong question (the "you" is ambiguous -- it might
mean either "you, Emanuel" or the generic, impersonal "you".
I meant the latter.

[...]

My whole point can be reduced to two things:

 (1) what we perceive as "literal display" is already the result
    of a more or less complex mapping; its undeniable advantage
    is, of course, that it is very conventional and thus large
    swaths of that mapping are already well-understood and relatively
    stable. But it's no better or worse (and no "truer" or "falser")
    than any other display.

 (2) there are other needs, views and preferences than the ones
    we currently understand. They are as important as ours.

One of the things I really like Emacs for is that, while it carries
the "plain text" paradigm very far (making Emacs, among other things
eminently understandable and hackable [1]), it doesn't close the door
on (2) (for an example on this, see e.g. Widget).

Cheers

[1] The very Org mode is a living proof to that.
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlnLktoACgkQBcgs9XrR2kaJZACdHtvztGtmKXQIoFsXcZSJgeem
85IAnRgT6FARFheDX6gRES86vmsq5svP
=jlG5
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Modify text appearance (put spaces after commas just for display)

Tomas Zerolo
In reply to this post by Marcin Borkowski-3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Sep 27, 2017 at 08:23:35AM +0200, Marcin Borkowski wrote:

>
> On 2017-09-26, at 23:29, Emanuel Berg <[hidden email]> wrote:
>
> > Newline are displayed as is. Again, the example
> > would rather be a displayed newline where there
> > isn't one [...]
>
> A common situation, see visual-line-mode.  And for a good reason.
>
> > Tabs shouldn't be used [...]
>
> Seriously?

Yeah, that's one of the patterns over which I get my regular spats with
Emanuel. Luckily he's a pretty civilized arguer (hopefully he sees me
likewise):

I practically never use tabs (perhaps none or close to that in the last
15 years). But I'd *never* *ever* dare that sentence "tabs should not
be used". Who am I to forbid you the use of tabs?

If I had a colleague who insisted on tabs, I'd try to listen to her
reasons for tabs, I'd try to offer my reasons against them (always
talking about tabs in source code), and if no conviction happens,
I'd try to cope with the outcome however best.

I think last time around I tried to make my point to Emanuel with
the sentence "generalizations suck" or some such.

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlnLlI0ACgkQBcgs9XrR2kbajQCfag64K1AwrOSzzFnuWls02cDe
tdoAn3sPk2KP9bH3WVmiTSYTXd33TI94
=bWh6
-----END PGP SIGNATURE-----

12