initial commit
[emacs-init.git] / nxhtml / related / flymakemsg.el
1 ;;; flymakemsg.el --- Show flymake compile errors in echo area
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: 2009-11-21 Sat
5 ;; Version: 0.1
6 ;; Last-Updated: 2009-11-21 Sat
7 ;; URL:
8 ;; Keywords:
9 ;; Compatibility:
10 ;;
11 ;; Features that might be required by this library:
12 ;;
13   ;; `backquote', `bytecomp'.
14 ;;
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;;
17 ;;; Commentary:
18 ;;
19 ;; Show flymake error messages in minibuffer when point is on a
20 ;; flymake error overlay.
21 ;;
22 ;; To use it just load this file. Put this in .emacs:
23 ;;
24 ;;   (require 'flymakemsg)
25 ;;
26 ;; This file run `defadvice' on some functions in `flymake-mode'.
27 ;; This code started from an idea in a paste.
28 ;;
29 ;; Note: This code breaks Emacs conventions since it does the
30 ;; defadvising when you just loads this file.
31 ;;
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;;
34 ;;; Change log:
35 ;;
36 ;;
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 ;;
39 ;; This program is free software; you can redistribute it and/or
40 ;; modify it under the terms of the GNU General Public License as
41 ;; published by the Free Software Foundation; either version 3, or
42 ;; (at your option) any later version.
43 ;;
44 ;; This program is distributed in the hope that it will be useful,
45 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
46 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
47 ;; General Public License for more details.
48 ;;
49 ;; You should have received a copy of the GNU General Public License
50 ;; along with this program; see the file COPYING.  If not, write to
51 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
52 ;; Floor, Boston, MA 02110-1301, USA.
53 ;;
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
55 ;;
56 ;;; Code:
57
58 (eval-when-compile (require 'flymake))
59
60 (defun flymakemsg-show-err-at-point ()
61   "If point is on a flymake error, show it in echo area.
62 Protected to run in timers and hooks."
63   (condition-case err
64       (flymakemsg-show-err-at-point-1)
65     (error (message "%s" err))))
66
67 (defvar flymakemsg-last-errovl nil)
68
69 (defun flymakemsg-show-err-at-point-1 ()
70   "If point is on a flymake error, show it in echo area."
71   (interactive)
72   (when flymake-mode
73     (let ((flyovl (flymakemsg-get-errovl (point))))
74       (unless (eq flyovl flymakemsg-last-errovl)
75         (setq flymakemsg-last-errovl flyovl)
76         (when flyovl
77           (message "%s" (propertize
78                                     (overlay-get flyovl 'help-echo)
79                                     'face 'flymake-errline)))))))
80
81 (defun flymakemsg-get-errovl (POS)
82   "Get flymake error overlay at POS."
83   (catch 'errovl
84     (dolist (ovl (overlays-at POS))
85       (when (eq 'flymake-errline (overlay-get ovl 'face))
86         (throw 'errovl ovl)))))
87
88 (defadvice flymake-mode (after
89                          flymakemsg-ad-flymake-mode
90                          activate compile)
91   "Turn on showing of flymake errors then point is on them.
92 This shows the error in the echo area."
93   (if flymake-mode
94       nil ;;(add-hook 'post-command-hook 'flymakemsg-post-command t t)
95     (remove-hook 'post-command-hook 'flymakemsg-post-command t)))
96
97 (defadvice flymake-log (after
98                         flymakemsg-ad-flymake-log
99                         activate compile)
100   "Display error on current line if any."
101   ;;(message "flymake-log defadvice called")
102   (if (not flymake-err-info)
103       (remove-hook 'post-command-hook 'flymakemsg-post-command t)
104     (add-hook 'post-command-hook 'flymakemsg-post-command t t)
105     ;; Wait, because there is another message first.
106     (flymakemsg-start-msg-timer 3.0)))
107
108 (defun flymakemsg-post-command ()
109   ;; Wait to not disturb to much.
110   (flymakemsg-start-msg-timer 0.2))
111
112 (defvar flymakemsg-msg-timer nil)
113
114 (defun flymakemsg-cancel-msg-timer ()
115   (when (timerp flymakemsg-msg-timer)
116     (cancel-timer flymakemsg-msg-timer)))
117
118 (defun flymakemsg-start-msg-timer (delay)
119   (flymakemsg-cancel-msg-timer)
120   (run-with-idle-timer delay nil 'flymakemsg-show-err-at-point))
121
122 ;;; I have no idea why it was done the way below. It was in the paste.
123 ;;; It seems very unnecessary but I keep it for now.
124 ;;
125 ;; (defun fly-pyflake-determine-message (err)
126 ;;   "pyflake is flakey if it has compile problems, this adjusts the
127 ;; message to display, so there is one ;)"
128 ;;   (cond ((not (or (eq major-mode 'Python) (eq major-mode 'python-mode) t)))
129 ;;      ((null (flymake-ler-file err))
130 ;;       ;; normal message do your thing
131 ;;       (flymake-ler-text err))
132 ;;      (t ;; could not compile err
133 ;;       (format "compile error, problem on line %s" (flymake-ler-line err)))))
134
135 ;; (let ((line-no (line-number-at-pos)))
136 ;;   (dolist (elem flymake-err-info)
137 ;;     (if (eq (car elem) line-no)
138 ;;         (let ((err (car (second elem))))
139 ;;           (message "%s" (fly-pyflake-determine-message err))))))
140
141
142 (provide 'flymakemsg)
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 ;;; flymakemsg.el ends here