helm-M-x 有没有高亮 enabled 的 mode 的功能?

就是像图上 counsel-M-x 高亮了 dashboard-mode 那样, helm-M-x 有没有当某个 mode 是 t 的时候,给一个不同的颜色一类?

Advice 大法好。

https://github.com/abo-abo/swiper/commit/76fff20e6774f777787902030e9dcb528682078a 抄一个,face 效果不明显的话就换别的:

(defface helm-active-mode
  '((t :inherit error))
  "Face used by `helm-M-x' for activated modes."
  :group 'helm-faces)

(define-advice helm-M-x-transformer-1
    (:filter-return (candidates) highlight-enabled-mode)
  (mapcar
   (pcase-lambda (`(,text . ,cmd))
     (let* ((sym (intern cmd))
            (alias (symbol-function sym))
            (key (where-is-internal (intern cmd) nil t)))       
       (cons
        (if (or (eq sym major-mode)
                (and
                 (memq sym minor-mode-list)
                 (boundp sym)
                 (buffer-local-value sym helm-current-buffer)))
            (propertize text 'face 'helm-active-mode)
          text)
        cmd)))
   candidates))

最好还给 helm 提个 pr,用 advice 又多迭代了一次,不环保。

1赞

哇!好评!紫薯布丁

稍微修改了一点点(face什么的)

你要去提 pr 吗?不去的话,我去了(

(defface helm-command-active-mode
  '((t :inherit helm-M-x-key))
  "Face used by `helm-M-x' for activated modes."
  :group 'helm-command-faces)

(defun helm-M-x-transformer-1 (candidates &optional sort)
  "Transformer function to show bindings in emacs commands.
Show global bindings and local bindings according to current
`major-mode'.
If SORT is non nil sort list with `helm-generic-sort-fn'.
Note that SORT should not be used when fuzzy matching because
fuzzy matching is running its own sort function with a different
algorithm."
  (with-helm-current-buffer
    (cl-loop with local-map = (helm-M-x-current-mode-map-alist)
          for cand in candidates
          for local-key  = (car (rassq cand local-map))
          for key        = (substitute-command-keys (format "\\[%s]" cand))
          for sym        = (intern cand)
+         for cand       = (if (or (eq sym major-mode)
+                                 (and (memq sym minor-mode-list)
+                                      (boundp sym)
+                                      (buffer-local-value sym helm-current-buffer)))
+                              (propertize cand 'face 'helm-command-active-mode)
+                           cand)
          unless (get (intern (if (consp cand) (car cand) cand)) 'helm-only)
          collect
          (cons (cond ((and (string-match "^M-x" key) local-key)
                       (format "%s (%s)"
                               cand (propertize
                                     local-key
                                     'face 'helm-M-x-key)))
                      ((string-match "^M-x" key) cand)
                      (t (format "%s (%s)"
                                 cand (propertize
                                       key
                                       'face 'helm-M-x-key))))
                cand)
          into ls
          finally return
          (if sort (sort ls #'helm-generic-sort-fn) ls))))

你提吧 :sweet_potato: :custard: