Subject: bug#17558: 24.4.50; global-subword-mode breaks ERC

From: Dima Kogan <[email-masked]>
Date: Fri, 08 Jan 2016 10:54:05 -0800

Hi. New patch attached. Notes inline

Lars Ingebrigtsen <[email-masked]> writes:

> I've got a couple of comments about the code...
>
>>      (goto-char (point-min))
>> -    (upcase-word 1)
>> +
>> +    ;; this is (upcase-word 1), but working even with subword-mode
>> +    ;; active
>> +    (skip-syntax-forward "^w")
>> +    (let*
>> +        ((word-start (point))
>> +         (word-end
>> +          (progn (skip-syntax-forward "w") (point))))
>> +      (upcase-region word-start word-end))
>> +
>
> If you had a function `erc-forward-word' that did all the syntax
> skipping, you could basically just say
>
> (upcase-region (point) (erc-forward-word))

Sure. New patch does this.

>> -      (while (forward-word 1)
>> -        (setq bounds (bounds-of-thing-at-point 'word))
>> -        (setq word (buffer-substring-no-properties
>> -                    (car bounds) (cdr bounds)))
>> -        (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
>> -                  (and erc-channel-users (erc-get-channel-user word)))
>> -          (erc-button-add-button (car bounds) (cdr bounds)
>> -                                 fun t (list word)))))))
>> +
>> +      (while
>> +          (progn
>> +
>> +            ;; I move forward a word (independent of subword-mode) ...
>> +            (skip-syntax-forward "^w")
>> +            (let*
>> +                ((word-start (point))
>> +                 (word-end
>> +                  (progn (skip-syntax-forward "w") (point))))
>> +
>> +              ;; ... if the word was empty we're at the end of buffer ...
>> +              (and (/= word-start word-end)
>> +
>> +                   ;; ... otherwise, we do stuff with this word
>> +                   (progn
>> +                     (setq word (buffer-substring-no-properties
>> +                                 word-start word-end))
>> +                     (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
>> +                               (and erc-channel-users (erc-get-channel-user word)))
>> +                       (erc-button-add-button word-start word-end
>> +                                              fun t (list word)))))))))))
>
> Similarly here, you could use that erc-forward-word to avoid rewriting
> this code so much.  It would be just
>
> (while (erc-forward-word)
>   (setq bound-stuff ...)
>   )

That would be nice, but no. This chunk of code was affected by
subword-mode in two ways:

1. (forward-word 1)
2. (bounds-of-thing-at-point 'word)

The proposed change only takes care of #1, and I don't see an obvious
way to make it take care of both. I can do (while (erc-forward-word)
...), but then to get the word, I'd need to backtrack to the previous
word marker, and it's not obvious to me that this would be an
improvement over the existing change in the patch.

The attached patch thus has no changes to this hunk. Let me know if you
think of a nicer way to do this.

dima

>From 28d49bfe6581a108b68c9b85dd57c77ecc94125c Mon Sep 17 00:00:00 2001
From: Dima Kogan <[email-masked]>
Date: Tue, 30 Dec 2014 23:29:21 -0800
Subject: [PATCH] ERC no longer gets confused by subword-mode

In commit 6ddc44225e743e2b2a0d5c192f50aefd7a4a915b subword-mode was
integrated into the syntax table instead of simply remapping the
interactive motion bindings as was done previously.  This had the
unintended effect of changing the behavior of lisp programs that touch
words.  In the case of ERC, it completely broke it: emacs now throws an
error when ERC is launched, making it unusable when subword-mode is
active.

This commit replaces the word-oriented calls with ones that navigate
the buffer using syntax classes

Closes: #17558
---
 lisp/erc/erc-backend.el |  8 +++++++-
 lisp/erc/erc-button.el  | 30 ++++++++++++++++++++++--------
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/lisp/erc/erc-backend.el b/lisp/erc/erc-backend.el
index ec45dcf..c315c47 100644
--- a/lisp/erc/erc-backend.el
+++ b/lisp/erc/erc-backend.el
@@ -474,13 +474,19 @@ Currently this is called by `erc-send-input'."
                      nil t))
       (split-string (buffer-string) "\n"))))
 
+(defun erc-forward-word ()
+  "Moves forward one word, ignoring any subword settings.  If no
+subword-mode is active, then this is (forward-word)."
+  (skip-syntax-forward "^w")
+  (> (skip-syntax-forward "w") 0))
+
 ;; Used by CTCP functions
 (defun erc-upcase-first-word (str)
   "Upcase the first word in STR."
   (with-temp-buffer
     (insert str)
     (goto-char (point-min))
-    (upcase-word 1)
+    (upcase-region (point) (erc-forward-word))
     (buffer-string)))
 
 (defun erc-server-setup-periodical-ping (buffer)
diff --git a/lisp/erc/erc-button.el b/lisp/erc/erc-button.el
index 0e4c709..653488c 100644
--- a/lisp/erc/erc-button.el
+++ b/lisp/erc/erc-button.el
@@ -300,14 +300,28 @@ specified by `erc-button-alist'."
     (when (or (eq t form)
               (eval form))
       (goto-char (point-min))
-      (while (forward-word 1)
-        (setq bounds (bounds-of-thing-at-point 'word))
-        (setq word (buffer-substring-no-properties
-                    (car bounds) (cdr bounds)))
-        (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
-                  (and erc-channel-users (erc-get-channel-user word)))
-          (erc-button-add-button (car bounds) (cdr bounds)
-                                 fun t (list word)))))))
+
+      (while
+          (progn
+
+            ;; I move forward a word (independent of subword-mode) ...
+            (skip-syntax-forward "^w")
+            (let*
+                ((word-start (point))
+                 (word-end
+                  (progn (skip-syntax-forward "w") (point))))
+
+              ;; ... if the word was empty we're at the end of buffer ...
+              (and (/= word-start word-end)
+
+                   ;; ... otherwise, we do stuff with this word
+                   (progn
+                     (setq word (buffer-substring-no-properties
+                                 word-start word-end))
+                     (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
+                               (and erc-channel-users (erc-get-channel-user word)))
+                       (erc-button-add-button word-start word-end
+                                              fun t (list word)))))))))))
 
 (defun erc-button-add-buttons-1 (regexp entry)
   "Search through the buffer for matches to ENTRY and add buttons."
-- 
2.1.4