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

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

Query replace inrectangles


From: Thorsten Bucksch
Subject: Query replace inrectangles
Date: Thu, 30 Jan 2003 13:00:49 +0100

Hello ,

since a long time I am missing a "query-replace-in-rectangles" for the
emacs. I found something in the web, but this does not work in emacs 21
(error: wrong number of arguments ...).

Since version 21 there is already a "string-in-rectangle". This is not to
bad. But replacing in a rectangular area is still not possible.

Can anyone help me with that topic?

Thanks a lot!

Thorsten
____________________________________________________________________________
_________
------------------------------------------------------------------------
; bind commands to ^xr% and ^xr&:
(define-key ctl-x-map "r&" `replace-string-in-rectangle)
(define-key ctl-x-map "r%" `query-replace-in-rectangle)
; ------------------------------------------------------------------------
(defun replace-string-in-rectangle-line (startpos ignore ignore)
; narrow to region:
(narrow-to-region startpos (point))
; do replace
(goto-char startpos)
(while (search-forward from-string nil t)
    (replace-match to-string nil t))
(widen)
)

(defun replace-string-in-rectangle(from-string to-string &optional ignore)
  "Replace occurrences of FROM-STRING with TO-STRING in a rectangle"
(interactive (query-replace-read-args "Replace string" nil))
(setq start (mark))
(setq end (point))
(operate-on-rectangle `replace-string-in-rectangle-line start end t)

"interactive do mode in load path"
  (require 'ido)
  (ido-mode t)

)
; ------------------------------------------------------------------------
(defun query-replace-in-rectangle-line (startpos ignore ignore)
(setq endpos (point))
(setq transient-mark-mode t)
(set-mark startpos)
(goto-char endpos)
(perform-replace from-string to-string t nil nil 1 nil)
(setq replace-count-rectangle-lines (+ replace-count-rectangle-lines 1))
)

(defun query-replace-in-rectangle(from-string to-string &optional ignore)
"Query-replace occurrences of FROM-STRING with TO-STRING in a rectangle.
query-replace is done on one line of the rectangle at a time.  To
abort the query-replace for entire rectangle, use ESC-ESC-ESC or ^G."
(interactive (query-replace-read-args "Replace string" nil))
(setq start (mark))
(setq end (point))
(setq replace-count-rectangle 0)
(setq replace-count-rectangle-lines 0)
(apply-on-rectangle `query-replace-in-rectangle-line start end t)
(message "Completed query-replace in a rectangle of %d line%s"
     replace-count-rectangle-lines
     (if (= replace-count-rectangle-lines 1) "" "s")))





reply via email to

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