Subject: bug#14427: 24.3.50; Highlight symbol at point

From: Juri Linkov <[email-masked]>
Date: Tue, 21 May 2013 22:07:49 +0300

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)))))
+
 
 ;; 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.