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: Michaël Cadilhac
Subject: Re: [Proposal] M-x tabify to indent only when needed.
Date: Tue, 25 Jul 2006 13:38:24 +0200
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.50 (gnu/linux)

Richard Stallman <address@hidden> writes:

>     !           (unless (save-match-data
>     !                     (save-excursion
>     !                       (goto-char (match-beginning 0))
>     !                       (string-match
>     !                        (format "^\t* \\{0,%d\\}$"
>     !                                (- tab-width (1+ (mod (current-column) 
> tab-width))))
>     !                        (match-string 0))))
>
> That would be very slow.  Please do it without using regexps,
> perhaps using skip-chars-forward.

I can come up with the following regexp-free, search-free patch that
makes the job, and that uses Stefan's good proposal of using " [ \t]+"
as `tabify-regexp'.

However, the code now supposes that if a TAB appears in the match,
a space must have preceded it.  I specified that in the docstring.

Index: lisp/tabify.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/tabify.el,v
retrieving revision 1.19
diff -c -r1.19 tabify.el
*** lisp/tabify.el      6 Feb 2006 14:33:35 -0000       1.19
--- lisp/tabify.el      25 Jul 2006 10:30:09 -0000
***************
*** 50,59 ****
          (delete-region tab-beg (point))
          (indent-to column))))))
  
! (defvar tabify-regexp "[ \t][ \t]+"
    "Regexp matching whitespace that tabify should consider.
! Usually this will be \"[ \\t][ \\t]+\" to match two or more spaces or tabs.
! \"^[ \\t]+\" is also useful, for tabifying only initial whitespace.")
  
  ;;;###autoload
  (defun tabify (start end)
--- 50,60 ----
          (delete-region tab-beg (point))
          (indent-to column))))))
  
! (defvar tabify-regexp " [ \t]+"
    "Regexp matching whitespace that tabify should consider.
! Usually this will be \" [ \\t]+\" to match two or more spaces or tabs.
! \"^\\\\( \\t*\\\\)+\" is also useful, for tabifying only initial whitespace.
! Note that if a `\\t' is matched, a space must have been matched before.")
  
  ;;;###autoload
  (defun tabify (start end)
***************
*** 75,82 ****
        (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))))))
  
  (provide 'tabify)
  
--- 76,94 ----
        (while (re-search-forward tabify-regexp nil t)
        (let ((column (current-column))
              (indent-tabs-mode t))
!         (unless (save-excursion
!                   (if (not (= (skip-chars-backward " " (match-beginning 0))
!                               (- (match-beginning 0) (match-end 0))))
!                       ;; There is a TAB after a space: (match-string 0) is
!                       ;; not full of spaces.
!                       nil
!                     ;; See if there's room for a TAB.
!                     ;; We already are at (match-beginning 0).
!                     (< (+ (mod (current-column) tab-width)
!                           (- column (current-column)))
!                        tab-width)))
!           (delete-region (match-beginning 0) (point))
!           (indent-to column)))))))
  
  (provide 'tabify)
  
Index: lisp/ChangeLog
===================================================================
RCS file: /sources/emacs/emacs/lisp/ChangeLog,v
retrieving revision 1.9820
diff -c -r1.9820 ChangeLog
*** lisp/ChangeLog      17 Jul 2006 04:07:48 -0000      1.9820
--- lisp/ChangeLog      25 Jul 2006 10:30:10 -0000
***************
*** 1,3 ****
--- 1,7 ----
+ 2006-07-24  Michaël Cadilhac  <address@hidden>
+ 
+       * tabify.el (tabify): Check if not already tabified before indenting.
+ 
  2006-07-17  Chong Yidong  <address@hidden>
  
        * progmodes/compile.el (compilation-mode-font-lock-keywords):
The understanding is slightly improved :-) I can't make it clearer, in
fact.

-- 
 |      Michaël `Micha' Cadilhac   |  Isn't vi that text editor with        |
 |         Epita/LRDE Promo 2007   |   two modes... One that beeps and      |
 | http://www.lrde.org/~cadilh_m   |     one that corrupts your file?       |
 `--  -   JID: address@hidden --'           -- Dan Jacobson         -  --'

Attachment: pgprJKoE3UYEo.pgp
Description: PGP signature


reply via email to

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