How do I determine why emacs indented a certain amount? - emacs

In Emacs I'm editing some source code, and I hit <tab>. Emacs indents the line to n spaces. I'd like to change the amount that indents for that kind of line. How do I figure out what rule emacs applied to indent that line by n spaces?
I want to change n, but I need to figure out which of the many indentation-related variables Emacs just used.

A generic answer is difficult. Some modes will make this more apparent than others, but in the general case (as they are free to implement indentation however they wish) I don't think you'll get away from needing to read some elisp.
Starting with the binding for TAB will work, but might be slightly time-consuming depending on how many layers of indirection are involved.
If you know that the major mode in question implements its own indentation, then one (non-rigorous, but fast) approach that you could try to help track down the functions being called is to use ELP, the built in elisp profiler. elp-instrument-package will instrument for profiling all functions with names matching the prefix string argument you specify. Therefore you might do something like the following in a PHP file (noting that php-mode tells you that it is derived from c-mode)
M-x elp-instrument-package RET php- RET
M-x elp-instrument-package RET c- RET
M-x elp-instrument-package RET indent RET
Now type TAB in your source code, and run M-x elp-results to see which of those instrumented functions were called.
At this point you're on your own -- look for the likely suspects, and see what the code is doing -- but it can be a handy way to filter the search.
Once you've finished, use M-x elp-restore-all to prevent any further profiling.

If you're using a mode based on cc-mode (e.g. c-mode, c++-mode, java-mode, etc.), you can hit C-c C-s and it'll tell you what syntactic category the line is. If you want to change it, hit C-c C-o and you'll be guided through the process. Check out the cc-mode docs on customization for more details: https://www.gnu.org/s/emacs/manual/html_node/ccmode/Customizing-Indentation.html

If you happen to enjoy getting your hands really dirty, there's always the elisp debugger to tell you just what Emacs is up to.
If you hit C-h k TAB you'll find the function that Emacs is running (e.g. indent-for-tab-command) then you can do M-x debug-on-entry RET indent-for-tab-command RET. Now whenever you hit TAB you'll pop up a debugger and can watch the execution step by step.
Depending on your taste for debugging, it's either a maddening or enlightening experience. Either way, don't forget to M-x cancel-debug-on-entry when you're done.

Related

ajax-like command helper for EMACS?

I am new to Emacs. I find it is powerful and very convenient IF one can master its set of commands. I also believe anyone can do this with patience, and the eagerness to learn and USE the software. However it will be much nicer for beginners, and seasonal users to have some command helper plugin that do following thing:
When the user start typing any CONTROL or META, COMMAND, or any special keys, a "ajax-like" or "smart search" window appear below or in the minibuffer showing help text like, for example: If I hold "CONTROL", command helper windows shows:
C-p Up one line
C-n Down one line
C-f Forward one character
C-b Backward one character
C-a Beginning of line
C-e End of line
C-x ...
Then if I continue to type "x" while holding "CONTROL", the window will update and show something like:
C-x C-a add-mode-abbrev
C-x C-b list-buffers
C-x C-c save-buffers-kill-emacs
C-x C-d list-directory
C-x C-e eval-last-sexp
C-x C-f find-file
C-x C-h inverse-add-mode-abbrev
C-x TAB indent-rigidly
C-x C-l downcase-region
C-x C-n set-goal-column
C-x C-o delete-blank-lines
I have tried googling it, but it doesn't seem exist yet. So I guess this is more a feature request for EMACS rather then a question. But it is great if somebody can write a plugin/module to do this.
I do not believe this is out there, but I also believe that it is not really needed. It would just clutter the screen, though, maybe for a rookie it might be indeed useful.
For now, you might consider printing a cheatsheet.
Do not lose hope, I learned it without any sort of help, the key is indeed to just use it often.
Also, there is quite a good logic behind it all, so if you read up the structure in a cheatsheet or anywhere else, then it might help you "guess" what is the right key combo sooner than later.
EDIT: I actually myself asked a similar question, at least the same functionality was required. I accepted an answer which apparently mentions that Icicles offers this functionality.
Also, one advise is that after you type the prefix key, e.g. C-c, typing C-h afterwards will show you the possible completions.
This leads me to the following:
I believe we could use code like:
"If last key is a prefix key, run C-h*, switch back to previous buffer and rehit the last prefix key"
*this is a general method, working even for my own poorly designed minor mode
I also don't know an existing package that would do that, but here's a couple of tips.
To get an overview of all available bindings, press C-h b. The displayed buffer looks fairly raw, because it includes all ways used to enter special characters (and they are usually at the top), but you can search it with C-s if you remember at least a part of the command name.
If you know the prefix (e.g. C-x) but don't remember what comes next, you can follow it with C-h instead (type C-x C-h). That will bring up a similar buffer that only contains bindings that start with the same prefix. It's one of the more obscure features, I think.
Speaking of "if I hold Ctrl", though, showing hints when you've just pressed Ctrl and nothing else if currently impossible to implement at the Lisp level. That would require some lower-level changes.
There are too many keybindings in emacs for this to be practically useful.
I agree with the other responses and just wanted to add that I found the built-in tutorial very useful for getting past the basics. You can reach the tutorial with C-h t.
You'll probably end up changing several of the default bindings, but learning them will help you find your way around new modes (assuming that they work in an "emacsy" way).
I come back to this be cause I found a perfect solution and can't live without it: The emacs-helm and which-key

