Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Bin committed Jul 28, 2016
1 parent 34c51f6 commit 38df050
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 92 deletions.
13 changes: 5 additions & 8 deletions evil-matchit-diff.el
Expand Up @@ -26,20 +26,17 @@

;;; Code:

(defun evilmi--cur-line ()
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(require 'evil-matchit-sdk)

(defun evilmi-diff-guess-final-pos ()
(let* ((final-pos (point)))
(save-excursion
(let* (tmp-line)
(forward-line -1)
(setq tmp-line (evilmi--cur-line))
(setq tmp-line (evilmi-sdk-curline))
(if (string-match-p "^index [0-9a-z]+\\.+[0-9a-z]+ [0-9]+$" tmp-line)
(forward-line -1))
(setq tmp-line (evilmi--cur-line))
(setq tmp-line (evilmi-sdk-curline))
(if (string-match-p "^diff [^ ]" tmp-line)
(forward-line -1))
(setq final-pos (line-end-position))))
Expand All @@ -48,7 +45,7 @@
;;;###autoload
(defun evilmi-diff-get-tag ()
;; do nothing
(let* ((cur-line (evilmi--cur-line))
(let* ((cur-line (evilmi-sdk-curline))
(final-pos (point)))
(if (string-match-p "^--- " cur-line)
(save-excursion
Expand All @@ -57,7 +54,7 @@

;;;###autoload
(defun evilmi-diff-jump (rlt NUM)
(let* ((cur-line (evilmi--cur-line))
(let* ((cur-line (evilmi-sdk-curline))
(final-pos (point)))
(cond
((string-match-p "^\\+\\+\\+ " cur-line)
Expand Down
21 changes: 6 additions & 15 deletions evil-matchit-javascript.el
Expand Up @@ -26,9 +26,9 @@

;;; Code:

(require 'evil-matchit-sdk)
(require 'evil-matchit)

;; TODO, fn.then().({}, find the second (
(defun evilmi--javascript-find-open-brace (cur-line)
(let (rlt)
;; javascript code line "(function(...) { ..."
Expand All @@ -38,22 +38,17 @@
(setq rlt 1)
(save-excursion
(forward-line)
(setq cur-line (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(setq cur-line (evilmi-sdk-curline))
(if (string-match "^[ \t]*{ *$" cur-line)
(setq rlt 2)
)
)
)
(setq rlt 2))))
rlt))

;;;###autoload
(defun evilmi-javascript-get-tag ()
(let (p
forward-line-num
rlt
(cur-line (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(cur-line (evilmi-sdk-curline))
)
;; only handle open tag
(if (not (memq (following-char) (string-to-list "{[(}}])")))
Expand All @@ -76,17 +71,13 @@
(when rlt
(evilmi--simple-jump)

(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))
;; hack for javascript
(if (or (string-match "^[ \t]*}\)\(.*\)\; *$" cur-line)
(string-match "^[ \t]*}\(.*\))\; *$" cur-line)
(string-match "^[ \t]*}\])\; *$" cur-line))
(line-end-position)
(1+ (point))
)
)
(1+ (point))))
))

(provide 'evil-matchit-javascript)
44 changes: 13 additions & 31 deletions evil-matchit-python.el
Expand Up @@ -26,6 +26,7 @@

;;; Code:

(require 'evil-matchit-sdk)
(require 'evil-matchit)

;; @return number of indent
Expand Down Expand Up @@ -53,9 +54,7 @@
keyword
where-to-go
regexp
(cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(cur-line (evilmi-sdk-curline)))

;; extract keyword from current line
(if (string-match "^[ \t]*\\([a-z]+\\) *.*:\s*\\(#.*\\)?$" cur-line)
Expand All @@ -73,27 +72,20 @@
(save-excursion
(while (not out-of-loop)
(forward-line -1)
(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))

(when (and (= cur-indent (evilmi--python-calculate-indent cur-line))
(string-match regexp cur-line)
)
(string-match regexp cur-line))
(setq where-to-go (line-beginning-position))
(setq out-of-loop t)
)
(setq out-of-loop t))

;; if it's first line, we need get out of loop
(if (= (point-min) (line-beginning-position))
(setq out-of-loop t)
)
)
)
(setq out-of-loop t))
))
(when where-to-go
(goto-char where-to-go)
(skip-chars-forward " \t"))
)
(skip-chars-forward " \t")))
))

(defun evilmi--python-move-to-next-open-tag (keyword cur-indent)
Expand All @@ -116,9 +108,7 @@
(save-excursion
(while (not out-of-loop)
(forward-line)
(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))

(when (= cur-indent (evilmi--python-calculate-indent cur-line))
(if (and regexp (string-match regexp cur-line))
Expand All @@ -142,9 +132,7 @@
(defun evilmi-python-get-tag ()
(let (rlt
(regexp "^[ \t]*\\([a-z]+\\) *.*:\s*\\(#.*\\)?$")
(cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(cur-line (evilmi-sdk-curline))
cur-indent
tag-type
keyword
Expand Down Expand Up @@ -176,9 +164,7 @@
(let ((p (nth 0 rlt))
(tag-type (nth 1 rlt))
(keyword (nth 2 rlt))
(cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(cur-line (evilmi-sdk-curline))
cur-indent
dendent
rlt)
Expand All @@ -195,9 +181,7 @@
(while (not dendent)
(forward-line -1)
;; first line
(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))

(if evilmi-debug (message "cur-line=%s" cur-line))

Expand All @@ -215,9 +199,7 @@
;; jump to closed tag
(while (not dendent)
(forward-line)
(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))

;; just skip empty line
(if (not (string-match "^[ \t]*$" cur-line))
Expand Down
44 changes: 18 additions & 26 deletions evil-matchit-sdk.el
Expand Up @@ -6,8 +6,7 @@
Each howto is actually a pair. The first element of pair is the regular
expression to match the current line. The second is the index of sub-matches
to extract the keyword which starts from one. The sub-match is the match defined
between '\\(' and '\\)' in regular expression.
")
between '\\(' and '\\)' in regular expression.")

;; slower but I don't care
;; @see http://ergoemacs.org/emacs/modernization_elisp_lib_problem.html
Expand Down Expand Up @@ -42,6 +41,12 @@ between '\\(' and '\\)' in regular expression.
)))
rlt))

;;;###autoload
(defun evilmi-sdk-curline ()
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))

;;;###autoload
(defun evilmi-sdk-member (KEYWORD LIST)
"check if KEYWORD exist in LIST"
Expand Down Expand Up @@ -85,8 +90,8 @@ is-function-exit-point could be 'FN_EXIT' or other status"
(setq elem (nth j elems))
(setq found (and (or (stringp elem) (listp elem))
(evilmi-sdk-member KEYWORD elem)))
(if (not found) (setq j (1+ j)))
)
(if (not found)
(setq j (1+ j))))
(if (not found) (setq i (1+ i))))
(when found
;; function exit point maybe?
Expand All @@ -98,8 +103,7 @@ is-function-exit-point could be 'FN_EXIT' or other status"
KEYWORD))
(setq rlt (list i j nil KEYWORD))
))
rlt
))
rlt))

(defun evilmi--sdk-extract-keyword (cur-line match-tags howtos)
"extract keyword from cur-line. keyword should be defined in match-tags"
Expand All @@ -117,8 +121,7 @@ is-function-exit-point could be 'FN_EXIT' or other status"
;; keep search keyword by using next howto (regex and match-string index)
(if (not (evilmi-sdk-member keyword match-tags)) (setq keyword nil))
)
(setq i (1+ i))
)
(setq i (1+ i)))
keyword))

(defun evilmi--is-monogamy (tag-info)
Expand All @@ -130,18 +133,15 @@ is-function-exit-point could be 'FN_EXIT' or other status"
;; i1 and i2 should be at same row if either of them is monogamy
(if (or (evilmi--is-monogamy i1) (evilmi--is-monogamy i2))
(setq rlt (= (nth 0 i1) (nth 0 i2)))
(setq rlt t))
)
(setq rlt t)))
rlt))

