ä»åã¯ãã®ãããã¢ã¼ãã§ã®Emacs Lispã«ç¦ç¹ãå½ã¦ã¦ï¼ããã¹ããå¦çããã¹ã¯ãªããããµã¼ãã¼ãã¼ã¢ã³ãæ¸ãããã®ãã¯ããã¯ãç´¹ä»ãããã¨æãã¾ãï¼ â â ãããã¢ã¼ãã§ã®æ¨æºå ¥åºå ãããã¢ã¼ãã§ã¯ä¸é¨ã®é¢æ°ãæ¨æºå ¥åºåãæ±ãããã®é¢æ°ã«å¤åãã¾ãï¼æ©éããã¾ãã®ã¹ã¯ãªãããæ¸ãã¦ã¿ã¾ããã(ãªã¹ã1)ï¼ ------------------ <ãªã¹ã1> hello.el (princ "Hello, world.\n") ------------------ princé¢æ°ã¯ç¬¬ä¸å¼æ°ãæ¨æºåºåã«åºåããé¢æ°ã§ãï¼ãã®ã¹ã¯ãªãããå®è¡ããã«ã¯ã¿ã¼ããã«ä¸ã§Emacsãèµ·åãã¾ã(ãªã¹ã2)ï¼ ------------------ <ãªã¹ã2> % emacs --script hello.el Loading subst-ksc... oading subst-gb2312... Loading subst-big5... Loading subst-jis... Hello, world. ------------------ --scriptãªãã·ã§ã³ã¯æå®ããããã¡ã¤ã«ããããã¢ã¼ãã§å®è¡ãã¾ã(è注1)ï¼Loading...ã¨åºã¦ããã®ã¯ã¨ããããç¡è¦ãã¦ãã ããï¼ ------------------ <è注1> --scriptãªãã·ã§ã³ã¯Emacs 22ã§å ¥ã£ããªãã·ã§ã³ã§ãï¼ãã®è¨äºèªä½Emacs 22ãåæã«ãã¦æ¸ãã¦ãã¾ããï¼Emacs 21ã使ãããå ´åã¯--scriptãªãã·ã§ã³ã®ãããã«-batchãªãã·ã§ã³ã¨-lãªãã·ã§ã³ã使ç¨ãã¦ãã ããï¼ ------------------ æ¨æºå ¥åããæååãåå¾ããã«ã¯read-stringé¢æ°ã使ãã¾ãï¼ãã®é¢æ°ã¯ç¬¬ä¸å¼æ°ãæ¨æºåºåã«åºåãã¦(è注2)ï¼æ¨æºå ¥åããä¸è¡åå¾ãã¦è¿ãã¾ãï¼æ¨æºå ¥åããä¸è¡åå¾ãã¦ãã®ã¾ã¾æ¨æºåºåã«åºåããã¹ã¯ãªãããæ¸ãã¦ã¿ã¾ããã(ãªã¹ã3)ï¼ ------------------ <è注2> ã¦ã¼ã¶ã¼ã«ã©ã®ãããªå ¥åãæ±ãããã®èª¬ææã¨ãã¦ä½¿ãã¾ã ------------------ ------------------ <ãªã¹ã3> ask.el (princ-list "Input string is: " (read-string "Input some string: ")) vv------------------ princ-listé¢æ°ã¯å ¨ã¦ã®å¼æ°ãæ¨æºåºåã«åºåãï¼æå¾ã«æ¹è¡ãåºåããé¢æ°ã§ãï¼å®è¡ããã¨ãªã¹ã4ã®ããã«ãªãã¾ãï¼ ------------------ <ãªã¹ã4> % emacs --script ask.el Input some string: Hello, Emacs Lisp! Input string is: Hello, Emacs Lisp! ------------------ åç´ã«æ¨æºå ¥åããä¸è¡åå¾ããã«ã¯read-stringé¢æ°ã®ç¬¬ä¸å¼æ°ã«ç©ºæåãæå®ãã¾ãï¼é¢æ°ã¨ãã¦å®ç¾©ãã¦ããã¨è¯ãã§ããã(ãªã¹ã5)ï¼ ------------------ <ãªã¹ã5> read-lineé¢æ° (defun read-line () (read-string "")) ------------------ messageé¢æ°ãerroré¢æ°ã¯æ¨æºã¨ã©ã¼ã«åºåããé¢æ°ã§ãï¼å ã»ã©Loading...ã¨åºã¦ããã®ã¯ï¼Emacs Lispãã¡ã¤ã«ããã¼ãããloadé¢æ°ãmessageé¢æ°ã使ã£ã¦ãã¼ãä¸ã§ãããã¨ãç¥ããã¦ããããã§ãï¼æ°ã«ãªãããã§ãããå®è¡æã«æ¨æºã¨ã©ã¼ã表示ãããªãããã«ãã¦ãã ãã(ãªã¹ã6)ï¼ ------------------ <ãªã¹ã6> % emacs --script hello.el 2> /dev/null Hello, world. ------------------ â â ã³ãã³ãã©ã¤ã³å¼æ° ã³ãã³ãã©ã¤ã³å¼æ°ãåå¾ããã«ã¯command-line-args-leftå¤æ°ã使ãã¾ã(ãªã¹ã7)ï¼ãã®å¤æ°ã«ã¯Emacsãå¦çãã¦ããªãã³ãã³ãã©ã¤ã³å¼æ°ã®ãªã¹ããå ¥ã£ã¦ãã¾ãï¼ ------------------ <ãªã¹ã7> args.el (princ-list "First argument is " (nth 0 command-line-args-left)) (princ-list "Second argument is " (nth 1 command-line-args-left)) ------------------ nthé¢æ°ã¯ãªã¹ãã®Nçªç®ã®è¦ç´ ãåå¾ããé¢æ°ã§ãï¼ãããå®è¡ããã¨ãªã¹ã8ã®ããã«ãªãã¾ãï¼ ------------------ <ãªã¹ã8> % emacs --script args.el foo bar First argument is foo Second argument is bar ------------------ â â ã½ã¼ã¹ã³ã¼ãæ´å½¢ã¹ã¯ãªããã®ä½æ åºæ¬çãªãã¨ã¯èª¬æããã®ã§ï¼ããã¤ãå®ç¨çãªãã¼ã«ãä½ã£ã¦ã¿ããã¨æãã¾ãï¼ ä½ãã®ã¯ã½ã¼ã¹ã³ã¼ãæ´å½¢ã¹ã¯ãªããã§ãï¼ããã¯sample.cã®ãããªã¤ã³ãã³ãã®æ´ã£ã¦ããªãã½ã¼ã¹ã³ã¼ãããããã«æ´å½¢ããã¹ã¯ãªããã§ãï¼ ------------------ <ãªã¹ã9> sample.c int main(int argc, char *argv[]) { char *s = argv[1]; while (*s) putchar(((*s++ - 'A' + 13) % 26) + 'A'); return 0; } ------------------ 使ãä»çµã¿ã¯find-fileé¢æ°ã¨indent-regioné¢æ°ã®buffer-stringé¢æ°ã®ä¸ã¤ã§ãï¼find-fileé¢æ°ã¯å¼æ°ã«æå®ããããã¡ã¤ã«ãéãã¦ï¼ãã®ãã¡ã¤ã«ã®ãããã¡ãã«ã¬ã³ããããã¡ã«è¨å®ãã¾ãï¼indent-regioné¢æ°ã¯ã«ã¬ã³ããããã¡ã®æå®ãããç¯å²ã®ã¤ã³ãã³ããæ´ãã¾ãï¼buffer-stringé¢æ°ã¯ã«ã¬ã³ããããã¡ã®å ¨å 容ãæååã§è¿ãã¾ãï¼ãããé çªéãã«æ¸ãã¦ããã°å®æã§ã(ãªã¹ã10)ï¼ ------------------ <ãªã¹ã10> indent.el (find-file (nth 0 command-line-args-left)) ; ã³ãã³ãã©ã¤ã³ã®ç¬¬ä¸å¼æ°ã«æå®ããããã¡ã¤ã«ãéã㦠(indent-region (point-min) (point-max)) ; æåããæå¾ã¾ã§æ´å½¢ (princ (buffer-string)) ------------------ æ´å½¢ããããã¡ã¤ã«ã第ä¸å¼æ°ã«æå®ãã¦å®è¡ãã¾ã(ãªã¹ã11)ï¼ ------------------ <ãªã¹ã11> % emacs --script indent.el sample.c int main(int argc, char *argv[]) { char *s = argv[1]; while (*s) putchar(((*s++ - 'A' + 13) % 26) + 'A'); return 0; } ------------------ ãã®ã¹ã¯ãªããã¯Emacsãèªèã§ããå ¨ã¦ã®è¨èªã§æ£ããåä½ãã¾ã(è注3)ï¼ããã¯indent-regioné¢æ°ãããããã®ã¡ã¸ã£ã¼ã¢ã¼ãã«é©ããåä½ããããããªã®ã§ããï¼ããã§ã¯è©³ç´°ã¯å²æããã¦ããã ãã¾ãï¼ ------------------ <è注3> ã¡ãªã¿ã«Pythonã¯åãã¾ããï¼ãªããªãã¤ã³ãã³ãã®å´©ããPythonã¹ã¯ãªããã¯äººéã§ã修復ã§ããªãããã§ãï¼ ------------------ â â grepã®ä½æ å®ç¨çãã¨è¨ãããã¨å¤§ããªçå符ãä»ãã¾ããï¼Emacs Lispã®ãã¯ãã«ããç¥ã£ã¦ããã ãããã«grepãä½ã£ã¦ã¿ããã¨æãã¾ãï¼grepã¯æå®ããããã¿ã¼ã³ã§å ¥åã«å¯¾ãã¦ããããè¡ãï¼ãããããè¡ãåºåããããã°ã©ã ã§ãï¼ æ©éä½ãããã®ã§ããï¼ãã®åã«ä¸ã¤éè¦ãªæºåããã¦ããå¿ è¦ãããã¾ãï¼å ã»ã©å®ç¾©ããread-lineé¢æ°ã¯ï¼Perlãªã©ã§ã¯ãã使ãä¸è¡èªã¿åºãé¢æ°ã§ããï¼ãããã¡ããã¼ã¹ã«ãã¦ããã¹ããå¦çããEmacs Lispã§ã¯ããªã使ãã¥ããé¢æ°ã§ã(è注4)ï¼ããã§æ¨æºå ¥åã®å 容ãæååã§ã¯ãªããããã¡ã¨ãã¦èªã¿åºããããã«get-stdin-bufferã¨ããé¢æ°ãå®ç¾©ãã¦ããã¾ããã(ãªã¹ã12)ï¼ ------------------ <è注4> æ£ç¢ºã«ã¯ãããã¡ã使ãæãããã®ããããã¾ããï¼ ------------------ ------------------ <ãªã¹ã12> get-stdin-bufferé¢æ° (defun get-stdin-buffer () (let ((buf-name "*stdin*")) (or (get-buffer buf-name) ; ãããã¡ããã§ã«åå¨ããå ´åã¯ãã®ã¾ã¾è¿ã (let ((buffer (generate-new-buffer buf-name))) ; ãªããã°ãããã¡ãä½æã㦠(with-current-buffer buffer ; ãã®ãããã¡ãä¸æçã«ã«ã¬ã³ããããã¡ã«ãã (condition-case nil ; EOFã«éããã¨ãã«çºçããã¨ã©ã¼ãæå¶ããããã®ãã¾ããªã (let (line) (while (setq line (read-line)) ; ä¸è¡èªã¿åºã㦠(insert line "\n"))) ; ãã®ä¸è¡ãã«ã¬ã³ããããã¡ã«æ¿å ¥ãã (error nil))) buffer)))) ; ä½æãããããã¡ãè¿ã ------------------ ãã®é¢æ°ã¯æ¨æºå ¥åã®å 容ãå ¨ã¦èªã¿è¾¼ãã§ãããã¡ã¨ãã¦è¿ãé¢æ°ã§ãï¼æ¨æºå ¥åã®å 容ãåç´ã«æ¨æºåºåã«åºåããã¹ã¯ãªãããæ¸ãã¦ã¿ã¾ããã(ãªã¹ã13)ï¼ ------------------ <ãªã¹ã13> (set-buffer (get-stdin-buffer)) ; æ¨æºå ¥åãããã¡ãã«ã¬ã³ããããã¡ã«ã㦠(princ (buffer-string)) ; ã«ã¬ã³ããããã¡ã®å 容ãæ¨æºåºåã«åºåãã ------------------ ãããã¡ãç¨ãããã¨ã«ããEmacs Lispã«æè¼ããã¦ãããã¾ãã¾ãªä¾¿å©ãªæ©è½ã使ããã¨ãã§ãã¾ãï¼Emacs Lispã®çé«ã¯ãããã¡ã«ããã¨è¨ã£ã¦ãéè¨ã§ã¯ããã¾ããï¼ å°ã横éã«ããã¾ãããããã§æºåãã§ãã¾ããï¼ ä»å使ãä»çµã¿ã¯get-stdin-bufferé¢æ°ã¨occuré¢æ°ã®äºã¤ã§ãï¼ occuré¢æ°ã¯ã«ã¬ã³ããããã¡ã®å ¨ã¦ã®è¡ã«å¯¾ãã¦å¼æ°ã§æå®ããããã¿ã¼ã³ã§ããããè¡ã£ã¦ï¼ãããããè¡ã*Occur*ãããã¡ã«è¡¨ç¤ºããé¢æ°ã§ãï¼è©¦ãã«Emacsã§sample.cãéãã¦ï¼M-x occur RET char RETã¨ãã¦ã¿ã¦ãã ããï¼ãªã¹ã14ã®ãããªå 容ã*Occur*ãããã¡ã«è¡¨ç¤ºãããã¨æãã¾ãï¼ ------------------ <ãªã¹ã14> *Occur* 3 matches for "char" in buffer: sample.c 1:int main(int argc, char *argv[]) { 2:char *s = argv[1]; 4:putchar(((*s++ - 'A' + 13) % 26) + 'A'); ------------------ è¦ã¦ã®ã¨ããoccuré¢æ°ããã£ã¦ãããã¨ã¯grepããã£ã¦ãããã¨ãã®ãã®ã§ãï¼ãªã®ã§å¾ã¯*Occur*ãããã¡ã®å 容ãåºåããã³ã¼ããæ¸ãã¦ããã°ããã°å®æã§ã(ãªã¹ã15)ï¼ ------------------ <ãªã¹ã15> *Occur* ; grep.el (set-buffer (get-stdin-buffer)) ; æ¨æºå ¥åãããã¡ãã«ã¬ã³ããããã¡ã«ã㦠(occur (nth 0 command-line-args-left)) ; occuré¢æ°ãå®è¡ãã (set-buffer "*Occur*") ; *Occur*ãããã¡ãã«ã¬ã³ããããã¡ã«ã㦠(forward-line 1) (princ (buffer-substring (point) (point-max))) ; æ¨æºåºåã«åºåãã ------------------ forward-lineé¢æ°ã§ãã¤ã³ããä¸è¡ä¸ã«ç§»åãã¦ããï¼buffer-substringé¢æ°ã§ãã®ãã¤ã³ããããããã¡ã®æå¾ã¾ã§ã®æååãåå¾ãããã¨ã«ããï¼*Occur*ãããã¡ã®æåã®è¡ãåºåããªãããã«ãã¦ãã¾ãï¼Emacs Lispãªãã§ã¯ã¨ããå¦çæ¹æ³ã§ãï¼ ãããå®è¡ããã¨ãªã¹ã16ã®ããã«ãªãã¾ãï¼ ------------------ <ãªã¹ã16> % emacs --script grep.el char sample.c 1:int main(int argc, char *argv[]) { 2:char *s = argv[1]; 4:putchar(((*s++ - 'A' + 13) % 26) + 'A'); ------------------ â â éåæå¦ç Emacsã¯ä¸è¦ãã«ãã¹ã¬ããã§åä½ãã¦ããããã«è¦ãã¾ããï¼å®ã¯ã·ã³ã°ã«ã¹ã¬ããã§åä½ãã¦ãã¾ãï¼ããã¯Emacs Lispãåæ§ã§ãï¼ä¾ãã°run-at-timeã¨ããã³ã¼ã«ããã¯ãå®æçã«å¼ã³åºãããã«ããé¢æ°ãããã¾ããï¼ãã®ã³ã¼ã«ããã¯ãã·ã³ã°ã«ã¹ã¬ããã§å¼ã³åºããã¾ãï¼ã¾ãï¼ã³ã¼ã«ããã¯ãç¨ãã¦å®è¡ããããã»ã¹ã®åºåãé次çã«å¦çãããã¨ãã§ãã¾ããï¼ãã®ã³ã¼ã«ããã¯ãã·ã³ã°ã«ã¹ã¬ããã§å¼ã³åºããã¾ãï¼ ãã®ããã«ã·ã³ã°ã«ã¹ã¬ããã§éåæå¦çãå®è¡ãã¤ã¤ã¦ã¼ã¶ã¼ã¸ã®ã¬ã¹ãã³ã¹æ§ãçãã«ããªãã¨ãããã¨ãå®ç¾ãã¦ããã®ãã³ãã³ãã«ã¼ãã§ãï¼ã³ãã³ãã«ã¼ãã¯Emacsãèµ·åããã¨ãã«å®è¡ãããæ°¸ä¹ ã«ã¼ãã§ããï¼ã¦ã¼ã¶ã¼ã«ããå ¥åããªããªã©ã®ã¢ã¤ãã«æéãå©ç¨ãã¦éåæå¦çãå®è¡ãã¦ãã¾ãï¼ æ確ãªçµäºããããã¨ãåæã¨ãã¦ãããããã¢ã¼ãã§ã¯å½ç¶ãªããã³ãã³ãã«ã¼ãã¯å®è¡ããã¾ããï¼ã¤ã¾ãéåæå¦çã¯ä½¿ããªãã¨ãããã¨ã«ãªãã¾ãï¼ããã¹ãå¦çã¹ã¯ãªãããå®è¡ããåã«ã¯åé¡ãªãã®ã§ããï¼ã³ãã¯ã·ã§ã³ãå¼µããããã¨ããã¼ã¿ãåä¿¡ãããã¨ãç¥ãããã®ã«ã³ã¼ã«ããã¯ã使ããµã¼ãã¼ãã¼ã¢ã³ãå®è¡ããå ´åã¯å°ã å°ã£ããã¨ã«ãªãã¾ãï¼ å¯¾çã¨ãã¦ã¯ã¹ã¯ãªããå ã§æ示çã«ã³ãã³ãã«ã¼ããå®è¡ããæ¹æ³ãèãããã¾ãï¼å ã»ã©ã®run-at-timeé¢æ°ã使ã£ã¦æ¤è¨¼ãã¦ã¿ã¾ããã(ãªã¹ã17)ï¼ ------------------ <ãªã¹ã17> timer1.el (run-at-time 1 1 (lambda () (princ-list "Hello"))) ; 1ç§ãã¨ã«Helloãåºåãã (recursive-edit) ; ã³ãã³ãã«ã¼ããå®è¡ãã ------------------ ãããå®è¡ããã°1ç§ãã¨ã«Helloãåºåãããã¨æå¾ ããã®ã§ããï¼ãã¤ã¾ã§å¾ ã£ã¦ãåºåããã¾ãã(ãªã¹ã18)ï¼ ------------------ <ãªã¹ã18> % emacs --script timer1.el ä½ãã§ãªã ------------------ åå ã¯ã³ãã³ãã«ã¼ãå ã®getcharé¢æ°ãã¦ã¼ã¶ã¼ã®å ¥åãå¾ ã£ã¦ããããã§ï¼è©¦ãã«é©å½ãªæåãæã£ã¦ã¨ã³ã¿ã¼ãæ¼ãã¦ããã¨ã¨ã©ã¼ã¡ãã»ã¼ã¸ã¨ã¨ãã«ããã¤ãã®Helloã表示ããã¾ãï¼ ã³ãã³ãã«ã¼ããå®è¡ããã®ã¯ãã¾ããããªããããªã®ã§ï¼èªåã§æ°¸ä¹ ã«ã¼ããå®è¡ãã¤ã¤é©å½ã«ã¢ã¤ãã«æéãä½ãæ¹æ³ãæ¤è¨¼ãã¦ã¿ã¾ã(ãªã¹ã18)ï¼ ------------------ <ãªã¹ã18> timer2.el (run-at-time 1 1 (lambda () (princ-list "Hello"))) (while t (sleep-for 0 100)) ; 100ããªç§ã®ã¢ã¤ãã«æéãä½ã ------------------ ãããå®è¡ããã¨æå¾ éãã«1ç§ãã¨ã«Helloãåºåããã¾ã(ãªã¹ã19)ï¼ ------------------ <ãªã¹ã19> % emacs --script timer2.el Hello Hello Hello ... ------------------ ããã§ãããã¢ã¼ãã§ãéåæå¦çã使ããããã«ãªãã¾ããï¼ãµã¼ãã¼ãã¼ã¢ã³ãæ¸ãéã¯ãã®æ°¸ä¹ ã«ã¼ããå¿ ãã©ããã«æ¸ãå¿ è¦ãããã¾ãï¼ â â HTTPãµã¼ãã¼ã®ä½æ ãµã¼ãã¼ãã¼ã¢ã³ã®æ¸ãæ¹ãããã£ãã®ã§ï¼HTTPãµã¼ãã¼ãä½ã£ã¦ã¿ããã¨æãã¾ãï¼ãã®HTTPãµã¼ãã¼ã¯ãããã¡ã¤ã«ãã¯ã©ã¤ã¢ã³ãã«éä¿¡ããã ãã®ããç°¡åãªãã®ã§ãï¼ â â â ãã¼ããéãã¦å¾ æ© ã¾ããã¼ããéãã¦å¾ æ©ããé¨åãä½ãã¾ãããï¼ãã¼ããéãã«ã¯make-network-processé¢æ°(è注5)ã使ãã¾ãï¼ãã®é¢æ°ã®filterãã¼ã¯ã¼ãã«ã³ã¼ã«ããã¯ãè¨å®ããã¨ã¯ã©ã¤ã¢ã³ãã®éä¿¡ãããã¼ã¿ãé次çã«å¦çãããã¨ãã§ãã¾ãï¼make-network-processé¢æ°ã ãã ã¨ããã«çµäºãã¦ãã¾ãã®ã§å ã»ã©ã®æ°¸ä¹ ã«ã¼ããæå¾ã«æ¸ãã¦éå½¢ã®å®æã§ã(ãªã¹ã20)ï¼ ------------------ <è注5> make-network-processé¢æ°ã¯Emacs 22ã§å ¥ã£ãé¢æ°ã§ãï¼ ------------------ ------------------ <ãªã¹ã20> httpd.el (defun httpd-filter (process string) ; ããã«ãã¡ã¤ã«ãéä¿¡ããã³ã¼ããæ¸ã (delete-process process)) ; ã³ãã¯ã·ã§ã³ãåæãã (defun httpd-start () (interactive) (make-network-process :name "HTTPD" :server t :host 'local :service 4000 :filter 'httpd-filter)) (when noninteractive ; ãããã¢ã¼ããªã (httpd-start) ; ãµã¼ãã¼ãéå§ã㦠(while t ; å¾ æ© (sleep-for 0 100))) ------------------ noninteractiveå¤æ°ã¯Emacsããããã¢ã¼ãã§èµ·åãããã©ãããå ¥ã£ã¦ãã¾ãï¼noninteractiveå¤æ°ã§åå²ãããã¨ã«ããï¼Emacsã§ãã®ã¹ã¯ãªããããã¼ããã¦ãåæã«ãµã¼ãã¼ãéå§ããããæ°¸ä¹ ã«ã¼ããå®è¡ããããããªãããã«ãã¦ãã¾ãï¼ â â â ãã¡ã¤ã«ã®éä¿¡ 次ã«httpd-filteré¢æ°ãå®è£ ãã¾ãï¼ã¯ã©ã¤ã¢ã³ãããªã¯ã¨ã¹ããéä¿¡ãã¦ããã¨httpd-filteré¢æ°ãã³ã¼ã«ããã¯ããã¾ãï¼ã³ã¼ã«ããã¯æã«ï¼ã³ãã¯ã·ã§ã³ã®ããã»ã¹ã¨ã¯ã©ã¤ã¢ã³ãããéããããã¼ã¿(ä»åã¯ä½¿ãã¾ãã)ã渡ãããã®ã§ï¼process-send-stringé¢æ°ã¨process-send-regioné¢æ°ã使ã£ã¦ã¯ã©ã¤ã¢ã³ãã«ãã¡ã¤ã«ãéä¿¡ãã¾ã(ãªã¹ã21)ï¼ ------------------ <ãªã¹ã21> httpd.el (defvar httpd-file "~/.emacs") (defun httpd-filter (process string) (with-current-buffer (find-file-noselect httpd-file) ; ãã¡ã¤ã«ã®ãããã¡ãä¸æçã«ã«ã¬ã³ããããã¡ã«è¨å®ã㦠(process-send-string process ; ã¬ã¹ãã³ã¹ããããéä¿¡ãã (concat "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n\r\n")) (process-send-string process "
") (process-send-region process (point-min) (point-max)) ; ã«ã¬ã³ããããã¡ã®å 容ãã¯ã©ã¤ã¢ã³ãã«éä¿¡ãã (process-send-string process "")) (delete-process process)) ... ------------------ find-file-noselecté¢æ°ã¯éãããã¡ã¤ã«ã®ãããã¡ãã«ã¬ã³ããããã¡ã«è¨å®ããªãã¨ããç¹ãé¤ãã°find-fileé¢æ°ã¨åãã§ãï¼ ãã®ã¹ã¯ãªãããå®è¡ãã¦ããï¼ãã©ã¦ã¶ã§http://localhost:4000/ãéãã¨~/.emacsã®å 容ã表示ãããã¨æãã¾ãï¼ â â â ããå°ã工夫 ä¸å¿å½åã®ç®çã¯éæã§ããã®ã§ããï¼ããã ã¨ãã¾ãè¸ããªãã®ã§ï¼åç´ã«ãã¡ã¤ã«ãéä¿¡ããã®ã§ã¯ãªãï¼äºç´èªãã³ã¡ã³ãã«è²ãä»ãã¦éä¿¡ããããã«ãã¦ã¿ããã¨æãã¾ãï¼ ã¾ãhtmlizeã¨ããããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¾ã(ãªã¹ã22)ï¼ ------------------ <ãªã¹ã21> ; install-elisp.elãå ¥ã£ã¦ãªãå ´å㯠; URLããç´æ¥ãã¦ã³ãã¼ããã¦ã¤ã³ã¹ãã¼ã«ãã¦ãã ããï¼ (install-elisp "http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el") ------------------ ãã®ããã±ã¼ã¸ã®htmlizer-bufferé¢æ°ã使ãã°è²ã¥ãããããããã¡ãHTMLã«å¤æãããã¨ãã§ãã¾ã(ãªã¹ã22)ï¼ ------------------ <ãªã¹ã22> httpd.el (require 'htmlize) ; 追å ... (defun httpd-filter (process string) (with-current-buffer (with-current-buffer (find-file-noselect httpd-file) ; ãã¡ã¤ã«ã®ãããã¡ãä¸æçã«ã«ã¬ã³ããããã¡ã«è¨å®ã㦠(font-lock-fontify-buffer) ; è²ã¥ãã㦠(htmlize-buffer)) ; HTMLå¤æãã¦ãã®ãããã¡ãè¿ã (process-send-string process ; ã¬ã¹ãã³ã¹ããããéä¿¡ãã (concat "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n\r\n")) (process-send-region process (point-min) (point-max))) ; ã«ã¬ã³ããããã¡ã®å 容ãã¯ã©ã¤ã¢ã³ãã«éä¿¡ãã (delete-process process)) ... ------------------ ããããããã¢ã¼ãã§å®è¡ãã¦ãã©ã¦ã¶ã§éãã¨å³1ã®ããã«çã£é»ã«ãªãã¨æãã¾ãï¼ããã¯ãããã¢ã¼ãã§èµ·åããEmacsã¯è¡¨ç¤ºé¢é£ã®åæåãå®å ¨ã«ã¹ãããããããã§ãï¼ãªã¹ã23ã®ããã«RGBã§è²ãæå®ãã¦ããã°å³2ã®ããã«ã¡ããã¨è²ã¥ãããã¾ãï¼ ------------------ <å³1> httpd1.png ------------------ ------------------ <å³2> httpd2.png ------------------ ------------------ <ãªã¹ã23> httpd.el (when noninteractive ; ãããã (set-foreground-color "black") (set-background-color "#ffffe0") (set-face-attribute 'font-lock-builtin-face nil :foreground "#da70d6") (set-face-attribute 'font-lock-comment-face nil :foreground "#b22222") (set-face-attribute 'font-lock-constant-face nil :foreground "#5f9ea0") (set-face-attribute 'font-lock-function-name-face nil :foreground "#0000ff") (set-face-attribute 'font-lock-keyword-face nil :foreground "#a020f0") (set-face-attribute 'font-lock-string-face nil :foreground "#ffc1c1") (set-face-attribute 'font-lock-type-face nil :foreground "#bc8f8f") (set-face-attribute 'font-lock-variable-name-face nil :foreground "#ffb90f") (set-face-attribute 'font-lock-warning-face nil :bold t :foreground "#ff0000") ; ããã¾ã§è¿½å ... ------------------ æçµçã«ãªã¹ã24ã®ããã«ãªãã¾ãï¼ ------------------ <ãªã¹ã24> httpd.el (require 'htmlize) (defvar httpd-file "~/.emacs") (defun httpd-filter (process string) (with-current-buffer (with-current-buffer (find-file-noselect httpd-file) (font-lock-fontify-buffer) (htmlize-buffer)) (process-send-string process (concat "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n\r\n")) (process-send-region process (point-min) (point-max))) (delete-process process)) (defun httpd-start () (interactive) (make-network-process :name "HTTPD" :server t :host 'local :service 4000 :filter 'httpd-filter)) (when noninteractive (set-foreground-color "black") (set-background-color "#ffffe0") (set-face-attribute 'font-lock-builtin-face nil :foreground "#da70d6") (set-face-attribute 'font-lock-comment-face nil :foreground "#b22222") (set-face-attribute 'font-lock-constant-face nil :foreground "#5f9ea0") (set-face-attribute 'font-lock-function-name-face nil :foreground "#0000ff") (set-face-attribute 'font-lock-keyword-face nil :foreground "#a020f0") (set-face-attribute 'font-lock-string-face nil :foreground "#ffc1c1") (set-face-attribute 'font-lock-type-face nil :foreground "#bc8f8f") (set-face-attribute 'font-lock-variable-name-face nil :foreground "#ffb90f") (set-face-attribute 'font-lock-warning-face nil :bold t :foreground "#ff0000") (httpd-start) (while t (sleep-for 0 100))) ------------------ â â â emacsclientã®æ emacsclientã使ã£ã¦åçã«ãµã¼ãã¼ã®ç¶æ ãå¤æ´ãããã¯ããã¯ãç´¹ä»ãã¾ãï¼ã¾ãhttpd.elã®é©å½ãªå ´æã«server-starté¢æ°ã追å ãã¾ã(ãªã¹ã25)ï¼ ------------------ <ãªã¹ã25> httpd.el (when noninteractive (server-start) ; 追å ... ------------------ ããããããã¢ã¼ãã§å®è¡ãã¦ï¼ã¯ã©ã¤ã¢ã³ãã«éä¿¡ããããã¡ã¤ã«ãå¤ãããå ´åã¯emacsclientã使ã£ã¦åçã«httpd-fileå¤æ°ã®å¤ãå¤æ´ãã¾ã(ãªã¹ã26)ï¼ ------------------ <ãªã¹ã26> % emacsclient -e '(setq httpd-file "~/sample.c")' ------------------ ããã§ãã©ã¦ã¶ããªãã¼ãããã¨~/sample.cã®å 容ãè²ã¤ãã§è¡¨ç¤ºããã¾ãï¼Emacs Lispã§ã¹ã¯ãªãããæ¸ãã°ãã®ãããªãã¨ãã§ããã®ã§ãï¼ â â æå¾ã« ä»å触ããã¹ã¯ãªããè¨èªã¨ãã¦ã®Emacs Lispã¯å®ã¯ããã»ã©æ°ãããã®ã§ã¯ããã¾ããï¼ãããã¨æãã°ããªãå¤ãEmacsã§ãã¹ã¯ãªãããå®è¡ãããã¨ãã§ããã¨æãã¾ãï¼ãããï¼Emacs Lispãé ããããªã®ãï¼ãã®æã®é¢æ°é¡ã貧弱ã ãããªã®ãï¼ãããã¯Emacsã®èµ·åãé ããããªã®ãï¼ãããããã¾ãããï¼Emacs Lispãã¹ã¯ãªããè¨èªã¨ãã¦ä½¿ã£ã¦ããã®ã¯ããã¾ã§è¦ããã¨ãããã¾ããï¼æè¿ã§ã¯Emacsã®èµ·åã®é ããæ°ã«ãªããªããããã«ãã·ã³ã¹ããã¯ãåä¸ãï¼å ã»ã©ä½¿ã£ãmake-network-processé¢æ°ã®ããã«æ°ããªä½¿ãæ¹ãå±éãããããªé¢æ°ãå¾ã ã«å¢ãã¦ãã¦ããã®ã§ï¼ä»å¾Emacs Lispãã¹ã¯ãªããè¨èªã¨ãã¦ä½¿ãæ©ä¼ãããããããããããããã¾ããï¼ä»åç´¹ä»ããã®ã¯Emacs Lispã®ä¸é¢ã§ããããã¾ãããï¼ãã®è¨äºãæ©ä¼ã«Emacs Lispã§ã¹ã¯ãªãããæ¸ã人ãä¸äººã§ãå¢ããã°ããããéãã§ãï¼ â â TODO Appendix ãã使ãé¢æ°