![]() ( defadvice ,function (around smart-tabs activate) ( let ((indent-tabs-mode indent-tabs-mode))Īd-do-it)) ( defmacro smart-tabs-advice (function offset) ( let ((indent-tabs-mode nil)) ad-do-it)) ( defadvice indent-according-to-mode (around smart-tabs activate) ( let ((indent-tabs-mode nil)) ad-do-it)) ( defadvice indent-relative (around smart-tabs activate) ( let ((indent-tabs-mode nil)) ad-do-it)) ( defadvice align-regexp (around smart-tabs activate) (setq cua-auto-tabify-rectangles nil) ( defadvice align (around smart-tabs activate) Note: This code does not need to be placed in your config file, this is legacy code here for the explanation of how the code works.įor CC Mode (C, C++, Java et al.), add (setq-default tab-width 4) or any other preferred value To do this run ‘ M-x customize-variable RET whitespace-style RET’ and remove the entries for “Indentation SPACEs” and “SPACEs after TAB”. If you use whitespace-cleanup either manually or as a before-save-hook, you have to disable a few items in whitespace-style. ![]() Note: All language support must be added before the call to smart-tabs-insinuate. For example, if C++ mode was not provided by default it could be added as follows: (smart-tabs-add-language-support c++ c++-mode-hook All you need to do is pass in the unique symbol to identify the language, the name of the mode hook and a list of cons cells containing (indent-line-function. Support for other languages can be added by using the smart-tabs-add-language-support macro. (Of course, since smart tabs are tab size-independent, such files do not have an offset.) Adding Language Support See GuessStyle for a package which attempts to determine the proper values for indent-tabs-mode and the indentation offset by analysing how the file is formatted. For CC Mode: (setq-default indent-tabs-mode nil) (add-hook 'c-mode-common-hook What you probably want to do: Disable tabs globally (spaces only) and reactivate them for modes with smart tabs handling. To change indent-tabs-mode for the current buffer only, type ‘ M-x set-variable indent-tabs-mode RET’ or customize the mode hook. Type ‘ C-h v indent-tabs-mode RET’ to see the current, buffer-local value. Smart tabs are only used when indent-tabs-mode is non- nil (default). ( TabCompletion provides a way to do this with fewer keystrokes.) You can retab the whole file by pressing ‘ C-x h C-M-\’. To make Emacs insert the proper amount of tabs and spaces automatically, provided indent-tabs-mode is not disabled (see toggling) just press ‘ ’ as usual. You will see an alist of the languages supported as well as the hooks that will get added if you decide to turn it on. That’s it! To view the list of available languages, enable the minor mode with ‘ M-x smart-tabs-mode’ and then run ‘ M-x describe-variable RET smart-tabs-insinuate-alist RET’. For example, to enable smart-tabs-mode automatically for C and Javascript, you would add the following to your config: (smart-tabs-insinuate 'c 'javascript) Just pass this function the symbols identifying the languages that you want it to be enabled for and the appropriate hooks will be set up automatically. This can be accomplished with the smart-tabs-insinuate function. If you installed Smart Tabs Mode through Emacs’ package manager, the only thing that you need to do is tell it which languages you wish to use it for. Bug reports, code contributions, etc., are all welcome. The source code is available at GitHub see the file smart-tabs-mode.el. You can add MELPA to your package archive list with: (add-to-list 'package-archives '( "melpa". ![]() ![]() This code is now available as an Emacs minor mode through the MELPA package smart-tabs-mode and can be installed through the Emacs package manager. You can still use all spaces if you prefer, of course the code merely ensures that if you enable tabs (maybe when maintaining legacy code), the tab size does not matter. This page provides code which improves on Emacs’ auto-indenting, so that whenever you press the key, the appropriate amount of tabs and spaces is inserted automatically. One tab per “block” – any remaining whitespace is spaces only. Tabs are only used for expressing the indentation level.Everything else, like ASCII art and tables, should be formatted with spaces. Tabs are only used at the beginning of lines.The approach can be summed up in two points: This ensures that the code is displayed correctly everywhere, regardless of the viewer’s tab size. There is a semantic way of using tab characters in source code: tabs for indentation, spaces for alignment. For Sébastien Rocca-Serra’s smart-tab command, see TabCompletion. Note: This is about indenting with tabs and aligning with spaces.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |