[Top][All Lists]

[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 15:14:41 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Now the code is even better. Now you can go to a hit
with one keystroke - RET - and iterate with the
favorites "i" and "k".

;; This file:

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

(defun files-as-list (file-regexp)
      (format "ls %s" file-regexp) nil t) ; no INFILE, temp BUFFER
     (buffer-substring (point-min) (point-max)) )))

(defun find-file-goto-line (file line)
  (find-file file)
  (goto-char (point-min))
  (forward-line (1- line)) )

(defun regexp-hits-find-hit ()
  (let ((file (thing-at-point 'filename nil))) ; NO-PROPERTIES
    (search-backward "(")
    (forward-char 1)
    (let ((line (thing-at-point 'number nil)))
      (find-file-goto-line file line) )))

(defun set-regexp-hits-keys ()
  (local-set-key "\r" 'regexp-hits-find-hit)
  (local-set-key "i"  'previous-line)
  (local-set-key "k"  'forward-line) )

(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
        (set-regexp-hits-keys) )
      (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
            (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)

(provide 'search-regexp-in-files)

underground experts united

reply via email to

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