edit_server.el 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;;
  2. ;; Emacs edit-server
  3. ;;
  4. ;; This provides an edit server to respond to requests from the Chrome
  5. ;; Emacs Chrome plugin. Based on
  6. ;; http://www.emacswiki.org/emacs/EmacsEchoServer to start with.
  7. ;;
  8. ;; (C) 2009 Alex Bennee (alex@bennee.com)
  9. ;; Licensed under GPLv3
  10. ;;
  11. ;;
  12. (defvar edit-server-port 9292
  13. "Port the edit server listens too")
  14. (defvar edit-server-clients '()
  15. "alist where KEY is a client process and VALUE is the string")
  16. (defvar edit-server-current-proc 'nil
  17. "Network process associated with the current edit, made local when
  18. the edit buffer is create")
  19. (defun edit-server-start nil
  20. "Start the edit server"
  21. (interactive)
  22. (unless (process-status "edit-server")
  23. (make-network-process
  24. :name "edit-server"
  25. :buffer "*edit-server*"
  26. :family 'ipv4
  27. :host 'local ; only listen to local connections
  28. :service edit-server-port
  29. :filter 'edit-server-filter
  30. :server 't)
  31. (setq edit-server-clients '())))
  32. (defun edit-server-stop nil
  33. "Stop the edit server"
  34. (interactive)
  35. (while edit-server-clients
  36. (delete-process (car (car edit-server-clients)))
  37. (setq edit-server-clients (cdr edit-server-clients)))
  38. (delete-process "edit-server"))
  39. (defun edit-server-filter (proc string)
  40. "Called each time something connects to the edit server"
  41. (message (format "edit-server-filter: got %s" string))
  42. (let ((pending (assoc proc edit-server-clients))
  43. message
  44. index)
  45. ;;create entry if required
  46. (unless pending
  47. (setq edir-server-clients (cons (cons proc "") edit-server-clients))
  48. (setq pending (assoc proc edit-server-clients)))
  49. ;;Get the content from the headers, we don't actually much care
  50. ;;about the headers for now. I suspect this would break on Windows
  51. (let ((content (cdr (split-string http " \n \n"))))
  52. (edit-server-create-edit-buffer proc content))))
  53. (defun edit-server-create-edit-buffer(proc string)
  54. "Create an edit buffer, place content in it and setup the call
  55. backs"
  56. (switch-to-buffer "edit-text-buffer")
  57. (insert 'string)
  58. (set (make-variable-buffer-local edit-server-current-proc) proc)
  59. (local-set-key (kbd "C-x k") 'edit-server-done)
  60. (local-set-key ((kbd "C-x C-s") 'edit-server-done)))
  61. ;
  62. ; HTTP/1.0 200 OK
  63. ; Server: BaseHTTP/0.3 Python/2.6.4
  64. ; Date: Fri, 18 Dec 2009 19:00:28 GMT
  65. ;
  66. ; This is a test
  67. ; For the text
  68. ;
  69. (defun edit-server-done()
  70. "Once someone is done with editing their text edit-server-done is
  71. called and the response is sent back to the browser"
  72. (interactive)
  73. (message "edit-server-done")
  74. (let (proc edit-server-current-proc)
  75. (process-send-string proc "HTTP/1.0 200 OK \n")
  76. (process-send-string proc "Server: Emacs \n")
  77. (process-send-string proc " \n")
  78. (process-send-string proc (buffer-string))))
  79. (defun edit-server-sentinel (proc msg)
  80. (delq proc edit-server-clients)
  81. (message (format "client %s has quit" proc)))