Skip to content

Commit

Permalink
[maybe] magit-read-char-case: Use read-multiple-choice
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsius committed Feb 22, 2024
1 parent eb3c4ce commit f7a340c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
23 changes: 12 additions & 11 deletions lisp/magit-base.el
Original file line number Diff line number Diff line change
Expand Up @@ -769,18 +769,19 @@ This is similar to `read-string', but
(magit-read-string prompt initial-input history default-value
inherit-input-method t))

(defmacro magit-read-char-case (prompt verbose &rest clauses)
(defmacro magit-read-char-case (prompt show-abort &rest clauses)
(declare (indent 2)
(debug (form form &rest (characterp form body))))
`(prog1 (pcase (read-char-choice
(let ((parts (nconc (list ,@(mapcar #'cadr clauses))
,(and verbose '(list "[C-g] to abort")))))
(concat ,prompt
(mapconcat #'identity (butlast parts) ", ")
", or " (car (last parts)) " "))
',(mapcar #'car clauses))
,@(--map `(,(car it) ,@(cddr it)) clauses))
(message "")))
(debug (form form &rest (characterp form form))))
(let ((cs (gensym)))
`(let ((,cs (list ,@(mapcar (lambda (c) `(list ,@c)) clauses))))
(pcase-exhaustive
(car (read-multiple-choice
,prompt
(mapcar (pcase-lambda (`(,k ,d ,_)) (list k d))
,(if show-abort
`(append ,cs '((?\C-g "to abort" "")))
cs))))
,@(mapcar (pcase-lambda (`(,k ,_ ,v)) (list k v)) clauses)))))

(defun magit-y-or-n-p (prompt &optional action)
"Ask user a \"y or n\" or a \"yes or no\" question using PROMPT.
Expand Down
12 changes: 6 additions & 6 deletions lisp/magit-branch.el
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,12 @@ prompt is confusing."
(pcase (if (or (equal branch target)
(not target))
(magit-read-char-case prompt nil
(?d "[d]etach HEAD & delete" 'detach)
(?a "[a]bort" 'abort))
(magit-read-char-case prompt nil
(?d "[d]etach HEAD & delete" 'detach)
(?c (format "[c]heckout %s & delete" target) 'target)
(?a "[a]bort" 'abort)))
(?d "detach HEAD & delete" 'detach)
(?a "abort" 'abort))
(magit-read-char-case "prompt" nil
(?d "detach HEAD & delete" 'detach)
(?c (format "checkout %s & delete" target) 'target)
(?a "abort" 'abort)))
(`detach (unless (or (equal force '(4))
(member branch force)
(magit-branch-merged-p branch t))
Expand Down
40 changes: 20 additions & 20 deletions lisp/magit-diff.el
Original file line number Diff line number Diff line change
Expand Up @@ -1021,12 +1021,12 @@ and `:slant'."
:always-read t)

(defun magit-diff-select-algorithm (&rest _ignore)
(magit-read-char-case nil t
(?u "[u]nspecified" nil)
(?d "[d]efault" "default")
(?m "[m]inimal" "minimal")
(?p "[p]atience" "patience")
(?h "[h]istogram" "histogram")))
(magit-read-char-case "Algorithm" t
(?u "unspecified" nil)
(?d "default" "default")
(?m "minimal" "minimal")
(?p "patience" "patience")
(?h "histogram" "histogram")))

(transient-define-argument magit-diff:--diff-merges ()
:description "Diff merges"
Expand All @@ -1037,7 +1037,7 @@ and `:slant'."
:always-read t)

(defun magit-diff-select-merges (&rest _ignore)
(magit-read-char-case nil t
(magit-read-char-case nil t ; TODO
(?u "[u]nspecified" nil)
(?o "[o]ff" "off")
(?f "[f]irst-parent" "first-parent")
Expand All @@ -1056,9 +1056,9 @@ and `:slant'."

(defun magit-diff-select-ignore-submodules (&rest _ignored)
(magit-read-char-case "Ignore submodules " t
(?u "[u]ntracked" "untracked")
(?d "[d]irty" "dirty")
(?a "[a]ll" "all")))
(?u "untracked" "untracked")
(?d "dirty" "dirty")
(?a "all" "all")))

(transient-define-argument magit-diff:--color-moved ()
:description "Color moved lines"
Expand All @@ -1069,11 +1069,11 @@ and `:slant'."

(defun magit-diff-select-color-moved-mode (&rest _ignore)
(magit-read-char-case "Color moved " t
(?d "[d]efault" "default")
(?p "[p]lain" "plain")
(?b "[b]locks" "blocks")
(?z "[z]ebra" "zebra")
(?Z "[Z] dimmed-zebra" "dimmed-zebra")))
(?d "default" "default")
(?p "plain" "plain")
(?b "blocks" "blocks")
(?z "zebra" "zebra")
(?Z "dimmed-Zebra" "dimmed-zebra")))

(transient-define-argument magit-diff:--color-moved-ws ()
:description "Whitespace treatment for --color-moved"
Expand All @@ -1084,11 +1084,11 @@ and `:slant'."

(defun magit-diff-select-color-moved-ws-mode (&rest _ignore)
(magit-read-char-case "Ignore whitespace " t
(?i "[i]ndentation" "allow-indentation-change")
(?e "[e]nd of line" "ignore-space-at-eol")
(?s "[s]pace change" "ignore-space-change")
(?a "[a]ll space" "ignore-all-space")
(?n "[n]o" "no")))
(?i "indentation" "allow-indentation-change")
(?e "end of line" "ignore-space-at-eol")
(?s "space change" "ignore-space-change")
(?a "all space" "ignore-all-space")
(?n "no" "no")))

;;;; Setup Commands

Expand Down
6 changes: 3 additions & 3 deletions lisp/magit-refs.el
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ Compared with a branch read from the user."
(interactive)
(setq-local magit-refs-show-commit-count
(magit-read-char-case "Show commit counts for " nil
(?a "[a]ll refs" 'all)
(?b "[b]ranches only" t)
(?n "[n]othing" nil)))
(?a "all refs" 'all)
(?b "branches only" t)
(?n "nothing" nil)))
(magit-refresh))

(defun magit-visit-ref ()
Expand Down

0 comments on commit f7a340c

Please sign in to comment.