Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

whitespace issues

25 views
Skip to first unread message

Rusi

unread,
Jun 30, 2015, 10:17:41 AM6/30/15
to
I have two somewhat related questions, both around whitespace:

1. Is it possible to have some sort of save-hook so that all leading (indentation) whitespace is cleaned up to space alone and tabs are removed?


2. How to make a dirty crlf file in emacs?
By dirty I mean, some lines ending LF (ie Unix) and some ending CRLF (ie DOS)
[This is to test that such dirty-files are not add-able into git]

to...@tuxteam.de

unread,
Jun 30, 2015, 10:54:46 AM6/30/15
to Rusi, help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Jun 30, 2015 at 07:17:39AM -0700, Rusi wrote:
> I have two somewhat related questions, both around whitespace:
>
> 1. Is it possible to have some sort of save-hook so that all leading (indentation) whitespace is cleaned up to space alone and tabs are removed?

I'll leave that to more knowledgeable.

> 2. How to make a dirty crlf file in emacs?
> By dirty I mean, some lines ending LF (ie Unix) and some ending CRLF (ie DOS)
> [This is to test that such dirty-files are not add-able into git]

Since CR == CTLR-M, create a file with a buffer coding system
whatever-nix (e.g. utf8-unix). This is the default on a sensible
operating system anyway :-)

At the end of a line, type CTRL-Q CTRL-M -- voilà, this line has crlf
at its end. Check either with hexl-mode, or outside Emacs with hexdump -C.

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlWSraQACgkQBcgs9XrR2kZ89ACfSaNmH/5SeCDcV5r+yAnVZbig
MtMAn2ogT/O7Z6vHpFBctGwobLonf+HG
=MoaO
-----END PGP SIGNATURE-----

Rusi

unread,
Jun 30, 2015, 10:57:58 AM6/30/15
to
On Tuesday, June 30, 2015 at 8:24:46 PM UTC+5:30, to...@tuxteam.de wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Tue, Jun 30, 2015 at 07:17:39AM -0700, Rusi wrote:
> > I have two somewhat related questions, both around whitespace:
> >
> > 1. Is it possible to have some sort of save-hook so that all leading (indentation) whitespace is cleaned up to space alone and tabs are removed?
>
> I'll leave that to more knowledgeable.
>
> > 2. How to make a dirty crlf file in emacs?
> > By dirty I mean, some lines ending LF (ie Unix) and some ending CRLF (ie DOS)
> > [This is to test that such dirty-files are not add-able into git]
>
> Since CR == CTLR-M, create a file with a buffer coding system
> whatever-nix (e.g. utf8-unix). This is the default on a sensible
> operating system anyway :-)
>
> At the end of a line, type CTRL-Q CTRL-M -- voilà, this line has crlf
> at its end. Check either with hexl-mode, or outside Emacs with hexdump -C.

Neat! Thanks!
[Its 2015 and our version systems cant get this right]

Jorge A. Alfaro-Murillo

unread,
Jun 30, 2015, 11:02:21 AM6/30/15
to help-gn...@gnu.org
Rusi writes:

> I have two somewhat related questions, both around whitespace:
>
> 1. Is it possible to have some sort of save-hook so that all
> leading (indentation) whitespace is cleaned up to space alone
> and tabs are removed?

(add-hook 'before-save-hook
(lambda () (save-excursion (untabify (point-min)
(point-max)))))

> 2. How to make a dirty crlf file in emacs? By dirty I mean, some
> lines ending LF (ie Unix) and some ending CRLF (ie DOS) [This is
> to test that such dirty-files are not add-able into git]

This I don't know.

Best,
--
Jorge.


Óscar Fuentes

unread,
Jun 30, 2015, 11:06:37 AM6/30/15
to help-gn...@gnu.org
Rusi <rusto...@gmail.com> writes:

> I have two somewhat related questions, both around whitespace:
>
> 1. Is it possible to have some sort of save-hook so that all leading
> (indentation) whitespace is cleaned up to space alone and tabs are
> removed?

https://github.com/purcell/whitespace-cleanup-mode

I use https://github.com/lewang/ws-butler which smarter in the sense
that only cleans the lines you touched.

[snip]


Ian Zimmerman

unread,
Jun 30, 2015, 11:24:55 AM6/30/15
to help-gn...@gnu.org
On 2015-06-30 07:17 -0700, Rusi wrote:

> 1. Is it possible to have some sort of save-hook so that all leading
> (indentation) whitespace is cleaned up to space alone and tabs are
> removed?

Yes, see write-file-functions and untabify

> 2. How to make a dirty crlf file in emacs? By dirty I mean, some
> lines ending LF (ie Unix) and some ending CRLF (ie DOS)

Emacs is probably the wrong tool for this, I would try some combination
of sed, tr, awk, jot

--
Please *no* private copies of mailing list or newsgroup messages.
Rule 420: All persons more than eight miles high to leave the court.


Rusi

unread,
Jun 30, 2015, 12:21:14 PM6/30/15
to
On Tuesday, June 30, 2015 at 8:32:21 PM UTC+5:30, Jorge A. Alfaro-Murillo wrote:
> Rusi writes:
>
> > I have two somewhat related questions, both around whitespace:
> >
> > 1. Is it possible to have some sort of save-hook so that all
> > leading (indentation) whitespace is cleaned up to space alone
> > and tabs are removed?
>
> (add-hook 'before-save-hook
> (lambda () (save-excursion (untabify (point-min)
> (point-max)))))

