emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Whitespace cleanup, tab-width and religion.


From: Michaël Cadilhac
Subject: Whitespace cleanup, tab-width and religion.
Date: Wed, 20 Dec 2006 15:11:07 +0100
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.91 (gnu/linux)

Well, this discussion has probably been made a few times, but I can't
find a thread.

First, some facts:
- Whitespace cleanup considers 8 (and only 8) spaces to be
a tabulation, and discards the fact that `tab-width' could have been
changed.
- On the contrary, `tabify' uses `tab-width'.

And  this is  inconsistent, as  I  see whitespace-cleanup  as a  super
tabify.delete-trailing-whitespace.do-some-magic function.

Second, a « mylife » part:
My company uses « tab » as indentation char. Not two spaces, not
whatever, but tabs. And it looks, well, IMHO, awful. For MY (very own)
eyes, I wish I could see tabs as 2 spaces, and this is hopefully
possible in the Mighty Editor by setting `tab-width' to 2.

Is it completely politically incorrect (or simply wrong) to consider
such a patch :

Index: lisp/whitespace.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/whitespace.el,v
retrieving revision 1.50
diff -c -r1.50 whitespace.el
*** lisp/whitespace.el  5 Dec 2006 06:55:48 -0000       1.50
--- lisp/whitespace.el  20 Dec 2006 13:50:35 -0000
***************
*** 32,39 ****
  ;;
  ;; 1. Leading space (empty lines at the top of a file).
  ;; 2. Trailing space (empty lines at the end of a file).
! ;; 3. Indentation space (8 or more spaces at beginning of line, that should be
! ;;                  replaced with TABS).
  ;; 4. Spaces followed by a TAB.  (Almost always, we never want that).
  ;; 5. Spaces or TABS at the end of a line.
  ;;
--- 32,39 ----
  ;;
  ;; 1. Leading space (empty lines at the top of a file).
  ;; 2. Trailing space (empty lines at the end of a file).
! ;; 3. Indentation space (`tab-width' or more spaces at beginning of
! ;;                       line, that should be replaced with TABS).
  ;; 4. Spaces followed by a TAB.  (Almost always, we never want that).
  ;; 5. Spaces or TABS at the end of a line.
  ;;
***************
*** 51,60 ****
  ;; If any of the whitespace checks is turned off, the modeline will display a
  ;; !<y>.
  ;;
! ;;     (since (3) is the most controversial one, here is the rationale: Most
! ;;     terminal drivers and printer drivers have TAB configured or even
! ;;     hardcoded to be 8 spaces.  (Some of them allow configuration, but 
almost
! ;;     always they default to 8.)
  ;;
  ;;     Changing `tab-width' to other than 8 and editing will cause your code 
to
  ;;     look different from within Emacs, and say, if you cat it or more it, or
--- 51,57 ----
  ;; If any of the whitespace checks is turned off, the modeline will display a
  ;; !<y>.
  ;;
! ;;     (For (3), here is a warning:
  ;;
  ;;     Changing `tab-width' to other than 8 and editing will cause your code 
to
  ;;     look different from within Emacs, and say, if you cat it or more it, or
***************
*** 62,68 ****
  ;;
  ;;     Almost all the popular programming modes let you define an offset (like
  ;;     c-basic-offset or perl-indent-level) to configure the offset, so you
! ;;     should never have to set your `tab-width' to be other than 8 in all
  ;;     these modes.  In fact, with an indent level of say, 4, 2 TABS will 
cause
  ;;     Emacs to replace your 8 spaces with one \t (try it).  If vi users in
  ;;     your office complain, tell them to use vim, which distinguishes between
--- 59,65 ----
  ;;
  ;;     Almost all the popular programming modes let you define an offset (like
  ;;     c-basic-offset or perl-indent-level) to configure the offset, so you
! ;;     probably never have to set your `tab-width' to be other than 8 in all
  ;;     these modes.  In fact, with an indent level of say, 4, 2 TABS will 
cause
  ;;     Emacs to replace your 8 spaces with one \t (try it).  If vi users in
  ;;     your office complain, tell them to use vim, which distinguishes between
***************
*** 205,213 ****
    :type 'boolean
    :group 'whitespace)
  
! (defcustom whitespace-indent-regexp "^\t*\\(        \\)+"
!   "Regexp to match multiples of eight spaces near line beginnings.
! The default value ignores leading TABs."
    :type 'regexp
    :group 'whitespace)
  
--- 202,211 ----
    :type 'boolean
    :group 'whitespace)
  
! (defcustom whitespace-indent-regexp "^\t*\\( \\{%d\\}\\)+"
!   "Regexp to match multiples of an amount of spaces near line beginnings.
! The default value ignores leading TABs.
! If a `%d' is in the string, it is replaced by the value of `tab-width'."
    :type 'regexp
    :group 'whitespace)
  
***************
*** 378,384 ****
      (message "Will%s check for indentation space in buffer."
             (if whitespace-check-buffer-indent "" " not"))
      (if whitespace-check-buffer-indent
!       (whitespace-buffer-search whitespace-indent-regexp))))
  
  ;;;###autoload
  (defun whitespace-toggle-spacetab-check ()
--- 376,382 ----
      (message "Will%s check for indentation space in buffer."
             (if whitespace-check-buffer-indent "" " not"))
      (if whitespace-check-buffer-indent
!       (whitespace-buffer-search (whitespace-indent-regexp)))))
  
  ;;;###autoload
  (defun whitespace-toggle-spacetab-check ()
***************
*** 410,416 ****
  These are:
  1. Leading space \(empty lines at the top of a file\).
  2. Trailing space \(empty lines at the end of a file\).
! 3. Indentation space \(8 or more spaces, that should be replaced with TABS\).
  4. Spaces followed by a TAB. \(Almost always, we never want that\).
  5. Spaces or TABS at the end of a line.
  
--- 408,415 ----
  These are:
  1. Leading space \(empty lines at the top of a file\).
  2. Trailing space \(empty lines at the end of a file\).
! 3. Indentation space \(`tab-width' or more spaces, that should be replaced
!                       with TABS\).
  4. Spaces followed by a TAB. \(Almost always, we never want that\).
  5. Spaces or TABS at the end of a line.
  
***************
*** 440,446 ****
                                         nil))
                  (whitespace-indent (if whitespace-check-buffer-indent
                                         (whitespace-buffer-search
!                                         whitespace-indent-regexp)
                                       nil))
                  (whitespace-spacetab (if whitespace-check-buffer-spacetab
                                           (whitespace-buffer-search
--- 439,445 ----
                                         nil))
                  (whitespace-indent (if whitespace-check-buffer-indent
                                         (whitespace-buffer-search
!                                         (whitespace-indent-regexp))
                                       nil))
                  (whitespace-spacetab (if whitespace-check-buffer-spacetab
                                           (whitespace-buffer-search
***************
*** 531,543 ****
    ;; If this buffer really contains a file, then run, else quit.
    (whitespace-check-whitespace-mode current-prefix-arg)
    (if (and buffer-file-name whitespace-mode)
!       (let ((whitespace-any nil)
!           (whitespace-tabwith 8)
!           (whitespace-tabwith-saved tab-width))
! 
!       ;; since all printable TABS should be 8, irrespective of how
!       ;; they are displayed.
!       (setq tab-width whitespace-tabwith)
  
        (if (and whitespace-check-buffer-leading
                 (whitespace-buffer-leading))
--- 530,536 ----
    ;; If this buffer really contains a file, then run, else quit.
    (whitespace-check-whitespace-mode current-prefix-arg)
    (if (and buffer-file-name whitespace-mode)
!       (let ((whitespace-any nil))
  
        (if (and whitespace-check-buffer-leading
                 (whitespace-buffer-leading))
***************
*** 552,558 ****
              (setq whitespace-any t)))
  
        (if (and whitespace-check-buffer-indent
!                (whitespace-buffer-search whitespace-indent-regexp))
            (progn
              (whitespace-indent-cleanup)
              (setq whitespace-any t)))
--- 545,551 ----
              (setq whitespace-any t)))
  
        (if (and whitespace-check-buffer-indent
!                (whitespace-buffer-search (whitespace-indent-regexp)))
            (progn
              (whitespace-indent-cleanup)
              (setq whitespace-any t)))
***************
*** 578,585 ****
              (if region-only
                  (message "The region is now clean")
                (message "%s is now clean" buffer-file-name)))
!           (whitespace-update-modeline)))
!       (setq tab-width whitespace-tabwith-saved))))
  
  ;;;###autoload
  (defun whitespace-cleanup-region (s e)
--- 571,578 ----
              (if region-only
                  (message "The region is now clean")
                (message "%s is now clean" buffer-file-name)))
!           (whitespace-update-modeline))))))
! 
  
  ;;;###autoload
  (defun whitespace-cleanup-region (s e)
***************
*** 647,661 ****
      (while (re-search-forward regexp nil t)
        (replace-match newregexp))))
  
  (defun whitespace-indent-cleanup ()
!   "Search for 8/more spaces at the start of a line and replace it with tabs."
    (save-excursion
      (goto-char (point-min))
!     (while (re-search-forward whitespace-indent-regexp nil t)
!       (let ((column (current-column))
!           (indent-tabs-mode t))
!       (delete-region (match-beginning 0) (point))
!       (indent-to column)))))
  
  (defun whitespace-unchecked-whitespaces ()
    "Return the list of whitespaces whose testing has been suppressed."
--- 640,660 ----
      (while (re-search-forward regexp nil t)
        (replace-match newregexp))))
  
+ (defun whitespace-indent-regexp ()
+   "Give the actual regexp that matches spaces to be replaced by tabs."
+   (format whitespace-indent-regexp tab-width))
+ 
  (defun whitespace-indent-cleanup ()
!   "Search for `tab-width'/more spaces at line starts and replace it with tabs.
! See `whitespace-indent-regexp' for how these spaces are searched."
    (save-excursion
      (goto-char (point-min))
!     (let ((indent-regexp (whitespace-indent-regexp)))
!       (while (re-search-forward indent-regexp nil t)
!       (let ((column (current-column))
!             (indent-tabs-mode t))
!         (delete-region (match-beginning 0) (point))
!         (indent-to column))))))
  
  (defun whitespace-unchecked-whitespaces ()
    "Return the list of whitespaces whose testing has been suppressed."
Index: lisp/ChangeLog
===================================================================
RCS file: /sources/emacs/emacs/lisp/ChangeLog,v
retrieving revision 1.10462
diff -C0 -r1.10462 ChangeLog
*** lisp/ChangeLog      20 Dec 2006 05:57:40 -0000      1.10462
--- lisp/ChangeLog      20 Dec 2006 13:50:44 -0000
***************
*** 0 ****
--- 1,12 ----
+ 2006-12-20  Michaël Cadilhac  <address@hidden>
+ 
+       * whitespace.el: Change comments to say that `tab-width' is used
+       instead of 8.
+       (whitespace-indent-regexp): (Variable) Use a `%d' that will be
+       replaced by `tab-width'.
+       (whitespace-indent-regexp): (Function) New.  Compute the actual
+       value of `whitespace-indent-regexp' and return it.
+       (whitespace-toggle-indent-check, whitespace-buffer)
+       (whitespace-cleanup-internal, whitespace-indent-cleanup): Use it.
+       (whitespace-buffer): Doc change.
+ 
TIA!

-- 
/!\ My mail address has changed, please update your files accordingly.
 |      Michaël `Micha' Cadilhac     |  ... KVim is cited in the talk.        |
 |         Epita/LRDE Promo 2007     |   "I can't tell if I am more sorry     |
 |  http://michael.cadilhac.name     |        for vim or for KDE."            |
 `--JID: address@hidden'          -- RMS                   -  --'

Attachment: pgpLveXFBlQZ5.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]