dmenu-M-x

dmenu 补全 M-x

Code:

(defun dmenu-completing-read (prompt strings)
  (let ((temp-stdin-buffer
         (generate-new-buffer " *temp-stdin"))
        (temp-stdout-buffer
         (generate-new-buffer " *temp-stdout")))
    (unwind-protect
        (progn
          (with-current-buffer temp-stdin-buffer
            (dolist (s strings)
              (insert s ?\n))
            (shell-command-on-region (point-min)
                                     (point-max)
                                     (format "dmenu -p '%s'" prompt)
                                     temp-stdout-buffer)
            (with-current-buffer temp-stdout-buffer
              (buffer-substring-no-properties (point-min)
                                              (line-end-position)))))
      (kill-buffer temp-stdin-buffer)
      (kill-buffer temp-stdout-buffer))))

(defun dmenu-M-x (prefix command)
  (interactive
   (list current-prefix-arg
         (intern
          (dmenu-completing-read
           "M-x"
           (all-completions "" obarray #'commandp)))))
  (and (commandp command)               ; Prevent fail of dmenu
       (command-execute command)))

实际用处不大,纯粹为了好玩。

1赞

我之前也搞过一个模拟dmenu功能的

然而觉得用处不大…

1赞

了解。我感兴趣的是一个单独的 GUI 的 Mini buffer,这在比较新的 IDE 和编辑器中很常见。感觉 Emacs 的 Mini buffer 太复杂,用 dmenu 这样的外部程序像 ido/helm/ivy 那样定制 completing-read 感觉不可能。