unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* looking for: query-replace with a "yank-word" binding ala isearch
@ 2002-11-08  4:24 bigfaceworm
  2002-11-08 17:10 ` Kevin Rodgers
  0 siblings, 1 reply; 6+ messages in thread
From: bigfaceworm @ 2002-11-08  4:24 UTC (permalink / raw)


All,

I was wondering if anyone had a yank-word binding for query-replace
similar to the C-w binding for isearch-forward.

I tried defining my own, but because the control switches to the 
minibuffer, I'm not sure how to find the buffer in which to do:

    (buffer-substring (point) (progn (forward-word 1) (point)))

any help?


Thank
BFW

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

* Re: looking for: query-replace with a "yank-word" binding ala isearch
  2002-11-08  4:24 looking for: query-replace with a "yank-word" binding ala isearch bigfaceworm
@ 2002-11-08 17:10 ` Kevin Rodgers
  2002-11-11 16:36   ` looking for: query-replace with a yank-word " Ehud Karni
  2002-11-11 22:21   ` looking for: query-replace with a "yank-word" " bigfaceworm
  0 siblings, 2 replies; 6+ messages in thread
From: Kevin Rodgers @ 2002-11-08 17:10 UTC (permalink / raw)


bigfaceworm wrote:

> I was wondering if anyone had a yank-word binding for query-replace
> similar to the C-w binding for isearch-forward.
> 
> I tried defining my own, but because the control switches to the 
> minibuffer, I'm not sure how to find the buffer in which to do:
> 
>     (buffer-substring (point) (progn (forward-word 1) (point)))

Try wrapping that in

(save-excursion
   (set-buffer (window-buffer minibuffer-scroll-window))
   ...)

-- 
<a href="mailto:&lt;kevin.rodgers&#64;ihs.com&gt;">Kevin Rodgers</a>

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

* Re: looking for: query-replace with a yank-word binding ala isearch
  2002-11-08 17:10 ` Kevin Rodgers
@ 2002-11-11 16:36   ` Ehud Karni
  2002-11-11 22:21   ` looking for: query-replace with a "yank-word" " bigfaceworm
  1 sibling, 0 replies; 6+ messages in thread
From: Ehud Karni @ 2002-11-11 16:36 UTC (permalink / raw)
  Cc: help-gnu-emacs

On Thr, 7 Nov 2002 20:24:37 -0800, bigfaceworm <bigfaceworm@hotmail.com> wrote:
> 
> I tried defining my own, but because the control switches to the 
> minibuffer, I'm not sure how to find the buffer in which to do:
  
I use the following to insert the current word (defined as ANY non
white space string) into the mini buffer:

(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)))
       ;;  audit-mode is a privetly defined mode for MVS
       ;;      ((eq major-mode 'audit-mode)                        
       ;;              (setq str (audit-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-no-properties 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)))

;; to add it to mini buffer 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-w" 'insert-word-or-file-name)))

You don't need to change the `query-replace-map' because when you edit
the replacement string you are using the minibuffer maps.

Ehud.


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

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

* Re: looking for: query-replace with a "yank-word" binding ala isearch
  2002-11-08 17:10 ` Kevin Rodgers
  2002-11-11 16:36   ` looking for: query-replace with a yank-word " Ehud Karni
@ 2002-11-11 22:21   ` bigfaceworm
  2002-11-12 17:46     ` Kevin Rodgers
  1 sibling, 1 reply; 6+ messages in thread
From: bigfaceworm @ 2002-11-11 22:21 UTC (permalink / raw)


Kevin's help worked wonderfully,
*except* now I have some odd behavior in my elisp.
When I debug the routine (via edebug) it works just
fine, but when it's loaded outside of edebug, it 
grabs the wrong characters:

(defun bfw-insert-word ()
  "insert word the cursor is on into minibuffer"
  (interactive)
  (let ((oldtxt (buffer-substring (save-excursion (beginning-of-line)
(point)) (point))))
    (insert
     (save-excursion
       (set-buffer (window-buffer minibuffer-scroll-window))
       (buffer-substring (progn (forward-char (length oldtxt))
(point)) (progn (forward-word 1) (point)))))))

M-x global-set-key f9 bfw-insert-word

Create a new buffer, with contents:

1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z

move the point to the left of the `1', and query-replace

    M-% f9 f9 f9 f9 f9 f9

gives you "12 4 7 b g"

but if you debug the routine (M-x edebug-defun)
and do the same test (hitting 'c' to continue)
you get "1 2 3 4 5 6"


Any idea why a defun would act differently when debugged versus 
normal?


TJ

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

* Re: looking for: query-replace with a "yank-word" binding ala isearch
  2002-11-11 22:21   ` looking for: query-replace with a "yank-word" " bigfaceworm
@ 2002-11-12 17:46     ` Kevin Rodgers
  2002-11-13 17:53       ` bigfaceworm
  0 siblings, 1 reply; 6+ messages in thread
From: Kevin Rodgers @ 2002-11-12 17:46 UTC (permalink / raw)


bigfaceworm wrote:

> Kevin's help worked wonderfully,
> *except* now I have some odd behavior in my elisp.
> When I debug the routine (via edebug) it works just
> fine, but when it's loaded outside of edebug, it 
> grabs the wrong characters:
> 
> (defun bfw-insert-word ()
>   "insert word the cursor is on into minibuffer"
>   (interactive)
>   (let ((oldtxt (buffer-substring (save-excursion (beginning-of-line)
> (point)) (point))))
>     (insert
>      (save-excursion
>        (set-buffer (window-buffer minibuffer-scroll-window))
>        (buffer-substring (progn (forward-char (length oldtxt))
> (point)) (progn (forward-word 1) (point)))))))


Why should the contents of the minibuffer affect what text is inserted?

(defun bfw-insert-word ()
   "Insert the word after point from the selected buffer into the minibuffer.
This advances point in the selected buffer."
   (interactive)
   (insert (save-excursion
	    (set-buffer (window-buffer minibuffer-scroll-window))
	    (buffer-substring (point)
			      (progn (forward-word 1) (point))))))


> M-x global-set-key f9 bfw-insert-word
> 
> Create a new buffer, with contents:
> 
> 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z
> 
> move the point to the left of the `1', and query-replace
> 
>     M-% f9 f9 f9 f9 f9 f9
> 
> gives you "12 4 7 b g"
> 
> but if you debug the routine (M-x edebug-defun)
> and do the same test (hitting 'c' to continue)
> you get "1 2 3 4 5 6"
> 
> 
> Any idea why a defun would act differently when debugged versus 
> normal?


Perhaps the debugger is restoring point or some other aspect of the window/
buffer configuration.

-- 
<a href="mailto:&lt;kevin.rodgers&#64;ihs.com&gt;">Kevin Rodgers</a>

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

* Re: looking for: query-replace with a "yank-word" binding ala isearch
  2002-11-12 17:46     ` Kevin Rodgers
@ 2002-11-13 17:53       ` bigfaceworm
  0 siblings, 0 replies; 6+ messages in thread
From: bigfaceworm @ 2002-11-13 17:53 UTC (permalink / raw)


Works great, thanks.
But I'm confused now.

I didn't expect the code to work b/c I don't see how subsequent calls to 
'bfw-insert-word would insert more and more words.  I expected the
'save-excursion to save the point&mark in the minibuffer-scroll-window (as
well as the state of the current buffer (minibuffer).

But... the documentation says it just saves the point&mark in the current
buffer - a distinction I didn't realize before.

So, this code works b/c we're actually moving the point in the
minibuffer-scroll-window?  (which is protected by a larger save-excursion
inside of the query-replace)?


That is probably why the debugged version worked (like Kevin pointed out).
Learn a new thing every day...


BFW

Kevin Rodgers <kevinr@ihs.com> wrote in message news:<3DD13E7D.5060800@ihs.com>...
> Why should the contents of the minibuffer affect what text is inserted?
> 
> (defun bfw-insert-word ()
>    "Insert the word after point from the selected buffer into the minibuffer.
> This advances point in the selected buffer."
>    (interactive)
>    (insert (save-excursion
> 	    (set-buffer (window-buffer minibuffer-scroll-window))
> 	    (buffer-substring (point)
> 			      (progn (forward-word 1) (point))))))

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

end of thread, other threads:[~2002-11-13 17:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-08  4:24 looking for: query-replace with a "yank-word" binding ala isearch bigfaceworm
2002-11-08 17:10 ` Kevin Rodgers
2002-11-11 16:36   ` looking for: query-replace with a yank-word " Ehud Karni
2002-11-11 22:21   ` looking for: query-replace with a "yank-word" " bigfaceworm
2002-11-12 17:46     ` Kevin Rodgers
2002-11-13 17:53       ` bigfaceworm

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