Skip to content

Commit

Permalink
rewrite search related code with isearch
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiuf committed Feb 28, 2024
1 parent 54d4e93 commit 5ddfc66
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 166 deletions.
29 changes: 16 additions & 13 deletions meow-beacon.el
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,23 @@ MATCH is the search regexp."
(meow--narrow-secondary-selection)
(let ((orig-end (region-end))
(orig-beg (region-beginning))
(back (meow--direction-backward-p)))
(back (meow--direction-backward-p))
(inhibit-redisplay t)
(isearch-state (isearch--get-state))
(isearch-wrap-pause nil)) ; never wrap, just stop at the last match.
(save-mark-and-excursion
(goto-char (point-min))
(let ((case-fold-search nil))
(while (re-search-forward match nil t)
(unless (or (= orig-end (point))
(= orig-beg (point)))
(let ((match (match-data)))
(meow--beacon-add-overlay-at-region
'(select . visit)
(car match)
(cadr match)
back)))))
(setq meow--beacon-overlays (reverse meow--beacon-overlays))))))
(while (meow--search nil match nil t)
(unless (or (= orig-end (point))
(= orig-beg (point)))
(let ((match isearch-match-data))
(meow--beacon-add-overlay-at-region
'(select . visit)
(car match)
(cadr match)
back))))
(setq meow--beacon-overlays (reverse meow--beacon-overlays)))
(isearch--set-state isearch-state))))

