суббота, 29 марта 2008 г.

Search-at-point или как быстро найти слово

Надоело мне нажимать C-s C-w, чтоб искать слово. Особенно неудобно, если в слове есть _.
Поискал в интернете уже готовое, кое-что нашел, но все таки решил написать свое.

(defvar sap-overlay '()
"last highlighted word")

(defface sap-face
'((((type tty pc) (class color))
(:background "magenta4" :foreground "cyan1"))
(((class color) (background light))
(:background "magenta4" :foreground "lightskyblue1"))
(((class color) (background dark))
(:background "palevioletred2" :foreground "brown4"))
(t (:inverse-video t)))
"Face for highlighting search-at-point matches."
:group 'sap-faces)

(defvar sap-face 'sap-face)

(defun isearch-highlight-found-word (curword flag)
(if (= flag 1)
(progn
(setq pt1 (- (point) (length curword)))
(setq pt2 (point))
)
(progn
(setq pt2 (+ (point) (length curword)))
(setq pt1 (point))
)
)
(message "Found at %s" (what-line))
(if sap-overlay (delete-overlay sap-overlay))
(setq sap-overlay (make-overlay 1 1))
(overlay-put sap-overlay 'face sap-face)
(overlay-put sap-overlay 'modification-hooks (list 'delete-this-overlay))
(move-overlay sap-overlay pt1 pt2)
)

(defun isearch-forward-current-word ()
"search forward at point feature."
(interactive)
(if (thing-at-point 'symbol)
(progn
(let ((re-curword) (curword) (offset (point))
(old-case-fold-search case-fold-search) )
(setq curword (thing-at-point 'symbol))
(setq re-curword (concat "\\<" (thing-at-point 'symbol) "\\>") )
(beginning-of-thing 'symbol)
(setq offset (- offset (point))) ; offset from start of symbol/word
(setq pt1 offset) ; offset from start of symbol/word
(setq offset (- (length curword) offset)) ; offset from end
(setq pt2 offset) ; offset from end
(forward-char)
(setq case-fold-search nil)
(if (re-search-forward re-curword nil t)
(isearch-highlight-found-word curword 1)

(backward-char offset)
;; else
(progn (goto-char (point-min))
(if (re-search-forward re-curword nil t)
(isearch-highlight-found-word curword 1)


(progn (message "Searching from top. %s" (what-line))
(backward-char offset))
;; else
(message "Searching from top: Not found"))))
(setq case-fold-search old-case-fold-search))
)
(progn
(if sap-overlay (delete-overlay sap-overlay))
)
)
)

(defun isearch-backward-current-word ()
"Mimic vi search backwards at point feature."
(interactive)
(if (thing-at-point 'symbol) (progn
(let ((re-curword) (curword) (offset (point))
(old-case-fold-search case-fold-search) )
(setq curword (thing-at-point 'symbol))
(setq re-curword (concat "\\<" curword "\\>") )
(beginning-of-thing 'symbol)
(setq offset (- offset (point))) ; offset from start of symbol/word
(forward-char)
(setq case-fold-search nil)
(if (re-search-backward re-curword nil t)
(isearch-highlight-found-word curword 0)
(forward-char offset)
;; else
(progn (goto-char (point-max))
(if (re-search-backward re-curword nil t)
(isearch-highlight-found-word curword 0)
(progn (message "Searching from bottom. %s" (what-line))
(forward-char offset))
;; else
(message "Searching from bottom: Not found"))))
(setq case-fold-search old-case-fold-search)))
(progn
(if sap-overlay (delete-overlay sap-overlay))
)
);if
)

;; F3 will search the word under the cursor
;; Ctrl Shift F3 will search backward
(global-set-key [f3] 'isearch-forward-current-word)
(global-set-key [(shift f3)] 'isearch-backward-current-word)



Нажимаем F3 на слове, чтоб искать вперед и Shift-F3 - для поиска назад.

Комментариев нет: