Nicer dispatching on item types.

* magit.el (magit-item-case): New.
(magit-stage-item, magit-unstage-item, magit-ignore-item,
magit-discard-item, magit-visit-item): Use it.
This commit is contained in:
Marius Vollmer 2008-08-24 02:31:49 +03:00
parent 358ed0abd0
commit 322208708e

107
magit.el
View file

@ -279,6 +279,24 @@ Many Magit faces inherit from this one by default."
(current-buffer)) (current-buffer))
(delete-overlay magit-highlight-overlay))))) (delete-overlay magit-highlight-overlay)))))
(defmacro magit-item-case (head &rest body)
(declare (indent 1))
(let ((item (car head))
(info (cadr head))
(opname (caddr head)))
`(let* ((,item (magit-get-item))
(,info (and ,item (magit-item-info ,item))))
(case (and ,item (magit-item-type ,item))
,@body
,@(if opname
`(((nil)
(error "Nothing to %s here." ,opname))
(t
(error "Can't %s a %s." ,opname
(let ((type (magit-item-type ,item)))
(or (get type 'magit-description)
type))))))))))
;;; Sections ;;; Sections
(defun magit-insert-section (section title washer cmd &rest args) (defun magit-insert-section (section title washer cmd &rest args)
@ -703,31 +721,27 @@ Please see the manual for a complete description of Magit.
(defun magit-stage-item () (defun magit-stage-item ()
"Add the item at point to the staging area." "Add the item at point to the staging area."
(interactive) (interactive)
(let ((item (magit-get-item))) (magit-item-case (item info "stage")
(if item ((untracked-file)
(case (magit-item-type item) (magit-run "git" "add" info))
((untracked-file) ((hunk)
(magit-run "git" "add" (magit-item-info item))) (if (magit-hunk-item-is-conflict-p item)
((hunk) (error "Can't stage individual resolution hunks. Please stage the whole file."))
(if (magit-hunk-item-is-conflict-p item) (magit-write-hunk-item-patch item ".git/magit-tmp")
(error "Can't stage individual resolution hunks. Please stage the whole file.")) (magit-run "git" "apply" "--cached" ".git/magit-tmp"))
(magit-write-hunk-item-patch item ".git/magit-tmp") ((diff)
(magit-run "git" "apply" "--cached" ".git/magit-tmp")) (magit-run "git" "add" (magit-diff-or-hunk-item-file item)))))
((diff)
(magit-run "git" "add" (magit-diff-or-hunk-item-file item)))))))
(defun magit-unstage-item () (defun magit-unstage-item ()
"Remove the item at point from the staging area." "Remove the item at point from the staging area."
(interactive) (interactive)
(let ((item (magit-get-item))) (magit-item-case (item info "unstage")
(if item ((hunk)
(case (magit-item-type item) (magit-write-hunk-item-patch item ".git/magit-tmp")
((hunk) (magit-run "git" "apply" "--cached" "--reverse" ".git/magit-tmp"))
(magit-write-hunk-item-patch item ".git/magit-tmp") ((diff)
(magit-run "git" "apply" "--cached" "--reverse" ".git/magit-tmp")) (magit-run "git" "reset" "HEAD"
((diff) (magit-diff-or-hunk-item-file item)))))
(magit-run "git" "reset" "HEAD"
(magit-diff-or-hunk-item-file item)))))))
(defun magit-stage-all () (defun magit-stage-all ()
(interactive) (interactive)
@ -1055,41 +1069,34 @@ Please see the manual for a complete description of Magit.
(defun magit-ignore-item () (defun magit-ignore-item ()
(interactive) (interactive)
(let ((item (magit-get-item))) (magit-item-case (item info "ignore")
(if item ((untracked-file)
(case (magit-item-type item) (append-to-file (concat "/" info "\n")
((untracked-file) nil ".gitignore")
(append-to-file (concat "/" (magit-item-info item) "\n") (magit-update-status (magit-find-status-buffer)))))
nil ".gitignore")
(magit-update-status (magit-find-status-buffer)))))))
(defun magit-discard-item () (defun magit-discard-item ()
(interactive) (interactive)
(let ((item (magit-get-item))) (magit-item-case (item info "discard")
(if item ((untracked-file)
(case (magit-item-type item) (if (yes-or-no-p (format "Delete file %s? " info))
((untracked-file) (magit-run "rm" info)))))
(let ((file (magit-item-info item)))
(if (yes-or-no-p (format "Delete file %s? " file))
(magit-run "rm" file))))))))
(defun magit-visit-item () (defun magit-visit-item ()
(interactive) (interactive)
(let ((item (magit-get-item))) (magit-item-case (item info)
(if item ((untracked-file)
(case (magit-item-type item) (find-file info))
((untracked-file) ((diff hunk)
(find-file (magit-item-info item))) (let ((file (magit-diff-or-hunk-item-file item))
((diff hunk) (line (if (eq (magit-item-type item) 'hunk)
(let ((file (magit-diff-or-hunk-item-file item)) (magit-hunk-item-target-line item)
(line (if (eq (magit-item-type item) 'hunk) nil)))
(magit-hunk-item-target-line item) (find-file file)
nil))) (if line
(find-file file) (goto-line line))))
(if line ((commit)
(goto-line line)))) (magit-show-commit))))
((commit)
(magit-show-commit))))))
(defun magit-describe-item () (defun magit-describe-item ()
(interactive) (interactive)