On this page:

Really funky Planner sorting

The following code sorts completed tasks in reverse-chronological
order and incomplete tasks in chronological order. This makes it
easier to see the next action (top of list) and the last action (first
completed task).

(defun sacha/planner-sort-tasks-by-date ()
  "Sort tasks by date, status and priority."
  (skip-chars-forward "#ABC")
  (let ((ch (char-before))
        status)
    (skip-chars-forward "0123456789 ")
    (setq status (char-after))
    (goto-char (line-end-position))
    (skip-chars-backward "]) ")
    (format "%1c%1c%10s"
            (cond
             ((= status ?o) ?1)
             ((= status ?X) ?3)
             ((= status ?C) ?4)
             (t ?2))
            ch
            (let ((date (or (planner-task-date (planner-current-task-info))
                            planner-sort-undated-tasks-equivalent)))
              (if (or (= status ?X)
                      (= status ?C))
                  (sacha/planner-invert-date date)
                date)))))

(defun sacha/planner-invert-date (date)
  "Reverse the date in the sorting order."
  (mapconcat (lambda (ch)
               (if (string= ch ".")
                   ch
                 (number-to-string
                  (- 9 (string-to-number ch)))))
             (split-string date "" t) ""))

pos://../emacs/planner-config.el#35188

ここのパソコンはシステムを変えることができないので何もできない。 Because the personal computer here cannot change the system, anything cannot be done.

On Technorati: , ,

semi-form letters: hooray for open source!

I needed to write 31 thank-you-for-attending-my-send-off-party e-mail
messages. Instead of having a generic letter that I’d send to lots of
people all listed in To: or Bcc:, I decided to personalize it a bit by
including their nicknames, two-year goals, and a personal message I’d
add to each letter in different places.

Fortunately, my mail client was up to the task. One of the benefits of
having both your contact information and your mail client in an
easily-programmable environment is that you can hack together a quick
program to do exactly what you want.

I had typed everyone’s two-year-plans into my contact information
manager’s Notes field following a special format. It was the work of a
few minutes to record and run a macro that extracted the data and
created a signature for each person who attended my send-off party.

I then wrote a couple of short functions that looped over the
currently displayed contact records and drafted messages for each of
them following a template.

This resulted in 31 drafts I could edit and send without hassles. Much
fun!

For future reference (and for the handful of Emacs geeks or learners
in the audience), here’s the code I used to make it all happen:

