Skip to content
Permalink
Browse files
optimize mergetool workflow
  • Loading branch information
redguardtoo committed Nov 12, 2019
1 parent 3d9f4cf commit f35e749df0a1771a663b2458f1acbf5016b125e3
Showing 5 changed files with 98 additions and 53 deletions.
@@ -142,6 +142,9 @@
(require-init 'init-shackle t)
(require-init 'init-dired t)
(require-init 'init-writting t)

;; ediff configuration should be last so it can override
;; the key bindings in previous configuration
(require-init 'init-ediff)

;; @see https://github.com/hlissner/doom-emacs/wiki/FAQ
@@ -1,9 +1,54 @@
;; -*- coding: utf-8; lexical-binding: t; -*-
(defvar my-ediff-panel-name nil)

(when (and (boundp 'startup-now) startup-now)

(defmacro my-ediff-command (cmd &optional no-arg)
`(lambda (&optional arg)
(interactive "P")
(let* ((w (get-buffer-window)))
;; go to panel window
(select-window (get-buffer-window my-ediff-panel-name))
;; execute ediff command, ignore any error
(condition-case e
(if ,no-arg (funcall ,cmd) (funcall ,cmd arg))
(error
(message "%s" (error-message-string e))))
;; back to original window
(select-window w))))

(unless (featurep 'ediff) (require 'ediff))

;; @see https://stackoverflow.com/a/29757750/245363
(defun ediff-copy-both-to-C (&optional arg)
"Copy code from both A and B to C."
(interactive)
(ediff-copy-diff ediff-current-difference nil 'C nil
(concat
(ediff-get-region-contents ediff-current-difference 'A ediff-control-buffer)
(ediff-get-region-contents ediff-current-difference 'B ediff-control-buffer))))

(my-space-leader-def
"a" (lambda () (interactive (jump-to-register ?a)))
"n" (my-ediff-command 'ediff-next-difference)
"p" (my-ediff-command 'ediff-previous-difference)
"r" (my-ediff-command 'ediff-restore-diff-in-merge-buffer)
"x" (lambda () (interactive) (save-buffers-kill-terminal t))
"1" (my-ediff-command 'ediff-copy-A-to-C)
"2" (my-ediff-command 'ediff-copy-A-to-C)
"3" (my-ediff-command 'ediff-copy-both-to-C))

(defun ediff-startup-hook-setup ()
;; hide contron panel if it's current buffer
(when (string-match-p (buffer-name) "\*Ediff Control Panel.*\*")
(bury-buffer)))
;; hide control panel if it's current buffer
(when (string-match-p (setq my-ediff-panel-name (buffer-name))
"\*Ediff Control Panel.*\*")
;; move to the first difference
(ediff-next-difference)
;; move to the merged buffer window
(winum-select-window-by-number 3)
;; save the windows layout
(window-configuration-to-register ?a)))

(add-hook 'ediff-startup-hook
'ediff-startup-hook-setup))

@@ -433,7 +433,7 @@ If the character before and after CH is space or tab, CH is NOT slash"
(my-comma-leader-def
"bf" 'beginning-of-defun
"bu" 'backward-up-list
"bb" 'back-to-previous-buffer
"bb" (lambda () (interactive) (switch-to-buffer nil)) ; to previous buffer
"ef" 'end-of-defun
"m" 'evil-set-marker
"em" 'erase-message-buffer
@@ -126,10 +126,6 @@
"CMakeLists\\.txt\\'"
"\\.cmake\\'" )

(defun back-to-previous-buffer ()
(interactive)
(switch-to-buffer nil))

;; {{ dictionary setup
(defun my-lookup-dict-org ()
(interactive)
@@ -1354,51 +1350,6 @@ Including indent-buffer, which should not be called automatically on save."
(add-hook 'nov-mode-hook 'nov-mode-hook-setup)
;; }}

(defun narrow-to-region-indirect-buffer-maybe (start end use-indirect-buffer)
"Indirect buffer could multiple widen on same file."
(if (region-active-p) (deactivate-mark))
(if use-indirect-buffer
(with-current-buffer (clone-indirect-buffer
(generate-new-buffer-name
(format "%s-indirect-:%s-:%s"
(buffer-name)
(line-number-at-pos start)
(line-number-at-pos end)))
'display)
(narrow-to-region start end)
(goto-char (point-min)))
(narrow-to-region start end)))

;; {{ @see https://gist.github.com/mwfogleman/95cc60c87a9323876c6c
(defun narrow-or-widen-dwim (&optional use-indirect-buffer)
"If the buffer is narrowed, it widens.
Otherwise, it narrows to region, or Org subtree.
If use-indirect-buffer is not nil, use `indirect-buffer' to hold the widen content."
(interactive "P")
(cond ((buffer-narrowed-p) (widen))
((region-active-p)
(narrow-to-region-indirect-buffer-maybe (region-beginning)
(region-end)
use-indirect-buffer))
((equal major-mode 'org-mode)
(org-narrow-to-subtree))
((derived-mode-p 'diff-mode)
(let* (b e)
(save-excursion
;; If the (point) is already beginning or end of file diff,
;; the `diff-beginning-of-file' and `diff-end-of-file' return nil
(setq b (progn (diff-beginning-of-file) (point)))
(setq e (progn (diff-end-of-file) (point))))
(when (and b e (< b e))
(narrow-to-region-indirect-buffer-maybe b e use-indirect-buffer))))
((derived-mode-p 'prog-mode)
(mark-defun)
(narrow-to-region-indirect-buffer-maybe (region-beginning)
(region-end)
use-indirect-buffer))
(t (error "Please select a region to narrow to"))))
;; }}

;; {{ octave
(add-auto-mode 'octave-mode "\\.m$")
(add-hook 'octave-mode-hook
@@ -306,6 +306,52 @@ you can '(setq my-mplayer-extra-opts \"-ao alsa -vo vdpau\")'.")
(unless (string= (substring signal 0 -1) "finished")
(message "Failed to run \"%s\"." ,command))))))))

;; {{ narrow region
(defun narrow-to-region-indirect-buffer-maybe (start end use-indirect-buffer)
"Indirect buffer could multiple widen on same file."
(if (region-active-p) (deactivate-mark))
(if use-indirect-buffer
(with-current-buffer (clone-indirect-buffer
(generate-new-buffer-name
(format "%s-indirect-:%s-:%s"
(buffer-name)
(line-number-at-pos start)
(line-number-at-pos end)))
'display)
(narrow-to-region start end)
(goto-char (point-min)))
(narrow-to-region start end)))

;; @see https://gist.github.com/mwfogleman/95cc60c87a9323876c6c
(defun narrow-or-widen-dwim (&optional use-indirect-buffer)
"If the buffer is narrowed, it widens.
Otherwise, it narrows to region, or Org subtree.
If use-indirect-buffer is not nil, use `indirect-buffer' to hold the widen content."
(interactive "P")
(cond ((buffer-narrowed-p) (widen))
((region-active-p)
(narrow-to-region-indirect-buffer-maybe (region-beginning)
(region-end)
use-indirect-buffer))
((equal major-mode 'org-mode)
(org-narrow-to-subtree))
((derived-mode-p 'diff-mode)
(let* (b e)
(save-excursion
;; If the (point) is already beginning or end of file diff,
;; the `diff-beginning-of-file' and `diff-end-of-file' return nil
(setq b (progn (diff-beginning-of-file) (point)))
(setq e (progn (diff-end-of-file) (point))))
(when (and b e (< b e))
(narrow-to-region-indirect-buffer-maybe b e use-indirect-buffer))))
((derived-mode-p 'prog-mode)
(mark-defun)
(narrow-to-region-indirect-buffer-maybe (region-beginning)
(region-end)
use-indirect-buffer))
(t (error "Please select a region to narrow to"))))
;; }}

;; reply y/n instead of yes/no
(fset 'yes-or-no-p 'y-or-n-p)

0 comments on commit f35e749

Please sign in to comment.