From 7144b05b9d024222cb7e46a370ba3d974ae235af Mon Sep 17 00:00:00 2001 From: Konubinix Date: Fri, 11 Jul 2014 09:10:02 +0200 Subject: [PATCH] org-git-link.el: Add support for line in org-git-ling * org-git-link.el (org-git-open): Jump to the line if provided in the link. (org-git-split-string): Parse a third element of the list and change the error message accordingly. (org-git-create-git-link): Accept the third optional line argument. (org-git-store-link): call `org-git-create-git-link' with the current line number. --- contrib/lisp/org-git-link.el | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/contrib/lisp/org-git-link.el b/contrib/lisp/org-git-link.el index 7d95bbb..ad0ce71 100644 --- a/contrib/lisp/org-git-link.el +++ b/contrib/lisp/org-git-link.el @@ -98,10 +98,12 @@ (let* ((strlist (org-git-split-string str)) (filepath (first strlist)) (commit (second strlist)) + (line (third strlist)) (dirlist (org-git-find-gitdir (file-truename filepath))) (gitdir (first dirlist)) (relpath (second dirlist))) - (org-git-open-file-internal gitdir (concat commit ":" relpath)))) + (org-git-open-file-internal gitdir (concat commit ":" relpath)) + (when line (goto-line (string-to-int line))))) ;; Utility functions (file names etc) @@ -141,16 +143,19 @@ ;; splitting the link string ;; Both link open functions are called with a string of -;; consisting of two parts separated by a double colon (::). +;; consisting of three parts separated by a double colon (::). (defun org-git-split-string (str) - "Given a string of the form \"str1::str2\", return a list of - two substrings \'(\"str1\" \"str2\"). If the double colon is mising, take str2 to be the empty string." + "Given a string of the form \"str1::str2::str3\", return a list of + three substrings \'(\"str1\" \"str2\" \"str3\"). If there are less +than two double colons, str2 and/or str3 may be set the empty string." (let ((strlist (split-string str "::"))) (cond ((= 1 (length strlist)) - (list (car strlist) "")) + (list (car strlist) "" "")) ((= 2 (length strlist)) + (append strlist (list ""))) + ((= 3 (length strlist)) strlist) - (t (error "org-git-split-string: only one :: allowed: %s" str))))) + (t (error "org-git-split-string: only one or two :: allowed: %s" str))))) ;; finding the file name part of a commit (defun org-git-link-filename (str) @@ -168,22 +173,24 @@ (concat branch "@{" timestring "}")) -(defun org-git-create-git-link (file) +(defun org-git-create-git-link (file &optional line) "Create git link part to file at specific time" (interactive "FFile: ") (let* ((gitdir (first (org-git-find-gitdir (file-truename file)))) (branchname (org-git-get-current-branch gitdir)) (timestring (format-time-string "%Y-%m-%d" (current-time)))) - (concat "git:" file "::" (org-git-create-searchstring branchname timestring)))) + (concat "git:" file "::" (org-git-create-searchstring branchname timestring) + (if line (format "::%s" line) "")))) (defun org-git-store-link () "Store git link to current file." (when (buffer-file-name) - (let ((file (abbreviate-file-name (buffer-file-name)))) + (let ((file (abbreviate-file-name (buffer-file-name))) + (line (line-number-at-pos))) (when (org-git-gitrepos-p file) (org-store-link-props :type "git" - :link (org-git-create-git-link file)))))) + :link (org-git-create-git-link file line)))))) (add-hook 'org-store-link-functions 'org-git-store-link) -- 2.0.0.rc2