Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 103 characters in body
Source Link
Gabriele
  • 1.7k
  • 11
  • 22

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

also defalias-copy get adviced.

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

added 1857 characters in body
Source Link
Gabriele
  • 1.7k
  • 11
  • 22

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
            :override
            #'test-override)

(test-origin)             ; => "override"

(defalias-copy)           ; => "override"

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

also defalias-copy get adviced.

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

(advice-add 'defalias-copy
            :override
            #'test-override)


(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
            :override
            #'test-override)

(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.

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
            :override
            #'test-override)

(test-origin)             ; => "override"

(defalias-copy)           ; => "override"

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

also defalias-copy get adviced.

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

(advice-add 'defalias-copy
            :override
            #'test-override)


(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
            :override
            #'test-override)

(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.

added 25 characters in body; edited title
Source Link
Drew
  • 79.1k
  • 10
  • 123
  • 257

Advise a How to create an independent copy (not alias) of a functionfunction's definition?

I want to create a modified COPYcopy 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
`isearch-filter-predicate'"
  (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)
      nil)))
(defalias 're-search-forward-ifpmod
  (symbol-function 're-search-forward)
  "Copy of `re-search-forward' function (to be) adviced to obey to
`isearch-filter-predicate'")
(defalias 'search-forward-regexp-ifpmod 're-search-forward-ifpmod)

(advice-add 're-search-forward-ifpmod
            :around
            #'make-search-sensitive-to-ifpmod-advice)

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): IsDoes the usage of defalias combined with symbol-function to create an "independent" COPY copy (not an ALIASalias) of the original function correct? If not, how to do that?

Advise a copy (not alias) of a function

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
`isearch-filter-predicate'"
  (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)
      nil)))
(defalias 're-search-forward-ifpmod
  (symbol-function 're-search-forward)
  "Copy of `re-search-forward' function (to be) adviced to obey to
`isearch-filter-predicate'")
(defalias 'search-forward-regexp-ifpmod 're-search-forward-ifpmod)

(advice-add 're-search-forward-ifpmod
            :around
            #'make-search-sensitive-to-ifpmod-advice)

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): Is the usage of defalias combined with symbol-function to create an "independent" COPY (not an ALIAS) of the original function correct?

How to create an independent copy of a function's definition?

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
`isearch-filter-predicate'"
  (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)
      nil)))
(defalias 're-search-forward-ifpmod
  (symbol-function 're-search-forward)
  "Copy of `re-search-forward' function (to be) adviced to obey to
`isearch-filter-predicate'")
(defalias 'search-forward-regexp-ifpmod 're-search-forward-ifpmod)

(advice-add 're-search-forward-ifpmod
            :around
            #'make-search-sensitive-to-ifpmod-advice)

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?

added 56 characters in body
Source Link
Gabriele
  • 1.7k
  • 11
  • 22
Loading
deleted 3 characters in body; edited title
Source Link
Drew
  • 79.1k
  • 10
  • 123
  • 257
Loading
added 2 characters in body
Source Link
Gabriele
  • 1.7k
  • 11
  • 22
Loading
Source Link
Gabriele
  • 1.7k
  • 11
  • 22
Loading