[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: detect if line has only white space?
From: |
Gian Uberto Lauri |
Subject: |
Re: detect if line has only white space? |
Date: |
Mon, 10 Nov 2003 19:39:51 +0100 |
>>>>> "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
\/
- detect if line has only white space?, Miguel Frasson, 2003/11/10
- Re: detect if line has only white space?, marc0, 2003/11/10
- Re: detect if line has only white space?, Phillip Lord, 2003/11/10
- Re: detect if line has only white space?, Johan Bockgård, 2003/11/10
- Re: detect if line has only white space?, jan, 2003/11/10
- Re: detect if line has only white space?, François Fleuret, 2003/11/11