tagify.el
1 (defun wrap-region (left right) 2 "Wrap the region in arbitrary text, LEFT goes to the left and RIGHT goes to the right." 3 (interactive) 4 (let* ((left-pos (region-beginning)) 5 (right-pos (+ (region-end) (length left))) 6 (end-pos (+ right-pos (length right)))) 7 (goto-char left-pos) 8 (insert left) 9 (goto-char right-pos) 10 (insert right) 11 (goto-char end-pos))) 12 13 (defun tagify-region-or-insert-self (arg) 14 "If there is a visible region, call `tagify-region-or-insert', otherwise 15 call `self-insert-command' passing it any prefix arg given." 16 (interactive "*P") 17 (if (and mark-active transient-mark-mode) 18 (call-interactively 'tagify-region-or-insert-tag) 19 (self-insert-command (prefix-numeric-value arg)))) 20 21 (defun tagify-region-or-insert-tag (tag) 22 "If there is a visible region, wrap it in the given HTML/XML tag using 23 `wrap-region'. If any attributes are specified then they are only included 24 in the opening tag. 25 26 Otherwise insert the opening and closing tags and position point between the two." 27 (interactive "*sTag (including attributes): \n") 28 (let* ((open (concat "<" tag ">")) 29 (close (concat "</" (car (split-string tag " ")) ">"))) 30 (if (and mark-active transient-mark-mode) 31 (wrap-region open close) 32 (insert (concat open close)) 33 (backward-char (length close))))) 34 35 (provide 'tagify)