summaryrefslogtreecommitdiff
path: root/home/dots/.emacs.d/elisp/org-link-minor-mode.el
blob: 7f1faaa4448c95329144945a56976a9e2b6f1b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
;;; org-link-minor-mode.el --- Enable org-mode links in non-org modes -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2012-2020
;;
;; Author: Sean O'Halpin <sean.ohalpin@gmail.com>
;; Maintainer: Sean O'Halpin <sean.ohalpin@gmail.com>
;; Created: 20120825
;; Modified: 20200129
;; Version: 0.0.3
;; Package-Requires: ((emacs "24.3"))
;; Package-Version: 20200129.0141
;; Keywords: hypermedia
;; Url: https://github.com/seanohalpin/org-link-minor-mode
;; 
;; Changes for org v9: Stefan-W. Hahn <stefan dot hahn at s-hahn dot de>
;;
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; 
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; Enables org-mode links of the form:
;; 
;;   http://www.bbc.co.uk
;;   man:emacs
;;   info:emacs
;;   <http://www.bbc.co.uk>
;;   [[http://www.bbc.co.uk][BBC]]
;;   [[org-link-minor-mode]]
;;   [2012-08-18]
;;   <2012-08-18>
;; 
;; Note that `org-toggle-link-display' will also work when this mode
;; is enabled.
;;
;;; Code:

(require 'org-element)

;; Following declarations are necessary to make the byte compiler happy.

;; For org v8 compatibility (if used with org v9)
(declare-function org-activate-plain-links "org" (limit))
(declare-function org-activate-angle-links "org" (limit))
(declare-function org-activate-bracket-links "org" (limit))
(declare-function org-decompose-region "org-compat" (beg end))

;; For org v9 compatibility (if used with org v8)
(declare-function org-activate-links "org" (limit))
(declare-function org-activate-dates "org" (limit))

(defun org-link-minor-mode--unfontify-region (beg end)
  "Remove org-link fontification between BEG and END."
  (font-lock-default-unfontify-region beg end)
  (let* ((buffer-undo-list t)
         (inhibit-read-only t) (inhibit-point-motion-hooks t)
         (inhibit-modification-hooks t)
         deactivate-mark buffer-file-name buffer-file-truename)
    (if (fboundp 'org-decompose-region)
        (org-decompose-region beg end)
      (decompose-region beg end))
    (remove-text-properties beg end
                            '(mouse-face t keymap t org-linked-text t
                                         invisible t intangible t
                                         help-echo t rear-nonsticky t
                                         htmlize-link t
                                         org-no-flyspell t org-emphasis t))
    (org-remove-font-lock-display-properties beg end)))

(defvar org-link-minor-mode-map (make-sparse-keymap)
  "Local keymap.")
(make-variable-buffer-local 'org-link-minor-mode-map)

;;;###autoload
(define-minor-mode org-link-minor-mode
  "Toggle display of org-mode style links in non-org-mode buffers."
  :lighter " org-link"
  :keymap org-link-minor-mode-map
  (let ((lk org-highlight-links)
        org-link-minor-mode-keywords)
    (if (fboundp 'org-activate-links)
        ;; from Org v9.2
        (setq org-link-minor-mode-keywords
              (list
               '(org-activate-links)
               (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
               (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
               (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
               (when (memq 'footnote lk) '(org-activate-footnote-links))))
      (setq org-link-minor-mode-keywords
            (list
             (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
             (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
             (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
             (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
             (when (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
             (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
             (when (memq 'footnote lk) '(org-activate-footnote-links)))))
    (if org-link-minor-mode
        (if (derived-mode-p 'org-mode)
            (progn
              (message "org-mode doesn't need org-link-minor-mode")
              (org-link-minor-mode -1))
          (font-lock-add-keywords nil org-link-minor-mode-keywords t)
          (kill-local-variable 'org-mouse-map)
          (setq-local org-mouse-map
                      (let ((map (make-sparse-keymap)))
                        (define-key map [return] 'org-open-at-point)
                        (define-key map [tab] 'org-next-link)
                        (define-key map [backtab] 'org-previous-link)
                        (define-key map [mouse-2] 'org-open-at-point)
                        (define-key map [follow-link] 'mouse-face)
                        map))
          (setq-local font-lock-unfontify-region-function
                      'org-link-minor-mode--unfontify-region)
          (setq-local org-descriptive-links nil)
          (org-toggle-link-display))
      (unless (derived-mode-p 'org-mode)
        (font-lock-remove-keywords nil org-link-minor-mode-keywords)
        (setq org-descriptive-links t)
        (org-toggle-link-display)
        (kill-local-variable 'org-descriptive-links)
        (kill-local-variable 'org-mouse-map)
        (kill-local-variable 'font-lock-unfontify-region-function)))))

(provide 'org-link-minor-mode)
;;; org-link-minor-mode.el ends here