(defun sacha/bbdb-send-form-mail (bbdb-record &optional subject template)
  "Compose a form message to the people indicated by the current BBDB record(s)."
  (interactive (list (if (bbdb-do-all-records-p)
                         (mapcar 'car bbdb-records)
                       (bbdb-current-record))
                     (read-string "Subject: ")
                     (read-string "Template (): ")))
  (if (consp bbdb-record)
      (let ((records bbdb-record))
        (while records
          (sacha/bbdb-send-form-mail-1 (car records) subject template)
          (setq records (cdr records))))
    (sacha/bbdb-send-form-mail-1 bbdb-record subject template)))

(defun sacha/bbdb-send-form-mail-1  (bbdb-record &optional subject template)
  "Compose a form message for one person."
    (if bbdb-inside-electric-display
      (bbdb-electric-throw-to-execute
       (list 'sacha/bbdb-send-form-mail bbdb-record subject)))
  ;; else...
  (cond ((null bbdb-record) (error "record unexists"))
        ((null (bbdb-record-net bbdb-record))
         (message "%s record unhas a network addresses." (or (bbdb-record-name bbdb-record) "Unknown")))
        (t (bbdb-send-mail-internal (bbdb-dwim-net-address bbdb-record)
                                    subject (list bbdb-record))
           (goto-char (point-min))
           (when (re-search-forward "--text follows this line--" nil t)
             (forward-line 1)
             (insert template)
             (goto-char (point-min))
             (while (search-forward "" nil t)
               (replace-match (or (bbdb-record-getprop bbdb-record 'nick)
                                  (bbdb-record-name bbdb-record))
                              t t))
             (when (bbdb-record-getprop bbdb-record 'signature)
               (goto-char (point-max))
               (insert "\n")
               (let ((p (point)))
                 (insert (bbdb-record-getprop bbdb-record 'signature))
                 (fill-region-as-paragraph p (point))))
             (goto-char (point-min))
             (if (re-search-forward "^Subject: $" nil t) (end-of-line))))))

I used M-x local-set-key to bind sacha/bbdb-send-form-mail to M. * M
then applies the function to all displayed records.
../emacs/miniedit.el makes it easy to edit long strings in the
minibuffer, and that made the template much easier to write.

Emacs totally rocks. Nothing else has ever given me this much power.

自動車製造は人間の労働者に代わって、コンピューターが組み込まれたロボットによって行われている。 Car manufacturing is carried out by computer-programmed robots in place of human workers.

On Technorati: ,

Ruby: Turn bash.org quotes into a fortune file

The following code turns XML quotes from bash.org (a popular IRC quotes server) into a fortune-cookie file.
Handy for using with ../emacs/flashcard.el and my ../emacs/flashcard-config.el, which pops up a fortune
every time I get a correct answer.

bash-org-to-fortune.rb:

require 'rss/1.0'
require 'cgi'
require 'net/http'
host = Net::HTTP.new('bash.org', 80)
if ARGV[0] then
   resp, data = host.get('http://bash.org/xml/?top&below=' + ARGV[0], nil)
else
   resp, data = host.get('http://bash.org/xml/?top', nil)
end
parsed = RSS::Parser.parse(data, false)
parsed.items.each { |x| puts CGI::unescapeHTML(x.description.gsub('
', "\n")); puts "%\n" }

Call like this:

ruby bash-org-to-fortune.rb > bash; strfile bash; fortune bash

# or to get the top quotes with score < 1000
ruby bash-org-to-fortune.rb 1000 > bash; strfile bash; fortune bash

On Technorati: , ,

The Year in Bookmarks

Top 10 tags for 2005 productivity(104) web2.0(88) digitalpinay(88) social(84) useful(83) business(80) blogs(70) research(69) lifehacks(68) blogging(60)

Check out my year in bookmarks for more detail. =) If you want me to analyze yours, just save http://del.icio.us/api/posts/all to all.xml and run this Ruby script. You can also e-mail me ([email protected]) your all.xml if you don’t want to go through the hassle yourself.

Much fun!

#!/usr/bin/ruby

require 'rexml/document'
require 'date'

include REXML

YEAR = 2005
USER = "sachac"
doc = Document::new(File::new('all.xml'))

month_hash = {}
month_total = {}
tag_total = {}
doc.elements[1].elements.each {
  |x|
  date = DateTime::parse(x.attributes['time'])
  if (date.year == YEAR)
    x.attributes['tag'].split(' ').each {
      |tag|
      month_hash[date.month] ||= {}
      month_hash[date.month][tag] ||= 0
      month_hash[date.month][tag] += 1
      tag_total[tag] ||= 0
      tag_total[tag] += 1
    }
    month_total[date.month] ||= 0
    month_total[date.month] += 1
  end
}

s = "Top 10 tags for " + YEAR.to_s + " |"
tag_total.sort_by { |tag,total| -total }.slice(0, 10).each { |tag,total|
  s += ' " + tag + "(" + total.to_s + ")"
}
puts s

month_hash.sort.each { |month,tags|
  s = Date::MONTHNAMES[month] + "
(" + month_total[month].to_s + " bookmarks) |" tags.sort_by { |tag,total| -total }.each { |tag,total| s += ' " + tag + "(" + total.to_s + ")" } puts s }

On Technorati: , ,

Emacs BBDB magic: Greeting people with nicknames

I use Gnus to read my mail within the Emacs text editor. One of the
advantages of using a mail client that’s infinitely programmable is
that you can add all sorts of little tweaks to it. Gnus can be
integrated with Emacs’ Big Brother Database (BBDB), a semi-structured
text database in which I store all sorts of weird notes. This little
hack takes the nick field of the database and automatically inserts a
greeting. If someone signs himself as Mikong, I should call him that
instead of Joseph Michael. Similarly, I sign my messages as Sacha, not
Sandra Jean. This little tidbit makes it easier to remember to call
people by their nicknames.

(defun sacha/gnus-add-nick-to-message ()
  "Inserts \"Hello, NICK!\" in messages based on the recipient's nick field."
  (interactive)
  (save-excursion
    (let ((bbdb-get-addresses-headers (list (assoc 'recipients bbdb-get-addresses-headers)))
          nicks)
      (setq nicks
            (delq nil
                  (mapcar (lambda (rec) (bbdb-record-getprop rec 'nick))
                          (bbdb-update-records
                           (bbdb-get-addresses nil gnus-ignored-from-addresses 'gnus-fetch-field)
                           nil
                           nil))))
      (goto-char (point-min))
      (when (and nicks
                 (re-search-forward "--text follows this line--" nil t))
        (forward-line 1)
        (insert "Hello, "
                (mapconcat 'identity nicks ", ")
                "!\n\n")))))

(defadvice gnus-post-news (after sacha activate)
  (sacha/gnus-add-nick-to-message))

On Technorati: , , ,

Random Japanese sentence: 虎を大きな猫というなら、同じように猫を小さな虎といってもよい。 You may as well call a cat a small tiger as call a tiger a big cat.

Getting the WordPress Lifestream plugin to work on my blog

I’ve been thinking about including a digest of Twitter, Delicious bookmarks, Google Reader shared items, and other social activity in my weekly review. This lets me include the information in my archive, and it gives people more opportunities to bump into things I found interesting.

It took a bit of hacking, but I eventually got the Lifestream plugin for WordPress to work, with the help of another webpage and some source code diving. Here’s the code that powers this lifestream page:

<?php $options = array('limit' => 50); $events = $lifestream->get_events($options); foreach ($events as $event) { echo '<li>'; $label_inst = $event->get_label_instance($options); if ($event->feed->options['icon_url']) { echo '<img src="' . $event->feed->options['icon_url'] . '" alt="(' . $event->feed->options['feed_label'] . ') \ "> '; } echo '<a href="' . $event->data[0]['link'] . '">' . $event->data[0]['title'] . '</a> (' . date('D, M j, Y', $event->data[0]['date']) . ')'; echo '</li>'; } ?>

$event->render had been giving me problems, so I specified my own output format. It didn’t automatically pick up icon URLs, so I specified the URLs myself. (Bug: the settings get lost if you re-configure the feed.) The plugin seems to be broken out of the box, but there are enough pieces in there for a geek to make things work.

Because I don’t want to use up two of my one-post-a-day slots on weekly reviews, I’m leaving it as a web page that I can review and manually copy into my weekly review post instead of automatically publishing something.

You can see it in action in last week’s review.

Work in progress. Hope this helps!