Thursday, August 25, 2016

An Emacs Friendly Caps Lock Configuration on Windows

While this may be obvious, I was pretty dang pleased with myself when I managed to turn the Caps Lock key on my Windows 10 computer into an emacs friendly Hyper key. Here's what I did:

Step 1. Use AutoHotKey to trivially map the Caps Lock key to the Windows Menu key, or as AutoHotKey calls it, the AppsKey.

;; Add this to your standard AutoHotKey configuration

Step 2. Use this elisp code to capture the Menu key from within emacs and map it to the Hyper modifier:

(setq w32-pass-apps-to-system nil)
(setq w32-apps-modifier 'hyper) ; Menu/App key

Step 3. Enjoy! I can now map any key binding using the H- modifier. Here's some code I added to my PHP setup:

(defun bs-php-mode-hook ()
  (local-set-key '[backtab] 'indent-relative)
  (local-set-key (kbd "<H-left>") 'beginning-of-defun)
  (local-set-key (kbd "<H-right>") 'end-of-defun)
  (auto-complete-mode t)
  (require 'ac-php)
  (setq ac-sources  '(ac-source-php ))
  (yas-global-mode 1)
  (setq indent-tabs-mode nil)
  (setq php-template-compatibility nil)
  (setq c-basic-offset 2))

The result: when I open up a PHP file, I can jump between function definitions by holding down Caps Lock and left or right arrow.

I feel like I just won the keyboard shortcut lottery!

Wednesday, August 24, 2016

BYOWB - The Joy of the Portable Whiteboard

Between my love of whiteboards and my fascination with all things portable, the Noteboard was pretty much custom built for me. The Noteboard is a collection laminated 3x5 cards that can fold out, like a city map, to produce a 15"x35" whiteboard.

My Mother-in-Law was kind enough to gift me one (thanks Mom!). I'd played with it a couple of times, but had yet to find the perfect use for it.

Earlier this week I had an onsite meeting, and while the other attendees busted out their laptops and leather bound notebooks, I took out my Noteboard. I unfolded about half of it, making for a relatively contained workspace. As we started into the meeting, I used each individual 3x5 section to capture a different core concept. When the bulk of the meeting was over, I had a fantastic summary in front of me. When I ran out of cards, I simply unfolded another row.

Once I was satisfied with my notes, I took out my cell phone and snapped a photo. And just like that, I had captured my notes for posterity.

As we wrapped up the meeting, a discussion of source code repositories came up, and I quickly sketched out various branching strategies on my now cleaned off whiteboard space. This was the kind of discussion that absolutely needs a drawing to make sense, and the Noteboard provided the perfect space to create it.

I went into the meeting wary of how useful the Noteboard would be in real life. Would it be too bulky? Nope, you can fold it in half and still write on it. Does it need to be hung up to be useful? Nope, just treating it like a large sheet of notebook paper works just fine. Would the underlying 3x5 notecards be too limiting? Nope, they're perfect for capture core concepts.

As you can tell, I left a believer.

Years ago I stopped bringing a laptop to meetings. They've always been more of a distraction than anything else, and my Phone + Bluetooth Keyboard + browser / ssh is surprisingly capable. But the Noteboard is now officially considered must bring gear. I'll have to pick up a collection of mini whiteboard markers to bring my note taking to a new level.

Bonus tip: when I got home, I realized that the "hand sanitizer" I carry would be ideal for getting and keeping the cards nice and clean.

Tuesday, August 23, 2016

Turtles at Sunrise

Back in High School, I'd run with a group of friends before school. We called ourselves the Turtles and were quite fine with the implication that we were slow and steady.

There's nothing quite like getting a run with friends at 5am, when the rest of the city is still asleep, and your inbox (or high school math homework) can be thoroughly ignored. Time seems to stop, as all that matters is good conversation and pounding out footsteps.

This morning, we had a partial reunion of the Turtles as Elizabeth and I squeezed in a morning run. Such fun! We effectively caught sunrise at the Lincoln Memorial, something that if I'd planned would never have happened.

Go Turtles!

The Making of a Good Fish Tale

A couple of months ago, while talking with Chana, Dovid and Tzipora about their summer visit, I mentioned we might go fishing. And just like that, they decided this was a must do activity. From then on, any discussion of their visit would include the reminder that we were going fishing (with Dovid already concerned about how we'd prepare all the fish we were obviously going to catch). Heck, the first thing they said when we arrived in Boston to pick them up is that they were excited to go fishing and camping.

Now don't get me wrong, I was eager to take them fishing. But I also knew that it may very well not happen, and if it did happen, they may be disappointed with the experience. Fishing often consists of a lot of waiting around, not exactly 7 and 5 year old strengths.

This morning, I realized I'd have a window of time to take them fishing tonight and decided to go for it.

The first problem: I didn't have any gear for them. Without any options left, I went to Dick Sporting Goods, found the fishing section and asked a person working there for help. He walked me through the kid friendly pole options, and handed me the sinkers, hooks and live bait that I would need. He even showed me the multi-color bobbers that he thought the kids would enjoy (which they did).

I considered my rod and reel options: I could buy a low end adult setup, but they just looked huge when I imagined them in the hands of the kids, or I could buy what was obviously kid oriented junk. As a compromise, again at the recommendation of this clerk, I picked up three Zebco Dock Demons. These are short fishing rods, but designed for adult use. I left the store fully armed for our fishing adventure and totally pleased with my brick and mortar shopping experience. Go Dicks!

The next problem: where to actually take them fishing. I'd been considering the C&O Canal in Georgetown, but I wasn't sure that was my best option. So I decided I'd head over to Fletchers Cove: that would get us canal and Potomac access, and most importantly, a small bait shop where I could ask for advice. When I arrived, the guys at the kiosk highly recommended the canal. One of the clerks explained that he and "everyone else" had learned to fish on the canal. Perfect, say no more.

So there I was, with three kids, three tiny rods and a body of water. Let's fish!

I have to say, the kids did amazingly well. Our first few casts into the canal showed no sign of life. But as we walked up 20 yards or so, I could see some smaller fish. I had Tzipora drop her line in, and the fish took her worm. This was fun!

And so for the next 45 minutes or so, we worked this little section of the canal. I'd bait a hook, toss a line out, hand a kid a rod and repeat. While the kids weren't catching fish, they were having their bait taken and getting more and more comfortable with the worms as well as the rest of the process.

And then Dovid announced, as I was helping bait Tzipora's line, that he had a fish on. I went over to help him, assuming that he had snagged the bottom, but found that he had reeled in a catfish. I got a few blurry pictures of it as it flopped around, and then, without any disappointment on my part, we lost it while trying to bring it ashore. Trust me, this guy was huge! He must have been at least 15 inches, if not longer. We'd caught a fish. Hurray!

Before I knew it, it was time to pack it in. The kids all left happy. When they had arrived, they'd been squeamish about the worms, yet and when we left, they were all confident enough to handle the bits of worm left on their hook, tossing it into the canal for the fish to enjoy. They'd come a long way in about an hour.

Speaking of worms, as we were driving to the canal, we had quite the philosophical discussion on the use of worms; PETA would have been proud. The kids were shocked to learn that we'd be using live bait, and couldn't fathom why it was appropriate to skewer and then drowned another creature. I wasn't planning on having such a deep conversation, but I guess I'm glad that they're thoughtful kids. They worried especially that some of the worms may be babies, and wouldn't it be a tragedy if they were hurt.

In the end, the fish got a good meal from us and the kids learned that real fishing is more complex than what they'd imagined. But they also saw first hand that it's tons of fun. Mission accomplished.

I can't recommend the Zebco Dock Demon highly enough for kids. Every once in a while you get lucky with a last minute purchase, and this was definitely one of those times.

Monday, August 22, 2016

Breezy Point All The Beach Fun, A Lot Less Of The Beach Drive

With Chana, Dovid and Tzipora in from Boston, we decided the perfect Sunday adventure would be a trip to the beach. Rather than schlep hours and hours to the shore, we decided to take a chance on one of the closer bay beaches nearby. At just about an hour away, it's hard to argue with the convenience of Breezy Point. We did pause at the warning about jellyfish, as we had a recent encounter and knew that a sting would put quite a damper on the day. But still, we went for it.

Good thing we did. As soon as we arrived it became obvious that we'd be just fine. The swimming area is apparently netted off, and as a result, there was no sign of jellyfish all day.

Generally, Breezy Point delivered. The water was warm enough that the kids could enjoy swimming, we went early enough that we had plenty of space to spread out. The wind even cooperated and we got some kite flying in, too. I brought our $20 inflatable raft and the kids had a blast on it. We brought along a football, which the older kids enjoyed playing with. I took Dovid aside to do a bit of beach combing, and even found a shark tooth, though he was far more interested in the waves crashing than anything else.

Perhaps the best part of the trip was watching the transformation of the little ones. Dovid, Chana and Tzipora all started off quite timid in the water, with fears of jellyfish and getting pulled out to sea top of mind. By the end, they were romping away without a care in the world. It's exactly as it should be.

This wasn't the most pristine beach I've ever been to (that honor still goes to this one), but sure made for a fun Sunday. Everyone had a blast.

Well Duh: a more intelligent emacs file opening strategy

Last week I finally modernized my PHP emacs setup. I did so by selecting two powerful modes (php-mode and web-mode) and implementing a bit of code to easily toggle between the two. I included this comment in my blog post:

At some point, I could codify this so that files in the snippets directory, for example, always open in web-mode, whereas files loaded from under lib start off in php-mode.

When I wrote the above statement I assumed that I'd need to dust off the o'l emacs lisp manual and would need to write some code to analyze the directory of the file being opened. Turns out, I was vastly over-thinking this.

The standard way to associate a mode with a file is by using the elisp variable auto-mode-alist. This is Emacs 101 stuff, and is something I've been doing for 20+ years. In my emacs config file I had this line:

(add-to-list 'auto-mode-alist '("[.]php$" . php-mode))

Which says too open .php files in php-mode. What I'd never done, nor considered, is that you don't have to limit yourself to matching the base filename. The auto-mode-alist is matched against the entire path. To open up ‘snippet’ files in web-mode is trivial. I just put the above code in my .emacs file:

(add-to-list 'auto-mode-alist '("[.]php$" . php-mode))
(add-to-list 'auto-mode-alist 
   '("\\(pages\\|snippets\\|templates\\)/.*[.]php?$" . web-mode))

The order is key here. add-to-list pushes new items to the front of the list. So the first line adds a general rule to open up all .php files in php-mode, and the second line adds a specific rule: if the full path to the file contains the the word pages or snippets or templates, then open the file in web-mode. It's not perfect, but files matching this path convention are far more likely to be in the right mode for me.

While I'm a bonehead for not seeing this sooner, I sure do appreciate trivial solutions.

Thursday, August 18, 2016

Oy Vey, What's a Nice Jewish Boy Doing With a Yarmulke That Says That?!

Picture it: it's late March, 2016 and the primaries are in full swing. Most of the candidates are scheduled to speak at AIPAC, though the only one that seems to matter is Donald Trump. In a rare flipping of roles, after the speech it's the organization, rather than Trump himself, who feels compelled to issue an apology. Phil shows up at minyan that week with a special "gift" for me in hand: a Donald J. Trump yarmulke from the big event.

I know then that I have a single mission: find, buy and give Phil a Hillary Clinton yarmulke.

I figure it's a race against the clock: I'll need to complete this project before Trump's campaign flames out; which we're sure will be any day.

Turns out, I needn't have worried.

I suppose I could have gone the route of finding a professionally done yalmuka with Hillary's name on it. But a hand made one consisting of a nice poofy kippah with glittery lettering seemed like it would be far more appropriate.

Last week, months after I had the idea, I finally finished preparing the specimen. And by "I," I really mean We. Our Rabbi was kind enough to do the Hebrew lettering. So here it is, the action shot!

Phil, to his credit, proudly donned the kippah and promised he'd wear it to Minyan, like I do with my Trump yalmukah.

It's not exactly the lamb lying down the lion, but it sure is of peace when we can disagree and still have a great laugh.

I'm telling you, look at this craftsmanship! Rabbis just never know what task their going to be called on to do. I wonder if they take classes in fabric paint techniques?

Wednesday, August 17, 2016

Gotcha of the Day: WordPress 4.6 Crashes with a Segfault when Serving HTTPs Content

Earlier this morning, one of my clients' sites started spitting out Secure Connection Failed messages when attempting to access https pages within WordPress:

PHP files served via https worked fine, and WordPress files served via http worked fine.

Looking in Apache's error log I saw a stream of generic segfault message:

[Wed Aug 17 12:06:19 2016] [notice] child pid 9015 exit signal Segmentation fault (11)
[Wed Aug 17 12:06:22 2016] [notice] child pid 9035 exit signal Segmentation fault (11)
[Wed Aug 17 12:06:25 2016] [notice] child pid 9034 exit signal Segmentation fault (11)
[Wed Aug 17 12:06:33 2016] [notice] child pid 8883 exit signal Segmentation fault (11)
[Wed Aug 17 12:06:42 2016] [notice] child pid 9031 exit signal Segmentation fault (11)
[Wed Aug 17 12:06:52 2016] [notice] child pid 9037 exit signal Segmentation fault (11)
[Wed Aug 17 12:07:04 2016] [notice] child pid 8976 exit signal Segmentation fault (11)
[Wed Aug 17 12:07:11 2016] [notice] child pid 9033 exit signal Segmentation fault (11)
[Wed Aug 17 12:07:19 2016] [notice] child pid 9036 exit signal Segmentation fault (11)

Ugh. That's not good. Or helpful.

In response to the ticket I opened with the hosting company, the support tech suggested I follow the instructions here which outline a method for analyzing the core dump generated during a segfault.

It's been years since I busted out gdb and tried to make sense of a core dump, but I didn't have much choice.

I tried to follow the instructions in the article, but most of steps didn't work. I wasn't able to install the debug symbols, and my attempt to have the core dump written to a special directory failed. But still, setting ulimit -c unlimited was key, and once that was done, Apache was glad to drop 200+ Megabyte core files in my webroot.

Analyzing the core dump was more fruitful than I'd imagined. Here's a relevant snippet:

Core was generated by `/usr/sbin/httpd'.
Program terminated with signal 11, Segmentation fault.
#0  0xb77d0a0a in ?? () from /lib/
Missing separate debuginfos, use: debuginfo-install httpd-2.2.15-54.el6.centos.i686
(gdb) bt
#0  0xb77d0a0a in ?? () from /lib/
#1  0xb77d33d4 in ?? () from /lib/
#2  0xb77dafb4 in pcre_exec () from /lib/
#3  0xb6c658ed in php_pcre_split_impl () from /etc/httpd/modules/
#4  0xb6c65ed7 in ?? () from /etc/httpd/modules/
#5  0xb6e18659 in ?? () from /etc/httpd/modules/
#6  0xb6deee7e in execute () from /etc/httpd/modules/
#7  0xb6dbd0fb in zend_call_function () from /etc/httpd/modules/
#8  0xb6d048c6 in ?? () from /etc/httpd/modules/
#9  0xb6e18659 in ?? () from /etc/httpd/modules/
#10 0xb6deee7e in execute () from /etc/httpd/modules/
#11 0xb6dbd0fb in zend_call_function () from /etc/httpd/modules/
#12 0xb6d048c6 in ?? () from /etc/httpd/modules/
#13 0xb6e18659 in ?? () from /etc/httpd/modules/
#14 0xb6deee7e in execute () from /etc/httpd/modules/
#15 0xb6dbd0fb in zend_call_function () from /etc/httpd/modules/
#16 0xb6d048c6 in ?? () from /etc/httpd/modules/
#17 0xb6e18659 in ?? () from /etc/httpd/modules/
#18 0xb6deee7e in execute () from /etc/httpd/modules/
#19 0xb6dbd0fb in zend_call_function () from /etc/httpd/modules/

What I see there is an infinite loop within PHP code. The system is probably crashing due to running out of stack space. As noted above, this is a WordPress and https problem. Given that there's no mention of SSL in the above function calls, this lead me to believe the problem was strictly within PHP.

Then it hit me: earlier in this morning we'd upgraded the site to WordPress 4.6. Could that be the cause? I grabbed WordPress 4.5.3 and ran some diffs. Whoa, there was way too many changes to detect what might have happened in 4.6 to cause this.

I then decided to try rolling back WordPress. I followed the instructions here, which turned out to be surprisingly simple. They boiled down to:

  1. Shutting down the web server
  2. Moving the existing 4.6 wp-includes, wp-admin away
  3. Copying 4.5.3 version of wp-includes and wp-admin into the web root
  4. Copying and overwriting top level 4.5.3 *.php files over those that came with 4.6
  5. Started up the web server
  6. Logged into wp-admin
  7. WordPress detected the version change and told me that it would need to "upgrade" the database. I clicked the button to proceed
  8. The system was happy again

I have to say, I'm quite impressed that WordPress can so smoothy "upgrade" to a past version.

Most importantly, WordPress + https worked again!

I've got to say, I'm totally floored by this. It appears as though a WordPress update triggered a low level PHP bug which triggered a massive stack overflow and core dump. What are the odds?

A quick check of Twitter showed that nobody else was complaining of this issue, so my guess is that it's a weird mix of the version of PHP and Apache and such that's causing this. More than anything, this is a reminder that under the hood there's serious complexity (WordPress depends on PHP, which depends on Apache, which depends on the OS, which depends on SSL networking code, and so on) and yet most of the time, things Just Work. Remarkable.

Tuesday, August 16, 2016

Emacs + PHP - A Modern and (Far More Complete) Recipe

Recently, I had a chance to do a bit of emacs evangelism. Man, is that a soapbox I like to climb on! I hit all my favorite features from dired and dynamic abbreviation expansion to under appreciated gems like ispell. I talked about the power of having a fully programmable, self documenting editor at your fingertips. When I was done, I thought for sure I had managed to add another member to the tribe.

Then, yesterday, my possible convert came to me with a simple question: what mode do you use to edit PHP? Considering that most of the code I write is PHP, you'd think I would be ready to deliver a solid answer. Instead, I mumbled something about having a hacked bit of code I relied on, but really wouldn't recommend it for the general use. Yeah, not cool. I closed out the conversation with a promise: I'd see what the latest PHP options were and report back.

PHP is actually a fairly tricky language to build an editor for. That's because depending on the style it's written in, it can range from disciplined C like code to a gobbly gook C like code with HTML, CSS and JavaScript all mixed in. Add to that the classic emacs problem of having too many 85% solutions, and there's definitely room for frustration. Check out the emacs wiki to see what I mean. You'll find lots of promising options, but no one definitive solution.

After reading up on my choices and trying out some options, I do believe I have a new recipe for PHP + emacs success. Here goes.

Step 1: Setup Melpa

Melpa is a code repository that emacs can magically pull in packages from. Back in the day, adding packages to emacs meant downloading, untarring, and running make. Like all things emacs, the process has been both streamlined, and of course, is fully executable from within emacs. To add Melpa, you'll need to follow the instructions here. Assuming you have a modern version of emacs, this probably just means adding the following to your .emacs:

(add-to-list 'package-archives
             '("melpa" . ""))

Step 2: Install the Melpa available PHP Mode

Emacs comes with a PHP mode out of the box, but it seems as though this one (also named php-mode) is more modern. I love that the README talks about PHP 7, showing just how up to date this mode is with respect to the latest language constructs.

Installing this mode, assuming Melpa is cooperating, couldn't be easier. Just run: M-x package-install and enter php-mode.

My preference for indentation is 2 spaces and no insertion of tab characters. Two other tweaks I was after was to turn off HTML support from within PHP and enable subword-mode. All these tweaks are stored in a function and attached to the php-mode-hook. This is all standard .emacs stuff:

(defun bs-php-mode-hook ()
  (setq indent-tabs-mode nil)
  (setq c-basic-offset 2)
  (setq php-template-compatibility nil)
  (subword-mode 1))

(add-hook 'php-mode-hook 'bs-php-mode-hook)

Step 3: Install Web-Mode.el

php-mode above is ideal for code centric PHP files. But what about those pesky layout files that contain HTML, CSS and JavaScript? For that, web-mode.el looks especially promising. Installation and configuration mirrors php-mode. Here's the code I use to customize it:

(defun bs-web-mode-hook ()
  (local-set-key '[backtab] 'indent-relative)
  (setq indent-tabs-mode nil)
  (setq web-mode-markup-indent-offset 2
        web-mode-css-indent-offset 2
        web-mode-code-indent-offset 2))

(add-hook 'web-mode-hook 'bs-web-mode-hook)

Web-mode.el is quite impressive and who knows, it may actually fulfill all my PHP needs. If that's the case, I may choose to stop using the php-mode. However, for now, I like the idea of being able to switch between the two modes. Which brings me to the next step...

Step 4: Add a Quick Mode Toggle

Inspired by this tip on the EmacsWiki, I went ahead and setup a quick toggle between php-mode and web-mode. Here's that code:

(defun toggle-php-flavor-mode ()
  "Toggle mode between PHP & Web-Mode Helper modes"
  (cond ((string= mode-name "PHP")
        ((string= mode-name "Web")

(global-set-key [f5] 'toggle-php-flavor-mode)

Now I'm only an F5 keystroke away from two different editor strategies.

When I have a say in the matter, I tend to be pretty particular about which files in my source tree are pure code and which are pure markup. At some point, I could codify this so that files in the snippets directory, for example, always open in web-mode, whereas files loaded from under lib start off in php-mode. Such is the joy of having a fully programmable editor at your fingertips. You make up the rules!

Step 5: Bonus - Setup aggressive auto-completion

For bonus points, I decided to play with ac-php, a library that supports auto completion of function and class names. I followed the install here, updated my .emacs file as noted below, created the empty file named .ac-php-conf.json in my project's root and then ran M-x ac-php-remake-tags-all. Once that's done, emacs now shouts completions like crazy at me:

It's slick, I'll give you that. I think I may have to see if I can turn down the volume, a bit. Here's what my .emacs now looks like to configure php-mode: now looks like:

(defun bs-php-mode-hook ()
  (auto-complete-mode t)                 ;; «
  (require 'ac-php)                      ;; «
  (setq ac-sources  '(ac-source-php ))   ;; «
  (yas-global-mode 1)                    ;; «
  (setq indent-tabs-mode nil)
  (setq php-template-compatibility nil)
  (setq c-basic-offset 2))

Bye-bye hacked PHP code. Hello modern, feature filled, super easy to install, mega powerful code.

Update: updated the web-mode hook I'm using to make sure all code on the page, not just markup code, is indented 2 steps.

Monday, August 15, 2016

The Nike Approach to Learning about eCommerce

MailChimp, one of the well known names in e-mail marketing wanted to up its eCommerce game. Did they hold a summit, spam folks with surveys or hire an expert in the space? Nope, they took a far more straightforward approach: they opened a web store themselves.

You can check out the store here.

It's the brilliant treat the problem like an experiment approach to problem solving that's not about success or failure, but about collecting data. Does the store crash and burn? Well there you go, now you know first hand the frustrations of life in the digital marketplace.

Naturally, the team at MailChimp will need to remember that their data set is a so called N of 1, so they shouldn't read too much into any specific aspect of their experiment. But I'm willing to bet that they learn far more by trying than they would be sitting passively on the sidelines.

Bottom line: want to understand something? Just Do It!

Update: You can read about the evolution of the store here.


Related Posts with Thumbnails