I never write about Emacs because I’m such a n00b and most of my ELisp “programming” has consisted of pasting random code from the Internet into my ~/.emacs file and hoping it works. But finally today I wrote a lick of it and it worked!

I’ve been using Ethan Schoonover’s obsessively comfortable “Solarized” color scheme for a while now, but I’ve become annoyed how many keys I have to mash to switch from light to dark. So I set a global variable to track the current state and mapped Ctrl-c, d (which I at least wasn’t using) to a function to toggle between the two states.

(when window-system
 
  ;; ColorTheme - see http://www.emacswiki.org/cgi-bin/wiki/ColorTheme
  ;; and see gallery at http://www.cs.cmu.edu/~maverick/GNUEmacsColorThemeTest/
  ;; try also M-x color-theme-select
 
  (message "loading color-theme")
  (add-to-list 'load-path "~/.emacs.d/color-theme-6.6.0")
 
  (require 'color-theme)
 
  ;; Ethan Schoonover's Solarized theme from
  ;; https://github.com/sellout/emacs-color-theme-solarized
  (load-file "~/.emacs.d/emacs-colors-solarized/color-theme-solarized.el")
 
  ;; Set initial theme to "dark"
  (setq dark-or-light 'dark)
  (color-theme-solarized dark-or-light)
 
  ;; Shortcut to toggle between light and dark
  (global-set-key (kbd "C-c d")
                    (lambda ()
                      (interactive)
		      (if (eq dark-or-light 'light)
			  (setq dark-or-light 'dark)
			  (setq dark-or-light 'light)
			)
                      (color-theme-solarized dark-or-light)))
  )

And ooh, I just found this awesome crash course on Elisp by Steve Yegge which I will certainly consult next time I have a reason to write my own (i.e., I can’t paste it from some random Internet).

And then after it was all over I finally found some instructions for doing it which would’ve been easy enough to paste. Ah well, I’ve learned something.

2012-3
27

Steps to get PHPUnit to run on my XAMPP setup with Cygwin, so I can write and run Symfony2 unit tests.

1. Upgrade PEAR

  1. Download http://pear.php.net/go-pear.phar to C:\xampp\php\go-pear.phar

  2. Run this in cmd.exe (cygwin prompts hosed somehow), taking all defaults

c:\xampp\php>go-pear.bat
  1. Yay.
c:\xampp\php>pear version
PEAR Version: 1.9.4
PHP Version: 5.3.5
Zend Engine Version: 2.3.0
Running on: Windows NT FAI1046162 6.1 build 7600 (Unknow Windows version Enterpr
ise Edition) i586

2. Install/upgrade PHPUnit

Now we can use the cygwin shell. Not sure all these channels are needed, I did this out of order.

cd /cygdrive/c/xampp/php
pear update-channels
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear channel-discover pear.phpunit.de
 
pear install --alldeps phpunit/PHPUnit

3. Use it

XAMPP and/or PHPUnit come with a wrapper called phpunit.bat, which has now been upgraded, but you may need to set your PHPBIN environment var. Also I already have c:\xampp\php in my $PATH.

export PHPBIN=c:/xampp/php/php.exe
 
cd /path/to/mysf2project
phpunit.bat -c app

Now PHPUnit works. Make tests and make them work!

Propel 1.6 (and Propel 1.5 before it) is pretty sweet (thank you François!). I had some confusion with my model’s array of related objects though, thinking it was a regular PHP array. Actually it’s a Collection, specifically a PropelObjectCollection, which implements PHP 5′s ArrayObject interface. You can do a lot of cool things with them.

Sorting

Not immediately obvious, however, was how to sort them. This did the trick for my case (I have a Sequence field manually re-calculable through a jQueryUI sortable widget. Also note the cool inline anonymous function syntax available since PHP 5.3. Incidentally, I’m not sure the terms lambda or closure are helpful because they’re not quite like Lisp lambdas or JavaScript closures.

// Re-sort them by Sequence, numerically
$this->collSegments->uasort(function($a, $b) {
    return $a->getSequence() - $b->getSequence();
});
// Re-sort them as strings, case-insensitively.
$this->collSegments->uasort(function($a, $b) {
    return strnatcasecmp($a->__toString(), $b->__toString();
});

Deleting

Thanks to the PropelArrayCollection API Documentation

  /*
   * Remove the provided Segment object.
   *
   * @param Segment $s
   * @return Segment $s that was deleted.
   */
  public function deleteSegment(Segment $s) {
      $s->delete();
      $key = $this->collSegments->search($s);
      $ret = $this->collSegments->remove($key);
      return $s;
  }

So elsewhere,

	      $this->deleteSegment($s);

Problem

On my Snow Leopard machine this kept happening.

  • The “open” indicator (glowing silver ball under the app icon) in the Dock was flaky, only showing for a few apps though more were running.
  • The task switcher (which you see when you option-tab/alt-tab) didn’t show all running apps. That RUINS it for me. I always use option-tab.
  • I hate rebooting.

Workaround–Restart the Dock process.

Open Terminal† and use one command:

killall -HUP Dock

Don’t be afraid of “killall”. HUP means “Hang Up” and is the normal way of telling something to relaunch.

Thanks to AcmeTech’s old post.

† Terminal is located in the Utilities folder in the Applications folder. Or the quick Spotlight way is command-space, Terminal.

A while back I realized something important:

Monitoring tools are to the sysadmin what testing tools are to the developer.

Recently I realized that there need to be more ways to bring both toolsets together. Here’s one, tying the Nagios monitoring toolset to anything that emits the popular Test Anything Protocol.

More …

I just got this working today. The weirdnesses of Cygwin’s half Unix-half Windows nature had stymied me before, but I’ve prevailed!

I’ve got Gnu Emacs 23.3 for Windows installed in c:/emacs, and a fairly recent install of Cygwin on Windows 7.

Saved this script in ~/cygemacs.sh

#!/usr/bin/bash
c:/emacs/bin/emacsclientw.exe -n -a "c:/emacs/bin/runemacs.exe" `cygpath -wa $@`

Then in my ~/.bashrc:

alias ec="~/cygemacs.sh"

Now I can be all like,

$ ec ~/.minttyrc

And it opens a new frame in my running Emacs (I have (server-start) in my ~/.emacs), or starts Emacs and opens the file if Emacs isn’t running yet.

See also: EmasClient at EmacsWiki.

Bonus tips

Yes I’m still using Subversion but also gitting going with Git.

And courtesy of The Lumber Room, in ~/.bashrc and others:

    export SVN_EDITOR='c:/emacs/bin/emacsclientw.exe -a c:/emacs/bin/runemacs.exe '

And silence that annoying “kill client buffer z0mgbbq?!?” warning:

  (remove-hook 'kill-buffer-query-functions 'server-kill-buffer-query-function)

Stupid me made the same mistake twice in a row so I’m documenting it for humanity.

I’m updating an old Symfony project to use Symfony 1.3/1.4 and Propel 1.5 through François Zaninotto’s sfPropel15Plugin.

Problem:

I followed the README, right? But…

$ ./symfony propel:build --forms
>> schema    converting "C:/web/myproject/config/schema.yml" to XML
>> schema    putting C:/web/myproject/config/generated-schema.xml
>> propel    Running "om" phing task
>> file-     C:/web/myproject/config/generated-schema.xml
>> autoload  Resetting application autoloaders
>> autoload  Resetting CLI autoloader
>> propel    generating form classes
PHP Warning:  call_user_func() expects parameter 1 to be a valid callback, class 'FooPeer' does not have a method 'getUniqueColumnNames' in C:\web\myproject\plugins\sfPropel15Plugin\lib\generator\sfPropelFormGenerator.class.php on line 485
PHP Stack trace:
PHP   1. {main}() C:\web\myproject\symfony:0
PHP   2. include() C:\web\myproject\symfony:14
PHP   3. sfSymfonyCommandApplication->run() C:\symfony1.3\lib\command\cli.php:20
PHP   4. sfTask->runFromCLI() C:\symfony1.3\lib\command\sfSymfonyCommandApplication.class.php:76
PHP   5. sfBaseTask->doRun() C:\symfony1.3\lib\task\sfTask.class.php:97
PHP   6. sfPropelBuildTask->execute() C:\symfony1.3\lib\task\sfBaseTask.class.php:68
PHP   7. sfTask->run() C:\web\myproject\plugins\sfPropel15Plugin\lib\task\sfPropelBuildTask.class.php:135
PHP   8. sfBaseTask->doRun() C:\symfony1.3\lib\task\sfTask.class.php:173
PHP   9. sfPropelBuildFormsTask->execute() C:\symfony1.3\lib\task\sfBaseTask.class.php:68
PHP  10. sfGeneratorManager->generate() C:\web\myproject\plugins\sfPropel15Plugin\lib\task\sfPropelBuildFormsTask.class.php:72
PHP  11. sfPropelFormGenerator->generate() C:\symfony1.3\lib\generator\sfGeneratorManager.class.php:126
PHP  12. sfGenerator->evalTemplate() C:\web\myproject\plugins\sfPropel15Plugin\lib\generator\sfPropelFormGenerator.class.php:106
PHP  13. require() C:\symfony1.3\lib\generator\sfGenerator.class.php:84
PHP  14. sfPropelFormGenerator->getUniqueColumnNames() C:\web\myproject\plugins\sfPropel15Plugin\data\generator\sfPropelForm\default\template\sfPropelFormGeneratedTemplate.php:34
PHP  15. call_user_func() C:\web\myproject\plugins\sfPropel15Plugin\lib\generator\sfPropelFormGenerator.class.php:485
... etc ...

Solution

I removed too much of the default config in propel.ini..

Make sure this original line is still intact in propel.ini, even though you’ve removed/commented out all the propel.behavior lines that point to the old sfPropelPlugin:

propel.behavior.default                        = symfony,symfony_i18n

So, I had to deal with ISO 8601 formatted dates in JavaScript, and I can’t just use the cool ISO 8601 support in js 1.8.5′s Date.parse because of IE 6. I found a couple of examples out there on blogs but couldn’t get them to work right with my data, and I wasn’t sure I wanted a heavyweight do-it-all solution. I reluctantly reinvented the wheel so I thought I’d share it.

function parseISO8601Date(s){
 
  // parenthese matches:
  // year month day    hours minutes seconds  
  // dotmilliseconds 
  // tzstring plusminus hours minutes
  var re = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(\.\d+)?(Z|([+-])(\d\d):(\d\d))/;
 
  var d = [];
  d = s.match(re);
 
  // "2010-12-07T11:00:00.000-09:00" parses to:
  //  ["2010-12-07T11:00:00.000-09:00", "2010", "12", "07", "11",
  //     "00", "00", ".000", "-09:00", "-", "09", "00"]
  // "2010-12-07T11:00:00.000Z" parses to:
  //  ["2010-12-07T11:00:00.000Z",      "2010", "12", "07", "11", 
  //     "00", "00", ".000", "Z", undefined, undefined, undefined]
 
  if (! d) {
    throw "Couldn't parse ISO 8601 date string '" + s + "'";
  }
 
  // parse strings, leading zeros into proper ints
  var a = [1,2,3,4,5,6,10,11];
  for (var i in a) {
    d[a[i]] = parseInt(d[a[i]], 10);
  }
  d[7] = parseFloat(d[7]);
 
  // Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
  // note that month is 0-11, not 1-12
  // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC
  var ms = Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6]);
 
  // if there are milliseconds, add them
  if (d[7] > 0) {  
    ms += Math.round(d[7] * 1000);
  }
 
  // if there's a timezone, calculate it
  if (d[8] != "Z" && d[10]) {
    var offset = d[10] * 60 * 60 * 1000;
    if (d[11]) {
      offset += d[11] * 60 * 1000;
    }
    if (d[9] == "-") {
      ms -= offset;
    }
    else {
      ms += offset;
    }
  }
 
  return new Date(ms);
};

A friendly user named Raynos in the new StackOverflow JavaScript chat room helped me with the 0-based month gotcha. Thanks again, Raynos!

Also, the author of Anentropic tipped me off to this effort to promote good-quality JavaScript documentation, like the Mozilla Javascript Reference and Javascript Guide, which are still definitive after all these years. Googling for javascript stuff usually gets you spam sites with lots of ads and less-than-stellar docs. W3schools, I love you, but seriously.

So listen, Google:

Javascript Date .UTC

I published my second plugin for WordPress today. It lets you configure how long user sessions and the “remember me” cookie last. By default they’re only 2 days and 2 weeks, respectively.

Twofold impetus: a problem with the internal P2 microblog at work when people like me left their browsers open for long periods, and the fact that I access too many WordPress blogs from too many different computers for the 2 week “Remember Me” timeout to be convenient enough. Cranking it up to 22 years!

The WordPress documentation is pretty great BTW.

And it was a little bit interesting to run in to the Year 2038 problem with 32-bit int timestamps.  I don’t know, it was fun working around the  ”infinity” ceiling:

  if ( PHP_INT_MAX - time() < $expire_in ) {
    $expire_in =  PHP_INT_MAX - time() - 5;
  }