Display function name in status-line - emacs

I'd like to have the function name displayed next to the buffer name
on the mode-line if the point is inside a function (maybe with some reasonable truncation if necessary).
Is ther a simple way to achieve this? (via imenu? defuns?)
Example: in test.py if my cursor is inside def myfunction, I'd like my mode-line say:
test.py: myfunction
Could be also interesting for org mode (displaying the inside-most header)

Which Function Mode should help you :
WhichFuncMode (also known as WhichFunctionMode) is a minor mode, that when activated displays the current function name in the mode line. It works under certain major modes, like CcMode or PerlMode.
To activate this minor mode : M-x which-function-mode RET
An interesting effect of this mode is by middle-clicking on the current function name : it can narrow the display to the current function.

OK... I found it: the mode is built in and can be globally activated by:
(which-function-mode 1)
Change colors and add major modes (e.g. org-mode is not enabled by default) with:
M-x customize-group and selecting which-func

Related

Some questions about emacs mode

As we know, mode is very important in emacs. But I feel I am not very clear about how to set it. For example, I often see something like (***-mode 1) or (***-mode) in .emacs file. And some tutorials also say that a mode can be set by M-x ***-mode. Could you tell me what's the differences between them and how to use them? Thanks!
A Lisp function is a piece of code which declares a name for another piece of code to be executed later.
(defun hello ()
(message "Hiya!"))
Now, you can invoke the named code from anywhere else in Lisp.
(hello)
Only at this point does the message form get executed.
Many Lisp functions contain an interactive form which specifies how they should behave when called interactively (for example, should it prompt for an argument, or use the cursor or mouse position as the argument, etc). Those which do can be invoked with M-x and the function name.
A major mode specifies a function which sets up some variables to exclusively control the behavior of Emacs. For example, M-x text-mode sets a (very basic) regime for word wrapping and cursor movement which is suitable for text files. When you are in text mode, you cannot be in C++ mode, or Lisp mode, or fundamental mode. These are other major modes which define different or additional functionality suitable for editing other types of text.
Because a major mode is exclusive, it is usually a function which doesn't take any arguments. So to put the current buffer in text mode, the Lisp code is simply
(text-mode)
Minor modes, by contrast, specify additional behavior which is independent from the major mode. For example, Overwrite mode specifies a different behavior when inserting text before some other text -- normally, Emacs pushes any existing text ahead, but when overwrite mode is active, existing text in front of the cursor will be replaced as you type.
You can have multiple minor modes active at any time -- you could have flyspell (spell checking as you type), tool bar mode, menu bar mode, and line number mode active at the same time as you are in text mode and overwrite mode.
Because of this, a common (though not universal) convention for minor modes is to perform a toggle. When you are already in toolbar mode, M-x toolbar-mode will disable this minor mode. To unambiguously disable the mode, pass it a negative numeric argument;
(toolbar-mode -1)
Without the argument, the code will toggle -- the result will depend on whether the mode was already active, or not.
(As noted in a comment, this changed in Emacs 24; I'm describing the historical behavior.)

emacs auto formatting when I type space at end of line

Hi i have the following issue with my emacs:
When typing a long line, sometimes, when I type a space at the end of a line the line automatically gets split into multiple lines.
e.g. Line I am typing emacs: 'This is my line 1. This is my line 2. This is my line 3'%space%
emacs automatically formats this to:
'This is my line 1. %emacs adds new line%
This is my line 2. %emacs adds new line%
This is my line 3 %space%
Please help me fix this annoyance :)
What you are seeing is a feature (grin). "Auto Fill" mode is a buffer-local minor mode in
which lines are broken automatically when they become too wide. "Minor mode" means it is additional functionality which is associated with a buffer.
If you look at the emacs mode line, it will say "Auto Fill" if this is active. To turn it off for that buffer, M-x auto-fill-mode.
If for a particular major mode you would always like it on, you can turn it on by modifying the hook for that. For example, if every time you edit a text file, you would like auto-fill on, you can customize the variable text-mode-hook to turn it on.
M-x customize-variable
when prompted for the variable, say:text-mode-hook. You can use the same mechanism to turn it on (or off) for other modes.
Turn off auto-fill-mode: Put this in your init file, or do it for a particular mode on the mode hook: (auto-fill-mode).

emacs - stop adding indentation to current and next line when pressing RET

