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

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

问题描述

我是 emacs 的新手,它的缩进让我爬上墙.为了自己的利益,它太聪明了;它(错误地)认为它知道我想如何格式化我的源代码,但我没有时间为我编写代码的每种不同语言的每种模式追逐每个设置;并且其中许多语言根本没有启用任何模式.

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.

这是我想要的行为:

  • TAB 插入缩进
  • RET 插入一个新行,然后将上一行开头的空白字符复制到第一个非空白字符或行尾,以先到者为准
  • 行开始和第一个非空白字符/行尾之间的空白文本中的 DEL(退格键)如果可能,删除一个 缩进,否则像正常一样删除单个字符
  • 没有自动缩进 {
  • 没有自动缩进 }
  • 事实上,随时随地都没有聪明的缩进行为,只需在 RET 上复制上一行的缩进即可.
  • 每个源文件格式要配置两个变量:显示标签宽度,以及缩进的内容.最好可以将这些配置为随机源代码格式,而不必为它们编写主模式,除非编写主模式是 .emacs 中的单行代码,由两个 setq 组成.
  • 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.

这将使我在所有语言中获得合乎逻辑且一致的行为.它会将格式化代码的工作留给我,但没关系,我已经这样做了 20 年,而且我知道如何制作其他宏以使其高效.更重要的是,它使我免于无休止地摆弄配置设置,试图让自动行为满足我的喜好.而且我的宏可以依赖一致的行为,因此它们可以在所有模式下正常工作.

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?

推荐答案

代码如下:

(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天全站免登陆