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

Helm exit current process and run a new helm process with same pattern as before #2437

Closed
zw963 opened this issue Aug 11, 2021 · 18 comments
Closed

Comments

@zw963
Copy link
Contributor

zw963 commented Aug 11, 2021

Following is part of config helm.

(defun helm-quit-and-helm-do-ag-current-directory ()
  "Drop into `helm-do-ag-current-directory' from `helm'."
  (interactive)
  (with-helm-alive-p
    (helm-run-after-exit (lambda () (helm-do-ag default-directory)))
    ))

(global-set-key [(control r)] 'helm-do-ag-this-file)
(define-key helm-ag-map [(control r)] 'helm-quit-and-helm-do-ag-current-directory)

We assume current in enh-ruby-mode, cursor on following @dist, and set mark region on it.

image

Then i press Ctrl+r, i get expected behavior like this:

image

But, i want to search @dist on current folder instead of only current file, so i press Ctrl+r again(will invoke 'helm-quit-and-helm-do-ag-current-directory i defined on above)

Expected behavior

expect to search @dist in current folder.

Actual behavior (from emacs-helm.sh if possible, see note above)

search dist (without @ prefix) in current folder

image

Describe versions of Helm, Emacs, operating system, etc.

Compiled emacs 27.2, Helm lastest (08e95db), Arch linux

Are you using emacs-helm.sh to reproduce this bug? (yes/no):

no

@zw963
Copy link
Contributor Author

zw963 commented Aug 11, 2021

After test, it seem like when following code is executing, current keyword will expand to match a symbol instead of original.

(with-helm-alive-p
(helm-run-after-exit (lambda () (helm-do-ag default-directory)))
)

e.g. when i search setup in setup_dirs, when invoke Ctrl+R again, keyboard will change to setup_dirs automatically.

@thierryvolpiatto
Copy link
Member

thierryvolpiatto commented Aug 11, 2021 via email

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

@thierryvolpiatto , sorry for late reply.

I consider this is a issue about helm-run-after-exit not pass default marked input into helm-do-ag function, so, helm-ag select a default fallback behavior, which is set by helm-ag-insert-at-point variable, it can set values same as thing-at-point.

as the code here,

https://github.com/emacsorphanage/helm-ag/blob/master/helm-ag.el#L1320-L1321, because helm-ag--marked-input not result, it select to use helm-ag--insert-thing-at-point.

so, could you please help on how to pass original marked input into helm-ag when run with helm-run-after-exit ?

Thank you.

@thierryvolpiatto
Copy link
Member

thierryvolpiatto commented Aug 14, 2021 via email

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

That said you are trying to implement with convoluted code + external package usage what is already implemented in Helm.

Yes, maybe, sorry, followign is some feature i don't know helm if provided by offical helm-do-grep-ag

  1. helm-ag--do-ag-up-one-level, if i can't search result with in current folder, i can search it from upper level folder instead use Ctrl+l, then continue Ctrl+l ...

  2. helm-do-ag can use follow mode, when selection is moving, the opened buffer in another window is moving.

  3. Ctrl+c Ctrl+e switch to edit-mode is useful for replace matched result selectively, as following, i can change content in a individual buffer directly, then Ctrl+c Ctrl+c to apply it to original buffer.
    image

No issue only bad usage.

Yes, i think so too. : ) but, i am no clue about how to pass current keyword into new helm-run-after-exit as &rest ARGS (helm-run-after-exit FUNCTION &rest ARGS), hope more clear explain, i don't know how to export the keyword to next after exit run helm command when do searching, is it possible? and how?

Thank you.

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

BTW, in fact, i try several solution, but both not work ... include (interactive) change to (interactive "p"), add args to helm-quit-and-helm-do-ag-current-directory, etc ...

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

In fact, i consider when i C-r run helm-do-ag-this-file, and second C-r try run helm-quit-and-helm-do-ag-current-directory with in current helm live session, there is no any args is passed into, so there is no way to get the keyword in first helm-do-ag-this-file helm command, right?

this is not a helm issue, but, instead of, it should be a helm feature request? anyway, i search helm-quit-and on helm source code, there exists some matched, e.g helm-quit-and-helm-mini, helm-quit-and-find-file, but none of them pass search keyword in.

@thierryvolpiatto
Copy link
Member

thierryvolpiatto commented Aug 14, 2021 via email

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

As you can see you are passing an anonymous function with no arguments to helm-run-after-exit.

Oops! i am forget to add args to anonymous functions ...

i change your's code to this, becuse helm-do-ag if last args is nil, it will use helm-ag-insert-at-point instead.

(defun helm-quit-and-helm-do-ag-current-directory ()
  "Drop into `helm-do-ag-current-directory' from `helm'."
  (interactive)
  (with-helm-alive-p
    (helm-run-after-exit #'helm-do-ag
                         default-directory nil
                         (with-helm-current-buffer
                           (if (region-active-p)
                               (buffer-substring-no-properties
                                (region-beginning) (region-end)))))))

But, this solution still not work, because region never active. please see screenshot.

image

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

Current the issue is, Is there exist a way to extract the @dist part of the above "pattern: @dist" in the helm minibuffer? because it seem like no region exists anymore after helm buffer is active, or after down/up to select candidate.

@thierryvolpiatto
Copy link
Member

thierryvolpiatto commented Aug 14, 2021 via email

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

So I didn't understand what you want to pass as DEFAULT-INPUT

sorry, let me explain.

What i want to pass as DEFAULT-INPUT is the pattern when i first time run helm-ag, for current case, it is @dist, but, if i not exit helm and want to run another helm-ag command, i expect the previous pattern (@dist) could be passed in, this is not possible for current helm, right?

@zw963
Copy link
Contributor Author

zw963 commented Aug 14, 2021

AFAIK, i guess helm-run-after-exit is not design to work with this situation.

https://github.com/emacs-helm/helm/blob/master/helm.el#L2363-L2365

as above code, it not pass current pattern(keyword i said before) to the function

@zw963 zw963 changed the title Unexpected change current input keyword when use helm-run-after-exit Helm exit current process and run a new helm process with same pattern as before Aug 14, 2021
@thierryvolpiatto
Copy link
Member

thierryvolpiatto commented Aug 14, 2021 via email

@zw963
Copy link
Contributor Author

zw963 commented Aug 15, 2021

It is helm-pattern.

It works, thank you very much.

Following is the working solution:

(defun helm-quit-and-helm-do-ag-current-directory ()
  "Drop into `helm-do-ag-current-directory' from `helm'."
  (interactive)
  (with-helm-alive-p
    (helm-run-after-exit #'helm-do-ag
                         default-directory nil
                         (with-helm-current-buffer
                                 helm-pattern
                               ))))

(global-set-key [(control r)] 'helm-do-ag-this-file)
(define-key helm-ag-map [(control r)] 'helm-quit-and-helm-do-ag-current-directory)
  1. C-r, search in current file
  2. C-r again, search in default directory instead.
  3. C-l, set current parent directory as default directory, search again.
  4. until found expected result.

@zw963
Copy link
Contributor Author

zw963 commented Aug 15, 2021

Following is the complete code for resolve my issue, please check here.

emacsorphanage/helm-ag#381 (comment)

Perhaps someone interested in this.

@thierryvolpiatto
Copy link
Member

thierryvolpiatto commented Aug 15, 2021 via email

@zw963
Copy link
Contributor Author

zw963 commented Aug 15, 2021

Oops, previous solution still have bug .... for decide if still on first time run helm-do-ag buffer, i use (eq 'last-command helm-do-ag-this-file), but it not work if i moving up/down, because last-command change to helm-next-line, helm-previous-line.

so, i have to set a toggle variable to decide current if still on first time C-r helm buffer, it is dirty, but works!

(setq helm-do-ag-on-current-directory-p nil)

(defun helm-quit-and-helm-do-ag-on-current-directory ()
  "Drop into `helm-do-ag' on DEFAULT-DIRECTORY from `helm'."
  (interactive)
  (setq helm-do-ag-on-current-directory-p t)
  (with-helm-alive-p
    (helm-run-after-exit #'helm-do-ag default-directory nil helm-pattern)))

(defun advice-up-on-level-corretly-when-run-helm-do-ag-this-file (orig-fun &rest command)
  "If start helm-ag with `helm-do-ag-this-file', `helm-ag--do-ag-up-one-level' not work,
we have to run `helm-do-ag' on DEFAULT-DIRECTORY first, then up one level function start to work."
  (if helm-do-ag-on-current-directory-p
      (apply orig-fun command)
      (helm-quit-and-helm-do-ag-on-current-directory)))

(advice-add #'helm-ag--do-ag-up-one-level :around #'advice-up-on-level-corretly-when-run-helm-do-ag-this-file)
(global-set-key [(control r)] 'helm-do-ag-this-file)
(define-key helm-do-ag-map [(control r)] 'helm-ag--do-ag-up-one-level)
(add-hook 'helm-quit-hook (lambda () (setq helm-do-ag-on-current-directory-p nil)))

Now, this new solution works well for now.

  1. Ctrl+r, run helm-do-ag-this-file
  2. Ctrl+r again, run helm-quit-and-helm-do-ag-on-current-directory, that is, search on current default directory instead.
  3. Ctrl+r again, run helm-ag--do-ag-up-one-level, that is, search on parent directory.
  4. Ctrl+r again to repeat 3

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

No branches or pull requests

2 participants