edit-server.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. ;; -*- tab-width:2; indent-tabs-mode:t -*- vim: set noet ts=2:
  2. ;;
  3. ;; Emacs edit-server
  4. ;;
  5. ;; This provides an edit server to respond to requests from the Chrome
  6. ;; Emacs Chrome plugin. This is my first attempt at doing something
  7. ;; with sockets in Emacs. I based it on the following examples:
  8. ;;
  9. ;; http://www.emacswiki.org/emacs/EmacsEchoServer
  10. ;; http://nullprogram.com/blog/2009/05/17/
  11. ;;
  12. ;; To use it ensure the file is in your load-path and add something
  13. ;; like the following examples to your .emacs:
  14. ;;
  15. ;; To open pages for editing in new buffers in your existing Emacs
  16. ;; instance:
  17. ;;
  18. ;; (if (locate-library "edit-server")
  19. ;; (progn
  20. ;; (require 'edit-server)
  21. ;; (setq edit-server-new-frame nil)
  22. ;; (edit-server-start)))
  23. ;;
  24. ;; To open pages for editing in new frames using a running emacs
  25. ;; started in --daemon mode:
  26. ;;
  27. ;; (if (and (daemonp) (locate-library "edit-server"))
  28. ;; (progn
  29. ;; (require 'edit-server)
  30. ;; (edit-server-start)))
  31. ;;
  32. ;; Buffers are edited in `text-mode' by default;
  33. ;; to use a different mode, set it in `edit-server-start-hook'.
  34. ;; For example:
  35. ;;
  36. ;; (add-hook 'edit-server-start-hook
  37. ;; (lambda ()
  38. ;; (if (string-match "github.com" (buffer-name))
  39. ;; (markdown-mode))))
  40. ;;
  41. ;; (C) 2009 Alex Bennee (alex@bennee.com)
  42. ;; (C) 2010 Riccardo Murri (riccardo.murri@gmail.com)
  43. ;;
  44. ;; Licensed under GPLv3
  45. ;;
  46. ;; uncomment to debug
  47. ;(setq debug-on-error 't)
  48. ;(setq edebug-all-defs 't)
  49. (if (not (featurep 'make-network-process))
  50. (error "Incompatible version of [X]Emacs - lacks make-network-process"))
  51. ;; Customization
  52. (defcustom edit-server-port 9292
  53. "Local port the edit server listens to."
  54. :group 'edit-server
  55. :type 'integer)
  56. (defcustom edit-server-host nil
  57. "If not nil, accept connections from HOST address rather than just
  58. localhost. This may present a security issue."
  59. :group 'edit-server
  60. :type 'boolean)
  61. (defcustom edit-server-verbose nil
  62. "If not nil, log connections and progress also to the echo area."
  63. :group 'edit-server
  64. :type 'boolean)
  65. (defcustom edit-server-done-hook nil
  66. "Hook run when done editing a buffer for the Emacs HTTP edit-server.
  67. Current buffer holds the text that is about to be sent back to the client."
  68. :group 'edit-server
  69. :type 'hook)
  70. (defcustom edit-server-start-hook nil
  71. "Hook run when starting a editing buffer. Current buffer is
  72. the fully prepared editing buffer. Use this hook to enable
  73. buffer-specific modes or add key bindings."
  74. :group 'edit-server
  75. :type 'hook)
  76. ; frame options
  77. (defcustom edit-server-new-frame t
  78. "If not nil, edit each buffer in a new frame (and raise it)."
  79. :group 'edit-server
  80. :type 'boolean)
  81. (defcustom edit-server-new-frame-alist
  82. '((name . "Emacs TEXTAREA")
  83. (width . 80)
  84. (height . 25)
  85. (minibuffer . t)
  86. (menu-bar-lines . t))
  87. "Frame parameters for new frames. See `default-frame-alist' for examples.
  88. If nil, the new frame will use the existing `default-frame-alist' values."
  89. :group 'edit-server
  90. :type '(repeat (cons :format "%v"
  91. (symbol :tag "Parameter")
  92. (sexp :tag "Value"))))
  93. (defcustom edit-server-new-frame-mode-line t
  94. "Show the emacs frame's mode-line if set to t; hide if nil."
  95. :group 'edit-server
  96. :type 'boolean)
  97. ;; Vars
  98. (defconst edit-server-process-buffer-name " *edit-server*"
  99. "Template name of the edit-server process buffers.")
  100. (defconst edit-server-log-buffer-name "*edit-server-log*"
  101. "Template name of the edit-server process buffers.")
  102. (defconst edit-server-edit-buffer-name "TEXTAREA"
  103. "Template name of the edit-server text editing buffers.")
  104. (defvar edit-server-proc 'nil
  105. "Network process associated with the current edit, made local when
  106. the edit buffer is created")
  107. (defvar edit-server-frame 'nil
  108. "The frame created for a new edit-server process, made local when
  109. then edit buffer is created")
  110. (defvar edit-server-clients '()
  111. "List of all client processes associated with the server process.")
  112. (defvar edit-server-phase nil
  113. "Symbol indicating the state of the HTTP request parsing.")
  114. (defvar edit-server-received nil
  115. "Number of bytes received so far in the client buffer.
  116. Depending on the character encoding, may be different from the buffer length.")
  117. (defvar edit-server-request nil
  118. "The HTTP request (GET, HEAD, POST) received.")
  119. (defvar edit-server-content-length nil
  120. "The value gotten from the HTTP `Content-Length' header.")
  121. (defvar edit-server-url nil
  122. "The value gotten from the HTTP `x-url' header.")
  123. ;; Mode magic
  124. ;
  125. ; We want to re-map some of the keys to trigger edit-server-done
  126. ; instead of the usual emacs like behaviour. However using
  127. ; local-set-key will affect all buffers of the same mode, hence we
  128. ; define a special (derived) mode for handling editing of text areas.
  129. ;
  130. (defvar edit-server-edit-mode-map
  131. (make-sparse-keymap)
  132. "Keymap for minor mode `edit-server-edit-mode'.
  133. Redefine a few common Emacs keystrokes to functions that can
  134. deal with the response to the edit-server request.
  135. Any of the following keys will close the buffer and send the text
  136. to the HTTP client: C-x #, C-c C-c.
  137. Pressing C-x C-s will save the current state to the kill-ring.
  138. If any of the above isused with a prefix argument, the
  139. unmodified text is sent back instead.
  140. ")
  141. (define-key edit-server-edit-mode-map (kbd "C-x C-s") 'edit-server-save)
  142. (define-key edit-server-edit-mode-map (kbd "C-x #") 'edit-server-done)
  143. (define-key edit-server-edit-mode-map (kbd "C-c C-c") 'edit-server-done)
  144. (define-key edit-server-edit-mode-map (kbd "C-x C-c") 'edit-server-abort)
  145. (define-minor-mode edit-server-edit-mode
  146. "Minor mode enabled on buffers opened by `edit-server-accept'.
  147. Its sole purpose is currently to enable
  148. `edit-server-edit-mode-map', which overrides common keystrokes to
  149. send a response back to the client.
  150. "
  151. :group 'edit-server
  152. :lighter " EditSrv"
  153. :init-value nil
  154. :keymap edit-server-edit-mode-map)
  155. ;; Edit Server socket code
  156. ;
  157. (defun edit-server-start (&optional verbose)
  158. "Start the edit server.
  159. If argument VERBOSE is non-nil, logs all server activity to buffer `*edit-server-log*'.
  160. When called interactivity, a prefix argument will cause it to be verbose.
  161. "
  162. (interactive "P")
  163. (if (or (process-status "edit-server")
  164. (null (condition-case err
  165. (make-network-process
  166. :name "edit-server"
  167. :buffer edit-server-process-buffer-name
  168. :family 'ipv4
  169. :host (if edit-server-host
  170. edit-server-host
  171. 'local)
  172. :service edit-server-port
  173. :log 'edit-server-accept
  174. :server t
  175. :noquery t)
  176. (file-error nil))))
  177. (message "An edit-server process is already running")
  178. (setq edit-server-clients '())
  179. (if verbose (get-buffer-create edit-server-log-buffer-name))
  180. (edit-server-log nil "Created a new edit-server process")))
  181. (defun edit-server-stop nil
  182. "Stop the edit server"
  183. (interactive)
  184. (while edit-server-clients
  185. (edit-server-kill-client (car edit-server-clients))
  186. (setq edit-server-clients (cdr edit-server-clients)))
  187. (if (process-status "edit-server")
  188. (delete-process "edit-server")
  189. (message "No edit server running"))
  190. (if (get-buffer edit-server-process-buffer-name)
  191. (kill-buffer edit-server-process-buffer-name)))
  192. (defun edit-server-log (proc fmt &rest args)
  193. "If a `*edit-server-log*' buffer exists, write STRING to it for logging purposes.
  194. If `edit-server-verbose' is non-nil, then STRING is also echoed to the message line."
  195. (let ((string (apply 'format fmt args)))
  196. (if edit-server-verbose
  197. (message string))
  198. (if (get-buffer edit-server-log-buffer-name)
  199. (with-current-buffer edit-server-log-buffer-name
  200. (goto-char (point-max))
  201. (insert (current-time-string)
  202. " "
  203. (if (processp proc)
  204. (concat
  205. (buffer-name (process-buffer proc))
  206. ": ")
  207. "") ; nil is not acceptable to 'insert
  208. string)
  209. (or (bolp) (newline))))))
  210. (defun edit-server-accept (server client msg)
  211. "Accept a new client connection."
  212. (let ((buffer (generate-new-buffer edit-server-process-buffer-name)))
  213. (and (fboundp 'set-buffer-multibyte)
  214. (set-buffer-multibyte t)) ; djb
  215. (buffer-disable-undo buffer)
  216. (set-process-buffer client buffer)
  217. (set-process-filter client 'edit-server-filter)
  218. (set-process-query-on-exit-flag client nil) ; kill-buffer kills the associated process
  219. (with-current-buffer buffer
  220. (set (make-local-variable 'edit-server-phase) 'wait)
  221. (set (make-local-variable 'edit-server-received) 0)
  222. (set (make-local-variable 'edit-server-request) nil))
  223. (set (make-local-variable 'edit-server-content-length) nil)
  224. (set (make-local-variable 'edit-server-url) nil))
  225. (add-to-list 'edit-server-clients client)
  226. (edit-server-log client msg))
  227. (defun edit-server-filter (proc string)
  228. "Process data received from the client."
  229. ;; there is no guarantee that data belonging to the same client
  230. ;; request will arrive all in one go; therefore, we must accumulate
  231. ;; data in the buffer and process it in different phases, which
  232. ;; requires us to keep track of the processing state.
  233. (with-current-buffer (process-buffer proc)
  234. (insert string)
  235. (setq edit-server-received
  236. (+ edit-server-received (string-bytes string)))
  237. (when (eq edit-server-phase 'wait)
  238. ;; look for a complete HTTP request string
  239. (save-excursion
  240. (goto-char (point-min))
  241. (when (re-search-forward "^\\([A-Z]+\\)\\s-+\\(\\S-+\\)\\s-+\\(HTTP/[0-9\.]+\\)\r?\n" nil t)
  242. (edit-server-log proc
  243. "Got HTTP `%s' request, processing in buffer `%s'..."
  244. (match-string 1) (current-buffer))
  245. (setq edit-server-request (match-string 1))
  246. (setq edit-server-content-length nil)
  247. (setq edit-server-phase 'head))))
  248. (when (eq edit-server-phase 'head)
  249. ;; look for "Content-length" header
  250. (save-excursion
  251. (goto-char (point-min))
  252. (when (re-search-forward "^Content-Length:\\s-+\\([0-9]+\\)" nil t)
  253. (setq edit-server-content-length (string-to-number (match-string 1)))))
  254. ;; look for "x-url" header
  255. (save-excursion
  256. (goto-char (point-min))
  257. (when (re-search-forward "^x-url: .*//\\(.*\\)/" nil t)
  258. (setq edit-server-url (match-string 1))))
  259. ;; look for head/body separator
  260. (save-excursion
  261. (goto-char (point-min))
  262. (when (re-search-forward "\\(\r?\n\\)\\{2\\}" nil t)
  263. ;; HTTP headers are pure ASCII (1 char = 1 byte), so we can subtract
  264. ;; the buffer position from the count of received bytes
  265. (setq edit-server-received
  266. (- edit-server-received (- (match-end 0) (point-min))))
  267. ;; discard headers - keep only HTTP content in buffer
  268. (delete-region (point-min) (match-end 0))
  269. (setq edit-server-phase 'body))))
  270. (when (eq edit-server-phase 'body)
  271. (if (and edit-server-content-length
  272. (> edit-server-content-length edit-server-received))
  273. (edit-server-log proc
  274. "Received %d bytes of %d ..."
  275. edit-server-received edit-server-content-length)
  276. ;; all content transferred - process request now
  277. (cond
  278. ((string= edit-server-request "POST")
  279. ;; create editing buffer, and move content to it
  280. (edit-server-create-edit-buffer proc))
  281. (t
  282. ;; send 200 OK response to any other request
  283. (edit-server-send-response proc "edit-server is running.\n" t)))
  284. ;; wait for another connection to arrive
  285. (setq edit-server-received 0)
  286. (setq edit-server-phase 'wait)))))
  287. (defun edit-server-create-frame(buffer)
  288. "Create a frame for the edit server"
  289. (if edit-server-new-frame
  290. (let ((new-frame
  291. (if (memq window-system '(ns mac))
  292. ;; Aquamacs, Emacs NS, Emacs (experimental) Mac port
  293. (make-frame edit-server-new-frame-alist)
  294. (make-frame-on-display (getenv "DISPLAY")
  295. edit-server-new-frame-alist))))
  296. (if (not edit-server-new-frame-mode-line)
  297. (setq mode-line-format nil))
  298. (select-frame new-frame)
  299. (if (and (eq window-system 'x)
  300. (fboundp 'x-send-client-message))
  301. (x-send-client-message nil 0 nil
  302. "_NET_ACTIVE_WINDOW" 32
  303. '(1 0 0)))
  304. (raise-frame new-frame)
  305. (set-window-buffer (frame-selected-window new-frame) buffer)
  306. new-frame)
  307. (pop-to-buffer buffer)
  308. (raise-frame)
  309. nil))
  310. (defun edit-server-create-edit-buffer(proc)
  311. "Create an edit buffer, place content in it and save the network
  312. process for the final call back"
  313. (let ((buffer (generate-new-buffer
  314. (if edit-server-url
  315. edit-server-url
  316. edit-server-edit-buffer-name))))
  317. (with-current-buffer buffer
  318. (and (fboundp 'set-buffer-multibyte)
  319. (set-buffer-multibyte t))) ; djb
  320. (copy-to-buffer buffer (point-min) (point-max))
  321. (with-current-buffer buffer
  322. ;; use `text-mode' by default, but allow
  323. ;; `edit-server-start-hook' to override it however, (re)setting
  324. ;; the minor mode seems to clear the buffer-local variables that
  325. ;; we depend upon for the response, so call the hooks early on
  326. (text-mode)
  327. (run-hooks 'edit-server-start-hook)
  328. (not-modified)
  329. (add-hook 'kill-buffer-hook 'edit-server-abort* nil t)
  330. (buffer-enable-undo)
  331. (set (make-local-variable 'edit-server-proc) proc)
  332. (set (make-local-variable 'edit-server-frame)
  333. (edit-server-create-frame buffer))
  334. (edit-server-edit-mode))))
  335. (defun edit-server-send-response (proc &optional body close)
  336. "Send an HTTP 200 OK response back to process PROC.
  337. Optional second argument BODY specifies the response content:
  338. - If nil, the HTTP response will have null content.
  339. - If a string, the string is sent as response content.
  340. - Any other value will cause the contents of the current
  341. buffer to be sent.
  342. If optional third argument CLOSE is non-nil, then process PROC
  343. and its buffer are killed with `edit-server-kill-client'."
  344. (interactive)
  345. (if (processp proc)
  346. (let ((response-header (concat
  347. "HTTP/1.0 200 OK\n"
  348. (format "Server: Emacs/%s\n" emacs-version)
  349. "Date: "
  350. (format-time-string
  351. "%a, %d %b %Y %H:%M:%S GMT\n"
  352. (current-time)))))
  353. (process-send-string proc response-header)
  354. (process-send-string proc "\n")
  355. (cond
  356. ((stringp body) (process-send-string proc (encode-coding-string body 'utf-8)))
  357. ((not body) nil)
  358. (t (progn
  359. (encode-coding-region (point-min) (point-max) 'utf-8)
  360. (process-send-region proc (point-min) (point-max)))))
  361. (process-send-eof proc)
  362. (if close
  363. (edit-server-kill-client proc))
  364. (edit-server-log proc "Editing done, sent HTTP OK response."))
  365. (message "edit-server-send-response: invalid proc (bug?)")))
  366. (defun edit-server-kill-client (proc)
  367. "Kill client process PROC and remove it from the list."
  368. (let ((procbuf (process-buffer proc)))
  369. (delete-process proc)
  370. (kill-buffer procbuf)
  371. (setq edit-server-clients (delq proc edit-server-clients))))
  372. (defun edit-server-done (&optional abort nokill)
  373. "Finish editing: send HTTP response back, close client and editing buffers.
  374. The current contents of the buffer are sent back to the HTTP
  375. client, unless argument ABORT is non-nil, in which case then the
  376. original text is sent back.
  377. If optional second argument NOKILL is non-nil, then the editing
  378. buffer is not killed.
  379. When called interactively, use prefix arg to abort editing."
  380. (interactive "P")
  381. ;; Do nothing if the connection is closed by the browser (tab killed, etc.)
  382. (unless (eq (process-status edit-server-proc) 'closed)
  383. (let ((buffer (current-buffer))
  384. (proc edit-server-proc)
  385. (procbuf (process-buffer edit-server-proc)))
  386. ;; edit-server-* vars are buffer-local, so they must be used before issuing kill-buffer
  387. (if abort
  388. ;; send back original content
  389. (with-current-buffer procbuf
  390. (run-hooks 'edit-server-done-hook)
  391. (edit-server-send-response proc t))
  392. ;; send back edited content
  393. (save-restriction
  394. (widen)
  395. (buffer-disable-undo)
  396. ;; ensure any format encoding is done (like longlines)
  397. (dolist (format buffer-file-format)
  398. (format-encode-region (point-min) (point-max) format))
  399. ;; send back
  400. (run-hooks 'edit-server-done-hook)
  401. (edit-server-send-response edit-server-proc t)
  402. ;; restore formats (only useful if we keep the buffer)
  403. (dolist (format buffer-file-format)
  404. (format-decode-region (point-min) (point-max) format))
  405. (buffer-enable-undo)))
  406. (if edit-server-frame (delete-frame edit-server-frame))
  407. ;; delete-frame may change the current buffer
  408. (unless nokill (kill-buffer buffer))
  409. (edit-server-kill-client proc))))
  410. ;;
  411. ;; There are a couple of options for handling the save
  412. ;; action. The intent is to preserve data but not finish
  413. ;; editing. There are a couple of approaches that could
  414. ;; be taken:
  415. ;; a) Use the iterative edit-server option (send a buffer
  416. ;; back to the client which then returns new request
  417. ;; to continue the session).
  418. ;; b) Back-up the edit session to a file
  419. ;; c) Save the current buffer to the kill-ring
  420. ;;
  421. ;; I've attempted to do a) a couple of times but I keep running
  422. ;; into problems which I think are emacs bugs. So for now I'll
  423. ;; just push the current buffer to the kill-ring.
  424. (defun edit-server-save ()
  425. "Save the current state of the edit buffer."
  426. (interactive)
  427. (save-restriction
  428. (widen)
  429. (buffer-disable-undo)
  430. (copy-region-as-kill (point-min) (point-max))
  431. (buffer-enable-undo)))
  432. (defun edit-server-abort ()
  433. "Discard editing and send the original text back to the browser."
  434. (interactive)
  435. (edit-server-done t))
  436. (defun edit-server-abort* ()
  437. "Discard editing and send the original text back to the browser,
  438. but don't kill the editing buffer."
  439. (interactive)
  440. (edit-server-done t t))
  441. (provide 'edit-server)
  442. ;;; edit-server.el ends here