Category Archives: pimpmyemacs

Personal contact relationship management

It’s a good thing that computer geeks appreciate automation. They can
sniff out form e-mail in seconds, but they don’t mind as long as it
comes from a very clever technical hack. Such was the case with the
form letter engine I put together just in time to ask people for their
postal addresses for my holiday updates. Paul Lussier wanted to know what kind of Emacs Lisp magic I was doing
behind the scenes. Simon Ditner got his
revenge by obfuscating his reply with 1337sp34|<. People humored me and replied with their addresses and birthdays, knowing that although the e-mail they got may have been mostly automated, my interest in them and my replies to the replies they sent me were very much real.

Good magicians never reveal their tricks, but I like talking about the
crazy Emacs wizardry that goes on behind the scenes. Let me lift the
curtain:

(concat
 "Hello, " (or (bbdb-record-getprop record 'nick) (bbdb-record-name record)) "!

I've actually managed to write my 2006 life update / holiday
letter somewhat in time, and will be mailing them out soon. I'd
love to find out how your year has been and what you're planning
to do next year, and I'd be happy to keep you up to date too!

"

(cond
 ((= (length (bbdb-record-addresses record)) 1)
  (concat "Is this address the best one to reach you at?\n\n"
          (sacha/bbdb-address-string (car (bbdb-record-addresses record)))))
 ((> (length (bbdb-record-addresses record)) 1)
  (concat "Which of these addresses is the best one to reach you at?\n\n"
          (mapconcat 'sacha/bbdb-address-string (bbdb-record-addresses record) "\n")))
 (t "I don't seem to have a mailing address for you, though. I'd
like to be able to snail-mail you postcards or holiday updates.
I promise not to use your address for anything evil! =) What's the best
way to send something to you?"))

(if (bbdb-record-getprop record 'birthdate)
    ""
  "\n\nBy the way, when is your birthday?")
"\n\nHope to hear from you soon!

Sacha Chua

p.s. No kittens were harmed in the writing of this message.")

That’s the source for my form letter – a Lisp expression, allowing me
to use the full power of Emacs. I used that as the input to the
following function:

(defun sacha/gnus-send-message-to-all (subject &optional text)
  "Compose message to everyone, with notes.
SUBJECT is a string.
TEXT is a string or an arbitrary Lisp expression starting with (."
  (interactive
   (list (read-string "Subject: ")
         (read-string "Body: ")))
  (let ((records bbdb-records))
    (while records
      (when (bbdb-record-net (caar records))
        (bbdb-send-mail (caar records) subject)
        (goto-char (point-min))
        (re-search-forward "--text " nil t)
        (forward-line 1)
        (let ((record (caar records)))
          (when text
            (insert (if (= (aref text 0) ?\() (eval (read text)) text))))
        (when (bbdb-record-notes (caar records))
          (save-excursion
            (insert "\n--- NOTES ---\n"
                    (bbdb-record-notes (caar records))
                    "\n--- END NOTES ---\n"))))
      (setq records (cdr records)))))

The function composed a message for each of the records currently
displayed. I edited the messages by hand, combining messages where
appropriate, and sent them off.

What else can I do with this? Because this function accepts arbitrary
Lisp expressions, it would be really easy to include a random
holiday-related greeting or poem. If I had a database of significant
events, I can include a random factoid about the recipient’s birthday.
If I had a local database of people’s names, I could send one-off
messages including the meaning of their names.

Yes, it’s pretty crazy, but that’s what you get when you have a geek
who cares about connecting with people. I’ve stolen all the cool
features from the contact relationship management systems I know
about, and I keep trying out more ideas. It’s a pity that the base
system I’m working on can be quite intimidating. If I found the time
to learn enough, say, Microsoft Outlook programming to implement a
similar system, I think I’d have quite a market.

Even with my idiosyncratic setup, though, it’s fun pushing the
envelope. =) There are a lot of other things I’d like to add, and I
don’t think I’ll ever stop coming up with new ideas. In terms of
personal contact relationship management, I’ve got one of the most
advanced systems I know—which just means I need to get to know more
people, so that I can find other inspirations!

On Technorati: , ,

Random Emacs symbol: previous-buffer – Command: Switch to the previous buffer in cyclic order.

Yay, done with Emacs 22 prerelease review!

All sorts of goodies in the new Emacs 22. I’ve just sent a ZIP of the
article and some images off to Don Marti, and I
look forward to merciless editing. I won’t be able to post it on my
blog until some time after it gets released, but that was fun to
write.

I had to trim so much material from the individual sections.
Post-thesis, I should really start an Emacs column…

Chatting on #emacs gave me an interesting idea. You know, that Livin’
la Vida Emacs talk I gave at DemoCamp would be great as a
polished talk that I could take on a tour. Post-thesis (or maybe
mostly-done-thesis), I should take to the road and go across US and
Canada talking about tech and meeting up with all sorts of cool
people. It’ll be a great excuse to visit Google and Amazon, which must
have lots of incredibly cool Emacs users.

Someday…

On Technorati: ,

Random Emacs symbol: remember – Command: Remember an arbitrary piece of data. – Group: A mode to remember information.

The history of Calc

I’m taking some time off from cramming my KMD2004 project to write a
review of Emacs 22 prerelease for Don Marti. It’s a great excuse to
look at all the cool new features I’d been taking for granted in my
Emacs CVS.

I love the stories people tell through their code and their
documentation! For example, take Calc mode. You’d expect it to be a
simple desk calculator, right? No, it can do “arithmetic on rational
numbers, complex numbers (rectangular and polar), error forms with
standard deviations, open and closed intervals, vectors and matrices,
dates and times, infinities, sets, quantities with units, and
algebraic formulas.” It has tons of other features, too.

How did a calculator get so big, the way all Emacs features seem to
grow and grow and grow? Check out the History and Acknowledgements
section of the info manual for Calc. Here’s the story from David
Gillespie in the manual:

Calc was originally started as a two-week project to occupy a lull in
the author’s schedule. Basically, a friend asked if I remembered the
value of `2^32′. I didn’t offhand, but I said, “that’s easy, just call
up an `xcalc’.” `Xcalc’ duly reported that the answer to our question
was `4.294967e+09’—with no way to see the full ten digits even though
we knew they were there in the program’s memory! I was so annoyed, I
vowed to write a calculator of my own, once and for all.

I chose Emacs Lisp, a) because I had always been curious about it
and b) because, being only a text editor extension language after all,
Emacs Lisp would surely reach its limits long before the project got
too far out of hand.

To make a long story short, Emacs Lisp turned out to be a
distressingly solid implementation of Lisp, and the humble task of
calculating turned out to be more open-ended than one might have
expected.

Emacs Lisp doesn’t have built-in floating point math, so it had to be
simulated in software. In fact, Emacs integers will only comfortably
fit six decimal digits or so—not enough for a decent calculator. So I
had to write my own high-precision integer code as well, and once I had
this I figured that arbitrary-size integers were just as easy as large
integers. Arbitrary floating-point precision was the logical next step.
Also, since the large integer arithmetic was there anyway it seemed only
fair to give the user direct access to it, which in turn made it
practical to support fractions as well as floats. All these features
inspired me to look around for other data types that might be worth
having.

Around this time, my friend Rick Koshi showed me his nifty new HP-28
calculator. It allowed the user to manipulate formulas as well as
numerical quantities, and it could also operate on matrices. I decided
that these would be good for Calc to have, too. And once things had
gone this far, I figured I might as well take a look at serious algebra
systems for further ideas. Since these systems did far more than I
could ever hope to implement, I decided to focus on rewrite rules and
other programming features so that users could implement what they
needed for themselves.

Rick complained that matrices were hard to read, so I put in code to
format them in a 2D style. Once these routines were in place, Big mode
was obligatory. Gee, what other language modes would be useful?

Scott Hemphill and Allen Knutson, two friends with a strong
mathematical bent, contributed ideas and algorithms for a number of
Calc features including modulo forms, primality testing, and
float-to-fraction conversion.

Units were added at the eager insistence of Mass Sivilotti. Later,
Ulrich Mueller at CERN and Przemek Klosowski at NIST provided invaluable
expert assistance with the units table. As far as I can remember, the
idea of using algebraic formulas and variables to represent units dates
back to an ancient article in Byte magazine about muMath, an early
algebra system for microcomputers.

Many people have contributed to Calc by reporting bugs and suggesting
features, large and small. A few deserve special mention: Tim Peters,
who helped develop the ideas that led to the selection commands, rewrite
rules, and many other algebra features; Francois Pinard, who
contributed an early prototype of the Calc Summary appendix as well as
providing valuable suggestions in many other areas of Calc; Carl Witty,
whose eagle eyes discovered many typographical and factual errors in
the Calc manual; Tim Kay, who drove the development of Embedded mode;
Ove Ewerlid, who made many suggestions relating to the algebra commands
and contributed some code for polynomial operations; Randal Schwartz,
who suggested the `calc-eval’ function; Robert J. Chassell, who
suggested the Calc Tutorial and exercises; and Juha Sarlin, who first
worked out how to split Calc into quickly-loading parts. Bob Weiner
helped immensely with the Lucid Emacs port.

Among the books used in the development of Calc were Knuth’s _Art of
Computer Programming_ (especially volume II, _Seminumerical
Algorithms_); _Numerical Recipes_ by Press, Flannery, Teukolsky, and
Vetterling; Bevington’s _Data Reduction and Error Analysis for the
Physical Sciences_; _Concrete Mathematics_ by Graham, Knuth, and
Patashnik; Steele’s _Common Lisp, the Language_; the _CRC Standard Math
Tables_ (William H. Beyer, ed.); and Abramowitz and Stegun’s venerable
_Handbook of Mathematical Functions_. Also, of course, Calc could not
have been written without the excellent _GNU Emacs Lisp Reference
Manual_, by Bil Lewis and Dan LaLiberte.

Final thanks go to Richard Stallman, without whose fine
implementations of the Emacs editor, language, and environment, Calc
would have been finished in two weeks.

I’ve had a lot of these two week projects. I wasn’t supposed to get
hooked on Planner. It was just supposed to be one of the components of
my fourth-year undergrad project. It turned into a way of life and my
main open source project for almost three years.

Most people look at Emacs and they see an editor with way, way, way
too many features. How many people need to do in-line matrix
calculations, anyway? I might never use it (then again, who knows?),
but I think it’s terrific that someone just sat down one day and put
it in. When I look at Emacs, I see more than a text editor. I see a
community of hackers and a tradition of tinkerers. It’s awesome. =)

On Technorati: ,

History and Acknowledgements

Random Emacs symbol: enable-kinsoku – Variable: *Non-nil means enable “kinsoku” processing on filling paragraphs.

Contact report

I started tracking e-mail sent on 2006.09.01 with a
nifty piece of Emacs Lisp code I wrote just for the
purpose. Now I have two months of interesting data which include not
only e-mail but also the occasional in-person contact or phone call
that I remember to note. It’s not complete – e-mail’s the only thing
that gets automatically tracked – but it does give me interesting
information. Here’s the contact report for your amusement:

Contact report

It’s sorted by overall frequency and then by regular frequency.
Warning! Parentheses follow.

(defun sacha/count-matches (regexp string)
  (let ((count 0)
        (start 0))
    (while (string-match regexp string start)
      (setq start (match-end 0)
            count (1+ count)))
    count))

(defun sacha/bbdb-contact-report-as-alist (&rest regexps)
  "Creates a list of (name count-regexp1 count-regexp2 count-regexp3)..."
  (setq regexps (reverse regexps))
  (delq nil
        (mapcar
         (lambda (rec)
           (when (bbdb-record-name (car rec))
             (let ((reg regexps)
                   (notes (bbdb-record-notes (car rec)))
                   list)
               (while reg
                 (setq list (cons (sacha/count-matches (car reg) notes)
                                  list))
                 (setq reg (cdr reg)))
               (cons (sacha/planner-bbdb-annotation-from-bbdb rec)
                     list))))
         bbdb-records)))

(defun sacha/bbdb-alist-sort-by-total (alist)
  "Sort ALIST by total contact."
  (sort alist 'sacha/bbdb-contact-sort-predicate))

(defun sacha/bbdb-contact-sort-predicate (a b)
  (and a b
       (let ((count-a (apply '+ (cdr a)))
             (count-b (apply '+ (cdr b))))
         (or
          (> count-a count-b)
          (and (= count-a count-b)
               ;; If equal, look at the subtotal of the rest
               (sacha/bbdb-contact-sort-predicate (cdr a) (cdr b)))))))

(defun sacha/bbdb-kill-contact-barchart (alist)
  "Kill a barchart with the contact report for ALIST."
  (kill-new
   (mapconcat
    (lambda (entry)
      (concat
       (car entry)
       " | "
       (mapconcat (lambda (count)
                    (if (= count 0)
                        " "
                      (make-string count ?-)))
                  (cdr entry)
                  " | ")))
    alist
    "\n")))

;; Usage: (sacha/bbdb-kill-contact-barchart
;;         (sacha/bbdb-alist-sort-by-total
;;          (sacha/bbdb-contact-report-as-alist "2006.09" "2006.10")))
;; Then yank (paste) this into another buffer

On Technorati: , , , ,

Random Emacs symbol: standard-display-cyrillic-translit – Command: Display a cyrillic buffer using a transliteration.

Keeping track of the age of messages

I can get pretty bad at responding to e-mail. This is an experiment to
see whether the negative reinforcement of seeing just how old a
message is will help me be more responsive. Either that, or I can
strive for a Mean Time Between Responses of whatever… ;)

Hmm, maybe I should combine this with my blog and start distinguishing
between E-mail to and Reply to…

(defadvice gnus-post-news (around sacha/gnus-track-message-age activate)
  "Insert a header showing how old a message is, to shame me into replying faster."
  ;; Before you post the news, figure out how old it is
  (let (days)
    (when article-buffer
      (setq days
            (- (time-to-days (current-time))
               (time-to-days
                (gnus-date-get-time
                 (mail-header-date
                  (gnus-summary-article-header
                   (gnus-summary-article-number))))))))
    ad-do-it
    (when days
      (goto-char (point-min))
      (when (re-search-forward "--text follows this line--" nil t)
        (forward-line 1)
        (insert "In reply to a message sent by "
                (mail-header-from message-reply-headers)
                " "
                (cond
                 ((= days 0) "today")
                 ((= days 1) "yesterday")
                 (t (format "%d days ago" days)))
                ": \n\n")))))
(setq message-citation-line-function nil)

On Technorati: , ,

Random Emacs symbol: tramp-perl-directory-files-and-attributes – Variable: Perl script implementing `directory-files-attributes’ as Lisp `read’able

Planet Emacsen

http://planet.emacsen.org/ by Edward O’Connor. ‘Nuff said! =D

On Technorati: ,

Random Emacs symbol: ido-subdir – Face: *Font used by ido for highlighting subdirs in the alternatives.