summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/related/flymu.el
blob: 6b3a552873a27c66e3c32183e00ee5c7c0b3906c (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
;;; flymu.el --- Flymake for mumamo-mode
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: Sun Dec 02 14:52:32 2007
;; Version: 0.1
;; Last-Updated:
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Flymake syntax checks for mumamo chunks.
;;
;; Not ready yet!!!
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program 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 2, or
;; (at your option) any later version.
;;
;; This program 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; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(require 'flymake)

;;(flymu-make-major-mode-alist)
(defun flymu-make-major-mode-alist ()
  "Grab values from `flymake-allowed-file-name-masks'.
We need a list of major modes and the corresponding init and
cleanup functions for flymake. This functions creates such a list
from flymakes dito list for file names."
  (let ((allowed nil))
    (save-match-data
      (dolist (regexp-init flymake-allowed-file-name-masks)
        (let* ((regexp (car regexp-init))
               (init   (cdr regexp-init))
               ;; Make it as simple as possible. First see if the same
               ;; regexp is used:
               (mode (let ((m (cdr (assoc regexp auto-mode-alist))))
                       ;; Don't use this if it is complicated:
                       (when (commandp m) m)))
               (ext regexp))
          (unless mode
            ;; Try to make a simple file name, this could be made
            ;; better but I do not know if that would be meaningful:
            (setq ext (replace-regexp-in-string "\\\\\\." "." ext))
            (setq ext (replace-regexp-in-string "\\\\'" "" ext))
            (setq ext (replace-regexp-in-string "[\\$?+*]" "" ext))
            ;; Next compare the filename against the entries in
            ;; auto-mode-alist. The code is from `set-auto-mode'.
            (let ((name ext)
                  (done nil))
              (while name
                ;; Find first matching alist entry.
                (setq mode
                      (if (memq system-type '(vax-vms windows-nt cygwin))
                          ;; System is case-insensitive.
                          (let ((case-fold-search t))
                            (assoc-default name auto-mode-alist
                                           'string-match))
                        ;; System is case-sensitive.
                        (or
                         ;; First match case-sensitively.
                         (let ((case-fold-search nil))
                           (assoc-default name auto-mode-alist
                                          'string-match))
                         ;; Fallback to case-insensitive match.
                         (and auto-mode-case-fold
                              (let ((case-fold-search t))
                                (assoc-default name auto-mode-alist
                                               'string-match))))))
                (if (and mode
                         (consp mode)
                         (cadr mode))
                    (setq name (substring name 0 (match-beginning 0)))
                  (setq name)))))
          (when (and mode
                     ;; nxml-mode's do not need flymake:
                     (let ((major-mode mode))
                       (not (derived-mode-p 'nxml-mode))))
            (let ((rec (append (list mode) init)))
              (when (= (length rec) 2)
                (setq rec (append rec (list nil))))
              (add-to-list 'allowed rec))))))
    allowed))

(defcustom flymu-allowed-major-modes (flymu-make-major-mode-alist)
  "Major modes syntax checking is allowed for."
  :type '(repeat (list (function :tag "Major mode")
                       (function :tag "Init function")
                       (choice (const :tag "No cleanup function" nil)
                               (function :tag "Cleanup function"))))
  :set-after '(flymake-allowed-file-name-masks)
  :group 'flymu)

(defvar flymu-mumamo-chunk nil)
(make-variable-buffer-local 'flymu-mumamo-chunk)


;; Fix-me: What to check? When? Make flymu-mumamo-chunk a function
;; instead? Mark chunks for checking - let mumamo do that? Flymake
;; should be able to mark a chunk to, even if it is not a whole
;; line. What about line numbers?

;; Advice these functions:
(defadvice flymake-get-file-name-mode-and-masks (around
                                                 flymu-ad-flymake-get-file-name-mode-and-masks
                                                 (file-name))
  "Make flymake init file selection according to mode."
  (if flymu-mumamo-chunk
      (let ((major (overlay-get ovl 'mumamo-major-mode))
            (rec (assq major flymu-allowed-major-modes)))
        (when rec
          (setq ad-return-value (cdr rec))))
    ad-do-it))
(ad-activate 'flymake-get-file-name-mode-and-masks)

;;(defun flymake-save-buffer-in-file (file-name)
(defadvice flymake-save-buffer-in-file (around
                                        flymu-ad-flymake-save-buffer-in-file
                                        (file-name))
  (if flymu-mumamo-chunk
      (let ((min (overlay-start flymu-mumamo-chunk))
            (max (overlay-end   flymu-mumamo-chunk)))
        (make-directory (file-name-directory file-name) 1)
        (write-region min max file-name nil 566)
        (flymake-log 3 "saved chunk %s:%s-%s in file %s" (buffer-name) min ma file-name))
    ad-do-it))

(provide 'flymu)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; flymu.el ends here