Commit 43aa38b9 authored by Jonas Bernoulli's avatar Jonas Bernoulli

magit-read-string-ns: new function

Add a new optional argument NO-WHITESPACE to `magit-read-string'.
If it is non-nil then use the new `magit-minibuffer-local-ns-map'
instead of `minibuffer-local-map'.  That map binds "SPC" and "TAB"
to the new command `magit-whitespace-disallowed' which informs the
user that whitespace is disallowed.  If the user manages to insert
whitespace through other means, then raise an error.

Add the new function `magit-read-string-ns' which is a wrapper
around `magit-read-string' which always sets NO-WHITESPACE to t.

Use the new `magit-read-string-ns' in most places where previously
`magit-read-string' was used.

Re #2145.
parent 46e2af59
......@@ -38,7 +38,7 @@
"Clone the REPOSITORY to DIRECTORY.
Then show the status buffer for the new repository."
(interactive
(let ((url (magit-read-string "Clone repository")))
(let ((url (magit-read-string-ns "Clone repository")))
(list url (file-name-as-directory
(expand-file-name
(read-directory-name
......@@ -66,8 +66,8 @@ Then show the status buffer for the new repository."
;;;###autoload
(defun magit-remote-add (remote url)
"Add a remote named REMOTE and fetch it."
(interactive (list (magit-read-string "Remote name")
(magit-read-string "Remote url")))
(interactive (list (magit-read-string-ns "Remote name")
(magit-read-string-ns "Remote url")))
(magit-run-git-async "remote" "add" "-f" remote url))
;;;###autoload
......@@ -75,7 +75,7 @@ Then show the status buffer for the new repository."
"Rename the remote named OLD to NEW."
(interactive
(let ((remote (magit-read-remote "Rename remote")))
(list remote (magit-read-string (format "Rename %s to" remote)))))
(list remote (magit-read-string-ns (format "Rename %s to" remote)))))
(unless (string= old new)
(magit-run-git "remote" "rename" old new)))
......@@ -84,7 +84,8 @@ Then show the status buffer for the new repository."
"Change the url of the remote named REMOTE to URL."
(interactive
(let ((remote (magit-read-remote "Set url of remote")))
(list remote (magit-read-string "Url" (magit-get "remote" remote "url")))))
(list remote (magit-read-string-ns
"Url" (magit-get "remote" remote "url")))))
(magit-set url "remote" remote "url"))
;;;###autoload
......
......@@ -190,8 +190,8 @@ When the region is active offer to drop all contained stashes."
;;;###autoload
(defun magit-stash-branch (stash branch)
"Create and checkout a new BRANCH from STASH."
(interactive (list (magit-read-stash "Branch stash" t)
(magit-read-string "Branch name")))
(interactive (list (magit-read-stash "Branch stash" t)
(magit-read-string-ns "Branch name")))
(magit-run-git "stash" "branch" branch stash))
;;; Plumbing
......
......@@ -233,16 +233,54 @@ back to built-in `completing-read' for now." :error)
(format "%s (default %s): " (substring prompt 0 -2) def)
prompt))
(defun magit-read-string (prompt &optional initial-input history default-value)
"Like `read-string' but require non-empty input.
Empty input is only allowed if DEFAULT-VALUE is non-nil in
which case that is returned. Also append \": \" to PROMPT."
(let ((reply (read-string (magit-prompt-with-default
(concat prompt ": ") default-value)
initial-input history default-value)))
(if (string= reply "")
(user-error "Need non-empty input")
reply)))
(defvar magit-minibuffer-local-ns-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map "\s" 'magit-whitespace-disallowed)
(define-key map "\t" 'magit-whitespace-disallowed)
map))
(defun magit-whitespace-disallowed ()
"Beep to tell the user that whitespace is not allowed."
(interactive)
(ding)
(message "Whitespace isn't allowed here")
(setq defining-kbd-macro nil)
(force-mode-line-update))
(defun magit-read-string (prompt &optional initial-input history default-value
inherit-input-method no-whitespace)
"Read a string from the minibuffer, prompting with string PROMPT.
This is similar to `read-string', but
* empty input is only allowed if DEFAULT-VALUE is non-nil in
which case that is returned,
* whitespace is not allowed if NO-WHITESPACE is non-nil,
* \": \" is appended to PROMPT, and
* an invalid DEFAULT-VALUE is silently ignored."
(when default-value
(when (consp default-value)
(setq default-value (car default-value)))
(unless (stringp default-value)
(setq default-value nil)))
(let* ((minibuffer-completion-table nil)
(val (read-from-minibuffer
(magit-prompt-with-default (concat prompt ": ") default-value)
initial-input (and no-whitespace magit-minibuffer-local-ns-map)
nil history default-value inherit-input-method)))
(when (and (string= val "") default-value)
(setq val default-value))
(cond ((string= val "")
(user-error "Need non-empty input"))
((and no-whitespace (string-match-p "[\s\t\n]" val))
(user-error "Input contains whitespace"))
(t val))))
(defun magit-read-string-ns (prompt &optional initial-input history
default-value inherit-input-method)
"Call `magit-read-string' with non-nil NO-WHITESPACE."
(magit-read-string prompt initial-input history default-value
inherit-input-method t))
(defmacro magit-read-char-case (prompt verbose &rest clauses)
(declare (indent 2)
......
......@@ -1164,13 +1164,13 @@ changes.
(magit-branch-read-upstream-first
(setq start (magit-read-branch-or-commit (concat prompt " starting at")
secondary-default))
(setq branch (magit-read-string
(setq branch (magit-read-string-ns
"Branch name"
(and (member start (magit-list-remote-branch-names))
(mapconcat #'identity
(cdr (split-string start "/")) "/")))))
(t
(setq branch (magit-read-string "Branch name"))
(setq branch (magit-read-string-ns "Branch name"))
(setq start (magit-read-branch-or-commit (concat prompt " starting at")
secondary-default))))
(unless (magit-branch-p start)
......@@ -1275,7 +1275,7 @@ With prefix, forces the rename even if NEW already exists.
(interactive
(let ((branch (magit-read-local-branch "Rename branch")))
(list branch
(magit-read-string (format "Rename branch '%s' to" branch))
(magit-read-string-ns (format "Rename branch '%s' to" branch))
current-prefix-arg)))
(unless (string= old new)
(magit-run-git-no-revert "branch" (if force "-M" "-m") old new)))
......@@ -1701,7 +1701,7 @@ When there are conflict, then they have to resolved in the
temporary worktree \".git/NOTES_MERGE_WORKTREE\". When
done use `magit-notes-merge-commit' to finish. To abort
use `magit-notes-merge-abort'."
(interactive (list (magit-read-string "Merge reference")))
(interactive (list (magit-read-string-ns "Merge reference")))
(magit-run-git-with-editor "notes" "merge" ref))
(defun magit-notes-merge-commit ()
......@@ -1822,7 +1822,7 @@ based on URL."
(setq path (file-name-as-directory (expand-file-name path)))
(when (member path (list "" default-directory))
(setq path nil)))
(list (magit-read-string
(list (magit-read-string-ns
"Remote url"
(and path (magit-git-repo-p path t)
(let ((default-directory path))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment