unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#14427: 24.3.50; Highlight symbol at point
@ 2013-05-20 22:54 Juri Linkov
  2013-05-20 23:28 ` Juri Linkov
  2013-06-02 10:01 ` Jambunathan K
  0 siblings, 2 replies; 6+ messages in thread
From: Juri Linkov @ 2013-05-20 22:54 UTC (permalink / raw)
  To: 14427

This feature request mostly doesn't depend on the outcome of bug#14405.
I don't know why Jambunathan didn't propose this feature earlier, but it
logically follows from his other improvements in hi-lock.el, i.e. the
feature that doesn't ask for a face and automatically uses the next face
from the list of available faces suggests also a command that doesn't ask
for a symbol and automatically uses the symbol at point:

=== modified file 'lisp/hi-lock.el'
--- lisp/hi-lock.el	2013-03-31 13:34:35 +0000
+++ lisp/hi-lock.el	2013-05-20 22:52:54 +0000
@@ -483,6 +461,27 @@ (defun hi-lock-face-phrase-buffer (regex
   (unless hi-lock-mode (hi-lock-mode 1))
   (hi-lock-set-pattern regexp face))
 
+;;;###autoload
+(defalias 'highlight-symbol-at-point 'hi-lock-face-symbol-at-point)
+;;;###autoload
+(defun hi-lock-face-symbol-at-point ()
+  "Set face of each match of the symbol at point.
+Use `find-tag-default-as-symbol-regexp' to retrieve the symbol at point.
+Use non-nil `hi-lock-auto-select-face' to retrieve the next face
+from `hi-lock-face-defaults' automatically.
+
+Use Font lock mode, if enabled, to highlight symbol at point.
+Otherwise, use overlays for highlighting.  If overlays are used,
+the highlighting will not update as you type."
+  (interactive)
+  (let* ((regexp (hi-lock-regexp-okay
+		  (find-tag-default-as-symbol-regexp)))
+	 (hi-lock-auto-select-face t)
+	 (face (hi-lock-read-face-name)))
+    (or (facep face) (setq face 'hi-yellow))
+    (unless hi-lock-mode (hi-lock-mode 1))
+    (hi-lock-set-pattern regexp face)))
+
 (defun hi-lock-keyword->face (keyword)
   (cadr (cadr (cadr keyword))))    ; Keyword looks like (REGEXP (0 'FACE) ...).
 





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#14427: 24.3.50; Highlight symbol at point
  2013-05-20 22:54 bug#14427: 24.3.50; Highlight symbol at point Juri Linkov
@ 2013-05-20 23:28 ` Juri Linkov
  2013-05-21 19:07   ` Juri Linkov
  2013-06-02 10:01 ` Jambunathan K
  1 sibling, 1 reply; 6+ messages in thread
From: Juri Linkov @ 2013-05-20 23:28 UTC (permalink / raw)
  To: 14427

BTW, the current symbol is incorrectly highlighted when
`hi-lock-face-buffer' is called from isearch.  This should be
fixed by this patch:

=== modified file 'lisp/isearch.el'
--- lisp/isearch.el	2013-05-18 22:46:59 +0000
+++ lisp/isearch.el	2013-05-20 23:27:28 +0000
@@ -1773,7 +1791,10 @@ (defun isearch-highlight-regexp ()
     (isearch-done nil t)
     (isearch-clean-overlays))
   (require 'hi-lock nil t)
-  (let ((string (cond (isearch-regexp isearch-string)
+  (let ((string (cond ((functionp isearch-word)
+		       (funcall isearch-word isearch-string))
+		      (isearch-word (word-search-regexp isearch-string))
+		      (isearch-regexp isearch-string)
 		      ((if (and (eq isearch-case-fold-search t)
 				search-upper-case)
 			   (isearch-no-upper-case-p

It still can't correctly set `case-fold-search' for hi-lock, but
this can't be improved because hi-lock is based on font-lock
when font-lock is enabled in the buffer, and changing
`font-lock-keywords-case-fold-search' to the same value as
`isearch-case-fold-search' might break font-lock highlighting
for its other default keywords.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#14427: 24.3.50; Highlight symbol at point
  2013-05-20 23:28 ` Juri Linkov
@ 2013-05-21 19:07   ` Juri Linkov
  0 siblings, 0 replies; 6+ messages in thread
From: Juri Linkov @ 2013-05-21 19:07 UTC (permalink / raw)
  To: 14427

The intention was to accompany the proposed `highlight-symbol-at-point'
with its isearch correlate.  This feature was requested long ago in
http://www.emacswiki.org/emacs/SearchAtPoint
and after recent improvements it is straightforward to implement now.

For keybindings it makes sense to take the command `find-tag'
as a model with its keybinding `M-.' since `find-tag' searches
for a tag in source code and `isearch-forward-symbol-at-point' and
`highlight-symbol-at-point' should do the same only in the current buffer:

M-.     - find-tag
M-s .   - isearch-forward-symbol-at-point
M-s h . - highlight-symbol-at-point

=== modified file 'lisp/bindings.el'
--- lisp/bindings.el	2013-04-22 04:17:30 +0000
+++ lisp/bindings.el	2013-05-21 19:05:45 +0000
@@ -892,6 +892,7 @@ (define-key esc-map "s" search-map)
 
 (define-key search-map "o"  'occur)
 (define-key search-map "hr" 'highlight-regexp)
+(define-key search-map "h." 'highlight-symbol-at-point)
 (define-key search-map "hp" 'highlight-phrase)
 (define-key search-map "hl" 'highlight-lines-matching-regexp)
 (define-key search-map "hu" 'unhighlight-regexp)

=== modified file 'lisp/isearch.el'
--- lisp/isearch.el	2013-05-18 22:46:59 +0000
+++ lisp/isearch.el	2013-05-21 18:59:23 +0000
@@ -677,6 +677,7 @@ (define-key global-map "\C-r" 'isearch-b
 (define-key esc-map "\C-r" 'isearch-backward-regexp)
 (define-key search-map "w" 'isearch-forward-word)
 (define-key search-map "_" 'isearch-forward-symbol)
+(define-key search-map "." 'isearch-forward-symbol-at-point)
 
 ;; Entry points to isearch-mode.
 
@@ -817,6 +818,25 @@ (defun isearch-backward-regexp (&optiona
   (interactive "P\np")
   (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
 
+(defun isearch-forward-symbol-at-point ()
+  "Do incremental search forward for a symbol found near point.
+Like ordinary incremental search except that the symbol found at point
+is added to the search string initially as a regexp surrounded
+by symbol boundary constructs \\_< and \\_>.
+See the command `isearch-forward-symbol' for more information."
+  (interactive)
+  (isearch-forward-symbol nil 1)
+  (let ((bounds (find-tag-default-bounds)))
+    (cond
+     (bounds
+      (when (< (car bounds) (point))
+	(goto-char (car bounds)))
+      (isearch-yank-string
+       (buffer-substring-no-properties (car bounds) (cdr bounds))))
+     (t
+      (setq isearch-error "No symbol at point")
+      (isearch-update)))))
+
 \f
 ;; isearch-mode only sets up incremental search for the minor mode.
 ;; All the work is done by the isearch-mode commands.


PS: One problem was that `find-tag-default' returns only a tag as a string,
not its exact location, but it's necessary to move point to the beginning
of the symbol.  To solve this problem, another patch splits `find-tag-default'
with part of code moved to `find-tag-default-bounds' that returns
the beginning anf end of the found tag:

=== modified file 'lisp/subr.el'
--- lisp/subr.el	2013-05-18 22:46:59 +0000
+++ lisp/subr.el	2013-05-21 19:07:07 +0000
@@ -2717,9 +2717,7 @@ (defsubst buffer-narrowed-p ()
   "Return non-nil if the current buffer is narrowed."
   (/= (- (point-max) (point-min)) (buffer-size)))
 
-(defun find-tag-default ()
-  "Determine default tag to search for, based on text at point.
-If there is no plausible default, return nil."
+(defun find-tag-default-bounds ()
   (let (from to bound)
     (when (or (progn
 		;; Look at text around `point'.
@@ -2742,7 +2740,14 @@ (defun find-tag-default ()
 		     (< (setq from (point)) bound)
 		     (skip-syntax-forward "w_")
 		     (setq to (point)))))
-      (buffer-substring-no-properties from to))))
+      (cons from to))))
+
+(defun find-tag-default ()
+  "Determine default tag to search for, based on text at point.
+If there is no plausible default, return nil."
+  (let ((bounds (find-tag-default-bounds)))
+    (when bounds
+      (buffer-substring-no-properties (car bounds) (cdr bounds)))))
 
 (defun find-tag-default-as-regexp ()
   "Return regexp that matches the default tag at point.






^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#14427: 24.3.50; Highlight symbol at point
  2013-05-20 22:54 bug#14427: 24.3.50; Highlight symbol at point Juri Linkov
  2013-05-20 23:28 ` Juri Linkov
@ 2013-06-02 10:01 ` Jambunathan K
  2013-06-02 10:04   ` Jambunathan K
  1 sibling, 1 reply; 6+ messages in thread
From: Jambunathan K @ 2013-06-02 10:01 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 14427


If you could commit this change, I can start using it right away.  I
have no firm opinion on whether `hi-lock-auto-select-face' should come
from user setting or be let bound.

Remember to provide a keybinding in hi-lock mode.  I suggest C-x w
"dot".

Juri Linkov <juri@jurta.org> writes:

> This feature request mostly doesn't depend on the outcome of bug#14405.
> I don't know why Jambunathan didn't propose this feature earlier, but it
> logically follows from his other improvements in hi-lock.el, i.e. the
> feature that doesn't ask for a face and automatically uses the next face
> from the list of available faces suggests also a command that doesn't ask
> for a symbol and automatically uses the symbol at point:
>
> === modified file 'lisp/hi-lock.el'
> --- lisp/hi-lock.el	2013-03-31 13:34:35 +0000
> +++ lisp/hi-lock.el	2013-05-20 22:52:54 +0000
> @@ -483,6 +461,27 @@ (defun hi-lock-face-phrase-buffer (regex
>    (unless hi-lock-mode (hi-lock-mode 1))
>    (hi-lock-set-pattern regexp face))
>  
> +;;;###autoload
> +(defalias 'highlight-symbol-at-point 'hi-lock-face-symbol-at-point)
> +;;;###autoload
> +(defun hi-lock-face-symbol-at-point ()
> +  "Set face of each match of the symbol at point.
> +Use `find-tag-default-as-symbol-regexp' to retrieve the symbol at point.
> +Use non-nil `hi-lock-auto-select-face' to retrieve the next face
> +from `hi-lock-face-defaults' automatically.
> +
> +Use Font lock mode, if enabled, to highlight symbol at point.
> +Otherwise, use overlays for highlighting.  If overlays are used,
> +the highlighting will not update as you type."
> +  (interactive)
> +  (let* ((regexp (hi-lock-regexp-okay
> +		  (find-tag-default-as-symbol-regexp)))
> +	 (hi-lock-auto-select-face t)
> +	 (face (hi-lock-read-face-name)))
> +    (or (facep face) (setq face 'hi-yellow))
> +    (unless hi-lock-mode (hi-lock-mode 1))
> +    (hi-lock-set-pattern regexp face)))
> +
>  (defun hi-lock-keyword->face (keyword)
>    (cadr (cadr (cadr keyword))))    ; Keyword looks like (REGEXP (0 'FACE) ...).
>  





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#14427: 24.3.50; Highlight symbol at point
  2013-06-02 10:01 ` Jambunathan K
@ 2013-06-02 10:04   ` Jambunathan K
  2013-06-03  8:54     ` Juri Linkov
  0 siblings, 1 reply; 6+ messages in thread
From: Jambunathan K @ 2013-06-02 10:04 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 14427

Jambunathan K <kjambunathan@gmail.com> writes:

> If you could commit this change, I can start using it right away.

Small note.  I haven't tested the isearch portions of the patch.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#14427: 24.3.50; Highlight symbol at point
  2013-06-02 10:04   ` Jambunathan K
@ 2013-06-03  8:54     ` Juri Linkov
  0 siblings, 0 replies; 6+ messages in thread
From: Juri Linkov @ 2013-06-03  8:54 UTC (permalink / raw)
  To: Jambunathan K; +Cc: 14427-done

>> If you could commit this change, I can start using it right away.

Thanks for the reminder, this is installed now.

> Small note.  I haven't tested the isearch portions of the patch.

Isearch integration with hi-lock is far from ideal yet.
The limiting factor is that hi-lock is based on font-lock
when font-lock is enabled.  Otherwise, it is plain search-based.

Perhaps we should check in `isearch-highlight-regexp'
whether font-lock is active in the current buffer,
and when font-lock is disabled then let-bind all relevant
search variables like `lax-whitespace', `search-invisible',
filters, etc.  So search-based hi-lock will be able to highlight
correctly the same matches as isearch.

This will provide the correct conversion of the search parameters
to hi-lock (at least partly for the case when font-lock is disabled
and hi-lock is search-based).





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-06-03  8:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-20 22:54 bug#14427: 24.3.50; Highlight symbol at point Juri Linkov
2013-05-20 23:28 ` Juri Linkov
2013-05-21 19:07   ` Juri Linkov
2013-06-02 10:01 ` Jambunathan K
2013-06-02 10:04   ` Jambunathan K
2013-06-03  8:54     ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).