blog/content/post/2008/12/07/2008-12-07-00001088.md

161 lines
10 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 『やさしいEmacsLisp講座』を購入
author: kazu634
date: 2008-12-07
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4427;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Emacs
- Lisp
---
<div class="section">
<p>
Emacs-Lispの勉強用に購入しました。外部プロセスを起動する方法をペラっとめくって、ちょっと書いてみたら、革命的に便利になってしまった…ちょっと前にisbn13からisbn10つまりasinを求めるスクリプトを書きましたが、それをEmacs上で実行できるようにしてみました。
</p>
<p>
これで本を片手に、日記を書くのが便利になった。
</p>
<h4>
.emacsとかに表記するの
</h4>
<pre class="syntax-highlight">
<span class="synSpecial">(</span><span class="synStatement">defun</span> lookup-asin <span class="synSpecial">(</span>isbn13<span class="synSpecial">)</span>
<span class="synSpecial">(</span>interactive <span class="synConstant">&#34;sISBN13: \n&#34;</span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>call-process <span class="synConstant">&#34;isbn&#34;</span> <span class="synStatement">nil</span> <span class="synStatement">t</span> <span class="synStatement">t</span> isbn13<span class="synSpecial">))</span>
</pre>
<p>
isbn13の値をミニバッファーで尋ね、isbn10をカレントバッファーに返す。M-x lookup-asinしてみよう。
</p>
<h4>
Perlスクリプト
</h4>
<pre class="syntax-highlight">
<span class="synPreProc">#!/usr/bin/perl</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># ==================</span>
<span class="synComment"># === 変数の宣言 ===</span>
<span class="synComment"># ==================</span>
<span class="synComment"># isbn10を格納するための配列</span>
<span class="synStatement">my</span> <span class="synIdentifier">@isbn10</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$isbn13</span>;
<span class="synComment"># ====================</span>
<span class="synComment"># === 起動時の処理 ===</span>
<span class="synComment"># ====================</span>
<span class="synComment"># スクリプトに引数が設定されてなければ終了</span>
<span class="synStatement">exit</span> <span class="synStatement">if</span> (<span class="synIdentifier">@ARGV</span> == <span class="synConstant"></span>);
<span class="synComment"># スクリプトの引数を代入する</span>
<span class="synStatement">my</span> <span class="synIdentifier">$chk</span> = <span class="synIdentifier">&#38;chk_arg</span>(<span class="synIdentifier">$ARGV</span>[<span class="synConstant"></span>]);
<span class="synComment"># 引数のチェック</span>
<span class="synStatement">exit</span> <span class="synStatement">if</span> (<span class="synIdentifier">$chk</span> == <span class="synConstant">-1</span>);
<span class="synIdentifier">$isbn13</span> = <span class="synIdentifier">$ARGV</span>[<span class="synConstant"></span>];
<span class="synComment"># ================</span>
<span class="synComment"># === 処理開始 ===</span>
<span class="synComment"># ================</span>
<span class="synComment"># 数字を一つずつ配列に格納</span>
<span class="synStatement">my</span> <span class="synIdentifier">@temp</span> = <span class="synStatement">split</span>(<span class="synStatement">//</span>, <span class="synIdentifier">$isbn13</span>);
<span class="synComment"># check digit以外の部分が完成</span>
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant">3</span>; <span class="synIdentifier">$i</span> &#60; <span class="synConstant">12</span>; <span class="synIdentifier">$i</span>++) {
<span class="synStatement">push</span>(<span class="synIdentifier">@isbn10</span>, <span class="synIdentifier">$temp</span>[<span class="synIdentifier">$i</span>])
}
<span class="synComment"># check digitの計算</span>
<span class="synStatement">my</span> <span class="synIdentifier">$c_digit</span> = <span class="synIdentifier">&#38;calc_c_digit</span>(<span class="synIdentifier">@isbn10</span>);
<span class="synComment"># check digitを配列に格納</span>
<span class="synStatement">push</span>(<span class="synIdentifier">@isbn10</span>, <span class="synIdentifier">$c_digit</span>);
<span class="synComment"># isbn10を表示</span>
<span class="synStatement">print</span>(<span class="synIdentifier">@isbn10</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
<span class="synComment"># ================</span>
<span class="synComment"># === 処理終了 ===</span>
<span class="synComment"># ================</span>
<span class="synStatement">exit</span>;
<span class="synComment"># ==================================</span>
<span class="synComment"># ========== サブルーチン ==========</span>
<span class="synComment"># ==================================</span>
<span class="synComment"># ================================</span>
<span class="synComment"># === チェックディジットの計算 ===</span>
<span class="synComment"># ================================</span>
<span class="synComment"># 戻り値: チェックディジット</span>
<span class="synStatement">sub</span><span class="synIdentifier"> calc_c_digit </span>{
<span class="synComment"># check digitを計算するためのisbn</span>
<span class="synStatement">my</span> <span class="synIdentifier">@tmp_isbn9</span> = <span class="synIdentifier">@_</span>;
<span class="synComment"># 計算用</span>
<span class="synStatement">my</span> <span class="synIdentifier">$temp</span>;
<span class="synStatement">for</span> (<span class="synStatement">my</span> <span class="synIdentifier">$i</span> = <span class="synConstant"></span>; <span class="synIdentifier">$i</span> &#60; <span class="synIdentifier">@tmp_isbn9</span>; <span class="synIdentifier">$i</span>++) {
<span class="synIdentifier">$temp</span> += <span class="synIdentifier">$tmp_isbn9</span>[<span class="synIdentifier">$i</span>] * (<span class="synConstant">10</span> - <span class="synIdentifier">$i</span>);
}
<span class="synIdentifier">$temp</span> = <span class="synStatement">int</span>(<span class="synIdentifier">$temp</span> % <span class="synConstant">11</span>);
<span class="synIdentifier">$temp</span> = <span class="synConstant">11</span> - <span class="synIdentifier">$temp</span>;
<span class="synStatement">if</span> (<span class="synIdentifier">$temp</span> == <span class="synConstant">10</span>) {
<span class="synStatement">return</span> (<span class="synConstant">&#34;X&#34;</span>);
} <span class="synStatement">else</span> {
<span class="synStatement">return</span> <span class="synIdentifier">$temp</span>;
}
}
<span class="synComment"># ================================</span>
<span class="synComment"># === スクリプトの引数チェック ===</span>
<span class="synComment"># ================================</span>
<span class="synComment"># 0 : 正常な引数</span>
<span class="synComment"># -1 : 異常な引数</span>
<span class="synStatement">sub</span><span class="synIdentifier"> chk_arg </span>{
<span class="synStatement">my</span> <span class="synIdentifier">@tmp</span> = <span class="synIdentifier">@_</span>;
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$i</span> (<span class="synIdentifier">@tmp</span>) {
<span class="synStatement">return</span> (<span class="synConstant">-1</span>) <span class="synStatement">if</span> (<span class="synIdentifier">$i</span> =~<span class="synStatement"> /</span><span class="synSpecial">[a-zA-Z]</span><span class="synStatement">/</span>);
}
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
<p>
このスクリプトをisbnとして保存し、パスの通ったところに保存する。
</p>
<h4>
まとめ
</h4>
<p>
外部プロセスを起動させられるようになると、Emacsの拡張性がさらに広がるなぁ。
</p>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4906391702/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4906391702/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/51C4N4SHT7L._SL160_.jpg" class="hatena-asin-detail-image" alt="やさしいEmacsLisp講座" title="やさしいEmacsLisp講座" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4906391702/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4906391702/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'やさしいEmacsLisp講座');">やさしいEmacsLisp講座</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/%B9%AD%C0%A5%CD%BA%C6%F3" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%B9%AD%C0%A5%CD%BA%C6%F3', '広瀬雄二');" class="keyword">広瀬雄二</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%A5%AB%A5%C3%A5%C8%A5%B7%A5%B9%A5%C6%A5%E0" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%A5%AB%A5%C3%A5%C8%A5%B7%A5%B9%A5%C6%A5%E0', 'カットシステム');" class="keyword">カットシステム</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 1999/01
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 単行本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 4人 <span class="hatena-asin-detail-label">クリック</span>: 145回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4906391702" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4906391702', 'この商品を含むブログ (30件) を見る');" target="_blank">この商品を含むブログ (30件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>