-;;
-;; Emacs edit-server
-;;
+;;; edit-server.el --- server that responds to edit requests from Chrome
+
+;; Copyright (C) 2009-2011  Alex Bennee
+;; Copyright (C) 2010-2011  Riccardo Murri
+
+;; Author: Alex Bennee <alex@bennee.com>
+;; Maintainer: Alex Bennee <alex@bennee.com>
+;; Version: 1.10
+;; Homepage: https://github.com/stsquad/emacs_chrome
+
+;; This file is not part of GNU Emacs.
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
 ;; This provides an edit server to respond to requests from the Chrome
 ;; Emacs Chrome plugin. This is my first attempt at doing something
 ;; with sockets in Emacs. I based it on the following examples:
 ;;
-;; http://www.emacswiki.org/emacs/EmacsEchoServer
-;; http://nullprogram.com/blog/2009/05/17/
+;;   http://www.emacswiki.org/emacs/EmacsEchoServer
+;;   http://nullprogram.com/blog/2009/05/17/
 ;;
 ;; To use it ensure the file is in your load-path and add something
 ;; like the following examples to your .emacs:

		       (symbol :tag "Parameter")
		       (sexp :tag "Value"))))

(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)

;; Vars
(defconst edit-server-process-buffer-name " *edit-server*"
  "Template name of the edit-server process buffers.")


  ;; 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)))
      (when 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))))

;;
;; There are a couple of options for handling the save
;; action. The intent is to preserve data but not finish
;; editing. There are a couple of approaches that could
;; be taken:
;;  a) Use the iterative edit-server option (send a buffer
;;     back to the client which then returns new request
;;     to continue the session).
;;  b) Back-up the edit session to a file
;;  c) Save the current buffer to the kill-ring
;;
;; I've attempted to do a) a couple of times but I keep running
;; into problems which I think are emacs bugs. So for now I'll
;; just push the current buffer to the kill-ring.

(defun edit-server-save ()
  "Save the current state of the edit buffer."
  (interactive)
  (save-restriction
    (widen)
    (buffer-disable-undo)
    (copy-region-as-kill (point-min) (point-max))
    (buffer-enable-undo)))

(defun edit-server-abort ()
  "Discard editing and send the original text back to the browser."
