mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-19 a4b9172aefa91861b587831e06f55b1e19f3f3be
commit | author | age
5cb5f7 1 ;;; flycheck-pos-tip.el --- Display Flycheck errors in GUI tooltips -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2015-2016 Sebastian Wiesner <swiesner@lunaryorn.com>
4 ;; Copyright (C) 2014 Akiha Senda
5
6 ;; Author: Akiha Senda <senda.akiha@gmail.com>
7 ;;     Sebastian Wiesner <swiesner@lunaryorn.com>
8 ;; Maintainer: Sebastian Wiesner <swiesner@lunaryorn.com>
9 ;; URL: https://github.com/flycheck/flycheck-pos-tip
10 ;; Package-Version: 20180610.1615
11 ;; Keywords: tools, convenience
12 ;; Version: 0.4-cvs
13 ;; Package-Requires: ((emacs "24.1") (flycheck "0.22") (pos-tip "0.4.6"))
14
15 ;; This file is not part of GNU Emacs.
16
17 ;; This program is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; This program is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;; Provide an error display function to show errors in a tooltip.
33
34 ;;;; Setup
35
36 ;; (with-eval-after-load 'flycheck
37 ;;   (flycheck-pos-tip-mode))
38
39 ;;; Code:
40
41 (require 'flycheck)
42 (require 'pos-tip)
43
44 (defgroup flycheck-pos-tip nil
45   "Display Flycheck errors in tooltips."
46   :prefix "flycheck-pos-tip-"
47   :group 'flycheck
48   :link '(url-link :tag "Github" "https://github.com/flycheck/flycheck-pos-tip"))
49
50 (defcustom flycheck-pos-tip-max-width nil
51   "If non-nil, the max width of the tooltip in chars."
52   :group 'flycheck-pos-tip
53   :type '(choice (const :tag "Auto" nil)
54                  (integer :tag "Characters"))
55   :package-version '(flycheck-pos-tip . "0.4"))
56
57 (defcustom flycheck-pos-tip-timeout 5
58   "Time in seconds to hide the tooltip after."
59   :group 'flycheck-pos-tip
60   :type 'number
61   :package-version '(flycheck-pos-tip . "0.2"))
62
63 (defcustom flycheck-pos-tip-display-errors-tty-function
64   #'flycheck-display-error-messages
65   "Fallback function for error display on TTY frames.
66
67 Like `flycheck-display-errors-function'; called to show error
68 messages on TTY frames if `flycheck-pos-tip-mode' is active."
69   :group 'flycheck-pos-tip
70   :type 'function
71   :package-version '(flycheck-pos-tip . "0.2"))
72
73 (defun flycheck-pos-tip-error-messages (errors)
74   "Display ERRORS, using a graphical tooltip on GUI frames."
75   (when errors
76     (if (display-graphic-p)
77         (let ((message (mapconcat #'flycheck-error-format-message-and-id
78                                   errors "\n\n"))
79               (line-height (car (window-line-height))))
80           (pos-tip-show message nil nil nil flycheck-pos-tip-timeout
81                         flycheck-pos-tip-max-width nil
82                         ;; Add a little offset to the tooltip to move it away
83                         ;; from the corresponding text in the buffer.  We
84                         ;; explicitly take the line height into account because
85                         ;; pos-tip computes the offset from the top of the line
86                         ;; apparently.
87                         nil (and line-height (+ line-height 5))))
88       (funcall flycheck-pos-tip-display-errors-tty-function errors))))
89
90 (defun flycheck-pos-tip-hide-messages ()
91   "Hide messages currently being shown if any."
92   (if (display-graphic-p)
93       (pos-tip-hide)
94     (flycheck-hide-error-buffer)))
95
96 (defvar flycheck-pos-tip-old-display-function nil
97   "The former value of `flycheck-display-errors-function'.")
98
99 ;;;###autoload
100 (define-minor-mode flycheck-pos-tip-mode
101   "A minor mode to show Flycheck error messages in a popup.
102
103 When called interactively, toggle `flycheck-pos-tip-mode'.  With
104 prefix ARG, enable `flycheck-pos-tip-mode' if ARG is positive,
105 otherwise disable it.
106
107 When called from Lisp, enable `flycheck-pos-tip-mode' if ARG is
108 omitted, nil or positive.  If ARG is `toggle', toggle
109 `flycheck-pos-tip-mode'.  Otherwise behave as if called
110 interactively.
111
112 In `flycheck-pos-tip-mode' show Flycheck's error messages in a
113 GUI tooltip.  Falls back to `flycheck-display-error-messages' on
114 TTY frames."
115   :global t
116   :group 'flycheck
117   (let ((hooks '(post-command-hook focus-out-hook)))
118     (cond
119      ;; Use our display function and remember the old one but only if we haven't
120      ;; yet configured it, to avoid activating twice.
121      ((and flycheck-pos-tip-mode
122            (not (eq flycheck-display-errors-function
123                     #'flycheck-pos-tip-error-messages)))
124       (setq flycheck-pos-tip-old-display-function
125             flycheck-display-errors-function
126             flycheck-display-errors-function
127             #'flycheck-pos-tip-error-messages)
128       (dolist (hook hooks)
129         (add-hook hook #'flycheck-pos-tip-hide-messages)))
130      ;; Reset the display function and remove ourselves from all hooks but only
131      ;; if the mode is still active.
132      ((and (not flycheck-pos-tip-mode)
133            (eq flycheck-display-errors-function
134                #'flycheck-pos-tip-error-messages))
135       (setq flycheck-display-errors-function
136             flycheck-pos-tip-old-display-function
137             flycheck-pos-tip-old-display-function nil)
138       (dolist (hook hooks)
139         (remove-hook hook 'flycheck-pos-tip-hide-messages))))))
140
141 (provide 'flycheck-pos-tip)
142
143 ;;; flycheck-pos-tip.el ends here