all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* use Elisp to improve your Elisp - some code issues
@ 2015-07-31  0:22 Emanuel Berg
  2015-07-31  2:30 ` Marcin Borkowski
  2015-08-03  7:56 ` Tassilo Horn
  0 siblings, 2 replies; 30+ messages in thread
From: Emanuel Berg @ 2015-07-31  0:22 UTC (permalink / raw)
  To: help-gnu-emacs

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

    (or a b)

See the comments for the issues!

Issue one is how to best create a temporary buffer to
display the results.

Issue two is to not kill buffers that were already
open at invocation - I can solve that by checking if
there is such a buffer, but I suspect there is
a better way to do these kind of things all in the
background, rather than the `find-file' and then
conditionally `kill-buffer' combo.

Third (minor) issue is the annoying message that
`downcase' does. Isn't there a (shut-up (do-stuff))?

Other comments also appreciated, as always.

;; This file: http://user.it.uu.se/~embe8573/conf/emacs-init/search-regexp-in-files.el

(defun files-as-list (file-regexp)
  (split-string
   (with-temp-buffer
     (call-process-shell-command
      (format "ls %s" file-regexp) nil t) ; no INFILE, temp BUFFER
     (buffer-substring (point-min) (point-max)) )))

(defun search-regexp-in-files (file-regexp regexp)
  (let ((paths       (files-as-list file-regexp))
        (regexp-hits "regexp-hits") ; unlikely, but if there is another such buffer
        (hits        nil))
    (with-current-buffer regexp-hits (erase-buffer)) ; then we can't have this
    (dolist (p paths)
      (let ((buffer (find-file p)))
        (with-current-buffer buffer
          (goto-char (point-min))
          (while (re-search-forward regexp nil t) ; no BOUND, NOERROR
            (setq hits t)
            (let ((hit-line (downcase (what-line))))
              (with-current-buffer regexp-hits
                (insert (format "file: %s (%s)\n" p hit-line)))))
          (kill-buffer buffer) ))) ; what if the buffer was open already?
                                   ; we only want to kill buffers that we opened
    (if hits (switch-to-buffer regexp-hits)
      (message "No hits!") )))

;; use this to test
(when nil

  ;; find "kill" - should be some hits even for pacifists
  (search-regexp-in-files "~/.emacs.d/emacs-init/*.el" "kill")

  ;; find the construct (if a a b) if you want to replace it with (or a b)
  ;; if it works, when applied to this file, it should find the example above!
  (search-regexp-in-files (buffer-file-name)
   "([[:space:]\n]*if[[:space:]\n]+\\(.*\\)[[:space:]\n]+\\1[[:space:]\n]+\\(.*\\))"
   )
  )

(provide 'search-regexp-in-files)

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2015-08-06  0:59 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.7671.1438302261.904.help-gnu-emacs@gnu.org>
2015-07-31  2:39 ` use Elisp to improve your Elisp - some code issues Pascal J. Bourguignon
2015-08-01  4:09   ` Emanuel Berg
     [not found]   ` <mailman.7712.1438402251.904.help-gnu-emacs@gnu.org>
2015-08-01  8:54     ` Pascal J. Bourguignon
2015-08-01 12:41       ` Emanuel Berg
     [not found]       ` <mailman.7727.1438432975.904.help-gnu-emacs@gnu.org>
2015-08-01 15:59         ` Pascal J. Bourguignon
2015-08-02  0:06           ` Emanuel Berg
2015-08-03  1:23             ` Ian Zimmerman
     [not found]           ` <mailman.7751.1438474104.904.help-gnu-emacs@gnu.org>
2015-08-02  0:44             ` Pascal J. Bourguignon
2015-08-02  1:29               ` Emanuel Berg
2015-08-02 15:36                 ` Robert Thorpe
2015-08-02 16:44                   ` Pascal J. Bourguignon
2015-08-05 23:40                   ` Emanuel Berg
2015-08-06  0:59                     ` John Mastro
     [not found]                 ` <mailman.7758.1438529790.904.help-gnu-emacs@gnu.org>
2015-08-02 16:25                   ` Rusi
2015-08-01 16:13       ` Michael Heerdegen
2015-07-31 20:24 ` Sam Halliday
2015-08-01  4:20   ` Emanuel Berg
2015-08-01  6:26     ` Marcin Borkowski
     [not found]     ` <mailman.7714.1438410426.904.help-gnu-emacs@gnu.org>
2015-08-01  8:57       ` Pascal J. Bourguignon
2015-08-01 12:48         ` Emanuel Berg
2015-08-01 13:05           ` Marcin Borkowski
2015-08-01 13:14             ` Emanuel Berg
2015-08-01 13:21             ` Emanuel Berg
     [not found]   ` <mailman.7713.1438402953.904.help-gnu-emacs@gnu.org>
2015-08-02 10:42     ` Sam Halliday
2015-08-05 23:21       ` Emanuel Berg
2015-07-31  0:22 Emanuel Berg
2015-07-31  2:30 ` Marcin Borkowski
2015-07-31  7:42   ` Stefan Monnier
2015-07-31 12:11   ` Navy Cheng
2015-08-03  7:56 ` Tassilo Horn

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.