Add a function to prettify format explanation long string block
[org-tag-eldoc.git] / org-tag-eldoc.el
blob623baf4d7742c1140cca21df75cbd617e28a8326
1 ;;; org-tag-eldoc.el --- Display tag explanation in Eldoc -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Authors: stardiviner <numbchild@gmail.com>
5 ;; Package-Requires: ((emacs "26.1") (request "0.3.3"))
6 ;; Version: 0.1.0
7 ;; Keywords: org
8 ;; Homepage: https://repo.or.cz/org-tag-eldoc.git
10 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
12 ;; org-tag-eldoc is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; org-tag-eldoc is distributed in the hope that it will be useful, but WITHOUT
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 ;; License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Display tag explanation in Eldoc when point on tag.
29 ;;; Usage:
30 ;;;
31 ;;; (add-hook 'org-mode-hook #'org-tag-eldoc-setup)
33 ;;; Code:
35 (require 'org)
36 (require 'org-tag-eldoc-common)
37 (require 'org-tag-eldoc-database)
38 (require 'org-tag-eldoc-wikipedia)
39 (require 'org-tag-eldoc-urban-dictionary)
40 (require 'org-tag-eldoc-pixiv-encyclopedia)
41 (require 'org-tag-eldoc-moegirl)
42 (require 'subr-x)
45 (defgroup org-tag-eldoc nil
46 "Customize group of `org-tag-eldoc-mode'."
47 :prefix "org-tag-eldoc-"
48 :group 'org-tags)
50 (defcustom org-tag-eldoc-tag-explanation-functions
51 '(org-tag-eldoc-database-query
52 org-tag-eldoc-wikipedia-query
53 org-tag-eldoc-urban-dictionary-query
54 org-tag-eldoc-pixiv-encyclopedia-query
55 org-tag-eldoc-moegirl-query)
56 "A list of functions to be executed for query tag explanation."
57 :type 'list
58 :safe #'listp
59 :group 'org-tag-eldoc)
61 (defcustom org-tag-eldoc-request-proxy
62 (or url-proxy-services
63 '(("http" . "127.0.0.1:7890")
64 ("https" . "127.0.0.1:7890")))
65 "The proxy services inherited from `url-proxy-services'."
66 :type 'alist
67 :group 'org-tag-eldoc)
69 (defcustom org-tag-eldoc-tag-explanations-alist
70 '(("Linux" . "Linux (/ˈlɪnʊks/ LIN-uuks) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds.")
71 ("Emacs" . "Emacs /ˈiːmæks/ ⓘ, originally named EMACS (an acronym for \"Editor Macros\"), is a family of text editors that are characterized by their extensibility. The manual for the most widely used variant, GNU Emacs, describes it as \"the extensible, customizable, self-documenting, real-time display editor\". Development of the first Emacs began in the mid-1970s, and work on GNU Emacs, directly descended from the original, is ongoing; its latest version is 29.2, released January 2024.")
72 ("GNU" . "GNU (/ɡnuː/ ⓘ) is an extensive collection of free software (385 packages as of September 2023), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operating systems popularly known as Linux. Most of GNU is licensed under the GNU Project's own General Public License (GPL)."))
73 "Alist of cons cell with tag and explanation."
74 :type 'alist
75 :safe #'listp
76 :group 'org-tag-eldoc)
78 (defcustom org-tag-eldoc-translation nil
79 "Boolean value to toggle translation for explanation."
80 :type 'boolean
81 :safe #'booleanp)
84 (defun org-tag-eldoc-translate (explanation)
85 "Translate EXPLANATION."
86 ;; TODO:
87 (when org-tag-eldoc-translation
89 explanation)
91 (defun org-tag-eldoc--format-explanation (explanation)
92 "Format the EXPLANATION string."
93 ;; prettify display explanation long string.
94 (org-tag-eldoc-translate
95 (string-fill
96 (if (string-match-p "\n" explanation) ; if explanation is a large block of paragraphs.
97 explanation
98 ;; if only have single long line with several paragraphs, then break paragraphs into lines.
99 (string-replace ". " ". \n\n" explanation))
100 fill-column)))
102 (defun org-tag-eldoc-tag-explanation (&optional tag)
103 "Display tag explanation in Eldoc when point on TAG."
104 (let ((tag (or tag (buffer-substring-no-properties (thing-at-point 'symbol)))))
105 (if-let ((explanation (cdr (assoc tag org-tag-eldoc-tag-explanations-alist))))
106 (org-tag-eldoc--format-explanation explanation)
107 (let ((explanation (seq-some
108 (lambda (f)
109 ;; reset `org-tag-eldoc--explanation' to avoid bellowing `seq-some' chaos.
110 (setq org-tag-eldoc--explanation nil)
111 (apply f (list tag))
112 (unless (string-equal org-tag-eldoc--explanation "nil")
113 org-tag-eldoc--explanation))
114 org-tag-eldoc-tag-explanation-functions)))
115 ;; cache already queried result explanation into `org-tag-eldoc-tag-explanations-alist'.
116 (when explanation
117 (add-to-list 'org-tag-eldoc-tag-explanations-alist (cons tag explanation)))
118 (org-tag-eldoc--format-explanation explanation)))))
120 (defun org-tag-eldoc-function (&rest args)
121 "The eldoc function to be added into `eldoc-documentation-functions' with ARGS."
122 ;; NOTE: `org-element-at-point' return `headline' instead of `tags.' Need Org mode to implement it.
123 ;; Use `thing-at-point' to workaround the missing `tags' syntax element node in `org-element-at-point' / `org-context'.
124 (let ((tag (thing-at-point 'symbol))
125 (tags (org-get-tags nil 'local)))
126 (when (member tag tags) (org-tag-eldoc-tag-explanation tag))))
128 ;;; `eldoc-documentation-function', `eldoc-documentation-functions'
129 ;;;###autoload
130 (defun org-tag-eldoc-setup ()
131 "Setup `eldoc-documentation-functions'."
132 (eldoc-mode t)
133 (add-hook 'eldoc-documentation-functions #'org-tag-eldoc-function -10 t))
135 ;;;###autoload
136 (defun org-tag-eldoc-enable ()
137 "Enable `org-tag-eldoc-mode'."
138 (add-hook 'org-mode-hook #'org-tag-eldoc-setup 100))
140 ;;;###autoload
141 (defun org-tag-eldoc-disable ()
142 "Disable `org-tag-eldoc-mode'."
143 (remove-hook 'org-mode-hook #'org-tag-eldoc-setup))
145 ;;;###autoload
146 (define-minor-mode org-tag-eldoc-mode
147 "A minor mode that display Org tag explanation through Eldoc."
148 :init-value nil
149 :lighter nil
150 :group 'org-tag-eldoc
151 :global t
152 (if org-tag-eldoc-mode
153 (org-tag-eldoc-enable)
154 (org-tag-eldoc-disable)))
156 ;;; TODO: Add mouse hover popup info support.
161 (provide 'org-tag-eldoc)
163 ;;; org-tag-eldoc.el ends here