Tiny helper function
Felix Gatzemeier <[email protected]>
| Newsgroups | gmane.emacs.psgml.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello!
As useful as it is to have the comments telling the user what kinds of
elements are expected, as annoying is it to keep removing them
(IMHO). I have written this wrapper around sgml-insert-element that
checks for the typical insertion comment before or following point at
the time of insertion, potentially separated by whitespace, but surely
containing the name of the inserted element. If found, that comment
and its line break are removed.
This has been working for a while for me (Using a custom extension of
DocBook) now, and I was just wondering whether anybody else finds this
useful --- or whether I've just overlooked a switch ;-)
======================================================================
(defun fxg-sgml-insert-element-wrapper (name &optional after silent)
"Get rid of required-element comments, too.
Removes a comment of the form \"<!-- one of (... tag ...) -->\"
following or preceding the newly inserted element of type 'tag'. This
is implemented crudely and may not work if any options are supplied.
For the comment to be removed, point at the time of creation must be
in the whitespace preceding or following it. Calls
`sgml-insert-element' with NAME, AFTER, and SILENT."
(interactive (list (sgml-read-element-name "Element: ")
sgml-leave-point-after-insert))
(let (
(oldMark (point-marker))
(searchRe (concat "\\s-*<!-- one of (\\([-_a-zA-Z0-9: ]* \\)?"
name
"\\( [-_a-zA-Z0-9: ]*\\)?) -->"))
)
(sgml-insert-element name after silent)
(save-excursion
(goto-char (marker-position oldMark))
(sgml-forward-element)
(if (looking-at searchRe)
(replace-match "")
(progn
;; No match at old point. Try backwards.
(goto-char (marker-position oldMark))
;; Enforce eager matching by requiring one non-space
;; character. This will thus not work for a comment having
;; nothing but whitespace before it in the file, but I (fxg)
;; am willing to take the risk.
(if (search-backward-regexp (concat "\\S-" searchRe) nil t)
(let ((beginComment (match-beginning 0)) ; Remember match data
(beginWS (match-end 0))
endWS)
(goto-char beginWS) ; Get end of whitespace following comment
(if (search-forward-regexp "\\s-*" nil t)
(setq endWS (match-end 0))
(setq endWS beginWS))
(if (and
(<= beginWS (marker-position oldMark))
(<= (marker-position oldMark) endWS))
;; new element has been created in whitespace
;; following this comment => replaces this
;; comment. Add one to comment begin for the
;; pre-whitespace match anchor.
(delete-region (+ 1 beginComment) beginWS)
)
)
)
)
)
)))
(add-hook 'sgml-mode-hook
(lambda ()
(local-set-key [(control c) (control e)] 'fxg-sgml-insert-element-wrapper)))
======================================================================
Best wishes,
--
Felix H. Gatzemeier [email protected]
Office Phone: (0(049)241)80-21313
Disclaimer: I do not speak for anyone but myself.
Please do not send me mails containing documents in proprietary
formats (such as Microsoft Word) unless you really need to.
-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php