Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to new advice mechanism #23

Open
gongzhitaao opened this issue Feb 27, 2016 · 6 comments
Open

Port to new advice mechanism #23

gongzhitaao opened this issue Feb 27, 2016 · 6 comments

Comments

@gongzhitaao
Copy link

According to Porting Old Advice, advice such as crux-with-region-or-line may need to be updated.

  1. Here is a proposed new advice syntax.

    ;; This is the advising function
    (defun me--ad-with-region-or-line (&rest r)
    "Operate on line or region."
    (if (use-region-p)
        (list (region-beginning) (region-end))
      (list (line-beginning-position) (line-end-position))))

    Either (personally prefer this style)

    (defmacro crux-with-region-or-line (func)
    "When called with no active region, call FUNC on current line."
    `(advice-add ,func :filter-args #'me--ad-with-region-or-line))
    
    (crux-with-region-or-line #'comment-or-uncomment-region)

    Or

    (defmacro crux-with-region-or-line (func)
    "When called with no active region, call FUNC on current line."
    (advice-add func :filter-args #'me--ad-with-region-or-line))
    
    (crux-with-region-or-line comment-or-uncomment-region)
  2. On top of that, since crux-with-region-or-line adds advice, i.e., changes the behaviour of the function, it might be convenient to provide an option to remove the advice.

    (defmacro crux-with-region-or-line (func &optional remove)
    "If not REMOVE, add advice to FUNC, i.e., when called with no
    active region, call FUNC on current line.  Otherwise remove
    advice."
    (if remove
        `(advice-remove ,func #'me--ad-with-region-or-line)
      `(advice-add ,func :filter-args #'me--ad-with-region-or-line)))

    So (crux-with-region-or-line #'comment-or-uncomment-region) adds advice to this function while (crux-with-region-or-line #'comment-or-uncomment-region t) removes the advice.

@bbatsov
Copy link
Owner

bbatsov commented Feb 28, 2016

I'm fine with your suggestions, so PR welcome! :-)

The only small downside is that the new mechanism was introduced in Emacs 24.4, but I'm guessing the majority of people are using it anyways.

@jxs
Copy link

jxs commented Nov 27, 2017

Hi,
his is the status of this? I'm using emacs 25.3.1 and crux-with-region-or-buffer and crux-with-region-or-line are not working, it's related to this?

@bbatsov
Copy link
Owner

bbatsov commented Dec 30, 2017

I think @gongzhitaao never sent this PR, however, the old advice mechanism should work just fine with newer Emacsen.

@bbatsov
Copy link
Owner

bbatsov commented Dec 30, 2017

Ah, actually the PR got stalled by some upstream Emacs bug. My bad.

@gongzhitaao
Copy link
Author

gongzhitaao commented Dec 30, 2017

Really sorry about this dangling submission ☹️ Because of the old bug (if I remember correctly)

you have to activate and deactivate a random mark in a buffer before you can use any function advised by this crux-with-region-or-buffer, otherwise, it will report errors.

And yes, it has something to do with the upstream Emacs implementation.

It is not a big deal but really annoying. And I use some hard-code workaround now

(defun me--ad-with-region-or-line (args)
  "Operate on line or region."
  (if (region-active-p)
      args
    (let ((bol (+ (line-beginning-position) (current-indentation)))
          (eol (line-end-position)))
      (push-mark bol)
      (goto-char eol)
      (list bol eol (nth 2 args)))))

If you need, I could test it and submit a PR, probably sometime next weekend since I'm running a dealine now 😄

@gongzhitaao
Copy link
Author

Now I use this advice again. It seems the new version of emacs still has this bug LOL. So I have to manually push a mark as a workaround...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants