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

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

RE: search&replace macro puzzle please help...


From: Bingham, Jay
Subject: RE: search&replace macro puzzle please help...
Date: Tue, 18 Sep 2001 11:42:46 -0500

Here is some code that I have written to do a similar thing.  I did not
originate the concept.  I found the method in someone else's code, I
think that it was a released package.  A good place to look for examples
is in the source code for emacs (the .el files).  There are two
functions because I wanted to be able to use the tl_replace-match
function from other functions.  I have run these functions in emacs 20.3
running on a unix machine and 20.7 running under windows, I have not
tested them under any other versions of emacs.  They should work in
versions prior to 20.7, but I cannot guaranty that they will.  As the
note in the tl_replace-match function states there seems to be a bug in
replace-match which prevents it from adjusting markers as is done in
some other functions that remove text from a buffer.  I don't know if
this will be fixed in version 21 or not.  I have not reported the
condition because I was able to work around it.  Hope this helps.

;; Function: tl_replace-match
;;   Adjust the starting location marker if needed and replace the
match.
;;
;;   Note: this function modifies the variable start-loc which must be
defined
;;         prior to calling the function.
;;
;; Pseudo code:
;;  adjust the starting location if the match to be replaced is before
it
;;  replace the match with the supplied argument
;; 
(defun tl_replace-match (replstring &optional fixedcase literal string
subexp)
  "Adjust the starting location marker if needed and replace the match.
This function is a wrapper around the replace-match function to overcome
the
inability of replace-match to adjust markers.

The variable start-loc must be defined prior to calling this function."
  ;; If no subexp was supplied use zero
  (let ((subx (or subexp 0)))
    ;; If the end of the match is before or at the starting location
    ;; decrement the starting location by the length of the match.
    (if (<= (match-end subx) start-loc)
        (setq start-loc
              (- start-loc (- (match-end subx) (match-beginning subx))))
      ;; else if the start of the match is before the starting location
      ;; set the starting location to the match start
      (if (< (match-beginning subx) start-loc)
          (setq start-loc (match-beginning subx)))))
  (replace-match replstring fixedcase literal string subexp))

;; Function: trim-buffer
;;  Remove trailing whitespace from the lines in a buffer
;;  (Whitespace is: space, tab, and carriage return characters).
;;  For files which display (DOS) on the left end of the status line do
;;  find-file-literally to disable the conversion before using this
function.
;;
;; Pseudo code:
;;  save the starting location
;;  go to the beginning of the buffer
;;  while any space, tab or carriage-return characters
;;        are found at the end of a line and the end of the buffer has
not
;;        been reached
;;    adjust the starting location if the chars to be removed are before
it
;;    replace them with a null string
;;  end while
;;  clear the modified flag
;;  go to the starting location
;;
(defun trim-buffer ()
  "Remove trailing whitespace from the lines in a buffer
leaving the point in the `same place' it was when the function was
invoked.
The `same place' means that it will be on the same character that it was
on
unless the character is one that was removed, in which case it will be
on the
last non-whitespace character to the right of that position.
Whitespace for the purposes of this function is space, tab, and carriage
return characters."
  (interactive)
  (let ((start-loc (point)))
    (goto-char (point-min))
    (while (and (< (point) (point-max))
                (re-search-forward "[ \t?\r]+$" nil t))
      ;; replace the match and adjust the start location if necessary.
      (tl_replace-match "" nil t)
      (and (< (point) (point-max)) (forward-char)))
    (not-modified)
    (goto-char start-loc)))


J_)
C_)ingham
.    COMPAQ Telecommunications
.    Austin, TX

 -----Original Message-----
From:   Chris Seberino [mailto:seberino@spawar.navy.mil] 
Sent:   Monday, 17 September, 2001 3:34 p
To:     help-gnu-emacs@gnu.org
Subject:        search&replace macro puzzle please help...

Hello! 

I want a search and replace command that 

1. works thru entire doc
2. ends up in same place as before

I got this so far...

(defun new-replace-string(search-string replace-string)
   (interactive "ssearch string: \nsreplace string: ")
   (setq total-fix 0)
   (setq fix-per-replacement (- (length replace-string)
                                (length search-string)
                                )
      )
   (setq initial-shift (- (point) 1))
   (backward-char initial-shift)
   (while (search-forward  search-string nil t)
      (replace-match replace-string nil t)
      (setq total-fix (+ total-fix fix-per-replacement))
      )
   (backward-char (- (point) 1))
   (forward-char (+ initial-shift 0))
)

THE TRICKY PART IS GETTING CURSOR BACK TO ORIGINAL
POSITION.  THE MATH IS TRICKY DUE TO DIFFERENT
LENGTH OF search-string & replace-string and
probably also because of tabs.

Any help would be greatly appreciate.

Sincerely,

Chris
-- 
=======================================================
| Dr. Christian Seberino  || (619) 553-7811 (office1) |
| SPAWARSYSCEN 2733       || (619) 553-2564 (office2) |
| 53560 HULL ST           || (619) 553-6307 (fax)     |
| SAN DIEGO CA 92152-5001 || seberino@spawar.navy.mil |
=======================================================

_______________________________________________
Help-gnu-emacs mailing list
Help-gnu-emacs@gnu.org
http://mail.gnu.org/mailman/listinfo/help-gnu-emacs



reply via email to

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