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

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

[nongnu] elpa/evil-matchit 3417dbb275 117/244: Add ocaml support


From: ELPA Syncer
Subject: [nongnu] elpa/evil-matchit 3417dbb275 117/244: Add ocaml support
Date: Thu, 6 Jan 2022 02:58:54 -0500 (EST)

branch: elpa/evil-matchit
commit 3417dbb2754de6f9d076400a9289db9921ef7d65
Author: Tomasz KoƂodziejski <tkolodziejski@gmail.com>
Commit: Tomasz KoƂodziejski <tkolodziejski@gmail.com>

    Add ocaml support
---
 evil-matchit-ocaml.el | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/evil-matchit-ocaml.el b/evil-matchit-ocaml.el
new file mode 100644
index 0000000000..00e1c2e00e
--- /dev/null
+++ b/evil-matchit-ocaml.el
@@ -0,0 +1,82 @@
+(setq evilmi-ocaml-keywords
+      '((("struct" "begin" "object") ("end"))
+        (("if") ("then"))
+        (("match") ("with"))
+        (("match" "try") ("with"))
+        (("while" "for") ("done"))
+        (("let") ("in"))
+        ()))
+
+;; regexp to find next/previous keyword
+(setq keywords-regex
+  (let ((all-keywords (apply 'append (apply 'append evilmi-ocaml-keywords))))
+    (format "\\<\\(%s\\)\\>" (mapconcat 'identity all-keywords "\\|"))))
+
+;; jumps to next keyword. Returs nil if there's no next word
+(defun evilmi-ocaml-next-word (direction)
+  (if (= direction 0)
+      (let ((new-point (save-excursion
+          (forward-char)
+          (if (search-forward-regexp keywords-regex nil t)
+              (search-backward-regexp keywords-regex)
+            nil)
+        )))
+        (if new-point (goto-char new-point)))
+    (search-backward-regexp keywords-regex nil t)))
+
+(defun evilmi-ocaml-end-word ()
+  (save-excursion
+    (search-forward-regexp "\\>")
+    (point)))
+
+(defun evilmi-ocaml-get-word ()
+  (buffer-substring-no-properties (point) (evilmi-ocaml-end-word)))
+
+(defun evilmi-ocaml-is-keyword (l keyword)
+  "Checks if the keyword belongs to a row"
+  (find-if (lambda (w) (string-equal w keyword)) (apply 'append l)))
+
+(defun evilmi-ocaml-get-tag-info (keyword)
+  "Find the row in the evilmi-ocaml-keywords"
+  (find-if (lambda (l) (evilmi-ocaml-is-keyword l keyword)) 
evilmi-ocaml-keywords))
+
+;; 0 - forward
+;; 1 - backward
+(defun evilmi-ocaml-go (tag-info level direction)
+  (if (= level 0)
+      (point)
+    (if (evilmi-ocaml-next-word direction)
+        (progn 
+          (setq keyword (evilmi-ocaml-get-word))
+
+          (if (evilmi-ocaml-is-keyword tag-info keyword)
+              ;; interesting tag
+              (if (member keyword (nth direction tag-info))
+                  (evilmi-ocaml-go tag-info (+ level 1) direction)
+                (evilmi-ocaml-go tag-info (- level 1) direction))
+
+            ;; other tag
+            (evilmi-ocaml-go tag-info level direction)))
+      nil)))
+
+(defun evilmi-ocaml-goto-word-beginning ()
+  ;; this is so that when the cursor is on the first character we don't jump 
to previous word
+  (forward-char)
+  (search-backward-regexp "\\<"))
+
+;;;###autoload
+(defun evilmi-ocaml-get-tag ()
+  (save-excursion 
+    (evilmi-ocaml-goto-word-beginning)
+    (evilmi-ocaml-get-word)))
+
+;;;###autoload
+(defun evilmi-ocaml-jump (rlt num)
+  (let* ((tag-info (evilmi-ocaml-get-tag-info rlt))
+         (direction (if (member rlt (car tag-info)) 0 1)))
+    (let ((new-point (save-excursion
+                       (evilmi-ocaml-goto-word-beginning)
+                       (evilmi-ocaml-go tag-info 1 direction))))
+      (if new-point (goto-char new-point)))))
+
+(provide 'evil-matchit-ocaml)



reply via email to

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