12

I would like to display the opening part of a scope when this one get out of screen.

Example:

namespace a  
{  
...  
--------------------- <- display frame 
...                 |  
}*                  | * when cursor is here display "namespace a" in the minibuffer
---------------------   or on screen

This could work for many programming language but I am mainly thinking of C++.

To clarify a bit more and for those who know VS, I would like a similar feature as what exist for Visual Studio. i.e. positioning the cursor at the end of a scope display the beginning of the scope at the top of the edit buffer.

Is this feature already existing somewhere ?

2
  • I'm curious, did my answer help?
    – Tu Do
    Commented Nov 7, 2014 at 2:55
  • @TuDo yes! I just had time to have a look. I already had semantic and just turning on global-semantic-stickyfunc-mode worked nicely. Thanks. My problem now is that the color does not make the headerline readable..
    – log0
    Commented Nov 7, 2014 at 17:21

2 Answers 2

15

If you are writing C/C++/Java/Emacs Lisp/Python, then semantic-sticky-func-mode will do what you want.

NOTE: This seems to be working with CEDET from Git, not stock CEDET currently in Emacs 24.4. To get Emacs from Git:

git clone http://git.code.sf.net/p/cedet/git cedet

And load CEDET first above everything else in your init file:

(load-file (concat user-emacs-directory "/cedet/cedet-devel-load.el"))
(add-to-list 'load-path (concat user-emacs-directory "cedet/contrib"))
(load-file (concat user-emacs-directory "cedet/contrib/cedet-contrib-load.el"))

When enable, this mode shows the function point is currently in at the first line of the current buffer. This is useful when you have a very long function that spreads more than a screen, and you don't have to scroll up to read the function name and then scroll down to original position.

It displays full function interface (return type, function name and parameters), not just function name.

To enable it, put this code in your init file:

(require 'semantic)
(semantic-mode 1)
(global-semantic-stickyfunc-mode 1)

UPDATE: One of the problem with current semantic-stickyfunc-mode is that it does not display all parameters that are scattered on multiple lines. To solve this problem, I created the package stickyfunc-enhance.

DEMO:

Here is a demo in C:

semantic-sticky-func-mode-c

Here is a demo in Emacs Lisp:

semantic-sticky-func-mode-elisp

UPDATE: Alternatively, you can use helm-semantic-or-imenu. When you are inside a function and run the command, the cursor is placed right at the function in the list, so you can always see the full function interface. DEMO:

  • First, I use helm-semantic-or-imenu to move to the function helm-define-key-with-subkeys and move point there.

  • Then, I start helm-semantic-or-imenu again and helm-define-key-with-subkeys is pre-selected.

  • Then, I move point to the variable helm-map and execute helm-semantic-or-imenu again on two function: helm-next-source and helm-previous-source. This time, instead of showing the current semantic tag I'm operating in (which is helm-map), it shows the other two tags in Helm Semantic buffer. This is because I supplied a prefix argument before running the command.

Also this demo is in Emacs Lisp, it works for C/C++ as well and it's more flexible if you got a really long function interface.

helm-semantic-or-imenu

3
  • How did you get this to work for Lisp (Elisp in particular)? I evaluated your code in emacs -Q but I don't get the behavior you describe when I browse, e.g., org.el.
    – itsjeyd
    Commented Nov 5, 2014 at 20:06
  • I updated the answer. You should try it with Semantic from bzr.
    – Tu Do
    Commented Nov 6, 2014 at 2:37
  • 1
    Totally off topic. Thank you for introducing me to Nyan mode. My coding will never be the same again. Commented Nov 6, 2014 at 3:35
6

You can turn on which-function-mode by doing

M-x which-function-mode RET

To make the setting permanent, add

(which-function-mode 1)

to your init-file.

From the documentation:

Toggle mode line display of current function (Which Function mode). [...]

Which Function mode is a global minor mode. When enabled, the current function name is continuously displayed in the mode line, in certain major modes.

Note: The name doesn't really suggest it, but this works not only for "functions", but for other types of scopes as well. For example, in org-mode, which-function-mode displays the heading of the current subtree.

1
  • Thanks! I like the function name beeing displayed in the mode line. Some case were not working very well though. Like method in a struct in a namespace, the namespace name only was displayed
    – log0
    Commented Nov 7, 2014 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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