2011年12月31日土曜日

C-a で「行頭」と「非空白文字の先頭」をトグル的に移動

Emacs で「行頭」への移動は C-a で「非空白文字の先頭」への移動は M-m というキーバインドが 割り当てられているが M-m がなかなかなじまない。
どこかのブログで C-a で「行頭」と「非空白文字の先頭」をトグル的に移動させるというアイデアを見た。
なるほど。
アイデアを拝借して覚えたての Emacs Lisp で書いてみる。
(defun move-beginning-alt()
  (interactive)
  (if (bolp)  
      (back-to-indentation)
    (beginning-of-line)))
(define-key global-map "\C-a" 'move-beginning-alt)

参考:やさしいEmacs‐Lisp講座








ブログ作成をちょっと便利に(very personal)

<body> - </body>間をコピー

僕はブログの原稿を Emacs の org-mode で書いています。
それを html ファイルに変換して、その html ファイルから必要な部分をコピーしてブログに貼り付けています。

以下は html ファイル中の必要な部分をキーボードショートカットでコピーする個人的な改善。
前述の 'paste-to-osx' を利用して <body> と </body> の間をコピーします。(参考:ターミナル Emacs - GUIアプリ間のコピペ
;; バッファ内の2つの文字列間をクリップボードにコピー
(defun paste-to-osx-between(start-str end-str)
   (interactive "scopy between first:\nsand second:")
   (save-excursion
              (goto-char (point-min))   
              (let (copystart)
                 (if (search-forward start-str nil t)
                             (setq copystart (point))
                         (error "Cannot copy. Start string \"%s\" not found" start-str))
                 (goto-char (point-max))
                 (if (search-backward end-str nil t)
                             (backward-char)
                         (error "Cannot copy. End string \"%s\" not found" end-str))
                 (paste-to-osx(buffer-substring copystart (point))))
              (message "copy done.")))

(defun copy-between-body()
   (interactive)
   (paste-to-osx-between "<body>" "</body>"))
(define-key global-map "\C-cc" 'copy-between-body)
C-c c で <body> と </body> の間のテキストをクリップボードにコピーします。
M-x paste-to-osx-between で任意の2つの文字列を指定も可能です。他の用途があればどうぞ。


参考:やさしいEmacs‐Lisp講座








2011年12月25日日曜日

ターミナル Emacs - GUIアプリ間のコピペ

参考: http://hakurei-shain.blogspot.com/2010/05/mac.html
うれしい。
blog 作成は、Emacs の org-mode で記事を書いて html 出力して、それをコピーして blog に貼りつけているのですごく便利になりました。
ただ、Emacs 内のペーストも command + V となってしまう。
以下ソースの引用。
; http://blog.lathi.net/articles/2007/11/07/sharing-the-mac-clipboard-with-emacs
(defun copy-from-osx ()
 (shell-command-to-string "pbpaste"))

(defun paste-to-osx (text &optional push)
 (let ((process-connection-type nil))
     (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
       (process-send-string proc text)
       (process-send-eof proc))))

(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)

 

初めて読む Emacs Lisp

このコードが読みたくて Emacs Lisp 始めました。
(shell-command-to-string "pbpaste"))

;; シェルコマンドとして pbpaste を実行する。
(let ((process-connection-type nil))
   (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))

;; (process-connection-type nil)はこれから作る非同期サブプロセスとの通信はパイプであることを示す。
;; start-process はプログラム pbcopy(2回目のpbcopy) を非同期サブプロセスとして実行する。
;; サブプロセスの名前は"pbcopy"(1つ目のpbcopy)でバッファ"*Messages*"と対応付けられている。
;; pbcoyp は OSX のコマンドでコマンドラインからクリップボードへコピーする。
;; (let ((proc (start-process ... で変数 proc に start-process が返すプロセスオブジェクトが代入される。

(process-send-string proc text)
(process-send-eof proc)

;; proc すなわち pbcopy に文字列 text を送る。
;; process-send-eof は proc に eof を送る処理。(バッファを flush するため(?))
(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)

;; 変数 interprogram-cut-function は kill した文字列をwindow systemで使用する方法を指定する。
;; 引数として関数名で指定する。kill-new 関数や kill-append 関数が呼ばれたときにこの指定された関数も呼ばれる。
;; 変数 interprogram-paste-function は他のプログラムから kill した文字列を貼り付ける方法を指定する。


追記 (2012/1/15)
Emacs 内で kill して yank する時にも C + y ではなくて Command + V しないといけなかったのが、若干不満でした。試行錯誤しているうちに上記の "copy-from-osx" の定義は不要ということがわかりました。現在では、ターミナルにペースト機能があるので特に定義しなくてもペーストできます。( terminal でも iTerm でもできました。)
なので、
(defun copy-from-osx ()
    (shell-command-to-string "pbpaste"))
と、
(setq interprogram-paste-function 'copy-from-osx)
は削除します。
すると Emacs でコピーしたものを Emacs 内でペーストするときには C + y で出来ます。 (GUI アプリへのペーストは Command + V で貼付けできます。Emacs へのペーストも Command + V でも出来ます。)


参考:やさしいEmacs‐Lisp講座

参考:36.4 非同期プロセスの作成
参考:32.8.5 Low-Level Kill Ring
参考:OSX固有のコマンド






2011年12月23日金曜日

emacsclient with Emacs 23

Mac で Emacs の org-mode を使いたかったので、 org-mode が標準で備わっている Emacs 23 を インストールして使い始めた。(Mac に元々入っているのはEmacs 22。) すると、下記のようなエラーが出てemacsclient がうまく起動しなくなった。
emacsclient: can't find socket; have you started the server?     
To start the server in Emacs, type "M-x server-start".     
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)

原因は emaceclient のバージョンが 22 になっていたため。 emacsclient と Emacs はバージョンを合わせる必要がある。
   
$ locate emacsclient
/opt/local/bin/emacsclient                     # <- コレか
/opt/local/share/man/man1/emacsclient.1.gz
/usr/bin/emacsclient                           # <- コレのどちらか
/usr/share/emacs/22.1/etc/emacsclient.1
/usr/share/man/man1/emacsclient.1.gz

$ /usr/bin/emacsclient --version
emacsclient 22.1

$ /opt/local/bin/emacsclient --version
emacsclient 23.3

$ which emacsclient
/usr/bin/emacsclient     # ver. 22.1 の方が呼ばれていた

emacsclient 23.3 の方のパスから呼び出すようにしてOK.

参考: http://www.emacswiki.org/emacs/EmacsClient#toc41
参考: Emacsテクニックバイブル ~作業効率をカイゼンする200の技~





コマンド less のよく使う操作コマンド

よく使うけど使いこなせていないコマンドをこの際覚えておく。
# ヘルプ  
 h: コマンドのまとめ表示

# 検索
 /pattern: patternを検索(patternは正規表現)
 n:        検索結果を順に表示
 N:        検索結果を逆順に表示
 ESC-u:    検索結果のハイライトを消す/つける(トグル)

# 移動
 g: ファイルの先頭に移動
 G: ファイルの末尾に移動

 SPACE: 1画面進む
 d:   半画面進む
 b:   1画面戻る
 u:   半画面戻る
 




2011年12月14日水曜日

コマンド locate

1 コマンド locate –高速ファイル検索

  • ファイル名データベースからパターンに合うものを検索

2 使い方

  • $ locate [オプション] pattern
  • ubuntu の man ではオプションの詳細説明が無いが、–help オプションで表示される。
    $ locate --help
    

3 よく使うオプション

  • $ locate -i pattern   #  大文字/小文字を無視
    

4 データベースの更新が必要。

  • 更新コマンドは、
    $ sudo updatedb                             # ubuntu
    $ sudo /usr/libexec/locate.updatedb         # mac or FreeBSD