branch: elpa/magit
commit 1d13151c27f70235a2f1a413f27c02ccab09f289
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>
magit-file-region-line-numbers: New function
- Only include line at end of region if at least one character
on that line falls within the region.
- Never include the line after the end of the file. Emacs thinks
there is another line there, but everything else, including Git,
disagrees.
- Use `line-number-at-pos's ABSOLUTE argument instead of `widen'.
- It would have been better to define it in "magit-base.el", if
only `magit-buffer-file-name' were defined this early.
---
lisp/magit-log.el | 16 +---------------
lisp/magit-mode.el | 16 ++++++++++++++++
2 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/lisp/magit-log.el b/lisp/magit-log.el
index 489d3131afa..6c0159913c5 100644
--- a/lisp/magit-log.el
+++ b/lisp/magit-log.el
@@ -756,21 +756,7 @@ completion candidates."
With a prefix argument or when `--follow' is an active log
argument, then follow renames. When the region is active,
restrict the log to the lines that the region touches."
- (interactive
- (cons current-prefix-arg
- (and (region-active-p)
- (magit-file-relative-name)
- (not (derived-mode-p 'dired-mode))
- (save-restriction
- (widen)
- (list (line-number-at-pos (region-beginning))
- (line-number-at-pos
- (let ((end (region-end)))
- (if (char-after end)
- end
- ;; Ensure that we don't get the line number
- ;; of a trailing newline.
- (1- end)))))))))
+ (interactive (cons current-prefix-arg (magit-file-region-line-numbers)))
(require 'magit)
(if-let ((file (magit-file-relative-name)))
(magit-log-setup-buffer
diff --git a/lisp/magit-mode.el b/lisp/magit-mode.el
index fa81b340356..baf35f88ac4 100644
--- a/lisp/magit-mode.el
+++ b/lisp/magit-mode.el
@@ -1499,6 +1499,22 @@ The additional output can be found in the *Messages*
buffer."
(message " %-50s %f" fn (benchmark-elapse (funcall
fn))))))))
((run-hooks hook))))
+(defun magit-file-region-line-numbers ()
+ "Return the bounds of the region as line numbers.
+The returned value has the form (BEGINNING-LINE END-LINE). If
+the region end at the beginning of a line, do not include that
+line. Avoid including the line after the end of the file."
+ (and (or magit-buffer-file-name buffer-file-name)
+ (region-active-p)
+ (not (= (region-beginning) (region-end) (1+ (buffer-size))))
+ (let ((beg (region-beginning))
+ (end (min (region-end) (buffer-size))))
+ (list (line-number-at-pos beg t)
+ (line-number-at-pos (if (= (magit--bol-position end) end)
+ (max beg (1- end))
+ end)
+ t)))))
+
;;; _
(provide 'magit-mode)
;;; magit-mode.el ends here