| 1 |
;;; mew-highlight.el --- Highlight for Mew |
| 2 |
|
| 3 |
;; Author: Kazu Yamamoto <[email protected]> |
| 4 |
;; Created: Oct 18, 1997 |
| 5 |
|
| 6 |
;;; Code: |
| 7 |
|
| 8 |
(require 'mew) |
| 9 |
|
| 10 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 11 |
;;; |
| 12 |
;;; Cursor line |
| 13 |
;;; |
| 14 |
|
| 15 |
(defun mew-highlight-cursor-line () |
| 16 |
"A function to highlight the cursor line in Summary and Virtual mode." |
| 17 |
(when mew-use-highlight-cursor-line |
| 18 |
(mew-elet |
| 19 |
(if (overlayp (mew-sinfo-get-cursor-line)) |
| 20 |
(move-overlay (mew-sinfo-get-cursor-line) |
| 21 |
(save-excursion (beginning-of-line) (point)) |
| 22 |
(save-excursion (end-of-line) (point))) |
| 23 |
(mew-sinfo-set-cursor-line |
| 24 |
(mew-overlay-make |
| 25 |
(save-excursion (beginning-of-line) (point)) |
| 26 |
(save-excursion (end-of-line) (point)))) |
| 27 |
(overlay-put |
| 28 |
(mew-sinfo-get-cursor-line) 'face mew-highlight-cursor-line-face)))) |
| 29 |
(when mew-use-cursor-mark |
| 30 |
(unless (markerp overlay-arrow-position) |
| 31 |
(make-local-variable 'overlay-arrow-position) |
| 32 |
(setq overlay-arrow-position (make-marker))) |
| 33 |
(unless (and overlay-arrow-string |
| 34 |
(string= overlay-arrow-string mew-cursor-mark)) |
| 35 |
(make-local-variable 'overlay-arrow-string) |
| 36 |
(setq overlay-arrow-string mew-cursor-mark)) |
| 37 |
(set-marker overlay-arrow-position |
| 38 |
(save-excursion (beginning-of-line) (point))))) |
| 39 |
|
| 40 |
(defun mew-unhighlight-cursor-line () |
| 41 |
(when (overlayp (mew-sinfo-get-cursor-line)) |
| 42 |
(move-overlay (mew-sinfo-get-cursor-line) 1 1) |
| 43 |
;; (goto-char (point-max)) works wrong if underline exists. |
| 44 |
;; Redisplays it so that it works well. |
| 45 |
;; (sit-for 0) |
| 46 |
;; This code rocks Summary mode when typing "x", sigh. |
| 47 |
)) |
| 48 |
|
| 49 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 50 |
;;; |
| 51 |
;;; Marks |
| 52 |
;;; |
| 53 |
|
| 54 |
(defun mew-highlight-mark-line (mark) |
| 55 |
(when mew-use-highlight-mark |
| 56 |
(let ((face (mew-highlight-mark-get-face mark))) |
| 57 |
(unless face (setq face 'default)) |
| 58 |
(put-text-property |
| 59 |
(save-excursion (beginning-of-line) (point)) |
| 60 |
(save-excursion (end-of-line) (point)) |
| 61 |
'face face)))) |
| 62 |
|
| 63 |
(defun mew-highlight-unmark-line () |
| 64 |
(remove-text-properties |
| 65 |
(save-excursion (beginning-of-line) (point)) |
| 66 |
(save-excursion (end-of-line) (point)) |
| 67 |
'(face nil))) |
| 68 |
|
| 69 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 70 |
;;; |
| 71 |
;;; Header |
| 72 |
;;; |
| 73 |
|
| 74 |
(defun mew-unhighlight-region (BEG END) |
| 75 |
(mew-overlay-delete-region BEG END)) |
| 76 |
|
| 77 |
(defun mew-unhighlight-header () |
| 78 |
(save-restriction |
| 79 |
(widen) |
| 80 |
(mew-unhighlight-region (point-min) (mew-header-end)))) |
| 81 |
|
| 82 |
(defun mew-highlight-header-region (BEG END) |
| 83 |
"A function to highlight header in Message and Draft mode." |
| 84 |
(when mew-use-highlight-header |
| 85 |
(let (key beg med nspec overlay key-face val-face start) |
| 86 |
(save-excursion |
| 87 |
(mew-elet |
| 88 |
(mew-unhighlight-region BEG END) |
| 89 |
(save-restriction |
| 90 |
(narrow-to-region BEG END) |
| 91 |
(goto-char (point-min)) |
| 92 |
(while (not (eobp)) |
| 93 |
(if (not (looking-at mew-keyval)) |
| 94 |
(forward-line) |
| 95 |
(setq start (point)) |
| 96 |
(setq key (mew-match-string 1)) |
| 97 |
(setq beg (match-beginning 0)) |
| 98 |
(setq med (match-end 0)) |
| 99 |
(forward-line) |
| 100 |
(setq nspec (mew-nspec-by-key key)) |
| 101 |
(setq key-face (mew-key-face key nspec)) |
| 102 |
(setq val-face (mew-val-face key nspec)) |
| 103 |
(setq overlay (mew-overlay-make beg med)) |
| 104 |
(overlay-put overlay 'face key-face) |
| 105 |
(setq overlay (mew-overlay-make med (1- (point)))) |
| 106 |
(overlay-put overlay 'face val-face) |
| 107 |
(while (looking-at mew-lwsp+) |
| 108 |
(forward-line) |
| 109 |
(setq overlay (mew-overlay-make (match-end 0) (1- (point)))) |
| 110 |
(overlay-put overlay 'face val-face)) |
| 111 |
(if (member (mew-capitalize key) |
| 112 |
mew-draft-address-warning-fields) |
| 113 |
(mew-highlight-header-address-region start (point))))))))))) |
| 114 |
|
| 115 |
(defun mew-highlight-header-address-region (beg end) |
| 116 |
(let ((case (mew-tinfo-get-case)) |
| 117 |
addr domain overlay begovl endovl warnp) |
| 118 |
(save-excursion |
| 119 |
(goto-char beg) |
| 120 |
(while (re-search-forward "[^\n\t, <>:;]+@\\([^\n\t, <>:;]+\\)" end t) |
| 121 |
(setq addr (downcase (mew-match-string 0))) |
| 122 |
(setq domain (downcase (mew-match-string 1))) |
| 123 |
(setq begovl (match-beginning 0)) |
| 124 |
(setq endovl (match-end 0)) |
| 125 |
(cond |
| 126 |
((member addr (mew-safe-addresses case)) |
| 127 |
(setq warnp nil)) |
| 128 |
((member addr (mew-warn-addresses case)) |
| 129 |
(setq warnp t)) |
| 130 |
((member domain (mew-safe-domains case)) |
| 131 |
(setq warnp nil)) |
| 132 |
((member domain (mew-warn-domains case)) |
| 133 |
(setq warnp t)) |
| 134 |
((or (mew-warn-addresses case) (mew-safe-domains case)) |
| 135 |
(setq warnp t)) |
| 136 |
(t |
| 137 |
(setq warnp nil))) |
| 138 |
(when warnp |
| 139 |
(setq overlay (mew-overlay-make begovl endovl)) |
| 140 |
(overlay-put overlay 'face 'mew-face-header-warning) |
| 141 |
(overlay-put overlay 'priority 2)))))) ;; a bug of Meadow |
| 142 |
|
| 143 |
(defun mew-highlight-header () |
| 144 |
(save-restriction |
| 145 |
(widen) |
| 146 |
(mew-highlight-header-region (point-min) (mew-header-end)))) |
| 147 |
|
| 148 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 149 |
;;; |
| 150 |
;;; Body |
| 151 |
;;; |
| 152 |
|
| 153 |
(defun mew-highlight-body-region (BEG END &optional draft rehighlight) |
| 154 |
(when (and (or mew-use-highlight-body mew-use-highlight-url) |
| 155 |
(or (= 0 mew-highlight-body-max-size) |
| 156 |
(<= BEG mew-highlight-body-max-size))) |
| 157 |
(unless (or (= 0 mew-highlight-body-max-size) |
| 158 |
(<= END mew-highlight-body-max-size)) |
| 159 |
(setq END mew-highlight-body-max-size)) |
| 160 |
(let* ((inhibit-point-motion-hooks t) |
| 161 |
(cite-regex mew-highlight-body-regex-cite) |
| 162 |
(cmt-regex mew-highlight-body-regex-comment) |
| 163 |
(url-regex mew-regex-url) |
| 164 |
(fancy-num 0) |
| 165 |
(fancy-length (length mew-highlight-body-cite-faces)) |
| 166 |
beg end face fancy-alst fancy-prefix idn url) |
| 167 |
(save-excursion |
| 168 |
(mew-elet |
| 169 |
(when draft |
| 170 |
(mew-rear-nonsticky BEG END) |
| 171 |
(if rehighlight |
| 172 |
(remove-text-properties BEG END '(face nil mouse-face nil)))) |
| 173 |
(when mew-use-highlight-body |
| 174 |
(goto-char BEG) |
| 175 |
(while (and (<= (point) END) (re-search-forward cite-regex END t)) |
| 176 |
(setq beg (match-beginning 0)) |
| 177 |
(setq end (match-end 0)) |
| 178 |
(if (match-beginning 1) |
| 179 |
(setq fancy-prefix (mew-match-string 1)) |
| 180 |
(setq fancy-prefix nil)) |
| 181 |
(when (and fancy-prefix |
| 182 |
(< (string-width fancy-prefix) |
| 183 |
mew-highlight-body-prefix-width)) |
| 184 |
(if (setq face (cdr (assoc fancy-prefix fancy-alst))) |
| 185 |
(put-text-property beg end 'face face) |
| 186 |
(setq face (nth fancy-num mew-highlight-body-cite-faces)) |
| 187 |
(setq fancy-alst (cons (cons fancy-prefix face) fancy-alst)) |
| 188 |
(setq fancy-num (1+ fancy-num)) |
| 189 |
(when (= fancy-length fancy-num) |
| 190 |
(setq fancy-num 0)) |
| 191 |
(put-text-property beg end 'face face))) |
| 192 |
(forward-line)) |
| 193 |
(goto-char BEG) |
| 194 |
(while (and (<= (point) END) (re-search-forward cmt-regex END t)) |
| 195 |
(setq beg (match-beginning 0)) |
| 196 |
(setq end (match-end 0)) |
| 197 |
(put-text-property beg end 'face 'mew-face-body-comment))) |
| 198 |
(when mew-use-highlight-url |
| 199 |
(goto-char BEG) |
| 200 |
(while (and (<= (point) END) (re-search-forward url-regex END t)) |
| 201 |
(save-restriction |
| 202 |
(setq beg (match-beginning 0)) |
| 203 |
(setq end (match-end 0)) |
| 204 |
(narrow-to-region beg end) |
| 205 |
(goto-char (point-min)) |
| 206 |
(setq url (mew-buffer-substring (point-min) (point-max))) |
| 207 |
(while (and mew-use-punycode (re-search-forward mew-regex-punycode nil t)) |
| 208 |
(setq idn (mew-puny-decode (mew-buffer-substring (match-beginning 0) (match-end 0)))) |
| 209 |
(when idn |
| 210 |
(delete-region (match-beginning 0) (match-end 0)) |
| 211 |
(insert idn))) |
| 212 |
(goto-char (point-max)) |
| 213 |
(put-text-property (point-min) (point-max) 'mew-url url) |
| 214 |
(put-text-property (point-min) (point-max) 'face 'mew-face-body-url) |
| 215 |
(put-text-property |
| 216 |
(point-min) (point-max) 'mouse-face mew-highlight-url-mouse-face) |
| 217 |
(setq END (- END (- end (point-max)))))))))))) |
| 218 |
|
| 219 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 220 |
;;; |
| 221 |
;;; X Face |
| 222 |
;;; |
| 223 |
|
| 224 |
(defun mew-highlight-x-face (beg end) |
| 225 |
"Display X-Face." |
| 226 |
(if (and mew-use-highlight-x-face |
| 227 |
window-system |
| 228 |
(fboundp mew-highlight-x-face-function)) |
| 229 |
(funcall mew-highlight-x-face-function beg end))) |
| 230 |
|
| 231 |
(defvar mew-highlight-x-face-function (if mew-icon-p 'mew-highlight-x-face-original) |
| 232 |
"*A function to display-Xface") |
| 233 |
|
| 234 |
(defun mew-highlight-x-face-original (beg end) |
| 235 |
(save-excursion |
| 236 |
(goto-char beg) |
| 237 |
(mew-elet |
| 238 |
(let ((regex1 "^X-Face: *\\(.*\\(\n[ \t].*\\)*\\)\n") |
| 239 |
(buf (current-buffer)) |
| 240 |
overlay xface beg0 end0 xbmp) |
| 241 |
(while (re-search-forward regex1 end t) |
| 242 |
(setq beg0 (match-beginning 0)) |
| 243 |
(setq end0 (match-end 0)) |
| 244 |
(with-temp-buffer |
| 245 |
(mew-insert-buffer-substring |
| 246 |
buf (match-beginning 1) (match-end 1)) |
| 247 |
(setq xbmp (mew-x-face-compface-to-xbm)) |
| 248 |
(if xbmp (setq xface (mew-x-face-create)))) |
| 249 |
(when xface |
| 250 |
(setq overlay (mew-overlay-make beg0 end0)) |
| 251 |
(overlay-put overlay 'invisible t) |
| 252 |
(save-restriction |
| 253 |
(narrow-to-region beg end) |
| 254 |
(mew-x-face-display xface )))))))) |
| 255 |
|
| 256 |
(defun mew-x-face-compface-to-xbm () |
| 257 |
(when (and (mew-which-exec mew-prog-uncompface) |
| 258 |
(mew-which-exec mew-prog-icontopbm) |
| 259 |
(mew-which-exec mew-prog-pbmtoxbm)) |
| 260 |
(mew-set-buffer-multibyte nil) |
| 261 |
(mew-flet |
| 262 |
(call-process-region (point-min) (point-max) mew-prog-uncompface t t nil) |
| 263 |
(goto-char (point-min)) |
| 264 |
(insert "/* Format_version=1, Width=48, Height=48, Depth=1, Valid_bits_per_item=16 */\n") |
| 265 |
(call-process-region (point-min) (point-max) mew-prog-icontopbm t t nil) |
| 266 |
(if mew-use-highlight-x-face-inversion |
| 267 |
(call-process-region (point-min) (point-max) mew-prog-pbminvert t t nil)) |
| 268 |
(call-process-region (point-min) (point-max) mew-prog-pbmtoxbm t t nil)) |
| 269 |
t)) ;; return value |
| 270 |
|
| 271 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 272 |
;;; |
| 273 |
;;; Cooking |
| 274 |
;;; |
| 275 |
|
| 276 |
(defun mew-summary-cook-window (&optional WIN BEG) |
| 277 |
(let* ((win (or WIN (selected-window))) |
| 278 |
(beg (or BEG (window-start win))) |
| 279 |
(end (window-end win t)) |
| 280 |
(buf (window-buffer win))) |
| 281 |
(with-current-buffer buf |
| 282 |
(funcall mew-summary-cook-function beg end)))) |
| 283 |
|
| 284 |
;; See also mew-scan-insert-line |
| 285 |
(defun mew-summary-cook-region (beg end &optional interrupt) |
| 286 |
(when (and (mew-summary-or-virtual-p) mew-summary-buffer-raw) |
| 287 |
(let ((inhibit-point-motion-hooks t) |
| 288 |
ret mark face start med) |
| 289 |
(catch 'loop |
| 290 |
(save-excursion |
| 291 |
(mew-elet |
| 292 |
(goto-char beg) |
| 293 |
(if (mew-in-decode-syntax-p) |
| 294 |
(goto-char (mew-decode-syntax-end))) |
| 295 |
(if (and (mew-thread-p) |
| 296 |
mew-use-thread-separator |
| 297 |
(not (mew-summary-message-number))) |
| 298 |
(forward-line)) |
| 299 |
(setq start (point)) |
| 300 |
(while (and (< (point) end) ;; we cannot trust end |
| 301 |
(search-forward "\r" end t)) |
| 302 |
(if (and interrupt (input-pending-p)) |
| 303 |
(throw 'loop (setq ret t))) |
| 304 |
(setq med (match-beginning 0)) |
| 305 |
(forward-line) |
| 306 |
(if (and mew-use-highlight-mouse-line window-system) |
| 307 |
(put-text-property |
| 308 |
start med 'mouse-face mew-highlight-mouse-line-face)) |
| 309 |
(put-text-property med (1- (point)) 'invisible t) |
| 310 |
(if (mew-in-decode-syntax-p) |
| 311 |
(goto-char (mew-decode-syntax-end))) |
| 312 |
(if (and (mew-thread-p) |
| 313 |
mew-use-thread-separator |
| 314 |
(not (mew-summary-message-number))) |
| 315 |
(forward-line)) |
| 316 |
(setq start (point))) |
| 317 |
(when mew-use-highlight-mark |
| 318 |
(goto-char beg) |
| 319 |
(while (and (< (point) end) ;; we cannot trust end |
| 320 |
(re-search-forward mew-regex-mark end t)) |
| 321 |
(if (and interrupt (input-pending-p)) |
| 322 |
(throw 'loop (setq ret t))) |
| 323 |
(setq mark (mew-sumsyn-mark)) |
| 324 |
(setq face (mew-highlight-mark-get-face mark)) |
| 325 |
(beginning-of-line) |
| 326 |
(setq start (point)) |
| 327 |
(forward-line) |
| 328 |
(if face (put-text-property start (1- (point)) 'face face))))))) |
| 329 |
(set-buffer-modified-p nil) |
| 330 |
ret))) |
| 331 |
|
| 332 |
(defun mew-summary-cook-folders () |
| 333 |
(save-excursion |
| 334 |
(dolist (buf mew-buffers) |
| 335 |
(when (and (get-buffer buf) (not (input-pending-p))) |
| 336 |
(set-buffer buf) |
| 337 |
(setq mew-summary-buffer-raw |
| 338 |
(mew-summary-cook-region |
| 339 |
(point-min) (point-max) 'interrupt)))))) |
| 340 |
|
| 341 |
(defvar mew-highlight-timer-id nil) |
| 342 |
|
| 343 |
(defun mew-highlight-timer-setup () |
| 344 |
(if mew-highlight-timer-id (cancel-timer mew-highlight-timer-id)) |
| 345 |
(setq mew-highlight-timer-id |
| 346 |
(run-with-idle-timer mew-highlight-timer-interval |
| 347 |
t 'mew-summary-cook-folders))) |
| 348 |
|
| 349 |
(defun mew-highlight-timer-clean-up () |
| 350 |
(if mew-highlight-timer-id (cancel-timer mew-highlight-timer-id)) |
| 351 |
(setq mew-highlight-timer-id nil)) |
| 352 |
|
| 353 |
(provide 'mew-highlight) |
| 354 |
|
| 355 |
;;; Copyright Notice: |
| 356 |
|
| 357 |
;; Copyright (C) 1997-2015 Mew developing team. |
| 358 |
;; All rights reserved. |
| 359 |
|
| 360 |
;; Redistribution and use in source and binary forms, with or without |
| 361 |
;; modification, are permitted provided that the following conditions |
| 362 |
;; are met: |
| 363 |
;; |
| 364 |
;; 1. Redistributions of source code must retain the above copyright |
| 365 |
;; notice, this list of conditions and the following disclaimer. |
| 366 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 367 |
;; notice, this list of conditions and the following disclaimer in the |
| 368 |
;; documentation and/or other materials provided with the distribution. |
| 369 |
;; 3. Neither the name of the team nor the names of its contributors |
| 370 |
;; may be used to endorse or promote products derived from this software |
| 371 |
;; without specific prior written permission. |
| 372 |
;; |
| 373 |
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND |
| 374 |
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 375 |
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 376 |
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE |
| 377 |
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 378 |
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 379 |
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 380 |
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 381 |
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 382 |
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 383 |
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 384 |
|
| 385 |
;;; mew-highlight.el ends here |