Skip to content

Instantly share code, notes, and snippets.

@DivineDominion
Last active March 8, 2022 07:48
Show Gist options
  • Save DivineDominion/d39110eef36d739cbc811801639d1847 to your computer and use it in GitHub Desktop.
Save DivineDominion/d39110eef36d739cbc811801639d1847 to your computer and use it in GitHub Desktop.
Add "Discard", "Stage", and "Unstage" to right-click menu in Magit buffers
(require 'magit)
(defun magit-current-section-menu-label ()
"Menu item label for section at point."
(pcase (magit-diff-scope)
('hunk "Hunk")
('hunks "Hunks")
('file "File")
('files "Files")
('module "Module")
('region "Region")))
(defun magit-context-menu (menu click)
"Populate MENU with commands that perform actions on magit sections at point."
(save-excursion
(mouse-set-point click)
;; Ignore log and commit buffers, only apply to status.
(when (magit-section-match 'status magit-root-section)
(when-let ((section-label (magit-current-section-menu-label)))
(define-key-after menu [magit-separator] menu-bar-separator)
(when (eq 'staged (magit-diff-type))
(define-key-after menu [magit-unstage-thing-at-mouse]
`(menu-item (concat "Unstage " ,section-label)
magit-unstage
:help "Unstage thing at point")))
(when (member (magit-diff-type) '(unstaged untracked))
(define-key-after menu [magit-stage-thing-at-mouse]
`(menu-item (concat "Stage " ,section-label)
magit-stage
:help "Stage thing at point")))
;; `magit-section-match' without 2nd parameter uses section at point automatically.
(when (magit-section-match '(magit-module-section
magit-file-section
magit-hunk-section))
(define-key-after menu [magit-discard-thing-at-mouse]
`(menu-item (concat "Discard " ,section-label)
magit-discard
:help "Discard thing at point"))))))
menu)
(setq-default context-menu-functions
'(context-menu-ffap
magit-context-menu
context-menu-highlight-symbol
occur-context-menu
context-menu-region
context-menu-undo
context-menu-dictionary))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment