emacsorphanage / helm-ag

The silver searcher with helm interface
492 stars 76 forks source link

helm-ag--do-ag-up-one-level not work when run after `helm-do-ag-this-file` #381

Open zw963 opened 3 years ago

zw963 commented 3 years ago
  1. Run helm-do-ag-this-file
  2. Pressing Ctrl+l(helm-ag--do-ag-up-one-level) again and again.
  3. folder is up one level, but search result keep no changes.

Expected behavior

search result should be update when up one level from current file to current folder.

More detail, please check this issue.

zw963 commented 3 years ago

Okay, i resolve this issue finally use a device, let me show me code for a more clear describe.

(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, let me describe how to use it.

  1. C-r first time, invoke helm-do-ag-this-file, search keyboard on current file. image

  2. C-r again, it invoke helm-quit-and-helm-do-ag-current-directory because helm-do-ag-on-current-directory-p is nil

image

  1. C-r again, because helm-do-ag-on-current-directory-p is true, invoke advicedagain, this time, it run original helm-ag--do-ag-up-one-level command, just up one level as expected.

image

  1. repeat step 3. image

My solution is dirty, the reason is, when invoking helm-do-ag-this-file, it not support helm-ag--do-ag-up-one-level to up one level from current file to default directory, if anyone can fix this from helm-ag directly, all above code can be eliminate to only one line.

(define-key helm-do-ag-map [(control r)] 'helm-ag--do-ag-up-one-level)

Or even zero config, if use default C-l to up one level instead.

Thank you.