开发者

How to edit code in Emacs handling tabs and whitespace like Visual Studio does?

开发者 https://www.devze.com 2023-02-08 21:09 出处:网络
I am starting on a project where everyone is using Microsoft Visual Studio to edit code. But I am an Emacs user.

I am starting on a project where everyone is using Microsoft Visual Studio to edit code. But I am an Emacs user.

When I open those C++ files in my Emacs they look bizarre. Example:

namespace·ns·{
»class·Foo·{
»»virtual·function_name(·some_type::const_iterator·start
»»»»»»»»····,·some_type::const_iterator·end
»»»»»»»»»,·boost::shared_ptr<some_type>·varname·)·=·0;
»};
}

where I here display a tab as », and a space as '.', to show the differences.

I'd like to view and edit code transparently, so that it looks right in my Emacs and is saved so that it looks normal to the Visual Studio users.

So, how can I set Emacs so that it shows the files like they were supposed to be shown, which is presumably:

namespace ns {
  class Foo {
    virtual function_name( some_type::const_iterator start
                         , some_type::const_iterator end
                         , boos开发者_开发知识库t::shared_ptr<some_type> varname ) = 0;
  };
}

And I'd like to tell Emacs to save it using the tabs in the original, weird, way.


Setting tab-width 3 gives us:

namespace ns {
   class Foo {
     virtual function_name( some_type::const_iterator start
                           , some_type::const_iterator end
                          , boost::shared_ptr<some_type> varname ) = 0;
   };
}

which is as close to your presumed formatting as we can get, but not exact. Do you know if that matches what Visual Studio shows?

You'll presumably want the following:

(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(defun my-c++-mode-hook ()
  (setq tab-width 3
        indent-tabs-mode t
        c-basic-offset 3))

Hopefully that makes a good start, but there are bound to be other settings needed to match how Visual Studio does things, so I would suggest you start reading at the Emacs Wiki:

http://www.emacswiki.org/emacs/IndentingC


You should convert all tabs to spaces selecting the code and using M-x untabify. That will pretty much unify the appearance of the code in every editor.

0

精彩评论

暂无评论...
验证码 换一张
取 消