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

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

[nongnu] elpa/evil-matchit 56fb86a108 1/4: user can add new plugin rule


From: ELPA Syncer
Subject: [nongnu] elpa/evil-matchit 56fb86a108 1/4: user can add new plugin rule for a specific major mode
Date: Thu, 5 Dec 2024 03:59:32 -0500 (EST)

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

    user can add new plugin rule for a specific major mode
---
 README.org      | 28 +++++++++++++++++++++++++
 evil-matchit.el | 65 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 pkg.sh          |  2 +-
 3 files changed, 85 insertions(+), 10 deletions(-)

diff --git a/README.org b/README.org
index 9de9ebcfbc..2bceabc04d 100644
--- a/README.org
+++ b/README.org
@@ -258,6 +258,34 @@ Step 3, add below code to =~/.emacs.=,
 #+BEGIN_SRC lisp
 (evilmi-load-plugin-rules '(ruby-mode lua-mode) '(script))
 #+END_SRC
+*** Integrate third party jump tag function
+Use =evilmi-add-one-plugin-rule= to add one new plugin rule for specific 
MAJOR-MODE.
+
+For example, to integrate =rjsx-jump-tag= into this package is as simple as,
+#+begin_src elisp
+(evilmi-add-one-plugin-rule #'rjsx-mode #'my-rjsx-jump-tag)
+#+end_src
+
+Or,
+#+begin_src elisp
+(defun my-rjsx-jump-tag ()
+  (when (string-match "</?>" (string-trim (evilmi-sdk-curline)))
+    (rjsx-jump-tag)))
+(evilmi-add-one-plugin-rule #'rjsx-mode #'my-rjsx-jump-tag)
+#+end_src
+
+Or,
+#+begin_src elisp
+(defun my-rjsx-get-tag()
+  (let ((info ("some info")))
+    info))
+(defun my-rjsx-jump-tag (info num)
+  (rjsx-jump-tag)
+  (point))
+(defvar append-p t)
+(evilmi-add-one-plugin-rule #'rjsx-mode #'my-rjsx-jump-tag #'my-rjsx-get-tag 
append-p)
+#+end_src
+
 *** Support languages using indentation to identify a block of code
 It's easy to support such language (Python, Yaml, ...).
 
diff --git a/evil-matchit.el b/evil-matchit.el
index da5bc74774..ac407b8fe4 100644
--- a/evil-matchit.el
+++ b/evil-matchit.el
@@ -4,7 +4,7 @@
 
 ;; Author: Chen Bin <chenbin.sh@gmail.com>
 ;; URL: http://github.com/redguardtoo/evil-matchit
-;; Version: 3.0.4
+;; Version: 4.0.0
 ;; Keywords: matchit vim evil
 ;; Package-Requires: ((emacs "27.1"))
 ;;
@@ -99,7 +99,9 @@ Some modes can be toggle on/off in the hook."
   (let* ((jump-rules (plist-get evilmi-plugins major-mode))
          rlt
          jumped
-         ideal-dest)
+         ideal-dest
+         get-tag-fn
+         jump-tag-fn)
 
     (unless num (setq num 1))
 
@@ -112,17 +114,38 @@ Some modes can be toggle on/off in the hook."
 
     (when jump-rules
       (dolist (rule jump-rules)
-        ;; execute evilmi-xxxx-get-tag
+        ;; execute evilmi-xxxx-get-tag if it's not nil
         ;; every rule should be executed.
         ;; the simple rule might just forward a word
-        (setq rlt (funcall (nth 0 rule)))
-        (when (and rlt (not jumped))
+        (when (setq get-tag-fn (nth 0 rule))
+          (setq rlt (funcall get-tag-fn)))
+
+        (when (and (or rlt (not get-tag-fn)) (not jumped))
           ;; before jump, we may need some operation
           (if func (funcall func rlt))
           ;; jump now, execute evilmi-xxxx-jump
-          (setq ideal-dest (funcall (nth 1 rule) rlt num))
-          ;; jump only once if the jump is successful
-          (setq jumped t))
+          (setq jump-tag-fn (nth 1 rule))
+          (cond
+           (get-tag-fn
+            ;; pass the result of non-nil get-tag-fn into jump-tag-fn
+            (setq ideal-dest (funcall jump-tag-fn rlt num))
+            ;; jump only once if the jump is successful
+            (setq jumped t))
+           (t
+            ;; if get-tag-fn is nil, assume jump-tag-fn is from third parties.
+            ;; So it need NO arguments and returns nil.
+            ;; Also need double check if the cursor is moved.
+            (let ((point-before-jump (point))
+                  point-after-jump)
+              (funcall jump-tag-fn)
+              (setq point-after-jump (point))
+              (cond
+               ((eq point-before-jump point-after-jump)
+                (setq jumped nil))
+               (t
+                (setq idea-dest point-after-jump)
+                (setq jumped t)))))))
+
         (when (and evilmi-debug rlt)
           (message "rlt=%s rule=%s p=%s jumped=%s idea-dest=%s"
                    rlt
@@ -169,6 +192,30 @@ Some modes can be toggle on/off in the hook."
 
     (nreverse rlt)))
 
+;;;###autoload
+(defun evilmi-add-one-plugin-rule (major-mode jump-tag-fn &optional get-tag-fn 
append-p)
+  "Add one new plugin rule for specific MAJOR-MODE.
+A rule has a non-nil function JUMP-TAG-FN and could-be nil function GET-TAG-FN.
+If APPEND-P is t, new plugin rule is appended into existing rules."
+
+  (let ((rules (plist-get evilmi-plugins major-mode))
+        (new-rule (list get-tag-fn jump-tag-fn)))
+
+    ;; delete old rule with same jump tag function
+    (setq rules (cl-remove-if (lambda (pair) (eq jump-tag-fn (nth 1 pair)))
+                              rules))
+
+    (cond
+     (append-p
+      ;; append the new rule
+      (setq rules (nreverse rules))
+      (push new-rule rules)
+      (setq rules (nreverse rules)))
+     (t
+      (setq rules (push new-rule rules))))
+
+    (setq evilmi-plugins (plist-put evilmi-plugins major-mode rules))))
+
 ;;;###autoload
 (defun evilmi-load-plugin-rules(modes rules)
   "Load MODES's plugin RULES."
@@ -327,7 +374,7 @@ If IS-INNER is t, the region is inner text object."
 (defun evilmi-version()
   "Print version."
   (interactive)
-  (message "3.0.4"))
+  (message "4.0.0"))
 
 ;; initialize evilmi-plugins only once
 (evilmi-init-plugins)
diff --git a/pkg.sh b/pkg.sh
index 5dd261363a..9b56b19629 100755
--- a/pkg.sh
+++ b/pkg.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 name=evil-matchit
-version=3.0.4
+version=4.0.0
 pkg=$name-$version
 mkdir $pkg
 cp README.org $pkg



reply via email to

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