Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.1 KiB

  1. ;;; ein-file.el --- Editing files downloaded from jupyter
  2. ;; Copyright (C) 2017- John M. Miller
  3. ;; Authors: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; John M. Miller <millejoh at mac.com>
  5. ;; This file is NOT part of GNU Emacs.
  6. ;; ein-file.el is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; ein-file.el is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with ein-notebooklist.el. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. (require 'ein-contents-api)
  18. (defvar *ein:file-buffername-template* "'/ein:%s:%s")
  19. (ein:deflocal ein:content-file-buffer--content nil)
  20. ;; (push '("^ein:.*" . ein:content-file-handler)
  21. ;; file-name-handler-alist)
  22. (defun ein:file-buffer-name (urlport path)
  23. (format *ein:file-buffername-template*
  24. urlport
  25. path))
  26. (defun ein:file-open (url-or-port path)
  27. (interactive
  28. (ein:notebooklist-parse-nbpath (ein:notebooklist-ask-path "file")))
  29. (ein:content-query-contents url-or-port path #'ein:file-open-finish nil))
  30. (defun ein:file-delete (url-or-port path)
  31. (ein:query-singleton-ajax
  32. (list 'file-delete url-or-port path)
  33. (ein:content-url* url-or-port path)
  34. :type "DELETE"
  35. :timeout ein:content-query-timeout
  36. :parser #'ein:json-read
  37. :sync ein:force-sync
  38. :success (lexical-let ((path path))
  39. #'(lambda (&rest ignore) (ein:notebooklist-reload)
  40. (message "Successful deleted file: %s" path)))
  41. :error (lexical-let ((path path))
  42. #'(lambda (&rest ignore) (ein:notebooklist-reload)
  43. (message "Delete file %s failed." path)))
  44. ))
  45. (defun ein:file-open-finish (content)
  46. (with-current-buffer (get-buffer-create (ein:file-buffer-name (ein:$content-url-or-port content)
  47. (ein:$content-path content)))
  48. (setq ein:content-file-buffer--content content)
  49. (let ((raw-content (ein:$content-raw-content content)))
  50. (if (eql system-type 'windows-nt)
  51. (insert (decode-coding-string raw-content 'utf-8))
  52. (insert raw-content)))
  53. (set-visited-file-name (buffer-name))
  54. (set-auto-mode)
  55. (add-hook 'write-contents-functions 'ein:content-file-save) ;; FIXME Brittle, will not work
  56. ;; if user changes major mode.
  57. (ein:log 'verbose "Opened file %s" (ein:$content-name content))
  58. (set-buffer-modified-p nil)
  59. (goto-char (point-min))
  60. (pop-to-buffer (buffer-name))))
  61. (defun ein:content-file-save ()
  62. (when (boundp 'ein:content-file-buffer--content)
  63. (setf (ein:$content-raw-content ein:content-file-buffer--content) (buffer-string))
  64. (ein:content-save ein:content-file-buffer--content)
  65. (set-buffer-modified-p nil)
  66. t))
  67. (provide 'ein-file)