Edit with emacs という Google Chrome アドオンを文字化けせずに使う

で今これを書いてみようと思ったのですが。
文字コードのせいか化けてうまくかけません。

このはてなのエディタのページは、EUC-JP なので、
emacs 側でも同じ文字コードに合わせてやればと思ったのですが。

matz tw 2010-06-15 03:27
Edit with Emacs という拡張を知った.
ChromeのテキストエリアをEmacsで編集できればいいなと思っていたんだよね.
文字化けするので TextAid's Edit Server というやつを使うとよい

perlスクリプトでサーバをつくるといいらしい。
TextAid's Edit Server
http://opencoder.net/edit-server

#!/usr/bin/perl
# A simple web server that just listens for textarea filter requests
# and runs an editor to manipulate the text.  Is intended to be
# used with the TextAid extention for Chrome.
#
# NOTE:  If you use this on a shared system, you should configure TextAid
# with a username & password and make sure that the saved authorization
# config file stays secret!

use strict;
use warnings;
use threads;
use Socket;
use IO::Select;
use File::Temp;

# If you don't want to require authentication, set $REQUIRE_AUTH to 0.
# When it is set to 1, the first authenticated request that is received
# will be saved to the $SAVE_AUTH_FILE file.  All subsequent requests
# are compared to that value.  To change your password, just remove the
# file and make a new edit-server request using the new auth info.
our $REQUIRE_AUTH = 1;
our $SAVE_AUTH_FILE = "$ENV{HOME}/.edit-server-auth";

# Only accept requests from something that claims to be a chrome-extension.
# Set this to 0 if you want other things to be able to use this script.
our $REQUIRE_CHROME_EXTENSION = 1;

# Configures the port we listen on and if we allow only localhost requests.
our $PORT = 8888;
our $LOCALHOST_ONLY = 1;

# Configure the program that you want to run to handle the requests.
our $EDITOR_CMD = '/usr/bin/rgvim -f "%s"';


# The settings to configure the temp dir and how soon old files are removed.
our $TMPDIR = '/tmp';
our $TMPTEMPLATE = 'edit-server-XXXXXX';
our $TMPSUFFIX = '.txt';
our $CLEAN_AFTER_HOURS = 4;

umask 0077; # Disables all "group" and "other" perms when saving files.
$|  = 1;

local *S;
socket(S, PF_INET, SOCK_STREAM , getprotobyname('tcp')) or die "couldn't open socket: $!\n";
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, 1);
if ($LOCALHOST_ONLY) {
    bind(S, sockaddr_in($PORT, INADDR_LOOPBACK));
} else {
    bind(S, sockaddr_in($PORT, INADDR_ANY));
}
listen(S, 5) or die "listen failed: $!\n";

my $sel = IO::Select->new();
$sel->add(*S);

while (1) {
    my @con = $sel->can_read();
    foreach my $con (@con) {
	my $fh;
	my $remote = accept($fh, $con);
	my($port, $iaddr) = sockaddr_in($remote);
	my $addr = inet_ntoa($iaddr);

	my $t = threads->create(\&do_edit, $fh);
	$t->detach();
    }
}

exit;

