1 ;;; html-write.el --- Hide some tags for writing text in XHTML
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: 2008-10-03T01:29:44+0200 Thu
5 (defconst html-write:version "0.6") ;; Version:
6 ;; Last-Updated: 2009-08-11 Tue
11 ;; Features that might be required by this library:
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;; The minor mode `html-write-mode' displays simple tags like <i>,
20 ;; <b>, <em>, <strong> or <a> with appropriate faces (for example bold
21 ;; and italic) instead of displaying the tags.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; This program is free software; you can redistribute it and/or
32 ;; modify it under the terms of the GNU General Public License as
33 ;; published by the Free Software Foundation; either version 2, or
34 ;; (at your option) any later version.
36 ;; This program is distributed in the hope that it will be useful,
37 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
38 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 ;; General Public License for more details.
41 ;; You should have received a copy of the GNU General Public License
42 ;; along with this program; see the file COPYING. If not, write to
43 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
44 ;; Floor, Boston, MA 02110-1301, USA.
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 ;; Silence byte compiler
51 (defvar jit-lock-start)
54 (eval-when-compile (require 'mumamo)) ;; Just for the defmacro ...
55 (eval-when-compile (require 'mlinks nil t))
58 (defgroup html-write nil
59 "Customization group for html-write."
63 (defface html-write-base
64 '((t (:inherit font-lock-type-face)))
65 "Face from which other faces inherits."
68 (defface html-write-em
69 '((t (:inherit html-write-base :slant italic)))
70 "Face used for <em> tags."
73 (defface html-write-strong
74 '((t (:inherit html-write-base :weight bold)))
75 "Face used for <strong> tags."
78 (defface html-write-link
79 '((t (:inherit html-write-base :underline t)))
80 "Face used for <a> tags."
83 (defconst html-write-tag-list
84 '(("i" html-write-em-tag-actions)
85 ("b" html-write-strong-tag-actions)
86 ("em" html-write-em-tag-actions)
87 ("strong" html-write-strong-tag-actions)
88 ("a" html-write-a-tag-actions)
89 ;;("img" html-write-img-tag-actions t)
91 "List of tags that should be hidden.
92 A record in the list has the format
94 \(TAG HANDLE [SINGLE])
97 - TAG is the tag name string.
99 - HANDLE is a function to call when hiding the tag. It takes
100 three parameters, TAG-BEGIN, TAG-END and OVERLAY. TAG-BEGIN
101 and TAG-END are start and end of the start tag. OVERLAY is an
102 overlay used for faces, keymaps etc that covers the whole tag."
105 (defun html-write-em-tag-actions (tag-begin tag-end overlay)
106 "Do actions for <em> tags for tag between TAG-BEGIN and TAG-END.
107 OVERLAY is the overlay added by `html-write-mode' for this tag."
108 (overlay-put overlay 'face 'html-write-em))
110 (defun html-write-strong-tag-actions (tag-begin tag-end overlay)
111 "Do actions for <strong> tags for tag between TAG-BEGIN and TAG-END.
112 OVERLAY is the overlay added by `html-write-mode' for this tag."
113 (overlay-put overlay 'face 'html-write-strong))
116 (defun html-write-img-tag-actions (tag-begin tag-end overlay)
117 "Do actions for <img> tags for tag between TAG-BEGIN and TAG-END.
118 OVERLAY is the overlay added by `html-write-mode' for this tag."
120 (let ((here (point-marker))
123 (narrow-to-region tag-begin tag-end)
124 (goto-char tag-begin)
125 (when (looking-at (rx (*? anything)
129 (+ (not (any "\"\n"))))
131 (setq href (match-string-no-properties 1))))
133 (overlay-put overlay 'display (concat "image " href))
134 (overlay-put overlay 'html-write-url href))
135 (goto-char (point)))))
137 (defun html-write-point-entered-echo (left entered)
138 (let ((msg (get-char-property entered 'help-echo)))
139 (when msg (message "%s" msg))))
141 (defun html-write-a-tag-actions (tag-begin tag-end overlay)
142 "Do actions for <a> tags for tag between TAG-BEGIN and TAG-END.
143 OVERLAY is the overlay added by `html-write-mode' for this tag."
145 (let ((here (point-marker))
148 (narrow-to-region tag-begin tag-end)
149 (goto-char tag-begin)
150 (when (looking-at (rx (*? anything)
154 (+ (not (any "\"\n"))))
156 (setq href (match-string-no-properties 1))))
158 (overlay-put overlay 'face 'html-write-link)
159 (overlay-put overlay 'help-echo href)
160 ;; Fix-me: Seems like point-entered must be a text prop
161 (overlay-put overlay 'point-entered 'html-write-point-entered-echo)
162 (overlay-put overlay 'mouse-face 'highlight)
163 (if (eq ?# (string-to-char href))
164 (setq href (concat "file:///" buffer-file-name href))
165 (when (file-exists-p href)
166 (setq href (expand-file-name href))))
167 (overlay-put overlay 'html-write-url href))
168 (goto-char (point)))))
170 (defun html-write-get-tag-ovl ()
171 "Get tag overlay at current point."
173 (dolist (ovl (overlays-at (point)))
174 (let ((ranges (overlay-get ovl 'html-write)))
176 (throw 'ranges ovl))))))
178 (defun html-write-toggle-current-tag ()
179 "Toggle display of tag at current point."
181 (let* ((ovl (html-write-get-tag-ovl))
182 (hiding-ranges (overlay-get ovl 'html-write))
183 (invis (get-text-property (caar hiding-ranges) 'invisible))
184 (ovl-start (overlay-start ovl))
185 (ovl-end (overlay-end ovl)))
188 (overlay-put ovl 'html-face (overlay-get ovl 'face))
189 (overlay-put ovl 'face 'highlight)
190 (dolist (range hiding-ranges)
191 (let ((start (car range))
193 (mumamo-with-buffer-prepared-for-jit-lock
194 (put-text-property start end 'invisible nil)))))
196 (html-write-hide-tags ovl-start ovl-end))))
198 (defun html-write-browse-link ()
199 "Browse link in current tag."
201 (let* ((ovl (html-write-get-tag-ovl))
202 (url (overlay-get ovl 'html-write-url)))
204 (error "No link in this tag"))
208 (defvar html-write-keymap
209 (let ((map (make-sparse-keymap))
211 (define-key map [(control ?c) ?+] 'html-write-toggle-current-tag)
212 (define-key map [(control ?c) ?!] 'html-write-browse-link)
213 (define-key map [mouse-1] 'html-write-browse-link)
214 (when (featurep 'mlinks)
215 (setq keys (where-is-internal 'mlinks-goto mlinks-mode-map))
217 (define-key map key 'html-write-mlinks-goto))
218 (setq keys (where-is-internal 'mlinks-goto-other-window mlinks-mode-map))
220 (define-key map key 'html-write-mlinks-goto-other-window))
221 (setq keys (where-is-internal 'mlinks-goto-other-frame mlinks-mode-map))
223 (define-key map key 'html-write-mlinks-goto-other-frame))
227 (defun html-write-mlinks-goto ()
230 (html-write-mlinks-goto-1 'mlinks-goto))
232 (defun html-write-mlinks-goto-other-window ()
233 "Goto link in other window."
235 (html-write-mlinks-goto-1 'mlinks-goto-other-window))
237 (defun html-write-mlinks-goto-other-frame ()
238 "Goto link in other frame."
240 (html-write-mlinks-goto-1 'mlinks-goto-other-frame))
242 (defun html-write-mlinks-goto-1 (goto-fun)
243 (let* ((ovl (html-write-get-tag-ovl))
244 (ovl-start (overlay-start ovl))
245 (ovl-end (overlay-end ovl))
246 (here (point-marker)))
247 (goto-char ovl-start)
248 (skip-chars-forward "^\"" ovl-end)
250 (unless (funcall goto-fun) (goto-char here))
253 ;;(html-write-make-hide-tags-regexp)
254 (defun html-write-make-hide-tags-regexp ()
255 "Make regexp used for finding tags to hide."
256 ;; fix-me: single tags. Fix-me: what did I mean??? Maybe < etc...
259 (mapcar (lambda (elt)
267 "\\(?:" tags-re "\\)"
268 "\\)[^>]*>\\(?3:[^<]*\\)\\(?2:</\\1>\\)"
271 (defvar html-write-pending-changes nil)
272 (make-variable-buffer-local 'html-write-pending-changes)
273 (put 'html-write-pending-changes 'permanent-local t)
276 (defun html-write-hide-tags (start end)
277 "Hide tags matching `html-write-tag-list' between START and END."
278 ;;(message "html-write-hide-tags %s %s" start end)
279 (let ((here (point-marker))
280 (buffer-name (buffer-file-name))
286 (let ((hide-tags-regexp (html-write-make-hide-tags-regexp)))
287 (when dbg (message "before search start=%s end=%s, point=%s" start end (point)))
288 (while (re-search-forward hide-tags-regexp end t)
289 (let* ((ovl (make-overlay (match-beginning 0) (match-end 0)
291 (tag-fun (cadr (assoc (match-string-no-properties 1)
292 html-write-tag-list)))
294 ;;(overlay-put ovl 'face 'font-lock-variable-name-face)
295 (overlay-put ovl 'keymap html-write-keymap)
297 (list (cons (1- (match-beginning 1)) (match-beginning 3))
298 (cons (match-beginning 2) (match-end 2))))
299 (overlay-put ovl 'html-write hiding-ranges)
300 (mumamo-with-buffer-prepared-for-jit-lock
301 (dolist (range hiding-ranges)
302 (let ((start (car range))
304 (put-text-property start end 'invisible 'html-write)
305 ;; Fix-me: more careful rear-nonsticky?
306 (put-text-property (1- end) end
307 'rear-nonsticky '(invisible)))))
308 ;; Let tag-fun override
310 (funcall tag-fun (match-end 1) (match-beginning 3) ovl))
314 (defun html-write-reveal-tags (start end)
315 "Reveal tags between START and END."
316 (let ((here (point-marker)))
319 (goto-char (point-min))
321 (mumamo-with-buffer-prepared-for-jit-lock
322 (remove-text-properties start
324 '(invisible html-write))
325 (dolist (ovl (overlays-in start end))
326 (when (overlay-get ovl 'html-write)
327 (let ((end (overlay-end ovl)))
328 (remove-list-of-text-properties (1- end) end '(rear-nonsticky))
329 (delete-overlay ovl)))))))
333 (define-minor-mode html-write-mode
334 "Minor mode for convenient display of some HTML tags.
335 When this mode is on a tag in `html-write-tag-list' is displayed as
336 the inner text of the tag with a face corresponding to the tag.
337 By default for example <i>...</i> is displayed as italic and
338 <a>...</a> is displayed as an underlined clickable link.
340 Only non-nested tags are hidden. The idea is just that it should
341 be easier to read and write, not that it should look as html
344 See the customization group `html-write' for more information about
347 The following keys are defined when you are on a tag handled by
350 \\{html-write-keymap}
352 IMPORTANT: Most commands you use works also on the text that is
353 hidden. The movement commands is an exception, but as soon as
354 you edit the buffer you may also change the hidden parts.
356 Hint: Together with `wrap-to-fill-column-mode' this can make it
357 easier to see what text you are actually writing in html parts of
362 (html-write-font-lock t)
363 (html-write-font-lock nil)
366 (html-write-reveal-tags (point-min) (point-max))))))
367 (put html-write-mode 'permanent-local t)
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 (defun html-write-jit-extend-after-change (start end old-len)
373 "For JIT lock extending.
374 Should be on `jit-lock-after-change-extend-region-functions'.
376 START, END and OLD-LEN are the parameters from after change."
377 (let ((our-ovls nil))
378 (dolist (ovl (append (overlays-in start end)
381 ;; Leave the overlays until re-fontification time, but note their extent.
382 (when (overlay-get ovl 'html-write)
383 (setq jit-lock-start (min jit-lock-start (overlay-start ovl)))
384 (setq jit-lock-end (max jit-lock-end (overlay-end ovl)))))))
387 (defun html-write-fontify (bound)
388 ;;(message "html-write-fontify %s" bound)
391 (let* ((hide-tags-regexp (html-write-make-hide-tags-regexp))
392 (next-tag (re-search-forward hide-tags-regexp bound t))
393 (tag-beg (when next-tag (match-beginning 0)))
394 (tag-end (when next-tag (match-end 0)))
395 (tag-nam (when next-tag (match-string-no-properties 1)))
396 (tag-fun (when next-tag (cadr (assoc tag-nam html-write-tag-list))))
398 (old-start (next-single-char-property-change (max (point-min) (1- (point))) 'html-write nil bound)))
399 ;;(message "here a old-start=%s, tag-beg/end=%s/%s" old-start tag-beg tag-end)
400 (setq tag-ovl (when next-tag (make-overlay tag-beg tag-end)))
402 ;; Fix-me: maybe valid, perhaps better keep it then?
403 (let ((ovl (catch 'ovl
404 (dolist (o (append (overlays-at old-start)
405 (overlays-in old-start (1+ old-start))
407 (when (overlay-get o 'html-write)
409 (when ovl ;; fix-me: there should be one...
411 (mumamo-with-buffer-prepared-for-jit-lock
412 (remove-list-of-text-properties (overlay-start ovl) (overlay-end ovl) '(invisible html-write)))
413 (delete-overlay ovl))))
414 ;;(html-write-hide-tags start end)
415 ;;(message "here d, tag-ovl=%s" tag-ovl)
417 (overlay-put tag-ovl 'face 'font-lock-variable-name-face)
418 (overlay-put tag-ovl 'keymap html-write-keymap)
420 (list (cons (1- (match-beginning 1)) (match-beginning 3))
421 (cons (match-beginning 2) (match-end 2))))
422 (overlay-put tag-ovl 'html-write tag-hid)
424 (funcall tag-fun (match-end 1) (match-beginning 3) tag-ovl))
425 (mumamo-with-buffer-prepared-for-jit-lock
426 (dolist (range tag-hid)
427 (let ((start (car range))
429 (put-text-property start end 'invisible 'html-write)
430 ;;(put-text-property start end 'html-write t)
431 ;; Fix-me: more careful rear-nonsticky?
432 (put-text-property (1- end) end
433 'rear-nonsticky '(invisible)))))))
436 (set-match-data (list (copy-marker (overlay-start tag-ovl))
437 (copy-marker (overlay-end tag-ovl))))
438 (goto-char (1+ (overlay-end tag-ovl)))
441 (defun html-write-font-lock (on)
443 (let* ((add-or-remove (if on 'font-lock-add-keywords 'font-lock-remove-keywords))
444 (fontify-fun 'html-write-fontify)
445 (args (list nil `(( ,fontify-fun ( 0 'html-write-base t ))))))
447 (when on (setq args (append args (list t))))
448 (apply add-or-remove args)
453 (provide 'html-write)
454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
455 ;;; html-write.el ends here