Skip to content

Commit

Permalink
Add org-mode link-type "https" to open local copies
Browse files Browse the repository at this point in the history
The https link-type opens the local copies of the Spacemacs documentation files with
the spacemacs/view-org-file function. It supports GitHub style heading links

For example, the link:

https://github.com/syl20bnr/spacemacs/blob/develop/layers/org/README.org#links

Will be handled similary to as if it was:

file:~/.emacs.d/layers/org/README.org::*links

Also the `space-doc' mode will be applied.
  • Loading branch information
JAremko committed Mar 22, 2016
1 parent b8f0ea7 commit 00c14dd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
34 changes: 31 additions & 3 deletions core/core-funcs.el
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,40 @@ Supported properties:
(find-file file)
(org-indent-mode)
(view-mode)
(when(and(boundp 'space-doc-mode)(fboundp 'space-doc-mode)(space-doc-mode)))

(goto-char (point-min))

;; Enable `space-doc-mode' if defined.
(when (and (boundp 'space-doc-mode)
(fboundp 'space-doc-mode))
(space-doc-mode))

(when anchor-text
(re-search-forward anchor-text))
(beginning-of-line)
(if (string= anchor-text "^")

(re-search-forward anchor-text)

(progn (save-excursion
(let
;; Copied from "toc-org" to avoid requiring the package.
((toc-regexp (concat "^*.*:toc\\([@_][0-9]\\|"
"\\([@_][0-9][@_][a-zA-Z]+\\)"
"\\)?:\\($\\|[^ ]*:$\\)")))
(when (re-search-forward toc-regexp nil t)
;; Expand TOC.
(outline-show-subtree))))

(when (re-search-forward anchor-text nil t)
(let ((cur-line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(toc-org-rel-link "\\[\\[\\#[[:alnum:]_-]+\\]\\[.*\\]\\]"))
;; If the `point' is over a TOC-org relative heading link -
;; go to the heading.
(when (string-match-p toc-org-rel-link cur-line)
(org-open-at-point)))))))

(beginning-of-line)

(cond
((eq expand-scope 'subtree)
Expand Down
34 changes: 34 additions & 0 deletions layers/org/local/space-doc/space-doc.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
;;
;; This file is not part of GNU Emacs.
;;
;; Description:
;; This package provides:
;; - `space-doc-mode' - buffer local minor mode
;; for viewing the Spacemacs documentation files.
;; The mode hides org meta tags to improve readability.
;; - `org-mode' link-type "https" that opens the local
;; copies of the Spacemacs documentation files with
;; `spacemacs/view-org-file' and supports GitHub style
;; heading links.
;;
;; For example, the link:
;; https://github.com/syl20bnr/spacemacs/blob/develop/layers/org/README.org#links
;; Will be handled similary to as if it was:
;; file:~/.emacs.d/layers/org/README.org::*links
;; Also the `space-doc' mode will be applied.

;;; License: GPLv3
;;; Code:
(require 'face-remap)
Expand Down Expand Up @@ -42,5 +58,23 @@ keeping their content visible."
(progn (message (format "space-doc-mode error:%s isn't an org-mode buffer" (buffer-name)))
(setq org-mode nil))))

(defun spacemacs//space-doc-open (path)
"If the `path' argument is a link to an .org file that is located
in the Spacemacs GitHub repository - Visit the local copy
of the file with `spacemacs/view-org-file'.
Open all other links with `browse-url'."
(let ((git-url-root-regexp
(concat "\\/\\/github\\.com\\/syl20bnr"
"\\/spacemacs\\/blob\\/[^/]+\\/\\(.*\\.org\\)\\(\\#.*\\)?")))
(if (string-match git-url-root-regexp path)
(spacemacs/view-org-file (concat user-emacs-directory
(match-string 1 path))
(or (match-string 2 path)
"^")
'subtree)
(browse-url (concat "https://" path)))))

(org-add-link-type "https" 'spacemacs//space-doc-open)

(provide 'space-doc)
;;; space-doc.el ends here

0 comments on commit 00c14dd

Please sign in to comment.