[tips] my forgery on muse-insert-tag

"ITSUMI ken-ichi" <[email protected]> Wed, 12 Jul 2006 09:49:06 +0900
Newsgroups gmane.emacs.wiki.general
Message-ID <[email protected]>
Hi all

* Problem
Current Design of muse-insert-tag is thought to be based on
assumptions shown below.

  1) Tag should be inserted *before* its contents.
  2) Enclosing Tag should not be enclosed *inline* elements, it always
enclose lines.

These assumptions missed at least me ;-).

* Proposition
If marker is active, tag enclose current region.

* My Quick and Dirty Solution
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(defun muse-insert-tag (tag)
  "Insert a tag interactively with a blank line after it."
  (interactive
   (list
    (completing-read
     (concat "Tag: "
             (when muse-tag-history
               (concat "(default: " (car muse-tag-history) ") ")))
     (progn
       (require 'muse-publish)
       (mapcar 'list (nconc (mapcar 'car muse-publish-markup-tags)
                            muse-custom-tags)))
     nil nil nil 'muse-tag-history
     (car muse-tag-history))))
  (when (equal tag "")
    (setq tag (car muse-tag-history)))
  (unless (interactive-p)
    (require 'muse-publish))
  (let ((tag-entry (assoc tag muse-publish-markup-tags))
        (options ""))
    ;; Add to custom list if no entry exists
    (unless tag-entry
      (add-to-list 'muse-custom-tags tag))
    ;; Get option
    (when (nth 2 tag-entry)
      (setq options (read-string "Option: ")))
    (unless (equal options "")
      (setq options (concat " " options)))
    ;; If marker is active, region should be enclosed by tag.
    (let* ((p (point))
	   (m (if mark-active		; if marker is inactive
		  (mark)		; m==p
		p))
	   (beg (min p m))
	   (end (max p m)))
      ;; Insert the tag, closing if necessary
      (goto-char beg)
      (when tag
	(let* ((tag-string (concat "<" tag options ">"))
	       (tag-length (length tag-string)))
	  (insert tag-string)
	  (when (nth 1 tag-entry)
	    (goto-char end)
	    (forward-char tag-length)
	    (insert (concat "</" tag ">"))
	    (goto-char beg)
	    (forward-char tag-length))))
      )
    )
)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

-- 
逸見研一(いつみけんいち)
Mixi http://mixi.jp/show_friend.pl?id=113732
Orkut http://www.orkut.com/Profile.aspx?uid=11299027584966195898
Blog http://d.hatena.ne.jp/amt/
Plan http://calendar.yahoo.co.jp/amt_tmp