* Opening and Closing a buffer in elisp, and elisp regexp
@ 2006-07-22 3:21 Brett Kelly
0 siblings, 0 replies; 3+ messages in thread
From: Brett Kelly @ 2006-07-22 3:21 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Opening and Closing a buffer in elisp, and elisp regexp
[not found] <mailman.4345.1153642974.9609.help-gnu-emacs@gnu.org>
@ 2006-07-23 11:42 ` Anselm Helbig
0 siblings, 0 replies; 3+ messages in thread
From: Anselm Helbig @ 2006-07-23 11:42 UTC (permalink / raw)
hi brett,
> [writing some elisp to extract urls from an erc buffer and select
> them for opening in another buffer]
just some short comments on your 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))
^^^^^^^^^^^^^
point can never be smaller than 1!
you should include the search in your while condition like this
(while (and (< (length urls) count)
(search-backward-regexp "<\\(http://.*\\)\\>" nil t))
^- ignore errors!
> (search-backward-regexp "<\\(http://.*\\)\\>")
> (add-to-list 'urls (match-string 0))))
^- this should be 1, 0
means the entire match,
not the parenthesized expression
> 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))))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this wont work. this code tries to make the buffer designated by
`buf' current, if `buf' is unbound this will throw an error.
use (kill-buffer (current-buffer)) instead. OTOH, (bury-buffer)
might serve you just as well.
use M-x re-builder. use M-x ielm. use the online documentation (C-h f,
C-h v, C-h i, ...). use eldoc-mode. if all else fails, have a look at
edebug.
there may be better java-ides out there, but there's no better IDE for
elisp than emacs. 8-)
happy hacking!
anselm
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Opening and Closing a buffer in elisp, and elisp regexp
@ 2006-07-24 6:53 martin rudalics
0 siblings, 0 replies; 3+ messages in thread
From: martin rudalics @ 2006-07-24 6:53 UTC (permalink / raw)
Cc: help-gnu-emacs
(defun get-last-urls (count)
"Build a list of the count most recent urls in the buffer"
;; Let `actual' count the actual number of urls found, calling `length'
;; in a loop is not very elegant.
(let (urls (actual 0))
(save-excursion
(while (and (< actual count) ; Did not find yet enough.
;; Move re-search into `and' to stop when no more matches are
;; found and set third argument to t to avoid that it barfs
;; when there are no more matches. Note: This finds only text
;; starting with "<http://" and terminating with a word
;; character - whatever that is in the current buffer - on the
;; same line.
(re-search-backward "<\\(http://.*\\)\\>" nil t))
(setq actual (1+ actual))
;; Use "(match-string 1)" to strip the leading "<".
(add-to-list 'urls (match-string 1))))
urls))
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-07-24 6:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.4345.1153642974.9609.help-gnu-emacs@gnu.org>
2006-07-23 11:42 ` Opening and Closing a buffer in elisp, and elisp regexp Anselm Helbig
2006-07-24 6:53 martin rudalics
-- strict thread matches above, loose matches on Subject: below --
2006-07-22 3:21 Brett Kelly
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.