Created
August 24, 2013 21:00
-
-
Save richlowe/6330401 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun diff-line-info-as-kill (&optional prefix) | |
"Copy a chunk of text with the file and line number of the diff line at point. | |
If the diff line is a deletion, this will be the path and line | |
number from the old file with (old) appended, otherwise the path | |
and line number of the new file. | |
With a prefix argument, remove that meaning leading elements from | |
the path. The default is 1 (since that's mostly commonly | |
desired)." | |
(interactive "P") | |
(let* ((rev (save-excursion (beginning-of-line) (looking-at "[-<]"))) | |
(fname (remove-file-name-prefix (if (null prefix) 1 prefix) | |
(car (diff-hunk-file-names rev))))) | |
(set-text-properties 0 (length fname) nil fname) | |
(kill-new (format "%s:%d%s" fname | |
(diff-find-source-offset nil rev) | |
(if rev " (old)" ""))))) | |
;; Largely stolen from `diff-find-source-location' | |
(defun diff-find-source-offset (other-file rev) | |
(save-excursion | |
(let* ((char-offset (- (point) (diff-beginning-of-hunk t))) | |
(line (split-string | |
(if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) | |
(error "Can't find the hunk header") | |
(if rev (match-string 1) | |
(if (match-end 3) (match-string 3) | |
(unless (re-search-forward | |
diff-context-mid-hunk-header-re nil t) | |
(error "Can't find the hunk separator")) | |
(match-string 1)))) ",")) | |
(hunk (buffer-substring (point) (save-excursion (diff-end-of-hunk) | |
(point)))) | |
(old (diff-hunk-text hunk (not rev) char-offset)) | |
(new (diff-hunk-text hunk (not rev) char-offset)) | |
(line-offset | |
(length (replace-regexp-in-string "[^\n]" "" | |
(substring (car (if rev old new)) 0 | |
(cdr (if rev old new))))))) | |
(+ (string-to-number (car line)) line-offset)))) | |
(defun remove-file-name-prefix (n path) | |
(let ((path (split-string path "/" t))) | |
(mapconcat #'identity (last path (- (length path) n)) "/"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment