2

How to show all functions in a js file? I am using js2, but any solution will do.

Additional info: I am expecting an occur style buffer. This can persist and be edited if necessary. Or searched. This is preferable in my opinion to a helm style navigation tool for two reasons: 1. Helm is useful for navigating quickly to a known entity, but not for grokking structure. 2. I can continue investigation and keep that buffer alive as alluded to above.

What have I tried? I tried using a straight up occur, but the regex started to get too complex, and I imagine there is a better solution. I also know that js2 or something can parse js properly, and surely that would do something close to what I need, and properly too.

2

js-mode supports the minor mode semantic-mode. You can activate it with M-x semantic-mode. M-x imenu-add-to-menubar RET Index RET adds a menu item "Index" to the menu bar with submenus Functions and Variables. You can activate semantic mode by default with the following lines in your init file:

(defun js-setup-semantic ()
  "Setup semantic mode for js."
  (semantic-mode)
  (imenu-add-to-menubar "Index"))

(add-hook 'js-mode-hook 'js-setup-semantic)

If you want to have the functions listed in a buffer you can use imenu-tree.

Alternatively, there is imenu-list available on melpa. You can install it with M-x package-install RET imenu-list RET if you have package-archives configured for melpa.


Variant: Imenu in an Occur buffer

I've put together a small package imenu-occur.el that shows the Imenu in an Occur buffer.

In JavaScript files it shows the analysis result of Semantic. That result is not very complete for JavaScript files but maybe useful for you.

You can install that package by putting imenu-occur.el into your load path and adding
(autoload 'occur-imenu "occur-imenu")
to your init file.

You can try it by going to your JavaScript buffer and calling M-x occur-imenu RET.

  • 1
    This doesn't seem to give me what I expected. I was expecting something like an occur buffer with the lines containing all function and classes – cammil Jul 17 at 16:25
  • @cammil I added imenu-tree to the answer. – Tobias Jul 17 at 16:31
0

Maybe you're looking for speedbar which will open a side-window which lists the same functions as imenu-add-to-menubar but inside a buffer.

  • Hi Stefan. "Maybe you're looking for speedbar...": No he isn't. He is expecting an occur buffer since he wants to use the special properties of such a buffer. One of those properties is the possibility to edit the occurrences within the occur buffer. He has clarified that in his last edit of the question. – Tobias Jul 18 at 21:14

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.