unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: GianUberto.Lauri@eng.it (Gian Uberto Lauri)
Cc: help-gnu-emacs@gnu.org
Subject: Re: detect if line has only white space?
Date: Mon, 10 Nov 2003 19:39:51 +0100	[thread overview]
Message-ID: <16303.56183.397055.26656@mail.eng.it> (raw)
In-Reply-To: <87r80gdse3.fsf@despammed.com>

>>>>> "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 
\/

  reply	other threads:[~2003-11-10 18:39 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=16303.56183.397055.26656@mail.eng.it \
    --to=gianuberto.lauri@eng.it \
    --cc=help-gnu-emacs@gnu.org \
    --cc=saint@eng.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).