[Top][All Lists]

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


From: Joe Corneli
Subject: not-replace-regexp.el
Date: Sat, 07 Aug 2004 13:22:46 -0500

Two little functions to help keep the function `replace-regexp' out
of your elisp code.  The first provides a quick way to insert the
preferred form, the second expunges appearances of replace-regexp
and replaces them with the preferred form.

There are also a couple of functions that are trivial changes to
things in lisp.el included at the end.  Why not add them to lisp.el?

;;; not-replace-regexp.el -- avoid using `replace-regexp' in your code

;; Copyright (C) 2004 Joe Corneli <address@hidden>

;; Time-stamp: <jac -- Sat Aug  7 13:07:49 CDT 2004>

;; This file is not part of GNU Emacs, but it is distributed under
;; the same terms as GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 2, or (at your
;; option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; I'm not sure why there isn't a standard short-hand for the idiom
;;   (while (re-search-forward regexp nil t) 
;;    (replace-match to-string nil nil))
;; However, it is not clear whether there is any benefit to a shorter
;; version it unless it really was adopted as a new standard, since
;; the above is so ubiquitous at this point in time, and any new
;; short-hand would likely to confuse most people.
;; Accordingly, this file simply contains a function that helps write
;; the cited expression more quickly, and another function for
;; replacing uses of `replace-regexp' appearing in code with the
;; preferred form.

;;; Code:

(defun not-replace-regexp ()
  "Insert the `replace-regexp' variant intended for use in programs.
See documentation of `replace-regexp' for details.  Note that
this function expects the user to write out the fully quoted
regexp exactly as it is supposed to appear in the code.  One
could instead use `query-replace-read-args' to read in a
user-level unquoted regexp, but the current way of doing things
may be more intuitive to more programmers. Feedback about this
point is certainly welcome."
  (let* ((regexp (read-string "Replace regexp: "))
         (to-string (read-string 
                     (concat "Replace regexp " regexp " with : "))))
    (insert "(while (re-search-forward \"" regexp "\" nil t)\n"
            "(replace-match \"" to-string "\" nil nil))"))
  (indent-region (save-excursion (backward-sexp)

(defun rid-code-of-replace-regexp ()
  "Replace `replace-regexp' with the preferred form for use in code.
See documentation of `replace-regexp' for details."
  ;; we don't try to replace instances of `replace-regexp' that appear
  ;; in comments.  This won't catch everything, though -- for instance,
  ;; if someone were to write 
  ;;  ( ;; this is where we replace "foo"
  ;;   replace-regexp "foo" "bar")
  ;; for generality that should be replaced but it won't be matched by
  ;; what I have here.
  (while (re-search-forward "^[^;\n]*(\\s-?replace-regexp\\s-" nil t)
    (let ((first-arg 
            (progn (forward-sexp)
            (progn (forward-sexp)
      (insert "(while (re-search-forward " first-arg " nil t)\n"
              "(replace-match " second-arg " nil nil))"))
    (indent-region (save-excursion (backward-sexp)

;; If these were in Emacs they would most likely be in lisp.el
(defun backward-delete-sexp (&optional arg)
  "Delete the sexp (balanced expression) preceding the cursor.
With ARG, delete that many sexps before the cursor.
Negative arg -N means delete N sexps after the cursor."
  (interactive "p")
  (delete-sexp (- (or arg 1))))

(defun delete-sexp (&optional arg)
  "Delete the sexp (balanced expression) following the cursor.
With ARG, delete that many sexps after the cursor.
Negative arg -N means delete N sexps before the cursor."
  (interactive "p")
  (let ((opoint (point)))
    (forward-sexp (or arg 1))
    (delete-region opoint (point))))

;;; not-replace-regexp.el ends here

reply via email to

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