;;;###autoload
(defun evilmi-sdk-get-tag (match-tags howtos)
"return '(start-point tag-info)"
(let (rlt
keyword
(cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(cur-line (evilmi-sdk-curline))
tag-info)

(when (setq keyword (evilmi--sdk-extract-keyword cur-line match-tags howtos))
Expand All @@ -155,8 +155,7 @@ is-function-exit-point could be 'FN_EXIT' or other status"
(line-end-position)
(line-beginning-position))
tag-info))
)
)
))
rlt))

;;;###autoload
Expand All @@ -166,18 +165,14 @@ is-function-exit-point could be 'FN_EXIT' or other status"
cur-tag-type
cur-tag-info
(level 1)
(cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(cur-line (evilmi-sdk-curline))
keyword
found
where-to-jump-in-theory)

(while (not found)
(forward-line (if (= orig-tag-type 2) -1 1))
(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))

(setq keyword (evilmi--sdk-extract-keyword cur-line match-tags howtos))

Expand Down Expand Up @@ -269,10 +264,7 @@ is-function-exit-point could be 'FN_EXIT' or other status"
(= (line-beginning-position) (point-min))
)
(setq found t)
)
)
where-to-jump-in-theory
)
)
))
where-to-jump-in-theory))

(provide 'evil-matchit-sdk)
21 changes: 9 additions & 12 deletions evil-matchit-simple.el
Expand Up @@ -27,6 +27,7 @@

;;; Code:

(require 'evil-matchit-sdk)
(require 'evil-matchit)

(defun evilmi--simple-find-open-brace (cur-line)
Expand All @@ -38,22 +39,20 @@
(setq rlt 1)
(save-excursion
(forward-line)
(setq cur-line (buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(setq cur-line (evilmi-sdk-curline))
(if (string-match "^[ \t]*{ *$" cur-line)
(setq rlt 2))
))
rlt))

;;;###autoload
(defun evilmi-simple-get-tag ()
(let (p
tmp
ch
forward-line-num
rlt
(cur-line (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
(let* (p
tmp
ch
forward-line-num
rlt
(cur-line (evilmi-sdk-curline)))

;; Only handle open tag
(setq tmp (evilmi--get-char-under-cursor))
Expand Down Expand Up @@ -85,9 +84,7 @@
(if evilmi-debug (message "evilmi-simple-jump called"))

(evilmi--simple-jump)
(setq cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(setq cur-line (evilmi-sdk-curline))
;; hack for javascript
(if (string-match "^[ \t]*})(.*)\; *$" cur-line)
(line-end-position)
Expand Down

0 comments on commit 38df050

Please sign in to comment.