Archive for December, 2006

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

Using TAB to complete and indent

Posted: December 2, 2006 in Emacs

I’m not a too fast typist, but emacs’ dabbrev-expand makes even me fly. Ok, not as fast as a hawk, but maybe like a quail. ;-)

So it wouldn’t be a bad idea to bind dabbrev-expand to the TAB key, if indenting wasn’t such an important command when programming.

Because I didn’t want to choose between pestilence and cholera, I wrote this function:

(defun th-complete-or-indent (arg)
  "If preceding character is a word character and the following
character is a whitespace or non-word character, then
`dabbrev-expand', else indent according to mode."
  (interactive "*P")
  (cond ((and
          (= (char-syntax (preceding-char)) ?w)
          (looking-at (rx (or word-end (any ".,;:#=?()[]{}")))))
         (require 'sort)
         (let ((case-fold-search t))
           (dabbrev-expand arg)))
         (t
          (indent-according-to-mode))))

Bind this function to the TAB key. Then TAB completes the current word with dabbrev-expand. But if you a not at the end of a word, it will indent according to the current major mode instead.