emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[nongnu] elpa/evil-matchit 9cd0ddaacb 233/244: can jump between quotes i


From: ELPA Syncer
Subject: [nongnu] elpa/evil-matchit 9cd0ddaacb 233/244: can jump between quotes in plain text
Date: Thu, 6 Jan 2022 02:59:04 -0500 (EST)

branch: elpa/evil-matchit
commit 9cd0ddaacb3476221d37344715c759ed3cb538d7
Author: Chen Bin <chenbin.sh@gmail.com>
Commit: Chen Bin <chenbin.sh@gmail.com>

    can jump between quotes in plain text
---
 evil-matchit-sdk.el         | 84 +++++++++++++++++++++++++++++++--------------
 evil-matchit.el             |  4 +--
 pkg.sh                      |  2 +-
 tests/evil-matchit-tests.el | 26 ++++++++++++++
 tests/hello.org             |  6 ++--
 5 files changed, 91 insertions(+), 31 deletions(-)

diff --git a/evil-matchit-sdk.el b/evil-matchit-sdk.el
index d6f44c734c..2c020ddd88 100644
--- a/evil-matchit-sdk.el
+++ b/evil-matchit-sdk.el
@@ -32,19 +32,38 @@ between '\\(' and '\\)' in regular expression.")
   "Get character at POSITION."
   (char-after position))
 
