2

Suppose you have a C code like this:

void f1() {
    int var1;

    var1 = 1; // local use of var1
}

void f2() {
    int var1;

    var1 = 2; // local use of var1
}

void f3() {
    int var1;

    var 1 = 3; // local use of var1
}

How do I go to the defition of a local variable with Emacs and GNU Global (gtags.el) while pointing at a local use of it?

I've tried gtags-find-tag (with "var1" as argument) and it doesn't find anything (looks like tags are supposed to be functions) and I've tried gtags-find-symbol, which shows me a list with all three var1 definitions (and possible uses as well).

ergosys
  • 47,835
  • 5
  • 49
  • 70
ivarec
  • 2,542
  • 2
  • 34
  • 57

1 Answers1

3

You do not need the help of Global, but it can be done as a keyboard macro:

  • Take a note of the current word, the "var1" in this case
  • Search backward using emacs beginning-of-defun lisp function
  • Search forward for "var1", this is usually the definition of var1 as it is the first appearance of var1 in the current defun.

Starting from this kerboard macro, I wrote the following lisp function:

(defun bhj-isearch-from-bod (&optional col-indent)
  (interactive "p")
  (let ((word (current-word)))
    (beginning-of-defun)
    (setq regexp-search-ring (cons (concat "\\b" word "\\b") regexp-search-ring))
    (search-forward-regexp (concat "\\b" word "\\b"))))
Bao Haojun
  • 966
  • 5
  • 9
  • 1
    Nice. It would be nice if I could use the same keybinding for this and for the tag lookup. – ivarec Sep 12 '12 at 21:06