Shammer's Philosophy

My private adversaria

Is Emacs cursor in or ?

I always write HTML documents almost every day. And I use some EmacsLisp functions including I wrote by myself. Sometime I would like to make Emacs judged where the cursor is in, what kind of HTML elements. This function helps in such a case.

(defun get-current-element ()
  (save-excursion
    (save-restriction
      (let* ((element-end (search-forward "</" nil t))
	     (element (buffer-substring element-end (+ 2 element-end))))
	(if (equal element "p>")
	    "p"
	  (let ((element (buffer-substring element-end (+ 4 element-end))))
	    (if (equal element "div>")
		"div"
	      nil)))))))

This function will be used like below.

(let ((element (get-current-element)))
  (when element
    (let ((end (progn
		 (search-forward (concat "</" element ">") nil t)
		 (match-beginning 0)))
	  (start (progn
		   (re-search-backward (concat "<" element " ['\"a-zA-z0-9]*>") nil t)
		   (match-end 0))))
      (...))))

The (...) means some procedures. This can do something within the current element. A end element, closing element, always doesn't have any attributes. But a beginning element, like <p align="center">, sometimes has any attributes. So element start position should be considered if the element has some attributes.