add-hook
add-hook 関数を使うことで定義済み hook 変数に関数を追加できる
(add-hook 'text-mode-hook 'turn-on-auto-fill)text-mode の場合は disable truncate にする
(add-hook 'text-mode-hook (lambda() (setq truncate-lines nil)))
(add-hook 'text-mode-hook 'turn-on-auto-fill)text-mode の場合は disable truncate にする
(add-hook 'text-mode-hook (lambda() (setq truncate-lines nil)))
; TCP コネクション (localhost:80) をはる ; open-network-stream は返値に process 名を返してくるので、それを proc に格納する (setq proc (open-network-stream "http-proc" "*test-http-buffer*" "localhost" 80)) #指定した *test-http-buffer* に結果が表示される; proc の状態を確認 (process-status proc) open ; proc に対して送る coding を指定する (set-process-coding-system proc 'binary 'binary) nil ; proc に文字列を送る (process-send-string proc (format(concat "GET / HTTP/1.0\r\n" "\r\n"))) nil
Process http-proc connection broken by remote peer HTTP/1.1 200 OK Date: Thu, 06 Aug 2009 13:59:44 GMT Server: Apache/2.2.9 (Win32) DAV/2 mod_ssl/2.2.9 OpenSSL/0.9.8i mod_autoindex_color mod_python/3.3.1 Python/2.5.2 PHP/5.2.6 Last-Modified: Fri, 06 Mar 2009 23:42:57 GMT ETag: "6000000000669-1bc-4647bd829e8db" Accept-Ranges: bytes Content-Length: 444 Connection: close Content-Type: text/html <html> <head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8"> <title>Test page</title> </head> <body> Hello, world </body> </html> Process http-proc connection broken by remote peer
open-network-stream NAME BUFFER-OR-NAME HOST SERVICETCP 接続を確立する
| 引数 | 内容 |
| NAME | process 名を指定する |
| BUFFER-OR-NAME | この TCP コネクションに割り付けられるバッファ名 |
| HOST | 接続先ホスト |
| SERVICE | ネットワークサービス名 または ポート番号 |
process-status PROCESS-NAMEPROCESS-NAME で指定したプロセスの状態を返す
| 引数 | 内容 |
| PROCESS-NAME | プロセス名 |
| 返値 | 意味 |
| run | プロセスは running 状態 |
| stop | プロセスは停止している、再開可能 |
| exit | プロセスは終了している |
| signal | プロセスは fatal signal を受けた |
| open | ネットワークコネクションが open している |
| closed | ネットワークコネクションは close した |
| connect | non-blocking コネクション、完了待ち |
| failed | non-blocking コネクション、失敗 |
| listen | ネットワークサーバが listen 中 |
| nil | 指定したプロセス名のプロセスは存在しない |
set-process-coding-system PROCESS &optional DECODING-SYSTEM ENCODING-SYSTEMPROCESS で指定したプロセスとの output/input 用コーディングを指定する
| 引数 内容 | |
| PROCESS | プロセス名 |
| DECODING-SYSTEM | プロセスからの output コーディング |
| ENCODING-SYSTEM | プロセスへの input コーディング |
process-send-string PROCESS STRINGプロセスに対して文字列を送る
| 引数 内容 | |
| PROCESS | プロセス名 |
| STRING | 文字列 |
start-process
set-process-sentinel process sentinelprocess が Finished, exited abnormally など特定のイベント状態になった場合に sentinel で渡した命令を実行する
(defun ls-test ()
(interactive)
(defun msg-me (process event)
(princ
(format "Process: %s had the event [%s]" process event)))
(set-process-sentinel
(start-process "ls" "*ls*" "ls" "-a")
'msg-me)
)
(defun ls-test2 ()
(interactive)
(set-process-sentinel
(start-process "ls" "*ls*" "ls" "-a")
(lambda (process event)
(princ
(format "Process: %s had the event [%s]" process event)))
)
)
*ls*
.
..
COPYING
addpm.exe
cmdproxy.exe
ctags.exe
ddeclient.exe
digest-doc.exe
ebrowse.exe
emacs.exe
emacsclient.exe
emacsclientw.exe
etags.exe
hexl.exe
movemail.exe
runemacs.exe
sorted-doc.exe
*Messages*
Process: ls<1> had the event [finished
]
(defun dosomething-region (pmin pmax) (interactive "r") (message "pmin:%d pmax:%d" pmin pmax) )region を選択して M-x dosomething-region を実行すると *Messages* バッファに以下のように表示される
pmin:405 pmax:423interactive に "r" を渡すと選択した region の開始位置、終了位置が引数に格納される。
(defun http-post (server fname lname)
(interactive "sServer:\nsFirst name:\nsLast name:")
(setq proc (open-network-stream "http-proc" "*http-buffer*" server 80))
(set-process-coding-system proc 'binary 'binary)
(setq content-len
(+ (+ (+ (length "fname=") (length fname)) (length "&lname=")) (length lname)))
(process-send-string
proc
(format (concat
"POST /test.php HTTP/1.0\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: "
(number-to-string content-len)
"\r\n"
"\r\n"
"fname="
fname
"&lname="
lname))))
(defun string-length (str) (interactive "sInput a string:") (setq len (length str)) (message "String: %s (%d)" str len))M-x string-length を実行すると mini-buffer に文字列の入力を求めるメッセージが表示される。文字列を入力すると入力した文字列とその文字数を表示する
String: hello (5)
(interactive "sWhat is your name: ")これを実行すると mini-buffer に What is your name: と表示される。そこで文字列を入力すると結果が表示される
(interactive "sWhat your name: ")
("hoge")
数字を入力させる場合は下記のように n を先頭につける
(interactive "nInput your favorite number: ") (5)関数を定義して M-x 関数名 で実行すると mini-buffer に interactive で値を入力して渡すことができる
(defun add (a b) (interactive "nFirst argument:\nnSecond argument:") (setq ans (+ a b)) (message "Ans: %d" ans)) addM-x add を実行すると First argument:, Second argument: と mini-buffer に表示され、数値を入力する。計算結果は mini-buffer に出力される
(defun hello (a b) (setq ans (+ a b)) (message "Ans: %d" ans)) hello (hello 1 3) "Ans: 4"フォーマット
defun NAME ARGUMENT-LIST BODY-FORMSTable: defun の引数
| 引数 | 意味 |
| NAME | 関数名 |
| ARGUMENT-LIST | 引数リスト |
| BODY-FORMS | 関数の中身 |
(defun http-post (server)
(setq proc (open-network-stream "http-proc" "*http-buffer*" server 80))
(set-process-coding-system proc 'binary 'binary)
(process-send-string
proc
(format (concat
"POST /test.php HTTP/1.0\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 23\r\n"
"\r\n"
"fname=hello&lname=world"))))
http-post
(http-post "localhost")
nil
(message "Hello, world")*Message* バッファには次のように出力される
Hello, world
(princ "hello, world") hello, world"hello, world"
(operand arg1 arg2)例)
(variable value)例)
(lambda (arg-variables...) [documentation-string] [interactive-declaration] body-forms ...)
((lambda (a b c) (+ a b c)) 1 2 3) 6(a b c) という新しい関数を登録する。内容は (+ a b c) となる。その (a b c) という関数に (1 2 3) という引数を渡した結果は 1 + 2 + 3 となるので結果は 6 となる。
| id | name |
|---|---|
| 1 | Albert |
| 3 | Emily |
| 14 | John |
| id | date |
|---|---|
| 1 | 2010-02-25 |
| 2 | 2010-07-20 |
| 3 | 2012-12-15 |
| id | student_id | date_id | math | physics | chemistry |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 69 | 42 | 74 |
| 2 | 3 | 1 | 80 | 93 | 85 |
| 3 | 14 | 1 | 35 | 42 | 38 |
| 4 | 1 | 2 | 72 | 53 | 68 |
| 5 | 3 | 2 | 75 | 85 | 87 |
| 6 | 14 | 2 | 46 | 54 | 46 |
| 7 | 1 | 3 | 63 | 54 | 65 |
| 8 | 3 | 3 | 85 | 88 | 92 |
| 9 | 14 | 3 | 43 | 53 | 41 |
testdb=# CREATE TABLE test_scores (id SERIAL PRIMARY KEY, student_id INTEGER NOT NULL, date_id INTEGER NOT NULL, math INTEGER, physics INTEGER, chemistry INTEGER); testdb=# CREATE TABLE students (id SERIAL PRIMARY KEY, name TEXT); testdb=# CREATE TABLE dates (id SERIAL PRIMARY KEY, date DATE);
testdb=# INSERT INTO students (id, name) VALUES (1, 'Albert'); INSERT 0 1 testdb=# INSERT INTO students (id, name) VALUES (3, 'Emily'); INSERT 0 1 testdb=# INSERT INTO students (id, name) VALUES (14, 'John');datesテーブルに登録。
testdb=# INSERT INTO dates (date) VALUES (date('2010-02-25'));
INSERT 0 1
testdb=# INSERT INTO dates (date) VALUES (date('2010-07-20'));
INSERT 0 1
testdb=# INSERT INTO dates (date) VALUES (date('2012-12-15'));
INSERT 0 1
test_scoreテーブルに登録。
testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (1, 1, 69, 42, 74); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (3, 1, 80, 93, 85); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (14, 1, 35, 42, 38); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (1, 2, 72, 53, 68); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (3, 2, 75, 85, 87); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (14, 2, 46, 54, 46); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (1, 3, 63, 54, 65); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (3, 3, 85, 88, 92); INSERT 0 1 testdb=# INSERT INTO test_scores (student_id, date_id, math, physics, chemistry) VALUES (14, 3, 43, 53, 41);
testdb=# SELECT * FROM test_scores JOIN students ON test_scores.student_id = students.id JOIN dates ON test_scores.date_id = dates.id; id | student_id | date_id | math | physics | chemistry | id | name | id | date ----+------------+---------+------+---------+-----------+----+--------+----+---- -------- 1 | 1 | 1 | 69 | 42 | 74 | 1 | Albert | 1 | 2010-02-25 2 | 3 | 1 | 80 | 93 | 85 | 3 | Emily | 1 | 2010-02-25 3 | 14 | 1 | 35 | 42 | 38 | 14 | John | 1 | 2010-02-25 4 | 1 | 2 | 72 | 53 | 68 | 1 | Albert | 2 | 2010-07-20 5 | 3 | 2 | 75 | 85 | 87 | 3 | Emily | 2 | 2010-07-20 6 | 14 | 2 | 46 | 54 | 46 | 14 | John | 2 | 2010-07-20 7 | 1 | 3 | 63 | 54 | 65 | 1 | Albert | 3 | 2012-12-15 8 | 3 | 3 | 85 | 88 | 92 | 3 | Emily | 3 | 2012-12-15 9 | 14 | 3 | 43 | 53 | 41 | 14 | John | 3 | 2012-12-15 (9 行)
testdb=# SELECT test_scores.id, students.name, dates.date, test_scores.math, test_scores.physics, test_scores.chemistry FROM test_scores JOIN students ON test_scores.student_id = students.id JOIN dates ON test_scores.date_id = dates.id; id | name | date | math | physics | chemistry ----+--------+------------+------+---------+----------- 1 | Albert | 2010-02-25 | 69 | 42 | 74 2 | Emily | 2010-02-25 | 80 | 93 | 85 3 | John | 2010-02-25 | 35 | 42 | 38 4 | Albert | 2010-07-20 | 72 | 53 | 68 5 | Emily | 2010-07-20 | 75 | 85 | 87 6 | John | 2010-07-20 | 46 | 54 | 46 7 | Albert | 2012-12-15 | 63 | 54 | 65 8 | Emily | 2012-12-15 | 85 | 88 | 92 9 | John | 2012-12-15 | 43 | 53 | 41 (9 行)
$ createdb testdb (作成するデータベース名) CREATE DATABASE
$ psql testdb testdb=>
$ psql postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
postgres=# drop database db_name; DROP DATABASE
testdb=> CREATE TABLE tbl (id SERIAL PRIMARY KEY, date DATE NOT NULL, name TEXT NOT NULL, math INTEGER, physics INTEGER, chemistry INTEGER);
testdb=> INSERT INTO tbl (date, name, math, physics, chemistry) VALUES (NULL, date('2010-02-25'), 'Albert', 69, 42, 74);
testdb=> INSERT INTO tbl (date, name, math, physics, chemistry) VALUES (NULL, date('2010-02-25'), 'Emily', 80, 93, 85);
testdb=> INSERT INTO tbl (date, name, math, physics, chemistry) VALUES (NULL, date('2010-02-25'), 'John', 35, 42, 38);
testdb=> SELECT * FROM tbl; id | date | name | math | physics | chemistry ----+------------+--------+------+---------+----------- 1 | 2010-02-25 | Albert | 69 | 42 | 74 2 | 2010-02-25 | Emily | 80 | 93 | 85 3 | 2010-02-25 | John | 35 | 42 | 38 (3 rows)
testdb=> \d tbl
Table "public.tbl"
Column | Type | Modifiers
-----------+---------+--------------------------------------------------
id | integer | not null default nextval('tbl_id_seq'::regclass)
date | date | not null
name | text | not null
math | integer |
physics | integer |
chemistry | integer |
Indexes:
"tbl_pkey" PRIMARY KEY, btree (id)
testdb=> \q
# service postgresql restart
# yum install postgresql # yum install postgresql-server
# service postgresql start postgresql サービスを開始中: [ OK ]
# su - postgres $ echo $PGDATA /var/lib/pgsql/data $ initdb --encoding=UTF8 --no-locale
$ su - postgres
$ createuser Enter name of role to add: xxxx Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) y Shall the new role be allowed to create more new roles? (y/n) n CREATE ROLE
$ su - postgres
$ createuser username
$ psql postgres=# SELECT usename,usesuper from pg_user; usename | usesuper ----------+---------- postgres | t xxxx | f (2 rows) postgres=# \q
$ sudo yum install phpPgAdmin
# vi /etc/phpPgAdmin/config.inc.php $conf['extra_login_security'] = false; ← trueをfalseに変更する
# service postgresql restart # service httpd restart