I have a file open called test.scss, and when I press RET on a line, emacs will add 2 spaces to the current line and 4 extra spaces to the next line.
I've highlighted what the file looks like with whitespace-mode.
before pressing RET
after pressing RET
You can see that the .my-element row was auto-indented by 2 spaces, and the new line is indented by 4 spaces too many.
I want the output to look like this instead
desired output
What can I do to make emacs produce my desired output?
Here is the output of describe-mode:
Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption
Electric-Indent File-Name-Shadow Font-Lock Global-Eldoc
Global-Font-Lock Line-Number Menu-Bar Tooltip Whitespace
(Information about these minor modes follows the major mode info.)
SCSS mode defined in `css-mode.el':
Major mode to edit "Sassy CSS" files.
In addition to any hooks its parent mode `css-mode' might have run,
this mode runs the hook `scss-mode-hook', as the final step
during initialization.
Although in this case I'm in scss-mode, I see similar behavior with most of the other modes I use, such as ruby-mode, sgml-mode, js.el mode and others. I'd like to make the behavior match the desired output shown above.
Each mode can handle indentation in its own way, and you may have to look for mode-specific settings to get two-space indentation working everywhere.
For starters, you can set css-indent-offset, which should cover css-mode and scss-mode:
(setq css-indent-offset 2)
You can set the basic indenting of many other modes similarly. ruby-mode seems to use ruby-indent-level, sgml-mode uses sgml-basic-offset, and js-mode uses js-indent-level.

Return to previous mode in Emacs

Is there a key binding to quit a mode and return to the previous mode in emacs?
For example suppose I entered line number mode using the following command:
Alt+x linum-mode
How can I quickly disable this mode and return to the mode which I was in before (not using the same command again)?
Why wouldn't you want to use the same command again?
M-xM-pRET
It doesn't get much simpler than that.
Edit: You can repeat M-p in that sequence to step back further in the command history, and you can search the command history with M-xC-r.
Also, when you disable a minor mode you're not "returning to the mode you were in before"; you're just disabling one (of many) minor modes which are all active at the same time.
Tangentially, the concept of "returning to the mode you were in before" could apply to major modes (as there's only ever one active major mode in a given buffer), but strictly speaking there's no notion of 'disabling' a major mode -- only of 'enabling' the one you wish to change to -- so to 'toggle' between two major modes, you would need to call them alternately.
Just repeat the same command: M-x linum-mode. Such minor-mode commands are toggles: on/off.
C-x z calls repeat - repeat last command.
Repeatedly calling a minor-mode enables/disables it.
One more way to do it is with smex: the last command
that you called with M-x kinda sticks around.
So you can enable linum-mode with smex, do a bunch of editing
with usual shortcuts and then disable linum-mode with
M-x RET.
A solution might follow the path kill-ring-save works: store the modes being active as current-modes-listing in a previous-modes-ring.
The code needed therefor exists basically inside describe-mode, see upward from "Enabled minor modes" - respective for the major mode.
Then a hook should check if this-command has "-mode" in it's name. If yes, check, if current modes-listing equals car of previous-modes-ring. If not, add new setting.
Finally write a command which sets current modes according to selected listing from previous-modes-ring.

Why do I start iswitchb-mode this way?

According to the emacs info page, here's how you enable iswitchb-mode:
To enable Iswitchb mode, type M-x iswitchb-mode, or customize the
variable iswitchb-mode to t
So I put the following in my .emacs:
(setq iswitchb-mode t)
However, this doesn't seem to work. After searching the emacs wiki, I found that I need to use this:
(iswitchb-mode 1)
Could someone explain why I need to enable it this way? I'd like to get a better understanding of elisp rather than just copying and pasting things from places.
Typically a mode will define both a variable and a function with the same name. The function will set the variable properly when it's called, but it's the function that turns on the mode, not just the variable (that only tracks the mode's state).
In your specific case, you were told told customize the variable, but you simply set it instead. The difference is that when the value of the variable changes, custom knows to do something and `setq' knows nothing of this. If you look at the help for this variable (C-h v iswitchb-mode) you'd get:
iswitchb-mode is a variable defined in `iswitchb.el'.
Its value is t
Documentation:
Non-nil if Iswitchb mode is enabled.
See the command `iswitchb-mode' for a description of this minor mode.
Setting this variable directly does not take effect;
either customize it (see the info node `Easy Customization')
or call the function `iswitchb-mode'.
You can customize this variable.