(defun meow--beacon-count-lines (beg end)
"Count selected lines from BEG to END."
Expand Down Expand Up @@ -393,7 +396,7 @@ MATCH is the search regexp."
((word) (if (not (eq 'expand ex))
(meow--add-beacons-for-word)
(meow--add-beacons-for-match (meow--beacon-region-words-to-match))))
((visit) (meow--add-beacons-for-match (car regexp-search-ring)))
((visit) (meow--add-beacons-for-match nil))
((line) (meow--add-beacons-for-line))
((join) (meow--add-beacons-for-join))
((find) (meow--add-beacons-for-find))
Expand Down
138 changes: 50 additions & 88 deletions meow-command.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The direction of selection is MARK -> POS."

(defun meow--select (selection &optional backward)
"Mark the SELECTION."
(isearch-dehighlight)
(unless (region-active-p)
(meow--cancel-selection))
(let ((sel-type (car selection))
Expand Down Expand Up @@ -129,9 +130,8 @@ This command supports `meow-selection-command-fallback'."
(interactive)
(meow--with-selection-fallback
(meow--execute-kbd-macro meow--kbd-exchange-point-and-mark)
(if (member last-command
'(meow-visit meow-search meow-mark-symbol meow-mark-word))
(meow--highlight-regexp-in-buffer (car regexp-search-ring))
(unless (member last-command
'(meow-visit meow-search meow-mark-symbol meow-mark-word))
(meow--maybe-highlight-num-positions))))

;;; Buffer
Expand Down Expand Up @@ -219,6 +219,8 @@ This command supports `meow-selection-command-fallback'."
This command supports `meow-selection-command-fallback'."
(interactive)
(meow--remove-match-highlights)
(meow--remove-search-highlight)
(meow--with-selection-fallback
(meow--cancel-selection)))

Expand Down Expand Up @@ -749,14 +751,16 @@ Use negative argument to create a backward selection."
(interactive "p")
(let* ((bounds (bounds-of-thing-at-point 'word))
(beg (car bounds))
(end (cdr bounds)))
(end (cdr bounds))
(backward (< n 0)))
(when beg
(thread-first
(meow--make-selection '(expand . word) beg end)
(meow--select (< n 0)))
(meow--select backward))
(let ((search (format "\\<%s\\>" (regexp-quote (buffer-substring-no-properties beg end)))))
(meow--push-search search)
(meow--highlight-regexp-in-buffer search)))))
(save-mark-and-excursion
(goto-char (if backward end beg))
(meow--search backward search nil nil t))))))

(defun meow-mark-symbol (n)
"Mark current symbol under cursor.
Expand All @@ -765,14 +769,16 @@ This command works similar to `meow-mark-word'."
(interactive "p")
(let* ((bounds (bounds-of-thing-at-point 'symbol))
(beg (car bounds))
(end (cdr bounds)))
(end (cdr bounds))
(backward (< n 0)))
(when beg
(thread-first
(meow--make-selection '(expand . word) beg end)
(meow--select (< n 0)))
(meow--select backward))
(let ((search (format "\\_<%s\\_>" (regexp-quote (buffer-substring-no-properties beg end)))))
(meow--push-search search)
(meow--highlight-regexp-in-buffer search)))))
(save-mark-and-excursion
(goto-char (if backward end beg))
(meow--search backward search nil nil t))))))

(defun meow--forward-symbol-1 ()
(when (forward-symbol 1)
Expand Down Expand Up @@ -1315,49 +1321,32 @@ with UNIVERSAL ARGUMENT, search both side."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun meow-search (arg)
" Search and select with the car of current `regexp-search-ring'.
If the contents of selection doesn't match the regexp, will push it to `regexp-search-ring' before searching.
"Search for the next occurrence by `isearch-repeat' or search for
the current active region.
To search backward, use \\[negative-argument]."
(interactive "P")
;; Test if we add current region as search target.
(when (and (region-active-p)
(let ((search (car regexp-search-ring)))
(or (not search)
(not (string-match-p
(format "^%s$" search)
(buffer-substring-no-properties (region-beginning) (region-end)))))))
(meow--push-search (regexp-quote (buffer-substring-no-properties (region-beginning) (region-end)))))
(when-let ((search (car regexp-search-ring)))
(let ((reverse (xor (meow--with-negative-argument-p arg) (meow--direction-backward-p)))
(case-fold-search nil))
(if (or (if reverse
(re-search-backward search nil t 1)
(re-search-forward search nil t 1))
;; Try research from buffer beginning/end
;; if we are already at the last/first matched
(save-mark-and-excursion
;; Recalculate search indicator
(meow--clean-search-indicator-state)
(goto-char (if reverse (point-max) (point-min)))
(if reverse
(re-search-backward search nil t 1)
(re-search-forward search nil t 1))))
(let* ((m (match-data))
(marker-beg (car m))
(marker-end (cadr m))
(beg (if reverse (marker-position marker-end) (marker-position marker-beg)))
(end (if reverse (marker-position marker-beg) (marker-position marker-end))))
(thread-first
(meow--make-selection '(select . visit) beg end)
(meow--select))
(if reverse
(message "Reverse search: %s" search)
(message "Search: %s" search))
(meow--ensure-visible))
(message "Searching %s failed" search))
(meow--highlight-regexp-in-buffer search))))
(let ((reverse (xor (meow--with-negative-argument-p arg)
(if (region-active-p)
(meow--direction-backward-p)
(not isearch-forward))))
match region mark point)
(when (region-active-p)
(setq region (buffer-substring-no-properties
(region-beginning)(region-end)))
(setq mark (mark))
(setq point (point))
(deactivate-mark)
(when (not(meow--search-match region))
(setq match (regexp-quote region))))
(when (and (not match) (string-empty-p isearch-string))
(user-error "Failed to Search: please make a selection to search for"))
(meow--search reverse match)
;; if failed reactivate origin region
(when (and region (not isearch-success))
(set-mark mark)
(goto-char point))
isearch-success))

(defun meow-pop-search ()
"Searching for the previous target."
Expand All @@ -1366,21 +1355,9 @@ To search backward, use \\[negative-argument]."
(message "current search is: %s" (car regexp-search-ring))
(meow--cancel-selection)))

(defun meow--visit-point (text reverse)
"Return the point of text for visit command.
Argument TEXT current search text.
Argument REVERSE if selection is reversed."
(let ((func (if reverse #'re-search-backward #'re-search-forward))
(func-2 (if reverse #'re-search-forward #'re-search-backward))
(case-fold-search nil))
(save-mark-and-excursion
(or (funcall func text nil t 1)
(funcall func-2 text nil t 1)))))

(defun meow-visit (arg)
"Read a string from minibuffer, then find and select it.
The input will be pushed into `regexp-search-ring'. So
\\[meow-search] can be used for further searching with the same condition.
A list of words and symbols in the current buffer will be provided for completion.
Expand All @@ -1391,25 +1368,10 @@ the words and symbols in the current buffer.
To search backward, use \\[negative-argument]."
(interactive "P")
(let* ((reverse arg)
(pos (point))
(text (meow--prompt-symbol-and-words
(if arg "Visit backward: " "Visit: ")
(point-min) (point-max)))
(visit-point (meow--visit-point text reverse)))
(if visit-point
(let* ((m (match-data))
(marker-beg (car m))
(marker-end (cadr m))
(beg (if (> pos visit-point) (marker-position marker-end) (marker-position marker-beg)))
(end (if (> pos visit-point) (marker-position marker-beg) (marker-position marker-end))))
(thread-first
(meow--make-selection '(select . visit) beg end)
(meow--select))
(meow--push-search text)
(meow--ensure-visible)
(meow--highlight-regexp-in-buffer text)
(setq meow--dont-remove-overlay t))
(message "Visit: %s failed" text))))
(point-min) (point-max))))
(meow--search reverse text)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; THING
Expand Down Expand Up @@ -1624,20 +1586,19 @@ Argument ARG if not nil, switching in a new window."
Use negative argument for backward application."
(interactive "P")
(let ((s (car regexp-search-ring))
(case-fold-search nil)
;;do not wrap when there are no more matches
(let ((isearch-wrap-pause nil)
(isearch-state (isearch--get-state))
(back (meow--with-negative-argument-p arg)))
(meow--wrap-collapse-undo
(while (if back
(re-search-backward s nil t)
(re-search-forward s nil t))
(while (meow--search back nil nil t)
(thread-first
(meow--make-selection '(select . visit)
(if back
(point)
(match-beginning 0))
(car isearch-match-data))
(if back
(match-end 0)
(cadr isearch-match-data)
(point)))
(meow--select))
(let ((ov (make-overlay (region-beginning) (region-end))))
Expand All @@ -1648,7 +1609,8 @@ Use negative argument for backward application."
(if back
(goto-char (min (point) (overlay-start ov)))
(goto-char (max (point) (overlay-end ov))))
(delete-overlay ov))))))))
(delete-overlay ov)))))
(isearch--set-state isearch-state))))

(defun meow-start-kmacro ()
"Start kmacro.
Expand Down
6 changes: 6 additions & 0 deletions meow-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,14 @@ there's no chance for meow to call an init function."
(add-hook 'window-state-change-functions #'meow--on-window-state-change)
(add-hook 'minibuffer-setup-hook #'meow--minibuffer-setup)
(add-hook 'pre-command-hook 'meow--highlight-pre-command)

(add-hook 'post-command-hook 'meow--highlight-post-command)
(add-hook 'post-command-hook 'meow--maybe-toggle-beacon-state)
(add-hook 'suspend-hook 'meow--on-exit)
(add-hook 'suspend-resume-hook 'meow--update-cursor)
(add-hook 'kill-emacs-hook 'meow--on-exit)
(add-hook 'desktop-after-read-hook 'meow--init-buffers)
(add-hook 'lazy-count-update-hook #'meow--lazy-count-hook)

(meow--enable-shims)
;; meow-esc-mode fix ESC in TUI
Expand All @@ -201,11 +204,14 @@ there's no chance for meow to call an init function."
(remove-hook 'window-state-change-functions #'meow--on-window-state-change)
(remove-hook 'minibuffer-setup-hook #'meow--minibuffer-setup)
(remove-hook 'pre-command-hook 'meow--highlight-pre-command)
(remove-hook 'post-command-hook 'meow--highlight-post-command)
(remove-hook 'post-command-hook 'meow--maybe-toggle-beacon-state)
(remove-hook 'suspend-hook 'meow--on-exit)
(remove-hook 'suspend-resume-hook 'meow--update-cursor)
(remove-hook 'kill-emacs-hook 'meow--on-exit)
(remove-hook 'desktop-after-read-hook 'meow--init-buffers)
(remove-hook 'lazy-count-update-hook #'meow--lazy-count-hook)

(meow--disable-shims)
(meow--remove-modeline-indicator)
(when meow-use-cursor-position-hack
Expand Down
5 changes: 0 additions & 5 deletions meow-face.el
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@
"Indicator for region direction."
:group 'meow)

(defface meow-search-highlight
'((t (:inherit lazy-highlight)))
"Search target highlight."
:group 'meow)

(defface meow-position-highlight-number
'((((class color) (background dark))
(:inherit default))
Expand Down
Loading

0 comments on commit 5ddfc66

Please sign in to comment.