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

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

jedit.el (was: Interative batch query-replace question)


From: Joel J. Adamson
Subject: jedit.el (was: Interative batch query-replace question)
Date: Mon, 03 Dec 2007 10:32:36 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

Some custom editing functions: suggestions welcome.

;;; jedit.el --- Joel's Custom Editing Functions
;; Author: Joel J. Adamson
;; Maintainer: Joel J. Adamson
;; version 0.1
;; keywords:  tex matching
;; Copyright (C) 2007 Joel J. Adamson

;; This program 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 3 of
;; the License, or (at your option) any later version.

;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.  See the GNU General Public License for more details.

;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301 USA
;;; Code:

;; strip unrecognized regexes from latex files for latex2rtf
(defun jedit-strip-regex (alist)
  "Takes a list of regex-replacement string pairs;
processes entire buffer."
  (interactive "sList: ")
  ;; for each cell in alist, define regex and replacement text
  (dolist (regex-cell alist)
    (let ((regex (car regex-cell))
          (replacement (cadr regex-cell)))
      ;; go to beginning of buffer
      (goto-char (point-min))
      ;; when you find the search string, replace it with replacement
      ;; text
      (while (re-search-forward regex nil t)
        (replace-match replacement nil nil)))))

;; tex-insert-file
;; takes an input command and inserts the file
;; directly into the current buffer
(defun jedit-tex-insert-file (&optional buffer)
  "Find a .tex file and insert it into a buffer
at \input{`filename'}; `filename' is found automatically from
\input command by searching for a `filename' as a blank text
file and then for `filename'.tex"
  ;; enter a buffer name; defaults to (current-buffer)
  (interactive "bBuffer: ")
  ;; return to the beginning of buffer
  (goto-char (point-min))
  ;; set the symbol filename to be nothing, incase it already exists
  (setq filename "")
  ;; every time you find an \input{`filename'} command, 
  (let ((count 0))
    (while (and (< (point) (point-max))
                (re-search-forward "\\\\input{\\(.*?\\)}" nil t))
      (setq count (1+ count))
      ;; search for the file first as `filename' (a plain text file)
      (let ((txt-filename (match-string-no-properties 1))
            ;; and then as a TeX file with a .tex extension
            (tex-filename (concat
                           (match-string-no-properties 1)
                           ".tex")))
        (when (cond ((file-readable-p txt-filename)
                     (setq filename txt-filename))
                    ((file-readable-p tex-filename)
                     (setq filename tex-filename))
                    ;; if the file can't be found within these
                    ;; parameters, exit with an error
                    (t (error "tex-insert-file Error: no %s or %s found"
                              txt-filename
                              tex-filename)))
          ;; remove the entire matched string
          (replace-match "")
          ;; insert the file contents into the current-buffer
          (insert-file-contents filename)))
      ;; output messages for user
      (cond ((zerop count)
             (message "No replacements"))
            ((= 1 count)
             (message "Made 1 replacement"))
            (t (message "Made %d replacements" count))))))

(provide 'jedit)
(require 'jedit)
;;; jedit.el ends here



reply via email to

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