Function: diff-mode

diff-mode is an autoloaded, interactive and byte-compiled function defined in diff-mode.el.gz.

Signature

(diff-mode)

Documentation

Major mode for viewing/editing context diffs.

Supports unified and context diffs as well as (to a lesser extent) normal diffs.

When the buffer is read-only, the ESC prefix is not necessary. If you edit the buffer manually, diff-mode will try to update the hunk headers for you on-the-fly.

You can also switch between context diff and unified diff with M-x diff-context->unified (diff-context->unified), or vice versa with M-x diff-unified->context (diff-unified->context) and you can also reverse the direction of a diff with M-x diff-reverse-direction (diff-reverse-direction).

C-M-i diff-hunk-next
C-M-x u diff-undo
C-c C-a diff-apply-hunk
C-c C-b diff-refine-hunk
C-c C-c diff-goto-source
C-c C-d diff-unified->context
C-c C-e diff-ediff-patch
C-c C-f next-error-follow-minor-mode(var)/next-error-follow-minor-mode(fun)
C-c C-l diff-refresh-hunk
C-c C-n diff-restrict-view
C-c C-r diff-reverse-direction
C-c C-s diff-split-hunk
C-c C-t diff-test-hunk
C-c C-u diff-context->unified
C-c C-w diff-ignore-whitespace-hunk
C-x 4 A diff-add-change-log-entries-other-window
ESC <backtab> diff-hunk-prev
ESC <mouse-2> diff-goto-source
ESC SPC..~ undefined
M-- negative-argument
M-0 digit-argument
M-1 digit-argument
M-2 digit-argument
M-3 digit-argument
M-4 digit-argument
M-5 digit-argument
M-6 digit-argument
M-7 digit-argument
M-8 digit-argument
M-9 digit-argument
M-< beginning-of-buffer
M-> end-of-buffer
M-? describe-mode
M-A diff-ediff-patch
M-A nil
M-DEL scroll-down-command
M-K diff-file-kill
M-N diff-file-next
M-P diff-file-prev
M-R diff-reverse-direction
M-R nil
M-RET diff-goto-source
M-S-SPC scroll-down-command
M-SPC scroll-up-command
M-W nil
M-W widen
M-g nil
M-g revert-buffer
M-h describe-mode
M-k diff-hunk-kill
M-n diff-hunk-next
M-o diff-goto-source
M-p diff-hunk-prev
M-q nil
M-q quit-window
M-r diff-restrict-view
M-r nil
M-z nil
M-{ diff-file-prev
M-} diff-file-next

This mode runs the hook diff-mode-hook, as the final or penultimate step during initialization.

Probably introduced at or before Emacs version 21.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/diff-mode.el.gz
;;;###autoload
(define-derived-mode diff-mode fundamental-mode "Diff"
  "Major mode for viewing/editing context diffs.
Supports unified and context diffs as well as (to a lesser extent)
normal diffs.

When the buffer is read-only, the ESC prefix is not necessary.
If you edit the buffer manually, `diff-mode' will try to update the hunk
headers for you on-the-fly.

You can also switch between context diff and unified diff with \\[diff-context->unified],
or vice versa with \\[diff-unified->context] and you can also reverse the direction of
a diff with \\[diff-reverse-direction].

\\{diff-mode-map}"

  (setq-local font-lock-defaults diff-font-lock-defaults)
  (add-hook 'font-lock-mode-hook #'diff--font-lock-cleanup nil 'local)
  (setq-local outline-regexp diff-outline-regexp)
  (setq-local imenu-generic-expression
              diff-imenu-generic-expression)
  ;; These are not perfect.  They would be better done separately for
  ;; context diffs and unidiffs.
  ;; (setq-local paragraph-start
  ;;        (concat "@@ "			; unidiff hunk
  ;;           "\\|\\*\\*\\* "		; context diff hunk or file start
  ;;           "\\|--- [^\t]+\t"))	; context or unidiff file
  ;;                                    ; start (first or second line)
  ;;   (setq-local paragraph-separate paragraph-start)
  ;;   (setq-local page-delimiter "--- [^\t]+\t")
  ;; compile support
  (setq-local next-error-function #'diff-next-error)

  (setq-local beginning-of-defun-function #'diff-beginning-of-file-and-junk)
  (setq-local end-of-defun-function #'diff-end-of-file)

  (diff-setup-whitespace)

  (if diff-default-read-only
      (setq buffer-read-only t))
  ;; setup change hooks
  (if (not diff-update-on-the-fly)
      (add-hook 'write-contents-functions #'diff-write-contents-hooks nil t)
    (make-local-variable 'diff-unhandled-changes)
    (add-hook 'after-change-functions #'diff-after-change-function nil t)
    (add-hook 'post-command-hook #'diff-post-command-hook nil t))
  ;; Neat trick from Dave Love to add more bindings in read-only mode:
  (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
    (add-to-list 'minor-mode-overriding-map-alist ro-bind)
    ;; Turn off this little trick in case the buffer is put in view-mode.
    (add-hook 'view-mode-hook
	      (lambda ()
		(setq minor-mode-overriding-map-alist
		      (delq ro-bind minor-mode-overriding-map-alist)))
	      nil t))
  ;; add-log support
  (setq-local add-log-current-defun-function #'diff-current-defun)
  (setq-local add-log-buffer-file-name-function
              (lambda () (diff-find-file-name nil 'noprompt)))
  (add-function :filter-return (local 'filter-buffer-substring-function)
                #'diff--filter-substring)
  (unless buffer-file-name
    (hack-dir-local-variables-non-file-buffer))
  (save-excursion
    (setq-local diff-buffer-type
                (if (re-search-forward "^diff --git" nil t)
                    'git
                  nil))))