bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: search,query replace UI Question


From: Ehud Karni
Subject: Re: search,query replace UI Question
Date: Sat, 29 Sep 2001 22:40:26 +0200

On Sat, 29 Sep 2001 11:10:10 GMT, Sivaram N<sivaram.neelakandanATwipro.com> 
<nospam@newsranger.com> wrote:
> 
> Hello,
> I've been using emacs for 6 months and i have a question regarding
> the above actions.
> C-s  followed by C-w(s) get the word(s) at point into the search buffer.
> Why doesn't the same thing happen when I do M-%,followed by C-w?
> Wouldn't that be more consistent?
> Usually(I said usually), search is followed by replace,people take a
> quick look at what they want to replace & then invoke replace. So, 
> most other editors have the search&replace within a single dialog with
> the prior search string in it already OR within a drop down list.
> So,the search action is not repeated and the replace can happen pronto.
> 
> While I'm not asking for the same bells & whistles,why is it that it's
> so easy to search while I'll have to type it for replace? If I've
> missed an easy way to get strings into query-replace,

I wrote some commands that do what you ask for. Here they are:

(defun insert-word-or-file-name ()
  "copy word cursor is on or file name to minibuff input"
       (interactive)
       (let* ((bfl (current-buffer))
              (str ""))
           (set-buffer (buffer-name-not-mini))
           (cond
               ((eq major-mode 'dired-mode)
                       (setq str (dired-get-filename t t)))
               (t
                       (let (bch ech)
                           (forward-char 1)
                           (backward-to-non-blank)
                           (setq bch (point))
                           (re-search-forward "[^ \t\n][ \t\n]" (point-max) t)
                           (setq ech (1- (point)))
                           (setq str (buffer-substring bch ech)))))
           (set-buffer bfl)
           (insert str)))

(defun backward-to-non-blank () "go to 1st non blank (after blank) to left"
 (interactive)
       (if (re-search-backward "[ \t\n][^ \t\n]" (point-min) t)
           (forward-char 1)
           (if (string-match "[^ \t\n]" (buffer-substring 1 2))
               (goto-char (point-min)))))

(defun buffer-name-not-mini ()
  "Return the name of current buffer, as a string.
If current buffer is the *mini-buffer* return name of previous-window."
       (buffer-name (if (window-minibuffer-p)
                           (if (eq (get-lru-window) (next-window))
                               (window-buffer (previous-window))
                               (window-buffer (next-window)))
                           nil)))


;;some extra functions I use
(defun insert-buffer-name ()
  "insert buffer name of current buffer or most recent buffer when in 
minibuffer"
       (interactive)
       (insert (buffer-name-not-mini)))


(defun insert-buffer-dir-name ()
  "insert dir name of current buffer or most recent buffer when in minibuffer"
       (interactive)
       (let* ((bfn (buffer-file-name (get-buffer (buffer-name-not-mini)))))
           (if bfn
               (insert (file-name-directory bfn)))))

(defun insert-buffer-file-name ()
  "insert file name of current buffer or most recent buffer when in minibuffer"
       (interactive)
       (let* ((bfn (buffer-file-name (get-buffer (buffer-name-not-mini)))))
           (if bfn
               (insert (file-name-nondirectory bfn)))))

(defun insert-current-date-time-minibuf ()
  "insert the current date and time into mini-buffer."
       (interactive)
       (insert (format-time-string "%y%m%d_%H%M%S" (current-time))))


;;setup the needed keys in minibuffer key maps
(defun keymap-test (var)           ; internal function for keymap checking
       (and (boundp var)
            (keymapp (symbol-value var))))

(let ((minimaps (apropos-internal "mini" 'keymap-test))
      map op)
       (while minimaps
           (setq map (symbol-value (car minimaps)))
           (setq minimaps (cdr minimaps))
           (define-key map "\C-b" 'insert-buffer-name)
           (define-key map "\C-d" 'insert-buffer-dir-name)
           (define-key map "\C-f" 'insert-buffer-file-name)
           (define-key map "\C-w" 'insert-word-or-file-name)
           (define-key map "\C-t" 'insert-current-date-time-minibuf)))


Add all this to your .emacs and try Control+[bdfwt] in the minibuffer.
You can use the ^W for renaming files when you are in `dired' mode.

Ehud.


-- 
 Ehud Karni     Mivtach - Simon  Insurance   /"\
 Tel: +972-3-7966-561 Fax: +972-3-7966-667   \ /  ASCII Ribbon Campaign
 (USA) Fax and  voice  mail: 1-815-5509341    X   Against  HTML  Mail
     Better     Safe     Than     Sorry      / \
     mailto:ehud@unix.simonwiesel.co.il    http://www.simonwiesel.co.il



reply via email to

[Prev in Thread] Current Thread [Next in Thread]