Emacs缩进难度 [英] Emacs indentation difficulty

查看:142
本文介绍了Emacs缩进难度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的想转向emacs,但学习设置环境是一个真正的痛苦。
每个人都说这是值得的,所以我只是继续。



我希望我的c代码被实现:

  if(asdf)
{
asdr = 1;
}

根据当前的标准(我知道,不要让我开始) ,可以是:

  if(asdf){
asdr = 1;
}

我似乎无法从2更改缩进大小,它总是看起来像GNU标准:

  if(asdf)
{
asdr = 1;
}

,我不喜欢
这是我放在我的.emacs中:

 ;在(); if(x = 0),... 
(global-cwarn-mode 1)

; c
中无电模式(c-toggle-electric-state -1)
;只有当光标位于行开头的时候,缩进当前行
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-基本缩进4)
;我在网上阅读的这些命令,但是它们不起作用
;(highlight-tabs)
;(highlight-trailing_whitespace)

没有帮助,我还是GNU缩进。
任何人?



---编辑添加我的全部.emacs(实际上〜/ .emacs.d / init.el)

 ;目录将各种el文件放入
(add-to-list'load-pathC:/Program/emacs-22.3/includes)

;打开一个.rb文件时加载红宝石模式。
(autoload'ruby-moderuby-mode编辑ruby脚本的主要模式t)
(setq auto-mode-alist(cons'(.rb $。ruby-mode )auto-mode-alist))
(setq auto-mode-alist(cons'(.rhtml $。html-mode)auto-mode-alist))

-hook'ruby-mode-hook
(lambda()
(add-hook'local-write-file-hooks
'(lambda()
(save-excursion
(untabify(point-min)(point-max))
(删除尾随空格)
)))
(set(make-local-variable'indent-tabs -mode)'nil)
(set(make-local-variable'tab-width)2)
(imenu-add-to-menubarIMENU)
(define-key ruby -mode-map\Cm'newline-and-indent);不确定此行是否为100%正确,但它可以正常工作!
(require'ruby-electric)
(ruby-electric-模式t)
))

;安装模式编译,以提供更友好的编译支持!
(autoload'mode-compilemode-compile
根据主要模式t编译当前缓冲区文件的命令
(global-set-key\C-cc '模式编译)
(autoload'mode-compile-killmode-compile
命令来杀死由`mode-compile'启动的编译'
(global- set-key\C-ck'mode-compile-kill)


(show-paren-mode 1)

;彩色主题
(需要'color-theme)
(color-theme-pok-wog)
;; Emacs.pane.menubar。*似乎不起作用?
; Emacs.pane.menubar.background:darkGrey
; Emacs.pane.menubar.foreground:black


;默认字体9 pt
(set-face-attribute'default nil:height 80)

(custom-set-variables
;; custom-set-variables
;;如果你手工编辑,你可以把它弄乱,所以要小心
;;你的init文件应该只包含一个这样的例子
;;如果有更多的

(custom-set-faces
;; Custom-set-faces被Custom添加
;;如果你编辑你可以把它弄乱,所以要小心
;;你的init文件应该只包含一个这样的实例
;;如果有不止一个,它们将无法正常工作。
'(font-lock-comment-face((t(:foregroundlimegreen:slant tilt))))
'(font-lock-preprocessor-face((t(:inherit font- lock-builtin-face:前景orange:weight bold))))


(全局设置键[C-tab]'其他窗口)
(global-set-key [CS-tab](lambda()(interactive)(other-window -1))

(defun linux-c-mode()
C
(交互式)
(c-mode)
(setq c-indent-level 8)
( setq c-brace-imaginary-offset 0)
(setq c-brace-offset -8)
(setq c-argdecl-indent 8)
(setq c-label-offset -8 )
(setq c-continue-statement-offset 8)
(setq indent-tabs-mode nil)
(setq tab-width 8))


;在(); if(x = 0),...
(global-cwarn-mode 1)

; c
中无电模式(c-toggle-electric-state -1)
;只有当光标位于行开头的时候,缩进当前行
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-缩进4)
;我在网上阅读的这些命令,但是它们不起作用
;(highlight-tabs)
;(highlight-trailing_whitespace)
(setq indent-tabs-mode nil)
(setq c-default-styleuser)


;;删除延迟:滚动条,工具栏,菜单栏。
(if(fboundp'scroll-bar-mode)(scroll-bar-mode -1))
(if(fboundp'tool-bar-mode)(tool-bar-mode -1))
(if(fboundp'menu-bar-mode)(menu-bar-mode -1))


;;恢复以前使用的窗口大小
(defun restore-saved-window-size()
(除非(加载〜/ .emacs.d / whsettingst nil t)
(setq saved-window-size'(80 30)))
(nconc default-frame-alist`((width。,(car saved-window-size))
(height。 save-window-size)))))

(restore-saved-window-size)

(defun save-window-size-if-changed(& optional未使用)
(let((原始窗口大小)(,(frame-width),(frame-height))))
(除非(相等的原始窗口大小保存窗口大小)
(with-temp-buffer
(setq saved-window-size original-window-size)
(insert(concat(setq saved-window-size'
(prin 1 string saved saved saved saved window window window saved saved saved))add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add add -hook'window-size-change-functions'save-window-size-if-changed)

;; Ack作为grep
的替代品(全局设置键\Ms'ack)
(require'ack)


解决方案

您的默认值可能被 cc-mode 的样式功能覆盖。



尝试初始化这样的默认样式:



(setq c-default- ((java-mode。java)(awk-mode。awk)(other。user)))



您应该可以将上一行粘贴到.emacs文件中,或者自定义 c-default-style 变量。默认值为(其他gnu)作为列表的最后一个元素,这意味着所有非Java和非awk文件都可以获得 gnu 样式,而不是使用 setq 设置的样式。特殊的用户样式从您手动设置的样式变量初始化。



另一个选项是选择其中一个内置样式,而不是自己定义,或使用 c-add-style 函数创建自己的样式。为此,将上述命令中的user更改为样式的名称(作为字符串)。尝试 stroustrup python 内置样式,以格式化if-statements如何。


I'm really trying to switch to emacs, but learning to setup the environment is a real pain. Everybody says it's worth it, so I just continue.

I want my c code to be implemented that way:

if(asdf)
{
    asdr = 1;
}

Depending on the current standard (I know, don't get me started), could be:

if(asdf) {
    asdr = 1;
}

I can't seem to change the indentation size from 2, it always looks like the GNU standard:

if(asdf)
  {
    asdr = 1;
  }

, which I dislike. Here is what I have put in my .emacs:

; Warn in C for while();, if(x=0), ...
(global-cwarn-mode 1)

; no electric mode in c
(c-toggle-electric-state -1)
; indent the current line only if the cursor is at the beginning of the line
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-indent 4)
; These commands I read about on the web, but they don't work?
;(highlight-tabs)
;(highlight-trailing_whitespace)

This did not help, I have still the GNU indent. Anyone?

--- EDIT TO ADD MY WHOLE .emacs (actually ~/.emacs.d/init.el)

; directory to put various el files into
(add-to-list 'load-path "C:/Program/emacs-22.3/includes")

; loads ruby mode when a .rb file is opened.
(autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
(setq auto-mode-alist  (cons '(".rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist  (cons '(".rhtml$" . html-mode) auto-mode-alist))

(add-hook 'ruby-mode-hook
          (lambda()
            (add-hook 'local-write-file-hooks
                      '(lambda()
                         (save-excursion
                           (untabify (point-min) (point-max))
                           (delete-trailing-whitespace)
                           )))
            (set (make-local-variable 'indent-tabs-mode) 'nil)
            (set (make-local-variable 'tab-width) 2)
            (imenu-add-to-menubar "IMENU")
            (define-key ruby-mode-map "\C-m" 'newline-and-indent) ;Not sure if this line is 100% right but it works!
            (require 'ruby-electric)
            (ruby-electric-mode t)
            ))

; Install mode-compile to give friendlier compiling support!
(autoload 'mode-compile "mode-compile"
   "Command to compile current buffer file based on the major mode" t)
(global-set-key "\C-cc" 'mode-compile)
(autoload 'mode-compile-kill "mode-compile"
 "Command to kill a compilation launched by `mode-compile'" t)
(global-set-key "\C-ck" 'mode-compile-kill)


(show-paren-mode 1)

; Color theme
(require 'color-theme)
(color-theme-pok-wog)
;;Emacs.pane.menubar.* does not seem to work? 
;Emacs.pane.menubar.background: darkGrey
;Emacs.pane.menubar.foreground: black


; Default font 9 pt
(set-face-attribute 'default nil :height 80)

(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 )
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(font-lock-comment-face ((t (:foreground "limegreen" :slant oblique))))
 '(font-lock-preprocessor-face ((t (:inherit font-lock-builtin-face :foreground "orange" :weight bold)))))


(global-set-key [C-tab] 'other-window)
(global-set-key [C-S-tab] (lambda () (interactive) (other-window -1)))

(defun linux-c-mode ()
  "C mode with adjusted defaults for use with the Linux 
kernel."
  (interactive)
  (c-mode)
  (setq c-indent-level 8)
  (setq c-brace-imaginary-offset 0)
  (setq c-brace-offset -8)
  (setq c-argdecl-indent 8)
  (setq c-label-offset -8)
  (setq c-continued-statement-offset 8)
  (setq indent-tabs-mode nil)
  (setq tab-width 8))


; Warn in C for while();, if(x=0), ...
(global-cwarn-mode 1)

; no electric mode in c
(c-toggle-electric-state -1)
; indent the current line only if the cursor is at the beginning of the line
(setq-default c-tab-always-indent nil)
(setq-default c-indent-level 4)
(setq-default tab-width 4)
(setq indent-tabs-mode nil)
(setq-default c-basic-offset 4)
(setq-default c-basic-indent 4)
; These commands I read about on the web, but they don't work?
;(highlight-tabs)
;(highlight-trailing_whitespace)
(setq indent-tabs-mode nil)
(setq c-default-style "user")


;; Remove lull: scroll bar, tool bar, menu bar.
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))


;; restore window size as it was at previous use
(defun restore-saved-window-size()
  (unless (load "~/.emacs.d/whsettings" t nil t)
    (setq saved-window-size '(80 30)))
  (nconc default-frame-alist `((width . ,(car saved-window-size))
                   (height . ,(cadr saved-window-size)))))

(restore-saved-window-size)

(defun save-window-size-if-changed (&optional unused)
  (let ((original-window-size  `(,(frame-width) ,(frame-height))))
    (unless (equal original-window-size saved-window-size)
      (with-temp-buffer
        (setq saved-window-size original-window-size) 
        (insert (concat "(setq saved-window-size '"
                        (prin1-to-string saved-window-size) ")"))
        (write-file "~/.emacs.d/whsettings")))))

(add-hook 'window-size-change-functions 'save-window-size-if-changed)

;; Ack as a replacment for grep
(global-set-key "\M-s" 'ack)
(require 'ack)

解决方案

Your defaults are possibly being overridden by cc-mode's style feature.

Try initialising the default style like this:

(setq c-default-style '((java-mode . "java") (awk-mode . "awk") (other . "user")))

You should be able to paste the preceding line into your .emacs file, or customize the c-default-style variable. The default has (other "gnu") as the last element of the list, which means that all non-Java and non-awk files get the gnu style instead of what you set with setq. The special user style gets initialised from your manually-set style variables.

Another option is to pick one of the built-in styles instead of defining it yourself, or to create your own style using the c-add-style function. To do this, change "user" in the above command to the name of the style (as a string). Try the the stroustrup or python built-in styles to format if-statements how you want.

这篇关于Emacs缩进难度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