branch: elpa/forth-mode
commit 5a08c82adb7de711c530a63dcac269cd6b593341
Author: Helmut Eller <helmut@msibook>
Commit: Helmut Eller <[email protected]>

    Improve word movement commands inside symbols.
    
    In particular keep "symbol" and "word" distinct so that
    M-DEL etc. works like in other modes.
    
    * forth-syntax.el (forth-syntax--set-word-syntax): New helper.
    (forth-syntax--state-normal, forth-syntax--state-parsing-word): Use it.
---
 forth-syntax.el | 19 +++++++++++++++++--
 test/tests.el   |  9 +++------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/forth-syntax.el b/forth-syntax.el
index 39133fd6d5..4c6e02a25b 100644
--- a/forth-syntax.el
+++ b/forth-syntax.el
@@ -27,6 +27,21 @@
 SYNTAX must be a valid argument for `string-to-syntax'."
   `(put-text-property ,start ,end 'syntax-table ',(string-to-syntax syntax)))
 
+;; Set the syntax in the region START/END to "word" or "symbol".  Do
+;; nothing for characters that already have the correct syntax so that
+;; word movement commands work "naturally".
+(defun forth-syntax--set-word-syntax (start end)
+  (save-excursion
+    (goto-char start)
+    (while (progn
+            (skip-syntax-forward "w_" end)
+            (cond ((< (point) end)
+                   (let ((start (point)))
+                     (skip-syntax-forward "^w_" end)
+                     (forth-syntax--set-syntax start (point) "_")
+                     t))
+                  (t nil))))))
+
 
 ;;; State functions
 
@@ -77,7 +92,7 @@ SYNTAX must be a valid argument for `string-to-syntax'."
     (skip-chars-forward forth-syntax--non-whitespace)
     (cond ((= start (point)) #'forth-syntax--state-eob)
          (t
-          (forth-syntax--set-syntax start (point) "w")
+          (forth-syntax--set-word-syntax start (point))
           #'forth-syntax--state-normal))))
 
 (defun forth-syntax--parse-comment (backward-regexp forward-regexp)
@@ -143,7 +158,7 @@ SYNTAX must be a valid argument for `string-to-syntax'."
     (skip-chars-forward forth-syntax--non-whitespace)
     (cond ((= start (point)) #'forth-syntax--state-eob)
          (t
-          (forth-syntax--set-syntax start (point) "w")
+          (forth-syntax--set-word-syntax start (point))
           (let ((word (buffer-substring-no-properties start (point))))
             (cond ((forth-syntax--lookup word))
                   (t
diff --git a/test/tests.el b/test/tests.el
index 5b3ce27302..3822334861 100644
--- a/test/tests.el
+++ b/test/tests.el
@@ -191,11 +191,8 @@ The whitespace before and including \"|\" on each line is 
removed."
   (forth-assert-forward-sexp " :noname foo bar ; \ x" 2 19)
   (forth-assert-forward-sexp " if drop exit else 1+ then bar " 2 27))
 
-;; IDEA 1: in words like foo-bar give the "-" a "word constituent"
-;; syntax so that word movement works like in Lisp mode (which
-;; everybody is used to :-).
-;;
-;; IDEA 2: give the filename in "include filename" string syntax.
+;; IDEA: give the filename in "include filename" string syntax.
 (ert-deftest forth-word-movements ()
   (forth-assert-forward-word "include /tmp/foo.fth \ bar" 1 8)
-  (forth-assert-forward-word "include /tmp/foo.fth \ bar" 8 21))
+  (forth-assert-forward-word "include /tmp/foo.fth \ bar" 8 13)
+  (forth-assert-forward-word "foo-bar" 1 4))

Reply via email to