7

I am able to add few extra sources to helm like this

(setq helm-mini-default-sources '(helm-source-buffers-list
                                  helm-source-recentf
                                  helm-source-dired-recent-dirs
                                  helm-chrome-source
                                  hgs/helm-c-source-stars
                                  hgs/helm-c-source-repos
                                  hgs/helm-c-source-search
                                  helm-source-buffer-not-found))

The last thing i need to add to this is helm-M-x. I just need to add all commands to default sources. By this i can invoke a single function and I can go to anything or i can invoke any command.

But helm-M-x is a function and its source-code doesn't have any sources. Any help on how to achieve this?

Chillar Anand
  • 4,042
  • 1
  • 23
  • 52
  • Why about creating Emacs commands source by yourself? – xuchunyang Jun 30 '15 at 03:09
  • As I'm unable to leave this as a comment due to >50 reputation, I'd just like to add for 2022 helm the only change needed to the above answers is to convert the symbol to a string before pushing onto the alist, else helm-flex--style-score will complain. (push (symbol-name elt) cmds) – TheWoodenPrince May 01 '22 at 23:04

2 Answers2

4
(defvar helm-source-emacs-commands
  (helm-build-sync-source "Emacs commands"
    :candidates (lambda ()
                  (let ((cmds))
                    (mapatoms
                     (lambda (elt) (when (commandp elt) (push elt cmds))))
                    cmds))
    :coerce #'intern-soft
    :action #'command-execute)
  "A simple helm source for Emacs commands.")

;; Try it
(helm :sources helm-source-emacs-commands)
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • i tried to build another source just like this https://gist.github.com/ChillarAnand/23119413409f00b7e995#file-helm-el but it didn't work as expected? – Chillar Anand Jul 01 '15 at 15:08
  • I've tried yours, it works fine, but just for the histroy of Emacs commands, so you have to use `helm-M-x` to run some commands to build that history first, because helm doesn't save history across session by default. – xuchunyang Jul 02 '15 at 02:01
4

Based on xuchunyang's answer, I was able to add helm-M-x to helm sources.

(defvar helm-source-emacs-commands
  (helm-build-sync-source "Emacs commands"
    :candidates (lambda ()
                  (let ((cmds))
                    (mapatoms
                     (lambda (elt) (when (commandp elt) (push elt cmds))))
                    cmds))
    :coerce #'intern-soft
    :action #'command-execute)
  "A simple helm source for Emacs commands.")

(defvar helm-source-emacs-commands-history
  (helm-build-sync-source "Emacs commands history"
    :candidates (lambda ()
                  (let ((cmds))
                    (dolist (elem extended-command-history)
                      (push (intern elem) cmds))
                    cmds))
    :coerce #'intern-soft
    :action #'command-execute)
  "Emacs commands history")

(setq helm-mini-default-sources '(helm-source-emacs-commands-history
                                  helm-source-emacs-commands))
Chillar Anand
  • 4,042
  • 1
  • 23
  • 52