dateパッケージを導入する場合
Atom Date Package https://atom.io/packages/date
Insert the current date & time.
フォーマットを指定。
Date Format: YYYY/MM/DD(DDD)
Time Format: HH24:MI:SS
2015/11/12(Thu) 19:20:21
これで概ねよいのだが、曜日が漢字で入らないのが惜しい。
Init Script を自作する場合
コマンド追加
################################################################
# 現在日時を挿入するコマンドを追加
# 参考: https://github.com/dannyfritz/atom-date
# 参考: https://github.com/JerrySievert/date-utils
daysKanji = ['日', '月', '火', '水', '木', '金', '土']
pad = (str, length) ->
str = String(str)
str = '0' + str while(str.length < length)
str
dateOrTime = (kind) ->
now = new Date()
yyyy = now.getYear() + 1900
mm = pad(now.getMonth() + 1, 2)
dd = pad(now.getDate(), 2)
ddd = daysKanji[now.getDay()]
hh24 = pad(now.getHours(), 2)
mi = pad(now.getMinutes(), 2)
ss = pad(now.getSeconds(), 2)
if kind == 1
"#{yyyy}/#{mm}/#{dd}(#{ddd})"
else if kind == 2
"#{hh24}:#{mi}:#{ss}"
else
"#{yyyy}/#{mm}/#{dd}(#{ddd}) #{hh24}:#{mi}:#{ss}"
insertText = (str) ->
return unless editor = atom.workspace.getActiveTextEditor()
selection = editor.getLastSelection()
selection.insertText(str)
atom.commands.add 'atom-text-editor', 'torout-date:date', ->
insertText(dateOrTime(1))
atom.commands.add 'atom-text-editor', 'torout-date:time', ->
insertText(dateOrTime(2))
atom.commands.add 'atom-text-editor', 'torout-date:datetime', ->
insertText(dateOrTime(0))
################################################################
これでdateパッケージと同じようなコマンドが追加される。
曜日は漢字で入る。
2015/11/12(木) 20:59:29
date-utilsは参考に
当初、dateパッケージのソースコードを写していったところ、require('date-utils')
が not found とエラーになった。
date-utils
とはnpmのパッケージである。
https://www.npmjs.com/package/date-utils
Atomパッケージならば、依存関係を宣言しておけば良きに計らってくれるようだ。
{
"dependencies": {
"date-utils": "^1.2.16"
}
}
Init Script では依存関係を解決しないので、手動でcd ~/.atom
してnpm install ...
して、~/.atom/node_modules/
にパッケージを入れるらしい。
手作業が増えるの面倒だし、そもそもnpmコマンドが入ってないし、どうせ曜日の処理を書かなければならないし、ということでdate-utilsは使用せずに参考にした。
キー割り当て
'atom-text-editor':
'alt-=': 'torout-date:date'
'alt-;': 'torout-date:time'
JIS配列キーボードでAlt + ;
、Alt + :
で動作するように書いた。AtomはUS配列で解釈する。
Keybinding Resolver で確認しながらやるとよい。
サクラエディタのキー割り当てを踏襲した。
http://sakura-editor.sourceforge.net/htmlhelp/HLP000107.html
キー
機能名Alt+;
日付挿入Alt+:
時刻挿入
余談
ところで、サクラエディタの場合、曜日は漢字で入ってたよな、と思ったら。
http://sakura-editor.sourceforge.net/htmlhelp/HLP000082.html
<日付書式>
ddd 曜日。ロケールの LOCALE_SABBREVDAYNAME が使われます。
dddd 曜日。ロケールの LOCALE_SDAYNAME が使われます。
Windowsのライブラリで定義されているらしい。