gpt4 book ai didi

emacs - 在 dired 中无需正则表达式即可查找和替换

转载 作者:行者123 更新时间:2023-12-02 12:37:02 27 4
gpt4 key购买 nike

我正在尝试将网站转换为电子书,并且每个页面的开头都有一大块我想要删除的 html。正如您可以想象的那样,由于大块中的某些内容未正确转义,因此使用 Q 会导致不匹配。当我尝试用正则表达式解决问题时,出现堆栈溢出。

我真正需要的是一种在没有正则表达式的情况下以通常的 M-% 方式在 dired 中查找和替换的方法。这可能吗?

最佳答案

理论上,正则表达式行为是可选的,但该函数在一些地方调用了有问题的硬编码该假设。我认为最简单的解决方案是制作在运行时不设置正则表达式标志的副本。

(eval-after-load 'dired
'(define-key dired-mode-map (kbd "C-c Q") 'my-dired-do-query-replace))

(defun my-dired-do-query-replace (from to &optional delimited)
"Do `query-replace' of FROM with TO, on all marked files.
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
with the command \\[tags-loop-continue]."
(interactive
(let ((common
(query-replace-read-args
"Query replace in marked files" nil t)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(require 'dired-aux)
(dolist (file (dired-get-marked-files nil nil 'dired-nondirectory-p))
(let ((buffer (get-file-buffer file)))
(if (and buffer (with-current-buffer buffer
buffer-read-only))
(error "File `%s' is visited read-only" file))))
(my-tags-query-replace
from to delimited '(dired-get-marked-files nil nil 'dired-nondirectory-p)))

(defun my-tags-query-replace (from to &optional delimited file-list-form)
"Do `query-replace' of FROM with TO on all files listed in tags table.
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
with the command \\[tags-loop-continue].
Fourth arg FILE-LIST-FORM non-nil means initialize the replacement loop.
Fifth and sixth arguments START and END are accepted, for compatibility
with `query-replace', and ignored.

If FILE-LIST-FORM is non-nil, it is a form to evaluate to
produce the list of files to search.

See also the documentation of the variable `tags-file-name'."
(interactive (query-replace-read-args "Tags query replace" nil t))
(require 'etags)
(setq tags-loop-scan `(let ,(unless (equal from (downcase from))
'((case-fold-search nil)))
(if (search-forward ',from nil t)
;; When we find a match, move back
;; to the beginning of it so perform-replace
;; will see it.
(goto-char (match-beginning 0))))
tags-loop-operate `(perform-replace ',from ',to t nil ',delimited
nil multi-query-replace-map))
(tags-loop-continue (or file-list-form t)))

关于emacs - 在 dired 中无需正则表达式即可查找和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038277/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com