禁止emacs自动填充选定的区域

时间:2010-01-05 20:29:22

标签: emacs latex elisp

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

   %%% BEGIN NO FILL
   %%% END NO FILL

并且它们之间不会自动填充。

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

4 个答案:

答案 0 :(得分:2)

如果您正在使用AUCTeX(您应该是),那么您可能需要查看LaTeX-indent-environment-list。为这个变量添加一个环境会使它(除其他外)M-q不重新填充段落。不幸的是,它似乎不适用于自动填充模式。添加到LaTeX-mode-hook的以下基本未经测试的代码可能会执行您想要的操作。

(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所以我确信这部分可以改进很多。

答案 1 :(得分:1)

知道了。看看这个:

(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)
     ))

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果你想建议/重新定义所有运动功能的路线,这应该有所帮助:

(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)))