diff-mode.el 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. ;;; diff-mode.el --- a mode for viewing/editing context diffs -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords: convenience patch diff vc
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Provides support for font-lock, outline, navigation
  18. ;; commands, editing and various conversions as well as jumping
  19. ;; to the corresponding source file.
  20. ;; Inspired by Pavel Machek's patch-mode.el (<pavel@@atrey.karlin.mff.cuni.cz>)
  21. ;; Some efforts were spent to have it somewhat compatible with XEmacs's
  22. ;; diff-mode as well as with compilation-minor-mode
  23. ;; Bugs:
  24. ;; - Reverse doesn't work with normal diffs.
  25. ;; Todo:
  26. ;; - Improve `diff-add-change-log-entries-other-window',
  27. ;; it is very simplistic now.
  28. ;;
  29. ;; - Add a `delete-after-apply' so C-c C-a automatically deletes hunks.
  30. ;; Also allow C-c C-a to delete already-applied hunks.
  31. ;;
  32. ;; - Try `diff <file> <hunk>' to try and fuzzily discover the source location
  33. ;; of a hunk. Show then the changes between <file> and <hunk> and make it
  34. ;; possible to apply them to <file>, <hunk-src>, or <hunk-dst>.
  35. ;; Or maybe just make it into a ".rej to diff3-markers converter".
  36. ;; Maybe just use `wiggle' (by Neil Brown) to do it for us.
  37. ;;
  38. ;; - in diff-apply-hunk, strip context in replace-match to better
  39. ;; preserve markers and spacing.
  40. ;; - Handle `diff -b' output in context->unified.
  41. ;;; Code:
  42. (eval-when-compile (require 'cl))
  43. (defvar add-log-buffer-file-name-function)
  44. (defgroup diff-mode ()
  45. "Major mode for viewing/editing diffs."
  46. :version "21.1"
  47. :group 'tools
  48. :group 'diff)
  49. (defcustom diff-default-read-only nil
  50. "If non-nil, `diff-mode' buffers default to being read-only."
  51. :type 'boolean
  52. :group 'diff-mode)
  53. (defcustom diff-jump-to-old-file nil
  54. "Non-nil means `diff-goto-source' jumps to the old file.
  55. Else, it jumps to the new file."
  56. :type 'boolean
  57. :group 'diff-mode)
  58. (defcustom diff-update-on-the-fly t
  59. "Non-nil means hunk headers are kept up-to-date on-the-fly.
  60. When editing a diff file, the line numbers in the hunk headers
  61. need to be kept consistent with the actual diff. This can
  62. either be done on the fly (but this sometimes interacts poorly with the
  63. undo mechanism) or whenever the file is written (can be slow
  64. when editing big diffs)."
  65. :type 'boolean
  66. :group 'diff-mode)
  67. (defcustom diff-advance-after-apply-hunk t
  68. "Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
  69. :type 'boolean
  70. :group 'diff-mode)
  71. (defcustom diff-mode-hook nil
  72. "Run after setting up the `diff-mode' major mode."
  73. :type 'hook
  74. :options '(diff-delete-empty-files diff-make-unified)
  75. :group 'diff-mode)
  76. (defvar diff-vc-backend nil
  77. "The VC backend that created the current Diff buffer, if any.")
  78. (defvar diff-outline-regexp
  79. "\\([*+][*+][*+] [^0-9]\\|@@ ...\\|\\*\\*\\* [0-9].\\|--- [0-9]..\\)")
  80. ;;;;
  81. ;;;; keymap, menu, ...
  82. ;;;;
  83. (easy-mmode-defmap diff-mode-shared-map
  84. '(;; From Pavel Machek's patch-mode.
  85. ("n" . diff-hunk-next)
  86. ("N" . diff-file-next)
  87. ("p" . diff-hunk-prev)
  88. ("P" . diff-file-prev)
  89. ("\t" . diff-hunk-next)
  90. ([backtab] . diff-hunk-prev)
  91. ("k" . diff-hunk-kill)
  92. ("K" . diff-file-kill)
  93. ;; From compilation-minor-mode.
  94. ("}" . diff-file-next)
  95. ("{" . diff-file-prev)
  96. ("\C-m" . diff-goto-source)
  97. ([mouse-2] . diff-goto-source)
  98. ;; From XEmacs's diff-mode.
  99. ("W" . widen)
  100. ;;("." . diff-goto-source) ;display-buffer
  101. ;;("f" . diff-goto-source) ;find-file
  102. ("o" . diff-goto-source) ;other-window
  103. ;;("w" . diff-goto-source) ;other-frame
  104. ;;("N" . diff-narrow)
  105. ;;("h" . diff-show-header)
  106. ;;("j" . diff-show-difference) ;jump to Nth diff
  107. ;;("q" . diff-quit)
  108. ;; Not useful if you have to metafy them.
  109. ;;(" " . scroll-up)
  110. ;;("\177" . scroll-down)
  111. ("A" . diff-ediff-patch)
  112. ("r" . diff-restrict-view)
  113. ("R" . diff-reverse-direction))
  114. "Basic keymap for `diff-mode', bound to various prefix keys."
  115. :inherit special-mode-map)
  116. (easy-mmode-defmap diff-mode-map
  117. `(("\e" . ,(let ((map (make-sparse-keymap)))
  118. ;; We want to inherit most bindings from diff-mode-shared-map,
  119. ;; but not all since they may hide useful M-<foo> global
  120. ;; bindings when editing.
  121. (set-keymap-parent map diff-mode-shared-map)
  122. (dolist (key '("A" "r" "R" "g" "q" "W" "z"))
  123. (define-key map key nil))
  124. map))
  125. ;; From compilation-minor-mode.
  126. ("\C-c\C-c" . diff-goto-source)
  127. ;; By analogy with the global C-x 4 a binding.
  128. ("\C-x4A" . diff-add-change-log-entries-other-window)
  129. ;; Misc operations.
  130. ("\C-c\C-a" . diff-apply-hunk)
  131. ("\C-c\C-e" . diff-ediff-patch)
  132. ("\C-c\C-n" . diff-restrict-view)
  133. ("\C-c\C-s" . diff-split-hunk)
  134. ("\C-c\C-t" . diff-test-hunk)
  135. ("\C-c\C-r" . diff-reverse-direction)
  136. ("\C-c\C-u" . diff-context->unified)
  137. ;; `d' because it duplicates the context :-( --Stef
  138. ("\C-c\C-d" . diff-unified->context)
  139. ("\C-c\C-w" . diff-ignore-whitespace-hunk)
  140. ("\C-c\C-b" . diff-refine-hunk) ;No reason for `b' :-(
  141. ("\C-c\C-f" . next-error-follow-minor-mode))
  142. "Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
  143. (easy-menu-define diff-mode-menu diff-mode-map
  144. "Menu for `diff-mode'."
  145. '("Diff"
  146. ["Jump to Source" diff-goto-source
  147. :help "Jump to the corresponding source line"]
  148. ["Apply hunk" diff-apply-hunk
  149. :help "Apply the current hunk to the source file and go to the next"]
  150. ["Test applying hunk" diff-test-hunk
  151. :help "See whether it's possible to apply the current hunk"]
  152. ["Apply diff with Ediff" diff-ediff-patch
  153. :help "Call `ediff-patch-file' on the current buffer"]
  154. ["Create Change Log entries" diff-add-change-log-entries-other-window
  155. :help "Create ChangeLog entries for the changes in the diff buffer"]
  156. "-----"
  157. ["Reverse direction" diff-reverse-direction
  158. :help "Reverse the direction of the diffs"]
  159. ["Context -> Unified" diff-context->unified
  160. :help "Convert context diffs to unified diffs"]
  161. ["Unified -> Context" diff-unified->context
  162. :help "Convert unified diffs to context diffs"]
  163. ;;["Fixup Headers" diff-fixup-modifs (not buffer-read-only)]
  164. ["Show trailing whitespace" whitespace-mode
  165. :style toggle :selected (bound-and-true-p whitespace-mode)
  166. :help "Show trailing whitespace in modified lines"]
  167. "-----"
  168. ["Split hunk" diff-split-hunk
  169. :active (diff-splittable-p)
  170. :help "Split the current (unified diff) hunk at point into two hunks"]
  171. ["Ignore whitespace changes" diff-ignore-whitespace-hunk
  172. :help "Re-diff the current hunk, ignoring whitespace differences"]
  173. ["Highlight fine changes" diff-refine-hunk
  174. :help "Highlight changes of hunk at point at a finer granularity"]
  175. ["Kill current hunk" diff-hunk-kill
  176. :help "Kill current hunk"]
  177. ["Kill current file's hunks" diff-file-kill
  178. :help "Kill all current file's hunks"]
  179. "-----"
  180. ["Previous Hunk" diff-hunk-prev
  181. :help "Go to the previous count'th hunk"]
  182. ["Next Hunk" diff-hunk-next
  183. :help "Go to the next count'th hunk"]
  184. ["Previous File" diff-file-prev
  185. :help "Go to the previous count'th file"]
  186. ["Next File" diff-file-next
  187. :help "Go to the next count'th file"]
  188. ))
  189. (defcustom diff-minor-mode-prefix "\C-c="
  190. "Prefix key for `diff-minor-mode' commands."
  191. :type '(choice (string "\e") (string "C-c=") string)
  192. :group 'diff-mode)
  193. (easy-mmode-defmap diff-minor-mode-map
  194. `((,diff-minor-mode-prefix . ,diff-mode-shared-map))
  195. "Keymap for `diff-minor-mode'. See also `diff-mode-shared-map'.")
  196. (define-minor-mode diff-auto-refine-mode
  197. "Toggle automatic diff hunk highlighting (Diff Auto Refine mode).
  198. With a prefix argument ARG, enable Diff Auto Refine mode if ARG
  199. is positive, and disable it otherwise. If called from Lisp,
  200. enable the mode if ARG is omitted or nil.
  201. Diff Auto Refine mode is a buffer-local minor mode used with
  202. `diff-mode'. When enabled, Emacs automatically highlights
  203. changes in detail as the user visits hunks. When transitioning
  204. from disabled to enabled, it tries to refine the current hunk, as
  205. well."
  206. :group 'diff-mode :init-value t :lighter nil ;; " Auto-Refine"
  207. (when diff-auto-refine-mode
  208. (condition-case-unless-debug nil (diff-refine-hunk) (error nil))))
  209. ;;;;
  210. ;;;; font-lock support
  211. ;;;;
  212. (defface diff-header
  213. '((((class color) (min-colors 88) (background light))
  214. :background "grey80")
  215. (((class color) (min-colors 88) (background dark))
  216. :background "grey45")
  217. (((class color) (background light))
  218. :foreground "blue1" :weight bold)
  219. (((class color) (background dark))
  220. :foreground "green" :weight bold)
  221. (t :weight bold))
  222. "`diff-mode' face inherited by hunk and index header faces."
  223. :group 'diff-mode)
  224. (define-obsolete-face-alias 'diff-header-face 'diff-header "22.1")
  225. (defvar diff-header-face 'diff-header)
  226. (defface diff-file-header
  227. '((((class color) (min-colors 88) (background light))
  228. :background "grey70" :weight bold)
  229. (((class color) (min-colors 88) (background dark))
  230. :background "grey60" :weight bold)
  231. (((class color) (background light))
  232. :foreground "green" :weight bold)
  233. (((class color) (background dark))
  234. :foreground "cyan" :weight bold)
  235. (t :weight bold)) ; :height 1.3
  236. "`diff-mode' face used to highlight file header lines."
  237. :group 'diff-mode)
  238. (define-obsolete-face-alias 'diff-file-header-face 'diff-file-header "22.1")
  239. (defvar diff-file-header-face 'diff-file-header)
  240. (defface diff-index
  241. '((t :inherit diff-file-header))
  242. "`diff-mode' face used to highlight index header lines."
  243. :group 'diff-mode)
  244. (define-obsolete-face-alias 'diff-index-face 'diff-index "22.1")
  245. (defvar diff-index-face 'diff-index)
  246. (defface diff-hunk-header
  247. '((t :inherit diff-header))
  248. "`diff-mode' face used to highlight hunk header lines."
  249. :group 'diff-mode)
  250. (define-obsolete-face-alias 'diff-hunk-header-face 'diff-hunk-header "22.1")
  251. (defvar diff-hunk-header-face 'diff-hunk-header)
  252. (defface diff-removed
  253. '((t :inherit diff-changed))
  254. "`diff-mode' face used to highlight removed lines."
  255. :group 'diff-mode)
  256. (define-obsolete-face-alias 'diff-removed-face 'diff-removed "22.1")
  257. (defvar diff-removed-face 'diff-removed)
  258. (defface diff-added
  259. '((t :inherit diff-changed))
  260. "`diff-mode' face used to highlight added lines."
  261. :group 'diff-mode)
  262. (define-obsolete-face-alias 'diff-added-face 'diff-added "22.1")
  263. (defvar diff-added-face 'diff-added)
  264. (defface diff-changed
  265. ;; We normally apply a `shadow'-based face on the `diff-context'
  266. ;; face, and keep `diff-changed' the default.
  267. '((((class color grayscale) (min-colors 88)))
  268. ;; If the terminal lacks sufficient colors for shadowing,
  269. ;; highlight changed lines explicitly.
  270. (((class color) (background light))
  271. :foreground "magenta" :weight bold :slant italic)
  272. (((class color) (background dark))
  273. :foreground "yellow" :weight bold :slant italic))
  274. "`diff-mode' face used to highlight changed lines."
  275. :group 'diff-mode)
  276. (define-obsolete-face-alias 'diff-changed-face 'diff-changed "22.1")
  277. (defvar diff-changed-face 'diff-changed)
  278. (defface diff-indicator-removed
  279. '((t :inherit diff-removed))
  280. "`diff-mode' face used to highlight indicator of removed lines (-, <)."
  281. :group 'diff-mode
  282. :version "22.1")
  283. (defvar diff-indicator-removed-face 'diff-indicator-removed)
  284. (defface diff-indicator-added
  285. '((t :inherit diff-added))
  286. "`diff-mode' face used to highlight indicator of added lines (+, >)."
  287. :group 'diff-mode
  288. :version "22.1")
  289. (defvar diff-indicator-added-face 'diff-indicator-added)
  290. (defface diff-indicator-changed
  291. '((t :inherit diff-changed))
  292. "`diff-mode' face used to highlight indicator of changed lines."
  293. :group 'diff-mode
  294. :version "22.1")
  295. (defvar diff-indicator-changed-face 'diff-indicator-changed)
  296. (defface diff-function
  297. '((t :inherit diff-header))
  298. "`diff-mode' face used to highlight function names produced by \"diff -p\"."
  299. :group 'diff-mode)
  300. (define-obsolete-face-alias 'diff-function-face 'diff-function "22.1")
  301. (defvar diff-function-face 'diff-function)
  302. (defface diff-context
  303. '((((class color grayscale) (min-colors 88)) :inherit shadow))
  304. "`diff-mode' face used to highlight context and other side-information."
  305. :group 'diff-mode)
  306. (define-obsolete-face-alias 'diff-context-face 'diff-context "22.1")
  307. (defvar diff-context-face 'diff-context)
  308. (defface diff-nonexistent
  309. '((t :inherit diff-file-header))
  310. "`diff-mode' face used to highlight nonexistent files in recursive diffs."
  311. :group 'diff-mode)
  312. (define-obsolete-face-alias 'diff-nonexistent-face 'diff-nonexistent "22.1")
  313. (defvar diff-nonexistent-face 'diff-nonexistent)
  314. (defconst diff-yank-handler '(diff-yank-function))
  315. (defun diff-yank-function (text)
  316. ;; FIXME: the yank-handler is now called separately on each piece of text
  317. ;; with a yank-handler property, so the next-single-property-change call
  318. ;; below will always return nil :-( --stef
  319. (let ((mixed (next-single-property-change 0 'yank-handler text))
  320. (start (point)))
  321. ;; First insert the text.
  322. (insert text)
  323. ;; If the text does not include any diff markers and if we're not
  324. ;; yanking back into a diff-mode buffer, get rid of the prefixes.
  325. (unless (or mixed (derived-mode-p 'diff-mode))
  326. (undo-boundary) ; Just in case the user wanted the prefixes.
  327. (let ((re (save-excursion
  328. (if (re-search-backward "^[><!][ \t]" start t)
  329. (if (eq (char-after) ?!)
  330. "^[!+- ][ \t]" "^[<>][ \t]")
  331. "^[ <>!+-]"))))
  332. (save-excursion
  333. (while (re-search-backward re start t)
  334. (replace-match "" t t)))))))
  335. (defconst diff-hunk-header-re-unified
  336. "^@@ -\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\+\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? @@")
  337. (defconst diff-context-mid-hunk-header-re
  338. "--- \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? ----$")
  339. (defvar diff-font-lock-keywords
  340. `((,(concat "\\(" diff-hunk-header-re-unified "\\)\\(.*\\)$")
  341. (1 diff-hunk-header-face) (6 diff-function-face))
  342. ("^\\(\\*\\{15\\}\\)\\(.*\\)$" ;context
  343. (1 diff-hunk-header-face) (2 diff-function-face))
  344. ("^\\*\\*\\* .+ \\*\\*\\*\\*". diff-hunk-header-face) ;context
  345. (,diff-context-mid-hunk-header-re . diff-hunk-header-face) ;context
  346. ("^[0-9,]+[acd][0-9,]+$" . diff-hunk-header-face) ;normal
  347. ("^---$" . diff-hunk-header-face) ;normal
  348. ;; For file headers, accept files with spaces, but be careful to rule
  349. ;; out false-positives when matching hunk headers.
  350. ("^\\(---\\|\\+\\+\\+\\|\\*\\*\\*\\) \\([^\t\n]+?\\)\\(?:\t.*\\| \\(\\*\\*\\*\\*\\|----\\)\\)?\n"
  351. (0 diff-header-face)
  352. (2 (if (not (match-end 3)) diff-file-header-face) prepend))
  353. ("^\\([-<]\\)\\(.*\n\\)"
  354. (1 diff-indicator-removed-face) (2 diff-removed-face))
  355. ("^\\([+>]\\)\\(.*\n\\)"
  356. (1 diff-indicator-added-face) (2 diff-added-face))
  357. ("^\\(!\\)\\(.*\n\\)"
  358. (1 diff-indicator-changed-face) (2 diff-changed-face))
  359. ("^Index: \\(.+\\).*\n"
  360. (0 diff-header-face) (1 diff-index-face prepend))
  361. ("^Only in .*\n" . diff-nonexistent-face)
  362. ("^\\(#\\)\\(.*\\)"
  363. (1 font-lock-comment-delimiter-face)
  364. (2 font-lock-comment-face))
  365. ("^[^-=+*!<>#].*\n" (0 diff-context-face))))
  366. (defconst diff-font-lock-defaults
  367. '(diff-font-lock-keywords t nil nil nil (font-lock-multiline . nil)))
  368. (defvar diff-imenu-generic-expression
  369. ;; Prefer second name as first is most likely to be a backup or
  370. ;; version-control name. The [\t\n] at the end of the unidiff pattern
  371. ;; catches Debian source diff files (which lack the trailing date).
  372. '((nil "\\+\\+\\+\\ \\([^\t\n]+\\)[\t\n]" 1) ; unidiffs
  373. (nil "^--- \\([^\t\n]+\\)\t.*\n\\*" 1))) ; context diffs
  374. ;;;;
  375. ;;;; Movement
  376. ;;;;
  377. (defvar diff-valid-unified-empty-line t
  378. "If non-nil, empty lines are valid in unified diffs.
  379. Some versions of diff replace all-blank context lines in unified format with
  380. empty lines. This makes the format less robust, but is tolerated.
  381. See http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01990.html")
  382. (defconst diff-hunk-header-re
  383. (concat "^\\(?:" diff-hunk-header-re-unified ".*\\|\\*\\{15\\}.*\n\\*\\*\\* .+ \\*\\*\\*\\*\\|[0-9]+\\(,[0-9]+\\)?[acd][0-9]+\\(,[0-9]+\\)?\\)$"))
  384. (defconst diff-file-header-re (concat "^\\(--- .+\n\\+\\+\\+ \\|\\*\\*\\* .+\n--- \\|[^-+!<>0-9@* \n]\\).+\n" (substring diff-hunk-header-re 1)))
  385. (defvar diff-narrowed-to nil)
  386. (defun diff-hunk-style (&optional style)
  387. (when (looking-at diff-hunk-header-re)
  388. (setq style (cdr (assq (char-after) '((?@ . unified) (?* . context)))))
  389. (goto-char (match-end 0)))
  390. style)
  391. (defun diff-end-of-hunk (&optional style donttrustheader)
  392. (let (end)
  393. (when (looking-at diff-hunk-header-re)
  394. ;; Especially important for unified (because headers are ambiguous).
  395. (setq style (diff-hunk-style style))
  396. (goto-char (match-end 0))
  397. (when (and (not donttrustheader) (match-end 2))
  398. (let* ((nold (string-to-number (or (match-string 2) "1")))
  399. (nnew (string-to-number (or (match-string 4) "1")))
  400. (endold
  401. (save-excursion
  402. (re-search-forward (if diff-valid-unified-empty-line
  403. "^[- \n]" "^[- ]")
  404. nil t nold)
  405. (line-beginning-position 2)))
  406. (endnew
  407. ;; The hunk may end with a bunch of "+" lines, so the `end' is
  408. ;; then further than computed above.
  409. (save-excursion
  410. (re-search-forward (if diff-valid-unified-empty-line
  411. "^[+ \n]" "^[+ ]")
  412. nil t nnew)
  413. (line-beginning-position 2))))
  414. (setq end (max endold endnew)))))
  415. ;; We may have a first evaluation of `end' thanks to the hunk header.
  416. (unless end
  417. (setq end (and (re-search-forward
  418. (case style
  419. (unified (concat (if diff-valid-unified-empty-line
  420. "^[^-+# \\\n]\\|" "^[^-+# \\]\\|")
  421. ;; A `unified' header is ambiguous.
  422. diff-file-header-re))
  423. (context "^[^-+#! \\]")
  424. (normal "^[^<>#\\]")
  425. (t "^[^-+#!<> \\]"))
  426. nil t)
  427. (match-beginning 0)))
  428. (when diff-valid-unified-empty-line
  429. ;; While empty lines may be valid inside hunks, they are also likely
  430. ;; to be unrelated to the hunk.
  431. (goto-char (or end (point-max)))
  432. (while (eq ?\n (char-before (1- (point))))
  433. (forward-char -1)
  434. (setq end (point)))))
  435. ;; The return value is used by easy-mmode-define-navigation.
  436. (goto-char (or end (point-max)))))
  437. (defun diff-beginning-of-hunk (&optional try-harder)
  438. "Move back to beginning of hunk.
  439. If TRY-HARDER is non-nil, try to cater to the case where we're not in a hunk
  440. but in the file header instead, in which case move forward to the first hunk."
  441. (beginning-of-line)
  442. (unless (looking-at diff-hunk-header-re)
  443. (forward-line 1)
  444. (condition-case ()
  445. (re-search-backward diff-hunk-header-re)
  446. (error
  447. (if (not try-harder)
  448. (error "Can't find the beginning of the hunk")
  449. (diff-beginning-of-file-and-junk)
  450. (diff-hunk-next))))))
  451. (defun diff-unified-hunk-p ()
  452. (save-excursion
  453. (ignore-errors
  454. (diff-beginning-of-hunk)
  455. (looking-at "^@@"))))
  456. (defun diff-beginning-of-file ()
  457. (beginning-of-line)
  458. (unless (looking-at diff-file-header-re)
  459. (let ((start (point))
  460. res)
  461. ;; diff-file-header-re may need to match up to 4 lines, so in case
  462. ;; we're inside the header, we need to move up to 3 lines forward.
  463. (forward-line 3)
  464. (if (and (setq res (re-search-backward diff-file-header-re nil t))
  465. ;; Maybe the 3 lines forward were too much and we matched
  466. ;; a file header after our starting point :-(
  467. (or (<= (point) start)
  468. (setq res (re-search-backward diff-file-header-re nil t))))
  469. res
  470. (goto-char start)
  471. (error "Can't find the beginning of the file")))))
  472. (defun diff-end-of-file ()
  473. (re-search-forward "^[-+#!<>0-9@* \\]" nil t)
  474. (re-search-forward (concat "^[^-+#!<>0-9@* \\]\\|" diff-file-header-re)
  475. nil 'move)
  476. (if (match-beginning 1)
  477. (goto-char (match-beginning 1))
  478. (beginning-of-line)))
  479. ;; Define diff-{hunk,file}-{prev,next}
  480. (easy-mmode-define-navigation
  481. diff-hunk diff-hunk-header-re "hunk" diff-end-of-hunk diff-restrict-view
  482. (if diff-auto-refine-mode
  483. (condition-case-unless-debug nil (diff-refine-hunk) (error nil))))
  484. (easy-mmode-define-navigation
  485. diff-file diff-file-header-re "file" diff-end-of-file)
  486. (defun diff-restrict-view (&optional arg)
  487. "Restrict the view to the current hunk.
  488. If the prefix ARG is given, restrict the view to the current file instead."
  489. (interactive "P")
  490. (save-excursion
  491. (if arg (diff-beginning-of-file) (diff-beginning-of-hunk 'try-harder))
  492. (narrow-to-region (point)
  493. (progn (if arg (diff-end-of-file) (diff-end-of-hunk))
  494. (point)))
  495. (set (make-local-variable 'diff-narrowed-to) (if arg 'file 'hunk))))
  496. (defun diff-hunk-kill ()
  497. "Kill current hunk."
  498. (interactive)
  499. (diff-beginning-of-hunk)
  500. (let* ((start (point))
  501. ;; Search the second match, since we're looking at the first.
  502. (nexthunk (when (re-search-forward diff-hunk-header-re nil t 2)
  503. (match-beginning 0)))
  504. (firsthunk (ignore-errors
  505. (goto-char start)
  506. (diff-beginning-of-file) (diff-hunk-next) (point)))
  507. (nextfile (ignore-errors (diff-file-next) (point)))
  508. (inhibit-read-only t))
  509. (goto-char start)
  510. (if (and firsthunk (= firsthunk start)
  511. (or (null nexthunk)
  512. (and nextfile (> nexthunk nextfile))))
  513. ;; It's the only hunk for this file, so kill the file.
  514. (diff-file-kill)
  515. (diff-end-of-hunk)
  516. (kill-region start (point)))))
  517. ;; "index ", "old mode", "new mode", "new file mode" and
  518. ;; "deleted file mode" are output by git-diff.
  519. (defconst diff-file-junk-re
  520. "diff \\|index \\|\\(?:deleted file\\|new\\(?: file\\)?\\|old\\) mode")
  521. (defun diff-beginning-of-file-and-junk ()
  522. "Go to the beginning of file-related diff-info.
  523. This is like `diff-beginning-of-file' except it tries to skip back over leading
  524. data such as \"Index: ...\" and such."
  525. (let* ((orig (point))
  526. ;; Skip forward over what might be "leading junk" so as to get
  527. ;; closer to the actual diff.
  528. (_ (progn (beginning-of-line)
  529. (while (looking-at diff-file-junk-re)
  530. (forward-line 1))))
  531. (start (point))
  532. (prevfile (condition-case err
  533. (save-excursion (diff-beginning-of-file) (point))
  534. (error err)))
  535. (err (if (consp prevfile) prevfile))
  536. (nextfile (ignore-errors
  537. (save-excursion
  538. (goto-char start) (diff-file-next) (point))))
  539. ;; prevhunk is one of the limits.
  540. (prevhunk (save-excursion
  541. (ignore-errors
  542. (if (numberp prevfile) (goto-char prevfile))
  543. (diff-hunk-prev) (point))))
  544. (previndex (save-excursion
  545. (forward-line 1) ;In case we're looking at "Index:".
  546. (re-search-backward "^Index: " prevhunk t))))
  547. ;; If we're in the junk, we should use nextfile instead of prevfile.
  548. (if (and (numberp nextfile)
  549. (or (not (numberp prevfile))
  550. (and previndex (> previndex prevfile))))
  551. (setq prevfile nextfile))
  552. (if (and previndex (numberp prevfile) (< previndex prevfile))
  553. (setq prevfile previndex))
  554. (if (and (numberp prevfile) (<= prevfile start))
  555. (progn
  556. (goto-char prevfile)
  557. ;; Now skip backward over the leading junk we may have before the
  558. ;; diff itself.
  559. (while (save-excursion
  560. (and (zerop (forward-line -1))
  561. (looking-at diff-file-junk-re)))
  562. (forward-line -1)))
  563. ;; File starts *after* the starting point: we really weren't in
  564. ;; a file diff but elsewhere.
  565. (goto-char orig)
  566. (signal (car err) (cdr err)))))
  567. (defun diff-file-kill ()
  568. "Kill current file's hunks."
  569. (interactive)
  570. (let ((orig (point))
  571. (start (progn (diff-beginning-of-file-and-junk) (point)))
  572. (inhibit-read-only t))
  573. (diff-end-of-file)
  574. (if (looking-at "^\n") (forward-char 1)) ;`tla' generates such diffs.
  575. (if (> orig (point)) (error "Not inside a file diff"))
  576. (kill-region start (point))))
  577. (defun diff-kill-junk ()
  578. "Kill spurious empty diffs."
  579. (interactive)
  580. (save-excursion
  581. (let ((inhibit-read-only t))
  582. (goto-char (point-min))
  583. (while (re-search-forward (concat "^\\(Index: .*\n\\)"
  584. "\\([^-+!* <>].*\n\\)*?"
  585. "\\(\\(Index:\\) \\|"
  586. diff-file-header-re "\\)")
  587. nil t)
  588. (delete-region (if (match-end 4) (match-beginning 0) (match-end 1))
  589. (match-beginning 3))
  590. (beginning-of-line)))))
  591. (defun diff-count-matches (re start end)
  592. (save-excursion
  593. (let ((n 0))
  594. (goto-char start)
  595. (while (re-search-forward re end t) (incf n))
  596. n)))
  597. (defun diff-splittable-p ()
  598. (save-excursion
  599. (beginning-of-line)
  600. (and (looking-at "^[-+ ]")
  601. (progn (forward-line -1) (looking-at "^[-+ ]"))
  602. (diff-unified-hunk-p))))
  603. (defun diff-split-hunk ()
  604. "Split the current (unified diff) hunk at point into two hunks."
  605. (interactive)
  606. (beginning-of-line)
  607. (let ((pos (point))
  608. (start (progn (diff-beginning-of-hunk) (point))))
  609. (unless (looking-at diff-hunk-header-re-unified)
  610. (error "diff-split-hunk only works on unified context diffs"))
  611. (forward-line 1)
  612. (let* ((start1 (string-to-number (match-string 1)))
  613. (start2 (string-to-number (match-string 3)))
  614. (newstart1 (+ start1 (diff-count-matches "^[- \t]" (point) pos)))
  615. (newstart2 (+ start2 (diff-count-matches "^[+ \t]" (point) pos)))
  616. (inhibit-read-only t))
  617. (goto-char pos)
  618. ;; Hopefully the after-change-function will not screw us over.
  619. (insert "@@ -" (number-to-string newstart1) ",1 +"
  620. (number-to-string newstart2) ",1 @@\n")
  621. ;; Fix the original hunk-header.
  622. (diff-fixup-modifs start pos))))
  623. ;;;;
  624. ;;;; jump to other buffers
  625. ;;;;
  626. (defvar diff-remembered-files-alist nil)
  627. (defvar diff-remembered-defdir nil)
  628. (defun diff-filename-drop-dir (file)
  629. (when (string-match "/" file) (substring file (match-end 0))))
  630. (defun diff-merge-strings (ancestor from to)
  631. "Merge the diff between ANCESTOR and FROM into TO.
  632. Returns the merged string if successful or nil otherwise.
  633. The strings are assumed not to contain any \"\\n\" (i.e. end of line).
  634. If ANCESTOR = FROM, returns TO.
  635. If ANCESTOR = TO, returns FROM.
  636. The heuristic is simplistic and only really works for cases
  637. like \(diff-merge-strings \"b/foo\" \"b/bar\" \"/a/c/foo\")."
  638. ;; Ideally, we want:
  639. ;; AMB ANB CMD -> CND
  640. ;; but that's ambiguous if `foo' or `bar' is empty:
  641. ;; a/foo a/foo1 b/foo.c -> b/foo1.c but not 1b/foo.c or b/foo.c1
  642. (let ((str (concat ancestor "\n" from "\n" to)))
  643. (when (and (string-match (concat
  644. "\\`\\(.*?\\)\\(.*\\)\\(.*\\)\n"
  645. "\\1\\(.*\\)\\3\n"
  646. "\\(.*\\(\\2\\).*\\)\\'") str)
  647. (equal to (match-string 5 str)))
  648. (concat (substring str (match-beginning 5) (match-beginning 6))
  649. (match-string 4 str)
  650. (substring str (match-end 6) (match-end 5))))))
  651. (defun diff-tell-file-name (old name)
  652. "Tell Emacs where the find the source file of the current hunk.
  653. If the OLD prefix arg is passed, tell the file NAME of the old file."
  654. (interactive
  655. (let* ((old current-prefix-arg)
  656. (fs (diff-hunk-file-names current-prefix-arg)))
  657. (unless fs (error "No file name to look for"))
  658. (list old (read-file-name (format "File for %s: " (car fs))
  659. nil (diff-find-file-name old 'noprompt) t))))
  660. (let ((fs (diff-hunk-file-names old)))
  661. (unless fs (error "No file name to look for"))
  662. (push (cons fs name) diff-remembered-files-alist)))
  663. (defun diff-hunk-file-names (&optional old)
  664. "Give the list of file names textually mentioned for the current hunk."
  665. (save-excursion
  666. (unless (looking-at diff-file-header-re)
  667. (or (ignore-errors (diff-beginning-of-file))
  668. (re-search-forward diff-file-header-re nil t)))
  669. (let ((limit (save-excursion
  670. (condition-case ()
  671. (progn (diff-hunk-prev) (point))
  672. (error (point-min)))))
  673. (header-files
  674. (if (looking-at "[-*][-*][-*] \\(\\S-+\\)\\(\\s-.*\\)?\n[-+][-+][-+] \\(\\S-+\\)")
  675. (list (if old (match-string 1) (match-string 3))
  676. (if old (match-string 3) (match-string 1)))
  677. (forward-line 1) nil)))
  678. (delq nil
  679. (append
  680. (when (and (not old)
  681. (save-excursion
  682. (re-search-backward "^Index: \\(.+\\)" limit t)))
  683. (list (match-string 1)))
  684. header-files
  685. (when (re-search-backward
  686. "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?"
  687. nil t)
  688. (list (if old (match-string 2) (match-string 4))
  689. (if old (match-string 4) (match-string 2)))))))))
  690. (defun diff-find-file-name (&optional old noprompt prefix)
  691. "Return the file corresponding to the current patch.
  692. Non-nil OLD means that we want the old file.
  693. Non-nil NOPROMPT means to prefer returning nil than to prompt the user.
  694. PREFIX is only used internally: don't use it."
  695. (unless (equal diff-remembered-defdir default-directory)
  696. ;; Flush diff-remembered-files-alist if the default-directory is changed.
  697. (set (make-local-variable 'diff-remembered-defdir) default-directory)
  698. (set (make-local-variable 'diff-remembered-files-alist) nil))
  699. (save-excursion
  700. (unless (looking-at diff-file-header-re)
  701. (or (ignore-errors (diff-beginning-of-file))
  702. (re-search-forward diff-file-header-re nil t)))
  703. (let ((fs (diff-hunk-file-names old)))
  704. (if prefix (setq fs (mapcar (lambda (f) (concat prefix f)) fs)))
  705. (or
  706. ;; use any previously used preference
  707. (cdr (assoc fs diff-remembered-files-alist))
  708. ;; try to be clever and use previous choices as an inspiration
  709. (dolist (rf diff-remembered-files-alist)
  710. (let ((newfile (diff-merge-strings (caar rf) (car fs) (cdr rf))))
  711. (if (and newfile (file-exists-p newfile)) (return newfile))))
  712. ;; look for each file in turn. If none found, try again but
  713. ;; ignoring the first level of directory, ...
  714. (do* ((files fs (delq nil (mapcar 'diff-filename-drop-dir files)))
  715. (file nil nil))
  716. ((or (null files)
  717. (setq file (do* ((files files (cdr files))
  718. (file (car files) (car files)))
  719. ;; Use file-regular-p to avoid
  720. ;; /dev/null, directories, etc.
  721. ((or (null file) (file-regular-p file))
  722. file))))
  723. file))
  724. ;; <foo>.rej patches implicitly apply to <foo>
  725. (and (string-match "\\.rej\\'" (or buffer-file-name ""))
  726. (let ((file (substring buffer-file-name 0 (match-beginning 0))))
  727. (when (file-exists-p file) file)))
  728. ;; If we haven't found the file, maybe it's because we haven't paid
  729. ;; attention to the PCL-CVS hint.
  730. (and (not prefix)
  731. (boundp 'cvs-pcl-cvs-dirchange-re)
  732. (save-excursion
  733. (re-search-backward cvs-pcl-cvs-dirchange-re nil t))
  734. (diff-find-file-name old noprompt (match-string 1)))
  735. ;; if all else fails, ask the user
  736. (unless noprompt
  737. (let ((file (expand-file-name (or (first fs) ""))))
  738. (setq file
  739. (read-file-name (format "Use file %s: " file)
  740. (file-name-directory file) file t
  741. (file-name-nondirectory file)))
  742. (set (make-local-variable 'diff-remembered-files-alist)
  743. (cons (cons fs file) diff-remembered-files-alist))
  744. file))))))
  745. (defun diff-ediff-patch ()
  746. "Call `ediff-patch-file' on the current buffer."
  747. (interactive)
  748. (condition-case nil
  749. (ediff-patch-file nil (current-buffer))
  750. (wrong-number-of-arguments (ediff-patch-file))))
  751. ;;;;
  752. ;;;; Conversion functions
  753. ;;;;
  754. ;;(defvar diff-inhibit-after-change nil
  755. ;; "Non-nil means inhibit `diff-mode's after-change functions.")
  756. (defun diff-unified->context (start end)
  757. "Convert unified diffs to context diffs.
  758. START and END are either taken from the region (if a prefix arg is given) or
  759. else cover the whole buffer."
  760. (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
  761. (list (region-beginning) (region-end))
  762. (list (point-min) (point-max))))
  763. (unless (markerp end) (setq end (copy-marker end t)))
  764. (let (;;(diff-inhibit-after-change t)
  765. (inhibit-read-only t))
  766. (save-excursion
  767. (goto-char start)
  768. (while (and (re-search-forward
  769. (concat "^\\(\\(---\\) .+\n\\(\\+\\+\\+\\) .+\\|"
  770. diff-hunk-header-re-unified ".*\\)$")
  771. nil t)
  772. (< (point) end))
  773. (combine-after-change-calls
  774. (if (match-beginning 2)
  775. ;; we matched a file header
  776. (progn
  777. ;; use reverse order to make sure the indices are kept valid
  778. (replace-match "---" t t nil 3)
  779. (replace-match "***" t t nil 2))
  780. ;; we matched a hunk header
  781. (let ((line1 (match-string 4))
  782. (lines1 (or (match-string 5) "1"))
  783. (line2 (match-string 6))
  784. (lines2 (or (match-string 7) "1"))
  785. ;; Variables to use the special undo function.
  786. (old-undo buffer-undo-list)
  787. (old-end (marker-position end))
  788. (start (match-beginning 0))
  789. (reversible t))
  790. (replace-match
  791. (concat "***************\n*** " line1 ","
  792. (number-to-string (+ (string-to-number line1)
  793. (string-to-number lines1)
  794. -1))
  795. " ****"))
  796. (save-restriction
  797. (narrow-to-region (line-beginning-position 2)
  798. ;; Call diff-end-of-hunk from just before
  799. ;; the hunk header so it can use the hunk
  800. ;; header info.
  801. (progn (diff-end-of-hunk 'unified) (point)))
  802. (let ((hunk (buffer-string)))
  803. (goto-char (point-min))
  804. (if (not (save-excursion (re-search-forward "^-" nil t)))
  805. (delete-region (point) (point-max))
  806. (goto-char (point-max))
  807. (let ((modif nil) last-pt)
  808. (while (progn (setq last-pt (point))
  809. (= (forward-line -1) 0))
  810. (case (char-after)
  811. (?\s (insert " ") (setq modif nil) (backward-char 1))
  812. (?+ (delete-region (point) last-pt) (setq modif t))
  813. (?- (if (not modif)
  814. (progn (forward-char 1)
  815. (insert " "))
  816. (delete-char 1)
  817. (insert "! "))
  818. (backward-char 2))
  819. (?\\ (when (save-excursion (forward-line -1)
  820. (= (char-after) ?+))
  821. (delete-region (point) last-pt) (setq modif t)))
  822. ;; diff-valid-unified-empty-line.
  823. (?\n (insert " ") (setq modif nil) (backward-char 2))
  824. (t (setq modif nil))))))
  825. (goto-char (point-max))
  826. (save-excursion
  827. (insert "--- " line2 ","
  828. (number-to-string (+ (string-to-number line2)
  829. (string-to-number lines2)
  830. -1))
  831. " ----\n" hunk))
  832. ;;(goto-char (point-min))
  833. (forward-line 1)
  834. (if (not (save-excursion (re-search-forward "^+" nil t)))
  835. (delete-region (point) (point-max))
  836. (let ((modif nil) (delete nil))
  837. (if (save-excursion (re-search-forward "^\\+.*\n-" nil t))
  838. ;; Normally, lines in a substitution come with
  839. ;; first the removals and then the additions, and
  840. ;; the context->unified function follows this
  841. ;; convention, of course. Yet, other alternatives
  842. ;; are valid as well, but they preclude the use of
  843. ;; context->unified as an undo command.
  844. (setq reversible nil))
  845. (while (not (eobp))
  846. (case (char-after)
  847. (?\s (insert " ") (setq modif nil) (backward-char 1))
  848. (?- (setq delete t) (setq modif t))
  849. (?+ (if (not modif)
  850. (progn (forward-char 1)
  851. (insert " "))
  852. (delete-char 1)
  853. (insert "! "))
  854. (backward-char 2))
  855. (?\\ (when (save-excursion (forward-line 1)
  856. (not (eobp)))
  857. (setq delete t) (setq modif t)))
  858. ;; diff-valid-unified-empty-line.
  859. (?\n (insert " ") (setq modif nil) (backward-char 2)
  860. (setq reversible nil))
  861. (t (setq modif nil)))
  862. (let ((last-pt (point)))
  863. (forward-line 1)
  864. (when delete
  865. (delete-region last-pt (point))
  866. (setq delete nil)))))))
  867. (unless (or (not reversible) (eq buffer-undo-list t))
  868. ;; Drop the many undo entries and replace them with
  869. ;; a single entry that uses diff-context->unified to do
  870. ;; the work.
  871. (setq buffer-undo-list
  872. (cons (list 'apply (- old-end end) start (point-max)
  873. 'diff-context->unified start (point-max))
  874. old-undo)))))))))))
  875. (defun diff-context->unified (start end &optional to-context)
  876. "Convert context diffs to unified diffs.
  877. START and END are either taken from the region
  878. \(when it is highlighted) or else cover the whole buffer.
  879. With a prefix argument, convert unified format to context format."
  880. (interactive (if (and transient-mark-mode mark-active)
  881. (list (region-beginning) (region-end) current-prefix-arg)
  882. (list (point-min) (point-max) current-prefix-arg)))
  883. (if to-context
  884. (diff-unified->context start end)
  885. (unless (markerp end) (setq end (copy-marker end t)))
  886. (let ( ;;(diff-inhibit-after-change t)
  887. (inhibit-read-only t))
  888. (save-excursion
  889. (goto-char start)
  890. (while (and (re-search-forward "^\\(\\(\\*\\*\\*\\) .+\n\\(---\\) .+\\|\\*\\{15\\}.*\n\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]+\\) \\*\\*\\*\\*\\)$" nil t)
  891. (< (point) end))
  892. (combine-after-change-calls
  893. (if (match-beginning 2)
  894. ;; we matched a file header
  895. (progn
  896. ;; use reverse order to make sure the indices are kept valid
  897. (replace-match "+++" t t nil 3)
  898. (replace-match "---" t t nil 2))
  899. ;; we matched a hunk header
  900. (let ((line1s (match-string 4))
  901. (line1e (match-string 5))
  902. (pt1 (match-beginning 0))
  903. ;; Variables to use the special undo function.
  904. (old-undo buffer-undo-list)
  905. (old-end (marker-position end))
  906. (reversible t))
  907. (replace-match "")
  908. (unless (re-search-forward
  909. diff-context-mid-hunk-header-re nil t)
  910. (error "Can't find matching `--- n1,n2 ----' line"))
  911. (let ((line2s (match-string 1))
  912. (line2e (match-string 2))
  913. (pt2 (progn
  914. (delete-region (progn (beginning-of-line) (point))
  915. (progn (forward-line 1) (point)))
  916. (point-marker))))
  917. (goto-char pt1)
  918. (forward-line 1)
  919. (while (< (point) pt2)
  920. (case (char-after)
  921. (?! (delete-char 2) (insert "-") (forward-line 1))
  922. (?- (forward-char 1) (delete-char 1) (forward-line 1))
  923. (?\s ;merge with the other half of the chunk
  924. (let* ((endline2
  925. (save-excursion
  926. (goto-char pt2) (forward-line 1) (point))))
  927. (case (char-after pt2)
  928. ((?! ?+)
  929. (insert "+"
  930. (prog1 (buffer-substring (+ pt2 2) endline2)
  931. (delete-region pt2 endline2))))
  932. (?\s
  933. (unless (= (- endline2 pt2)
  934. (- (line-beginning-position 2) (point)))
  935. ;; If the two lines we're merging don't have the
  936. ;; same length (can happen with "diff -b"), then
  937. ;; diff-unified->context will not properly undo
  938. ;; this operation.
  939. (setq reversible nil))
  940. (delete-region pt2 endline2)
  941. (delete-char 1)
  942. (forward-line 1))
  943. (?\\ (forward-line 1))
  944. (t (setq reversible nil)
  945. (delete-char 1) (forward-line 1)))))
  946. (t (setq reversible nil) (forward-line 1))))
  947. (while (looking-at "[+! ] ")
  948. (if (/= (char-after) ?!) (forward-char 1)
  949. (delete-char 1) (insert "+"))
  950. (delete-char 1) (forward-line 1))
  951. (save-excursion
  952. (goto-char pt1)
  953. (insert "@@ -" line1s ","
  954. (number-to-string (- (string-to-number line1e)
  955. (string-to-number line1s)
  956. -1))
  957. " +" line2s ","
  958. (number-to-string (- (string-to-number line2e)
  959. (string-to-number line2s)
  960. -1)) " @@"))
  961. (set-marker pt2 nil)
  962. ;; The whole procedure succeeded, let's replace the myriad
  963. ;; of undo elements with just a single special one.
  964. (unless (or (not reversible) (eq buffer-undo-list t))
  965. (setq buffer-undo-list
  966. (cons (list 'apply (- old-end end) pt1 (point)
  967. 'diff-unified->context pt1 (point))
  968. old-undo)))
  969. )))))))))
  970. (defun diff-reverse-direction (start end)
  971. "Reverse the direction of the diffs.
  972. START and END are either taken from the region (if a prefix arg is given) or
  973. else cover the whole buffer."
  974. (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
  975. (list (region-beginning) (region-end))
  976. (list (point-min) (point-max))))
  977. (unless (markerp end) (setq end (copy-marker end t)))
  978. (let (;;(diff-inhibit-after-change t)
  979. (inhibit-read-only t))
  980. (save-excursion
  981. (goto-char start)
  982. (while (and (re-search-forward "^\\(\\([-*][-*][-*] \\)\\(.+\\)\n\\([-+][-+][-+] \\)\\(.+\\)\\|\\*\\{15\\}.*\n\\*\\*\\* \\(.+\\) \\*\\*\\*\\*\\|@@ -\\([0-9,]+\\) \\+\\([0-9,]+\\) @@.*\\)$" nil t)
  983. (< (point) end))
  984. (combine-after-change-calls
  985. (cond
  986. ;; a file header
  987. ((match-beginning 2) (replace-match "\\2\\5\n\\4\\3" nil))
  988. ;; a context-diff hunk header
  989. ((match-beginning 6)
  990. (let ((pt-lines1 (match-beginning 6))
  991. (lines1 (match-string 6)))
  992. (replace-match "" nil nil nil 6)
  993. (forward-line 1)
  994. (let ((half1s (point)))
  995. (while (looking-at "[-! \\][ \t]\\|#")
  996. (when (= (char-after) ?-) (delete-char 1) (insert "+"))
  997. (forward-line 1))
  998. (let ((half1 (delete-and-extract-region half1s (point))))
  999. (unless (looking-at diff-context-mid-hunk-header-re)
  1000. (insert half1)
  1001. (error "Can't find matching `--- n1,n2 ----' line"))
  1002. (let* ((str1end (or (match-end 2) (match-end 1)))
  1003. (str1 (buffer-substring (match-beginning 1) str1end)))
  1004. (goto-char str1end)
  1005. (insert lines1)
  1006. (delete-region (match-beginning 1) str1end)
  1007. (forward-line 1)
  1008. (let ((half2s (point)))
  1009. (while (looking-at "[!+ \\][ \t]\\|#")
  1010. (when (= (char-after) ?+) (delete-char 1) (insert "-"))
  1011. (forward-line 1))
  1012. (let ((half2 (delete-and-extract-region half2s (point))))
  1013. (insert (or half1 ""))
  1014. (goto-char half1s)
  1015. (insert (or half2 ""))))
  1016. (goto-char pt-lines1)
  1017. (insert str1))))))
  1018. ;; a unified-diff hunk header
  1019. ((match-beginning 7)
  1020. (replace-match "@@ -\\8 +\\7 @@" nil)
  1021. (forward-line 1)
  1022. (let ((c (char-after)) first last)
  1023. (while (case (setq c (char-after))
  1024. (?- (setq first (or first (point)))
  1025. (delete-char 1) (insert "+") t)
  1026. (?+ (setq last (or last (point)))
  1027. (delete-char 1) (insert "-") t)
  1028. ((?\\ ?#) t)
  1029. (t (when (and first last (< first last))
  1030. (insert (delete-and-extract-region first last)))
  1031. (setq first nil last nil)
  1032. (memq c (if diff-valid-unified-empty-line
  1033. '(?\s ?\n) '(?\s)))))
  1034. (forward-line 1))))))))))
  1035. (defun diff-fixup-modifs (start end)
  1036. "Fixup the hunk headers (in case the buffer was modified).
  1037. START and END are either taken from the region (if a prefix arg is given) or
  1038. else cover the whole buffer."
  1039. (interactive (if (or current-prefix-arg (and transient-mark-mode mark-active))
  1040. (list (region-beginning) (region-end))
  1041. (list (point-min) (point-max))))
  1042. (let ((inhibit-read-only t))
  1043. (save-excursion
  1044. (goto-char end) (diff-end-of-hunk nil 'donttrustheader)
  1045. (let ((plus 0) (minus 0) (space 0) (bang 0))
  1046. (while (and (= (forward-line -1) 0) (<= start (point)))
  1047. (if (not (looking-at
  1048. (concat diff-hunk-header-re-unified
  1049. "\\|[-*][-*][-*] [0-9,]+ [-*][-*][-*][-*]$"
  1050. "\\|--- .+\n\\+\\+\\+ ")))
  1051. (case (char-after)
  1052. (?\s (incf space))
  1053. (?+ (incf plus))
  1054. (?- (incf minus))
  1055. (?! (incf bang))
  1056. ((?\\ ?#) nil)
  1057. (t (setq space 0 plus 0 minus 0 bang 0)))
  1058. (cond
  1059. ((looking-at diff-hunk-header-re-unified)
  1060. (let* ((old1 (match-string 2))
  1061. (old2 (match-string 4))
  1062. (new1 (number-to-string (+ space minus)))
  1063. (new2 (number-to-string (+ space plus))))
  1064. (if old2
  1065. (unless (string= new2 old2) (replace-match new2 t t nil 4))
  1066. (goto-char (match-end 3))
  1067. (insert "," new2))
  1068. (if old1
  1069. (unless (string= new1 old1) (replace-match new1 t t nil 2))
  1070. (goto-char (match-end 1))
  1071. (insert "," new1))))
  1072. ((looking-at diff-context-mid-hunk-header-re)
  1073. (when (> (+ space bang plus) 0)
  1074. (let* ((old1 (match-string 1))
  1075. (old2 (match-string 2))
  1076. (new (number-to-string
  1077. (+ space bang plus -1 (string-to-number old1)))))
  1078. (unless (string= new old2) (replace-match new t t nil 2)))))
  1079. ((looking-at "\\*\\*\\* \\([0-9]+\\),\\(-?[0-9]*\\) \\*\\*\\*\\*$")
  1080. (when (> (+ space bang minus) 0)
  1081. (let* ((old (match-string 1))
  1082. (new (format
  1083. (concat "%0" (number-to-string (length old)) "d")
  1084. (+ space bang minus -1 (string-to-number old)))))
  1085. (unless (string= new old) (replace-match new t t nil 2))))))
  1086. (setq space 0 plus 0 minus 0 bang 0)))))))
  1087. ;;;;
  1088. ;;;; Hooks
  1089. ;;;;
  1090. (defun diff-write-contents-hooks ()
  1091. "Fixup hunk headers if necessary."
  1092. (if (buffer-modified-p) (diff-fixup-modifs (point-min) (point-max)))
  1093. nil)
  1094. ;; It turns out that making changes in the buffer from within an
  1095. ;; *-change-function is asking for trouble, whereas making them
  1096. ;; from a post-command-hook doesn't pose much problems
  1097. (defvar diff-unhandled-changes nil)
  1098. (defun diff-after-change-function (beg end _len)
  1099. "Remember to fixup the hunk header.
  1100. See `after-change-functions' for the meaning of BEG, END and LEN."
  1101. ;; Ignoring changes when inhibit-read-only is set is strictly speaking
  1102. ;; incorrect, but it turns out that inhibit-read-only is normally not set
  1103. ;; inside editing commands, while it tends to be set when the buffer gets
  1104. ;; updated by an async process or by a conversion function, both of which
  1105. ;; would rather not be uselessly slowed down by this hook.
  1106. (when (and (not undo-in-progress) (not inhibit-read-only))
  1107. (if diff-unhandled-changes
  1108. (setq diff-unhandled-changes
  1109. (cons (min beg (car diff-unhandled-changes))
  1110. (max end (cdr diff-unhandled-changes))))
  1111. (setq diff-unhandled-changes (cons beg end)))))
  1112. (defun diff-post-command-hook ()
  1113. "Fixup hunk headers if necessary."
  1114. (when (consp diff-unhandled-changes)
  1115. (ignore-errors
  1116. (save-excursion
  1117. (goto-char (car diff-unhandled-changes))
  1118. ;; Maybe we've cut the end of the hunk before point.
  1119. (if (and (bolp) (not (bobp))) (backward-char 1))
  1120. ;; We used to fixup modifs on all the changes, but it turns out that
  1121. ;; it's safer not to do it on big changes, e.g. when yanking a big
  1122. ;; diff, or when the user edits the header, since we might then
  1123. ;; screw up perfectly correct values. --Stef
  1124. (diff-beginning-of-hunk)
  1125. (let* ((style (if (looking-at "\\*\\*\\*") 'context))
  1126. (start (line-beginning-position (if (eq style 'context) 3 2)))
  1127. (mid (if (eq style 'context)
  1128. (save-excursion
  1129. (re-search-forward diff-context-mid-hunk-header-re
  1130. nil t)))))
  1131. (when (and ;; Don't try to fixup changes in the hunk header.
  1132. (> (car diff-unhandled-changes) start)
  1133. ;; Don't try to fixup changes in the mid-hunk header either.
  1134. (or (not mid)
  1135. (< (cdr diff-unhandled-changes) (match-beginning 0))
  1136. (> (car diff-unhandled-changes) (match-end 0)))
  1137. (save-excursion
  1138. (diff-end-of-hunk nil 'donttrustheader)
  1139. ;; Don't try to fixup changes past the end of the hunk.
  1140. (>= (point) (cdr diff-unhandled-changes))))
  1141. (diff-fixup-modifs (point) (cdr diff-unhandled-changes)))))
  1142. (setq diff-unhandled-changes nil))))
  1143. (defun diff-next-error (arg reset)
  1144. ;; Select a window that displays the current buffer so that point
  1145. ;; movements are reflected in that window. Otherwise, the user might
  1146. ;; never see the hunk corresponding to the source she's jumping to.
  1147. (pop-to-buffer (current-buffer))
  1148. (if reset (goto-char (point-min)))
  1149. (diff-hunk-next arg)
  1150. (diff-goto-source))
  1151. (defvar whitespace-style)
  1152. (defvar whitespace-trailing-regexp)
  1153. ;;;###autoload
  1154. (define-derived-mode diff-mode fundamental-mode "Diff"
  1155. "Major mode for viewing/editing context diffs.
  1156. Supports unified and context diffs as well as (to a lesser extent)
  1157. normal diffs.
  1158. When the buffer is read-only, the ESC prefix is not necessary.
  1159. If you edit the buffer manually, diff-mode will try to update the hunk
  1160. headers for you on-the-fly.
  1161. You can also switch between context diff and unified diff with \\[diff-context->unified],
  1162. or vice versa with \\[diff-unified->context] and you can also reverse the direction of
  1163. a diff with \\[diff-reverse-direction].
  1164. \\{diff-mode-map}"
  1165. (set (make-local-variable 'font-lock-defaults) diff-font-lock-defaults)
  1166. (set (make-local-variable 'outline-regexp) diff-outline-regexp)
  1167. (set (make-local-variable 'imenu-generic-expression)
  1168. diff-imenu-generic-expression)
  1169. ;; These are not perfect. They would be better done separately for
  1170. ;; context diffs and unidiffs.
  1171. ;; (set (make-local-variable 'paragraph-start)
  1172. ;; (concat "@@ " ; unidiff hunk
  1173. ;; "\\|\\*\\*\\* " ; context diff hunk or file start
  1174. ;; "\\|--- [^\t]+\t")) ; context or unidiff file
  1175. ;; ; start (first or second line)
  1176. ;; (set (make-local-variable 'paragraph-separate) paragraph-start)
  1177. ;; (set (make-local-variable 'page-delimiter) "--- [^\t]+\t")
  1178. ;; compile support
  1179. (set (make-local-variable 'next-error-function) 'diff-next-error)
  1180. (set (make-local-variable 'beginning-of-defun-function)
  1181. 'diff-beginning-of-file-and-junk)
  1182. (set (make-local-variable 'end-of-defun-function)
  1183. 'diff-end-of-file)
  1184. ;; Set up `whitespace-mode' so that turning it on will show trailing
  1185. ;; whitespace problems on the modified lines of the diff.
  1186. (set (make-local-variable 'whitespace-style) '(face trailing))
  1187. (set (make-local-variable 'whitespace-trailing-regexp)
  1188. "^[-\+!<>].*?\\([\t ]+\\)$")
  1189. (setq buffer-read-only diff-default-read-only)
  1190. ;; setup change hooks
  1191. (if (not diff-update-on-the-fly)
  1192. (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
  1193. (make-local-variable 'diff-unhandled-changes)
  1194. (add-hook 'after-change-functions 'diff-after-change-function nil t)
  1195. (add-hook 'post-command-hook 'diff-post-command-hook nil t))
  1196. ;; Neat trick from Dave Love to add more bindings in read-only mode:
  1197. (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
  1198. (add-to-list 'minor-mode-overriding-map-alist ro-bind)
  1199. ;; Turn off this little trick in case the buffer is put in view-mode.
  1200. (add-hook 'view-mode-hook
  1201. (lambda ()
  1202. (setq minor-mode-overriding-map-alist
  1203. (delq ro-bind minor-mode-overriding-map-alist)))
  1204. nil t))
  1205. ;; add-log support
  1206. (set (make-local-variable 'add-log-current-defun-function)
  1207. 'diff-current-defun)
  1208. (set (make-local-variable 'add-log-buffer-file-name-function)
  1209. (lambda () (diff-find-file-name nil 'noprompt)))
  1210. (unless (buffer-file-name)
  1211. (hack-dir-local-variables-non-file-buffer)))
  1212. ;;;###autoload
  1213. (define-minor-mode diff-minor-mode
  1214. "Toggle Diff minor mode.
  1215. With a prefix argument ARG, enable Diff minor mode if ARG is
  1216. positive, and disable it otherwise. If called from Lisp, enable
  1217. the mode if ARG is omitted or nil.
  1218. \\{diff-minor-mode-map}"
  1219. :group 'diff-mode :lighter " Diff"
  1220. ;; FIXME: setup font-lock
  1221. ;; setup change hooks
  1222. (if (not diff-update-on-the-fly)
  1223. (add-hook 'write-contents-functions 'diff-write-contents-hooks nil t)
  1224. (make-local-variable 'diff-unhandled-changes)
  1225. (add-hook 'after-change-functions 'diff-after-change-function nil t)
  1226. (add-hook 'post-command-hook 'diff-post-command-hook nil t)))
  1227. ;;; Handy hook functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1228. (defun diff-delete-if-empty ()
  1229. ;; An empty diff file means there's no more diffs to integrate, so we
  1230. ;; can just remove the file altogether. Very handy for .rej files if we
  1231. ;; remove hunks as we apply them.
  1232. (when (and buffer-file-name
  1233. (eq 0 (nth 7 (file-attributes buffer-file-name))))
  1234. (delete-file buffer-file-name)))
  1235. (defun diff-delete-empty-files ()
  1236. "Arrange for empty diff files to be removed."
  1237. (add-hook 'after-save-hook 'diff-delete-if-empty nil t))
  1238. (defun diff-make-unified ()
  1239. "Turn context diffs into unified diffs if applicable."
  1240. (if (save-excursion
  1241. (goto-char (point-min))
  1242. (and (looking-at diff-hunk-header-re) (eq (char-after) ?*)))
  1243. (let ((mod (buffer-modified-p)))
  1244. (unwind-protect
  1245. (diff-context->unified (point-min) (point-max))
  1246. (restore-buffer-modified-p mod)))))
  1247. ;;;
  1248. ;;; Misc operations that have proved useful at some point.
  1249. ;;;
  1250. (defun diff-next-complex-hunk ()
  1251. "Jump to the next \"complex\" hunk.
  1252. \"Complex\" is approximated by \"the hunk changes the number of lines\".
  1253. Only works for unified diffs."
  1254. (interactive)
  1255. (while
  1256. (and (re-search-forward diff-hunk-header-re-unified nil t)
  1257. (equal (match-string 2) (match-string 4)))))
  1258. (defun diff-sanity-check-context-hunk-half (lines)
  1259. (let ((count lines))
  1260. (while
  1261. (cond
  1262. ((and (memq (char-after) '(?\s ?! ?+ ?-))
  1263. (memq (char-after (1+ (point))) '(?\s ?\t)))
  1264. (decf count) t)
  1265. ((or (zerop count) (= count lines)) nil)
  1266. ((memq (char-after) '(?! ?+ ?-))
  1267. (if (not (and (eq (char-after (1+ (point))) ?\n)
  1268. (y-or-n-p "Try to auto-fix whitespace loss damage? ")))
  1269. (error "End of hunk ambiguously marked")
  1270. (forward-char 1) (insert " ") (forward-line -1) t))
  1271. ((< lines 0)
  1272. (error "End of hunk ambiguously marked"))
  1273. ((not (y-or-n-p "Try to auto-fix whitespace loss and word-wrap damage? "))
  1274. (error "Abort!"))
  1275. ((eolp) (insert " ") (forward-line -1) t)
  1276. (t (insert " ") (delete-region (- (point) 2) (- (point) 1)) t))
  1277. (forward-line))))
  1278. (defun diff-sanity-check-hunk ()
  1279. (let (;; Every modification is protected by a y-or-n-p, so it's probably
  1280. ;; OK to override a read-only setting.
  1281. (inhibit-read-only t))
  1282. (save-excursion
  1283. (cond
  1284. ((not (looking-at diff-hunk-header-re))
  1285. (error "Not recognizable hunk header"))
  1286. ;; A context diff.
  1287. ((eq (char-after) ?*)
  1288. (if (not (looking-at "\\*\\{15\\}\\(?: .*\\)?\n\\*\\*\\* \\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)? \\*\\*\\*\\*"))
  1289. (error "Unrecognized context diff first hunk header format")
  1290. (forward-line 2)
  1291. (diff-sanity-check-context-hunk-half
  1292. (if (match-end 2)
  1293. (1+ (- (string-to-number (match-string 2))
  1294. (string-to-number (match-string 1))))
  1295. 1))
  1296. (if (not (looking-at diff-context-mid-hunk-header-re))
  1297. (error "Unrecognized context diff second hunk header format")
  1298. (forward-line)
  1299. (diff-sanity-check-context-hunk-half
  1300. (if (match-end 2)
  1301. (1+ (- (string-to-number (match-string 2))
  1302. (string-to-number (match-string 1))))
  1303. 1)))))
  1304. ;; A unified diff.
  1305. ((eq (char-after) ?@)
  1306. (if (not (looking-at diff-hunk-header-re-unified))
  1307. (error "Unrecognized unified diff hunk header format")
  1308. (let ((before (string-to-number (or (match-string 2) "1")))
  1309. (after (string-to-number (or (match-string 4) "1"))))
  1310. (forward-line)
  1311. (while
  1312. (case (char-after)
  1313. (?\s (decf before) (decf after) t)
  1314. (?-
  1315. (if (and (looking-at diff-file-header-re)
  1316. (zerop before) (zerop after))
  1317. ;; No need to query: this is a case where two patches
  1318. ;; are concatenated and only counting the lines will
  1319. ;; give the right result. Let's just add an empty
  1320. ;; line so that our code which doesn't count lines
  1321. ;; will not get confused.
  1322. (progn (save-excursion (insert "\n")) nil)
  1323. (decf before) t))
  1324. (?+ (decf after) t)
  1325. (t
  1326. (cond
  1327. ((and diff-valid-unified-empty-line
  1328. ;; Not just (eolp) so we don't infloop at eob.
  1329. (eq (char-after) ?\n)
  1330. (> before 0) (> after 0))
  1331. (decf before) (decf after) t)
  1332. ((and (zerop before) (zerop after)) nil)
  1333. ((or (< before 0) (< after 0))
  1334. (error (if (or (zerop before) (zerop after))
  1335. "End of hunk ambiguously marked"
  1336. "Hunk seriously messed up")))
  1337. ((not (y-or-n-p (concat "Try to auto-fix " (if (eolp) "whitespace loss" "word-wrap damage") "? ")))
  1338. (error "Abort!"))
  1339. ((eolp) (insert " ") (forward-line -1) t)
  1340. (t (insert " ")
  1341. (delete-region (- (point) 2) (- (point) 1)) t))))
  1342. (forward-line)))))
  1343. ;; A plain diff.
  1344. (t
  1345. ;; TODO.
  1346. )))))
  1347. (defun diff-hunk-text (hunk destp char-offset)
  1348. "Return the literal source text from HUNK as (TEXT . OFFSET).
  1349. If DESTP is nil, TEXT is the source, otherwise the destination text.
  1350. CHAR-OFFSET is a char-offset in HUNK, and OFFSET is the corresponding
  1351. char-offset in TEXT."
  1352. (with-temp-buffer
  1353. (insert hunk)
  1354. (goto-char (point-min))
  1355. (let ((src-pos nil)
  1356. (dst-pos nil)
  1357. (divider-pos nil)
  1358. (num-pfx-chars 2))
  1359. ;; Set the following variables:
  1360. ;; SRC-POS buffer pos of the source part of the hunk or nil if none
  1361. ;; DST-POS buffer pos of the destination part of the hunk or nil
  1362. ;; DIVIDER-POS buffer pos of any divider line separating the src & dst
  1363. ;; NUM-PFX-CHARS number of line-prefix characters used by this format"
  1364. (cond ((looking-at "^@@")
  1365. ;; unified diff
  1366. (setq num-pfx-chars 1)
  1367. (forward-line 1)
  1368. (setq src-pos (point) dst-pos (point)))
  1369. ((looking-at "^\\*\\*")
  1370. ;; context diff
  1371. (forward-line 2)
  1372. (setq src-pos (point))
  1373. (re-search-forward diff-context-mid-hunk-header-re nil t)
  1374. (forward-line 0)
  1375. (setq divider-pos (point))
  1376. (forward-line 1)
  1377. (setq dst-pos (point)))
  1378. ((looking-at "^[0-9]+a[0-9,]+$")
  1379. ;; normal diff, insert
  1380. (forward-line 1)
  1381. (setq dst-pos (point)))
  1382. ((looking-at "^[0-9,]+d[0-9]+$")
  1383. ;; normal diff, delete
  1384. (forward-line 1)
  1385. (setq src-pos (point)))
  1386. ((looking-at "^[0-9,]+c[0-9,]+$")
  1387. ;; normal diff, change
  1388. (forward-line 1)
  1389. (setq src-pos (point))
  1390. (re-search-forward "^---$" nil t)
  1391. (forward-line 0)
  1392. (setq divider-pos (point))
  1393. (forward-line 1)
  1394. (setq dst-pos (point)))
  1395. (t
  1396. (error "Unknown diff hunk type")))
  1397. (if (if destp (null dst-pos) (null src-pos))
  1398. ;; Implied empty text
  1399. (if char-offset '("" . 0) "")
  1400. ;; For context diffs, either side can be empty, (if there's only
  1401. ;; added or only removed text). We should then use the other side.
  1402. (cond ((equal src-pos divider-pos) (setq src-pos dst-pos))
  1403. ((equal dst-pos (point-max)) (setq dst-pos src-pos)))
  1404. (when char-offset (goto-char (+ (point-min) char-offset)))
  1405. ;; Get rid of anything except the desired text.
  1406. (save-excursion
  1407. ;; Delete unused text region
  1408. (let ((keep (if destp dst-pos src-pos)))
  1409. (when (and divider-pos (> divider-pos keep))
  1410. (delete-region divider-pos (point-max)))
  1411. (delete-region (point-min) keep))
  1412. ;; Remove line-prefix characters, and unneeded lines (unified diffs).
  1413. (let ((kill-char (if destp ?- ?+)))
  1414. (goto-char (point-min))
  1415. (while (not (eobp))
  1416. (if (eq (char-after) kill-char)
  1417. (delete-region (point) (progn (forward-line 1) (point)))
  1418. (delete-char num-pfx-chars)
  1419. (forward-line 1)))))
  1420. (let ((text (buffer-substring-no-properties (point-min) (point-max))))
  1421. (if char-offset (cons text (- (point) (point-min))) text))))))
  1422. (defun diff-find-text (text)
  1423. "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
  1424. If TEXT isn't found, nil is returned."
  1425. (let* ((orig (point))
  1426. (forw (and (search-forward text nil t)
  1427. (cons (match-beginning 0) (match-end 0))))
  1428. (back (and (goto-char (+ orig (length text)))
  1429. (search-backward text nil t)
  1430. (cons (match-beginning 0) (match-end 0)))))
  1431. ;; Choose the closest match.
  1432. (if (and forw back)
  1433. (if (> (- (car forw) orig) (- orig (car back))) back forw)
  1434. (or back forw))))
  1435. (defun diff-find-approx-text (text)
  1436. "Return the buffer position (BEG . END) of the nearest occurrence of TEXT.
  1437. Whitespace differences are ignored."
  1438. (let* ((orig (point))
  1439. (re (concat "^[ \t\n ]*"
  1440. (mapconcat 'regexp-quote (split-string text) "[ \t\n ]+")
  1441. "[ \t\n ]*\n"))
  1442. (forw (and (re-search-forward re nil t)
  1443. (cons (match-beginning 0) (match-end 0))))
  1444. (back (and (goto-char (+ orig (length text)))
  1445. (re-search-backward re nil t)
  1446. (cons (match-beginning 0) (match-end 0)))))
  1447. ;; Choose the closest match.
  1448. (if (and forw back)
  1449. (if (> (- (car forw) orig) (- orig (car back))) back forw)
  1450. (or back forw))))
  1451. (defsubst diff-xor (a b) (if a (if (not b) a) b))
  1452. (defun diff-find-source-location (&optional other-file reverse noprompt)
  1453. "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED).
  1454. BUF is the buffer corresponding to the source file.
  1455. LINE-OFFSET is the offset between the expected and actual positions
  1456. of the text of the hunk or nil if the text was not found.
  1457. POS is a pair (BEG . END) indicating the position of the text in the buffer.
  1458. SRC and DST are the two variants of text as returned by `diff-hunk-text'.
  1459. SRC is the variant that was found in the buffer.
  1460. SWITCHED is non-nil if the patch is already applied.
  1461. NOPROMPT, if non-nil, means not to prompt the user."
  1462. (save-excursion
  1463. (let* ((other (diff-xor other-file diff-jump-to-old-file))
  1464. (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
  1465. (point))))
  1466. ;; Check that the hunk is well-formed. Otherwise diff-mode and
  1467. ;; the user may disagree on what constitutes the hunk
  1468. ;; (e.g. because an empty line truncates the hunk mid-course),
  1469. ;; leading to potentially nasty surprises for the user.
  1470. ;;
  1471. ;; Suppress check when NOPROMPT is non-nil (Bug#3033).
  1472. (_ (unless noprompt (diff-sanity-check-hunk)))
  1473. (hunk (buffer-substring
  1474. (point) (save-excursion (diff-end-of-hunk) (point))))
  1475. (old (diff-hunk-text hunk reverse char-offset))
  1476. (new (diff-hunk-text hunk (not reverse) char-offset))
  1477. ;; Find the location specification.
  1478. (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?"))
  1479. (error "Can't find the hunk header")
  1480. (if other (match-string 1)
  1481. (if (match-end 3) (match-string 3)
  1482. (unless (re-search-forward
  1483. diff-context-mid-hunk-header-re nil t)
  1484. (error "Can't find the hunk separator"))
  1485. (match-string 1)))))
  1486. (file (or (diff-find-file-name other noprompt)
  1487. (error "Can't find the file")))
  1488. (buf (find-file-noselect file)))
  1489. ;; Update the user preference if he so wished.
  1490. (when (> (prefix-numeric-value other-file) 8)
  1491. (setq diff-jump-to-old-file other))
  1492. (with-current-buffer buf
  1493. (goto-char (point-min)) (forward-line (1- (string-to-number line)))
  1494. (let* ((orig-pos (point))
  1495. (switched nil)
  1496. ;; FIXME: Check for case where both OLD and NEW are found.
  1497. (pos (or (diff-find-text (car old))
  1498. (progn (setq switched t) (diff-find-text (car new)))
  1499. (progn (setq switched nil)
  1500. (condition-case nil
  1501. (diff-find-approx-text (car old))
  1502. (invalid-regexp nil))) ;Regex too big.
  1503. (progn (setq switched t)
  1504. (condition-case nil
  1505. (diff-find-approx-text (car new))
  1506. (invalid-regexp nil))) ;Regex too big.
  1507. (progn (setq switched nil) nil))))
  1508. (nconc
  1509. (list buf)
  1510. (if pos
  1511. (list (count-lines orig-pos (car pos)) pos)
  1512. (list nil (cons orig-pos (+ orig-pos (length (car old))))))
  1513. (if switched (list new old t) (list old new))))))))
  1514. (defun diff-hunk-status-msg (line-offset reversed dry-run)
  1515. (let ((msg (if dry-run
  1516. (if reversed "already applied" "not yet applied")
  1517. (if reversed "undone" "applied"))))
  1518. (message (cond ((null line-offset) "Hunk text not found")
  1519. ((= line-offset 0) "Hunk %s")
  1520. ((= line-offset 1) "Hunk %s at offset %d line")
  1521. (t "Hunk %s at offset %d lines"))
  1522. msg line-offset)))
  1523. (defvar diff-apply-hunk-to-backup-file nil)
  1524. (defun diff-apply-hunk (&optional reverse)
  1525. "Apply the current hunk to the source file and go to the next.
  1526. By default, the new source file is patched, but if the variable
  1527. `diff-jump-to-old-file' is non-nil, then the old source file is
  1528. patched instead (some commands, such as `diff-goto-source' can change
  1529. the value of this variable when given an appropriate prefix argument).
  1530. With a prefix argument, REVERSE the hunk."
  1531. (interactive "P")
  1532. (destructuring-bind (buf line-offset pos old new &optional switched)
  1533. ;; Sometimes we'd like to have the following behavior: if REVERSE go
  1534. ;; to the new file, otherwise go to the old. But that means that by
  1535. ;; default we use the old file, which is the opposite of the default
  1536. ;; for diff-goto-source, and is thus confusing. Also when you don't
  1537. ;; know about it it's pretty surprising.
  1538. ;; TODO: make it possible to ask explicitly for this behavior.
  1539. ;;
  1540. ;; This is duplicated in diff-test-hunk.
  1541. (diff-find-source-location nil reverse)
  1542. (cond
  1543. ((null line-offset)
  1544. (error "Can't find the text to patch"))
  1545. ((with-current-buffer buf
  1546. (and buffer-file-name
  1547. (backup-file-name-p buffer-file-name)
  1548. (not diff-apply-hunk-to-backup-file)
  1549. (not (set (make-local-variable 'diff-apply-hunk-to-backup-file)
  1550. (yes-or-no-p (format "Really apply this hunk to %s? "
  1551. (file-name-nondirectory
  1552. buffer-file-name)))))))
  1553. (error "%s"
  1554. (substitute-command-keys
  1555. (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
  1556. (if (not reverse) "\\[universal-argument] ")))))
  1557. ((and switched
  1558. ;; A reversed patch was detected, perhaps apply it in reverse.
  1559. (not (save-window-excursion
  1560. (pop-to-buffer buf)
  1561. (goto-char (+ (car pos) (cdr old)))
  1562. (y-or-n-p
  1563. (if reverse
  1564. "Hunk hasn't been applied yet; apply it now? "
  1565. "Hunk has already been applied; undo it? ")))))
  1566. (message "(Nothing done)"))
  1567. (t
  1568. ;; Apply the hunk
  1569. (with-current-buffer buf
  1570. (goto-char (car pos))
  1571. (delete-region (car pos) (cdr pos))
  1572. (insert (car new)))
  1573. ;; Display BUF in a window
  1574. (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
  1575. (diff-hunk-status-msg line-offset (diff-xor switched reverse) nil)
  1576. (when diff-advance-after-apply-hunk
  1577. (diff-hunk-next))))))
  1578. (defun diff-test-hunk (&optional reverse)
  1579. "See whether it's possible to apply the current hunk.
  1580. With a prefix argument, try to REVERSE the hunk."
  1581. (interactive "P")
  1582. (destructuring-bind (buf line-offset pos src _dst &optional switched)
  1583. (diff-find-source-location nil reverse)
  1584. (set-window-point (display-buffer buf) (+ (car pos) (cdr src)))
  1585. (diff-hunk-status-msg line-offset (diff-xor reverse switched) t)))
  1586. (defalias 'diff-mouse-goto-source 'diff-goto-source)
  1587. (defun diff-goto-source (&optional other-file event)
  1588. "Jump to the corresponding source line.
  1589. `diff-jump-to-old-file' (or its opposite if the OTHER-FILE prefix arg
  1590. is given) determines whether to jump to the old or the new file.
  1591. If the prefix arg is bigger than 8 (for example with \\[universal-argument] \\[universal-argument])
  1592. then `diff-jump-to-old-file' is also set, for the next invocations."
  1593. (interactive (list current-prefix-arg last-input-event))
  1594. ;; When pointing at a removal line, we probably want to jump to
  1595. ;; the old location, and else to the new (i.e. as if reverting).
  1596. ;; This is a convenient detail when using smerge-diff.
  1597. (if event (posn-set-point (event-end event)))
  1598. (let ((rev (not (save-excursion (beginning-of-line) (looking-at "[-<]")))))
  1599. (destructuring-bind (buf line-offset pos src _dst &optional switched)
  1600. (diff-find-source-location other-file rev)
  1601. (pop-to-buffer buf)
  1602. (goto-char (+ (car pos) (cdr src)))
  1603. (diff-hunk-status-msg line-offset (diff-xor rev switched) t))))
  1604. (defun diff-current-defun ()
  1605. "Find the name of function at point.
  1606. For use in `add-log-current-defun-function'."
  1607. ;; Kill change-log-default-name so it gets recomputed each time, since
  1608. ;; each hunk may belong to another file which may belong to another
  1609. ;; directory and hence have a different ChangeLog file.
  1610. (kill-local-variable 'change-log-default-name)
  1611. (save-excursion
  1612. (when (looking-at diff-hunk-header-re)
  1613. (forward-line 1)
  1614. (re-search-forward "^[^ ]" nil t))
  1615. (destructuring-bind (&optional buf _line-offset pos src dst switched)
  1616. ;; Use `noprompt' since this is used in which-func-mode and such.
  1617. (ignore-errors ;Signals errors in place of prompting.
  1618. (diff-find-source-location nil nil 'noprompt))
  1619. (when buf
  1620. (beginning-of-line)
  1621. (or (when (memq (char-after) '(?< ?-))
  1622. ;; Cursor is pointing at removed text. This could be a removed
  1623. ;; function, in which case, going to the source buffer will
  1624. ;; not help since the function is now removed. Instead,
  1625. ;; try to figure out the function name just from the
  1626. ;; code-fragment.
  1627. (let ((old (if switched dst src)))
  1628. (with-temp-buffer
  1629. (insert (car old))
  1630. (funcall (buffer-local-value 'major-mode buf))
  1631. (goto-char (+ (point-min) (cdr old)))
  1632. (add-log-current-defun))))
  1633. (with-current-buffer buf
  1634. (goto-char (+ (car pos) (cdr src)))
  1635. (add-log-current-defun)))))))
  1636. (defun diff-ignore-whitespace-hunk ()
  1637. "Re-diff the current hunk, ignoring whitespace differences."
  1638. (interactive)
  1639. (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder)
  1640. (point))))
  1641. (opts (case (char-after) (?@ "-bu") (?* "-bc") (t "-b")))
  1642. (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
  1643. (error "Can't find line number"))
  1644. (string-to-number (match-string 1))))
  1645. (inhibit-read-only t)
  1646. (hunk (delete-and-extract-region
  1647. (point) (save-excursion (diff-end-of-hunk) (point))))
  1648. (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
  1649. (file1 (make-temp-file "diff1"))
  1650. (file2 (make-temp-file "diff2"))
  1651. (coding-system-for-read buffer-file-coding-system)
  1652. old new)
  1653. (unwind-protect
  1654. (save-excursion
  1655. (setq old (diff-hunk-text hunk nil char-offset))
  1656. (setq new (diff-hunk-text hunk t char-offset))
  1657. (write-region (concat lead (car old)) nil file1 nil 'nomessage)
  1658. (write-region (concat lead (car new)) nil file2 nil 'nomessage)
  1659. (with-temp-buffer
  1660. (let ((status
  1661. (call-process diff-command nil t nil
  1662. opts file1 file2)))
  1663. (case status
  1664. (0 nil) ;Nothing to reformat.
  1665. (1 (goto-char (point-min))
  1666. ;; Remove the file-header.
  1667. (when (re-search-forward diff-hunk-header-re nil t)
  1668. (delete-region (point-min) (match-beginning 0))))
  1669. (t (goto-char (point-max))
  1670. (unless (bolp) (insert "\n"))
  1671. (insert hunk)))
  1672. (setq hunk (buffer-string))
  1673. (unless (memq status '(0 1))
  1674. (error "Diff returned: %s" status)))))
  1675. ;; Whatever happens, put back some equivalent text: either the new
  1676. ;; one or the original one in case some error happened.
  1677. (insert hunk)
  1678. (delete-file file1)
  1679. (delete-file file2))))
  1680. ;;; Fine change highlighting.
  1681. (defface diff-refine-change
  1682. '((((class color) (min-colors 88) (background light))
  1683. :background "grey85")
  1684. (((class color) (min-colors 88) (background dark))
  1685. :background "grey60")
  1686. (((class color) (background light))
  1687. :background "yellow")
  1688. (((class color) (background dark))
  1689. :background "green")
  1690. (t :weight bold))
  1691. "Face used for char-based changes shown by `diff-refine-hunk'."
  1692. :group 'diff-mode)
  1693. (defun diff-refine-preproc ()
  1694. (while (re-search-forward "^[+>]" nil t)
  1695. ;; Remove spurious changes due to the fact that one side of the hunk is
  1696. ;; marked with leading + or > and the other with leading - or <.
  1697. ;; We used to replace all the prefix chars with " " but this only worked
  1698. ;; when we did char-based refinement (or when using
  1699. ;; smerge-refine-weight-hack) since otherwise, the `forward' motion done
  1700. ;; in chopup do not necessarily do the same as the ones in highlight
  1701. ;; since the "_" is not treated the same as " ".
  1702. (replace-match (cdr (assq (char-before) '((?+ . "-") (?> . "<"))))))
  1703. )
  1704. (declare-function smerge-refine-subst "smerge-mode"
  1705. (beg1 end1 beg2 end2 props &optional preproc))
  1706. (defun diff-refine-hunk ()
  1707. "Highlight changes of hunk at point at a finer granularity."
  1708. (interactive)
  1709. (require 'smerge-mode)
  1710. (save-excursion
  1711. (diff-beginning-of-hunk 'try-harder)
  1712. (let* ((start (point))
  1713. (style (diff-hunk-style)) ;Skips the hunk header as well.
  1714. (beg (point))
  1715. (props '((diff-mode . fine) (face diff-refine-change)))
  1716. ;; Be careful to go back to `start' so diff-end-of-hunk gets
  1717. ;; to read the hunk header's line info.
  1718. (end (progn (goto-char start) (diff-end-of-hunk) (point))))
  1719. (remove-overlays beg end 'diff-mode 'fine)
  1720. (goto-char beg)
  1721. (case style
  1722. (unified
  1723. (while (re-search-forward "^\\(?:-.*\n\\)+\\(\\)\\(?:\\+.*\n\\)+"
  1724. end t)
  1725. (smerge-refine-subst (match-beginning 0) (match-end 1)
  1726. (match-end 1) (match-end 0)
  1727. props 'diff-refine-preproc)))
  1728. (context
  1729. (let* ((middle (save-excursion (re-search-forward "^---")))
  1730. (other middle))
  1731. (while (re-search-forward "^\\(?:!.*\n\\)+" middle t)
  1732. (smerge-refine-subst (match-beginning 0) (match-end 0)
  1733. (save-excursion
  1734. (goto-char other)
  1735. (re-search-forward "^\\(?:!.*\n\\)+" end)
  1736. (setq other (match-end 0))
  1737. (match-beginning 0))
  1738. other
  1739. props 'diff-refine-preproc))))
  1740. (t ;; Normal diffs.
  1741. (let ((beg1 (1+ (point))))
  1742. (when (re-search-forward "^---.*\n" end t)
  1743. ;; It's a combined add&remove, so there's something to do.
  1744. (smerge-refine-subst beg1 (match-beginning 0)
  1745. (match-end 0) end
  1746. props 'diff-refine-preproc))))))))
  1747. (defun diff-add-change-log-entries-other-window ()
  1748. "Iterate through the current diff and create ChangeLog entries.
  1749. I.e. like `add-change-log-entry-other-window' but applied to all hunks."
  1750. (interactive)
  1751. ;; XXX: Currently add-change-log-entry-other-window is only called
  1752. ;; once per hunk. Some hunks have multiple changes, it would be
  1753. ;; good to call it for each change.
  1754. (save-excursion
  1755. (goto-char (point-min))
  1756. (condition-case nil
  1757. ;; Call add-change-log-entry-other-window for each hunk in
  1758. ;; the diff buffer.
  1759. (while (progn
  1760. (diff-hunk-next)
  1761. ;; Move to where the changes are,
  1762. ;; `add-change-log-entry-other-window' works better in
  1763. ;; that case.
  1764. (re-search-forward
  1765. (concat "\n[!+-<>]"
  1766. ;; If the hunk is a context hunk with an empty first
  1767. ;; half, recognize the "--- NNN,MMM ----" line
  1768. "\\(-- [0-9]+\\(,[0-9]+\\)? ----\n"
  1769. ;; and skip to the next non-context line.
  1770. "\\( .*\n\\)*[+]\\)?")
  1771. nil t))
  1772. (save-excursion
  1773. ;; FIXME: this pops up windows of all the buffers.
  1774. (add-change-log-entry nil nil t nil t)))
  1775. ;; When there's no more hunks, diff-hunk-next signals an error.
  1776. (error nil))))
  1777. ;; provide the package
  1778. (provide 'diff-mode)
  1779. ;;; Old Change Log from when diff-mode wasn't part of Emacs:
  1780. ;; Revision 1.11 1999/10/09 23:38:29 monnier
  1781. ;; (diff-mode-load-hook): dropped.
  1782. ;; (auto-mode-alist): also catch *.diffs.
  1783. ;; (diff-find-file-name, diff-mode): add smarts to find the right file
  1784. ;; for *.rej files (that lack any file name indication).
  1785. ;;
  1786. ;; Revision 1.10 1999/09/30 15:32:11 monnier
  1787. ;; added support for "\ No newline at end of file".
  1788. ;;
  1789. ;; Revision 1.9 1999/09/15 00:01:13 monnier
  1790. ;; - added basic `compile' support.
  1791. ;; - have diff-kill-hunk call diff-kill-file if it's the only hunk.
  1792. ;; - diff-kill-file now tries to kill the leading garbage as well.
  1793. ;;
  1794. ;; Revision 1.8 1999/09/13 21:10:09 monnier
  1795. ;; - don't use CL in the autoloaded code
  1796. ;; - accept diffs using -T
  1797. ;;
  1798. ;; Revision 1.7 1999/09/05 20:53:03 monnier
  1799. ;; interface to ediff-patch
  1800. ;;
  1801. ;; Revision 1.6 1999/09/01 20:55:13 monnier
  1802. ;; (ediff=patch-file): add bindings to call ediff-patch.
  1803. ;; (diff-find-file-name): taken out of diff-goto-source.
  1804. ;; (diff-unified->context, diff-context->unified, diff-reverse-direction,
  1805. ;; diff-fixup-modifs): only use the region if a prefix arg is given.
  1806. ;;
  1807. ;; Revision 1.5 1999/08/31 19:18:52 monnier
  1808. ;; (diff-beginning-of-file, diff-prev-file): fixed wrong parenthesis.
  1809. ;;
  1810. ;; Revision 1.4 1999/08/31 13:01:44 monnier
  1811. ;; use `combine-after-change-calls' to minimize the slowdown of font-lock.
  1812. ;;
  1813. ;;; diff-mode.el ends here