# Read the text from the content body, edit it, and write it back as our output.
sub do_edit
{
    my($fh) = @_;
    binmode $fh;

    local $_ = <$fh>;
    my($method, $path, $ver) = /^(GET|HEAD|POST)\s+(.*?)\s+(HTTP\S+)/;
    unless (defined $ver) {
	http_header($fh, 500, 'Invalid request.');
	close $fh;
	return;
    }
    if ($method ne 'POST') {
	http_header($fh, 200, 'OK', 'Server is up and running.  To use it, issue a POST request with the file to edit as the content body.');
	close $fh;
	return;
    }

    my %header;

    while (<$fh>) {
	s/\r?\n$//;
	last if $_ eq '';

	my($name, $value) = /^(.*?): +(.*)/;
	$header{lc($name)} = $value;
    }

    if ($REQUIRE_AUTH) {
	my $authorized;
	my $auth = $header{authorization}; # e.g. "Basic 01234567890ABCDEF=="
	if (defined $auth) {
	    my $line;
	    if (open AUTH, '<', $SAVE_AUTH_FILE) {
		chomp($line = <AUTH>);
		close AUTH;
	    } elsif ($!{ENOENT} && open AUTH, '>', $SAVE_AUTH_FILE) {
		# The first request w/o an auth file saves the auth info.
		print AUTH $auth, "\n";
		close AUTH;
		$line = $auth;
	    } else {
		http_header($fh, 501, 'Internal failure -- auth-file failed.');
		close $fh;
		return;
	    }
	    if ($line eq $auth) {
		$authorized = 1;
	    }
	}
	unless ($authorized) {
	    http_header($fh, 401, 'Unauthorized!');
	    close $fh;
	    return;
	}
    }

    if ($REQUIRE_CHROME_EXTENSION) {
	my $origin = $header{origin};
	unless (defined $origin && $origin =~ /^chrome-extension:/) {
	    http_header($fh, 401, 'Unauthorized.');
	    close $fh;
	    return;
	}
    }

    my $len = $header{'content-length'};
    unless (defined $len && $len =~ /^\d+$/) {
	http_header($fh, 500, 'Invalid request -- no content-length.');
	close $fh;
	return;
    }

    my $got = read($fh, $_, $len);
    if ($got != $len) {
	http_header($fh, 500, 'Invalid request -- wrong content-length.');
	close $fh;
	return;
    }

    my $tmp = new File::Temp(
	TEMPLATE => $TMPTEMPLATE,
	DIR => $TMPDIR,
	SUFFIX => $TMPSUFFIX,
	UNLINK => 0,
    );
    my $name = $tmp->filename;

    print $tmp $_;
    close $tmp;

    my $cmd = sprintf($EDITOR_CMD, $name);
    system $cmd;

    unless (open FILE, '<', $name) {
	http_header($fh, 500, "Unable to re-open $name: $!");
	close $fh;
	return;
    }

    http_header($fh, 200, 'OK', '');
    print $fh <FILE>;

    close FILE;
    close $fh;

    # Clean-up old tmp files that have been around for a few hours.
    if (opendir(DP, $TMPDIR)) {
	(my $match = quotemeta($TMPTEMPLATE . $TMPSUFFIX)) =~ s/(.*)(X+)/ $1 . ('.' x length($2)) /e;
	foreach my $fn (grep /^$match$/o, readdir DP) {
	    $fn = "$TMPDIR/$fn";
	    if (-M $fn > $CLEAN_AFTER_HOURS/24) {
		unlink $fn;
	    }
	}
	closedir DP;
    }
}

sub http_header
{
    my $fh = shift;
    my $status = shift;
    my $status_txt = shift;
    @_ = $status_txt unless @_;
    print $fh "HTTP/1.0 $status $status_txt\r\n",
	      "Server: edit-server\r\n",
	      "Content-Type: text/plain\r\n",
	      "\r\n", @_;
}

emacs を利用するのでコメントイン。

our $EDITOR_CMD = '/usr/bin/emacsclient -c "%s"';

認証とか不要として

our $REQUIRE_AUTH = 0;

としても、edit-server.plを起動したコンソールが
怒っている。

To start the server in Emacs, type "M-x server-start".
/usr/bin/emacsclient: No socket or alternate editor.  Please use:

	--socket-name
	--server-file      (or environment variable EMACS_SERVER_FILE)
	--alternate-editor (or environment variable ALTERNATE_EDITOR)
Waiting for Emacs...

emacsサーバか起動してないので、
M-x server-start
で、とりあえずは、書き込めたりするが文字が化ける。

このテキストはEmacs上ではUTF-8で認識されてるのだけども、
HTMLヘッダをみると、

<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">

となっているのだけれどもうまく書き込めてる。

emacs上ではUTF−8で編集するのがいいのだろうか、
また.emacsに追記した

(edit-server-start)

が効いてないっぽい。

まとめ

結局、以下でいける。

1. edit with emacs アドオンを Chromeにインストールする。
2. PC上で、2行書き換え後 edit-server.pl を稼働させる。
http://opencoder.net/edit-server

our $EDITOR_CMD = '/usr/bin/emacsclient -c "%s"';
・・・
our $REQUIRE_AUTH = 0;

3. .emacsに以下を追記する

;; edit with emacs from Google Chrome
(require 'edit-server)
(if (locate-library "server")
    (progn
      (load-library "server")
      (server-start)
      ))

4. emacs を起動する。
5. WEBページのテキストエリアからeditボタンなどで emacsクライアント起動。
6. 入力するテキストは Ctl+RET-f で UTF-8 で編集する。