summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/markchars.el
blob: e1179b7d7911f170ef065e09d31f9dc985020f05 (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
;;; markchars.el --- Mark chars fitting certain characteristics
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2010-03-22 Mon
;; Version:
;; Last-Updated: 2010-03-25 Thu
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   Required feature `markchars' was not provided.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Mark special chars, by default non-ascii, non-IDN chars. See
;; `markchars-mode'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; 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 3, 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 'idn)

;;;###autoload
(defgroup markchars nil
  "Customization group for `markchars-mode'."
  :group 'convenience)

(defface markchars-light
  '((t (:underline "light blue")))
  "Light face for `markchars-mode' char marking."
  :group 'markchars)

(defface markchars-heavy
  '((t (:underline "magenta")))
  "Heavy face for `markchars-mode' char marking."
  :group 'markchars)

(defcustom markchars-face 'markchars-heavy
  "Pointer to face used for marking chars."
  :type 'face
  :group 'markchars)

;; (markchars-nonidn-fun (point-max))
;; åäö
;; character: å (229, #o345, #xe5)
;; (idn-is-recommended 229) => t
;; 152F ;	00B7 0034 ;	SL	# ( ᔯ → ·4 ) CANADIAN SYLLABICS YWE → MIDDLE DOT, DIGIT FOUR	# {source:835} ᐧ4 {[source:696]}

(defun markchars-nonidn-fun (bound)
  "Font lock matcher for non-IDN, non-ascii chars."
  (let* ((beg (catch 'beg
               (while (< (point) bound)
                 (let ((char (char-after)))
                   (unless (or (< char 256)
                               (idn-is-recommended char))
                     (throw 'beg (point)))
                   (forward-char)))))
         (end (when beg
                (catch 'end
                  (while (< (point) bound)
                    (let ((char (char-after (point))))
                      (when (or (< char 256)
                                (idn-is-recommended char))
                        (throw 'end (point)))
                      (forward-char)))))))
    (when beg
      (setq end (or end bound))
      (set-match-data (list (copy-marker beg) (copy-marker end)))
      t)))

(defcustom markchars-keywords (or (when (fboundp 'idn-is-recommended) 'markchars-nonidn-fun)
                                  "[[:nonascii:]]+")
  "Regexp or function for font lock to use for characters to mark.
By default it matches non-IDN, non-ascii chars."
  :type '(choice (const :tag "Non-ascii chars" "[[:nonascii:]]+")
                 (const :tag "Non IDN chars (Unicode.org tr39 suggestions)" markchars-nonidn-fun))
  :group 'markchars)

(defvar markchars-used-keywords nil
  "Keywords currently used for font lock.")
(put 'markchars-used-keywords 'permanent-local t)

(defun markchars-set-keywords ()
  "Set `markchars-used-keywords' from options."
  (set (make-local-variable 'markchars-used-keywords)
       (list
        (list markchars-keywords
              (list 0 '(put-text-property (match-beginning 0) (match-end 0)
                                          'face markchars-face))))))

;;;###autoload
(define-minor-mode markchars-mode
  "Mark special characters.
Which characters to mark are defined by `markchars-keywords'.

The default is to mark non-IDN, non-ascii chars with a magenta
underline.

For information about IDN chars see `idn-is-recommended'.

If you change anything in the customization group `markchars' you
must restart this minor mode for the changes to take effect."
  :group 'markchars
  :lighter " ø"
  (if markchars-mode
      (progn
        (markchars-set-keywords)
        (font-lock-add-keywords nil markchars-used-keywords))
    (font-lock-remove-keywords nil markchars-used-keywords))
  ;; Fix-me: Something like mumamo-mark-for-refontification should be in Emacs.
  (if (fboundp 'mumamo-mark-for-refontification)
      (save-restriction
        (widen)
        (mumamo-mark-for-refontification (point-min) (point-max)))
    (font-lock-fontify-buffer)))

;;;###autoload
(define-globalized-minor-mode markchars-global-mode markchars-mode
  (lambda () (markchars-mode 1))
  :group 'markchars)

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