Slip Ahead Logging

It's not your fault at all.

tmux に独自のコマンドを追加する

tmux に独自のコマンドを登録する手順を調べた.

一番良いのは本家へのコミットログを参考にすることだが,文章化しておくのもよかろうということで,私の行なった手順を以下にメモしておく.

tmux のソースコードは非常にまともなつくりをしているので,苦労することもほとんどないだろう.

cmd-show-current-session.c (追加したいコマンドの内容を書くファイル)

struct cmd_entry がコマンドの実体となる.以下のコメントを参照に作成.

#include "tmux.h"

/*
 * Show current session name.
 */

int cmd_show_current_session_exec(struct cmd *, struct cmd_ctx *);

const struct cmd_entry cmd_show_current_session_entry = {
    "show-current-session",       /* name */
    NULL,                         /* alias */
    "",                           /* args_emplate */
    0,                            /* args_lower */
    0,                            /* args_upper */
    "",                           /* usage */
    0,                            /* flags */
    NULL,                         /* key_binding */
    NULL,                         /* check */
    cmd_show_current_session_exec /* exec */
};

int
cmd_show_current_session_exec(struct cmd *self, struct cmd_ctx *ctx)
{
    struct session *s;

    s = cmd_current_client(ctx)->session;
    if (s) {
        ctx->print(ctx, "Session: %s", s->name);
    } else {
        ctx->print(ctx, "Oops. Session not found.");
    }

    return (0);
}

Makefile.am

追加したいコマンドのファイルをコンパイル対象とするため Makefile.am を書き換える.

--- INDEX:/Makefile.am
+++ WORKDIR:/Makefile.am
@@ -125,6 +125,7 @@
        cmd-set-environment.c \
        cmd-set-option.c \
        cmd-show-buffer.c \
+       cmd-show-current-session.c \
        cmd-show-environment.c \
        cmd-show-messages.c \
        cmd-show-options.c \

tmux.h

次に,追加したいコマンドの実体 (cmd_entry cmd_show_current_session_entry) をグローバルに参照できるように.

--- INDEX:/tmux.h
+++ WORKDIR:/tmux.h
@@ -1595,6 +1595,7 @@
 extern const struct cmd_entry cmd_set_environment_entry;
 extern const struct cmd_entry cmd_set_option_entry;
 extern const struct cmd_entry cmd_set_window_option_entry;
 extern const struct cmd_entry cmd_show_buffer_entry;
+extern const struct cmd_entry cmd_show_current_session_entry;
 extern const struct cmd_entry cmd_show_environment_entry;
 extern const struct cmd_entry cmd_show_messages_entry;
 extern const struct cmd_entry cmd_show_options_entry;

cmd.c

最後に,追加したいコマンドを cmd.c 内の cmd_table に追加することで,コマンドを登録する.

--- INDEX:/cmd.c
+++ WORKDIR:/cmd.c
@@ -95,6 +95,7 @@
const struct cmd_entry *cmd_table[] = {
 	&cmd_set_option_entry,
 	&cmd_set_window_option_entry,
 	&cmd_show_buffer_entry,
+       &cmd_show_current_session_entry,
 	&cmd_show_environment_entry,
 	&cmd_show_messages_entry,
 	&cmd_show_options_entry,

ビルド

ビルドに際しては autotools を使って Makefile.in や configure.ac を再生成してやる必要がある.といっても手順さえ知っていれば簡単で,以下のようにコマンドを打ち込むだけでよい.

aclocal
autoheader
automake -a -c
autoconf