Re: how to use etags and xemacs

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Fri Dec 27 2002 - 13:13:27 EST

  • Next message: Hironori Ito: "Re: how to use etags and xemacs"
    Hi Hiro, 
    
    Hironori Ito <hito@rcf.rhic.bnl.gov> wrote concerning
      how to use etags and xemacs [Mon, 23 Dec 2002 15:50:14 -0500] 
    ----------------------------------------------------------------------
    > Hello.  I have a question for xemacs (or emacs) expart for use of tags. 
    > (I think.)  For example, if I want to replace every "Br" character with 
    > "Brahms" in every file under brat directory, how can I do that easily? 
    > I heard that I can use "etags" or something, but I do not know how to 
    > use it.  If someone know how to do it, please let me know.  (Don't worry 
    > I am not changing Br to Brahms in brat =)  )
    
    It's quite easy, so don't be scared by the length of this mail - I'm
    simply pinning things out a bit. 
    
    First you need to make a TAGS file, which is a sort of index over the
    files you're interested in.  Suppose you want to index all files in a
    directory, simply do
    
      > etags * 
    
    However, that'll include files like README, INSTALL, Makefile.am, and
    so on.  So maybe you'd want to make the index over a more restricted
    set of file - well, use the shell glob patterns to do that: 
    
      > etags *.h *.cxx Makefile.am 
    
    (It's not a bad idea to index the Makefile.am too, as you'll probably
    want to change filenames too). 
    
    If your source tree isn't flat (in one single directory) but is nested
    (a tree of directories), you'd do better using `find' to help you out
    with the actual file names:
    
      > find . -name "*.h" -or -name "*.cxx" -or -name "Makefile.am" 
    
    That'll give you a list of source files + Makefile.am's.  However,
    suppose your source uses ROOT - then there's a distinct possibility
    that you will have `rootcint' generated files like `FooCint.cxx' and
    `FooCint.h'.  We really don't want to index those files, as they are
    automatically generated, and it's a bloody waste of time doing
    anything on those files.  Hence, a better search will be 
    
      > find . -not -name "*Cint.*" -and \( -name "*.cxx" -or -name "*.h" \
          -or -name "Makefile.am" \)
    
    (Note that the '(' and ')' must be escaped, or the shell will expand
    them - that is, try to evaluate the expression with the parentheses). 
    
    That'll give you a list of the relevant files.  Now, we need to put
    that list to `etags' to make it index those files.  That's easy too.
    We use the utility `xargs' to pass multiple arguments to `etags': 
    
      > find . -not -name "*Cint.*" -and \( -name "*.cxx" -or -name "*.h" \
          -or -name "Makefile.am" \) | xargs etags 
    
    That's it.  You should have a file called `TAGS' in the current
    directory.  `cat' it if you like - it's not clear text.  
    
    The next thing is to use the `TAGS' file in (X)Emacs (it's the same of
    both). 
    
    Start Emacs, and figure out what you want to do.  Suppose we have
    loads of classes, named something like `My...', and we've decided that
    we should really rename them all to some thing like `Foo...' as we're
    about to ready make a release of our `foo' package.  Hence, we'd like
    to substitute all occurrences of `My...' with `Foo...'.  However, we
    know that the `...' part should start with a capital letter or a
    digit, so we'd actually like to substitute the regular expression
    `My[A-Z0-9]...' with `Foo\1...' (where \1 refers to the matched
    capital letter or digit).  That's easily done using the `TAGS' file.
    
    In Emacs, type 
    
       M-x tags-query-replace 
    
    (that's the `Alt'-key down and an `x', followed  by the literal
    `tags-query-replace' - actually, you need only write `tags-q[TAB]' and
    Emacs will complete it  for you).  
    
    Emacs will then prompt you for a regular expression, so let's enter
    that: 
    
      My\([A-Z0-9]\)[ENTER]
      
    The `\(...\)' construct saves away the match in `\1'.  The `[ENTER]'
    makes Emacs prompt us for a replacement string, so let's enter that,
    using the saved-away match on a capital letter or digit: 
    
      Foo\1[ENTER]
    
    Here we're using the `\1' to get the matched part in `\(...\)'.   The
    `[ENTER]' makes Emacs prompt us for a `TAGS' file.  Assuming you're
    standing in the directory where you created the `TAGS' file using
    `etags', you can almost certainly just press `[ENTER]'.  If you're
    not, or the for some reason the default value suggested by Emacs is
    wrong, you need to specify it (use the `[TAB]'-completion!), and press
    `[ENTER]'.  
    
    Having  specified the regular expression, the replacement string, and
    the `TAGS' file, Emacs will start asking you whether you want to
    replace a specific occurrence of the regular expression.  
    
    To accept a replacement, type `y'; to reject a replacement, type `n',
    to cancel the replacements all together, type `q'; to see a
    replacement with out actually performing it, type `,'; to replace all
    occurrences of the regular expression in the current buffer, type `!';
    and finally, to get some more help, type `?'.  Note, that if you type
    `!' it will only do the replacements in the current buffer - not in
    all the files you've indexed.  Hence, you need to type `!' for each
    file you've index if you want to replace all occurrences of the
    regular expression.  
    
    Having done what ever replacements you like, you have most probably
    modified several buffers.  To save them all, simply type 
    
     C-x s 
    
    (that's the `Control'-key down and an `x', and the literal `s').
    Emacs will prompt you for each modified buffer. Pressing `y' saves the
    buffer, `n' skips it, `q' stops, and `!' saves all remaining buffers. 
    
    You can of course do a lot more with the `TAGS' file, like searching
    for a regular expression: 
    
      M-x tags-search <regular-expression> [ENTER]
    
    (`M-,' repeats the search), and ladida.   
    
    
    Anyway, I strongly suggest you refer to the following documentation
    available via the info system (`C-h i m <topic>' in Emacs): 
    
     find  
        (`C-h i m find [ENTER]')
    
     xargs 
        (`C-h i m find [ENTER] m Actions [ENTER] m Run [ENTER] m Multi [ENTER]')
    
     etags
        (`C-h i m Emacs [ENTER] m Tags [ENTER] m Create [ENTER]')
    
     tags-query-replace 
     tags-search
        (`C-h i m Emacs [ENTER] m Tags Search [ENTER]') 
    
     Emacs Regular Expression 
        (`C-h i m Emacs [ENTER] m Regexps [ENTER]') 
    
    There's probably also man(1) pages for `find', `xargs' and `etags' on
    your system.
    
    Note, that there's a similar thing for <shudder>Vi</shudder>, but I
    don't know it, so you have to Read The F...... Manual :-) 
    
    He, it took a pretty long mail to say (politely) RTFM :-)
    
    A nice thing about `Autotools', is that it recognises `TAGS' files,
    and can even recreate them for you as things changes in the source
    code.  Also, they are removed when you do `make maintainer-clean' (or
    is it `make dist-clean'?).   
    
    <rant> 
    As you can see, there's quite a wealth of utilities available on Un*x
    that can really help the programmer a lot.  Sometimes people complain,
    saying: `Why isn't there a Developer Studio on Un*x, like on MacOS,
    Windoze or Solaris?' - Well, the answer is - there is!  In fact
    there's more than just one.  However, in the true Un*x style of
    things, these studios are made up of small components that does each
    job well, and nothing more.  However, when you start combining these
    small tools, the result is so much more powerful than the mere sum of
    the individual features (witness the use of `find', `xargs' and
    `etags' above).
    
    Why is this good compared to large monolithic `Developer Studios?'
    Well, each tool does it's thing in it's own right - and it does it
    well.  Also, if a particular programmer doesn't like a particular
    tool, she can easily substitute another tool - say Emacs with
    <shudder>Vi</shudder> - all in all, the programmer has some _freedom_
    in how to work best (remember `Free as in speech, not beer').  With
    big monolithic suits, you simply do not have that freedom of choice -
    some system designer is some dark corner of a cellar in Silicon Vally
    or Redmond made the choice for you, and if you don't like it - touch
    luck buster.
    
    The studios that _does_ exist on Un*x (in particular GNU/Linux) does
    the Right Thing(tm) and provide GUI _wrappers_ around the smaller
    tools. 
    </rant>
    
    I hope all this helps you do the job nice and easy (being
    what-ever-FUD-term-M$-may-use), and with the basic freedom of choice
    that comes from using a proper OS.  
    
    <aside>
    There was an posting somewhere recently, that explain why DOS, and by
    extension Windows 3.x, and 9x, wasn't really operating systems, but a
    program launcher, much like the `loader-tape' on an old Gier machine.
    The posting also recognised that WinNT, and by extension Windows 2000
    and XP, _are_ operating systems, but that the basic design was all
    messed up when they added the GUI. 
    </aside> 
    
    Yours, 
    
     ___  |  Christian Holm Christensen 
      |_| |	 -------------------------------------------------------------
        | |	 Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
         _|	          DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
        _|	          Denmark                    Office: (+45) 353  25 305
     ____|	 Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
     | |
    


    This archive was generated by hypermail 2.1.5 : Fri Dec 27 2002 - 13:15:05 EST