edit_server.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;
  2. ;; Emacs edit-server
  3. ;;
  4. ;; This provides an edit server to respond to requests from the Chrome
  5. ;; Emacs Chrome plugin. This is my first attempt at doing something
  6. ;; with sockets in Emacs. I based it on the following examples:
  7. ;;
  8. ;; http://www.emacswiki.org/emacs/EmacsEchoServer
  9. ;; http://nullprogram.com/blog/2009/05/17/
  10. ;;
  11. ;; (C) 2009 Alex Bennee (alex@bennee.com)
  12. ;; Licensed under GPLv3
  13. ;;
  14. ;;
  15. ; still debugging
  16. (setq debug-on-error 't)
  17. (setq edebug-all-defs 't)
  18. ;; Vars
  19. (defvar edit-server-port 9292
  20. "Port the edit server listens too")
  21. (defvar edit-server-clients '()
  22. "alist where KEY is a client process and VALUE is the string")
  23. (defvar edit-server-current-proc 'nil
  24. "Network process associated with the current edit, made local when
  25. the edit buffer is create")
  26. (defun edit-server-start nil
  27. "Start the edit server"
  28. (interactive)
  29. (unless (process-status "edit-server")
  30. (make-network-process
  31. :name "edit-server"
  32. :buffer "*edit-server*"
  33. :family 'ipv4
  34. :host 'local ; only listen to local connections
  35. :service edit-server-port
  36. :filter 'edit-server-filter
  37. :server 't)
  38. (setq edit-server-clients '())))
  39. (defun edit-server-stop nil
  40. "Stop the edit server"
  41. (interactive)
  42. (while edit-server-clients
  43. (delete-process (car (car edit-server-clients)))
  44. (setq edit-server-clients (cdr edit-server-clients)))
  45. (delete-process "edit-server"))
  46. (defun edit-server-filter (proc string)
  47. "Called each time something connects to the edit server"
  48. (message (format "edit-server-filter: got %s" string))
  49. (let ((pending (assoc proc edit-server-clients))
  50. message
  51. index)
  52. ;;create entry if required
  53. (unless pending
  54. (setq edir-server-clients (cons (cons proc "") edit-server-clients))
  55. (setq pending (assoc proc edit-server-clients)))
  56. ;;Get the content from the headers, we don't actually much care
  57. ;;about the headers for now. I suspect this would break on Windows
  58. ; (let ((content (cdr (split-string string " \n \n"))))
  59. ; (edit-server-create-edit-buffer proc content))))
  60. (edit-server-send-response proc "You got a response")))
  61. (defun edit-server-create-edit-buffer(proc string)
  62. "Create an edit buffer, place content in it and setup the call
  63. backs"
  64. (switch-to-buffer "edit-text-buffer")
  65. (set (make-local-variable 'edit-server-current-proc) proc)
  66. ; Can't do this, affects all buffers of same major mode, will need to
  67. ; create a special mode to do this.
  68. ; (local-set-key (kbd "C-x k") 'edit-server-done)
  69. ; (local-set-key (kbd "C-x C-s") 'edit-server-done)
  70. (insert string))
  71. ;
  72. ; HTTP/1.0 200 OK
  73. ; Server: BaseHTTP/0.3 Python/2.6.4
  74. ; Date: Fri, 18 Dec 2009 19:00:28 GMT
  75. ;
  76. ; This is a test
  77. ; For the text
  78. ;
  79. (defun edit-server-send-response (proc string)
  80. "Send a response back to the calling process with a string"
  81. (interactive)
  82. (message "edit-server-send-response")
  83. (let ((response-header (concat
  84. "HTTP/1.0 200 OK\n"
  85. "Server: Emacs\n"
  86. "Date: "
  87. (format-time-string
  88. "%a, %d %b %Y %H:%M:%S GMT\n"
  89. (current-time)))))
  90. (process-send-string proc response-header)
  91. (process-send-string proc "\n")
  92. (process-send-string proc string)
  93. (process-send-eof proc)))
  94. (defun edit-server-done()
  95. "Once someone is done with editing their text edit-server-done is
  96. called and the response is sent back to the browser"
  97. (interactive)
  98. (edit-server-send-response edit-server-current-proc (buffer-string)))
  99. ; What do I need this for?
  100. (defun edit-server-sentinel (proc msg)
  101. (delq proc edit-server-clients)
  102. (message (format "client %s has quit" proc)))