branch: elpa/evil-matchit commit b363d12390a19ac623744bcaa1e514e976000f8c Author: Chen Bin <chenbin...@gmail.com> Commit: Chen Bin <chenbin...@gmail.com>
fix evil-matchit-diff --- README.org | 8 +-- evil-matchit-cmake.el | 13 +---- evil-matchit-diff.el | 130 ++++++++++++++++++++++++++------------------ evil-matchit-fortran.el | 4 +- evil-matchit-sh.el | 4 +- evil-matchit-template.el | 22 ++++---- evil-matchit.el | 4 +- pkg.sh | 2 +- tests/evil-matchit-tests.el | 64 ++++++++++++++++++++++ 9 files changed, 167 insertions(+), 84 deletions(-) diff --git a/README.org b/README.org index 9b9878fef4..d93d901f77 100644 --- a/README.org +++ b/README.org @@ -170,12 +170,12 @@ A complete setup: ;; @param NUM numeric argument when user press "%" to match tag ;; @return the matching tag position in theory, useful only for ;; selecting or deleting text between matching tags and tags -(defun evilmi-mylang-jump (rlt NUM) - (message "rlt=%s" rlt) +(defun evilmi-mylang-jump (info num) + (message "info=%s" info) ;; if we need select region between tags (including tags itself) ;; we get the beginning of region by reading the first element of - ;; rlt - (push-mark (nth 0 rlt) t t) + ;; info + (push-mark (nth 0 info) t t) ;; say 999 is the where we jump to (goto-char 999) ;; If you need know where is the end of the region for region operation, diff --git a/evil-matchit-cmake.el b/evil-matchit-cmake.el index bbf98ed83a..e07a146c31 100644 --- a/evil-matchit-cmake.el +++ b/evil-matchit-cmake.el @@ -22,12 +22,9 @@ ;; ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see <http://www.gnu.org/licenses/>. - - - +;; ;;; Commentary: ;; - ;;; Code: ;; OPTIONAL, you don't need SDK to write a plugin for evil-matchit @@ -36,8 +33,7 @@ (require 'evil-matchit-sdk) (defvar evilmi-cmake-extract-keyword-howtos - '(("^[ \t]*\\([a-zA-Z]+ *\\) *(.*$" 1) - )) + '(("^[ \t]*\\([a-zA-Z]+ *\\) *(.*$" 1))) ;; CMake (http://www.cmake.org) syntax (defvar evilmi-cmake-match-tags @@ -45,8 +41,7 @@ (("foreach") () ("endforeach") "MONOGAMY") (("macro") () ("endmacro") "MONOGAMY") (("while") () ("endwhile") "MONOGAMY") - (("function") () ("endfunction") "MONOGAMY") - )) + (("function") () ("endfunction") "MONOGAMY"))) ;;;###autoload (defun evilmi-cmake-get-tag () @@ -60,7 +55,5 @@ evilmi-cmake-match-tags evilmi-cmake-extract-keyword-howtos)) -(provide 'evil-matchit-cmake) - (provide 'evil-matchit-cmake) ;;; evil-matchit-cmake.el ends here diff --git a/evil-matchit-diff.el b/evil-matchit-diff.el index 4c9a7ab8d2..8d22286ff1 100644 --- a/evil-matchit-diff.el +++ b/evil-matchit-diff.el @@ -22,71 +22,97 @@ ;; ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see <http://www.gnu.org/licenses/>. - - +;; +;;; Commentary: +;; ;;; Code: (require 'evil-matchit-sdk) -(defun evilmi-diff-guess-final-pos () - (let* ((final-pos (point))) +(defun evilmi-diff-begin () + "Find diff begin." + (let* (rlt) + (save-excursion + (cond + ((string-match "^diff " (evilmi-sdk-curline)) + (setq rlt (line-beginning-position))) + + ((string-match "^index " (evilmi-sdk-curline)) + (forward-line -1) + (setq rlt (line-beginning-position))) + + (t + ;; make sure the current line has prefix "---" + (when (string-match "^\\+\\+\\+ " (evilmi-sdk-curline)) + (forward-line -1)) + (cond + ((string-match "^--- " (evilmi-sdk-curline)) + (setq rlt (line-beginning-position)) + (forward-line -2)) + (t + (forward-line -1))) + (when (string-match "^diff " (evilmi-sdk-curline)) + (setq rlt (line-beginning-position)))))) + rlt)) + +(defun evilmi-diff-end () + "Find diff end." + (let* (rlt) + ;; find next diff beginning + (save-excursion + (when (re-search-forward "^diff " (point-max) t) + (setq rlt (line-beginning-position)))) (save-excursion - (let* (tmp-line) + (when (re-search-forward "^--- " (point-max) t) + (when (or (not rlt) (< (line-beginning-position) rlt)) + (setq rlt (line-beginning-position))))) + ;; calculate diff end + (cond + (rlt + (save-excursion + (goto-char rlt) (forward-line -1) - (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-sdk-curline)) - (if (string-match-p "^diff [^ ]" tmp-line) - (forward-line -1)) - (setq final-pos (line-end-position)))) - final-pos)) + (setq rlt (line-end-position)))) + (t + (setq rlt (point-max)))) + rlt)) ;;;###autoload (defun evilmi-diff-get-tag () - ;; do nothing - (let* ((cur-line (evilmi-sdk-curline)) - (final-pos (point))) - (if (string-match-p "^--- " cur-line) - (save-excursion - (setq final-pos (evilmi-diff-guess-final-pos)))) - (list final-pos))) + "Get tag at point." + (let* (pos) + (cond + ((string-match "^\\(\\+\\+\\+\\|---\\|diff\\|index\\) " (evilmi-sdk-curline)) + (when (setq pos (evilmi-diff-begin)) + ;; open tags + (list pos 0))) + (t + (when (setq pos (evilmi-diff-end)) + ;; close tags + (list pos 1)))))) ;;;###autoload -(defun evilmi-diff-jump (rlt NUM) - (let* ((cur-line (evilmi-sdk-curline)) - (final-pos (point))) - (cond - ((string-match-p "^\\+\\+\\+ " cur-line) - ;; next file in diff-mode +(defun evilmi-diff-jump (info num) + "Jump to the matching tag using INFO and NUM." + (ignore num) + (when info + (let* ((pos (nth 0 info)) + (type (nth 1 info)) + dest) (cond - ((re-search-forward "^--- " (point-max) t) - (setq final-pos (evilmi-diff-guess-final-pos)) - (re-search-forward "^\\+\\+\\+ " (point-max) t) - (goto-char (line-beginning-position))) - (t - (setq final-pos (goto-char (point-max)))))) + ((eq type 0) + (goto-char pos) + (re-search-forward "^\\+\\+\\+ ") + (forward-line 1) + (when (setq dest (evilmi-diff-end)) + (goto-char dest))) - ((string-match "^--- " cur-line) - ;; previous file in diff-mode - (when (re-search-backward "^\\+\\+\\+ " (point-min) t) - (save-excursion - (forward-line 1) - (setq final-pos (line-end-position))) - (re-search-backward "^--- " (point-min) t) - (goto-char (line-beginning-position)))) - - ((string-match-p "^@@ " cur-line) - (forward-line 1) - ;; previous file in diff-mode - (cond - ((re-search-forward "^\\(diff\\|---\\|@@ \\)" (point-max) t) - (goto-char (line-beginning-position)) - (save-excursion - (forward-line -1) - (setq final-pos (line-end-position)))) (t - (setq final-pos (goto-char (point-max))))))) - final-pos)) + (goto-char pos) + (re-search-backward "^\\+\\+\\+ ") + (when (setq dest (evilmi-diff-begin)) + (goto-char dest)))) + dest))) (provide 'evil-matchit-diff) +;;; evil-matchit-diff.el ends here diff --git a/evil-matchit-fortran.el b/evil-matchit-fortran.el index 74e4d1e8f8..e7c92264fb 100644 --- a/evil-matchit-fortran.el +++ b/evil-matchit-fortran.el @@ -61,8 +61,8 @@ (evilmi-sdk-get-tag evilmi-fortran-match-tags evilmi-fortran-extract-keyword-howtos)) ;;;###autoload -(defun evilmi-fortran-jump (rlt NUM) - (evilmi-sdk-jump rlt NUM evilmi-fortran-match-tags evilmi-fortran-extract-keyword-howtos)) +(defun evilmi-fortran-jump (info num) + (evilmi-sdk-jump info num evilmi-fortran-match-tags evilmi-fortran-extract-keyword-howtos)) (provide 'evil-matchit-fortran) ;;; evil-matchit-fortran.el ends here diff --git a/evil-matchit-sh.el b/evil-matchit-sh.el index 932ed0fb4a..11f84a8da1 100644 --- a/evil-matchit-sh.el +++ b/evil-matchit-sh.el @@ -47,8 +47,8 @@ (evilmi-sdk-get-tag evilmi-sh-match-tags evilmi-sh-extract-keyword-howtos)) ;;;###autoload -(defun evilmi-sh-jump (rlt NUM) - (evilmi-sdk-jump rlt NUM evilmi-sh-match-tags evilmi-sh-extract-keyword-howtos)) +(defun evilmi-sh-jump (info num) + (evilmi-sdk-jump info num evilmi-sh-match-tags evilmi-sh-extract-keyword-howtos)) (provide 'evil-matchit-sh) ;;; evil-matchit-sh.el ends here diff --git a/evil-matchit-template.el b/evil-matchit-template.el index 9d50b6c775..0c7251ee79 100644 --- a/evil-matchit-template.el +++ b/evil-matchit-template.el @@ -22,8 +22,9 @@ ;; ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see <http://www.gnu.org/licenses/>. - - +;; +;;; Commentary: +;; ;;; Code: ;; OPTIONAL, you don't need SDK to write a plugin for evil-matchit @@ -33,8 +34,7 @@ (defvar evilmi-template-extract-keyword-howtos '(("^[ \t]*<\\?php +\\([a-z]+\\).*$" 1) - ("^[ \t]*\\([@a-z]+\\).*$" 1) - )) + ("^[ \t]*\\([@a-z]+\\).*$" 1))) (defvar evilmi-template-match-tags '(("if" ("elseif" "else") "endif" "MONOGAMY") @@ -47,17 +47,17 @@ ("@for" () "@endfor" "MONOGAMY") ("@foreach" () "@endforeach" "MONOGAMY") ("@forelse" "@empty" "@endforelse" "MONOGAMY") - ("@while" () "@endwhile" "MONOGAMY") - )) + ("@while" () "@endwhile" "MONOGAMY"))) ;;;###autoload (defun evilmi-template-get-tag () - (let (rlt) - (setq rlt (evilmi-sdk-get-tag evilmi-template-match-tags evilmi-template-extract-keyword-howtos)) - rlt)) + "Get tag at point." + (evilmi-sdk-get-tag evilmi-template-match-tags evilmi-template-extract-keyword-howtos)) ;;;###autoload -(defun evilmi-template-jump (rlt NUM) - (evilmi-sdk-jump rlt NUM evilmi-template-match-tags evilmi-template-extract-keyword-howtos)) +(defun evilmi-template-jump (info num) + "Jump to the matching tag using INFO and NUM." + (evilmi-sdk-jump info num evilmi-template-match-tags evilmi-template-extract-keyword-howtos)) (provide 'evil-matchit-template) +;;; evil-matchit-template.el ends here diff --git a/evil-matchit.el b/evil-matchit.el index 2466d503ab..2f3bfa0b31 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.5 +;; Version: 2.3.6 ;; Keywords: matchit vim evil ;; Package-Requires: ((evil "1.2.0") (emacs "24.4")) ;; @@ -307,7 +307,7 @@ If IS-INNER is t, the region is inner text object." (defun evilmi-version() "Print version." (interactive) - (message "2.3.5")) + (message "2.3.6")) (defvar evil-matchit-mode-map (make-sparse-keymap) "Keymap used by the minor mode.") diff --git a/pkg.sh b/pkg.sh index 479e589059..94e35e8450 100755 --- a/pkg.sh +++ b/pkg.sh @@ -1,6 +1,6 @@ #!/bin/bash name=evil-matchit -version=2.3.5 +version=2.3.6 pkg=$name-$version mkdir $pkg cp README.org $pkg diff --git a/tests/evil-matchit-tests.el b/tests/evil-matchit-tests.el index 1768c2319e..d0e786fcc5 100644 --- a/tests/evil-matchit-tests.el +++ b/tests/evil-matchit-tests.el @@ -223,5 +223,69 @@ (should (eq major-mode 'lua-mode)))) +(ert-deftest evilmi-test-diff () + (with-temp-buffer + (insert "diff 1\n" + "index abed2b7\n" + "--- a/1.c\n" + "+++ b/1.c\n" + "line1\n" + "line2\n" + "diff 2" + "index abed2b7\n" + "--- a/2.c\n" + "+++ b/2.c\n" + "line3\n" + "line4") + (diff-mode) + + ;; test git diff + ;; jump to diff end + (goto-char (point-min)) + (evilmi-jump-items) + (should (string= "line2" (evilmi-sdk-curline))) + ;; jump back to diff beginning + (evilmi-jump-items) + (should (string= "diff 1" (evilmi-sdk-curline))) + (should (eq (point) (point-min))) + ;; jump from second line of diff beginning + (forward-line 1) + (evilmi-jump-items) + (should (string= "line2" (evilmi-sdk-curline))) + ;; jump from third line of diff beginning + (goto-char (point-min)) + (forward-line 2) + (evilmi-jump-items) + (should (string= "line2" (evilmi-sdk-curline))) + ;; jump from fourth line of diff beginning + (goto-char (point-min)) + (forward-line 3) + (evilmi-jump-items) + (should (string= "line2" (evilmi-sdk-curline))) + + ;; test GNU diff + (erase-buffer) + (insert "--- a/1.c\n" + "+++ b/1.c\n" + "line1\n" + "line2\n" + "--- a/2.c\n" + "+++ b/2.c\n" + "line3\n" + "line4") + ;; jump to diff end + (goto-char (point-min)) + (evilmi-jump-items) + (should (string= "line2" (evilmi-sdk-curline))) + ;; jump back to diff beginning + (evilmi-jump-items) + (should (string= "--- a/1.c" (evilmi-sdk-curline))) + (should (eq (point) (point-min))) + ;; jump from second line of diff beginning + (forward-line 1) + (evilmi-jump-items) + (should (string= "line2" (evilmi-sdk-curline))) + (should (eq major-mode 'diff-mode)))) + (ert-run-tests-batch-and-exit) ;;; evil-matchit-tests.el ends here