Subject: 24.3.50; [PATCH] fixed (subword-forward) edge case at the end of a buffer
From: Dima Kogan <[email-masked]>
To: [email-masked]
Date: Tue, 19 Feb 2013 02:21:49 -0800
List-Id: <debbugs-submit.debbugs.gnu.org>

I'm attaching a patch to fix an edge case of (subword-forward). To
reproduce the issue:

1. create a buffer that has an all-caps string such as 'ASDF' at the end
   of the buffer. There must not be a newline at the end; the 'F' should
   be the last character

2. Go to the start of the buffer, and do (subword-forward).

Observe that the point ends up at the 'F', but it should end up one
character ahead of there. To see more clearly that this is wrong, do
(subword-downcase 1) instead of (subword-forward). This should make all
the characters lowercase, but it misses the last one.

This is happening because (subword-forward) is trying to match the
character after the start of the new subword, but at the end of the
buffer, this character doesn't exist.

From c248e56c86d96dac8990880b44b983fd4883b1d9 Mon Sep 17 00:00:00 2001
From: Dima Kogan <[email-masked]>
Date: Tue, 19 Feb 2013 02:14:26 -0800
Subject: [PATCH] (subword-forward) no longer gets confused by the end of the
 buffer

This fixes an edge case of (subword-forward). If the point is at the
start of an all-caps word at the end of the buffer, (subword-forward)
would end up at the last character of the word, NOT past the word, as
it should. This was happening because (subword-forward) was trying to
match the character after the start of the new subword, but at the end
of the buffer, this character didn't exist
---
 lisp/progmodes/subword.el |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/subword.el b/lisp/progmodes/subword.el
index 80e632c..a86a29b 100644
--- a/lisp/progmodes/subword.el
+++ b/lisp/progmodes/subword.el
@@ -87,7 +87,7 @@
   "Function to call for backward subword movement.")
 
 (defvar subword-forward-regexp
-  "\\W*\\(\\([[:upper:]]*\\W?\\)[[:lower:][:digit:]]*\\)"
+  "\\W*\\(\\([[:upper:]]*\\(\\W?\\)\\)[[:lower:][:digit:]]*\\)"
   "Regexp used by `subword-forward-internal'.")
 
 (defvar subword-backward-regexp
@@ -268,7 +268,16 @@ Optional argument ARG is the same as for `capitalize-word'."
       (goto-char
        (cond
 	((< 1 (- (match-end 2) (match-beginning 2)))
-	 (1- (match-end 2)))
+	 ;; if we saw start of subword (capital letters or
+	 ;; whitespace)...
+	 (if (and (= (match-end 2) (point-max))
+		  (= (match-end 2) (match-beginning 3)))
+	     ;; ... and we have all-caps at the end of the buffer, the
+	     ;; next subword starts at end of buffer...
+	     (match-end 2)
+	   ;; ... if we're not at the end, we have the position of the
+	   ;; 2nd character in the subword, so go back one
+	   (1- (match-end 2))))
 	(t
 	 (match-end 0))))
     (forward-word 1)))
-- 
1.7.10.4