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

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

[nongnu] elpa/multiple-cursors f721308 179/434: Tweak mc/mark-all-like-t


From: ELPA Syncer
Subject: [nongnu] elpa/multiple-cursors f721308 179/434: Tweak mc/mark-all-like-this-dwim and add tests
Date: Sat, 7 Aug 2021 09:20:21 -0400 (EDT)

branch: elpa/multiple-cursors
commit f721308591d170e922fa5338f6f0e96053bda11d
Author: Magnar Sveen <magnars@gmail.com>
Commit: Magnar Sveen <magnars@gmail.com>

    Tweak mc/mark-all-like-this-dwim and add tests
---
 features/mark-all-dwim-html.feature    |  94 +++++++++++++++++++++
 features/mark-all-dwim.feature         |  38 +++++++++
 features/support/env.el                |   1 +
 mc-mark-more.el                        | 144 ++++++++++++++++++++++++---------
 multiple-cursors-core.el               |   5 ++
 run-tests.watchr => watch-tests.watchr |   0
 6 files changed, 242 insertions(+), 40 deletions(-)

diff --git a/features/mark-all-dwim-html.feature 
b/features/mark-all-dwim-html.feature
new file mode 100644
index 0000000..51866a8
--- /dev/null
+++ b/features/mark-all-dwim-html.feature
@@ -0,0 +1,94 @@
+Feature: Mark all do-what-I-mean (html)
+
+  Background:
+    Given I turn on html-mode
+    And I turn on delete-selection-mode
+    And I insert:
+    """
+    <body>
+      <div class="abc"> def </div>
+      <div class="ghi"> jkl </div>
+    </body>
+    """
+
+  Scenario: Marks tags in html-mode, from front
+    When I go to the front of the word "abc"
+    And I press "M-b"
+    And I press "M-b"
+    And I press "M-$"
+    And I type "h1"
+    Then I should see:
+    """
+    <body>
+      <h1 class="abc"> def </h1>
+      <div class="ghi"> jkl </div>
+    </body>
+    """
+
+  Scenario: Marks tags in html-mode, from back
+    When I go to the end of the word "jkl"
+    And I press "M-f"
+    And I press "M-$"
+    And I type "h1"
+    Then I should see:
+    """
+    <body>
+      <div class="abc"> def </div>
+      <h1 class="ghi"> jkl </h1>
+    </body>
+    """
+
+  Scenario: Marks tags in html-mode, from outside front
+    When I go to the front of the word "abc"
+    And I press "M-b"
+    And I press "M-b"
+    And I press "C-b"
+    And I press "M-$"
+    And I type "h1"
+    Then I should see:
+    """
+    <body>
+      <h1 class="abc"> def </h1>
+      <div class="ghi"> jkl </div>
+    </body>
+    """
+
+  Scenario: Marks tags in html-mode, from outside back
+    When I go to the end of the word "jkl"
+    And I press "M-f"
+    And I press "C-f"
+    And I press "M-$"
+    And I type "h1"
+    Then I should see:
+    """
+    <body>
+      <div class="abc"> def </div>
+      <h1 class="ghi"> jkl </h1>
+    </body>
+    """
+
+  Scenario: Marks words in html-mode
+    When I go to the front of the word "abc"
+    And I press "M-$"
+    And I type "def"
+    Then I should see:
+    """
+    <body>
+      <div class="def"> def </div>
+      <div class="ghi"> jkl </div>
+    </body>
+    """
+
+  Scenario: Marks words in html-mode
+    When I go to the front of the word "abc"
+    And I press "M-$"
+    And I type "def"
+    And I press "M-$"
+    And I type "hah"
+    Then I should see:
+    """
+    <body>
+      <div class="hah"> hah </div>
+      <div class="ghi"> jkl </div>
+    </body>
+    """
diff --git a/features/mark-all-dwim.feature b/features/mark-all-dwim.feature
new file mode 100644
index 0000000..9eca714
--- /dev/null
+++ b/features/mark-all-dwim.feature
@@ -0,0 +1,38 @@
+Feature: Mark all do-what-I-mean
+
+  Scenario: Mark symbols in defun
+    Given I turn on emacs-lisp-mode
+    And I turn on delete-selection-mode
+    And I insert:
+    """
+    (defun abc (ghi) (message ghi))
+    (defun def (ghi) (message some-other-ghi))
+    """
+    When I go to the end of the word "abc"
+    And I press "M-f"
+    And I press "M-$"
+    And I type "hmm"
+    Then I should see:
+    """
+    (defun abc (hmm) (message hmm))
+    (defun def (ghi) (message some-other-ghi))
+    """
+
+  Scenario: Mark all symbols by pressing twice
+    Given I turn on emacs-lisp-mode
+    And I turn on delete-selection-mode
+    And I insert:
+    """
+    (defun abc (ghi) (message ghi))
+    (defun def (ghi) (message ghi))
+    """
+    When I go to the end of the word "abc"
+    And I press "M-f"
+    And I press "M-$"
+    And I press "M-$"
+    And I type "hmm"
+    Then I should see:
+    """
+    (defun abc (hmm) (message hmm))
+    (defun def (hmm) (message hmm))
+    """
diff --git a/features/support/env.el b/features/support/env.el
index 33fa6e7..53edb3b 100644
--- a/features/support/env.el
+++ b/features/support/env.el
@@ -23,6 +23,7 @@
  (global-set-key (kbd "C->") 'mc/mark-next-like-this)
  (global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
  (global-set-key (kbd "M-!") 'mc/mark-all-like-this)
+ (global-set-key (kbd "M-$") 'mc/mark-all-like-this-dwim)
  (global-set-key (kbd "M-#") 'mc/mark-all-in-region)
  (global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
  (global-set-key (kbd "H-SPC") 'set-rectangular-region-anchor)
diff --git a/mc-mark-more.el b/mc-mark-more.el
index 7a0f519..5270950 100644
--- a/mc-mark-more.el
+++ b/mc-mark-more.el
@@ -28,6 +28,7 @@
 ;;; Code:
 
 (require 'multiple-cursors-core)
+(require 'thingatpt)
 
 (defun mc/cursor-end (cursor)
   (if (overlay-get cursor 'mark-active)
@@ -308,59 +309,122 @@ is one of the above."
         (setq ev (read-event "Use arrow keys for more marks: "))))
     (push ev unread-command-events)))
 
-(defun mc/mark-all-like-this-dwim (arg)
-  "Uses some sane defaults to guess what the user want to do:
+(defvar mc--restrict-mark-all-to-symbols nil)
 
-- If inside a defun, find and mark all the parts of current defun matchign
-the currently active region. If no region is active, activate the word
-under cursor.
-- If in SGML/HTML mode and inside a tag, select the tag and its pair
+(defun mc/mark-all-like-this-dwim (arg)
+  "Tries to guess what you want to mark all of.
+Can be pressed multiple times to increase selection.
 
 With prefix, it behaves the same as original `mc/mark-all-like-this'"
   (interactive "P")
   (if arg
       (mc/mark-all-like-this)
-    (let ((mode (with-current-buffer (current-buffer) major-mode)))
-      (cond ((and (member mode '(sgml-mode html-mode))
-                  (mc/mark-tags)) t)
-            ((bounds-of-thing-at-point 'defun)
-             (mc/select-under-cursor)
-             (save-restriction
-               (widen)
-               (narrow-to-defun)
-               (mc/mark-all-like-this)))
-            (t (mc/select-under-cursor) (mc/mark-all-like-this))))))
-
-(defun mc/select-under-cursor ()
-  "Select the word under cursor"
+    (if (and (mc--no-region-and-in-sgmlish-mode)
+             (mc--on-tag-name-p))
+        (mc/mark-sgml-tags)
+      (let ((before (mc/num-cursors)))
+        (unless (eq last-command 'mc/mark-all-like-this-dwim)
+          (setq mc--restrict-mark-all-to-symbols nil))
+        (unless (use-region-p)
+          (mc--mark-symbol-at-point)
+          (setq mc--restrict-mark-all-to-symbols t))
+        (if mc--restrict-mark-all-to-symbols
+            (mc/mark-all-symbols-like-this-in-defun)
+          (mc/mark-all-like-this-in-defun))
+        (when (<= (mc/num-cursors) before)
+          (if mc--restrict-mark-all-to-symbols
+              (mc/mark-all-symbols-like-this)
+            (mc/mark-all-like-this)))
+        (when (<= (mc/num-cursors) before)
+          (mc/mark-all-like-this))))))
+
+(defun mc--no-region-and-in-sgmlish-mode ()
+  (and (not (use-region-p))
+       (memq major-mode '(sgml-mode html-mode))))
+
+(defun mc--in-defun ()
+  (bounds-of-thing-at-point 'defun))
+
+(defun mc/mark-all-like-this-in-defun ()
+  "Mark all like this in defun."
+  (interactive)
+  (if (mc--in-defun)
+      (save-restriction
+        (widen)
+        (narrow-to-defun)
+        (mc/mark-all-like-this))
+    (mc/mark-all-like-this)))
+
+(defun mc/mark-all-words-like-this-in-defun ()
+  "Mark all words like this in defun."
+  (interactive)
+  (if (mc--in-defun)
+      (save-restriction
+        (widen)
+        (narrow-to-defun)
+        (mc/mark-all-words-like-this))
+    (mc/mark-all-words-like-this)))
+
+(defun mc/mark-all-symbols-like-this-in-defun ()
+  "Mark all symbols like this in defun."
+  (interactive)
+  (if (mc--in-defun)
+      (save-restriction
+        (widen)
+        (narrow-to-defun)
+        (mc/mark-all-symbols-like-this))
+    (mc/mark-all-symbols-like-this)))
+
+(defun mc--mark-symbol-at-point ()
+  "Select the symbol under cursor"
   (interactive)
   (when (not (use-region-p))
-    (let ((b (bounds-of-thing-at-point 'word)))
+    (let ((b (bounds-of-thing-at-point 'symbol)))
       (goto-char (car b))
       (set-mark (cdr b)))))
 
-(defun mc/mark-tags ()
+(defun mc--get-nice-sgml-context ()
+  (car
+   (last
+    (progn
+      (when (looking-at "<") (forward-char 1))
+      (when (looking-back ">") (forward-char -1))
+      (sgml-get-context)))))
+
+(defun mc--on-tag-name-p ()
+  (let* ((context (save-excursion (mc--get-nice-sgml-context)))
+         (tag-name-len (length (aref context 4)))
+         (beg (aref context 2))
+         (end (+ beg tag-name-len (if (eq 'open (aref context 1)) 1 3))))
+    (and context
+         (>= (point) beg)
+         (<= (point) end))))
+
+(defun mc/mark-sgml-tags ()
   "Mark the tag we're in and its pair for renaming."
   (interactive)
-  (let ((context (car (last (save-excursion (sgml-get-context))))))
-    (when (and context
-               (> (point) (aref context 2))
-               (< (point) (aref context 3)))
-      (let* ((tag-position (aref context 1))
-             (tag-length (length (aref context 4)))
-             (main-start (- (aref context 3) 1 tag-length))
-             (mirror-start (save-excursion
-                             (if (eq tag-position 'open)
-                                 (sgml-skip-tag-forward 1)
-                               (sgml-skip-tag-backward 1)
-                               (forward-sexp))
-                             (- (point) 1 tag-length))))
-        (goto-char main-start)
-        (set-mark (+ main-start tag-length))
-        (mc/save-excursion (goto-char mirror-start)
-                           (push-mark (+ mirror-start tag-length))
-                           (mc/create-fake-cursor-at-point))
-        (mc/maybe-multiple-cursors-mode)))))
+  (when (not (mc--inside-tag-p))
+    (error "Place point inside tag to rename."))
+  (let ((context (mc--get-nice-sgml-context)))
+    (if (looking-at "</")
+        (setq context (car (last (sgml-get-context)))))
+    (goto-char (aref context 2))
+    (let* ((tag-name (aref context 4))
+           (num-chars (length tag-name))
+           (master-start (1+ (point)))
+           (mirror-end (save-excursion
+                         (sgml-skip-tag-forward 1)
+                         (1- (point)))))
+      (goto-char (- mirror-end num-chars))
+      (set-mark mirror-end)
+      (mc/create-fake-cursor-at-point)
+      (goto-char master-start)
+      (set-mark (+ (point) num-chars))))
+  (mc/maybe-multiple-cursors-mode))
+
+(defun mc--inside-tag-p ()
+  (save-excursion
+    (not (null (sgml-get-context)))))
 
 (provide 'mc-mark-more)
 
diff --git a/multiple-cursors-core.el b/multiple-cursors-core.el
index 7a94a30..549dfb9 100644
--- a/multiple-cursors-core.el
+++ b/multiple-cursors-core.el
@@ -545,6 +545,11 @@ for running commands with multiple cursors.")
                                      mc/mark-all-words-like-this
                                      mc/mark-all-symbols-like-this
                                      mc/mark-more-like-this-extended
+                                     mc/mark-all-like-this-in-defun
+                                     mc/mark-all-words-like-this-in-defun
+                                     mc/mark-all-symbols-like-this-in-defun
+                                     mc/mark-all-like-this-dwim
+                                     mc/mark-sgml-tags
                                      mc/cycle-forward
                                      mc/cycle-backward
                                      rrm/switch-to-multiple-cursors
diff --git a/run-tests.watchr b/watch-tests.watchr
similarity index 100%
rename from run-tests.watchr
rename to watch-tests.watchr



reply via email to

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