2013-02-23 3 views
4

Я пытаюсь преобразовать веб-сайт в электронную книгу, и в начале каждой страницы, которую я хочу удалить, есть огромный фрагмент html. Как вы можете себе представить, использование Q не приводит к совпадениям из-за того, что что-то в большом куске не ускользает должным образом. Когда я пытаюсь и повторно задаю проблему, я получаю переполнение стека.Найти и заменить без regexp в dired

Что мне действительно нужно, это найти и заменить в dired без regex, обычным способом M-%. Это возможно?

ответ

6

Поведение регулярного выражения является необязательным в теории, но функция вызывает сложный код, предполагающий в нескольких местах. Я думаю, что самым простым решением является создание копий, которые не устанавливают флагов регулярного выражения при их запуске.

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

Отлично. Только gripe (который, я уверен, случается с regexp one), заключается в том, что когда вы нажимаете! он завершается только для текущего буфера, а не для всех файлов. Тем не менее, 2 минуты удержания! это решение для меня! –

+0

Да, это буквально копия оригинальных функций с несколькими незначительными изменениями, чтобы использовать функциональность без регулярного выражения (и пару «require's», так как другой код загружает необходимые библиотеки). – phils