In HTML mode, and/or in related functions available in the internet, it appears that one can do the following:

Insert a pair of HTML codes that one would then fill in with stuff.

If point is in (or at the end of) a word, have the word wrapped in a specific HTML code pair.

If a region of text is selected, have the region wrapped in a specific HTML code pair.

In all cases, there is a specific function for a given HTML code pair. If there is not a function for the HTML code you like, it is easy to copy an existing functions and alter it. Then, apply key-bindings and you’re off.

The problem with this method is that one ends up needing four or five new keybindings for basic HTML pairs one uses commonly, and for the rare HTML pair that you didn’t happen to program, one is SOL.

The fix is a function, bound to one key and of course callable with Meta-X functionname, which queries the user to enter an HTML code, like “em” or “strong” or “blockquote” and so on, with that code then placed in the proper format surrounding the word point is in or the selected text.

It is quite possible that such code is out there, but I’ve not seen exactly that.

So, I took some of the code at this very very nice web site on using emacs HTML mode, and modified it, and got this:


;; Wrap word at point or selected text
;; in user specified HTML tag

(defun wrap-text (aa bb)
“Wrap strings aa and bb around current word or region.”

(save-excursion
(let (p1 p2 myword)

(if (and transient-mark-mode mark-active)
(progn (setq p1 (region-beginning)) (setq p2 (region-end)))
(progn
(skip-chars-backward “-A-Za-z”)
(setq p1 (point))
(skip-chars-forward “-A-Za-z”)
(setq p2 (point))))
(setq myword (buffer-substring-no-properties p1 p2))
(goto-char p2) (insert bb)
(goto-char p1) (insert aa)))

(search-forward sRight))

(defun query-html-tag (hTag)
“Wrap an HTML tag provided by the user around current word or region.”

(interactive “sEnter HTML tag: “)
(message “hTag: %s” hTag)
(setq sLeft (concat “<” hTag “>”))
(setq sRight (concat “</” hTag “>”))

(wrap-text sLeft sRight)
)

;; set a key to invoke query-html-tag function

(global-set-key (kbd ““) ‘query-html-tag)

That search function at the end of the main function is to move the cursor to the end of the wrapped region, with the presumption that one wants to be there rather than at the beginning. However, this also moves the curser to the end of, rather than the middle of, the pair of codes. Fixing that is a bit beyond me at the moment.

Now, have I duplicated someone else’s result? If so, let me know, because likely their is better, and I’d like to erase mine and replace it with theirs (or a link thereto) in order to not pollute the emacs airwaves with bad code.

Or, just fix this for me. Because I know there are many who can.

I have no ego when it comes to this sort of thing. I’m happy to pat myself on the back for having a bit of code that does what I wanted it to do without errors, but I’ll be happy to chuck it the moment something that actually works better comes along.

But if you have suggestions, please test them first. “Oh, this should word …bla bla bla” usually does not!

Comments

  1. #1 Blog HTML
    June 23, 2010

    the steps are really easy and remember able.

Current ye@r *