29.6.06

Elisp ile dosya calistirma

Emacs'da bir dosyayi calistirip, ciktisini yeni bir buffer'da gormek icin yazdigim bir script. Buradan da kopya cektim biraz. Eminim ki tekeri bastan icad ediyorum, hem de kagni tekeri (aklima bunu getirdi). Ama kucuk olsun benim olsun :) Bir de hala su degiskenlere dogru duzgun isim vermeyi ogrenemedim. Zamanimin yarisi bunu ne diye adlandirayim diye geciyor.

Not: daha duzgun degisken isimleri vermeye calistim.

; a list of triples that contain:
;  - the regex for the file name
;  - the name of the buffer that will be created
;  - a list containing the command to execute the file
(defvar exec-list nil)

(defun add-exec (regex buffer-name command-list)
  (let ((entry (list regex buffer-name command-list)))
    (setf exec-list (cons entry exec-list))))

(defun get-exec-help (filename execs)
  (unless (null execs)
    (let* ((entry (car execs))
       (regex (car entry)))
      (if (string-match regex filename)
      (cdr entry)
    (get-exec-help filename (cdr execs))))))

(defun get-exec (filename)
  (get-exec-help filename exec-list))


(defun exec-file () 
  "Execute a file, if possible."
  (interactive)
  (let* ((curr-buf (buffer-name))
     (exec-params (get-exec curr-buf)))
    (if exec-params
    (let ((exec-buffer (car exec-params))
          (exec-command-list (cadr exec-params)))
      (if (get-buffer exec-buffer) 
          (kill-buffer exec-buffer)
        (progn
          (eval (append
             (list 'start-process 
               exec-buffer
               exec-buffer)
             exec-command-list
             (list curr-buf)))
          (switch-to-buffer-other-window exec-buffer)
          (switch-to-buffer-other-window curr-buf)))))))

(add-exec "\\.js$" 
      "javascript" 
      (list "/usr/bin/rhino" "-f"))

(add-exec "\\.\\(pir\\|pasm\\)$" 
      "parrot" 
      (list "/home/yavuz/parrot_svn/parrot/parrot"))

(add-exec "\\.html$" 
      "html" 
      (list "/usr/bin/firefox"))

(global-set-key [f12] 'exec-file)