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

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

Re: use Elisp to improve your Elisp - some code issues


From: Navy Cheng
Subject: Re: use Elisp to improve your Elisp - some code issues
Date: Fri, 31 Jul 2015 20:11:17 +0800
User-agent: Mutt/1.5.23 (2014-03-12)

This is my code after your discuss. 

(defun search-regexp-in-files (wildcards regexp)
    "Search for regular expression REGEXP in files whose name matches 
WILDCARDS.                                                       
The results will be print to buffer *PrivateResults*."
    (let ((result (or (get-buffer "*PrivateResults*")       ; There will be 
only one result buffer  
                      (generate-new-buffer "*PrivateResults*")))
          (file-list (file-expand-wildcards wildcards))     ; All matched files
          (buffer-to-kill nil))                            ; Record the buffer 
to be killed
      
      (if (eq file-list nil)
          (with-current-buffer result
            (insert "Wrong file name!"))
        (dolist (file-path file-list)
          (setq file (file-name-nondirectory file-path))    ; Get the buffer 
name
          (let ((buffer (or (progn                          ; File buffer is 
exist
                              (setq buffer-to-kill nil)
                              (get-buffer file))
                            (progn                          ; Create buffer for 
a file
                              (setq buffer-to-kill file)
                              (find-file-noselect file)))))
            (with-current-buffer buffer
              (goto-char (point-min))
              (while (re-search-forward regexp nil t)       ; no BOUND, NOERROR
                (let ((hit-line (downcase (what-line))))
                  (with-current-buffer result
                    (insert (format "file: %s (%s)\n" buffer hit-line))))))
            (with-current-buffer result
              (if (eq (buffer-size result) 0)
                  (insert "No hits!"))))
          (if buffer-to-kill
              (kill-buffer buffer-to-kill))))               ; Kill the 
buffer-to-kill
      
      (switch-to-buffer result)))

On Fri, Jul 31, 2015 at 04:30:20AM +0200, Marcin Borkowski wrote:
> 
> On 2015-07-31, at 02:22, Emanuel Berg <embe8573@student.uu.se> wrote:
> 
> > I just wrote some Elisp which can be used on a set of
> > files to identify for example the construct
> >
> >     (if a a b)
> >
> > if you want to replace those for
> >




reply via email to

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