Emacs and working with someone else's code: indentation

        It so happened that many companies have their own standard code that regulates how to indent: spaces or tabs, and also how wide they should be. What makes a beginner the first thing when working on a project where the code is already designed according to the standard? That's right, he commits the fruits of his labor and receives from the team leader for not looking at how his editor works with indentation. If you are a beginner (or moderately lucky) user of emacs and want to avoid this sad, but natural, if you have not encountered this, outcome, then welcome to cat.



    What are we dealing with?

        First, it’s nice to find out what kind of code we got. Curious, but already at this stage you can see a jumble of indents of different widths if the editor of your predecessor set tabs / spaces in the file, where the tabs were already presented as spaces / tabs. I will make a reservation that you can only see this if the width of the display of tabs does not match the width of the tabs (in spaces) in the predecessor editor.
        If you have not seen anything, then do not despair, to see what is actually happening in the file, you can use whitespace-mode. It has been added to the standard emacs distribution starting with version 23, but in which case it can always be found here: WhiteSpace .
        This is how 2 files look, at first glance the same, but using different characters for indentation, externally:



        And using whitespace-mode:



        If the code is normal and homogeneous, and indentation uses either tabs or spaces, just remember what is used and in what quantity - it will come in handy during setup. If not, tell the team leader about it, because taking responsibility for choosing a tab character is a deliberate mistake. Whichever option you choose, there will always be someone who will tell you that your choice was wrong.


    Indentation settings

        In order to choose which character will indent the value of the variable indent-tabs-mode.
    To indent spaces, it must be set to nil:
      (setq indent-tabs-mode nil) ; отступ делается пробелами
    

    accordingly, for tabs, the value should be t:
      (setq indent-tabs-mode t) ; отступ делается табами
    


    In this case, the existing indentation in the file will not change, i.e. you must make sure that the new indentation settings correspond to the existing indentation in the file, otherwise you will get porridge.


        With the characters sorted out, now you need to set the width of the indent. And there is some trick. Most of the most common languages ​​like js, php, c ++, java have backlight modes coming from c-mode, in which there is a variable c-basic-offset. She is responsible for what will be the width of the tab during indentation in all these modes. There is also a variable tab-width, it is responsible for the width of the tab. It so happened historically that in order for everything to be formatted correctly, the values ​​of these variables must be equal. If you put different values ​​for each, the result will be little predictable, to the extent that emacs will use spaces mixed with tabs.
        So, for modes based on c-mode, you need to use the following scheme:
      ; устанавливаем ширину таба в 2 символа
      (setq tab-width 2)
      (setq c-basic-offset 2)
    


    If you use cperl-mode, then the indentation is set via the cperl-indent-level variable. Again, it is very important that its value and the tab-width value match.

      ; устанавливаем ширину таба в 4 символа
      (setq tab-width 4)
      (setq cperl-indent-level 4)
    

        In conclusion, I will say that to change the html indentation you need to set the sgml-basic-offset variable (not forgetting about tab-width).


    Each language has its own rules.

        Different languages ​​require different rules described in PEP, perl-tidy configuration, and even in your company's code-standard. In this editor for all I want to use one. The easiest way to implement this is to use hooks. Here is an example configuration file for python and perl:

    ; ширина таба 4, индентация пробелами
    (add-hook 'cperl-mode-hook
      (lambda()
        (setq tab-width 4)
    	(setq indent-tabs-mode nil)
    ))
    ; ширина таба 4, индентация табами
    (add-hook 'python-mode-hook
      (lambda()
        (setq tab-width 4)
    	(setq indent-tabs-mode t)
    ))
    


    Reduction to a common denominator

        If you nevertheless become a happy owner of the code, where both tabs and spaces are used at the same time, the untabify and tabify commands will help you (thanks to user fader44 for the reminder). The first command translates all indents to spaces, the second, respectively, to tabs. Regardless of what each line was formatted, the indentation will be reduced to a single form.

    Also popular now: