;; This file: http://user.it.uu.se/~embe8573/conf/emacs-init/get-search-string.el (require 'my-string) (defun region-to-string () "Return the contents of the region as a string. Replace newlines with spaces." (when mark-active (replace-regexp-in-string "\n" " " (buffer-substring-no-properties (mark) (point)) ))) (defun get-search-string (ps) "Ask for a search string. The default string is computed like this: If there is a region, use the contents (newlines are changed into whitespaces). Otherwise, use the word at point. If the default search string is long, it is not shown in full, and with three dots to indicate this. If available, the default string is killed, so it can be yanked and modified if something has to be changed." (let*((default-search (if mark-active (region-to-string) (let ((point-word (thing-at-point 'symbol t))) ; symbol as to get me-to; NO-PROPERTIES (if (stringp point-word) (string-make-multibyte point-word) "") ))) (max-len 20) (peek (if (> (length default-search) max-len) (format "%s..." (substring default-search 0 max-len) ) default-search) )) (unless (empty-string-p default-search) (kill-new default-search)) (read-string (format " %s" (if (string= peek "") (format "%s: " ps) (format "%s (%s): " ps peek) )) nil nil default-search) )) (provide 'get-search-string)