emacs-devel
[Top][All Lists]
Advanced

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

Re: [Proposal] M-x tabify to indent only when needed.


From: Stefan Monnier
Subject: Re: [Proposal] M-x tabify to indent only when needed.
Date: Mon, 24 Jul 2006 17:30:42 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

>         (while (re-search-forward tabify-regexp nil t)
>       (let ((column (current-column))
>             (indent-tabs-mode t))
> !       ;; Whether already tabified.
> !       (unless (save-match-data
> !                 (save-excursion
> !                   (skip-chars-backward " ")
> !                   (string-match
> !                    (format "^\t* \\{0,%d\\}$"
> !                            (- tab-width (1+ (mod (current-column) 
> tab-width))))
> !                    (match-string 0))))
> !         (delete-region (match-beginning 0) (point))
> !         (indent-to column)))))))

I'm not convinced it's correct.

I think we should only accept such a patch if the correctness is clear
(either because the code's correctnes is inherently obvious, or because it
is sufficiently commented to explain why it's correct).

How 'bout the 100% untested patch below?


        Stefan


--- orig/lisp/tabify.el
+++ mod/lisp/tabify.el
@@ -50,9 +50,9 @@
          (delete-region tab-beg (point))
          (indent-to column))))))
 
-(defvar tabify-regexp "[ \t][ \t]+"
+(defvar tabify-regexp " [ \t]+"
   "Regexp matching whitespace that tabify should consider.
-Usually this will be \"[ \\t][ \\t]+\" to match two or more spaces or tabs.
+Usually this will be \" [ \\t]+\" to match two or more spaces or tabs.
 \"^[ \\t]+\" is also useful, for tabifying only initial whitespace.")
 
 ;;;###autoload
@@ -73,12 +73,20 @@
       (narrow-to-region (point) end)
       (goto-char start)
       (while (re-search-forward tabify-regexp nil t)
-       (let ((column (current-column))
-             (indent-tabs-mode t))
-         (delete-region (match-beginning 0) (point))
-         (indent-to column))))))
+        ;; The region between (match-beginning 0) and (match-end 0) is just
+        ;; spacing which we want to adjust to use TABs where possible.
+        (let ((end-col (current-column))
+              (beg-col (save-excursion (goto-char (match-beginning 0))
+                                       (current-column))))
+          (if (= (/ end-col tab-width) (/ beg-col tab-width))
+              ;; The spacing does not straddle a TAB boundary, so we won't
+              ;; be able to use a TAB here anyway: there's nothing to do.
+              nil
+            (let ((indent-tabs-mode t))
+              (delete-region (match-beginning 0) (point))
+              (indent-to end-col))))))))
 
 (provide 'tabify)
 
-;;; arch-tag: c83893b1-e0cc-4e57-8a09-73fd03466416
+;; arch-tag: c83893b1-e0cc-4e57-8a09-73fd03466416
 ;;; tabify.el ends here




reply via email to

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