

Efficiently Browsing Text or Code (esp. with Emacs) - kirubakaran
http://www.kirubakaran.com/articles/efficiently-browsing-text-or-code.html

======
tptacek
Highly, highly recommend hilock-mode for browsing code; hilock does ad-hoc
syntax highlighting, so, as you read code and notice patterns, you write
regexes for them and let Emacs spot them for you.

This turned out to be so useful for code reviews (we do a lot of those) that I
"ported" it to a web app that lets us set hilocks that follow us from buffer
to buffer (along with xrefs, class/method/fn definition sites, bookmarks, and
a lot of other junk).

~~~
kaeluka
wow, that's amazing! Thanks for that :)

------
wladimir
It's 2012 and we're still navigating source code, which is effectively a
nested graph structure, using grep and simple keyword queries.

No (reverse) call trees, no high-level views, no help on how data propagates
through the program. We could do so much better, especially in static
languages. Sometimes it seems like reverse engineering tools that work without
source code (such as IDA) do a better job of cross-referencing than tools that
have the actual source code.

Is there any good/usable point and click code browser / code comprehension
tool these days? Or is the state of the art of 1998 still that of today?

~~~
tptacek
It is very hard to do this for C/C++, because C/C++ code is customized at
compile time by the build environment and usually can't be parsed and analyzed
without that customization.

The tools therefore need a fully working build environment, and the ability to
temporarily override aspects of that build environment.

~~~
turnersr
People at Mozilla have created a lot of neat tools for working with large
C/C++ code bases.

Relavent Links: <https://developer.mozilla.org/en/Dehydra>
<https://developer.mozilla.org/en/Treehydra> <http://dxr.lanedo.com/>
[http://corp.galois.com/blog/2010/6/4/tech-talk-large-
scale-s...](http://corp.galois.com/blog/2010/6/4/tech-talk-large-scale-static-
analysis-at-mozilla.html)

~~~
tptacek
Dehydra and Treehydra are GCC extensions; they have to be integrated
(painfully) into your build environment.

DXR is a source code search engine. It appears to be cscope with a database.
That's not nothing, but it's not exactly taking us out of the opaque blobs of
text and into semantics.

~~~
turnersr
I agree that it is pain to integrate. Once integrated, however, these tools
offer a start on the road towards semantics. Here is an overview of some of
the techniques used in Treehydra:
<https://wiki.mozilla.org/Abstract_Interpretation>

DXR is currently a pain to get up and running. There's been talk on the
mailing list on streamlining the install process but it would still require a
lot intervention depending the compilation process.
[https://groups.google.com/forum/#!topic/mozilla.dev.static-a...](https://groups.google.com/forum/#!topic/mozilla.dev.static-
analysis/q0TunpwhnOU)

~~~
tptacek
Does DXR do anything particularly interesting, as far as source code search
engines go?

------
walexander
I'm an emacs coder myself, so I'm wondering.. why no mention of cscope?

~~~
jbp
"cscope + ascope" is the best thing ever I found for groking the code(in
emacs). It is great for getting your way around huge code base. I find myself
using "ascope-find-this-symbol" all the time, which gives me all
occurrences/usages of that symbol.

~~~
signa11
yeah without cscope it would be impossible to follow huge code bases. though i
am not sure how good is cscope at generating symbol database for non-c
languages.

ideally, it would be really nice if cscope/xcscope provided some means of
creating (and saving) bunch of call-stacks, which you can navigate in
forwards/reverse direction (not sure if i am making much sense here...)

~~~
codemac
You can "pop" as you navigate around.

Also, I've had a lot of luck with pycscope.py for python code + cscope tools.

GNU Global needs some love though. It'd be awesome to see it handle more
stuff.

------
KC8ZKF
(find-grep-dired DIR REGEXP)

Find files in DIR containing a regexp REGEXP and start Dired on output.

~~~
kirubakaran
That is definitely useful. However the scenarios I explored needed two or more
keywords occurring on different lines in a file. This is so that I can
significantly reduce the number of files that needed a closer look. Is that
easily done with find-grep-dired?

------
Peaker
Then you want to search all uses of the Linux kernel's struct scsi_device
field "vendor". Unfortunately, there are hundreds of structs with a "vendor"
field.

cscope, etags, etc can't really handle this task. Eclipse and Visual Studio
can.

I am really disappointed that emacs and vim who spout productivity over
learning curve allow important productivity tasks such as this to remain more
than a decade behind the state of the art.

~~~
counterpunt
if it's really that important to you, quit bitchin and start coding!

~~~
Peaker
I am coding, but it's actually a more ambitious project to get rid of text in
the toolchain altogether, making searches such as this much simpler cross-
reference searches.

------
Erwin
There's an affordable (USD 299, free 7 day trial) commercial tool for
C/C++/Javaallowing structured, context-sensitive code browsing and refactoring
that actually integrates into Emacs:
<http://www.xref.sk/xrefactory/emacs.html>

In fact it seems the pure C version is free now; it claims full indexing of 3
million lines of Linux source code in 15 minutes.

I used to keep track of it many years back back while it was being developed,
but then C++ starting making up 1% of my projects rather than 99% (nowadays I
use the JetBrains PyCharm for those 99% which does a reasonable effort).

It seems the tool hasn't seen updates in a while (forget about C++11 support),
but I remember it working much better than just tags, cscope last time I tried
it many years ago.

------
aristus
There are many roads to Damascus.

(global-set-key "\M-/" 'tags-search) (global-set-key "\M-'" 'tags-apropos)

------
abc_lisper
Um. I thought I was inefficient at browsing Emacs and this sucks more. Here's
what I do.

1\. Generate a file index, every now and then(read cron).

    
    
      > find . > fileList
    

2\. Write a script(can also do this in emacs) in bash that takes two arguments
viz., filename and search string. The script does a two level grep(regex),
first on filenames and then in each of those files for the search string

3\. If the previous routine was written in bash written a elisp function that
calls the bash script.

That's mostly it.

I could use cscope but it sucks donkey balls with java. When I ask for
definition it finds a declaration.

------
decklin
No mention of M-x grep?

~~~
zacharypinter
I find M-x rgrep extremely useful as well.

------
dw5ight
awesome article, thanks K!

~~~
kirubakaran
Thank you :-)

------
res0nat0r
ack and ack.vim = bliss.

~~~
heretohelp
Indexing is better, but ack is still nice to have for ad-hoc searches.

Ack plugins for Emacs:

<http://www.emacswiki.org/emacs/Ack>

<http://rooijan.za.net/code/emacs-lisp/ack-el>

<https://github.com/jhelwig/ack-and-a-half>

<http://www.emacswiki.org/emacs/FullAck>

