Skip to content

Commit

Permalink
Fix and refactor symbol-highlight-transient-state
Browse files Browse the repository at this point in the history
  • Loading branch information
fnussbaum committed May 3, 2024
1 parent a58a7d7 commit 8a85819
Showing 1 changed file with 40 additions and 61 deletions.
101 changes: 40 additions & 61 deletions layers/+spacemacs/spacemacs-navigation/funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,31 @@ If the universal prefix argument is used then kill also the window."
(message "No previously searched for symbol found")))

(defun spacemacs/integrate-evil-search (forward)
;; isearch-string is last searched item. Next time
;; "n" is hit we will use this.
(let* ((symbol (evil-find-thing forward 'symbol))
(regexp (concat "\\<" symbol "\\>")))
(setq isearch-string regexp
isearch-regexp regexp
evil-ex-search-pattern (evil-ex-make-search-pattern regexp)))
;; Next time "n" is hit, go the correct direction.
(setq isearch-forward forward)
(setq evil-ex-search-direction (if forward 'forward 'backward))
;; ahs does a case sensitive search. We could set
;; this, but it would break the user's current
;; sensitivity settings. We could save the setting,
;; then next time the user starts a search we could
;; restore the setting.
;;(setq case-fold-search nil)
;; Place the search term into the search rings.
(isearch-update-ring isearch-string t)
(evil-push-search-history isearch-string forward)
;; Use this search term for empty pattern "%s//replacement/"
;; Append case sensitivity
(setq evil-ex-last-was-search nil
evil-ex-substitute-pattern `(,(concat isearch-string "\\C")
nil (0 0))))
"Set relevant variables for repeating the search with 'n' or 'N'
(after having left the symbol highlight transient state)."
(when-let* ((symbol (thing-at-point 'symbol t))
(regexp (concat "\\<" symbol "\\>")))
(setq isearch-string regexp
isearch-regexp regexp
evil-ex-search-pattern (evil-ex-make-search-pattern regexp))
;; Set the search direction for both possible values of
;; `evil-search-module': 'isearch' and 'evil-search'.
(setq isearch-forward forward)
(setq evil-ex-search-direction (if forward 'forward 'backward))
;; ahs does a case sensitive search. We could set
;; this, but it would break the user's current
;; sensitivity settings. We could save the setting,
;; then next time the user starts a search we could
;; restore the setting.
;;(setq case-fold-search nil)
;; Place the search term into the search rings.
(isearch-update-ring isearch-string t)
(evil-push-search-history isearch-string forward)
;; Use this search term for empty pattern "%s//replacement/"
;; Append case sensitivity
(setq evil-ex-last-was-search nil
evil-ex-substitute-pattern `(,(concat isearch-string "\\C")
nil (0 0)))))

(defun spacemacs//ahs-setup ()
"Remember the `auto-highlight-symbol-mode' state,
Expand All @@ -107,14 +108,16 @@ before highlighting a symbol."
`auto-highlight-symbol'"
(interactive)
(setq spacemacs--ahs-searching-forward t)
(spacemacs/quick-ahs-forward))
(spacemacs//ahs-setup)
(spacemacs/symbol-highlight-transient-state/spacemacs/quick-ahs-forward))

(defun spacemacs/enter-ahs-backward ()
"Go to the previous occurrence of symbol under point with
`auto-highlight-symbol'"
(interactive)
(setq spacemacs--ahs-searching-forward nil)
(spacemacs/quick-ahs-forward))
(spacemacs//ahs-setup)
(spacemacs/symbol-highlight-transient-state/spacemacs/quick-ahs-forward))

(defun spacemacs/quick-ahs-forward ()
"Go to the next occurrence of symbol under point with
Expand All @@ -132,19 +135,15 @@ before highlighting a symbol."
"Go to the next occurrence of symbol under point with
`auto-highlight-symbol'"
(evil-set-jump)
(spacemacs//ahs-setup)
(ahs-highlight-now)
(if (eq forward spacemacs--ahs-searching-forward)
(progn
(spacemacs/integrate-evil-search t)
(ahs-forward))
(spacemacs/integrate-evil-search nil)
(ahs-backward))
(spacemacs/symbol-highlight-transient-state/body))
(ahs-forward)
(ahs-backward)))

(defun spacemacs/symbol-highlight ()
"Highlight the symbol under point with `auto-highlight-symbol'."
(interactive)
(spacemacs/integrate-evil-search t)
(setq spacemacs--ahs-searching-forward t)
(spacemacs//remember-last-ahs-highlight)
(spacemacs//ahs-setup)
(spacemacs/symbol-highlight-transient-state/body))
Expand Down Expand Up @@ -179,28 +178,16 @@ ahs mode is only disabled if it was disabled before a symbol was highlighted.")

(defun spacemacs//ahs-ts-on-exit ()
(setq spacemacs//ahs-ts-exit-window (selected-window))
;; Restore user search direction state as ahs has exitted in a state
;; good for <C-s>, but not for 'n' and 'N'"
(setq isearch-forward spacemacs--ahs-searching-forward)
(spacemacs/integrate-evil-search spacemacs--ahs-searching-forward)
(spacemacs//disable-symbol-highlight-after-ahs-ts-exit))

(defun spacemacs//disable-symbol-highlight-after-ahs-ts-exit ()
"Disable `auto-highlight-symbol-mode', when the
Symbol Highlight Transient State buffer isn't found.
This occurs when the TS wasn't restarted.
It is restarted when navigating to the next or previous symbol.
ahs mode is only disabled if it was disabled before a symbol was highlighted."
(run-with-idle-timer
0 nil
(lambda ()
(unless (string= (spacemacs//transient-state-buffer-title)
"Symbol Highlight")
(cond ((and (spacemacs//prompt-opened-from-ahs-ts-p)
(spacemacs//ahs-was-disabled-in-ahs-ts-exit-window-p))
(spacemacs//disable-ahs-mode-in-ahs-ts-exit-window))
(spacemacs//ahs-was-disabled
(spacemacs//disable-symbol-highlight)))))))
"Disable `auto-highlight-symbol-mode' if it was disabled before a symbol was highlighted."
(cond ((and (spacemacs//prompt-opened-from-ahs-ts-p)
(spacemacs//ahs-was-disabled-in-ahs-ts-exit-window-p))
(spacemacs//disable-ahs-mode-in-ahs-ts-exit-window))
(spacemacs//ahs-was-disabled
(spacemacs//disable-symbol-highlight))))

(defun spacemacs//prompt-opened-from-ahs-ts-p ()
"Was a prompt opened (for example: M-x),
Expand All @@ -220,14 +207,6 @@ in the window where the Symbol Highlight Transient State was closed."
(auto-highlight-symbol-mode -1)
(setq-local spacemacs//ahs-was-disabled nil))

(defun spacemacs//transient-state-buffer-title ()
(let ((transient-state-buffer-name " *LV*"))
(when (get-buffer transient-state-buffer-name)
(with-current-buffer transient-state-buffer-name
(buffer-substring-no-properties
(point-min)
(string-match "Transient State" (buffer-string)))))))

(defun spacemacs/symbol-highlight-reset-range ()
"Reset the range for `auto-highlight-symbol'."
(interactive)
Expand Down

0 comments on commit 8a85819

Please sign in to comment.