|
From: | Vinicius Jose Latorre |
Subject: | Re: Proposal: diff-remove-trailing-blanks |
Date: | Sun, 27 Apr 2008 14:30:04 -0300 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080313 SeaMonkey/1.1.9 |
(while (re-search-forward "^[+!>] .*?[ \t]+$" (point-max) t)In unified diffs, there is no white space after `+'. In other diff types, its seems that there is: +foo (changed or added line, unified diff)foo (changed or added line, normal diff)! foo (changed line, context diff) + foo (added line, context diff) The solution is to test the format type and adapt the regexp for it: (diff-beginning-of-hunk 'try-harder) (setq (diff-hunk-style)) (case style (unified (setq re ...)) ;; (+) (context (setq re ...)) ;; (+! ) (t (setq re ...))) ;; (> ) [snip](if modified-buffers (let ((whitespace-style '(trailing)) (whitespace-trailing-regexp "^[+!>] .*?\\([\t ]+\\)$"))Likewise.(whitespace-mode 1) ; display trailing blanks in diff bufferSeeing that the diff introduces trailing whitespace is most interesting before removing it too. Actually, you want to highlight trailing withespace on the diff for knowing that you should invoke `diff-remove-trailing-blanks'. (A nice thing would be to automatically test the diff and put something on the buffer's modeline indicating that the diff introduces trailing whitespace. `diff-mode-hook' is handy for this). [snip]
Ok, so, maybe the functions below fix all of this. (defun diff-remove-trailing-blanks () "When on a buffer that contains a diff, inspects the differences and removes trailing whitespace (spaces, tabs) from the lines modified or introduced by this diff. Shows a message with the name of the altered buffers, which are unsaved. If a file referenced on the diff has no buffer and needs to be fixed, a buffer visiting that file is created." (interactive) (save-excursion ;; We assume that the diff header has no trailing whitespace. (let (modified-buffers white-positions) (goto-char (point-min)) (while (re-search-forward "^[+!>].*?[ \t]+$" (point-max) t) (save-excursion(destructuring-bind (buf line-offset pos src dst &optional switched)
(diff-find-source-location t t) (when line-offset (save-excursion (set-buffer buf) (goto-char (+ (car pos) (cdr src))) (beginning-of-line)(when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
(unless (member buf modified-buffers) (push buf modified-buffers)) (push buf white-positions) (push (match-beginning 0) white-positions) (push (match-end 0) white-positions))))))) (setq white-positions (nreverse white-positions)) (while white-positions (save-excursion (set-buffer (pop white-positions)) (delete-region (pop white-positions) (pop white-positions)))) (if modified-buffers (message "Deleted trailing whitespace from: %s" (mapconcat #'(lambda (buf) (format "`%s'" (buffer-name buf))) modified-buffers " ")) (message "No fixes needed."))))) (defun diff-show-trailing-blanks () "Show trailing blanks in modified lines for diff-mode." (interactive) (let ((whitespace-style '(trailing)) (whitespace-trailing-regexp "^[+!>].*?\\([\t ]+\\)$")) (whitespace-mode 1))) ; display trailing blanks in diff buffer (add-hook 'diff-mode-hook 'diff-show-trailing-blanks)
[Prev in Thread] | Current Thread | [Next in Thread] |