From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [Proposal] M-x tabify to indent only when needed. Date: Mon, 24 Jul 2006 17:30:42 -0400 Message-ID: References: <8764hm7vwo.fsf@lrde.org> <87irlm68k0.fsf@lrde.org> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1153776665 28381 80.91.229.2 (24 Jul 2006 21:31:05 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 24 Jul 2006 21:31:05 +0000 (UTC) Cc: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jul 24 23:31:03 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1G5811-0005GN-8w for ged-emacs-devel@m.gmane.org; Mon, 24 Jul 2006 23:31:03 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G5810-0007cU-GG for ged-emacs-devel@m.gmane.org; Mon, 24 Jul 2006 17:31:02 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1G580o-0007af-N7 for emacs-devel@gnu.org; Mon, 24 Jul 2006 17:30:50 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1G580n-0007ZE-TP for emacs-devel@gnu.org; Mon, 24 Jul 2006 17:30:49 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G580n-0007Yt-Hw for emacs-devel@gnu.org; Mon, 24 Jul 2006 17:30:49 -0400 Original-Received: from [132.204.24.67] (helo=mercure.iro.umontreal.ca) by monty-python.gnu.org with esmtp (Exim 4.52) id 1G581y-0001uk-3b for emacs-devel@gnu.org; Mon, 24 Jul 2006 17:32:02 -0400 Original-Received: from hidalgo.iro.umontreal.ca (hidalgo.iro.umontreal.ca [132.204.27.50]) by mercure.iro.umontreal.ca (Postfix) with ESMTP id 031122CEB78; Mon, 24 Jul 2006 17:30:49 -0400 (EDT) Original-Received: from faina.iro.umontreal.ca (faina.iro.umontreal.ca [132.204.26.177]) by hidalgo.iro.umontreal.ca (Postfix) with ESMTP id C9E4A445C; Mon, 24 Jul 2006 17:30:42 -0400 (EDT) Original-Received: by faina.iro.umontreal.ca (Postfix, from userid 20848) id B3B066C9DD; Mon, 24 Jul 2006 17:30:42 -0400 (EDT) Original-To: michael.cadilhac@lrde.org (=?iso-8859-1?Q?Micha=EBl?= Cadilhac) In-Reply-To: <87irlm68k0.fsf@lrde.org> (=?iso-8859-1?Q?Micha=EBl?= Cadilhac's message of "Mon, 24 Jul 2006 22:40:15 +0200") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) X-DIRO-MailScanner-Information: Please contact the ISP for more information X-DIRO-MailScanner: Found to be clean X-DIRO-MailScanner-SpamCheck: n'est pas un polluriel, SpamAssassin (score=-2.82, requis 5, autolearn=not spam, ALL_TRUSTED -2.82) X-DIRO-MailScanner-From: monnier@iro.umontreal.ca X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:57579 Archived-At: > (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