emacs-devel
[Top][All Lists]
Advanced

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

Infinite loop in align-regexp


From: Geoff Gole
Subject: Infinite loop in align-regexp
Date: Thu, 15 Jan 2009 20:32:48 +0900

For certain choices of regexp and region text, align-regexp will enter
an infinite loop. To reproduce:

  emacs -Q
  RET foo
  C-SPC C-a M-x align-regexp
  \<

Note that the infinite loop won't occur for text at the beginning of
the buffer, so make sure to test at least one newline down.

I'm suprised that this hasn't come up before. I would have thought
aligning to beginning-of-word would be a common case. Anyway, the
problem seems to be that align-region assumes that a successful search
will always move point forward. The following patch attempts to fix
this:

--- /home/ggole/src/emacs/lisp/align.el 2009-01-05 12:18:39.000000000 +0900
+++ /tmp/buffer-content-39973gV 2009-01-15 20:27:23.000000000 +0900
@@ -1307,6 +1307,7 @@
                 (rulesep (assq 'separate rule))
                 (thissep (if rulesep (cdr rulesep) separate))
                 same (eol 0)
+                search-origin
                 group group-c
                 spacing spacing-c
                 tab-stop tab-stop-c
@@ -1412,6 +1413,7 @@
                      ;; while we can find the rule in the alignment
                      ;; region..
                      (while (and (< (point) end-mark)
+                                 (setq search-origin (point))
                                  (if regfunc
                                      (funcall regfunc end-mark nil)
                                    (re-search-forward regexp
@@ -1436,7 +1438,8 @@
                        ;; if the search ended us on the beginning of
                        ;; the next line, move back to the end of the
                        ;; previous line.
-                       (if (bolp)
+                       (if (and (bolp)
+                                (> (point) search-origin))
                            (forward-char -1))

                        ;; lookup the `group' attribute the first time
@@ -1576,7 +1579,12 @@
                            ;; the next line; don't bother searching
                            ;; anymore on this one
                            (if (and (not repeat) (not (bolp)))
-                               (forward-line)))))
+                               (forward-line))
+
+                           ;; if the search did not change point,
+                           ;; move forward to avoid an infinite loop
+                           (if (= (point) search-origin)
+                               (forward-char)))))

                      ;; when they are no more matches for this rule,
                      ;; align whatever was left over




reply via email to

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