<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tassilo&#039;s Blog</title>
	<atom:link href="http://tsdh.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://tsdh.wordpress.com</link>
	<description>The personal blog of Tassilo Horn</description>
	<lastBuildDate>Fri, 06 Jan 2012 21:37:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='tsdh.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Tassilo&#039;s Blog</title>
		<link>http://tsdh.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://tsdh.wordpress.com/osd.xml" title="Tassilo&#039;s Blog" />
	<atom:link rel='hub' href='http://tsdh.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using Clojure&#8217;s core.logic with custom data structures</title>
		<link>http://tsdh.wordpress.com/2012/01/06/using-clojures-core-logic-with-custom-data-structures/</link>
		<comments>http://tsdh.wordpress.com/2012/01/06/using-clojures-core-logic-with-custom-data-structures/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 21:37:30 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[core.logic]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=123</guid>
		<description><![CDATA[With a lot of help from Ambrose, I managed to make Clojure&#8217;s core.logic library work with my custom Java data structures. In this posting, I&#8217;ll explain the code.  I assume that you are already familiar with Clojure in general, and you know core.logic and relational programming at least from a user&#8217;s point of view. Ok, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=123&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>With a lot of help from <a title="Ambrose at GitHub" href="http://github.com/frenchy64">Ambrose</a>, I managed to make Clojure&#8217;s core.logic library work with my custom Java data structures. In this posting, I&#8217;ll explain the code.  I assume that you are already familiar with Clojure in general, and you know <em>core.logic</em> and relational programming at least from a user&#8217;s point of view.</p>
<p>Ok, so let&#8217;s start.  My custom data structures are <a href="http://www.uni-koblenz-landau.de/koblenz/fb4/institute/IST/RGEbert/MainResearch-en/Graphtechnology">TGraphs that we develop here at our institute</a>.  You don&#8217;t need to know more than that a graph consists of vertices, and vertices can be connected by edges.  Furthermore, both vertices and edges may have attributes.  When you use such a graph, the graph itself, every vertex, and every edge is one Java object in your memory that implements the interface <em>Graph</em>, <em>Vertex</em>, and <em>Edge</em>, respectively.</p>
<p>I wrote a nice functional Clojure API for working with TGraphs (<em>funtg</em> on clojars; don&#8217;t use it for serious purposes, I&#8217;m constantly changing things without thinking about compatibility at all).  So probably, if you are reading this, you are in the same situation that I was in: <strong>You have a cool data structure, you have a cool API for it, and you are totally curious what you could do with core.logic on your data structure.  So how do I get core.logic to work with my stuff???</strong></p>
<p>The answer is: you have to write relations that use your existing API to access your data structure.  The topic of this posting is mainly how to doing that in a way that core.logic wants.  So let&#8217;s start with the namespace declaration for the code:<br />
<code>
<pre>(ns extend.example
  (:refer-clojure :exclude [==])
  (:use [clojure.core.logic])
  ;; The following two are my functional API
  (:require [de.uni-koblenz.ist.funtg.core :as core])
  (:require [de.uni-koblenz.ist.funtg.funql :as funql]))</pre>
<p></code><br />
Nothing special here, except that you can see that I require my functional TGraph API using prefixes.  So when you see <em>core/foo</em> or <em>funql/bar</em> in the following, you know that I&#8217;m calling my functional API there.  As next, I added some helper functions for testing if a logic variable is fresh or ground.  Ignore the comment about being walk-ed for now.<br />
<code>
<pre>(defn fresh?
  "Returns true, if `x' is fresh.
  `x' must have been `walk'ed before!"
  [x]
  (lvar? x))

(defn ground?
  "Returns true, if `x' is ground.
  `x' must have been `walk'ed before!"
  [x]
  (not (lvar? x)))</pre>
<p></code><br />
Then, I&#8217;ll defined a constant <em>+graph+</em> that holds some example TGraph (some route map like graph).  I decided to keep the graph as a var of the namespace instead of making it a parameter of relations, because my API has no way to enumerate all graphs that happen to be in memory.  If the graph was a parameter of relations, I couldn&#8217;t be fully relational, e.g., giving only fresh logic variables to my relations would have to error.<br />
<code>
<pre>(def +graph+ (core/load-graph "/home/horn/Repos/uni/funtg/test/greqltestgraph.tg"))</pre>
<p></code><br />
Now we&#8217;ll come to the actual first relation.  <em>vertexo</em> is a relation where <em>v</em> is a vertex in the graph <em>+graph+.</em><br />
<code>
<pre>(defn vertexo
  "A relation where `v' is a vertex."
  [v]
  (fn [a]                                 ;; (1)
    (let [gv (walk a v)]
      (if (fresh? gv)
        (to-stream                        ;; (2)
         (-&gt;&gt; (map #(unify a v %)
                   (funql/vseq +graph+))
              (remove not)))
         (if (.containsVertex +graph+ gv)
           a
          (fail a))))))</pre>
<p></code><br />
Basically, the stuff marked with (*) is what&#8217;s important. (1) A relation must return a function which gets a so-called substitution <em>a</em>. You can think of it as something like an environment map which knows what logical variables are ground, and if so, what value they have. <em>(walk a v)</em> gets you the value of the variable (or value) <em>v</em>. If <em>v</em> is ground, then you get a value back. If <em>v</em> is fresh, you get a logical variable back. So now the functions <em>fresh?</em> and <em>ground?</em> above make sense, right?</p>
<p>The other important part (2) is that the function returned by a relation has to return a substitution again: <em>A relation returns a function that gets a substitution and returns a substitution</em>. <em>to-stream</em> turns a seq into a choice, which essentially say which possible values are allowed for the relation&#8217;s parameters. We declare such a possible binding using <em>unify</em>. <em>funql/vseq</em> returns the lazy seq of the graph&#8217;s vertices, each of which may be unified with the parameter <em>v</em> in the substitution <em>a</em>. If <em>v</em> was ground, then it could only be unified with the vertex it is already bound to. Because of that, it&#8217;s a good idea to filter out <em>false</em> values in the sequence before giving it to <em>to-stream</em>.</p>
<p>If I had put (2) directly into (1), it would work exactly the same. The additional code is for performance purposes. If <em>v</em> is already ground, there&#8217;s no reason to try to unify it with every vertex in the graph just to check if it is contained. Instead, a simple check (via the Java API) to see if that vertex is in the graph is enough. If it&#8217;s contained, then the substitution <em>a</em> is correct, so I return it &#8220;unchanged&#8221; (quotes, because of course we don&#8217;t mutate in Clojure). Else, the vertex is not in <em>+graph+</em>, so the binding is invalid and we fail.</p>
<p>That&#8217;s it. Now let&#8217;s turn to edges which are accessed with a relation of 3 parameters denoting the edge itself, its start vertex, and its end vertex.<br />
<code>
<pre>(defn edgeo
  "A relation where `e' is an edge from `alpha' to `omega'."
  [e alpha omega]
  (fn [a]
    (let [ge     (walk a e)
          galpha (walk a alpha)
          gomega (walk a omega)]
      (cond
        (ground? ge) (unify a [alpha omega]
                            [(core/alpha ge) (core/omega ge)])
        (ground? galpha) (to-stream
                           (-&gt;&gt; (map #(unify a [e omega] [% (core/omega %)])
                                     (funql/iseq galpha nil :out))
                                (remove not)))
        (ground? gomega) (to-stream
                           (-&gt;&gt; (map #(unify a [e alpha] [% (core/alpha %)])
                                    (funql/iseq gomega nil :in))
                                (remove not)))
        :else (to-stream
                (-&gt;&gt; (for [edge (funql/eseq +graph+)]
                       (unify a [e alpha omega]
                              [edge (core/alpha edge) (core/omega edge)]))
                     (remove not)))))))</pre>
<p></code><br />
Again, basically the relation would work fine if we delete everything except the expression of the <em>:else</em> clause.  That unifies all edges in the graph including their start and end vertices with the parameters of the relation.  The three first clauses of the <em>cond</em> are for performance only.  The first says, if an edge is already given, then we only check the start and end vertex, which is a constant time operation.  The second and third clause say, if either the start or end vertex are already given, then we don&#8217;t need to unify all edges in the graph with the parameters but only the edges incident to the given vertex (<em>eseq</em> vs. <em>iseq</em>), which is again much faster.</p>
<p>The last relation I&#8217;ll show is concerned with attributes.<br />
<code>
<pre>(defn valueo
  "A relation where `ae' has value `val' for its `at' attribute."
  [ae at val]
  (fn [a]
    (let [gae (walk a ae)
          gat (walk a at)
          gval (walk a val)]
      (cond
        (and (ground? gae)
             (ground? gat)) (or (unify a [ae at val]
                                       [gae gat (core/value gae gat)]))
        (ground? gae) (to-stream
                        (-&gt;&gt; (for [attr (seq (.getAttributeList
                                               (core/attributed-element-class gae)))
                                   :let [an (keyword (.getName attr))]]
                                (unify a [ae at val]
                                      [gae an (core/value gae an)]))
                             (remove not)))
        :else (to-stream
                (-&gt;&gt; (for [elem (concat (funql/vseq +graph+)
                                        (funql/eseq +graph+))
                           attr (seq (.getAttributeList
                                       (core/attributed-element-class elem)))
                           :let [an (keyword (.getName attr))]]
                        (unify a [ae at val]
                               [elem an (core/value elem an)]))
                     (remove not)))))))</pre>
<p></code><br />
Again, only the <em>:else</em> part is neccessary.  I unify every vertex and every edge together with every attribute that is defined for them with the given parameters.  Clearly, that&#8217;s quite some effort.  The first clause of the <em>cond</em> simply looks up the attribute value if the element and the attribute name are already given.  The second clause deals with the case when at least the graph element is given, in which case only all its attribute/value pairs have to be unified.</p>
<p>So here are some example applications.</p>
<p>What are the 3 first vertices in the graph?<br />
<code>
<pre>(run 3 [q]
  (vertexo q))
;=&gt; (#&lt;v1: localities.Village&gt;
     #&lt;v2: localities.Village&gt;
     #&lt;v3: localities.Town&gt;)</pre>
<p></code><br />
Is this a vertex of the graph?<br />
<code>
<pre>(let [v1 (core/vertex +graph+ 1)]
  (run* [q]
    (vertexo v1)))
;=&gt; (_.0)     ; succeeded, so yes, it's a vertex of +graph+</pre>
<p></code><br />
What are the first 3 edges of the graph?<br />
<code>
<pre>(run 3 [q]
  (fresh [a o]
    (edgeo q a o)))
;=&gt; (#&lt;e1: connections.Footpath&gt;
     #&lt;e2: connections.Footpath&gt;
     #&lt;e3: connections.Footpath&gt;)</pre>
<p></code><br />
What edges end at the vertex <em>v1</em>?<br />
<code>
<pre>(let [v1 (core/vertex +graph+ 1)]
  (run* [q]
    (fresh [o]
      (edgeo q o v1))))
;=&gt; (#&lt;e-22: localities.ContainsLocality&gt;)</pre>
<p></code><br />
Which attribute of what element has the value 251?<br />
<code>
<pre>(run* [q]
  (fresh [e a]
    (valueo e a 251)
    (== q [e a])))
;=&gt; ([#&lt;v1: localities.Village&gt; :inhabitants])</pre>
<p></code><br />
So that village has 251 inhabitants.  Pretty small.  What&#8217;s the name of that village?<br />
<code>
<pre>(run* [q]
  (fresh [e a]
    (valueo e a 251)
    (valueo e :name q)))
;=&gt; ("Kammerforst")</pre>
<p></code><br />
Ah, it&#8217;s <a href="http://en.wikipedia.org/wiki/Kammerforst,_Rhineland-Palatinate">Kammerforst</a>.  Oh, wikipedia says its population has increased to 253. ;-)</p>
<p>Ok, that&#8217;s it.  I hope this helps you to make your custom data structures work with <em>core.logic</em>.  Have fun!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=123&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2012/01/06/using-clojures-core-logic-with-custom-data-structures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>Yay, I&#8217;m a celebrity now!</title>
		<link>http://tsdh.wordpress.com/2011/12/09/yay-im-a-celebrity-now/</link>
		<comments>http://tsdh.wordpress.com/2011/12/09/yay-im-a-celebrity-now/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 18:02:55 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=107</guid>
		<description><![CDATA[I&#8217;ve just received an email from wordpress congratulating me that my blog has gotten its first follower. I run that blog for about five years now, and I didn&#8217;t even know that there is some following functionality! Well, now I can see that button.  Prior to that, it was just one of the 378 other buttons [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=107&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just received an email from wordpress congratulating me that my blog has gotten its first follower. I run that blog for about five years now, and<strong> I didn&#8217;t even know that there is some <em>following</em> functionality!</strong> Well, now I can see that button.  Prior to that, it was just one of the 378 other buttons in the wordpress interface I have no clue for what they are good for.</p>
<p>Seems like I am an old fart, totally Web 0.1&#8230; ;-)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=107&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2011/12/09/yay-im-a-celebrity-now/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>A quick pop-up shell for emacs</title>
		<link>http://tsdh.wordpress.com/2011/10/12/a-quick-pop-up-shell-for-emacs/</link>
		<comments>http://tsdh.wordpress.com/2011/10/12/a-quick-pop-up-shell-for-emacs/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 10:07:16 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Emacs Lisp]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=108</guid>
		<description><![CDATA[Some file managers provide a shortcut to quickly embed some terminal that&#8217;s initialized with the current directory as cwd. Wouldn&#8217;t that be cool to have in emacs? For example, you edit some file, and then you want to quickly commit it. Using the code below, you can do that like this: Edit the file and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=108&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Some file managers provide a shortcut to quickly embed some terminal that&#8217;s initialized with the current directory as cwd.  Wouldn&#8217;t that be cool to have in emacs?</p>
<p>For example, you edit some file, and then you want to quickly commit it.  Using the code below, you can do that like this:</p>
<li>Edit the file and save: C-x C-s</li>
<li>Popup a shell: F12</li>
<li>Check changes and commit (using svn):<br />
            <code>$ svn diff | colordiff</code><br />
            <code>$ svn ci -m "some changes" RET</code></li>
<li>Close the popup shell again: F12</li>
<p>Here&#8217;s the code:</p>
<pre><code>(defvar th-shell-popup-buffer nil)

(defun th-shell-popup ()
  "Toggle a shell popup buffer with the current file's directory as cwd."
  (interactive)
  (unless (buffer-live-p th-shell-popup-buffer)
    (save-window-excursion (shell "*Popup Shell*"))
    (setq th-shell-popup-buffer (get-buffer "*Popup Shell*")))
  (let ((win (get-buffer-window th-shell-popup-buffer))
	(dir (file-name-directory (or (buffer-file-name)
				      ;; dired
				      dired-directory
				      ;; use HOME
				      "~/"))))
    (if win
	(delete-window win)
      (pop-to-buffer th-shell-popup-buffer nil t)
      (comint-send-string nil (concat "cd " dir "\n")))))

(global-set-key (kbd "&lt;f12&gt;") 'th-shell-popup)</code></pre>
<p>Using that, hitting F12 will popup a <code>*Popup Shell*</code> buffer, initialized in the directory containing the file your are currently editing.  If the current buffer is not associated with a file, then the shell&#8217;s cwd is your HOME directory.</p>
<p>Hitting F12 while the popup shell buffer is visible will hide it again.  So you can use it as a simple toggle.</p>
<p>The code ensures that the same shell buffer is reused over and over again (unless you kill it), so that you don&#8217;t end up with hundredth of them.</p>
<p>And here&#8217;s a screenshot:</p>
<p><a href="http://tsdh.files.wordpress.com/2011/10/emacs-popup-shell.png"><img src="http://tsdh.files.wordpress.com/2011/10/emacs-popup-shell.png?w=300&#038;h=300" alt="" title="emacs-popup-shell" width="300" height="300" class="alignnone size-medium wp-image-115" /></a></p>
<p><strong>UPDATES:</strong></p>
<li>2011-10-13T08:03:24+00:00: Made it work for dired buffers.</li>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/108/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/108/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/108/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=108&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2011/10/12/a-quick-pop-up-shell-for-emacs/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>

		<media:content url="http://tsdh.files.wordpress.com/2011/10/emacs-popup-shell.png?w=300" medium="image">
			<media:title type="html">emacs-popup-shell</media:title>
		</media:content>
	</item>
		<item>
		<title>defmacro! revisited</title>
		<link>http://tsdh.wordpress.com/2011/09/28/defmacro-revisited/</link>
		<comments>http://tsdh.wordpress.com/2011/09/28/defmacro-revisited/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 05:41:42 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[macros]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=97</guid>
		<description><![CDATA[In my last post, I&#8217;ve introduced the defmacro! macro, which is just like defmacro, except that it guarantees that all of the arguments are evaluated once only. However, in contrast to Doug Hoyte&#8217;s defmacro! he introduced in Let over Lambda, my macro expanded into a normal defmacro form that expanded into a form where all [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=97&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my last post, I&#8217;ve introduced the <code>defmacro!</code> macro, which is just like <code>defmacro</code>, except that it guarantees that all of the arguments are evaluated once only.</p>
<p>However, in contrast to Doug Hoyte&#8217;s <code>defmacro!</code> he introduced in Let over Lambda, my macro expanded into a normal <code>defmacro</code> form that expanded into a form where all args were evaluated <em>exactly once</em>.</p>
<p>Clearly, this was totally flawed, because in, say, new control structures, you may want to have some argument evaluated <em>never</em>.</p>
<p>So here&#8217;s a better version which allows for better control about evaluation.  All args with trailing ! (BANG, in Clojure speak) will be evaluated exactly once, and the rest of the args stays under the programmer&#8217;s control (note that this version also takes a mandatory docstring):</p>
<p><code>
<pre>(defn bang-symbol?
  "Returns true, if sym is a symbol with name ending in a exclamation
  mark (bang)."
  [sym]
  (and (symbol? sym)
       (= (last (name sym)) \!)))

(defmacro defmacro!
  "Defines a macro name with the given docstring, args, and body.
  All args ending in an exclamation mark (!, bang) will be evaluated only once
  in the expansion, even if they are unquoted at several places in body.  This
  is especially important for args whose evaluation has side-effecs or who are
  expensive to evaluate."
  [name docstring args &amp; body]
  (let [bang-syms (filter bang-symbol? args)
        rep-map (apply hash-map
                       (mapcat (fn [s] [s `(quote ~(gensym))])
                               bang-syms))]
    `(defmacro ~name
       ~docstring
       ~args<code>
       `(let ~~(vec (mapcat (fn [[s t]] [t s]) rep-map))
          ~(clojure.walk/postwalk-replace ~rep-map ~@body)))))</code></pre>
<p></code></p>
<p>Using that, we can now easily implement the numeric if, <code>nif</code>, you can find in On Lisp and Let over Lambda:</p>
<p><code>
<pre>(defmacro! nif
  "Numeric if: evals test! (only once) and executes either pos, zero, or neg
  depending on the result."
  [test! pos zero neg]
  `(cond
    (pos? ~test!)  ~pos
    (zero? ~test!) ~zero
    :else          ~neg))</pre>
<p></code></p>
<p>When evaluating <code>(nif 1 (println "pos") (println "zero") (println "neg"))</code>, now there&#8217;s only &#8220;pos&#8221; printed.  With the previous <code>defmacro!</code> version, &#8220;pos&#8221;, &#8220;zero&#8221;, and &#8220;neg&#8221; were printed.</p>
<p><strong>UPDATE</strong>: Stefan Kamphausen noticed that <code>defmacro!</code> doesn&#8217;t work as intended if destructuring is done in the argument list. So here&#8217;s yet another version that flattens the argument list when collecting the bang-symbols.</p>
<p><code>
<pre>(defmacro defmacro!
  "Defines a macro name with the given docstring, args, and body.
  All args ending in an exclamation mark (!, bang) will be evaluated only once
  in the expansion, even if they are unquoted at several places in body.  This
  is especially important for args whose evaluation has side-effecs or who are
  expensive to evaluate."
  [name docstring args &amp; body]
  (let [bang-syms (filter bang-symbol? (flatten args)) ;; &lt;==
        rep-map (apply hash-map
                       (mapcat (fn [s] [s `(quote ~(gensym))])
                               bang-syms))]
    `(defmacro ~name
       ~docstring
       ~args
       `(let ~~(vec (mapcat (fn [[s t]] [t s]) rep-map))
          ~(clojure.walk/postwalk-replace ~rep-map ~@body)))))</pre>
<p></code></p>
<p>Using that, you can define a strange <code>nif</code> variant that wants a vector, where the first entry is a vector containing the test, and the second entry is a vector of the pos, zero, neg entries.</p>
<p><code>
<pre>(defmacro! strange-nif
  "Like nif, but with strange destructuring"
  [[[test!] [pos zero neg]]]
  `(cond
    (pos? ~test!)  ~pos
    (zero? ~test!) ~zero
    :else          ~neg))

;; Trying it...
user&gt; (strange-nif [[1] [:pos :zero :neg]])
:pos
user&gt; (macroexpand '(strange-nif [[1] [:pos :zero :neg]]))
(let [G__1974 1]
     (cond (pos? G__1974) :pos (zero? G__1974) :zero :else :neg))</pre>
<p></code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=97&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2011/09/28/defmacro-revisited/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>Once-only evaluation for Clojure macros</title>
		<link>http://tsdh.wordpress.com/2011/09/23/once-only-evaluation-for-clojure-macros/</link>
		<comments>http://tsdh.wordpress.com/2011/09/23/once-only-evaluation-for-clojure-macros/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 17:14:06 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[macros]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=78</guid>
		<description><![CDATA[When programming macros, it&#8217;s often desired to have its arguments evaluated only once. Let&#8217;s have a look at a simple example: user&#62; (defmacro square [x] `(* ~x ~x)) #'user/square user&#62; (square 5) 25 At a first glance, it seems to work. But see what happens here: user&#62; (def c (let [a (atom 4)] #(swap! a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=78&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When programming macros, it&#8217;s often desired to have its arguments evaluated only once.  Let&#8217;s have a look at a simple example:</p>
<p><code>
<pre>user&gt; (defmacro square [x] `(* ~x ~x))
#'user/square
user&gt; (square 5)
25</pre>
<p></code></p>
<p>At a first glance, it seems to work.  But see what happens here:</p>
<p><code>
<pre>user&gt; (def c (let [a (atom 4)] #(swap! a inc)))
#'user/c</pre>
<p></code></p>
<p>This defines a counter function, which will increase it&#8217;s count on every call.  Now let&#8217;s feed that to our macro:</p>
<p><code>
<pre>user&gt; (square (c))
30</pre>
<p></code></p>
<p>Oh, shouldn&#8217;t that be 25, because we&#8217;re increasing our counter to 5?  No, because the macroexpansion is</p>
<p><code>
<pre>user&gt; (macroexpand-1 '(square (c)))
(clojure.core/* (c) (c))</pre>
<p></code></p>
<p>So our counter is increased twice resulting in <code>(* 5 6)</code>.  The lesson to be learned is that when writing a macro, one should take care that every argument is evaluated once, i.e., if you feel the need to unquote an argument more than once, you have to let-bind its value to some gensym and use that later on.  This is a fixed version of the <code>square</code> macro.</p>
<p><code>
<pre>user&gt; (defmacro square [x] `(let [x# ~x] (* x# x#)))
#'user/square
user&gt; (macroexpand-1 '(square (c)))
(clojure.core/let [x__5139__auto__ (c)]
  (clojure.core/* x__5139__auto__ x__5139__auto__))</pre>
<p></code></p>
<p>As you can see, now our counter is incremented only once, and its new value is bound to a generated variable which is used in the body.</p>
<p>But why not do exactly that by default?  Here&#8217;s a macro that does that:</p>
<p><code>
<pre>(defmacro defmacro!
  "Defines a macro in which all args are evaled only once."
  [name args &amp; body]
  (let [rep-map (apply hash-map
                       (mapcat (fn [s] [s `(quote ~(gensym))])
                               args))]
    `(defmacro ~name ~args
       `(let ~~(vec (mapcat (fn [[s t]] [t s]) rep-map))
          ~(clojure.walk/postwalk-replace ~rep-map ~@body)))))</pre>
<p></code></p>
<p>Using this macrowriting macro, we can now safely use our first implementation of <code>square</code>:</p>
<p><code>
<pre>user&gt; (defmacro! square [x]
  `(* ~x ~x))
#'user/square
user&gt; (macroexpand-1 '(square (c)))
(clojure.core/let [G__5491 (c)] (clojure.core/* G__5491 G__5491))
user&gt; (square (c))
49</pre>
<p></code></p>
<p>Great, seems to work.</p>
<p><strong>UPDATE</strong>: This version of <code>defmacro!</code> has one serious design flaw.  The completely expanded form will evaluate all arguments <em>exactly once</em>, but often you want to be able to have some argument evaluated <em>never</em>.  See <a href="http://tsdh.wordpress.com/2011/09/28/defmacro-revisited/" title="defmacro! revisited">this sequel post</a> for an enhanced version.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/78/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/78/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/78/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=78&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2011/09/23/once-only-evaluation-for-clojure-macros/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>Integrating Emacs&#8217; org-mode with the Awesome window manager</title>
		<link>http://tsdh.wordpress.com/2009/03/04/integrating-emacs-org-mode-with-the-awesome-window-manager/</link>
		<comments>http://tsdh.wordpress.com/2009/03/04/integrating-emacs-org-mode-with-the-awesome-window-manager/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 20:06:32 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[awesome]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[org-mode]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=55</guid>
		<description><![CDATA[I&#8217;ve switched to another tiling window manager called awesome and it&#8217;s really like its name suggests. It&#8217;s more a window manager framework that can be programmed in lua to create a window manager that does exactly what you want it to do. Awesome has a lua library called naughty for displaying notifications. Now my idea [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=55&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve switched to another tiling window manager called <a href="http://awesome.naquadah.org/">awesome</a> and it&#8217;s really like its name suggests.  It&#8217;s more a window manager framework that can be programmed in <a href="http://www.lua.org/">lua</a> to create a window manager that does exactly what you want it to do.</p>
<p>Awesome has a lua library called <a href="http://awesome.naquadah.org/apidoc/modules/naughty.html">naughty</a> for displaying notifications. Now my idea was that whenever my mouse pointer enters the region of the <a href="http://awesome.naquadah.org/apidoc/modules/capi.html#textbox">textbox</a> widget that shows the current date and time in a <a href="http://awesome.naquadah.org/apidoc/modules/capi.html#wibox">wibox</a>, a popup should show the agenda for this week and dispose automatically when I move the mouse away.</p>
<p>Ok, so here&#8217;s the code on the emacs side.  It assures that the file <tt>/tmp/org-agenda.txt</tt> always contains a plain-text export of the current org agenda.  It&#8217;s created once on emacs startup and after each change to any org agenda file it&#8217;ll be updated.</p>
<pre><code>;; update agenda file after changes to org files
(defun th-org-mode-init ()
  (add-hook 'after-save-hook 'th-org-update-agenda-file t t))

(add-hook 'org-mode-hook 'th-org-mode-init)

;; that's the export function
(defun th-org-update-agenda-file (&amp;optional force)
  (interactive)
  (save-excursion
    (save-window-excursion
      (let ((file "/tmp/org-agenda.txt"))
        (org-agenda-list)
        (org-write-agenda file)))))

;; do it once at startup
(th-org-update-agenda-file t)</code></pre>
<p>And here&#8217;s the lua code on the awesome side:</p>
<pre><code>-- the current agenda popup
org_agenda_pupup = nil

-- do some highlighting and show the popup
function show_org_agenda ()
   local fd = io.open("/tmp/org-agenda.txt", "r")
   if not fd then
      return
   end
   local text = fd:read("*a")
   fd:close()
   -- highlight week agenda line
   text = text:gsub("(Week%-agenda[ ]+%(W%d%d?%):)", "&lt;span style="text-decoration: underline;"&gt;%1&lt;/span&gt;")
   -- highlight dates
   text = text:gsub("(%w+[ ]+%d%d? %w+ %d%d%d%d[^n]*)", "%1")
   -- highlight times
   text = text:gsub("(%d%d?:%d%d)", "%1")
   -- highlight tags
   text = text:gsub("(:[^ ]+:)([ ]*n)", "%1%2")
   -- highlight TODOs
   text = text:gsub("(TODO) ", "&lt;strong&gt;%1&lt;/strong&gt; ")
   -- highlight categories
   text = text:gsub("([ ]+%w+:) ", "%1 ")
   org_agenda_pupup = naughty.notify(
      { text     = text,
        timeout  = 999999999,
        width    = 600,
        position = "bottom_right",
        screen   = mouse.screen })
end

-- dispose the popup
function dispose_org_agenda ()
   if org_agenda_pupup ~= nil then
      naughty.destroy(org_agenda_pupup)
      org_agenda_pupup = nil
   end
end

mydatebox = widget({ type = "textbox", align = "right" }) -- shows the date
mydatebox.mouse_enter = show_org_agenda
mydatebox.mouse_leave = dispose_org_agenda

-- after that the mydatebox is added to some wibox, of course...</code></pre>
<p>And that&#8217;s how it looks like.</p>
<div id="attachment_58" class="wp-caption alignnone" style="width: 510px"><a href="http://www.flickr.com/photos/tsdh/3328356025/"><img class="size-full wp-image-58" title="org-awesome" src="http://tsdh.files.wordpress.com/2009/03/org-awesome.png?w=614" alt="The Org Agenda in an Awesome/Naughty popup"   /></a><p class="wp-caption-text">The Org Agenda in an Awesome/Naughty popup</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=55&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2009/03/04/integrating-emacs-org-mode-with-the-awesome-window-manager/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>

		<media:content url="http://tsdh.files.wordpress.com/2009/03/org-awesome.png" medium="image">
			<media:title type="html">org-awesome</media:title>
		</media:content>
	</item>
		<item>
		<title>Calling org-remember from inside conkeror</title>
		<link>http://tsdh.wordpress.com/2008/11/14/calling-org-remember-from-inside-conkeror/</link>
		<comments>http://tsdh.wordpress.com/2008/11/14/calling-org-remember-from-inside-conkeror/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 11:30:48 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[conkeror]]></category>
		<category><![CDATA[org]]></category>
		<category><![CDATA[org-mode]]></category>
		<category><![CDATA[org-remember]]></category>
		<category><![CDATA[remember]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=51</guid>
		<description><![CDATA[I use Carsten Dominik&#8217;s great emacs package org-mode for project planning, appointments and TODOs (org-mode homepage). One cool feature is that it integrates nicely with the remember package. So when an idea comes into my mind I need to remember for later, I hit M-x org-remember RET which presents me a buffer with a custom [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=51&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I use Carsten Dominik&#8217;s great emacs package <em>org-mode</em> for project planning, appointments and TODOs (<a href="http://orgmode.org/">org-mode homepage</a>).  One cool feature is that it integrates nicely with the <em>remember</em> package.  So when an idea comes into my mind I need to remember for later, I hit <tt>M-x org-remember RET</tt> which presents me a buffer with a custom template, I type my note and hit <tt>C-c C-c</tt>.  The note is filed then and the buffer is gone, so there&#8217;s really no interruption.</p>
<p>One cool thing is that org-remember automatically inserts a link to the &#8220;thing&#8221; you had open when adding the note.  This &#8220;thing&#8221; could be a info buffer, a mail, a usenet article, an image,&#8230;</p>
<p>I thought it would be nice to use that facility to make TODO-bookmarks like &#8220;TODO read that great blog entry!&#8221; from inside my browser <a href="http://conkeror.org/">conkeror</a>.  And so I did.</p>
<p>Here&#8217;s the emacs side of the code (put it in your <tt>~/.emacs</tt>):</p>
<pre><code>(setq org-remember-templates
      '(
        ;; lot of other templates...
        ("TODO"    ?t "* TODO %?
  :PROPERTIES:
  :created: %U
  :link: %a
  :END:
  %i")))

(defun th-org-remember-conkeror (url)
  (interactive "s")
  (org-remember nil ?t)
  (save-excursion
    (insert "\n\n  [[" url "]]"))
  (local-set-key (kbd "C-c C-c")
         (lambda ()
           (interactive)
           (org-ctrl-c-ctrl-c)
           (delete-frame nil t))))</code></pre>
<p>And that&#8217;s the conkeror side (put it in your <tt>~/.conkerorrc</tt>):</p>
<pre><code>function org_remember(url, window) {
    var cmd_str = 'emacsclient -c --eval \'(th-org-remember-conkeror "' + url + '")\'';
    if (window != null) {
    window.minibuffer.message('Issuing ' + cmd_str);
    }
    shell_command_blind(cmd_str);
}

interactive("org-remember", "Remember the current url with org-remember",
        function (I) {
        org_remember(I.buffer.display_URI_string, I.window);
        });</code></pre>
<p>This code may require emacs 23, I&#8217;m not too sure.  What you need in every case is a running emacs server, so that you can connect to it with <tt>emacsclient</tt>.</p>
<p><strong>UPDATE: Now I use org-protocol instead of something home-brewn.</strong></p>
<p>Here&#8217;s the updated code for your <tt>~/.conkerorrc</tt>.  On the emacs side nothing is needed anymore.</p>
<pre><code>function org_remember(url, title, text, window) {
    var eurl = encodeURIComponent(url);
    var etitle = encodeURIComponent(title);
    var etext = encodeURIComponent(text);
    var cmd_str = "emacsclient -c org-protocol://remember://" + eurl + "/" + etitle + "/" + etext;
    window.minibuffer.message("Issuing " + cmd_str);
    shell_command_blind(cmd_str);
}

interactive("org-remember", "Remember the current url with org-remember",
        function (I) {
          org_remember(I.buffer.display_uri_string,
                       I.buffer.document.title,
                       I.buffer.top_frame.getSelection(),
                       I.window);
        });</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=51&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2008/11/14/calling-org-remember-from-inside-conkeror/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>Re-open read-only files as root automagically</title>
		<link>http://tsdh.wordpress.com/2008/08/20/re-open-read-only-files-as-root-automagically/</link>
		<comments>http://tsdh.wordpress.com/2008/08/20/re-open-read-only-files-as-root-automagically/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 19:54:09 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[TRAMP]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=43</guid>
		<description><![CDATA[I have my emacs running as server and two small wrapper scripts ec and et which connect to it with emacsclient (either creating a new frame or opening a terminal frame). Because I want to do all my system administration (editing files in /etc/) with emacs and don&#8217;t want to start another emacs instance as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=43&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have my emacs running as server and two small wrapper scripts <tt>ec</tt> and <tt>et</tt> which connect to it with <tt>emacsclient</tt> (either creating a new frame or opening a terminal frame).</p>
<p>Because I want to do all my system administration (editing files in <tt>/etc/</tt>) with emacs and don&#8217;t want to start another emacs instance as root, I use TRAMP to switch to superuser mode automagically if the opened file is read-only.</p>
<p>Here&#8217;s the code (<span style="text-decoration:underline;"><span style="color:#ff0000;"><strong>UPDATED</strong></span></span>: Uses defadvice instead of hooking into find-file-hook, which had the bad effect of find-file-hook running twice.):</p>
<pre><code>(defun th-rename-tramp-buffer ()
  (when (file-remote-p (buffer-file-name))
    (rename-buffer
     (format "%s:%s"
             (file-remote-p (buffer-file-name) 'method)
             (buffer-name)))))

(add-hook 'find-file-hook
          'th-rename-tramp-buffer)

(defadvice find-file (around th-find-file activate)
  "Open FILENAME using tramp's sudo method if it's read-only."
  (if (and (not (file-writable-p (ad-get-arg 0)))
           (y-or-n-p (concat "File "
                             (ad-get-arg 0)
                             " is read-only.  Open it as root? ")))
      (th-find-file-sudo (ad-get-arg 0))
    ad-do-it))

(defun th-find-file-sudo (file)
  "Opens FILE with root privileges."
  (interactive "F")
  (set-buffer (find-file (concat "/sudo::" file))))</code></pre>
<p>Now whenever I type <tt>ec /some/file/i/have/no/permissions/to/write</tt> (or <tt>emacsclient [-c|-t] /some/root/file</tt>, emacs asks me to re-open it using TRAMP&#8217;s sudo method.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/tsdh.wordpress.com/43/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/tsdh.wordpress.com/43/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=43&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2008/08/20/re-open-read-only-files-as-root-automagically/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>Some calendar and org-mode integration stuff</title>
		<link>http://tsdh.wordpress.com/2008/07/03/some-calendar-and-org-mode-integration-stuff/</link>
		<comments>http://tsdh.wordpress.com/2008/07/03/some-calendar-and-org-mode-integration-stuff/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 20:00:53 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[org-mode]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/?p=36</guid>
		<description><![CDATA[The following code binds RET in calendar mode to a function that opens an org agenda buffer for that day (or better that week). (defun th-calendar-open-agenda () (interactive) (let* ((calendar-date (or ;; the date at point in the calendar buffer (calendar-cursor-to-date) ;; if there's none, use the curren date (calendar-current-date))) (day (time-to-days (encode-time 1 1 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=36&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The following code binds RET in calendar mode to a function that opens an org agenda buffer for that day (or better that week).</p>
<pre><code>(defun th-calendar-open-agenda ()
  (interactive)
  (let* ((calendar-date (or
                         ;; the date at point in the calendar buffer
                         (calendar-cursor-to-date)
                         ;; if there's none, use the curren date
                         (calendar-current-date)))
         (day (time-to-days (encode-time 1 1 1
                                         (second calendar-date)
                                         (first calendar-date)
                                         (third calendar-date))))
         (calendar-buffer (current-buffer)))
    (org-agenda-list nil day)
    (select-window (get-buffer-window calendar-buffer))))

(define-key calendar-mode-map (kbd "RET") 'th-calendar-open-agenda)</code></pre>
<p>And here&#8217;s a small minor mode which uses the function above to refresh the agenda buffer when you move point in the calendar buffer, so calendar and agenda stay in sync.</p>
<pre><code>(define-minor-mode th-org-agenda-follow-calendar-mode
  "If enabled, each calendar movement will refresh the org agenda
buffer."
  :lighter " OrgAgendaFollow"
  (if (not (eq major-mode 'calendar-mode))
      (message "Cannot activate th-org-agenda-follow-calendar-mode in %s." major-mode)
    (if th-org-agenda-follow-calendar-mode
        (add-hook 'calendar-move-hook 'th-calendar-open-agenda)
      (remove-hook 'calendar-move-hook 'th-calendar-open-agenda))))

(add-hook 'calendar-mode-hook 'th-org-agenda-follow-calendar-mode)</code></pre>
<p>Another thing I added to calendar is the display of the week-of-year in the mode-line.</p>
<pre><code>(add-to-list 'calendar-mode-line-format
             '(let ((day (nth 1 date))
                    (month (nth 0 date))
                    (year (nth 2 date)))
                (format-time-string "Week of year: %V"
                                    (encode-time 1 1 1 day month year))))</code></pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/tsdh.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/tsdh.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=36&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2008/07/03/some-calendar-and-org-mode-integration-stuff/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>
	</item>
		<item>
		<title>My funky ZSH prompt</title>
		<link>http://tsdh.wordpress.com/2007/12/06/my-funky-zsh-prompt/</link>
		<comments>http://tsdh.wordpress.com/2007/12/06/my-funky-zsh-prompt/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 12:49:39 +0000</pubDate>
		<dc:creator>Tassilo Horn</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[ZSH]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://tsdh.wordpress.com/2007/12/06/my-funky-zsh-prompt/</guid>
		<description><![CDATA[Here&#8217;s the config for my ZSH prompt: local blue_op="%{$fg[blue]%}[%{$reset_color%}" local blue_cp="%{$fg[blue]%}]%{$reset_color%}" local path_p="${blue_op}%~${blue_cp}" local user_host="${blue_op}%n@%m${blue_cp}" local ret_status="${blue_op}%?${blue_cp}" local hist_no="${blue_op}%h${blue_cp}" local smiley="%(?,%{$fg[green]%}:%)%{$reset_color%},%{$fg[red]%}:(%{$reset_color%})" PROMPT="╭─${path_p}─${user_host}─${ret_status}─${hist_no} ╰─${blue_op}${smiley}${blue_cp} %# " local cur_cmd="${blue_op}%_${blue_cp}" PROMPT2="${cur_cmd}&#62; " It looks like this. It has two lines. The first displays the path of the current working directory, then the user and hostname, then the return [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=35&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the config for my ZSH prompt:<br />
<code>local blue_op="%{$fg[blue]%}[%{$reset_color%}"<br />
local blue_cp="%{$fg[blue]%}]%{$reset_color%}"<br />
local path_p="${blue_op}%~${blue_cp}"<br />
local user_host="${blue_op}%n@%m${blue_cp}"<br />
local ret_status="${blue_op}%?${blue_cp}"<br />
local hist_no="${blue_op}%h${blue_cp}"<br />
local smiley="%(?,%{$fg[green]%}:%)%{$reset_color%},%{$fg[red]%}:(%{$reset_color%})"<br />
PROMPT="╭─${path_p}─${user_host}─${ret_status}─${hist_no}<br />
╰─${blue_op}${smiley}${blue_cp} %# "<br />
local cur_cmd="${blue_op}%_${blue_cp}"<br />
PROMPT2="${cur_cmd}&gt; "</code></p>
<p>It looks like this. <img src="http://www.tsdh.de/images/zsh-prompt.png" alt="ZSH prompt" /></p>
<p>It has two lines.  The first displays the path of the current working directory, then the user and hostname, then the return value of the last executed command, and the last item is the command&#8217;s number in the history.</p>
<p>The second line displays a green smiley, if the last command worked well, or a red smiley, if it failed.</p>
<p>I took some inspirations from the prompt of strcat, see <a href="http://www.echox.de/blog/archives/74-ZSH-Prompt.html">http://www.echox.de/blog/archives/74-ZSH-Prompt.html</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/tsdh.wordpress.com/35/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/tsdh.wordpress.com/35/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tsdh.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tsdh.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tsdh.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tsdh.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tsdh.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tsdh.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tsdh.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tsdh.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tsdh.wordpress.com&amp;blog=573640&amp;post=35&amp;subd=tsdh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://tsdh.wordpress.com/2007/12/06/my-funky-zsh-prompt/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ab1a2fce09e326bb671eed17109a594f?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">tsdh</media:title>
		</media:content>

		<media:content url="http://www.tsdh.de/images/zsh-prompt.png" medium="image">
			<media:title type="html">ZSH prompt</media:title>
		</media:content>
	</item>
	</channel>
</rss>
