如何在emacs中设置.h和.cpp文件中的列宽? [英] How to set a column width in .h and .cpp files in emacs?
问题描述
我想在emacs中的.h和.cpp文件的text-width为80列。
我试过的是以下,
I want to have text-width to 80 columns for .h and .cpp files in emacs. What I tried is following,
Mx自动填充模式
Cx f
但以上解决方案无效。
But above solution is not working.
此外,我还尝试了 http:/ /www.emacswiki.org/emacs/CPlusPlusMode 。如下。
Also I tried the solution present at http://www.emacswiki.org/emacs/CPlusPlusMode. It is as below.
; style I want to use in c++ mode
(c-add-style "my-style"
'("stroustrup"
(indent-tabs-mode . nil) ; use spaces rather than tabs
(c-basic-offset . 4) ; indent by four spaces
(c-offsets-alist . ((inline-open . 0) ; custom indentation rules
(brace-list-open . 0)
(statement-case-open . +)))))
(defun my-c++-mode-hook ()
(c-set-style "my-style") ; use my-style defined above
(auto-fill-mode)
(c-toggle-auto-hungry-state 1))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)**
尽管我并没有完全了解emacswiki提供的解决方案。
欣赏这方面的任何帮助。
Though, I did not completely understand the solution provided at emacswiki. Appreciate any help in this regard.
谢谢。
推荐答案
您只需要在这些模式中设置 fill-column
,您可以通过定义一个函数来将其添加到相关模式中(您可以也可以作为一个 lambda
,但是通常很好的做法是把它放在一个命名的函数中,所以你可以稍后从钩子中删除它。默认情况下, *。h
文件出现在 c-mode
中,而 *。cpp
文件默认出现在 c ++ - mode
中。首先,定义一个函数:
You need to set fill-column
only in those modes, which you can do by defining a function to do so and adding that hook to the relevant modes (you could also do it as a lambda
, but it's usually good practice to put it in a named function so you can remove it from the hook later if you need to). *.h
files come up in c-mode
by default, while *.cpp
files come up in c++-mode
by default. First, define a function:
(defun my-c-hook ()
(setq fill-column 80))
然后,将该函数添加到相关模式钩子中:
Then, add that function to the relevant mode hooks:
(add-hook 'c-mode-hook 'my-c-hook)
(add-hook 'c++-mode-hook 'my-c-hook)
您可以使您的功能如您所愿,例如,放入其他您原来的项目:
You can make your function as rich as you like, eg, by putting in the other items you had originally:
(defun my-c-hook ()
(c-set-style "my-style")
(auto-fill-mode)
(c-toggle-auto-hungry-state 1)
(setq fill-column 80))
这篇关于如何在emacs中设置.h和.cpp文件中的列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!