<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>@Lathi.net: Jumping to Specific Windows in Emacs</title>
    <link>http://blog.lathi.net/articles/2007/11/07/jumping-to-specific-windows-in-emacs</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>On Life, Fatherhood, Christianity, and Computers</description>
    <item>
      <title>Jumping to Specific Windows in Emacs</title>
      <description>&lt;p&gt;OK, last emacs tip for a while.  It&amp;#8217;s ironic, but the last two years I&amp;#8217;ve written some cool elisp while at RubyConf.  I typically run emacs full screen and split buffers both vertically and horizontally to arrange bits of code.  It&amp;#8217;s not uncommon for me to end up with three, four, five or even six visible windows in emacs.  The problem though is navigating between them.&lt;/p&gt;


	&lt;p&gt;The out-of-the-box solution is to use &lt;code&gt;other-window&lt;/code&gt; (bound to C-x o) and just cycle through them.  If you&amp;#8217;re fancy you can give a prefix argument to &lt;code&gt;other-window&lt;/code&gt; and go backwards.  This window cycling is tedious to me though and I&amp;#8217;d like something faster.  Particularly when I&amp;#8217;m mostly bouncing back and forth between two windows.&lt;/p&gt;


	&lt;p&gt;To solve this problem I wrote &lt;code&gt;jump-to-buffer&lt;/code&gt;.  Of course, right now I&amp;#8217;m really enamored with the fuzzy matching in &lt;code&gt;ido-completing-read&lt;/code&gt;.  That&amp;#8217;s what allows you to type non-sequential characters to match the buffer name.  It&amp;#8217;s very similar to TextMate&amp;#8217;s pattern matching.  So, &lt;code&gt;jump-to-buffer&lt;/code&gt; uses &lt;code&gt;ido-completing-read&lt;/code&gt;.  The buffers it gives you as options for completion are only the buffers in visible windows.  It also sorts those buffers in the order of the &lt;code&gt;buffer-list&lt;/code&gt;; which is in the order of most recently accessed.&lt;/p&gt;


	&lt;p&gt;To make this work, I&amp;#8217;ve written two helper functions: &lt;code&gt;rotate-list&lt;/code&gt;, and &lt;code&gt;sort-by-other-list&lt;/code&gt;.  Without further ado:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;(defun dka-sort-by-other-list (to-sort-list other-list)
  (let* ((index 0)
         (other-alist (mapcar (lambda (buffer) 
                                (setq index (+ index 1))
                                (cons buffer index))
                              other-list))
         (swartz (mapcar (lambda (item) 
                           (cons (cdr (assoc item other-alist)) item))
                         to-sort-list))
         (sorted-list (sort swartz (lambda (a b) (&amp;lt; (car a) (car b))))))
    (mapcar 'cdr sorted-list)))

(defun rotate-list (list count)
  &amp;quot;Rotate the LIST by COUNT elements&amp;quot;
  (cond
   ((= count 0) list)
   ((not list) list)
   (t
    (rotate-list (nconc  (cdr list) (list (car list)) '()) (1- count)))))

(defun dka-jump-to-window ()
  &amp;quot;Interactively jump to another visible window based on it's `buffer-name' using `ido-completing-read'&amp;quot;
  (interactive)
  (let* ((visible-buffers (mapcar '(lambda (window) (window-buffer window)) (window-list)))
         (sorted-visible-buffers (dka-sort-by-other-list visible-buffers (buffer-list)))
         (rotated-buffer-list (rotate-list sorted-visible-buffers 1))
         (visible-buffer-names (mapcar (lambda (buffer) (buffer-name buffer)) rotated-buffer-list))
         (buffer-name (ido-completing-read &amp;quot;Enter buffer to jump to: &amp;quot; 
                                           visible-buffer-names
                                           nil t))
         (window-of-buffer
          (delq nil 
                (mapcar '(lambda (window) 
                           (if (equal buffer-name (buffer-name (window-buffer window)))
        window nil)) (window-list)))))
    (select-window (car window-of-buffer)))
)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;I&amp;#8217;m definitely interested in feedback on this code.  The &lt;code&gt;dka-sort-list-by-other-list&lt;/code&gt; method was particularly tricky for me to write.  I think it&amp;#8217;s both right and fast.  I haven&amp;#8217;t dove into &lt;a href="http://www.emacswiki.org/cgi-bin/wiki/ElUnit"&gt;elunit&lt;/a&gt; yet to know for sure if it&amp;#8217;s right.&lt;/p&gt;</description>
      <pubDate>Wed, 07 Nov 2007 20:58:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:d67cf0f1-66d6-4070-8fab-f497f3942676</guid>
      <author>Doug</author>
      <link>http://blog.lathi.net/articles/2007/11/07/jumping-to-specific-windows-in-emacs</link>
      <category>Emacs</category>
      <category>Programming</category>
      <category>elisp</category>
      <category>elunit</category>
      <category>transform</category>
      <category>schwartz</category>
      <category>sorting</category>
      <category>emacs</category>
    </item>
  </channel>
</rss>
