简体   繁体   中英

How do I determine why emacs indented a certain amount?

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.

If you're using a mode based on cc-mode (eg c-mode, c++-mode, java-mode, etc.), you can hit Cc Cs and it'll tell you what syntactic category the line is. If you want to change it, hit Cc Co 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

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)

Mx elp-instrument-package RET php- RET
Mx elp-instrument-package RET c- RET
Mx elp-instrument-package RET indent RET

Now type TAB in your source code, and run Mx 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 Mx elp-restore-all to prevent any further profiling.

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 Ch k TAB you'll find the function that Emacs is running (eg indent-for-tab-command ) then you can do Mx 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 Mx cancel-debug-on-entry when you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM