用于矩形选择的填充区域?

Joh*_*ler 4 emacs

我想做一些像fill-region之类的东西,除了我想选择一个矩形区域并且只包含它里面的文本,并让它保持在矩形边界内.有没有办法做到这一点?

phi*_*ils 7

编辑:啊,我知道我之前遇到过这种能力.

矩形编辑功能cua提供此功能.

首先启用cua-selection-mode.这是CUA中没有复制/剪切/粘贴密钥更改的好东西,因此您可能需要永久保存它:

(cua-selection-mode 1)
Run Code Online (Sandbox Code Playgroud)

然后C-RET标记一个角落,将点移动到对角,并C-q照常填充.C-RET再次退出矩形模式.

CUA的矩形编辑非常棒.在评论中阅读它M-x find-library RET cua-base RET.寻找"CUA矩形支持"标题.

原始答案:

(defun my-fill-rectangle (start end)
  "`fill-region' within the confines of a rectangle."
  (interactive "*r")
  (let* ((indent-tabs-mode nil)
         (content (delete-extract-rectangle start end)))
    (goto-char start)
    (insert-rectangle
     (with-temp-buffer
       (setq indent-tabs-mode nil
             fill-column (length (car content)))
       (insert-rectangle content)
       (fill-region (point-min) (point-max))
       (goto-char (point-max))
       (move-to-column fill-column t)
       (extract-rectangle (point-min) (point))))))
Run Code Online (Sandbox Code Playgroud)