How to have this only for (say) python-mode?

Dan Espen

unread,
Jun 30, 2015, 12:27:12 PM6/30/15
to
Rusi <rusto...@gmail.com> writes:

> I have two somewhat related questions, both around whitespace:
>
> 1. Is it possible to have some sort of save-hook so that all leading
> (indentation) whitespace is cleaned up to space alone and tabs are
> removed?

Yes, but if you set indent-tabs-mode to nil hitting tab will
insert spaces instead of tabs so you won't have to remove tabs.

--
Dan Espen

Rasmus

unread,
Jun 30, 2015, 12:35:44 PM6/30/15
to help-gn...@gnu.org
Rusi <rusto...@gmail.com> writes:

> How to have this only for (say) python-mode?

Condition on the mode, (member major-mode list-of-rm-tab-modes).

Rasmus

--
Warning: Everything saved will be lost


Emanuel Berg

unread,
Jun 30, 2015, 7:27:21 PM6/30/15
to help-gn...@gnu.org
Rusi <rusto...@gmail.com> writes:

> 1. Is it possible to have some sort of save-hook so
> that all leading (indentation) whitespace is cleaned
> up to space alone and tabs are removed?

Do this and indentation will be in whitespaces only:

(setq-default indent-tabs-mode nil)

Only in Makefiles you still get tabs - otherwise they
(the Makefiles) won't work.

But to answer your question, yes, you can use
`before-save-hook' and `untabify'.

If you want to add exceptions, it could look like
this:

(defun untab-all ()
(unless (member major-mode '(makefile-gmake-mode
makefile-mode) ) ; exceptions
(untabify (point-min) (point-max)))
nil) ; tell 'did not write buffer to disk'

But I think it is better to have the file as you edit
it be as close to the finished form as possible, and
not have it post-processed like that.

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


Rusi

unread,
Jun 30, 2015, 10:37:20 PM6/30/15
to
On Tuesday, June 30, 2015 at 8:36:37 PM UTC+5:30, Óscar Fuentes wrote:
Thanks Oscar -- looks interesting.
But this is not so much for me as for my kids er... students, and would like to
minimize moving parts like extra installs

Rusi

unread,
Jun 30, 2015, 10:50:06 PM6/30/15
to
On Wednesday, July 1, 2015 at 4:57:21 AM UTC+5:30, Emanuel Berg wrote:
Ok thanks Emanuel, Jorge
Putting your two together I have

--------------------
(add-hook 'before-save-hook
'untab-python
)

(defun untab-python ()
(when (member major-mode '(python-mode))
(untabify (point-min) (point-max)))
nil)
----------------------

1. dunno about the save-excursion
2. member rather than equal/eq so that other modes can be added as desired

to...@tuxteam.de

unread,
Jul 1, 2015, 4:07:07 AM7/1/15
to Rusi, help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Jun 30, 2015 at 07:37:17PM -0700, Rusi wrote:
> On Tuesday, June 30, 2015 at 8:36:37 PM UTC+5:30, Óscar Fuentes wrote:
> > Rusi writes:
> >
> > > I have two somewhat related questions, both around whitespace:
> > >
> > > 1. Is it possible to have some sort of save-hook so that all leading
> > > (indentation) whitespace is cleaned up to space alone and tabs are
> > > removed?
> >
> > https://github.com/purcell/whitespace-cleanup-mode

I don't know whether it has been mentioned in the thread, but there's
a "whitespace-cleanup" which is part of the standard Emacs distribution.

I use it from time to time, whenever I suspect that whitespace is
messed up in a file. Very handy.

- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlWTn58ACgkQBcgs9XrR2kbVVwCeIw3pwcPaDWH+vL8WZ3g8Y6l4
nPoAn0zOEPp8X1EQ6gqfKlswnvUfO+B6
=Favr
-----END PGP SIGNATURE-----

Jorge A. Alfaro-Murillo

unread,
Jul 1, 2015, 10:32:20 AM7/1/15
to help-gn...@gnu.org
Rusi writes:

> 1. dunno about the save-excursion

That makes saves the point, the mark, and the current buffer and
the restores those things after it is done. I don't think you need
it in this case as untabify, point-min and point-max don't move
the point or the mark.

--
Jorge.


Emanuel Berg

unread,
Jul 1, 2015, 1:16:28 PM7/1/15
to help-gn...@gnu.org
Rusi <rusto...@gmail.com> writes:

> (add-hook 'before-save-hook
> 'untab-python
> )
>
> (defun untab-python ()
> (when (member major-mode '(python-mode))
> (untabify (point-min) (point-max)))
> nil)
>
> 1. dunno about the save-excursion

You don't need it. Examine `untabify' as that is the
only thing that could possible be the reason to use
it. It isn't, but when in doubt, isolate what is
doubted and test it.

> 2. member rather than equal/eq so that other modes
> can be added as desired

Indeed, however then should it still be named
"untab-python"? Unless there are several Python modes,
which OTOH wouldn't surprise me considering how much
extracurriculuous activity that comes with...
0 new messages