help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Opening and Closing a buffer in elisp, and elisp regexp


From: Brett Kelly
Subject: Opening and Closing a buffer in elisp, and elisp regexp
Date: Fri, 21 Jul 2006 20:21:41 -0700

Hello all,

This is a rather odd one, I feel.  I spend a good deal of time on an
IRC channel (using ERC) where url's regularly appear, and I'm also
trying to get better with elisp.  So, I decided to try to write a
series of functions that do the following:

- Grab the last n urls (or less, if there are < n) pasted from the
channel buffer
- Open a new buffer called *urls* and paste each one on it's own line
- Remap RET so that it runs browse-url-at-point
- Once I hit RET on a url, kill the *urls* buffer and return to the
original ERC buffer (and at the same time opening the selected url in
my browser).

So far, almost everything works.  The two problems I'm having are these:

- If there are less than n urls in the ERC buffer, it gives an error:
  "Search failed: (pattern)"
- Getting the regexp exactly right - many of the urls that appear in
the channel look like <http://foo.com>, and the leading < is somehow
being included in the match.  Some folks have suggested using rx, but
I've not been able to find a good example of it.
- Closing the *urls* buffer after selecting a url.  The url will open
in my browser just fine, but the *urls* buffer persists.

Anyway, here's the current iteration of my code:

#####################################

(defun get-last-urls (count)
 "Build a list of the <count> most recent urls in the buffer"
 (let ((urls '()))
   (save-excursion
     (while (or (< (length urls) count) (< (point) 0))
       (search-backward-regexp "<\\(http://.*\\)\\>")
       (add-to-list 'urls (match-string 0))))
   urls))

(defun handle-url-select ()
 "Open the URL, close buffer - this doesn't completely work yet"
 (progn
   (browse-url (thing-at-point 'url))
   (with-current-buffer buf (kill-buffer buf))))

(defun print-urls (count)
 "Exposed function to open list of urls in the new buffer"
 (interactive)
 (save-excursion
  (let ((urls (get-last-urls count))
        (bname "*urls*"))
    (switch-to-buffer bname)
    (if (> 0 (buffer-size))
        (delete-and-extract-region 0 (buffer-size)))
    (local-set-key (kbd "RET") 'handle-url-select)
    (dolist (url urls) (insert (url-normalize-url url) "\n"))
    (goto-char 1))))

##############################################

And I apologize in advance if any of this code is bad form or style -
i'm still learning :)  Feel free to offer any constructive criticism.

Thanks in advance!

--
Brett Kelly
Blog:      http://blog.brettkelly.org
Images:  http://www.flickr.com/photos/inkedmn




reply via email to

[Prev in Thread] Current Thread [Next in Thread]