edit_server.el 2.9 KB

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