From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: use Elisp to improve your Elisp - some code issues Date: Sat, 01 Aug 2015 15:14:41 +0200 Message-ID: <87614z5ewu.fsf@nl106-137-147.student.uu.se> References: <683844af-0117-4f6e-a64e-aeabc12946e7@googlegroups.com> <87lhdvd4gw.fsf@nl106-137-147.student.uu.se> <87pp37v111.fsf@kuiper.lan.informatimago.com> <87bner5g3v.fsf@nl106-137-147.student.uu.se> <87fv4317ma.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1438435003 28742 80.91.229.3 (1 Aug 2015 13:16:43 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 1 Aug 2015 13:16:43 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Aug 01 15:16:34 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZLWeQ-0000bY-Cn for geh-help-gnu-emacs@m.gmane.org; Sat, 01 Aug 2015 15:16:34 +0200 Original-Received: from localhost ([::1]:53809 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZLWeP-0001Wu-LA for geh-help-gnu-emacs@m.gmane.org; Sat, 01 Aug 2015 09:16:33 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58367) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZLWeF-0001Wn-T1 for help-gnu-emacs@gnu.org; Sat, 01 Aug 2015 09:16:24 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZLWeC-000720-Kt for help-gnu-emacs@gnu.org; Sat, 01 Aug 2015 09:16:23 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:37956) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZLWeC-00071N-E4 for help-gnu-emacs@gnu.org; Sat, 01 Aug 2015 09:16:20 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZLWeB-0000UB-01 for help-gnu-emacs@gnu.org; Sat, 01 Aug 2015 15:16:19 +0200 Original-Received: from nl106-137-228.student.uu.se ([130.243.137.228]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 01 Aug 2015 15:16:18 +0200 Original-Received: from embe8573 by nl106-137-228.student.uu.se with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 01 Aug 2015 15:16:18 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Original-Lines: 82 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: nl106-137-228.student.uu.se Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:ocoEpgLQMaUtThfhkBYXZ+enRAc= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106187 Archived-At: 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: http://user.it.uu.se/~embe8573/conf/emacs-init/search-regexp-in-files.el (require 'cl) (defalias 'cl-set-xor 'cl-set-exclusive-or) (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 find-file-goto-line (file line) (interactive) (find-file file) (goto-char (point-min)) (forward-line (1- line)) ) (defun regexp-hits-find-hit () (interactive) (beginning-of-line) (let ((file (thing-at-point 'filename nil))) ; NO-PROPERTIES (end-of-line) (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 (erase-buffer) (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 (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