ソースを参照

Made indentation consistent across all source files

Raw tabs, displayed with indent 2. Added a section to the README
explaining this, and added emacs & vim modelines to adhere to these
settings.
gfxmonk 14 年 前
コミット
ee215ab680
2 ファイル変更407 行追加405 行削除
  1. 275 274
      edit-server.el
  2. 132 131
      pycl.py

+ 275 - 274
edit-server.el

@@ -1,3 +1,4 @@
+;; -*- tab-width:2; indent-tabs-mode:t -*- vim: set noet ts=2:
 ;;
 ;;
 ;; Emacs edit-server
 ;; Emacs edit-server
 ;;
 ;;
@@ -15,18 +16,18 @@
 ;; instance:
 ;; instance:
 ;;
 ;;
 ;; (if (locate-library "edit-server")
 ;; (if (locate-library "edit-server")
-;;    (progn
-;;      (require 'edit-server)
-;;      (setq edit-server-new-frame nil)
-;;      (edit-server-start)))
+;;		(progn
+;;			(require 'edit-server)
+;;			(setq edit-server-new-frame nil)
+;;			(edit-server-start)))
 ;;
 ;;
 ;; To open pages for editing in new frames using a running emacs
 ;; To open pages for editing in new frames using a running emacs
 ;; started in --daemon mode:
 ;; started in --daemon mode:
 ;;
 ;;
 ;; (if (and (daemonp) (locate-library "edit-server"))
 ;; (if (and (daemonp) (locate-library "edit-server"))
-;;     (progn
-;;       (require 'edit-server)
-;;       (edit-server-start)))
+;;		 (progn
+;;			 (require 'edit-server)
+;;			 (edit-server-start)))
 ;;
 ;;
 ;; (C) 2009 Alex Bennee (alex@bennee.com)
 ;; (C) 2009 Alex Bennee (alex@bennee.com)
 ;; (C) 2010 Riccardo Murri (riccardo.murri@gmail.com)
 ;; (C) 2010 Riccardo Murri (riccardo.murri@gmail.com)
@@ -39,98 +40,98 @@
 ;(setq edebug-all-defs 't)
 ;(setq edebug-all-defs 't)
 
 
 (if (not (featurep 'make-network-process))
 (if (not (featurep 'make-network-process))
-    (error "Incompatible version of [X]Emacs - lacks make-network-process"))
+		(error "Incompatible version of [X]Emacs - lacks make-network-process"))
 
 
 ;; Customization
 ;; Customization
 (defcustom edit-server-port 9292
 (defcustom edit-server-port 9292
-  "Local port the edit server listens to."
-  :group 'edit-server
-  :type 'integer)
+	"Local port the edit server listens to."
+	:group 'edit-server
+	:type 'integer)
 
 
 (defcustom edit-server-host nil
 (defcustom edit-server-host nil
-  "If not nil, accept connections from HOST address rather than just
+	"If not nil, accept connections from HOST address rather than just
 localhost. This may present a security issue."
 localhost. This may present a security issue."
-  :group 'edit-server
-  :type 'boolean)
+	:group 'edit-server
+	:type 'boolean)
 
 
 (defcustom edit-server-verbose nil
 (defcustom edit-server-verbose nil
-  "If not nil, log connections and progress also to the echo area."
-  :group 'edit-server
-  :type 'boolean)
+	"If not nil, log connections and progress also to the echo area."
+	:group 'edit-server
+	:type 'boolean)
 
 
 (defcustom edit-server-done-hook nil
 (defcustom edit-server-done-hook nil
-  "Hook run when done editing a buffer for the Emacs HTTP edit-server.
+	"Hook run when done editing a buffer for the Emacs HTTP edit-server.
 Current buffer holds the text that is about to be sent back to the client."
 Current buffer holds the text that is about to be sent back to the client."
-  :group 'edit-server
-  :type 'hook)
+	:group 'edit-server
+	:type 'hook)
 
 
 (defcustom edit-server-start-hook nil
 (defcustom edit-server-start-hook nil
-  "Hook run when starting a editing buffer.  Current buffer is
-the fully prepared editing buffer.  Use this hook to enable your
+	"Hook run when starting a editing buffer.  Current buffer is
+the fully prepared editing buffer.	Use this hook to enable your
 favorite minor modes or add key bindings."
 favorite minor modes or add key bindings."
-  :group 'edit-server
-  :type 'hook)
+	:group 'edit-server
+	:type 'hook)
 
 
 ; frame options
 ; frame options
 (defcustom edit-server-new-frame t
 (defcustom edit-server-new-frame t
-  "If not nil, edit each buffer in a new frame (and raise it)."
-  :group 'edit-server
-  :type 'boolean)
+	"If not nil, edit each buffer in a new frame (and raise it)."
+	:group 'edit-server
+	:type 'boolean)
 
 
 (defcustom edit-server-new-frame-alist
 (defcustom edit-server-new-frame-alist
-  '((name . "Emacs TEXTAREA")
-    (width . 80)
-    (height . 25)
-    (minibuffer . t)
-    (menu-bar-lines . t))
-  "Frame parameters for new frames.  See `default-frame-alist' for examples.
+	'((name . "Emacs TEXTAREA")
+		(width . 80)
+		(height . 25)
+		(minibuffer . t)
+		(menu-bar-lines . t))
+	"Frame parameters for new frames.  See `default-frame-alist' for examples.
 If nil, the new frame will use the existing `default-frame-alist' values."
 If nil, the new frame will use the existing `default-frame-alist' values."
-  :group 'edit-server
-  :type '(repeat (cons :format "%v"
-		       (symbol :tag "Parameter")
-		       (sexp :tag "Value"))))
+	:group 'edit-server
+	:type '(repeat (cons :format "%v"
+					 (symbol :tag "Parameter")
+					 (sexp :tag "Value"))))
 
 
 (defcustom edit-server-new-frame-mode-line t
 (defcustom edit-server-new-frame-mode-line t
-  "Show the emacs frame's mode-line if set to t; hide if nil."
-  :group 'edit-server
-  :type 'boolean)
+	"Show the emacs frame's mode-line if set to t; hide if nil."
+	:group 'edit-server
+	:type 'boolean)
 
 
 ;; Vars
 ;; Vars
 (defconst edit-server-process-buffer-name " *edit-server*"
 (defconst edit-server-process-buffer-name " *edit-server*"
-  "Template name of the edit-server process buffers.")
+	"Template name of the edit-server process buffers.")
 
 
 (defconst edit-server-log-buffer-name "*edit-server-log*"
 (defconst edit-server-log-buffer-name "*edit-server-log*"
-  "Template name of the edit-server process buffers.")
+	"Template name of the edit-server process buffers.")
 
 
 (defconst edit-server-edit-buffer-name "TEXTAREA"
 (defconst edit-server-edit-buffer-name "TEXTAREA"
-  "Template name of the edit-server text editing buffers.")
+	"Template name of the edit-server text editing buffers.")
 
 
 (defvar edit-server-proc 'nil
 (defvar edit-server-proc 'nil
-  "Network process associated with the current edit, made local when
+	"Network process associated with the current edit, made local when
  the edit buffer is created")
  the edit buffer is created")
 
 
 (defvar edit-server-frame 'nil
 (defvar edit-server-frame 'nil
-  "The frame created for a new edit-server process, made local when
+	"The frame created for a new edit-server process, made local when
  then edit buffer is created")
  then edit buffer is created")
 
 
 (defvar edit-server-clients '() 
 (defvar edit-server-clients '() 
-  "List of all client processes associated with the server process.")
+	"List of all client processes associated with the server process.")
 
 
 (defvar edit-server-phase nil 
 (defvar edit-server-phase nil 
-  "Symbol indicating the state of the HTTP request parsing.")
+	"Symbol indicating the state of the HTTP request parsing.")
 
 
 (defvar edit-server-received nil 
 (defvar edit-server-received nil 
-  "Number of bytes received so far in the client buffer. 
+	"Number of bytes received so far in the client buffer. 
 Depending on the character encoding, may be different from the buffer length.")
 Depending on the character encoding, may be different from the buffer length.")
 
 
 (defvar edit-server-request nil 
 (defvar edit-server-request nil 
-  "The HTTP request (GET, HEAD, POST) received.")
+	"The HTTP request (GET, HEAD, POST) received.")
 
 
 (defvar edit-server-content-length nil 
 (defvar edit-server-content-length nil 
-  "The value gotten from the HTTP `Content-Length' header.")
+	"The value gotten from the HTTP `Content-Length' header.")
 
 
 (defvar edit-server-url nil 
 (defvar edit-server-url nil 
-  "The value gotten from the HTTP `x-url' header.")
+	"The value gotten from the HTTP `x-url' header.")
 
 
 ;; Mode magic
 ;; Mode magic
 ;
 ;
@@ -141,7 +142,7 @@ Depending on the character encoding, may be different from the buffer length.")
 ;
 ;
 
 
 (define-derived-mode edit-server-text-mode text-mode "Edit Server Text Mode"
 (define-derived-mode edit-server-text-mode text-mode "Edit Server Text Mode"
-  "A derived version of text-mode with a few common Emacs keystrokes
+	"A derived version of text-mode with a few common Emacs keystrokes
 rebound to more functions that can deal with the response to the
 rebound to more functions that can deal with the response to the
 edit-server request.
 edit-server request.
 
 
@@ -151,7 +152,7 @@ to the HTTP client: C-x #, C-x C-s, C-c C-c.
 If any of the above isused with a prefix argument, the
 If any of the above isused with a prefix argument, the
 unmodified text is sent back instead.
 unmodified text is sent back instead.
 "
 "
-  :group 'edit-server)
+	:group 'edit-server)
 (define-key edit-server-text-mode-map (kbd "C-x #") 'edit-server-done)
 (define-key edit-server-text-mode-map (kbd "C-x #") 'edit-server-done)
 (define-key edit-server-text-mode-map (kbd "C-x C-s") 'edit-server-done)
 (define-key edit-server-text-mode-map (kbd "C-x C-s") 'edit-server-done)
 (define-key edit-server-text-mode-map (kbd "C-c C-c") 'edit-server-done)
 (define-key edit-server-text-mode-map (kbd "C-c C-c") 'edit-server-done)
@@ -162,224 +163,224 @@ unmodified text is sent back instead.
 ;
 ;
 
 
 (defun edit-server-start (&optional verbose) 
 (defun edit-server-start (&optional verbose) 
-  "Start the edit server.
+	"Start the edit server.
 
 
 If argument VERBOSE is non-nil, logs all server activity to buffer `*edit-server-log*'.
 If argument VERBOSE is non-nil, logs all server activity to buffer `*edit-server-log*'.
 When called interactivity, a prefix argument will cause it to be verbose.
 When called interactivity, a prefix argument will cause it to be verbose.
 "
 "
-  (interactive "P")
-  (if (or (process-status "edit-server")
-          (null (condition-case err
-                    (make-network-process
-                     :name "edit-server"
-                     :buffer edit-server-process-buffer-name
-                     :family 'ipv4
-                     :host (if edit-server-host
-                               edit-server-host
-                             'local)
-                     :service edit-server-port
-                     :log 'edit-server-accept
-                     :server t
-                     :noquery t)
-                  (file-error nil))))
-      (message "An edit-server process is already running")
-    (setq edit-server-clients '())
-    (if verbose (get-buffer-create edit-server-log-buffer-name))
-    (edit-server-log nil "Created a new edit-server process")))
+	(interactive "P")
+	(if (or (process-status "edit-server")
+					(null (condition-case err
+										(make-network-process
+										 :name "edit-server"
+										 :buffer edit-server-process-buffer-name
+										 :family 'ipv4
+										 :host (if edit-server-host
+															 edit-server-host
+														 'local)
+										 :service edit-server-port
+										 :log 'edit-server-accept
+										 :server t
+										 :noquery t)
+									(file-error nil))))
+			(message "An edit-server process is already running")
+		(setq edit-server-clients '())
+		(if verbose (get-buffer-create edit-server-log-buffer-name))
+		(edit-server-log nil "Created a new edit-server process")))
 
 
 (defun edit-server-stop nil
 (defun edit-server-stop nil
-  "Stop the edit server"
-  (interactive)
-  (while edit-server-clients
-    (edit-server-kill-client (car edit-server-clients))
-    (setq edit-server-clients (cdr edit-server-clients)))
-  (if (process-status "edit-server")
-      (delete-process "edit-server")
-    (message "No edit server running"))
-  (if (get-buffer edit-server-process-buffer-name)
-      (kill-buffer edit-server-process-buffer-name)))
+	"Stop the edit server"
+	(interactive)
+	(while edit-server-clients
+		(edit-server-kill-client (car edit-server-clients))
+		(setq edit-server-clients (cdr edit-server-clients)))
+	(if (process-status "edit-server")
+			(delete-process "edit-server")
+		(message "No edit server running"))
+	(if (get-buffer edit-server-process-buffer-name)
+			(kill-buffer edit-server-process-buffer-name)))
 
 
 (defun edit-server-log (proc fmt &rest args)
 (defun edit-server-log (proc fmt &rest args)
-  "If a `*edit-server-log*' buffer exists, write STRING to it for logging purposes.
+	"If a `*edit-server-log*' buffer exists, write STRING to it for logging purposes.
 If `edit-server-verbose' is non-nil, then STRING is also echoed to the message line."
 If `edit-server-verbose' is non-nil, then STRING is also echoed to the message line."
-  (let ((string (apply 'format fmt args)))
-    (if edit-server-verbose
-        (message string))
-    (if (get-buffer edit-server-log-buffer-name)
-        (with-current-buffer edit-server-log-buffer-name
-          (goto-char (point-max))
-          (insert (current-time-string) 
-                  " " 
-                  (if (processp proc)
-                      (concat 
-                       (buffer-name (process-buffer proc))
-                       ": ")
-                    "") ; nil is not acceptable to 'insert
-                  string)
-          (or (bolp) (newline))))))
+	(let ((string (apply 'format fmt args)))
+		(if edit-server-verbose
+				(message string))
+		(if (get-buffer edit-server-log-buffer-name)
+				(with-current-buffer edit-server-log-buffer-name
+					(goto-char (point-max))
+					(insert (current-time-string) 
+									" " 
+									(if (processp proc)
+											(concat 
+											 (buffer-name (process-buffer proc))
+											 ": ")
+										"") ; nil is not acceptable to 'insert
+									string)
+					(or (bolp) (newline))))))
 
 
 (defun edit-server-accept (server client msg)
 (defun edit-server-accept (server client msg)
-  "Accept a new client connection."
-  (let ((buffer (generate-new-buffer edit-server-process-buffer-name)))
-    (and (fboundp 'set-buffer-multibyte)
-         (set-buffer-multibyte t)) ; djb
-    (buffer-disable-undo buffer)
-    (set-process-buffer client buffer)
-    (set-process-filter client 'edit-server-filter)
-    (set-process-query-on-exit-flag client nil) ; kill-buffer kills the associated process
-    (with-current-buffer buffer
-      (set (make-local-variable 'edit-server-phase) 'wait)
-      (set (make-local-variable 'edit-server-received) 0)
-      (set (make-local-variable 'edit-server-request) nil))
-      (set (make-local-variable 'edit-server-content-length) nil)
-      (set (make-local-variable 'edit-server-url) nil))
-    (add-to-list 'edit-server-clients client)
-    (edit-server-log client msg))
+	"Accept a new client connection."
+	(let ((buffer (generate-new-buffer edit-server-process-buffer-name)))
+		(and (fboundp 'set-buffer-multibyte)
+				 (set-buffer-multibyte t)) ; djb
+		(buffer-disable-undo buffer)
+		(set-process-buffer client buffer)
+		(set-process-filter client 'edit-server-filter)
+		(set-process-query-on-exit-flag client nil) ; kill-buffer kills the associated process
+		(with-current-buffer buffer
+			(set (make-local-variable 'edit-server-phase) 'wait)
+			(set (make-local-variable 'edit-server-received) 0)
+			(set (make-local-variable 'edit-server-request) nil))
+			(set (make-local-variable 'edit-server-content-length) nil)
+			(set (make-local-variable 'edit-server-url) nil))
+		(add-to-list 'edit-server-clients client)
+		(edit-server-log client msg))
 
 
 (defun edit-server-filter (proc string)
 (defun edit-server-filter (proc string)
-  "Process data received from the client."
-  ;; there is no guarantee that data belonging to the same client
-  ;; request will arrive all in one go; therefore, we must accumulate
-  ;; data in the buffer and process it in different phases, which
-  ;; requires us to keep track of the processing state.
-  (with-current-buffer (process-buffer proc)
-    (insert string)
-    (setq edit-server-received 
-          (+ edit-server-received (string-bytes string)))
-    (when (eq edit-server-phase 'wait)
-      ;; look for a complete HTTP request string
-      (save-excursion
-        (goto-char (point-min))
-        (when (re-search-forward "^\\([A-Z]+\\)\\s-+\\(\\S-+\\)\\s-+\\(HTTP/[0-9\.]+\\)\r?\n" nil t)
-          (edit-server-log proc 
-                           "Got HTTP `%s' request, processing in buffer `%s'..." 
-                           (match-string 1) (current-buffer))
-          (setq edit-server-request (match-string 1))
-          (setq edit-server-content-length nil)
-          (setq edit-server-phase 'head))))
-    
-    (when (eq edit-server-phase 'head)
-      ;; look for "Content-length" header
-      (save-excursion
-        (goto-char (point-min))
-        (when (re-search-forward "^Content-Length:\\s-+\\([0-9]+\\)" nil t)
-          (setq edit-server-content-length (string-to-number (match-string 1)))))
-      ;; look for "x-url" header
-      (save-excursion
-        (goto-char (point-min))
-        (when (re-search-forward "^x-url: .*//\\(.*\\)/" nil t)
-          (setq edit-server-url (match-string 1))))
-      ;; look for head/body separator
-      (save-excursion
-        (goto-char (point-min))
-        (when (re-search-forward "\\(\r?\n\\)\\{2\\}" nil t)
-          ;; HTTP headers are pure ASCII (1 char = 1 byte), so we can subtract
-          ;; the buffer position from the count of received bytes
-          (setq edit-server-received
-                (- edit-server-received (- (match-end 0) (point-min))))
-          ;; discard headers - keep only HTTP content in buffer
-          (delete-region (point-min) (match-end 0))
-          (setq edit-server-phase 'body))))
-    
-    (when (eq edit-server-phase 'body)
-      (if (and edit-server-content-length
-               (> edit-server-content-length edit-server-received))
-          (edit-server-log proc 
-                           "Received %d bytes of %d ..." 
-                           edit-server-received edit-server-content-length)
-        ;; all content transferred - process request now
-        (cond
-         ((string= edit-server-request "POST")
-          ;; create editing buffer, and move content to it
-          (edit-server-create-edit-buffer proc))
-         (t
-          ;; send 200 OK response to any other request
-          (edit-server-send-response proc "edit-server is running.\n" t)))
-        ;; wait for another connection to arrive
-        (setq edit-server-received 0)
-        (setq edit-server-phase 'wait)))))
+	"Process data received from the client."
+	;; there is no guarantee that data belonging to the same client
+	;; request will arrive all in one go; therefore, we must accumulate
+	;; data in the buffer and process it in different phases, which
+	;; requires us to keep track of the processing state.
+	(with-current-buffer (process-buffer proc)
+		(insert string)
+		(setq edit-server-received 
+					(+ edit-server-received (string-bytes string)))
+		(when (eq edit-server-phase 'wait)
+			;; look for a complete HTTP request string
+			(save-excursion
+				(goto-char (point-min))
+				(when (re-search-forward "^\\([A-Z]+\\)\\s-+\\(\\S-+\\)\\s-+\\(HTTP/[0-9\.]+\\)\r?\n" nil t)
+					(edit-server-log proc 
+													 "Got HTTP `%s' request, processing in buffer `%s'..." 
+													 (match-string 1) (current-buffer))
+					(setq edit-server-request (match-string 1))
+					(setq edit-server-content-length nil)
+					(setq edit-server-phase 'head))))
+		
+		(when (eq edit-server-phase 'head)
+			;; look for "Content-length" header
+			(save-excursion
+				(goto-char (point-min))
+				(when (re-search-forward "^Content-Length:\\s-+\\([0-9]+\\)" nil t)
+					(setq edit-server-content-length (string-to-number (match-string 1)))))
+			;; look for "x-url" header
+			(save-excursion
+				(goto-char (point-min))
+				(when (re-search-forward "^x-url: .*//\\(.*\\)/" nil t)
+					(setq edit-server-url (match-string 1))))
+			;; look for head/body separator
+			(save-excursion
+				(goto-char (point-min))
+				(when (re-search-forward "\\(\r?\n\\)\\{2\\}" nil t)
+					;; HTTP headers are pure ASCII (1 char = 1 byte), so we can subtract
+					;; the buffer position from the count of received bytes
+					(setq edit-server-received
+								(- edit-server-received (- (match-end 0) (point-min))))
+					;; discard headers - keep only HTTP content in buffer
+					(delete-region (point-min) (match-end 0))
+					(setq edit-server-phase 'body))))
+		
+		(when (eq edit-server-phase 'body)
+			(if (and edit-server-content-length
+							 (> edit-server-content-length edit-server-received))
+					(edit-server-log proc 
+													 "Received %d bytes of %d ..." 
+													 edit-server-received edit-server-content-length)
+				;; all content transferred - process request now
+				(cond
+				 ((string= edit-server-request "POST")
+					;; create editing buffer, and move content to it
+					(edit-server-create-edit-buffer proc))
+				 (t
+					;; send 200 OK response to any other request
+					(edit-server-send-response proc "edit-server is running.\n" t)))
+				;; wait for another connection to arrive
+				(setq edit-server-received 0)
+				(setq edit-server-phase 'wait)))))
 
 
 (defun edit-server-create-frame(buffer)
 (defun edit-server-create-frame(buffer)
-  "Create a frame for the edit server"
-  (if edit-server-new-frame
-      (let ((new-frame
-	     (if (featurep 'aquamacs)
-                 (make-frame edit-server-new-frame-alist)
-               (make-frame-on-display (getenv "DISPLAY")
-                                      edit-server-new-frame-alist))))
+	"Create a frame for the edit server"
+	(if edit-server-new-frame
+			(let ((new-frame
+			 (if (featurep 'aquamacs)
+								 (make-frame edit-server-new-frame-alist)
+							 (make-frame-on-display (getenv "DISPLAY")
+																			edit-server-new-frame-alist))))
 	(if (not edit-server-new-frame-mode-line)
 	(if (not edit-server-new-frame-mode-line)
-            (setq mode-line-format nil))
+						(setq mode-line-format nil))
 	(select-frame new-frame)
 	(select-frame new-frame)
 	(if (and (eq window-system 'x)
 	(if (and (eq window-system 'x)
 		 (fboundp 'x-send-client-message))
 		 (fboundp 'x-send-client-message))
-	    (x-send-client-message nil 0 nil
-				   "_NET_ACTIVE_WINDOW" 32
-				   '(1 0 0)))
+			(x-send-client-message nil 0 nil
+					 "_NET_ACTIVE_WINDOW" 32
+					 '(1 0 0)))
 	(raise-frame new-frame)
 	(raise-frame new-frame)
-        (set-window-buffer (frame-selected-window new-frame) buffer)
+				(set-window-buffer (frame-selected-window new-frame) buffer)
 	new-frame)
 	new-frame)
-    (pop-to-buffer buffer)
-    nil))
+		(pop-to-buffer buffer)
+		nil))
 
 
 (defun edit-server-create-edit-buffer(proc)
 (defun edit-server-create-edit-buffer(proc)
-  "Create an edit buffer, place content in it and save the network
-  process for the final call back"
-  (let ((buffer (generate-new-buffer (if edit-server-url
+	"Create an edit buffer, place content in it and save the network
+	process for the final call back"
+	(let ((buffer (generate-new-buffer (if edit-server-url
 					 edit-server-url
 					 edit-server-url
-				       edit-server-edit-buffer-name))))
-    (with-current-buffer buffer
-      (and (fboundp 'set-buffer-multibyte)
-           (set-buffer-multibyte t))) ; djb
-    (copy-to-buffer buffer (point-min) (point-max))
-    (with-current-buffer buffer
-      (not-modified)
-      (edit-server-text-mode)
-      (add-hook 'kill-buffer-hook 'edit-server-abort* nil t)
-      (buffer-enable-undo)
-      (set (make-local-variable 'edit-server-proc) proc)
-      (set (make-local-variable 'edit-server-frame)
-	   (edit-server-create-frame buffer))
-      (run-hooks 'edit-server-start-hook))))
+							 edit-server-edit-buffer-name))))
+		(with-current-buffer buffer
+			(and (fboundp 'set-buffer-multibyte)
+					 (set-buffer-multibyte t))) ; djb
+		(copy-to-buffer buffer (point-min) (point-max))
+		(with-current-buffer buffer
+			(not-modified)
+			(edit-server-text-mode)
+			(add-hook 'kill-buffer-hook 'edit-server-abort* nil t)
+			(buffer-enable-undo)
+			(set (make-local-variable 'edit-server-proc) proc)
+			(set (make-local-variable 'edit-server-frame)
+		 (edit-server-create-frame buffer))
+			(run-hooks 'edit-server-start-hook))))
 
 
 (defun edit-server-send-response (proc &optional body close)
 (defun edit-server-send-response (proc &optional body close)
-  "Send an HTTP 200 OK response back to process PROC.
+	"Send an HTTP 200 OK response back to process PROC.
 Optional second argument BODY specifies the response content:
 Optional second argument BODY specifies the response content:
-  - If nil, the HTTP response will have null content.
-  - If a string, the string is sent as response content.
-  - Any other value will cause the contents of the current 
-    buffer to be sent.
+	- If nil, the HTTP response will have null content.
+	- If a string, the string is sent as response content.
+	- Any other value will cause the contents of the current 
+		buffer to be sent.
 If optional third argument CLOSE is non-nil, then process PROC
 If optional third argument CLOSE is non-nil, then process PROC
 and its buffer are killed with `edit-server-kill-client'."
 and its buffer are killed with `edit-server-kill-client'."
-  (interactive)
-  (if (processp proc)
-      (let ((response-header (concat
-                          "HTTP/1.0 200 OK\n"
-                          (format "Server: Emacs/%s\n" emacs-version)
-                          "Date: "
-                          (format-time-string
-                           "%a, %d %b %Y %H:%M:%S GMT\n"
-                           (current-time)))))
-        (process-send-string proc response-header)
-        (process-send-string proc "\n")
-        (cond
-         ((stringp body) (process-send-string proc body))
-         ((not body) nil)
-         (t (process-send-region proc (point-min) (point-max))))
-        (process-send-eof proc)
-        (if close 
-            (edit-server-kill-client proc))
-        (edit-server-log proc "Editing done, sent HTTP OK response."))
-    (message "edit-server-send-response: invalid proc (bug?)")))
+	(interactive)
+	(if (processp proc)
+			(let ((response-header (concat
+													"HTTP/1.0 200 OK\n"
+													(format "Server: Emacs/%s\n" emacs-version)
+													"Date: "
+													(format-time-string
+													 "%a, %d %b %Y %H:%M:%S GMT\n"
+													 (current-time)))))
+				(process-send-string proc response-header)
+				(process-send-string proc "\n")
+				(cond
+				 ((stringp body) (process-send-string proc body))
+				 ((not body) nil)
+				 (t (process-send-region proc (point-min) (point-max))))
+				(process-send-eof proc)
+				(if close 
+						(edit-server-kill-client proc))
+				(edit-server-log proc "Editing done, sent HTTP OK response."))
+		(message "edit-server-send-response: invalid proc (bug?)")))
 
 
 (defun edit-server-kill-client (proc)
 (defun edit-server-kill-client (proc)
-  "Kill client process PROC and remove it from the list."
-  (let ((procbuf (process-buffer proc)))
-    (delete-process proc)
-    (kill-buffer procbuf)
-    (setq edit-server-clients (delq proc edit-server-clients))))
+	"Kill client process PROC and remove it from the list."
+	(let ((procbuf (process-buffer proc)))
+		(delete-process proc)
+		(kill-buffer procbuf)
+		(setq edit-server-clients (delq proc edit-server-clients))))
 
 
 (defun edit-server-done (&optional abort nokill)
 (defun edit-server-done (&optional abort nokill)
-  "Finish editing: send HTTP response back, close client and editing buffers.
+	"Finish editing: send HTTP response back, close client and editing buffers.
 
 
 The current contents of the buffer are sent back to the HTTP
 The current contents of the buffer are sent back to the HTTP
 client, unless argument ABORT is non-nil, in which case then the
 client, unless argument ABORT is non-nil, in which case then the
@@ -388,47 +389,47 @@ If optional second argument NOKILL is non-nil, then the editing
 buffer is not killed.
 buffer is not killed.
 
 
 When called interactively, use prefix arg to abort editing."
 When called interactively, use prefix arg to abort editing."
-  (interactive "P")
-  ;; Do nothing if the connection is closed by the browser (tab killed, etc.)
-  (unless (eq (process-status edit-server-proc) 'closed)
-    (let ((buffer (current-buffer))
-           (proc edit-server-proc)
-           (procbuf (process-buffer edit-server-proc)))
-      ;; edit-server-* vars are buffer-local, so they must be used before issuing kill-buffer
-      (if abort
-        ;; send back original content
-        (with-current-buffer procbuf
-          (run-hooks 'edit-server-done-hook)
-          (edit-server-send-response proc t))
-        ;; send back edited content
-        (save-restriction
-          (widen)
-          (buffer-disable-undo)
-          ;; ensure any format encoding is done (like longlines)
-          (dolist (format buffer-file-format)
-            (format-encode-region (point-min) (point-max) format))
-          ;; send back
-          (run-hooks 'edit-server-done-hook)
-          (edit-server-send-response edit-server-proc t)
-          ;; restore formats (only useful if we keep the buffer)
-          (dolist (format buffer-file-format)
-            (format-decode-region (point-min) (point-max) format))
-          (buffer-enable-undo)))
-      (if edit-server-frame (delete-frame edit-server-frame))
-      ;; delete-frame may change the current buffer
-      (unless nokill (kill-buffer buffer))
-      (edit-server-kill-client proc))))
+	(interactive "P")
+	;; Do nothing if the connection is closed by the browser (tab killed, etc.)
+	(unless (eq (process-status edit-server-proc) 'closed)
+		(let ((buffer (current-buffer))
+					 (proc edit-server-proc)
+					 (procbuf (process-buffer edit-server-proc)))
+			;; edit-server-* vars are buffer-local, so they must be used before issuing kill-buffer
+			(if abort
+				;; send back original content
+				(with-current-buffer procbuf
+					(run-hooks 'edit-server-done-hook)
+					(edit-server-send-response proc t))
+				;; send back edited content
+				(save-restriction
+					(widen)
+					(buffer-disable-undo)
+					;; ensure any format encoding is done (like longlines)
+					(dolist (format buffer-file-format)
+						(format-encode-region (point-min) (point-max) format))
+					;; send back
+					(run-hooks 'edit-server-done-hook)
+					(edit-server-send-response edit-server-proc t)
+					;; restore formats (only useful if we keep the buffer)
+					(dolist (format buffer-file-format)
+						(format-decode-region (point-min) (point-max) format))
+					(buffer-enable-undo)))
+			(if edit-server-frame (delete-frame edit-server-frame))
+			;; delete-frame may change the current buffer
+			(unless nokill (kill-buffer buffer))
+			(edit-server-kill-client proc))))
 
 
 (defun edit-server-abort ()
 (defun edit-server-abort ()
-  "Discard editing and send the original text back to the browser."
-  (interactive)
-  (edit-server-done t))
+	"Discard editing and send the original text back to the browser."
+	(interactive)
+	(edit-server-done t))
 
 
 (defun edit-server-abort* ()
 (defun edit-server-abort* ()
-  "Discard editing and send the original text back to the browser,
+	"Discard editing and send the original text back to the browser,
 but don't kill the editing buffer."
 but don't kill the editing buffer."
-  (interactive)
-  (edit-server-done t t))
+	(interactive)
+	(edit-server-done t t))
 
 
 (provide 'edit-server)
 (provide 'edit-server)
 
 

+ 132 - 131
pycl.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
 #!/usr/bin/env python
+# -*- tab-width:2; indent-tabs-mode:t -*- vim: set noet ts=2:
 
 
 # Copyright (C) 2009  David Hilley <davidhi@cc.gatech.edu>
 # Copyright (C) 2009  David Hilley <davidhi@cc.gatech.edu>
 # Copyright (C) 2010  Matt DeVuyst <mdevuyst@gmail.com>
 # Copyright (C) 2010  Matt DeVuyst <mdevuyst@gmail.com>
@@ -10,7 +11,7 @@
 #
 #
 # This program is distributed in the hope that it will be useful,
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 # GNU General Public License for more details.
 # GNU General Public License for more details.
 #
 #
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
@@ -32,147 +33,147 @@ temp_has_delete=True
 processes = {}
 processes = {}
 
 
 class Handler(BaseHTTPRequestHandler):
 class Handler(BaseHTTPRequestHandler):
-    global temp_has_delete
+	global temp_has_delete
 
 
-    def do_GET(self):
+	def do_GET(self):
 	if self.path == '/status':
 	if self.path == '/status':
 		self.send_response(200)
 		self.send_response(200)
 		self.send_header('Content-Type', 'text/plain; charset=utf-8')
 		self.send_header('Content-Type', 'text/plain; charset=utf-8')
 		self.end_headers()
 		self.end_headers()
 		self.wfile.write('edit-server is running.\n')
 		self.wfile.write('edit-server is running.\n')
 		return
 		return
-        self.send_error(404, "GET Not Found: %s" % self.path)
-
-    def do_POST(self):
-        global processes
-        try:
-            (content, params) = cgi.parse_header(self.headers.
-                                                 getheader('content-type'))
-
-            clength = 0
-            cl = self.headers.getheader('content-length')
-
-            if cl != None:
-                clength = int(cl)
-            else:
-                self.send_response(411)
-                self.end_headers()
-                return
-
-            body = self.rfile.read(clength)
-            print body
-
-            l = [s for s in self.path.split('/') if s]
-            print l
-
-            existing_file = self.headers.getheader('x-file')
-
-            # write text into file
-            if not existing_file or existing_file == "undefined":
-                existing = False
-                url = self.headers.getheader('x-url')
-                print "url:", url
-                prefix = "chrome_"
-                if url:
-                    prefix += re.sub("[^.\w]", "_", re.sub("^.*?//","",url))
-                prefix += "_"
-                if temp_has_delete==True:
-                    f = tempfile.NamedTemporaryFile(
-                            delete=False, prefix=prefix, suffix='.txt')
-                    fname = f.name
-                else:
-                    tf = tempfile.mkstemp(prefix=prefix, suffix='.txt')
-                    f = os.fdopen(tf[0],"w")
-                    fname = tf[1]
-                print "Opening new file ", fname
-            else:
-                existing = True
-                p = processes[existing_file]
-                print "Opening existing file ", existing_file
-                f = open(existing_file, "w")
-                fname = existing_file
-
-            f.write(body)
-            f.close()
-            last_mod_time = os.stat(fname)[stat.ST_MTIME]
-
-            if not existing:
-                # spawn editor...
-                print "Spawning editor... ", fname
-
-                cmd = self.editor.split(",")
-                cmd.append(fname)
-                p = subprocess.Popen(cmd, close_fds=True)
-                processes[fname] = p
-
-            saved = False
-            rc = None
-            while (True):
-                time.sleep(1)
-                rc = p.poll()
-                if rc != None: break
-                mod_time = os.stat(fname)[stat.ST_MTIME]
-                if mod_time != last_mod_time:
-                    print "new mod time:", mod_time, " last:", last_mod_time
-                    last_mod_time = mod_time
-                    saved = True
-                if saved: break
-
-            if saved or not rc:
-                    self.send_response(200)
-
-                    f = file(fname, 'r')
-                    s = f.read()
-                    f.close()
-            else:
-                    if rc > 0:
-                            msg = 'text editor returned %d' % rc
-                    elif rc < 0:
-                            msg = 'text editor died on signal %d' % -rc
-                    self.send_error(404, msg)
-
-            if saved:
-                self.send_header('x-open', "true")
-            else:
-                try:
-                    os.unlink(fname)
-                except :
-                    print "Unable to unlink:", fname
-                    pass
-
-            self.send_header('x-file', fname)
-            self.end_headers()
-            self.wfile.write(s)
-        except :
-            print "Error: ", sys.exc_info()[0]
-            self.send_error(404, "Not Found: %s" % self.path)
+	self.send_error(404, "GET Not Found: %s" % self.path)
+
+	def do_POST(self):
+		global processes
+		try:
+			(content, params) = cgi.parse_header(self.headers.
+												 getheader('content-type'))
+
+			clength = 0
+			cl = self.headers.getheader('content-length')
+
+			if cl != None:
+				clength = int(cl)
+			else:
+				self.send_response(411)
+				self.end_headers()
+				return
+
+			body = self.rfile.read(clength)
+			print body
+
+			l = [s for s in self.path.split('/') if s]
+			print l
+
+			existing_file = self.headers.getheader('x-file')
+
+			# write text into file
+			if not existing_file or existing_file == "undefined":
+				existing = False
+				url = self.headers.getheader('x-url')
+				print "url:", url
+				prefix = "chrome_"
+				if url:
+					prefix += re.sub("[^.\w]", "_", re.sub("^.*?//","",url))
+				prefix += "_"
+				if temp_has_delete==True:
+					f = tempfile.NamedTemporaryFile(
+							delete=False, prefix=prefix, suffix='.txt')
+					fname = f.name
+				else:
+					tf = tempfile.mkstemp(prefix=prefix, suffix='.txt')
+					f = os.fdopen(tf[0],"w")
+					fname = tf[1]
+				print "Opening new file ", fname
+			else:
+				existing = True
+				p = processes[existing_file]
+				print "Opening existing file ", existing_file
+				f = open(existing_file, "w")
+				fname = existing_file
+
+			f.write(body)
+			f.close()
+			last_mod_time = os.stat(fname)[stat.ST_MTIME]
+
+			if not existing:
+				# spawn editor...
+				print "Spawning editor... ", fname
+
+				cmd = self.editor.split(",")
+				cmd.append(fname)
+				p = subprocess.Popen(cmd, close_fds=True)
+				processes[fname] = p
+
+			saved = False
+			rc = None
+			while (True):
+				time.sleep(1)
+				rc = p.poll()
+				if rc != None: break
+				mod_time = os.stat(fname)[stat.ST_MTIME]
+				if mod_time != last_mod_time:
+					print "new mod time:", mod_time, " last:", last_mod_time
+					last_mod_time = mod_time
+					saved = True
+				if saved: break
+
+			if saved or not rc:
+					self.send_response(200)
+
+					f = file(fname, 'r')
+					s = f.read()
+					f.close()
+			else:
+					if rc > 0:
+							msg = 'text editor returned %d' % rc
+					elif rc < 0:
+							msg = 'text editor died on signal %d' % -rc
+					self.send_error(404, msg)
+
+			if saved:
+				self.send_header('x-open', "true")
+			else:
+				try:
+					os.unlink(fname)
+				except :
+					print "Unable to unlink:", fname
+					pass
+
+			self.send_header('x-file', fname)
+			self.end_headers()
+			self.wfile.write(s)
+		except :
+			print "Error: ", sys.exc_info()[0]
+			self.send_error(404, "Not Found: %s" % self.path)
 
 
 def parse_options():
 def parse_options():
-    parser = optparse.OptionParser()
-    parser.add_option(
-        "-p", "--port", type="int", dest="port", default=_default_port,
-        help="port number to listen on (default: " + str(_default_port) + ")")
-    parser.add_option(
-        "-e", "--editor", dest="editor", default=_default_editor,
-        help='text editor to spawn (default: "' + _default_editor + '")')
-    return parser.parse_args()[0]
+	parser = optparse.OptionParser()
+	parser.add_option(
+		"-p", "--port", type="int", dest="port", default=_default_port,
+		help="port number to listen on (default: " + str(_default_port) + ")")
+	parser.add_option(
+		"-e", "--editor", dest="editor", default=_default_editor,
+		help='text editor to spawn (default: "' + _default_editor + '")')
+	return parser.parse_args()[0]
 
 
 def main():
 def main():
-    global temp_has_delete
-    import platform
-    t = platform.python_version_tuple()
-    if int(t[0]) == 2 and int(t[1]) < 6:
-        temp_has_delete = False;
-        print "Handling lack of delete for NamedTemporaryFile:", temp_has_delete
-    options = parse_options()
-    Handler.editor = options.editor
-    try:
-        httpserv = HTTPServer(('localhost', options.port), Handler)
-        httpserv.table = {}
-        httpserv.serve_forever()
-    except KeyboardInterrupt:
-        httpserv.socket.close()
+	global temp_has_delete
+	import platform
+	t = platform.python_version_tuple()
+	if int(t[0]) == 2 and int(t[1]) < 6:
+		temp_has_delete = False;
+		print "Handling lack of delete for NamedTemporaryFile:", temp_has_delete
+	options = parse_options()
+	Handler.editor = options.editor
+	try:
+		httpserv = HTTPServer(('localhost', options.port), Handler)
+		httpserv.table = {}
+		httpserv.serve_forever()
+	except KeyboardInterrupt:
+		httpserv.socket.close()
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    main()
+	main()