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: Emanuel Berg
Subject: Re: use Elisp to improve your Elisp - some code issues
Date: Sat, 01 Aug 2015 06:09:05 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

"Pascal J. Bourguignon" <pjb@informatimago.com>
writes:

> It's not so much a temporary buffer than a buffer
> that is not backed by a file, that you want.

Indeed, good clarification.

> You want your buffer to be visible by the user.
> The convention for this type of buffer is to name it
> with stars around "*Results*".

OK, done.

> If you want such a buffer but not visible to the
> user, add a prefix space:
>
> " *PrivateResults*"
>
> You can see the current "invisible" buffer with C-x
> b SPC TAB or all the buffers with (buffer-list)

Interesting, but of course the buffer should be
accessible the ordinary way as you might fix one
occurence, get back to the hit list, examine the next
hit, and so on.

>> 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.
>
> I don't think there's another way.
>
> In my opinion, it's not too important a feature; in
> my own with-file macro (used by with-files),
> I didn't check for pre-existing buffers.

Here it is very important! Because say that you have
a couple of Elisp files open. Then it strikes you you
can do (or a b) instead of (if a a b) while working on
one of them. So you think, did I do that in any of my
other Elisp files? You apply the tool - boom, all
buffers killed! See the code (last) for how the
situation can be solved (?).

> I never noted any message from downcase; what do
> you get?

Messages :)

In the form of: "Line X". But it wasn't `downcase' but
`what-line'!

> In anycase, it is a basic precept to never mix I/O
> with computing in a single function.

Indeed, and I want data, not I/O. `line-number-at-pos'
did it as mentioned by anther poster.

The code:

;; 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)) )))

(require 'cl)
(defalias 'cl-set-xor 'cl-set-exclusive-or)

(defun search-regexp-in-files (file-regexp regexp)
  (let ((paths       (files-as-list file-regexp))
        (regexp-hits "*regexp-hits*")
        (hits        nil) )
    (get-buffer-create regexp-hits)
    (let ((buffers     (buffer-list))) ; get list to see if we opened the file -
      (with-current-buffer regexp-hits (erase-buffer))
      (dolist (p paths)
        (let ((buffer     (find-file p)) ; here -
              (kill-later (cl-set-xor buffers (buffer-list))) ) ; by comparing!
          (with-current-buffer buffer
            (goto-char (point-min))
            (while (re-search-forward regexp nil t) ; no BOUND, NOERROR
              (setq hits t)
              (let ((hit-line (line-number-at-pos)))
                (with-current-buffer regexp-hits
                  (insert (format "%s (%s)\n" p hit-line)))))
            (when kill-later (kill-buffer buffer) ))))
      (if hits
          (progn
            (switch-to-buffer regexp-hits)
            (set-buffer-modified-p nil)
            (goto-char (point-min)) )
        (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




reply via email to

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