Slip Ahead Logging

It's not your fault at all.

単語の途中に改行が入っていても正しく query-replace する

(M-q などで) fill-paragraph として一行の文字数を制限することはよくある.このとき,日本語の文章では単語の途中へ改行が入ってしまうことがあり,これが様々な問題を引き起こしていた.例えば,あとから表記揺れをなくすために query-replace で単語の置換をやった場合に,途中に改行が入ってしまっていた単語が引っかからなくなり,修正漏れが生じてしまう.

本来であれば fill-paragraph の挙動を修正するのが良いのかもしれないが,今回は単語の途中に一つ改行文字が入っていても正しく動作するような query-replace を作成することで対処した.

delete-if をつかっているので (require 'cl) が必要.

(defun query-replace-ignore-filling (from-string to-string &optional delimited start end)
  "Modified query-replace which also works correctly for filled paragraphs."
  (interactive
   (let ((common
          (query-replace-read-args
           (concat "Query replace"
                   (if current-prefix-arg " word" "")
                   (if (and transient-mark-mode mark-active) " in region" ""))
           nil)))
     (list (nth 0 common) (nth 1 common) (nth 2 common)
           (if (and transient-mark-mode mark-active)
               (region-beginning))
           (if (and transient-mark-mode mark-active)
               (region-end)))))
  (perform-replace
   (mapconcat 'identity
              (delete-if #'(lambda (s) (equal s ""))
                         (split-string from-string ""))
              "\\(\r?\n\\)?")
   to-string t t delimited nil nil start end))