Why does the order of applying advice matter?

Shwouchk

org-icomplete is a function that calls ido-completing-read (icr). Instead, I want it to call my function: ido-completing-path-like-read (icplr). This would be easily implemented as advice, except that icplr calls icr, so another advice needs to be applied to icplr to resore the original definition of icr before it is called, to prevent an infinite recursion.

I implemented this in two ways, one of which works, and one of which doesn't - icplr goes into an infinite recursion, calling itself instead of icr. Why does the one not working, not work?

Works:

(defadvice org-icompleting-read (around ido-path-like-completion activate)
  (let ((sh/orig-ido-completing-read (symbol-function 'ido-completing-read)))
    (unwind-protect
        (progn
          (defadvice ido-completing-path-like-read
            (around save-ido-completing-read activate)
            (setf (symbol-function 'ido-completing-read)
                  sh/orig-ido-completing-read)
            ad-do-it
            )
          (setf (symbol-function 'ido-completing-read)
                (symbol-function 'ido-completing-path-like-read))
          ad-do-it
          )
      (setf (symbol-function 'ido-completing-read)
            sh/orig-ido-completing-read)
      (ad-remove-advice 'ido-completing-path-like-read 'around 'save-ido-completing-read)
      ))
  )

Does not work:

(defadvice org-icompleting-read (around ido-path-like-completion activate)
  (let ((sh/orig-ido-completing-read (symbol-function 'ido-completing-read)))
    (unwind-protect
        (progn
          (setf (symbol-function 'ido-completing-read)
                (symbol-function 'ido-completing-path-like-read))
          (defadvice ido-completing-path-like-read
            (around save-ido-completing-read activate)
            (setf (symbol-function 'ido-completing-read)
                  sh/orig-ido-completing-read)
            ad-do-it
            )
          ad-do-it
          )
      (setf (symbol-function 'ido-completing-read)
            sh/orig-ido-completing-read)
      (ad-remove-advice 'ido-completing-path-like-read 'around 'save-ido-completing-read)
      ))
  )

Edit

  1. I want to make it clear that all the mentioned functiond: ido-completing-path-like-read, ido-completing-read, and org-icompleting-read are to be treated (and two of them in fact are) library functions, the definitions of which I do not control.

  2. While I'm open to suggestions for alternative patterns here and would appreciate those, I will only accept an answer that actually answers my question: Why does the first code work and the second doesn't? Thanks for understanding.

Shwouchk

To anyone who may come searching:

The reason that the first one works and the second one doesn't, is not the order of applying advice - it is the order of applying the inner advice relative to:

      (setf (symbol-function 'ido-completing-read)
            (symbol-function 'ido-completing-path-like-read))

The reason this matters is, that if this statement is executed before the advice, then the symbol 'ido-completing-read will not be advised, whereas the symbol 'ido-completing-path-like-read copy will be. If however advice is applied first, then both symbols will refer to the advised versions.

I'm not sure this completely makes sense. I imagined (which is why this conundrum) that the function objects have some additional information attached to them, and advice we applied by way of manipulating this additional information.

Turns out that this is a more basic mechanism - the functions are actually wrapped ad-hoc...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the order of css selectors matter?

From Java

Why does order of mutable borrows matter in Rust?

From Dev

Why does declaration order matter for generic members?

From Dev

Pandas: Why does order of selection matter?

From Dev

Why does the order of template argument substitution matter?

From Dev

Order of calling methods - why does it matter in this example?

From Dev

Why does order in method declaration matter?

From Dev

Why does this library dlopen order matter?

From Dev

why does the order of variable declaring matter?

From Dev

why does order of loop nesting matter python?

From Dev

Why does declaration order matter for generic members?

From Dev

Why does the order of alternatives matter in regex?

From Dev

Why does order in method declaration matter?

From Dev

why does the order of variable declaring matter?

From Dev

why does order of loop nesting matter python?

From Dev

Why does the order of prerequisites matter in a makefile?

From Java

Does annotations order matter?

From Dev

Does order of conditions in $and matter?

From Dev

Why one child class alone flings error ? does order matter?

From Dev

why does order matter in this escaped characters class in sed?

From Dev

Why does order matter in this usage of Observable.merge?

From Dev

Why does order matter when using "data" and "formula" keyword arguments?

From Dev

Why does order of `Object.include` and `Fixnum.prepend` matter?

From Dev

Why does the order of asynchronous and gen.coroutine matter in Tornado?

From Dev

Why does the order of the before_actions declaration matter?

From Dev

Why does order of comparison matter for this apply/lambda inequality?

From Dev

Why does the order of LET statements matter in this Entity Framework query?

From Dev

Spring Security java config - why does order of options matter?

From Dev

Why does loop order matter when there's strided prefetching?

Related Related

  1. 1

    Why does the order of css selectors matter?

  2. 2

    Why does order of mutable borrows matter in Rust?

  3. 3

    Why does declaration order matter for generic members?

  4. 4

    Pandas: Why does order of selection matter?

  5. 5

    Why does the order of template argument substitution matter?

  6. 6

    Order of calling methods - why does it matter in this example?

  7. 7

    Why does order in method declaration matter?

  8. 8

    Why does this library dlopen order matter?

  9. 9

    why does the order of variable declaring matter?

  10. 10

    why does order of loop nesting matter python?

  11. 11

    Why does declaration order matter for generic members?

  12. 12

    Why does the order of alternatives matter in regex?

  13. 13

    Why does order in method declaration matter?

  14. 14

    why does the order of variable declaring matter?

  15. 15

    why does order of loop nesting matter python?

  16. 16

    Why does the order of prerequisites matter in a makefile?

  17. 17

    Does annotations order matter?

  18. 18

    Does order of conditions in $and matter?

  19. 19

    Why one child class alone flings error ? does order matter?

  20. 20

    why does order matter in this escaped characters class in sed?

  21. 21

    Why does order matter in this usage of Observable.merge?

  22. 22

    Why does order matter when using "data" and "formula" keyword arguments?

  23. 23

    Why does order of `Object.include` and `Fixnum.prepend` matter?

  24. 24

    Why does the order of asynchronous and gen.coroutine matter in Tornado?

  25. 25

    Why does the order of the before_actions declaration matter?

  26. 26

    Why does order of comparison matter for this apply/lambda inequality?

  27. 27

    Why does the order of LET statements matter in this Entity Framework query?

  28. 28

    Spring Security java config - why does order of options matter?

  29. 29

    Why does loop order matter when there's strided prefetching?

HotTag

Archive