I want to create a modified copy of re-search-forward and advise this copy to be sensitive to isearch-filter-predicate. I tried:

(defun make-search-sensitive-to-ifpmod-advice (orig-fun &rest args)
  "Temporary advice the search functions to make them sensitive to
  (let ((POINT (point)))
    (catch 'filtered
      (while (apply orig-fun args)
        (let ((B (match-beginning 0))
              (E (match-end 0)))
          ;; 1 - If all points in the region that matches the search
          ;; from the previous "search-command" meet the criteria
          ;; accepted by the filter, then the loop stops (`throw') and
          ;; returns the position `(point)`:
          (when (funcall isearch-filter-predicate B E)
            (throw 'filtered (point)))))
      ;; 2 - If the search is unsuccessful, or does not meet
      ;; the criteria accepted by the filter, then return to the
      ;; starting position and return the value `nil'.
      (goto-char POINT)
(defalias 're-search-forward-ifpmod
  (symbol-function 're-search-forward)
  "Copy of `re-search-forward' function (to be) adviced to obey to
(defalias 'search-forward-regexp-ifpmod 're-search-forward-ifpmod)

(advice-add 're-search-forward-ifpmod

I tested this code and it works but it generates a strange bug: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=69573

Eager macro-expansion failure: (wrong-type-argument integer-or-marker-p nil)

So my question is (regardless of the bug and the issue I mentioned above): Does the usage of defalias combined with symbol-function create an "independent" copy (not an alias) of the original function? If not, how to do that?

Note added. By "independent copy," I mean that a modification, such as an advice, to the original function should not affect the copy of that function. Let's take this code as an example:

(defun test-origin ()
  "Test function"
  (message "origin"))

(defalias 'defalias-copy 'test-origin
  "Alias of `test-origin'")

(defalias 'symbol-function-copy
  (symbol-function 'test-origin)
  "Copy (in my intention) of `test-origin`")

(defun test-override ()
  (message "override"))
  1. If I advice test-origin:
(advice-add 'test-origin

(test-origin)             ; => "override"

(defalias-copy)           ; => "override"

(symbol-function-copy)    ; => "origin"

also defalias-copy get adviced. (This is not good if I want override test-origin with an adviced version of defalias-copy later.)

  1. If I advice defalias-copy:
(advice-remove 'test-origin #'test-override)

(advice-add 'defalias-copy

(test-origin)             ; => "origin"

(defalias-copy)           ; => "override"

(symbol-function-copy)    ; => "origin"
  1. If I advice symbol-function-copy
(advice-remove 'defalias-copy #'test-override)

(advice-add 'symbol-function-copy

(test-origin)             ; => "origin"

(defalias-copy)           ; => "origin"

(symbol-function-copy)    ; => "override"

But due to the bug mentioned above, I suspect that the combination of defalias and symbol-function is not the appropriate way to obtain a copy of a function that is completely independent from the original

  • I thought of using symbol-function based on this: How to copy a function?.

  • My purpose is to advice (override) a function with an adviced copy of the same function.

  • 1
    Please summarize the problem (bug or whatever) with your code. To understand the question readers shouldn't need to follow links.
    – Drew
    Commented Mar 7 at 21:06
  • @Drew I'm not asking about the problem. I'm just asking if my code creates a copy of re-search-forward. The links are there just to justify my question.
    – Gabriele
    Commented Mar 7 at 21:31
  • @Drew I clarified this point in my question (I hope..).
    – Gabriele
    Commented Mar 7 at 21:39

2 Answers 2


No. Nothing about defalias or symbol-function makes a copy of the function.

  • Thanks. So, why do the original function and its copy behave differently? And how can I create (if possible) a copy of a function? (Do I need to open another issue or can I edit the current one?)
    – Gabriele
    Commented Mar 7 at 21:29

You can do this. I think it does what you're looking for.

(defalias 'bar 'forward-char)
(advice-add 'bar :after (lambda (&rest _) (message "AFTER")))
M-x forward-char ; No message "AFTER"
M-x bar          ; Message "AFTER"
(symbol-function 'forward-char) ; => #<subr forward-char>
(symbol-function 'bar) ; => forward-char

IOW, you advise bar, not the original function, forward-char. And you defalias bar to forward-char, not to (symbol-function 'forward-char).

Note this difference:

(defalias 'foo (symbol-function 'forward-char))
(eq (symbol-function 'foo) (symbol-function 'forward-char)) ; => t

(defalias 'bar 'forward-char)
(eq (symbol-function 'bar) (symbol-function 'forward-char)) ; => nil
  • This is not what I asked for. I need a copy of the original function that is completely independent from it. In my tests, if I advice the original function, its alias will also be adviced. I went to symbol-function because of this question emacs.stackexchange.com/q/63118/15606. The point is that I want to override a function with and adviced copy of the same function. I tried and I get this error: apply: Lisp nesting exceeds ‘max-lisp-eval-depth’
    – Gabriele
    Commented Mar 8 at 11:39
  • I edited my question to better explain my purpose.
    – Gabriele
    Commented Mar 8 at 14:14
  • I'm really sorry. But I don't think that I'm evolved or changed my question. I probably didn't explain myself well, so I just tried to further clarify my intention. However, I had already expressed at the beginning that I needed to obtain a copy completely independent from the original function and that a pure defalias was not suitable. I have great respect for the time you dedicate to helping users who are not very experienced like me, and I apologize again for the misunderstanding.
    – Gabriele
    Commented Mar 8 at 17:58
  • I checked and this sentence was already present in the first version of my question: So my question is: Is it correct the use of defalias combined with symbol-function to create an "independent" COPY (not an ALIAS) of the original function?. My fault, I thought I had explained myself well.
    – Gabriele
    Commented Mar 8 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.