5

I'd like to browse source code in emacs by searching for 'word' or 'tag' hits in a way somewhat similar to etags but:

  • clicking on words to search for matches across a code base (say a directory or project)
  • each click leads to a search hits listing view that itself has words that can be clicked to search again

To be more specific: Here's a program that works similarly to what I would like to use in emacs -- it's a small web application in Python:

https://github.com/mseaborn/sbrowse

There are basically two views, which might perhaps map onto emacs something like this:

web view -> emacs 'view'
------------------------
source code -> a regular editing buffer with appropriate mode (say python-mode)
hits listing -> something like compilation-mode

In the source code view, one can click on a 'word' to jump to the hits listing.

The hits listing looks like the inline block below (here I've shown what you'd see on searching for 'spam'

path/to/file.py:
104:    ham = spam()
123:    # Some more spam

path/to/other/file.py
98:     ham *= spam ** 2

From here one might:

  • Click on 'ham' or 'Some' and get a similar listing for that word
  • Click on a line number ('98:') to jump to a source code view that shows the hit in context
  • Click on a file path is a link to jump to the top of the corresponding source code view

Usually the search is done by simple grepping for words based on regexp word boundaries or similar. One other feature of sbrowse that is nice, though, is that it has pluggable search implementations. For example, I would like to be able to implement an elisp function or unix command to search using https://github.com/google/codesearch, so that clicks in both views perform the search using that program.

Is there anything available like this in the emacs universe?

  • You can install a development snapshot of Emacs 25 and try M-x xref-find-references. – Dmitry Jan 17 '16 at 14:39
  • @Dmitry that sounds very promising, I will certainly try it out! It would be amazing if somebody finds the time to provide an implemention that uses github.com/google/codesearch (MIT license, provides full text indexed regexp search) – Croad Langshan Jan 17 '16 at 15:20
1

Perhaps you could try any of the AG interface variants. AG with PCRE support regex searches over files.

This is one example of doing this in Emacs : https://github.com/syohex/emacs-helm-ag

see the custom option:

(setq helm-ag-use-emacs-lisp-regexp t)

  • Seems there's no 'click to search' but I guess that's just a matter of binding mouse events... – Croad Langshan May 22 '15 at 0:45
1

You can do something like the below. It is just an example you might want to customize it according to your need.

(require 'thingatpt)

(defun my-search-at-point (event)
  (interactive "e")
  (let ((position (event-start event)))
    (save-selected-window
      (select-window (posn-window position))
      (save-excursion
        (goto-char (posn-point position))
        (grep-compute-defaults)
        (rgrep (thing-at-point 'symbol)
               (if buffer-file-name (concat "*." (file-name-extension buffer-file-name)) "*")
               default-directory)))))

(global-set-key (kbd "<mouse-2>") #'my-search-at-point)

We pass the argument "e" to interactive, this tells Emacs that our command expects the mouse event as input. We require the mouse event since we want to search at word/symbol at the position of mouse click rather than where the cursor is currently. event-start, event-end can be used to get starting position or ending position of a mouse event (I am using event-start since both starting and ending position of a single click event is the same).

posn-window and posn-point are used to get the window and the point of the mouse event. See Accessing Mouse for more information about these functions. Finally thing-at-point is used to retrieve the symbol at point of mouse click, and the symbol is searched for recursively in the default-directory using rgrep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.