Creating short URL redirections within Emacs

Posted: December 21, 2006 in Emacs

I think everybody knows tinyurl.com or metamark.net. These are services which create short URL redirections.

To create such redirections from within emacs, I created some functions.

(require 'mm-url)

(defun th-get-tinyurl ()
  "Grabs the url at point and echos the equivalent tinyurl in the
minibuffer to ease cutting and pasting."
  (interactive)
  (let* ((long-url (thing-at-point 'url))
         (tinyurl
          (save-excursion
            (with-temp-buffer
              (mm-url-insert
               (concat "http://tinyurl.com/api-create.php?url=" long-url))
              (kill-ring-save (point-min) (point-max))
              (buffer-string)))))
    (message "Tinyurl: %s" tinyurl)
    (replace-regexp-in-string "\\n" "" tinyurl)))

Simply put point on an URL and hit M-x th-get-tinyurl and a short url redirection will be created, the string will be returned and the tinyurl will be messaged and saved in the kill-ring, so that you can easily yank it.

The same can be done with metamark’s service:

(defun th-get-metamark ()
  "Grabs the url at point and echos the equivalent metamark url
in the minibuffer to ease cutting and pasting."
  (interactive)
  (let* ((long-url (thing-at-point 'url))
         (url (concat "http://metamark.net/api/rest/simple?"
                      (mm-url-encode-www-form-urlencoded
                       (list (cons "long_url" long-url)))))
         (short-url
          (save-excursion
            (with-temp-buffer
              (mm-url-insert url)
              (kill-ring-save (point-min) (point-max))
              (buffer-string)))))
    (message "Metamark: %s" short-url)
    (replace-regexp-in-string "\\n" "" short-url)))

One thing that would be nifty is the ability to replace the url at point with such a short URL redirection. That’s done by the following function:

(defun th-replace-url-with-short-url ()
  "Grabs the url at point and replaces it with the equivalent
tinyurl or metamark url."
  (interactive)
  (let* ((long-url (thing-at-point 'url))
         (type (completing-read "What type? " '("metamark" "tinyurl")))
         (short-url
          (if (string= type "metamark")
              (th-get-metamark)
            (th-get-tinyurl))))
    (let ((bounds (bounds-of-thing-at-point 'url)))
      (kill-region (car bounds) (cdr bounds)))
    (insert short-url)))

These three functions are part of th-common.el you can fetch from my homepage. First enter the Software section, then click on the ElispArea link.

Advertisements
Comments
  1. Bobrila says:

    It’s so interesting:,

  2. […] Creating short URL redirections within Emacs […]

  3. […] Creating short URL redirections within Emacs […]

  4. […] Creating short URL redirections within Emacs […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s