Skip to content

Commit 2c2a294

Browse files
committed
Improve the feature to enable/disable highlighting at point
1 parent 8e07b44 commit 2c2a294

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ jobs:
2323
strategy:
2424
matrix:
2525
emacs-version:
26-
- 26.3
27-
- 27.1
2826
- 28.1
2927
- 29.1
3028
- 29.4

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![](https://raw.githubusercontent.com/jamescherti/bufferwizard.el/main/.images/made-for-gnu-emacs.svg)
55

66
The **bufferwizard** Emacs package offers a suite of helper functions:
7-
- `(bufferwizard-toggle-highlight-symbol-at-point)`: Toggle highlighting for the symbol at point. This function checks if the symbol at point is currently highlighted. If it is, it removes the highlight; otherwise, it applies the highlight. (This is a lightweight alternative to the `highlight-symbol` package.)
7+
- `(bufferwizard-toggle-highlight-at-point)`: Toggles highlighting for the symbol at point. If a selection is active, it highlights the selected text instead. This function checks whether the symbol at point or the selected text is currently highlighted. If it is, the highlight is removed; otherwise, it is applied. (This serves as a lightweight alternative to the `highlight-symbol` package.)
88
- `(bufferwizard-replace-symbol-at-point)`: Replace occurrences of a symbol at point with a specified string.
99
- `(bufferwizard-clone-indirect-buffer)` and `(bufferwizard-clone-and-switch-to-indirect-buffer)`: These functions are enhanced versions of the built-in `clone-indirect-buffer`. They create an indirect buffer with the same content as the current buffer while preserving the point position, window start, and horizontal scroll position. This package also provides the `(bufferwizard-switch-to-base-buffer)` function, which allows switching from an indirect buffer to its corresponding base buffer.
1010

@@ -40,9 +40,9 @@ To install `bufferwizard` with `use-package` and `:vc` (Emacs >= 30):
4040

4141
#### Case sensitivity
4242

43-
The functions `(bufferwizard-toggle-highlight-symbol-at-point)` and `(bufferwizard-replace-symbol-at-point)` depend on built-in functions that can be customized via the following variable:
43+
The functions `(bufferwizard-toggle-highlight-at-point)` and `(bufferwizard-replace-symbol-at-point)` depend on built-in functions that can be customized via the following variable:
4444

45-
- `case-fold-search`: This buffer-local variable determines the behavior of `(bufferwizard-toggle-highlight-symbol-at-point)` and `(bufferwizard-replace-symbol-at-point)`. When set to t (default), both symbol highlighting and searches become case-insensitive, matching symbols regardless of case. When set to nil, they become case-sensitive, matching symbols only when the case exactly matches the text in the buffer.
45+
- `case-fold-search`: This buffer-local variable determines the behavior of `(bufferwizard-toggle-highlight-at-point)` and `(bufferwizard-replace-symbol-at-point)`. When set to t (default), both symbol highlighting and searches become case-insensitive, matching symbols regardless of case. When set to nil, they become case-sensitive, matching symbols only when the case exactly matches the text in the buffer.
4646
Example:
4747
```elisp
4848
;; Setting case-fold-search to nil enables case-sensitive symbol highlighting

bufferwizard.el

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
;; Version: 1.0.0
77
;; URL: https://github.com/jamescherti/bufferwizard.el
88
;; Keywords: convenience
9-
;; Package-Requires: ((emacs "26.1"))
9+
;; Package-Requires: ((emacs "28.1"))
1010
;; SPDX-License-Identifier: GPL-3.0-or-later
1111

1212
;; This file is free software; you can redistribute it and/or modify
@@ -241,39 +241,57 @@ This function confirms each replacement."
241241

242242
;;; Highlight symbols
243243

244+
(defun bufferwizard-get-symbol-or-region-regexp ()
245+
"Return the regexp of the selected region or the default symbol at point."
246+
(when-let* ((regexp (if (use-region-p)
247+
(buffer-substring-no-properties (region-beginning)
248+
(region-end))
249+
(find-tag-default))))
250+
regexp))
251+
244252
(defun bufferwizard-highlight-p ()
245253
"Return non-nil the symbol at point is currently highlighted."
246-
(when-let* ((regexp (find-tag-default-as-symbol-regexp)))
254+
(when-let* ((regexp (bufferwizard-get-symbol-or-region-regexp)))
247255
(member regexp (hi-lock--regexps-at-point))))
248256

249257
;;;###autoload
250-
(defun bufferwizard-highlight-symbol-at-point ()
258+
(defun bufferwizard-highlight-at-point ()
251259
"Highlight the symbol at point in the current buffer.
252260
253261
This function identifies the symbol at the current point, generates the
254262
appropriate regular expression for it, and applies highlighting using the
255263
built-in `hi-lock' package."
256264
(interactive)
257-
(when-let* ((regexp (find-tag-default-as-symbol-regexp)))
258-
(hi-lock-face-symbol-at-point)))
265+
;; Similar to `hi-lock-face-symbol-at-point'
266+
(let* ((regexp (bufferwizard-get-symbol-or-region-regexp))
267+
(hi-lock-auto-select-face t)
268+
(face (hi-lock-read-face-name)))
269+
(when regexp
270+
(or (facep face) (setq face 'hi-yellow))
271+
(unless hi-lock-mode (hi-lock-mode 1))
272+
(hi-lock-set-pattern
273+
regexp face nil nil
274+
(if (and case-fold-search search-upper-case)
275+
(isearch-no-upper-case-p regexp t)
276+
case-fold-search)))))
259277

260278
;;;###autoload
261-
(defun bufferwizard-unhighlight-symbol-at-point ()
279+
(defun bufferwizard-unhighlight-at-point ()
262280
"Remove highlighting for the symbol at point."
263281
(interactive)
264-
(when-let* ((regexp (find-tag-default-as-symbol-regexp)))
282+
(when-let* ((regexp (bufferwizard-get-symbol-or-region-regexp)))
265283
(hi-lock-unface-buffer regexp)))
266284

267285
;;;###autoload
268-
(defun bufferwizard-toggle-highlight-symbol-at-point ()
286+
(defun bufferwizard-toggle-highlight-at-point ()
269287
"Toggle highlighting for the symbol at point.
270288
271289
This function checks if the symbol at point is currently highlighted.
272290
If it is, it removes the highlight; otherwise, it applies the highlight."
273291
(interactive)
274292
(if (bufferwizard-highlight-p)
275-
(bufferwizard-unhighlight-symbol-at-point)
276-
(bufferwizard-highlight-symbol-at-point)))
293+
(bufferwizard-unhighlight-at-point)
294+
(bufferwizard-highlight-at-point)))
277295

278296
;;;###autoload
279297
(defun bufferwizard-unhighlight ()

0 commit comments

Comments
 (0)