ol-gitlab.el (2949B)
1 ;;; ol-gitlab.el --- Links to Gitlab -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2020 Vincent Demeester 4 5 ;; Author: Vincent Demeester <vincent@sbr.pm> 6 ;; Keywords: org link gitlab 7 ;; Version: 0.1 8 ;; URL: https://gitlab.com/vdemeester/vorg 9 ;; Package-Requires: ((emacs "26.0") (org "9.0")) 10 ;; 11 ;; This file is not part of GNU Emacs. 12 13 ;; This program is free software; you can redistribute it and/or 14 ;; modify it under the terms of the GNU General Public License as 15 ;; published by the Free Software Foundation; either version 3.0, or 16 ;; (at your option) any later version. 17 18 ;; This program is distributed in the hope that it will be useful, 19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 ;; GNU General Public License for more details. 22 23 ;; You should have received a copy of the GNU General Public License 24 ;; along with this program; if not, write to the Free Software 25 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 27 ;; 28 ;;; Commentary: 29 30 ;; This file implements links to Gitlab from within Org mode. 31 ;; gl:vdemeester/emacs-config : project 32 ;; gl:vdemeester/emacs-config#1 : issue #1 33 ;; gl:vdemeester/emacs-config##1 : merge-request #1 34 35 ;;; Code: 36 37 (require 'ol) 38 39 ;; Install the link type 40 (org-link-set-parameters "gl" 41 :follow #'org-gitlab-follow-link 42 :export #'org-gitlab-export 43 :face '(:foreground "DimGrey" :underline t)) 44 45 46 (defun org-gitlab-export (link description format) 47 "Export a gitlab page link from Org files." 48 (let ((path (org-gitlab-get-url link)) 49 (desc (or description link))) 50 (cond 51 ((eq format 'html) (format "<a hrefl=\"_blank\" href=\"%s\">%s</a>" path desc)) 52 ((eq format 'latex) (format "\\href{%s}{%s}" path desc)) 53 ((eq format 'texinfo) (format "@uref{%s,%s}" path desc)) 54 ((eq format 'ascii) (format "%s (%s)" desc path)) 55 (t path)))) 56 57 (defun org-gitlab-follow-link (issue) 58 "Browse gitlab issue/pr specified." 59 (browse-url (org-gitlab-get-url issue))) 60 61 (defun org-gitlab-get-url (path) 62 "Translate org-mode link `gh:foo/bar#1' to gitlab url." 63 (setq expressions (split-string path "#")) 64 (setq project (nth 0 expressions)) 65 (setq issue (nth 1 expressions)) 66 (setq mr (nth 2 expressions)) 67 (message (format "issue: %s" issue)) 68 (message (format "mr: %s" mr)) 69 (if (not (empty-string-p mr)) 70 (format "https://gitlab.com/%s/-/merge_requests/%s" project mr) 71 (if (not (empty-string-p issue)) 72 (format "https://gitlab.com/%s/-/issues/%s" project issue) 73 (format "https://gitlab.com/%s" project)))) 74 75 (defun empty-string-p (string) 76 "Return true if the STRING is empty or nil. Expects string type." 77 (or (null string) 78 (zerop (length (string-trim string))))) 79 80 (provide 'ol-gitlab) 81 ;;; ol-gitlab.el ends here