Am Dienstag, 07. Oktober 2008 14:44:13 schrieb Dirk Meyer:
> > I am using some emacs code that makes sure that there is no trailing
> > whitespace in my source code; occasionally this will remove trailing
> > whitespace existing in SVN
>
> Can you give me that snippet? I once had a hook that stripped
> whitespaces on every safe which was a bad idea when editing signature
> files. :) I would like to have it only active for Python mode.

Here it is:

;---------------------------------------------------------------------

(defun hlm-clean-trailing-space (&optional only-non-empty)
  "Removes spaces at ends of lines, useful in find-file-hooks.
If optional parameter only-non-empty is non-nil, removes only trailing
whitespace of non-empty lines."
  (interactive)
  (save-excursion
        (goto-char (point-min))
        (let ((count 0)
                  (bmp (buffer-modified-p))
                  (unwanted-whitespace-re (if only-non-empty
                                                                          
"[^\n\t ]\\([\t ]+\\)$"
                                                                        "\\([\t 
]+\\)$")))
          (while (re-search-forward unwanted-whitespace-re nil t)
                (replace-match "" t t nil 1)
                (setq count (1+ count)))
          (and (> count 0)
                   (progn
                         (set-buffer-modified-p bmp)
                         (message "Cleaned trailing space from %d lines" 
count))))))

(add-hook 'find-file-hooks
  '(lambda ()
         ;; don't do this in LyX, TeX or diff files, or if the buffer is 
read-only:
         (or
          (string-match "\\.\\(lyx\\|tex\\)$" (buffer-file-name))
          (eq major-mode 'diff-mode)
          buffer-read-only
          (hlm-clean-trailing-space (eq major-mode 'python-mode)))))

;---------------------------------------------------------------------

Probably, only-non-empty should be set to true for all programming language 
modes (due to indentation of blocks which may contain "empty" lines).
The LyX part is for LyX versions < 1.4 IIRC, where spaces at EOL very really 
significant, but that was "fixed" upstream.

Ciao, /  /
     /--/
    /  / ANS

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Freevo-devel mailing list
Freevo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freevo-devel

Reply via email to