抑制选定区域中的emacs自动填充

抑制选定区域中的emacs自动填充,emacs,latex,elisp,Emacs,Latex,Elisp,我使用emacs编辑所有内容。在我的一些LateX文档中,我想在编辑表格和代码时自动禁用自动填充模式。基本上,我想要两个标签,比如: %%% BEGIN NO FILL %%% END NO FILL 它们之间的任何内容都不会自动填充 有人能想出一个办法吗?我需要弄清楚光标是否在区域内,然后必须切换模式,并且每次光标移动时都需要这样做。或者有更好的方法吗?如果您正在使用AUCTeX(您应该这样做),那么您可能需要查看LaTeX缩进环境列表。向该变量添加一个环境将使它(除其他外)不会

我使用emacs编辑所有内容。在我的一些LateX文档中,我想在编辑表格和代码时自动禁用自动填充模式。基本上,我想要两个标签,比如:

   %%% BEGIN NO FILL
   %%% END NO FILL
它们之间的任何内容都不会自动填充

有人能想出一个办法吗?我需要弄清楚光标是否在区域内,然后必须切换模式,并且每次光标移动时都需要这样做。或者有更好的方法吗?

如果您正在使用AUCTeX(您应该这样做),那么您可能需要查看
LaTeX缩进环境列表
。向该变量添加一个环境将使它(除其他外)不会重新填充段落。不幸的是,它似乎不适用于自动填充模式。添加到
LaTeX模式钩子中的以下大部分未经测试的代码可能会满足您的需要

(setq auto-fill-function
  (lambda ()
    (unless (> (save-excursion (or (search-backward "%%% BEGIN NO FILL" (point-min) t) 0))
               (save-excursion (or (search-backward "%%% END NO FILL"   (point-min) t) 0)))
      (do-auto-fill))))
这是非常愚蠢和低效的,但似乎在我的机器上足够快。它不允许嵌套,并要求您手动标记所有不希望填充的部分。我想添加到我的.emacs中的内容(直到我读了你的问题,我才意识到这个问题有多严重)低于当前环境的哪个键,因此不需要特殊标记(尽管它只查看最内部的环境(我不确定在实践中会导致多大问题))。将两者结合起来作为练习留给感兴趣的读者

;; You can use the following to unset the variables and play around with them
;; (makunbound 'auto-fill-ignore-environments)
;; (makunbound 'auto-fill-ignore-environments-regexp)
(defcustom auto-fill-ignore-environments
  (mapcar 'car LaTeX-indent-environment-list)
  "List of environments for which `auto-fill-mode' should be
disabled.  Used to generate `auto-fill-ignore-environments-regexp'."
  :type '(sexp)
  )
(defcustom auto-fill-ignore-environments-regexp
  (regexp-opt auto-fill-ignore-environments)
  "Regexp matching LaTeX environments for which `auto-fill-mode'
should be disabled.  If not set, automatically generated from
`auto-fill-ignore-environments'"
  :type '(string)
  :set-after '(auto-fill-ignore-environments)
  )
(add-hook 'LaTeX-mode-hook
          (lambda ()
            (setq auto-fill-function
                  (lambda ()
                    (unless (string-match auto-fill-ignore-environments-regexp
                                          (LaTeX-current-environment))
                      (do-auto-fill))))))

我以前从未使用过defcustom,所以我相信该部分可以改进很多。

或者,您可以关闭自动填充模式并使用m-q格式化段落。我不喜欢自动填充的跳跃性,所以我在每种模式下都使用它。

明白了。看看这个:

(defun in-no-auto-fill-region ()
  (> (save-excursion (or (search-backward "%%% BEGIN NO FILL" (point-min) t) 0))
     (save-excursion (or (search-backward "%%% END NO FILL"   (point-min) t) 0))
     ))

(defun previous-line-checking-auto-fill (arg)
  (interactive "P")
  (previous-line arg)
  (if (in-no-auto-fill-region)
      (turn-off-auto-fill)
    (turn-on-auto-fill)))

(defun next-line-checking-auto-fill (arg)
  (interactive "P")
  (next-line arg)
  (if (in-no-auto-fill-region)
      (turn-off-auto-fill)
    (turn-on-auto-fill)))

(add-hook 'LaTeX-mode-hook
      '(lambda nil
     (local-set-key "C-p" 'previous-line-checking-auto-fill)
     (local-set-key "C-n" 'next-line-checking-auto-fill)
     (auto-fill-mode 1)
     ))

如果您想建议/重新定义所有移动功能,这将有助于:

(defmacro movement-advice (func)
  `(defadvice ,func (after ; run this after the original function is done (and point has moved)
                     ;; Give it a unique name
                     ,(intern (concat (symbol-name func) "-auto-fill-auto-off"))
                     ;; Hopefully this satisfies the arguments of any function we can throw at it
                     (&rest args)
                     ;; turn it on
                     activate
                     )
     "Turn auto-fill-mode on or off automatically."
     (auto-fill-mode (not (in-no-auto-fill-region)))))

(dolist (func '(next-line
                previous-line
                forward-paragraph
                backward-paragraph
                mouse-drag-region
                ;; Whatever you use
                ))
  (eval `(movement-advice ,func)))

好问题!我很想知道自己是否有解决方案。我想我们可能需要自己来写……谢谢。我只是在使用MacOS内置的tex.el模式。我想我需要了解一下AUCtex的情况。你的系统效率很低,但看起来很聪明。筑巢并不重要。我打赌我们可以用光标上下切换自动填充的开关…是的,您可能可以解析文件,添加文本属性或具有点左和点输入属性的覆盖。这样做的一个困难是,您必须保持文本属性的同步,这可能是一个难题。我知道有些折叠模式是这样的。或者你可以建议使用运动命令,但它们有很多,我认为当你键入一个自动填充字符(例如空格)时,它会运行得更频繁。也许你只需要建议某些运动命令就可以了。有没有一个通用的方法来建议运动命令,或者运动命令?我提出了一个解决方案,只需重新绑定^p和^n。似乎没有。例如,table.el使用覆盖来触发表点输入单元格挂钩。如果向前和向后的线是你唯一使用的东西,那么它可能是好的。无论如何,要将其限制为LaTeX模式,而不是所有模式?奇怪的是,这不起作用:(自动填充模式(不是(在无自动填充区域)))你需要这个:(如果(在无自动填充区域)(关闭自动填充)(打开自动填充)))我不知道为什么。我不知道如何只在给定模式下打开建议。我想在LaTeX模式下重新绑定所有内容可能更容易。您可以使用
where is internal
覆盖命令绑定到的任何位置。我以为有一个函数可以做到这一点,但我现在找不到它。奇怪的是,自动填充模式不是这样工作的。对不起,误导了你。我想这就是为什么你应该经常测试的原因:)