Skip to content

Commit 4477ceb

Browse files
Integrate helm-M-x prefix arg in helm-mode
Add a new completion metadata category command-help to handle this feature.
1 parent d8f2602 commit 4477ceb

File tree

1 file changed

+85
-45
lines changed

1 file changed

+85
-45
lines changed

helm-mode.el

+85-45
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,11 @@ that use `helm-comp-read'. See `helm-M-x' for example."
10391039
(symbol-help . (metadata
10401040
(affixation-function . helm-symbol-completion-table-affixation)
10411041
(category . symbol-help)))
1042+
(command-help . (metadata
1043+
(prefix-arg . t)
1044+
(flags . (helm-M-x-prefix-argument))
1045+
(affixation-function . helm-symbol-completion-table-affixation)
1046+
(category . symbol-help)))
10421047
(eww-help . (metadata ;; Emacs-30 only
10431048
(affixation-function . helm-completion-eww-affixation)
10441049
(category . eww-help)))
@@ -1106,7 +1111,7 @@ FLAGS is a list of variables to renitialize to nil when exiting or quitting.")
11061111
("describe-minor-mode" . symbol-help)
11071112
("where-is" . symbol-help)
11081113
("execute-extended-command" . symbol-help)
1109-
("execute-extended-command-for-buffer" . symbol-help)
1114+
("execute-extended-command-for-buffer" . command-help)
11101115
("info-lookup-symbol" . symbol-help)
11111116
("Info-goto-emacs-command-node" . symbol-help)
11121117
("find-library" . library)
@@ -1506,7 +1511,7 @@ dynamically otherwise use `helm-completing-read-default-2'."
15061511
metadata 'display-sort-function)
15071512
(lambda (candidates)
15081513
(sort candidates #'helm-generic-sort-fn)))))
1509-
popup-info flags)
1514+
popup-info flags pref-arg keymap)
15101515
(helm-aif (and (null category)
15111516
(assoc-default name helm-completing-read-command-categories))
15121517
(setq metadata `(metadata (category . ,it))
@@ -1518,54 +1523,89 @@ dynamically otherwise use `helm-completing-read-default-2'."
15181523
(setq metadata it)
15191524
(setq afun (completion-metadata-get metadata 'annotation-function)
15201525
afix (completion-metadata-get metadata 'affixation-function)
1526+
pref-arg (completion-metadata-get metadata 'prefix-arg)
15211527
popup-info (completion-metadata-get metadata 'popup-info-function)
15221528
flags (completion-metadata-get metadata 'flags))))
1529+
(setq keymap (if pref-arg
1530+
(let ((map (make-sparse-keymap)))
1531+
(set-keymap-parent map helm-comp-read-map)
1532+
(define-key map (kbd "C-u") 'helm-M-x-universal-argument)
1533+
map)
1534+
helm-comp-read-map)
1535+
prompt (if pref-arg
1536+
(concat (helm-acase helm-M-x-prefix-argument
1537+
(- "-")
1538+
((guard* (and (consp it) (car it)))
1539+
(if (eq guard 4) "C-u " (format "%d " guard)))
1540+
((guard* (integerp it)) (format "%d " it)))
1541+
prompt)
1542+
prompt)
1543+
helm--mode-line-display-prefarg pref-arg)
1544+
(when pref-arg
1545+
(setq helm-M-x--timer (run-at-time 1 0.1 #'helm-M-x--notify-prefix-arg))
1546+
(setq current-prefix-arg nil)
1547+
(advice-add 'command-execute :around #'helm--advice-command-execute)
1548+
;; Remove command-execute advice when execute-extended-command exit.
1549+
(advice-add 'execute-extended-command :around #'helm--advice-execute-extended-command))
15231550
(unwind-protect
1524-
(helm-comp-read
1525-
prompt collection
1526-
:test test
1527-
:history history
1528-
:reverse-history helm-mode-reverse-history
1529-
:input-history history
1530-
:must-match require-match
1531-
:alistp alistp
1532-
:diacritics helm-mode-ignore-diacritics
1533-
:help-message #'helm-comp-read-help-message
1534-
:name name
1535-
:requires-pattern (if (and (stringp default)
1536-
(string= default "")
1537-
(memq require-match
1538-
'(confirm confirm-after-completion)))
1539-
1 0)
1540-
:fc-transformer
1541-
;; When afun afix and category are nil
1542-
;; helm-completion--decorate returns
1543-
;; candidates (COMPS) unmodified.
1544-
(append (list (lambda (candidates _source)
1545-
(helm-completion--decorate
1546-
(if (and sort-fn (> (length helm-pattern) 0))
1547-
(funcall sort-fn candidates)
1548-
candidates)
1549-
afun afix category)))
1550-
'(helm-cr-default-transformer))
1551-
:popup-info popup-info
1552-
:quit-when-no-cand (eq require-match t)
1553-
:nomark (null helm-comp-read-use-marked)
1554-
:candidates-in-buffer cands-in-buffer
1555-
:get-line (or get-line #'buffer-substring)
1556-
:exec-when-only-one exec-when-only-one
1557-
:fuzzy (eq helm-completion-style 'helm-fuzzy)
1558-
:buffer buffer
1559-
;; If DEF is not provided, fallback to empty string
1560-
;; to avoid `thing-at-point' to be appended on top of list
1561-
:default (or default "")
1562-
;; Fail with special characters (e.g in gnus "nnimap+gmail:")
1563-
;; if regexp-quote is not used.
1564-
;; when init is added to history, it will be unquoted by
1565-
;; helm-comp-read.
1566-
:initial-input initial-input)
1551+
(prog1
1552+
(helm-comp-read
1553+
prompt collection
1554+
:test test
1555+
:keymap keymap
1556+
:history history
1557+
:reverse-history helm-mode-reverse-history
1558+
:input-history history
1559+
:must-match require-match
1560+
:alistp alistp
1561+
:diacritics helm-mode-ignore-diacritics
1562+
:help-message #'helm-comp-read-help-message
1563+
:name name
1564+
:requires-pattern (if (and (stringp default)
1565+
(string= default "")
1566+
(memq require-match
1567+
'(confirm confirm-after-completion)))
1568+
1 0)
1569+
:fc-transformer
1570+
;; When afun afix and category are nil
1571+
;; helm-completion--decorate returns
1572+
;; candidates (COMPS) unmodified.
1573+
(append (list (lambda (candidates _source)
1574+
(helm-completion--decorate
1575+
(if (and sort-fn (> (length helm-pattern) 0))
1576+
(funcall sort-fn candidates)
1577+
candidates)
1578+
afun afix category)))
1579+
'(helm-cr-default-transformer))
1580+
:popup-info popup-info
1581+
:quit-when-no-cand (eq require-match t)
1582+
:nomark (null helm-comp-read-use-marked)
1583+
:candidates-in-buffer cands-in-buffer
1584+
:get-line (or get-line #'buffer-substring)
1585+
:exec-when-only-one exec-when-only-one
1586+
:fuzzy (eq helm-completion-style 'helm-fuzzy)
1587+
:buffer buffer
1588+
;; If DEF is not provided, fallback to empty string
1589+
;; to avoid `thing-at-point' to be appended on top of list
1590+
:default (or default "")
1591+
;; Fail with special characters (e.g in gnus "nnimap+gmail:")
1592+
;; if regexp-quote is not used.
1593+
;; when init is added to history, it will be unquoted by
1594+
;; helm-comp-read.
1595+
:initial-input initial-input)
1596+
(when pref-arg (setq current-prefix-arg helm-current-prefix-arg)))
1597+
(when (timerp helm-M-x--timer)
1598+
(cancel-timer helm-M-x--timer) (setq helm-M-x--timer nil))
15671599
(dolist (f flags) (set f nil)))))
15681600

1601+
(defun helm--advice-command-execute (old--fn &rest args)
1602+
(unless prefix-arg (setq prefix-arg current-prefix-arg))
1603+
(apply old--fn args))
1604+
1605+
(defun helm--advice-execute-extended-command (old--fn &rest args)
1606+
(prog1 (apply old--fn args)
1607+
(advice-remove 'command-execute 'helm--advice-command-execute)))
1608+
15691609
(defun helm-completing-read-default-2
15701610
(prompt collection predicate require-match
15711611
init hist default _inherit-input-method

0 commit comments

Comments
 (0)