-;; The following code is related to advice some interactive functions
-;; to make some editing commands in wdired mode not to fail trying to
-;; change read-only text. Notice that some advises advice and unadvise
-;; them-self to another functions: search-forward and
-;; re-search-forward. This is to keep these functions advised only
-;; when is necessary. Since they are built-in commands used heavily in
-;; lots of places, to have it permanently advised would cause some
-;; performance loss.
-
-
-(defun wdired-add-skip-in-replace (command)
- "Advice COMMAND to skip matches while they have read-only properties.
-This is useful to avoid \"read-only\" errors in search and replace
-commands. This advice only has effect in wdired mode."
- (eval
- `(defadvice ,command (around wdired-discard-read-only activate)
- ,(format "Make %s to work better with wdired,\n%s." command
- "skipping read-only matches when invoked without argument")
- ad-do-it
- (if (eq major-mode 'wdired-mode)
- (while (and ad-return-value
- (text-property-any
- (max 1 (1- (match-beginning 0))) (match-end 0)
- 'read-only t))
- ad-do-it))
- ad-return-value)))
-
-
-(defun wdired-add-replace-advice (command)
- "Advice COMMAND to skip matches while they have read-only properties.
-This is useful to avoid \"read-only\" errors in search and replace
-commands. This advice only has effect in wdired mode."
- (eval
- `(defadvice ,command (around wdired-grok-read-only activate)
- ,(format "Make %s to work better with wdired,\n%s." command
- "skipping read-only matches when invoked without argument")
- (if (eq major-mode 'wdired-mode)
- (progn
- (wdired-add-skip-in-replace 'search-forward)
- (wdired-add-skip-in-replace 're-search-forward)
- (unwind-protect
- ad-do-it
- (progn
- (ad-remove-advice 'search-forward
- 'around 'wdired-discard-read-only)
- (ad-remove-advice 're-search-forward
- 'around 'wdired-discard-read-only)
- (ad-update 'search-forward)
- (ad-update 're-search-forward))))
- ad-do-it)
- ad-return-value)))
-
-
-(if wdired-advise-functions
- (progn
- (mapcar 'wdired-add-replace-advice
- '(query-replace query-replace-regexp replace-string))))
-