-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorgish.el
73 lines (66 loc) · 2.54 KB
/
orgish.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;; -*- lexical-binding:t -*-
(defun orgish/save-buffer (buffer file)
(with-current-buffer (org-html-export-as-html)
; insert our source org stuff in
(insert "<!-- org\n")
(insert-buffer-substring buffer)
(insert "\n")
(insert "org -->\n")
(write-region nil nil file))
; notify to emacs that the file has no more changes
; this is needed to ensure that this buffer doesn't come up in save-some-buffers group
(with-current-buffer buffer
(set-buffer-modified-p nil))
t
)
(defun orgish/extract-source (file buffer)
"Extract out the source org from the html file into the buffer."
(with-temp-buffer
(insert-file-contents-literally file)
(search-forward "<!-- org\n")
(let* ((beg (point))
(end (progn
(search-forward "org -->\n")
(forward-line -1)
(forward-char -1)
(point)))
(sbuf (current-buffer)))
(with-current-buffer buffer
(insert-buffer-substring sbuf beg end)
)))
t)
(defun orgish/open (filename)
"Single entry point to open/create a new orgish file."
(interactive "FEnter file name: ")
(let* ((bufname (concat "*" (file-name-nondirectory filename) "-orgish" "*"))
(buf-already-exists (get-buffer bufname)))
(switch-to-buffer bufname)
(if (not buf-already-exists)
(orgish/setup filename))))
(defun orgish/setup (filename)
"Setup the current buffer as orgish with filename as the backing file.
Should never be called directly. Should only be called by orgish/open."
; check if we are opening an existing orgish file
(if (file-exists-p filename)
(if (not (ignore-errors (orgish/extract-source filename (current-buffer))))
; some errors in parsing
(progn
(kill-buffer)
(throw 'invalid-orgish-file "Source file does not seem to be orgish. Refusing to continue!"))
))
; do the normal processing here
(org-mode)
(setq org-export-show-temporary-export-buffer nil)
; unset the file connection - since this is not a normal buffer
(setq buffer-file-name nil)
; autosave on focus out
(add-hook 'focus-out-hook 'save-buffer 0 t)
(add-hook 'write-contents-functions (apply-partially 'orgish/save-buffer (buffer-name) filename)))
(defun orgish/check ()
"Check if the current file is orgish and upgrade the view to orgish."
(if (equal (buffer-substring-no-properties 1 9) "<!-- org")
(let ((srcfile (buffer-file-name)))
(kill-buffer)
(orgish/open srcfile))
))
(add-hook 'html-mode-hook 'orgish/check)