Personal tools
You are here: Home 原稿・資料 Software Design連載記事「Emacsのトラノマキ」の原稿 Emacsの検索機能を使いこなす(菅原泰樹)
Document Actions

Emacsの検索機能を使いこなす(菅原泰樹)

「Emacsのトラノマキ」 連載第一回 「Emacsの検索機能を使いこなす」


100()
EmacsEmacs使Emacs使使便Emacs便使EmacsEmacs LispEmacs LispEmacsEmacs LispEmacs LispEmacs使使Emacs LispTipsEmacs使EmacsEmacs

() http://www.aoky.net/articles/steve_yegge/tour_de_babel.htm



Emacs

findgrepEmacsEmacs2Emacs

: emacs23emacs22使

install-elisp -- EmacsWikielisp
install-elisp
elisp

install-elisp 
install-elisp.elURL
 http://www.emacswiki.org/emacs/download/install-elisp.el
~/.emacs.d/elisp

.emacs.el

======================
(add-to-list 'load-path "~/.emacs.d/elisp")
(require 'install-elisp)
(setq install-elisp-repository-directory "~/.emacs.d/elisp")
======================



install-elisp 使

使grep-edit

"M-x install-elisp-from-emacswiki""grep-edit.el RET"
======================
Pagename: grep-edit.el
======================

"grep-edit.el: Type C-c C-c to install!"pagename"C-x k"

"C-c C-c"
install-elisp.emacs.el
======================
(require 'grep-edit)
======================



lgreprgrep -- grep
grep
(emacs22)greplgreprgrep


 - lgrep: 
 - rgrep: 


case-fold-search便

使

lgrep使
"M-x lgrep""RET"

======================
Search for (default "XXX"): hoge
======================

*.txt

======================
Search for "hoge" in files (default ch): *.txt
======================

*.txt *.el

======================
Search for "hoge" in files (default ch): *.txt *.el
======================

"RET"

======================
In directory: ~/work/emacs-book/
======================



======================
-*- mode: grep; default-directory: "~/work/emacs-book/" -*-
Grep started at Fri Mar 20 07:59:09

grep -i -nH -e hoge *.txt
emacs-search.txt:31:Search for (default "XXX"): hoge
emacs-search.txt:38:Search for "hoge" in files (default ch): *.txt
emacs-search.txt:45:Search for "hoge" in files (default ch): *.txt *.el

Grep finished (matches found) at Fri Mar 20 07:59:09
======================

"RET"
*grep*"n""p"
"M-g M-n""M-g M-p"*grep*

rgrep使
使lgreprgrepCVS.svn便

lgreprgrep
lgreprgrep


greplv使

lgrepgrep-templatergrepgrep-find-template


<C><R>grep-templategrep-find-templatedocstring

======================
(setq grep-host-defaults-alist nil) ;; 
(setq grep-template "lgrep <C> -n <R> <F> <N>")
(setq grep-find-template "find . <X> -type f <F> -print0 | xargs -0 -e lgrep <C> -n <R> <N>")
======================

Windows
======================
;; shell-quote-argument
(defvar quote-argument-for-windows-p t "enables `shell-quote-argument' workaround for windows.")
(defadvice shell-quote-argument (around shell-quote-argument-for-win activate)
 "workaround for windows."
 (if quote-argument-for-windows-p
 (let ((argument (ad-get-arg 0)))
 (setq argument (replace-regexp-in-string "\\\\" "\\\\" argument nil t))
 (setq argument (replace-regexp-in-string "'" "'\\''" argument nil t))
 (setq ad-return-value (concat "'" argument "'")))
 ad-do-it))

;; lgrep  Shift_JIS 使
(setq grep-host-defaults-alist nil) ;; 
(setq grep-template "lgrep -Ks -Os <C> -n <R> <F> <N>")
(setq grep-find-template "find . <X> -type f <F> -print0 | xargs -0 -e lgrep -Ks -Os <C> -n <R> <N>")
======================

rgrep
grep-find-ignored-directories

======================
"SCCS" "RCS" "CVS" "MCVS" ".svn" ".git" ".hg" 
".bzr" "_MTN" "_darcs" "{arch}"
======================

.hoge

======================
(require 'grep)
(add-to-list 'grep-find-ignored-directories ".hoge")
======================

grep-edit -- grep 
grep grep-edit使

grep-edit

install-elisp使

======================
M-x install-elisp-from-emacswiki
Pagename: grep-edit.el
======================

.emacs.el

======================
(require 'grep-edit)
======================

grep-edit
grep"M-x lgrep"grep

======================
lgrep -i -n \\ sample.txt /dev/null
sample.txt:1:
sample.txt:2:
======================

*grep*query-replace使

======================
lgrep -i -n \\ sample.txt /dev/null
sample.txt:1:FooFooFoo
sample.txt:2:FooFoo
======================

"C-c C-e"

"C-x s""!"

find-name-dired -- find
find-name-dired便find-name-dired使finddired

findemacs

"M-x find-name-dired"
======================
Find-name (directory): /usr/share/emacs/23.0.60
======================

rgrep
Find-name (filename wildcard): *.xbm

finddired
======================
 /usr/share/emacs/23.0.60/:
 find . \( -name \*.xbm \) -exec ls -ld \{\} \;
 -rw-r--r-- 1 root root 281 2005-02-13 17:47 etc/images/gnus/gnus-pointer.xbm
 -rw-r--r-- 1 root root 47730 2005-02-13 17:47 etc/images/gnus/gnus.xbm
 -rw-r--r-- 1 root root 578 2005-02-13 17:47 etc/images/gnus/preview.xbm
======================

locate -- locate Emacs
homefind-name-diredlocate使便

locatehomelocate db

find-utilslocate
ubuntudistributionlocatemlocatemlocatefind-utilslocate

URL
 http://www.gnu.org/software/findutils/

aptapt-get install locateubuntuapt-get
 - locate: locate.findutils
 - updatedb: updatedb.findutils
使locatemlocateupdate-alternatives

findutilsupdatedblocate db/etc/cron.daily/locatedistribution

Windows使cygwin使

homelocate db
locate db使locate db
$HOME/locate.dblocate db
PRUNES
======================
#!/bin/sh

PRUNES="
-name RCS
-o -name CVS
-o -name .svn
-o -name .hg
-o -name .git
-o -name classes
-o -path $HOME/tmp
"

updatedb.findutils \
 --findoptions="( -type d ( $PRUNES ) -prune ) -o -type f" \
 --localpaths=$HOME --output=$HOME/locate.db
======================

cron

locate db

URLlocatewrapper script使
 http://dev.ariel-networks.com/Members/matsuyama/use-gnu-locate


home Emacs
Emacs使使locate db.emacs.el

======================
;; locate db
(defvar locate-db-name "locate.db")
;; locate
(setq locate-command "/usr/bin/locate.findutils")

;; locate
(defun locate-make-command-line (search-string dbpath &rest opts)
 (append
 ;; 
 (list locate-command "-i")
 (when (and dbpath (file-exists-p dbpath))
 (list "-d" dbpath))
 opts
 (list search-string)))

;; homelocate
(defun home-locate-make-command-line (search-string &optional &rest opts)
 (apply 'locate-make-command-line
 search-string
 ;; home locate.db 使
 (expand-file-name locate-db-name "~")
 opts))
;; locatehome
(setq locate-make-command-line 'home-locate-make-command-line)
======================

locate 使 M-x locate 

======================
Locate: *.txt
======================

locate

======================
 /:
 Matches for *.txt:

 /home/taiki/work/emacs-book/emacs-search.txt
...
======================

locate db使
locate

locate db
.emacs.el

======================
;;  dir 
(defun find-file-upward (name &optional dir)
 (setq dir (file-name-as-directory (or dir default-directory)))
 (cond
 ((string= dir (directory-file-name dir))
 nil)
 ((file-exists-p name)
 (expand-file-name name dir))
 (t
 (find-file-upward name (expand-file-name ".." dir)))))

;; locate db使locate
(defun plocate-make-command-line (search-string &optional &rest opts)
 (apply 'locate-make-command-line
 search-string
 (find-file-upward locate-db-name)
 opts))

;; locate db使locate
(defun plocate (search-string &optional arg)
 (interactive
 (list
 (locate-prompt-for-search-string)
 current-prefix-arg))
 (let ((locate-make-command-line 'plocate-make-command-line))
 (locate search-string nil arg)))
======================

M-x plocatelocate便

find-file-upwardlocate使javaant便

======================
(defvar ant-command-history nil)
(defun ant ()
 (interactive)
 (let ((build-xml (find-file-upward "build.xml"))
 command)
 (unless build-xml
 (error "cannot found build.xml"))
 (setq command (read-string "Ant: "
 (concat "ant -f " build-xml " -e ")
 'ant-command-history))
 (let ((default-directory (file-name-directory build-xml)))
 (compile command))))
======================

imenu -- 
imenuimenu使imenuetags

使elispCJava"M-x imenu"

======================
Index item: lgrep
======================

C-u C-SPCC-u C-SPC

etags -- TAGS使
imenuetagsimenuetags

etags使TAGS

Exuberant Ctags
TAGSExuberant CtagsEmacsetags

URL
 http://ctags.sourceforge.net

aptWindows

TAGS
TAGSetags使

emacselispTAGSshell

======================
> cd /usr/share/emacs/23.0.60/lisp
> ctags -e *.el
======================


Javafind使

======================
> find . -name "*.java" | xargs ctags -e -L -
======================

TAGS

etags使

etags使
etags使"M-."

======================
Find tag: find-file
======================

"M-."TAGSTAGS

======================
Visit tags table (default TAGS): /usr/share/emacs/23.0.60/lisp/TAGS
======================

"C-u M-."

"M-*"

gtags -- gnu-global
etagsctagsgnu-globalJavaCC++PHP

gnu-global
URL
 http://www.gnu.org/software/global/

aptWindows

gnu-globalgtags.elaptsite-lisp~/.emacs.d/elisp

.emacs.el

======================
(require 'gtags)
(add-hook 'java-mode-hook (lambda () (gtags-mode 1)))
(add-hook 'c-mode-hook (lambda () (gtags-mode 1)))
(add-hook 'c++-mode-hook (lambda () (gtags-mode 1)))
======================

gtags.el"M-x locate-library"gtags.el"Library is file ~/.emacs.d/elisp/gtags.el"

GTAGS
gnu-globaletagsTAGSGTAGS

gtags辿GTAGS
======================
> gtags
======================

GTAGS
======================
> global -u
======================

gtags使
使etags"M-."
======================
Find tag: (default Comparator) 
======================


======================
Comparator 723 .../com/sun/org/apache/xpath/internal/objects/XNodeSet.java abstract class Comparator
Comparator 80 .../java/util/Comparator.java public interface Comparator<T> {
======================

gtagsetags使"M-x gtags-find-rtag"使使
======================
Comparator 109 .../java/text/Collator.java implements java.util.Comparator<Object>, Cloneable
Comparator 3307 .../java/util/Collections.java implements Comparator<Comparable<Object>>, Serializable {
...
======================

etags"M-*"

anything
anything使

anything便anything

anything
install-elisp使anything.elanything-config.elanything-match-plugin.el
======================
M-x install-elisp-from-emacswiki
Pagename: anything.el
M-x install-elisp-from-emacswiki
Pagename: anything-config.el
M-x install-elisp-from-emacswiki
Pagename: anything-match-plugin.el
======================

anything-etagsanything-gtags
etagsgtagsanything使anything-etagsanything-gtags

install-elisp使
======================
M-x install-elisp-from-emacswiki
Pagename: anything-etags.el
M-x install-elisp-from-emacswiki
Pagename: anything-gtags.el
======================

anything


.emacs.el
======================
(require 'anything)
(require 'anything-config)
(require 'anything-etags)
(require 'anything-gtags)
======================

anything使anythinganything-sourcesbufferimenuetagsgtags.emacs.el

======================
(setq anything-sources
 '(anything-c-source-buffers
 anything-c-source-imenu
 anything-c-source-etags-select
 anything-c-source-gtags-select
 ))
======================

anythingswitch-to-buffer"C-x b"

======================
(global-set-key (kbd "C-x b") 'anything)
======================

anything使
"M-x anything"
scratch"C-n""C-p""RET"
AND(anything-match-plugin)

TAGSGTAGSM-x anything

*anything*"RET"

Emacsanything

anythinglocate使
anythinglocate使

homeanythinglocate.emacs.el
======================
(defvar anything-c-source-home-locate
 '((name . "Home Locate")
 (candidates . (lambda ()
 (apply 'start-process "anything-home-locate-process" nil
 (home-locate-make-command-line anything-pattern "-r"))))
 (type . file)
 (requires-pattern . 3)
 (delayed)))
======================

anything
======================
(defvar anything-c-source-plocate
 '((name . "Project Locate")
 (candidates
 . (lambda ()
 (let ((default-directory 
(with-current-buffer anything-current-buffer
 default-directory)))
 (apply 'start-process "anything-plocate-process" nil
 (plocate-make-command-line anything-pattern "-r")))))
 (type . file)
 (requires-pattern . 3)
 (delayed)))
======================

anything-sourcessourcehome-locateplocateprojecthome-locate
======================
(setq anything-sources
 '(anything-c-source-buffers
 ...
 anything-c-source-plocate
 anything-c-source-home-locate
 ))
======================

anythinglocate使"M-x anything"

locateanything使便便
 - 
 - (anything-match-plugin)

anythingrubikitchcandidates-file使
 http://d.hatena.ne.jp/rubikitch/20090211/1234349678


EmacsEmacs使namazuhyperestraieremacs使EmacsWikiEmacselisp

URL
 - EmacsWiki: http://www.emacswiki.org/
 - Emacs: http://www.bookshelf.jp/
 - Anything: http://www.emacswiki.org/cgi-bin/wiki/Anything

Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.