edit_server.el 3.0 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. 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-current-proc 'nil
  22. "Network process associated with the current edit, made local when
  23. the edit buffer is create")
  24. (defun edit-server-start nil
  25. "Start the edit server"
  26. (interactive)
  27. (unless (process-status "edit-server")
  28. (make-network-process
  29. :name "edit-server"
  30. :buffer "*edit-server*"
  31. :family 'ipv4
  32. :host 'local ; only listen to local connections
  33. :service edit-server-port
  34. :filter 'edit-server-filter
  35. :server 't)))
  36. (defun edit-server-stop nil
  37. "Stop the edit server"
  38. (interactive)
  39. (delete-process "edit-server"))
  40. (defun edit-server-filter (proc request)
  41. "Called each time something connects to the edit server"
  42. (message (format "edit-server-filter: got %sEOF" request))
  43. ;; Get the content from the headers, we don't actually much care
  44. ;; about the headers for now. I suspect this would break on Windows
  45. ;;
  46. ;; As we split on \n\n we need to re-assemble to take into account
  47. ;; any multiple new lines in our content part.
  48. (let* ((after-headers (cdr (split-string request " \n \n")))
  49. (content (car after-headers))
  50. (rest (cdr after-headers)))
  51. (if rest
  52. (dolist (x rest)
  53. (setq content (concat content "\n\n" x))))
  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. ; Can't do this, affects all buffers of same major mode, will need to
  61. ; create a special mode to do this.
  62. ; (local-set-key (kbd "C-x k") 'edit-server-done)
  63. ; (local-set-key (kbd "C-x C-s") 'edit-server-done)
  64. (insert string))
  65. ;
  66. ; Send the response back to the browser as a properly formed
  67. ; HTTP/1.0 200 OK message
  68. ;
  69. (defun edit-server-send-response (proc string)
  70. "Send a response back to the calling process with a string"
  71. (interactive)
  72. (message "edit-server-send-response")
  73. (let ((response-header (concat
  74. "HTTP/1.0 200 OK\n"
  75. "Server: Emacs\n"
  76. "Date: "
  77. (format-time-string
  78. "%a, %d %b %Y %H:%M:%S GMT\n"
  79. (current-time)))))
  80. (process-send-string proc response-header)
  81. (process-send-string proc "\n")
  82. (process-send-string proc string)
  83. (process-send-eof proc)))
  84. (defun edit-server-done()
  85. "Once someone is done with editing their text edit-server-done is
  86. called and the response is sent back to the browser"
  87. (interactive)
  88. (edit-server-send-response edit-server-current-proc (buffer-string)))