November 2006 Archives
Mon, 20 Nov 2006 @ 00:23:09
rapid window toggle in emacs
Every so often I encounter an
Emacs “quick reference” that lists the functionality of command
other-window (C-x o)
as toggling between two windows. In fact, this is incorrect as
Emacs only implements cyclical window navigation. There is no single
shortcut to toggle between two windows; only to cycle through a list
of windows. If there are more than two windows in an Emacs session (I
typically use four) then there is no simple reflexive key stroke
by which the user may
return to the previously visited window. To implement real window
toggling, identical to the other command in
GNU Screen, I use the following code:
;; Redefines "C-x o" to behave like 'other-window' while also ;; remembering the previously selected window. (defun other-window-history (&optional x) (interactive "P") (setq toggle-window-prev (selected-window)) (if (equal x nil) (other-window '1) (other-window x))) (global-set-key (kbd "C-x o") 'other-window-history) ;; By calling other-window-toggle, two windows can be rapidly switched ;; between with a single key sequence, definied below. (defun other-window-toggle (&optional x) (interactive "P") (if (not (boundp 'toggle-window-prev)) (if (equal x nil) (other-window-history 1) (other-window-history x)) (let ( (y toggle-window-prev) ) (setq toggle-window-prev (selected-window)) (if (equal toggle-window-prev y) (if (equal x nil) (other-window-history 1) (other-window-history x)) (select-window y))))) (global-set-key (kbd "C-x C-o") 'other-window-toggle) (global-set-key (kbd "C-c C-o") 'other-window-toggle)
The source also available at: conf-windows.el.
Other elisp code snippets available at: …/elisp-snippets/.
Sat, 18 Nov 2006 @ 02:12:46
submitting MSIE to xhtml+xml
Being somewhat of a standards nut, I have been frustrated by the fact
that Internet Explorer
6 and 7 are the only browsers to incorrectly render my documents and
gracelessly handle the application/xhtml+xml
MIME type—despite most other
softwares having adequately supported them for years.
On my server it is necessary to perform the following tricks to
maintain standards compliance while also supporting IE.
First, every document contains a pair of conditional comments to instruct Internet Explorer to load non-standard style sheets. This “invalid” styling provides compensating formating to “correct” IE's rendering blemishes. As these issues are similar in quantity, yet different, between IE versions 6 and 7, a style sheet is needed for each version.
<!--[if lte IE 6]><style type="text/css" media="all"> @import "/css/invalid_ie6.css"; </style><![endif]--> <!--[if gte IE 7]><style type="text/css" media="all"> @import "/css/invalid_ie7.css"; </style><![endif]-->
Second, I duplicate each static XHTML 1.1 page and rewrite its DOCTYPE as XHTML 1.0. On my site I use the extension .html for files have that have XHTML 1.1 doctypes and .htm for files that have older doctypes.
sed 's!DTD XHTML 1.1!DTD XHTML 1.0 Strict!'$'\n'\ 's!TR/xhtml11/DTD/xhtml11.dtd!xhtml1/DTD/xhtml1-strict.dtd!'$'\n'\ 's![<][?]xml .*[?][>]!!' \ file.html > file.htm
Third, to force IE to use the .htm documents, I configure Apache's mod_rewrite to redirect IE browsers to the equivalent .htm document of each page. This rewriting applies to all static content, which is every page not found within the Blog at URI /blog/.
# set file extensions I like to the MIME types appropriate to their
# doctype content (as per the W3C specs), XHTML 1.0 serving as
# traditional HTML and XHTML 1.1 serving as XML.
AddType text/html .htm
AddType application/xhtml+xml .html
# Force all versions of MSIE, and only MSIE, to use the web pages with
# older doctypes. No other popular browsers seem to require this.
Options +FollowSymlinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteCond %{HTTP_USER_AGENT} MSIE\ [123456789]
RewriteCond %{REQUEST_URI} !^.{0,30}/blog/{0,70}$
RewriteRule ^(.{0,100})/$ $1/index.htm [R]
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteCond %{HTTP_USER_AGENT} MSIE\ [123456789]
RewriteRule ^(.{1,100})\.html$ $1.htm [R]
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteCond %{HTTP_USER_AGENT} MSIE\ [123456789]
RewriteRule ^$ /index.htm [R]
Fourth, I perform a combination of the above DOCTYPE and MIME adjustments with the following PHP code. This only applies to the PHP-enabled pages with extension .phtml—mainly the Blog pages which already use PHP for the commenting system.
<?php $serv = isset($_SERVER["HTTP_ACCEPT"]) and isset($_SERVER["HTTP_USER_AGENT"]); $cond0 = strlen( stristr($_SERVER["HTTP_USER_AGENT"], "MSIE ") ) > 0; $cond1 = (! stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")); if ( $serv and $cond0 and $cond1 ) { header("Content-type: text/html; charset=UTF-8"); echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' . '"http://www.w3.org/xhtml1/DTD/xhtml1-strict.dtd">'; } else { header("Content-type: application/xhtml+xml"); echo '<?xml version="1.0" encoding="utf-8"?>'; echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"' . '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'; } ?>
It is quite unfortunate that all of this is needed just to support Internet Explorer. Were I only concerned with standards-compliant browsers such as Firefox, Safari, and Opera, I could have skipped the above and simply added this one line in Apache's configuration:
AddType application/xhtml+xml .html
Mon, 06 Nov 2006 @ 23:21:06
emacs -nw smart punctuation
Not many days ago I asked a few Emacs developers on #emacs how I might easily type characters from the General Punctuation Unicode block within console Emacs. To my surprise, the best answers I received were to copy-and-paste from a GTK application, or insert the characters by ISO 14755/UCS hex code entry.
Fortunately, xmlunicode provides a variety of Unicode entry methods that do not require numeric memorizations. With some quick coding, I bound the interactive input methods to familiar key strokes—similar to those used by Unibyte Editing. A minor mode is used to toggle automatic “word processing”-style punctuation, including smart quotes, ellipses, and em/en dashes.
My binding code in all of its simplicity:
(autoload 'unicode-character-insert "xmlunicode" "" t) (autoload 'unicode-character-shortcut-insert "xmlunicode" "" t) (autoload 'iso8879-character-insert "xmlunicode" "" t) (global-set-key (kbd "C-x 9") 'unicode-character-shortcut-insert) (global-set-key (kbd "C-x 7") 'iso8879-character-insert) (global-set-key (kbd "C-c 9") 'unicode-character-insert) (global-set-key (kbd "C-c 7") 'smart-chars-mode) (define-minor-mode smart-chars-mode "Toggle SmartChars unicode punctuation mode." nil " SmartChars" '(("\'" . unicode-smart-single-quote) ("\"" . unicode-smart-double-quote) ("\;" . unicode-smart-semicolon) ("\-" . unicode-smart-hyphen) ("\." . unicode-smart-period)) (if (not (commandp 'unicode-smart-period)) (load "xmlunicode")))
The source also available at: smartchars.el.
Other elisp code snippets available at: …/elisp-snippets/.
