armindarvish / consult-gh

An Interactive interface for "GitHub CLI" client inside GNU Emacs using Consult

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

void variable `consult-preview-key`

LemonBreezes opened this issue · comments

Hello. Basically, the issue is that this package accesses the value of consult-preview-key without loading consult. We need a (require 'consult) statement somewhere before this code:

(defcustom consult-gh-preview-key consult-preview-key
  "What key to use to show preview for consult-gh?

This key is bound in minibuffer, and is similar to `consult-preview-key' (the default) but explicitly for consult-gh. This is used for all categories (repos, issues, prs, codes, files, etc.)"
  :type '(choice (const :tag "Any key" any)
                 (list :tag "Debounced"
                       (const :debounce)
                       (float :tag "Seconds" 0.1)
                       (const any))
                 (const :tag "No preview" nil)
                 (key :tag "Key")
                 (repeat :tag "List of keys" key)))

@LemonBreezes I am confused! Have you not used this package before? You have filed issues that are way past this point. How come all of a sudden you have such basic issue with the package?

Also, a simple search in the issues would point you to #40! and there is (require 'consult) in the code here:

(require 'consult)

and the documentation clearly tells you to "make sure consult is loaded before loading consult-gh." and if you understand that the problem is consult is not loaded, then you should know to load it beofre loading consult-gh in your init! So, what exactly are you suggesting I should be doing here?

@LemonBreezes I am confused! Have you not used this package before? You have filed issues that are way past this point. How come all of a sudden you have such basic issue with the package?

Also, a simple search in the issues would point you to #40! and there is (require 'consult) in the code here:

(require 'consult)

and the documentation clearly tells you to "make sure consult is loaded before loading consult-gh." and if you understand that the problem is consult is not loaded, then you should know to load it beofre loading consult-gh in your init! So, what exactly are you suggesting I should be doing here?

Yes, that require is not evaluated when consult-gh is loaded. I suggest moving (require 'consult) outside of the eval-when-compile block.

Also, I lazy-load consult through Doom Emacs like how thousands of other users do and I would like to lazy-load this package as well.

Ok, can you please share the relevant parts of your doom config?

Ok, can you please share the relevant parts of your doom config?

Ok. https://github.com/doomemacs/doomemacs/blob/master/modules/completion/vertico/config.el#L118 Doom Emacs loads consult only when a consult command is called to reduce startup time. So if I call a consult-gh command before ever calling a consult command, I get the "void variable consult-preview-key" error.

So where is the part where you install consult-gh? are you using use-package and do you have the after: consult in that? Because while we can have the (require consult) in the way you suggested, it still won't matter if consult is not loaded when that line is evaluated and doing it the way you are suggesting had some other issues.

So where is the part where you install consult-gh? are you using use-package and do you have the after: consult in that? Because while we can have the (require consult) in the way you suggested, it still won't matter if consult is not loaded when that line is evaluated and doing it the way you are suggesting had some other issues.

Oh, here is my config for consult-gh: https://github.com/lemonbreezes/cae-emacs/blob/master/modules/cae/vc/config.el#L118. I do not load consult-gh explicitly, only just-in-time when a consult-gh command is called.

I don't know doom syntax that well. Can you add a :after consult to that use-package? Or some equivalent?

Basically when you call consult-gh, consult should be loaded first and then consult-gh otherwise you will keep running into other problems as well.

I don't know doom syntax that well. Can you add a :after consult to that use-package? Or some equivalent?

Basically when you call consult-gh, consult should be loaded first and then consult-gh otherwise you will keep running into other problems as well.

There is no way to hook into a package before it is loaded. Adding :after consult will not have the intended effect because then the consult-gh commands will not be bound until consult is loaded. To illustrate, this is the macro expansion of the use-package! block before and adding :after consult:

(when (modulep! :completion vertico)
  (unless (fboundp 'consult-gh-search-repos)
    (autoload (function consult-gh-search-repos) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-search-code)
    (autoload (function consult-gh-search-code) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-search-prs)
    (autoload (function consult-gh-search-prs) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-search-issues)
    (autoload (function consult-gh-search-issues) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-pr-list)
    (autoload (function consult-gh-pr-list) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-issue-list)
    (autoload (function consult-gh-issue-list) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-default-repos)
    (autoload (function consult-gh-default-repos) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-find-file)
    (autoload (function consult-gh-find-file) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-repo-clone)
    (autoload (function consult-gh-repo-clone) "consult-gh" nil t))
  (unless (fboundp 'consult-gh-repo-fork)
    (autoload (function consult-gh-repo-fork) "consult-gh" nil t))
  (setq consult-gh-prioritize-local-folder 'suggest
        consult-gh-confirm-before-clone nil)
  (let ((vc-prefix (if (modulep! :editor evil) "g" "v")))
    (map! :leader :prefix vc-prefix
          (:prefix-map ("h" . "GitHub") :desc "Search repos" "r"
           (function consult-gh-search-repos) :desc "Search code" "s"
           (function consult-gh-search-code) :desc "Search PRs" "p"
           (function consult-gh-search-prs) :desc "Search issues" "i"
           (function consult-gh-search-issues) :desc "List PRs" "P"
           (function consult-gh-pr-list) :desc "List issues" "I"
           (function consult-gh-issue-list) :desc "Default repos" "d"
           (function consult-gh-default-repos) :desc "Find file" "f"
           (function consult-gh-find-file) :desc "Clone" "c"
           (function consult-gh-repo-clone) :desc "Fork" "k"
           (function consult-gh-fork-current-repo))))
  (after! which-key
    (which-key-add-keymap-based-replacements doom-leader-GitHub-map "g"
      "GitHub CLI" "go" "Organizations" "gc" "Clone repo" "gs" "Search repos"
      "gi" "Search issues" "gf" "Find file" "gk" "Fork repo"))
  (after! (:all which-key consult-gh-embark)
    (which-key-add-keymap-based-replacements
      consult-gh-embark-general-actions-map "r"
      "repo" "l" "link" "b" "add/remove" "bo" "org" "br" "repo" "C" "consult"
      "f" "file"))
  (eval-after-load 'consult-gh
    '(progn
       (setq consult-gh-default-clone-directory "~/src/" consult-gh-show-preview
             t consult-gh-issue-action
             (function consult-gh--issue-browse-url-action)
             consult-gh-repo-action
             (function consult-gh--repo-browse-files-action)
             consult-gh-file-action (function consult-gh--files-view-action)
             consult-gh-preview-buffer-mode (function org-mode)
             consult-gh-default-orgs-list
             '("oantolin" "minad" "alphapapa" "LemonBreezes" "protesilaos"
               "emacs-mirror" "doomemacs" "tecosaur" "systemcrafters"))
       (after! projectile
         (add-hook! 'consult-gh-repo-post-clone-hook
           (defun cae-projectile-discover-projects-in-search-path-h (&rest _)
             (projectile-discover-projects-in-search-path))))
       t)))

And this is after adding :after consult:

(when (modulep! :completion vertico)
  (eval-after-load 'consult
    '(progn
       (unless (fboundp 'consult-gh-search-repos)
         (autoload (function consult-gh-search-repos) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-search-code)
         (autoload (function consult-gh-search-code) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-search-prs)
         (autoload (function consult-gh-search-prs) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-search-issues)
         (autoload (function consult-gh-search-issues) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-pr-list)
         (autoload (function consult-gh-pr-list) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-issue-list)
         (autoload (function consult-gh-issue-list) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-default-repos)
         (autoload (function consult-gh-default-repos) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-find-file)
         (autoload (function consult-gh-find-file) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-repo-clone)
         (autoload (function consult-gh-repo-clone) "consult-gh" nil t))
       (unless (fboundp 'consult-gh-repo-fork)
         (autoload (function consult-gh-repo-fork) "consult-gh" nil t))
       (setq consult-gh-prioritize-local-folder 'suggest
             consult-gh-confirm-before-clone nil)
       (let ((vc-prefix (if (modulep! :editor evil) "g" "v")))
         (map! :leader :prefix vc-prefix
               (:prefix-map ("h" . "GitHub") :desc "Search repos" "r"
                (function consult-gh-search-repos) :desc "Search code" "s"
                (function consult-gh-search-code) :desc "Search PRs" "p"
                (function consult-gh-search-prs) :desc "Search issues" "i"
                (function consult-gh-search-issues) :desc "List PRs" "P"
                (function consult-gh-pr-list) :desc "List issues" "I"
                (function consult-gh-issue-list) :desc "Default repos" "d"
                (function consult-gh-default-repos) :desc "Find file" "f"
                (function consult-gh-find-file) :desc "Clone" "c"
                (function consult-gh-repo-clone) :desc "Fork" "k"
                (function consult-gh-fork-current-repo))))
       (after! which-key
         (which-key-add-keymap-based-replacements doom-leader-GitHub-map "g"
           "GitHub CLI" "go" "Organizations" "gc" "Clone repo" "gs"
           "Search repos" "gi" "Search issues" "gf" "Find file" "gk" "Fork repo"))
       (after! (:all which-key consult-gh-embark)
         (which-key-add-keymap-based-replacements
           consult-gh-embark-general-actions-map "r"
           "repo" "l" "link" "b" "add/remove" "bo" "org" "br" "repo" "C"
           "consult" "f" "file"))
       (eval-after-load 'consult-gh
         '(progn
            (setq consult-gh-default-clone-directory "~/src/"
                  consult-gh-show-preview t consult-gh-issue-action
                  (function consult-gh--issue-browse-url-action)
                  consult-gh-repo-action
                  (function consult-gh--repo-browse-files-action)
                  consult-gh-file-action
                  (function consult-gh--files-view-action)
                  consult-gh-preview-buffer-mode (function org-mode)
                  consult-gh-default-orgs-list
                  '("oantolin" "minad" "alphapapa" "LemonBreezes" "protesilaos"
                    "emacs-mirror" "doomemacs" "tecosaur" "systemcrafters"))
            (after! projectile
              (add-hook! 'consult-gh-repo-post-clone-hook
                (defun cae-projectile-discover-projects-in-search-path-h
                    (&rest _)
                  (projectile-discover-projects-in-search-path))))
            t)))))

I think loading consult at the top of this package's code should not cause any problems.