* detect if line has only white space?
@ 2003-11-10 17:42 Miguel Frasson
2003-11-10 17:47 ` Phillip Lord
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: Miguel Frasson @ 2003-11-10 17:42 UTC (permalink / raw)
Hello.
I would like to have a elisp function that detects if the current line
contains only white space. Some idea?
Aditionally, I would like to remove all space at the end of the
lines at once (e-lisp code). How?
Many thanks.
Miguel.
--
Miguel Vinicius Santini Frasson
http://www.math.leidenuniv.nl/~frasson
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:42 detect if line has only white space? Miguel Frasson
@ 2003-11-10 17:47 ` Phillip Lord
2003-11-10 17:51 ` marc0
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Phillip Lord @ 2003-11-10 17:47 UTC (permalink / raw)
>>>>> "Miguel" == Miguel Frasson <frasson@enquist.math.leidenuniv.nl> writes:
Miguel> Hello.
Miguel> I would like to have a elisp function that detects if the
Miguel> current line contains only white space. Some idea?
Miguel> Aditionally, I would like to remove all space at the end of
Miguel> the lines at once (e-lisp code). How?
(forward-word) will move past the end of the line.
Or you get check whether a white space only regexp match is the same
length as the entire line.
Others may have better suggestions.
Cheers
Phil
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:42 detect if line has only white space? Miguel Frasson
2003-11-10 17:47 ` Phillip Lord
@ 2003-11-10 17:51 ` marc0
2003-11-10 18:39 ` Gian Uberto Lauri
2003-11-13 13:47 ` LEE Sau Dan
2003-11-10 18:40 ` Johan Bockgård
` (2 subsequent siblings)
4 siblings, 2 replies; 8+ messages in thread
From: marc0 @ 2003-11-10 17:51 UTC (permalink / raw)
Miguel Frasson on 10 Nov 2003 18:42:56 +0100 writes:
> I would like to have a elisp function that detects if the current line
> contains only white space. Some idea?
matching the current line content with the regexp ^[ ]*$ ?
> Aditionally, I would like to remove all space at the end of the
> lines at once (e-lisp code). How?
M-x replace-regexp RET [ ]*$ RET RET
--
marc0@autistici.org - 0x4E8899C2
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:51 ` marc0
@ 2003-11-10 18:39 ` Gian Uberto Lauri
2003-11-13 13:47 ` LEE Sau Dan
1 sibling, 0 replies; 8+ messages in thread
From: Gian Uberto Lauri @ 2003-11-10 18:39 UTC (permalink / raw)
Cc: help-gnu-emacs
>>>>> "m" == marc0 <marc0@autistici.org> writes:
m> Miguel Frasson on 10 Nov 2003 18:42:56 +0100 writes:
>> I would like to have a elisp function that detects if the current line
>> contains only white space. Some idea?
m> matching the current line content with the regexp ^[ ]*$ ?
>> Aditionally, I would like to remove all space at the end of the
>> lines at once (e-lisp code). How?
m> M-x replace-regexp RET [ ]*$ RET RET
;; I have this little piece of code in my .emacs (i think it comes from this list!):
(defun remove-trailing-blanks (&optional ask)
"Removes useless blanks from a buffer.
Removes trailing spaces and tabs from every line in the current buffer,
and trailing newlines from the end of the buffer, apart from one.
If ASK is non-nil, ask for confirmation."
(if (and (not (zerop (buffer-size)))
(char-equal (char-after (buffer-size)) ?
)
(save-excursion
(save-restriction
(save-match-data
(widen)
(goto-char 0)
(or (search-forward "
" nil t)
(search-forward "
" nil t)
(re-search-forward "
\`" nil t)))))
(if ask
(y-or-n-p "Remove trailing spaces and newlines before saving? ")
(message "Removing trailing spaces and newlines...")
t))
(save-excursion
(save-restriction
(save-match-data
(widen)
(goto-char 0)
(while (re-search-forward "[ ]+$" nil `move)
(replace-match ""))
(if (bolp)
(progn
(skip-chars-backward "
")
(delete-region (1+ (point)) (point-max))))
)))))
;; that can be hooked to several modes:
;;; Remove trailing blanks and newlines before saving a text buffer.
(add-hook 'text-mode-hook 'install-remove-trailing-blanks-ask)
(add-hook 'emacs-lisp-mode-hook 'install-remove-trailing-blanks)
(add-hook 'c-mode-hook 'install-remove-trailing-blanks)
(add-hook 'c++-mode-hook 'install-remove-trailing-blanks)
(add-hook 'octave-mode-hook 'install-remove-trailing-blanks)
(add-hook 'jde-mode-hook 'install-remove-trailing-blanks)
(defun install-remove-trailing-blanks ()
(add-hook 'write-contents-hooks 'remove-trailing-blanks))
(defun install-remove-trailing-blanks-ask ()
(add-hook 'write-contents-hooks '(lambda () (remove-trailing-blanks t))))
/\ ___
/___/\__|_|\_|__|___Gian Uberto Lauri_____________________
//--\ | | \| | Integralista GNUslamico e fancazzista
\/
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:42 detect if line has only white space? Miguel Frasson
2003-11-10 17:47 ` Phillip Lord
2003-11-10 17:51 ` marc0
@ 2003-11-10 18:40 ` Johan Bockgård
2003-11-11 12:13 ` François Fleuret
2003-11-11 21:38 ` jan
4 siblings, 0 replies; 8+ messages in thread
From: Johan Bockgård @ 2003-11-10 18:40 UTC (permalink / raw)
Miguel Frasson <frasson@enquist.math.leidenuniv.nl> writes:
> Aditionally, I would like to remove all space at the end of the
> lines at once (e-lisp code). How?
,----[ C-h f delete-trailing-whitespace RET ]
| delete-trailing-whitespace is an interactive compiled Lisp function in `simple'.
| (delete-trailing-whitespace)
|
| Delete all the trailing whitespace across the current buffer.
| All whitespace after the last non-whitespace character in a line is deleted.
| This respects narrowing, created by C-x n n and friends.
| A formfeed is not considered whitespace by this function.
`----
--
Johan Bockgård
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:42 detect if line has only white space? Miguel Frasson
` (2 preceding siblings ...)
2003-11-10 18:40 ` Johan Bockgård
@ 2003-11-11 12:13 ` François Fleuret
2003-11-11 21:38 ` jan
4 siblings, 0 replies; 8+ messages in thread
From: François Fleuret @ 2003-11-11 12:13 UTC (permalink / raw)
> Aditionally, I would like to remove all space at the end of the
> lines at once (e-lisp code).
delete-trailing-whitespace
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:42 detect if line has only white space? Miguel Frasson
` (3 preceding siblings ...)
2003-11-11 12:13 ` François Fleuret
@ 2003-11-11 21:38 ` jan
4 siblings, 0 replies; 8+ messages in thread
From: jan @ 2003-11-11 21:38 UTC (permalink / raw)
Cc: help-gnu-emacs
Miguel Frasson <frasson@enquist.math.leidenuniv.nl> writes:
> I would like to have a elisp function that detects if the current
> line contains only white space. Some idea?
Probably not the best way to do it, but this is from my utils file
(defun line-empty-p ()
"Returns non-nil if current line is empty."
(save-excursion
(end-of-line)
(let ((eol (point)))
(beginning-of-line)
(not (re-search-forward "[^ \t]" eol t)))))
--
jan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: detect if line has only white space?
2003-11-10 17:51 ` marc0
2003-11-10 18:39 ` Gian Uberto Lauri
@ 2003-11-13 13:47 ` LEE Sau Dan
1 sibling, 0 replies; 8+ messages in thread
From: LEE Sau Dan @ 2003-11-13 13:47 UTC (permalink / raw)
>>>>> "marc0" == marc0 <marc0@autistici.org> writes:
marc0> Miguel Frasson on 10 Nov 2003 18:42:56 퍝 writes:
>> I would like to have a elisp function that detects if the
>> current line contains only white space. Some idea?
marc0> matching the current line content with the regexp ^[ ]*$ ?
I've just checked and my Emacs (21.2.1) support the posix class syntax
in regex. So, the above would be better written as:
^[[:space:]]*$
--
Lee Sau Dan 李守敦(Big5) ~{@nJX6X~}(HZ)
E-mail: danlee@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2003-11-13 13:47 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-10 17:42 detect if line has only white space? Miguel Frasson
2003-11-10 17:47 ` Phillip Lord
2003-11-10 17:51 ` marc0
2003-11-10 18:39 ` Gian Uberto Lauri
2003-11-13 13:47 ` LEE Sau Dan
2003-11-10 18:40 ` Johan Bockgård
2003-11-11 12:13 ` François Fleuret
2003-11-11 21:38 ` jan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).