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

发布于 2024-08-17 02:03:15 字数 248 浏览 6 评论 0原文

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

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

并且它们之间不会自动填充任何内容。

有人能想出一种方法来做到这一点吗?我需要确定光标是否在该区域内,然后必须切换模式,并且每次光标移动时都需要执行此操作。或者有更好的方法吗?

I use emacs to edit everything. On some of my LateX documents I would like to automatically disable auto-fill mode when I am editing tables and code. Basically, I'd like to have two tags, like:

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

and nothing between them will be autofilled.

Can anybody think of a way to do this? I would need to figure out whether or not the cursor is inside the region and then have to toggle the mode, and would need to do that every time the cursor moved. Or is there a better way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花期渐远 2024-08-24 02:03:15

如果您正在使用 AUCTeX(您应该使用),那么您可能需要查看 LaTeX-indent-environment-list。向此变量添加环境将使(除其他外)Mq 不会重新填充该段落。不幸的是,它似乎不适用于自动填充模式。添加到 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,所以我确信这部分可以改进很多。

If you are using AUCTeX (you should be) then you may want to check out LaTeX-indent-environment-list. Adding an environment to this variable will make it so that (among other things) M-q doesn't refill the paragraph. Unfortunately it doesn't seem work for auto-fill-mode. The following largely untested code added to LaTeX-mode-hook might do what you want.

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

It's very stupid and inefficient, but seems to be fast enough on my machine. It doesn't allow nesting, and requires that you manually mark up all sections that you don't want filled. What I am thinking of adding to my .emacs (until I read your question I didn't realize how much this bugged me) is below which keys off of the current environment so there is no need for special markup (though it only looks at the innermost environment (I'm not sure how much of a problem that will cause in practice)). Combining the two is left as an exercise to the interested reader.

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

I have never used defcustom before so I'm sure that part could be improved quite a bit.

大姐,你呐 2024-08-24 02:03:15

知道了。看看这个:

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

Got it. Check this out:

(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)
     ))
逆蝶 2024-08-24 02:03:15

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

Alternately, you can turn off auto-fill-mode and use M-q to format paragraphs. I don't love auto-fill's jumpiness so I use this in every mode.

蓝海 2024-08-24 02:03:15

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

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

If you want to go the route of advising/redefining all the movement functions, this should help:

(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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文