diff options
| author | Stefan Monnier | 2019-04-09 18:39:22 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2019-04-09 18:39:22 -0400 |
| commit | 85fbdf027dc03e606c7c4532162148891e41d786 (patch) | |
| tree | 72526505f277d6fa88dc679d5ef479d31976e4e6 | |
| parent | d96b672f2b738bb6364023c2dcb9111efd3855ed (diff) | |
| download | emacs-85fbdf027dc03e606c7c4532162148891e41d786.tar.gz emacs-85fbdf027dc03e606c7c4532162148891e41d786.zip | |
diff-font-lock-syntax: clarify distinction between t and hunk-also
* lisp/vc/diff-mode.el (diff-font-lock-syntax): Rework docstring.
(diff-syntax-fontify-hunk): Never use the hunk method when
diff-font-lock-syntax is just t.
| -rw-r--r-- | lisp/vc/diff-mode.el | 132 |
1 files changed, 63 insertions, 69 deletions
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index eeac24376e7..8940c7e09a6 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el | |||
| @@ -116,7 +116,7 @@ You can always manually refine a hunk with `diff-refine-hunk'." | |||
| 116 | "If non-nil, diff hunk font-lock includes source language syntax highlighting. | 116 | "If non-nil, diff hunk font-lock includes source language syntax highlighting. |
| 117 | This highlighting is the same as added by `font-lock-mode' | 117 | This highlighting is the same as added by `font-lock-mode' |
| 118 | when corresponding source files are visited normally. | 118 | when corresponding source files are visited normally. |
| 119 | Syntax highlighting is added over diff own highlighted changes. | 119 | Syntax highlighting is added over diff-mode's own highlighted changes. |
| 120 | 120 | ||
| 121 | If t, the default, highlight syntax only in Diff buffers created by Diff | 121 | If t, the default, highlight syntax only in Diff buffers created by Diff |
| 122 | commands that compare files or by VC commands that compare revisions. | 122 | commands that compare files or by VC commands that compare revisions. |
| @@ -126,17 +126,17 @@ For diffs against the working-tree version of a file, the highlighting is | |||
| 126 | based on the current file contents. File-based fontification tries to | 126 | based on the current file contents. File-based fontification tries to |
| 127 | infer fontification from the compared files. | 127 | infer fontification from the compared files. |
| 128 | 128 | ||
| 129 | If revision-based or file-based method fails, use hunk-based method to get | 129 | If `hunk-only' fontification is based on hunk alone, without full source. |
| 130 | fontification from hunk alone if the value is `hunk-also'. | ||
| 131 | |||
| 132 | If `hunk-only', fontification is based on hunk alone, without full source. | ||
| 133 | It tries to highlight hunks without enough context that sometimes might result | 130 | It tries to highlight hunks without enough context that sometimes might result |
| 134 | in wrong fontification. This is the fastest option, but less reliable." | 131 | in wrong fontification. This is the fastest option, but less reliable. |
| 132 | |||
| 133 | If `hunk-also', use reliable file-based syntax highlighting when available | ||
| 134 | and hunk-based syntax highlighting otherwise as a fallback." | ||
| 135 | :version "27.1" | 135 | :version "27.1" |
| 136 | :type '(choice (const :tag "Don't highlight syntax" nil) | 136 | :type '(choice (const :tag "Don't highlight syntax" nil) |
| 137 | (const :tag "Hunk-based also" hunk-also) | ||
| 138 | (const :tag "Hunk-based only" hunk-only) | 137 | (const :tag "Hunk-based only" hunk-only) |
| 139 | (const :tag "Highlight syntax" t))) | 138 | (const :tag "Highlight syntax" t) |
| 139 | (const :tag "Allow hunk-based fallback" hunk-also))) | ||
| 140 | 140 | ||
| 141 | (defvar diff-vc-backend nil | 141 | (defvar diff-vc-backend nil |
| 142 | "The VC backend that created the current Diff buffer, if any.") | 142 | "The VC backend that created the current Diff buffer, if any.") |
| @@ -2434,67 +2434,61 @@ When OLD is non-nil, highlight the hunk from the old source." | |||
| 2434 | (string-to-number (match-string 2 line))) | 2434 | (string-to-number (match-string 2 line))) |
| 2435 | (list (string-to-number line) 1)))) ; One-line diffs | 2435 | (list (string-to-number line) 1)))) ; One-line diffs |
| 2436 | (props | 2436 | (props |
| 2437 | (cond | 2437 | (or |
| 2438 | ((and diff-vc-backend (not (eq diff-font-lock-syntax 'hunk-only))) | 2438 | (when (and diff-vc-backend |
| 2439 | (let* ((file (diff-find-file-name old t)) | 2439 | (not (eq diff-font-lock-syntax 'hunk-only))) |
| 2440 | (revision (and file (if (not old) (nth 1 diff-vc-revisions) | 2440 | (let* ((file (diff-find-file-name old t)) |
| 2441 | (or (nth 0 diff-vc-revisions) | 2441 | (revision (and file (if (not old) (nth 1 diff-vc-revisions) |
| 2442 | (vc-working-revision file)))))) | 2442 | (or (nth 0 diff-vc-revisions) |
| 2443 | (if file | 2443 | (vc-working-revision file)))))) |
| 2444 | (if (not revision) | 2444 | (when file |
| 2445 | ;; Get properties from the current working revision | 2445 | (if (not revision) |
| 2446 | (when (and (not old) (file-exists-p file) | 2446 | ;; Get properties from the current working revision |
| 2447 | (file-regular-p file)) | 2447 | (when (and (not old) (file-exists-p file) |
| 2448 | (let ((buf (get-file-buffer (expand-file-name file)))) | 2448 | (file-regular-p file)) |
| 2449 | ;; Try to reuse an existing buffer | 2449 | (let ((buf (get-file-buffer (expand-file-name file)))) |
| 2450 | (if buf | 2450 | ;; Try to reuse an existing buffer |
| 2451 | (with-current-buffer buf | 2451 | (if buf |
| 2452 | (diff-syntax-fontify-props nil text line-nb)) | 2452 | (with-current-buffer buf |
| 2453 | ;; Get properties from the file | 2453 | (diff-syntax-fontify-props nil text line-nb)) |
| 2454 | (with-temp-buffer | 2454 | ;; Get properties from the file |
| 2455 | (insert-file-contents file) | 2455 | (with-temp-buffer |
| 2456 | (diff-syntax-fontify-props file text line-nb))))) | 2456 | (insert-file-contents file) |
| 2457 | ;; Get properties from a cached revision | 2457 | (diff-syntax-fontify-props file text line-nb))))) |
| 2458 | (let* ((buffer-name (format " *diff-syntax:%s.~%s~*" | 2458 | ;; Get properties from a cached revision |
| 2459 | (expand-file-name file) | 2459 | (let* ((buffer-name (format " *diff-syntax:%s.~%s~*" |
| 2460 | revision)) | 2460 | (expand-file-name file) |
| 2461 | (buffer (gethash buffer-name | 2461 | revision)) |
| 2462 | diff-syntax-fontify-revisions))) | 2462 | (buffer (gethash buffer-name |
| 2463 | (unless (and buffer (buffer-live-p buffer)) | 2463 | diff-syntax-fontify-revisions))) |
| 2464 | (let* ((vc-buffer (ignore-errors | 2464 | (unless (and buffer (buffer-live-p buffer)) |
| 2465 | (vc-find-revision-no-save | 2465 | (let* ((vc-buffer (ignore-errors |
| 2466 | (expand-file-name file) revision | 2466 | (vc-find-revision-no-save |
| 2467 | diff-vc-backend | 2467 | (expand-file-name file) revision |
| 2468 | (get-buffer-create buffer-name))))) | 2468 | diff-vc-backend |
| 2469 | (when vc-buffer | 2469 | (get-buffer-create buffer-name))))) |
| 2470 | (setq buffer vc-buffer) | 2470 | (when vc-buffer |
| 2471 | (puthash buffer-name buffer | 2471 | (setq buffer vc-buffer) |
| 2472 | diff-syntax-fontify-revisions)))) | 2472 | (puthash buffer-name buffer |
| 2473 | (when buffer | 2473 | diff-syntax-fontify-revisions)))) |
| 2474 | (with-current-buffer buffer | 2474 | (when buffer |
| 2475 | (diff-syntax-fontify-props file text line-nb))))) | 2475 | (with-current-buffer buffer |
| 2476 | ;; If file is unavailable, get properties from the hunk alone | 2476 | (diff-syntax-fontify-props file text line-nb)))))))) |
| 2477 | (setq file (car (diff-hunk-file-names old))) | 2477 | (let ((file (car (diff-hunk-file-names old)))) |
| 2478 | (with-temp-buffer | 2478 | (cond |
| 2479 | (insert text) | 2479 | ((and file diff-default-directory |
| 2480 | (diff-syntax-fontify-props file text line-nb t))))) | 2480 | (not (eq diff-font-lock-syntax 'hunk-only)) |
| 2481 | ((and diff-default-directory | 2481 | (not diff-vc-backend) |
| 2482 | (not (eq diff-font-lock-syntax 'hunk-only))) | 2482 | (file-readable-p file) (file-regular-p file)) |
| 2483 | (let ((file (car (diff-hunk-file-names old)))) | 2483 | ;; Try to get full text from the file. |
| 2484 | (if (and file (file-exists-p file) (file-regular-p file)) | 2484 | (with-temp-buffer |
| 2485 | ;; Try to get full text from the file | 2485 | (insert-file-contents file) |
| 2486 | (with-temp-buffer | 2486 | (diff-syntax-fontify-props file text line-nb))) |
| 2487 | (insert-file-contents file) | 2487 | ;; Otherwise, get properties from the hunk alone |
| 2488 | (diff-syntax-fontify-props file text line-nb)) | 2488 | ((memq diff-font-lock-syntax '(hunk-also hunk-only)) |
| 2489 | ;; Otherwise, get properties from the hunk alone | 2489 | (with-temp-buffer |
| 2490 | (with-temp-buffer | 2490 | (insert text) |
| 2491 | (insert text) | 2491 | (diff-syntax-fontify-props file text line-nb t)))))))) |
| 2492 | (diff-syntax-fontify-props file text line-nb t))))) | ||
| 2493 | ((memq diff-font-lock-syntax '(hunk-also hunk-only)) | ||
| 2494 | (let ((file (car (diff-hunk-file-names old)))) | ||
| 2495 | (with-temp-buffer | ||
| 2496 | (insert text) | ||
| 2497 | (diff-syntax-fontify-props file text line-nb t))))))) | ||
| 2498 | 2492 | ||
| 2499 | ;; Put properties over the hunk text | 2493 | ;; Put properties over the hunk text |
| 2500 | (goto-char beg) | 2494 | (goto-char beg) |