How to work out which package is unsetting keybindings?

Something in my config is unsetting/overwriting some standard keybindings for C-c C-e so that commands in various major modes don't work properly. In AUCTeX C-c C-e should start the "insert environment" dialogue and in org-mode the same keys should start the export/publish dialogue, but in both of these modes C-c C-e is undefined and emacs just waits for more input.
I know I could start emacs with -Q and load packages until something breaks (and if I load emacs that way and open a file in org-mode, the keybindings work as they should) but, like lots of people, at some point my init file became monstrously big. Is there a quicker, smarter way?
[EDIT] Nothing in my config is unsetting C-c C-e explicitly.
The debugger might give you enough clues to sort it out. Try adding the following to the beginning of your .emacs:
(debug-on-entry 'global-unset-key)
You might need to try 'local-unset-key as well. For details see the debugger manual.
Binary search is your friend.
Go to the middle of your .emacs and add (error "here"), then launch Emacs and see if the problem persists.
If it does, then insert another error line half way between the current and the top of the file, if not, comment out the current error and add one half way between the current and the bottom.
Repeat the bisection process until you isolate the line or two which are responsible for causing the problem.
You could use C-h k C-c C-e to find out which function is bound to your combination.
In the popped up help buffer, you will get the function name, its documentation and a link to the file where it is defined. Follow that link and you will end up at the place of the function definition.
At this point, it should be simple to identify which package that is responsible for redefinition of your keybinding using the name of the file.
If this does not help, you could M-x rgrep in your customization directory for 'C-c C-e' or 'C-e' and manually check everything.

How can I more easily switch between buffers in Emacs?

I've recently started using emacs and I'm enjoying using it for the most part. The only thing I'm not enjoying, is switching between buffers. I often have a few buffers open and I've grown tired of using C-x b and C-x C-b, are there any packages that make switching between buffers easier? I've looked into emacs wiki on switching buffers and I'd appreciate insight/feedback on what are are using/enjoying. Thanks.
UPDATE: iswitchb-mode is obsolete in Emacs >= 24.4, replaced by ido.
All of the features of iswitchdb are now provided by ido. Ross provided a link to the documentation in his answer. You can activate with the following in your .emacs (or use the customization interface as Ross suggests):
(require 'ido)
(ido-mode 'buffers) ;; only use this line to turn off ido for file names!
(setq ido-ignore-buffers '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
By default, ido provides completions for buffer names and file names. If you only want to replace the features of iswitchb, the second line turns off this feature for file names. ido will ignore any buffers that match the regexps listed in ido-ignore-buffers.
The behaviour described below for iswitchb-mode applies equally to ido for switching buffers.
iswitchb-mode (Emacs < 24.4)
iswitchb-mode replaces the default C-x b behaviour with a very intuitive buffer-switching-with-completion system. There are more sophisticated options, but I've never needed more than this.
After you hit C-x b, you are presented with a list of all buffers. Start typing the name of the buffer you want (or part of its name), and the list is narrowed until only one buffer matches. You don't need to complete the name, though, as soon as the buffer you want is highlighted hitting enter will move you to it. You can also use C-s and C-r to move through the list in order.
You can turn it on by default with this in your .emacs:
(iswitchb-mode 1)
You can also tell it to ignore certain buffers that you never (or very rarely) need to switch to:
(setq iswitchb-buffer-ignore '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
You can use C-x <right> (next-buffer) and C-x <left> (previous-buffer) to cycle around in the buffer ring. You could bind S-<right> and S-<left> to these functions. (S is the "super-key" or windows-key). This way you can save some keystrokes.
Moreover, note that C-x b has a default entry, i.e. it displays a standard value (most of the time this is the previously viewed buffer), so that you don't always need to enter the buffer name explicitly.
Another nice trick is to open separate windows using C-x 2 and C-x 3. This displays several buffers simultaneously. Then you can bind C-<tab> to other-window and get something similar to tabbed browsing.
M-x customize-group ido then set Ido Mode to Turn on both buffer and file and set Ido Everywhere to on. Then click the Save for future sessions button at the top and enjoy ido magic for both files and buffers. Read the docs to get a sense of how to use ido.
Also, take a look at smex.
ido-mode provides an efficient way to switch buffers.
ibuffer is best for managing all opened buffers.
anything is good for selecting an interested thing from different
sources. (for eg: a single key can be used to switch to another
buffer or to open recently closed file or to open a file residing
in the same directory or ... anything you want ... )
If you've looked at the Emacs Wiki, you probably have all this information already, but here are a few other relevant Q&As:
Emacs: help me understand file/buffer management
Buffer switching in Emacs
How to invoke the buffer list in Emacs
My toolkit consists of ibuffer, windmove+framemove, winner-mode, and a custom binding to make C-xleft/right and C-cleft/right less of a hassle to use.
I have mapped the "ยง"-key to 'buffer-list and I find it to be very efficient.
I've started using anything for a couple of days and I'm really liking it: http://www.emacswiki.org/emacs/Anything .
Emacs-fu has an good intro to anything: http://emacs-fu.blogspot.com/2011/09/finding-just-about-anything.html
My favourite function for this is helm-mini which is part of helm.
As other helm functions, it allows incremental narrowing of the selection. It also searches your recently visited buffers, which is a really nice way to re-open a buffer. Helm can be a little surprising at first and as a new Emacs user, I found it visually overwhelming and I preferred ido or ibuffer which have been suggested in other replies. But now I absolutely love it and use it all the time for countless things.
Something that I realized by accident and that can be useful:
mouse-buffer-menu is by default bound to <C-mouse-1> (Control key + mouse left click) and opens a popup with a list of the current buffers.

Emacs Lisp: How to, an interactive buffer with REPL?

Imagine the dunnet.el game, the eliza doctor or the IELM mode...; This is, evaluating answers and prompting new questions on the main buffer, then making some actions according to their elisp rules.
I would to create something like an interactive assistant, BUT NOT prompting from minibuffer as (interactive) does (not doing M-x)... I need a kind of REPL loop. So the question i how to get interaction on the working buffer (the main screen), like any text conversational/adventure, but for call my defun'ed functions on this way.
So i need a basic elisp skeleton, to make a custom REPL. The target is to make an assistant with a natural language, then parsing the anwsers, and making some actions according the rules.
Thank you for your time,
Steve,
Check out ielm, which is an elisp repl included with emacs.
Depending on what can go on in your buffer, can you just give it a major mode with an appropriate re-binding of return? (This answer vaguely inspired by Lisp-Interaction-Mode's C-j)
You should bind all keys that use for moving in buffer like C-p C-n, Up Down Arrows/ page up/down just bind them to (lambda ()). change function for C-b and C-f (Left Right Arrows) if cursor position is in beginning or end of the line. And bind Return key to function that read current line from end to optional prompt (you can use Regex to remove the prompt) do whatever you want with that string, and then go to the next line in the buffer and print your result.

Does Emacs has word and line completion (like Vim's insert mode completion)?

Vim completes words and lines with CTRL-X P and CTRL-L. There's a Emacs plugin called Company mode but this plugin interfere and cause conflicts with lots of things within Emacs (with global linum and yasnippets). I know that I can complete words with CTRL-/ in Emacs. But it is possible to take previously written lines to complete code?
Maybe you're looking for hippie-expand? From that web page (as of this writing, anyway):
HippieExpand looks at the word before
point and tries to expand it in
various ways including expanding from
a fixed list (like expand-abbrev),
expanding from matching text found in
a buffer (like dabbrev-expand) or
expanding in ways defined by your own
functions. Which of these it tries and
in what order is controlled by a
configurable list of functions.
For a comprehensive list of completion options visit the emacs wiki page on completion.
There are a gazillion ways to do completion in Emacs. Some are mode specific, some inline, some configurable and what not. Here is a list of modes that might help you.
Use numberic argument to complete by line, say M-5 M-/ will complete by line, while M-/ alone still complete the normal way.
hippe-expend function has a very useful feature which is :
With a positive numeric argument, jumps directly to the ARG next function in this list. With a negative argument or just C-u, undoes the expansion.
Customize the expansion functions in hippie-expand-try-functions-list and put the function try-expand-line as 5th list element, then you could use M-5 M-/ to complete by line.
This tip is very handy and useful and I highly recommend it.
Also worth noting: if your window manager does not steal Alt-tab, emacs will auto-complete with Alt-tab (I set up my window manager to user the "windows key" instead of alt for this very reason).
If you are using evil, this is the most vim-like solution I use:
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line-all-buffers)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
This way you can use our old friend C-x C-l in insert mode for whole line all buffers completion.
Thanks #ymln for the suggestion of using try-expand-line-all-buffers.