在所有模式下,如何获得自动缩进(不是智能缩进)在emacs [英] How to get auto indent (not smart indent) in emacs in all modes

查看:183
本文介绍了在所有模式下,如何获得自动缩进(不是智能缩进)在emacs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的emacs,它的缩进正在推动我的墙壁。对自己的好处太聪明了它(错误地)认为它知道我想要格式化我的源,但是我没有时间为每个不同的语言为每个模式追逐每个设置,我写代码;许多这些语言根本没有启用任何模式。



这是我想要的行为:




  • TAB插入缩进

  • RET插入一个新行,然后将空白字符从上一行开头复制到第一个非空白字符或行尾,以较早者为准。在行开始和第一个非空白字符/行尾之间的空白文本中删除一个

  • DEL(退格键) em> indent 如果可能,否则单字符正常

  • {

  • 没有自动缩进} / li>
  • 事实上,随时随地都没有任何智能屁股缩进的行为,只需在RET上复制上一行的缩进。

  • 每个源文件格式要配置的两个变量:显示标签宽度,以及缩进的内容。优选地,这些可以被配置为随机源代码格式,而不必为它们写主要模式,除非写入主模式是.emacs中的单行,由两个 setq s。



这将使我在所有语言中都符合逻辑和一致的行为。它会将代码格式化到我的工作,但没关系,我已经做了20年了,我知道如何使其他宏使它有效率。更重要的是,它使我无限期地摆脱配置设置,试图获得符合自己喜好的自动行为。而且我的宏可以依赖一致的行为,所以他们可以在所有模式下正常工作。



以上可能吗?以前有人这样做过吗?有没有一些小的模式,那就是这样吗?

解决方案

这里是代码:

 (setq tab-width 4)
(defun plain-tab()
(interactive)
(insert(make-string tab-width?\)))
(defun plain-ret()
(interactive)
(look-back^ \\(+ \\) )
(newline)
(insert(match-string 1)))
(defun plain-del()
(interactive)
(backward-delete- char
(if(look-back(format\\ {%d\\\}tab-width))tab-width 1)))
(defvar all-the-mode -maps
'(c-mode-map c ++ - mode-map java-mode-map
js-mode-map emacs-lisp-mode-map
clojure-mode-map))
(require'cc-mode)
(require'js)
(require'clojure-mode)
(eval`(mapc
(lambda(map)
(define-key map [tab]'plain-tab)
(define-key map [return]'plain-ret)
(define-key map [空格]'plain-del)
(define-key map{(lambda()(interactive)(insert{))
(define-key map (交互式)(插入}))))
(list,@ all-the-mode-maps))


I'm new to emacs, and its indenting is driving me up the walls. It's too smart for its own good; it (incorrectly) thinks it knows how I want to format my source, but I don't have time to chase down every setting for every mode for every different language that I write code for; and many of those languages don't have any mode enabled at all.

Here's the behaviour I'd like:

  • TAB inserts indent
  • RET inserts a new line then copies the blank characters from the start of the previous line to the first non-blank character, or end of line, whichever comes sooner
  • DEL (backspace key) in the blank text between line start and first non-blank character / end of line deletes one indent if possible, otherwise single character like normal
  • No auto-indent on {
  • No auto-unindent on }
  • In fact, no smart-ass indenting behaviour anywhere anytime, just copy previous line's indent on RET.
  • Two variables to be configured per source file format: display tab width, and contents of indent. Preferably these can be configured for random source code formats without having to write a major mode for them, unless writing a major mode is a one-liner in .emacs, consisting of two setqs.

This would get me logical and consistent behaviour across all languages. It would leave the work of formatting the code to me, but that's OK, I've been doing that for 20 years, and I know how to make other macros that make it efficient. More importantly, it saves me from endless fiddling with configuration settings trying to get the automatic behaviour to suit my preferences. And my macros can rely on consistent behaviour so they work correctly in all modes.

Is the above possible? Surely someone else has done this before? Is there some minor mode out there that makes it so?

解决方案

Here's the code:

(setq tab-width 4)
(defun plain-tab ()
  (interactive)
  (insert (make-string tab-width ?\ )))
(defun plain-ret ()
  (interactive)
  (looking-back "^\\( +\\).*")
  (newline)
  (insert (match-string 1)))
(defun plain-del ()
  (interactive)
  (backward-delete-char
   (if (looking-back (format " \\{%d\\}" tab-width)) tab-width 1)))
(defvar all-the-mode-maps
  '(c-mode-map c++-mode-map java-mode-map
    js-mode-map emacs-lisp-mode-map
    clojure-mode-map))
(require 'cc-mode)
(require 'js)
(require 'clojure-mode)
(eval `(mapc 
        (lambda(map)
          (define-key map [tab] 'plain-tab)
          (define-key map [return] 'plain-ret)
          (define-key map [backspace] 'plain-del)
          (define-key map "{" (lambda()(interactive)(insert "{")))
          (define-key map "}" (lambda()(interactive)(insert "}"))))
        (list ,@all-the-mode-maps)))

这篇关于在所有模式下,如何获得自动缩进(不是智能缩进)在emacs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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