+(defun evilmi-sdk-guess-jump-direction-of-quote (ch ff)
+  "Guess jump direction by quote character CH and font face FF.
+Return t if jump forward."
+  (cond
+   (ff
+    (eq ff (get-text-property (+ 1 (point)) 'face)))
+
+   (t
+    (let* ((i 0)
+           (cnt 0)
+           (str (buffer-substring (point-min) (point)))
+           (len (length str)))
+      ;; count quote character
+      (while (< i len)
+        (when (eq ch (seq-elt str i))
+          (setq cnt (1+ cnt)))
+        (setq i (1+ i)))
+      (eq (% cnt 2) 0)))))
+
 (defun evilmi-sdk-jump-forward-p ()
   "Return: (forward-direction font-face-under-cursor character-under-cursor).
 If font-face-under-cursor is NOT nil, the quoted string is being processed."
   (let* ((ch (following-char))
-         (p (point))
-         ff
+         ff ; font-face
          (rlt t))
     (cond
      ((memq ch evilmi-backward-chars)
       (setq rlt nil))
+
      ((and (memq ch evilmi-quote-chars))
-      (setq rlt (eq (setq ff (get-text-property p 'face))
-                    (get-text-property (+ 1 p) 'face)))))
+      (setq ff (get-text-property (point) 'face))
+      (setq rlt (evilmi-sdk-guess-jump-direction-of-quote ch ff))))
 
     (if evilmi-debug (message "evilmi-sdk-jump-forward-p => (%s %s %s)" rlt ff 
(string ch)))
     (list rlt ff ch)))
@@ -80,46 +99,57 @@ If font-face-under-cursor is NOT nil, the quoted string is 
being processed."
     (evilmi-among-fonts-p pos '(font-lock-comment-face
                                 font-lock-comment-delimiter-face)))))
 
-(defun evilmi-sdk-scan-sexps (is-forward)
-  "Get the position of matching tag.
+(defun evilmi-sdk-scan-sexps (is-forward character)
+  "Get the position of matching tag with CHARACTER at point.
 If IS-FORWARD is t, jump forward; or else jump backward."
+  (if evilmi-debug (message "evilmi-sdk-scan-sexps called => (%s)" is-forward 
character))
   (let* ((start-pos (if is-forward (point) (+ 1 (point))))
          (arg (if is-forward 1 -1))
          (limit (if is-forward (point-max) (point-min)))
          (lvl 1)
-         (b (following-char))
-         (e (cond
-             ;; {}
-             ((= b 123) 125)
-             ((= b 125) 123)
-             ;; ()
-             ((= b 40) 41)
-             ((= b 41) 40)
-             ;; []
-             ((= b 91) 93)
-             ((= b 93) 91)))
+         (dest-ch (cond
+                     ;; {}
+                     ((= character 123) 125)
+                     ((= character 125) 123)
+                     ;; ()
+                     ((= character 40) 41)
+                     ((= character 41) 40)
+                     ;; []
+                     ((= character 91) 93)
+                     ((= character 93) 91)))
          (rlt start-pos))
+
     (cond
+     ;; search another quote character
+     ((memq character evilmi-quote-chars)
+      (save-excursion
+        (setq start-pos (if is-forward (1+ (point)) (1- (point))))
+        (goto-char start-pos)
+        (while (and (not (= start-pos limit))
+                    (not (eq (following-char) character)))
+          (goto-char (setq start-pos (+ start-pos arg))))
+        (when (eq (following-char) character)
+          (setq rlt (+ start-pos (if is-forward 1 0))))))
+
      ((evilmi-sdk-comment-p (point))
       ;; Matching tag in comment.
       ;; Use own algorithm instead of `scan-sexps'
       ;; because `scan-sexps' does not work in some major modes
       (save-excursion
         (setq start-pos (point))
-        (while (and e (not (= start-pos limit)) (> lvl 0))
+        (while (and dest-ch (not (= start-pos limit)) (> lvl 0))
           (goto-char (setq start-pos (+ start-pos arg)))
           (when (evilmi-sdk-comment-p start-pos)
             (cond
-             ((= (following-char) b)
+             ((= (following-char) character)
               (setq lvl (1+ lvl)))
-             ((= (following-char) e)
+             ((= (following-char) dest-ch)
               (setq lvl (1- lvl))))))
         (when (= lvl 0)
           (setq rlt (+ start-pos (if is-forward 1 0))))))
 
      (t
-      ;; not comment
-      ;; search but ignore comments
+      ;; jump inside code and ignore comments
       (let* ((parse-sexp-ignore-comments t))
         (setq rlt (scan-sexps start-pos arg)))))
 
@@ -136,11 +166,13 @@ If IS-FORWARD is t, jump forward; or else jump backward."
 
 ;; @see 
http://emacs.stackexchange.com/questions/13222/a-elisp-function-to-jump-between-matched-pair
 (defun evilmi-sdk-jumpto-where (ff is-forward ch)
-  "Non-nil ff means jumping between quotes."
+  "Use font face FF, jump direction IS-FORWARD and character CH to jump."
+  (if evilmi-debug (message "evilmi-sdk-jumpto-where => %s %s %s" ff 
is-forward ch))
   (let* ((dst (if ff (evilmi-sdk-the-other-quote-char ff is-forward ch)
-                (evilmi-sdk-scan-sexps is-forward))))
-    (if evilmi-debug (message "evilmi-sdk-jumpto-where => %s" 
(evilmi-sdk-adjust-jumpto is-forward dst)))
-    (evilmi-sdk-adjust-jumpto is-forward dst)))
+                (evilmi-sdk-scan-sexps is-forward ch)))
+         (rlt (evilmi-sdk-adjust-jumpto is-forward dst)))
+    (if evilmi-debug (message "dst=%s rlt=%s" dst rlt))
+    rlt))
 
 (defun evilmi-sdk-tweak-selected-region (font-face jump-forward)
   "Tweak selected region using FONT-FACE and JUMP-FORWARD."
diff --git a/evil-matchit.el b/evil-matchit.el
index 8db7469711..2d377dd1ec 100644
--- a/evil-matchit.el
+++ b/evil-matchit.el
@@ -4,7 +4,7 @@
 
 ;; Author: Chen Bin <chenbin DOT sh AT gmail DOT com>
 ;; URL: http://github.com/redguardtoo/evil-matchit
-;; Version: 2.3.11
+;; Version: 2.3.12
 ;; Keywords: matchit vim evil
 ;; Package-Requires: ((evil "1.2.0") (emacs "25.1"))
 ;;
@@ -315,7 +315,7 @@ If IS-INNER is t, the region is inner text object."
 (defun evilmi-version()
   "Print version."
   (interactive)
-  (message "2.3.11"))
+  (message "2.3.12"))
 
 (defvar evil-matchit-mode-map (make-sparse-keymap)
   "Keymap used by the minor mode.")
diff --git a/pkg.sh b/pkg.sh
index 52a0618867..113b0be5ca 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 name=evil-matchit
-version=2.3.11
+version=2.3.12
 pkg=$name-$version
 mkdir $pkg
 cp README.org $pkg
diff --git a/tests/evil-matchit-tests.el b/tests/evil-matchit-tests.el
index 90c83ae586..3f02c81b93 100644
--- a/tests/evil-matchit-tests.el
+++ b/tests/evil-matchit-tests.el
@@ -502,6 +502,32 @@
     (evilmi-jump-items)
     (should (string= "#+begin_quote" (evilmi-sdk-curline)))
 
+    ;; This is test string: "'hello world'"
+    (search-forward "test quotes begin ")
+
+    ;; test double quote
+    (should (eq (following-char) 34))
+    (let* ((start-pos (point)))
+      (evilmi-jump-items)
+      (should (eq (following-char) 34))
+      (should (> (point) start-pos))
+      ;; jump back
+      (evilmi-jump-items)
+      (should (eq (following-char) 34))
+      (should (eq (point) start-pos))
+
+      ;; test single quote
+      (forward-char)
+      (should (eq (following-char) 39))
+      (setq start-pos (point))
+      (evilmi-jump-items)
+      (should (eq (following-char) 39))
+      (should (> (point) start-pos))
+      ;; jump back
+      (evilmi-jump-items)
+      (should (eq (following-char) 39))
+      (should (eq (point) start-pos)))
+
     (should (eq major-mode 'org-mode))))
 
 (ert-run-tests-batch-and-exit)
diff --git a/tests/hello.org b/tests/hello.org
index e069511a56..6c75a8a70c 100644
--- a/tests/hello.org
+++ b/tests/hello.org
@@ -1,4 +1,4 @@
-#+OPTIONS: ^:{} toc:nil H:3
+    #+OPTIONS: ^:{} toc:nil H:3
 * myproject (v0.0.1)
 One liner to describe this program.
 * Why
@@ -20,4 +20,6 @@ console.log('hello world);
 while(true) {
   console('bye');
 }
-#+end_quote
\ No newline at end of file
+#+end_quote
+
+test quotes begin "'hello world'" end
\ No newline at end of file



reply via email to

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