org-src.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. ;;; org-src.el --- Source code examples in Org
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Bastien Guerry <bzg AT altern DOT org>
  8. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 6.31trans
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with source code examples in Org-mode.
  28. ;;; Code:
  29. (require 'org-macs)
  30. (require 'org-compat)
  31. (declare-function org-do-remove-indentation "org" (&optional n))
  32. (declare-function org-get-indentation "org" (&optional line))
  33. (defcustom org-edit-src-region-extra nil
  34. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  35. For examples see the function `org-edit-src-find-region-and-lang'.
  36. The regular expression identifying the begin marker should end with a newline,
  37. and the regexp marking the end line should start with a newline, to make sure
  38. there are kept outside the narrowed region."
  39. :group 'org-edit-structure
  40. :type '(repeat
  41. (list
  42. (regexp :tag "begin regexp")
  43. (regexp :tag "end regexp")
  44. (choice :tag "language"
  45. (string :tag "specify")
  46. (integer :tag "from match group")
  47. (const :tag "from `lang' element")
  48. (const :tag "from `style' element")))))
  49. (defcustom org-coderef-label-format "(ref:%s)"
  50. "The default coderef format.
  51. This format string will be used to search for coderef labels in literal
  52. examples (EXAMPLE and SRC blocks). The format can be overwritten in
  53. an individual literal example with the -f option, like
  54. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  55. ...
  56. #+END_SRC
  57. If you want to use this for HTML export, make sure that the format does
  58. not introduce special font-locking, and avoid the HTML special
  59. characters `<', `>', and `&'. The reason for this restriction is that
  60. the labels are searched for only after htmlize has done its job."
  61. :group 'org-edit-structure ; FIXME this is not in the right group
  62. :type 'string)
  63. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  64. "The mode that should be used to edit fixed-width regions.
  65. These are the regions where each line starts with a colon."
  66. :group 'org-edit-structure
  67. :type '(choice
  68. (const artist-mode)
  69. (const picture-mode)
  70. (const fundamental-mode)
  71. (function :tag "Other (specify)")))
  72. (defcustom org-src-preserve-indentation nil
  73. "If non-nil, leading whitespace characters in source code
  74. blocks are preserved. Otherwise, after editing with
  75. \\[org-edit-src-code], the minimum (across-lines) number of
  76. leading whitespace characters are removed from all lines, and
  77. the code block is then uniformly indented according to the
  78. value of `org-edit-src-content-indentation'."
  79. :group 'org-edit-structure
  80. :type 'boolean)
  81. (defcustom org-edit-src-content-indentation 2
  82. "Indentation for the content of a source code block.
  83. This should be the number of spaces added to the indentation of the #+begin
  84. line in order to compute the indentation of the block content after
  85. editing it with \\[org-edit-src-code]. Has no effect if
  86. `org-src-preserve-indentation' is non-nil."
  87. :group 'org-edit-structure
  88. :type 'integer)
  89. (defcustom org-edit-src-persistent-message t
  90. "Non-nil means show persistent exit help message while editing src examples.
  91. The message is shown in the header-line, which will be created in the
  92. first line of the window showing the editing buffer.
  93. When nil, the message will only be shown intermittently in the echo area."
  94. :group 'org-edit-structure
  95. :type 'boolean)
  96. (defvar org-src-mode-hook nil
  97. "Hook run after Org switched a source code snippet to its Emacs mode.
  98. This hook will run
  99. - when editing a source code snippet with \"C-c '\".
  100. - When formatting a source code snippet for export with htmlize.
  101. You may want to use this hook for example to turn off `outline-minor-mode'
  102. or similar things which you want to have when editing a source code file,
  103. but which mess up the display of a snippet in Org exported files.")
  104. (defcustom org-src-lang-modes
  105. '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
  106. ("asymptote" . asy))
  107. "Alist mapping languages to their major mode.
  108. The key is the language name, the value is the string that should
  109. be inserted as the name of the major mode. For many languages this is
  110. simple, but for language where this is not the case, this variable
  111. provides a way to simplify things on the user side.
  112. For example, there is no ocaml-mode in Emacs, but the mode to use is
  113. `tuareg-mode'."
  114. :group 'org-edit-structure
  115. :type '(repeat
  116. (cons
  117. (string "Language name")
  118. (symbol "Major mode"))))
  119. ;;; Editing source examples
  120. (defvar org-src-mode-map (make-sparse-keymap))
  121. (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
  122. (defvar org-edit-src-force-single-line nil)
  123. (defvar org-edit-src-from-org-mode nil)
  124. (defvar org-edit-src-picture nil)
  125. (defvar org-edit-src-beg-marker nil)
  126. (defvar org-edit-src-end-marker nil)
  127. (defvar org-edit-src-overlay nil)
  128. (defvar org-edit-src-block-indentation nil)
  129. (define-minor-mode org-src-mode
  130. "Minor mode for language major mode buffers generated by org.
  131. This minor mode is turned on in two situations:
  132. - when editing a source code snippet with \"C-c '\".
  133. - When formatting a source code snippet for export with htmlize.
  134. There is a mode hook, and keybindings for `org-edit-src-exit' and
  135. `org-edit-src-save'")
  136. (defun org-edit-src-code ()
  137. "Edit the source code example at point.
  138. The example is copied to a separate buffer, and that buffer is switched
  139. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  140. This will remove the original code in the Org buffer, and replace it with
  141. the edited version."
  142. (interactive)
  143. (let ((line (org-current-line))
  144. (col (current-column))
  145. (case-fold-search t)
  146. (msg (substitute-command-keys
  147. "Edit, then exit with C-c ' (C-c and single quote)"))
  148. (info (org-edit-src-find-region-and-lang))
  149. (org-mode-p (eq major-mode 'org-mode))
  150. (beg (make-marker))
  151. (end (make-marker))
  152. (preserve-indentation org-src-preserve-indentation)
  153. block-nindent total-nindent ovl lang lang-f single lfmt code begline buffer)
  154. (if (not info)
  155. nil
  156. (setq beg (move-marker beg (nth 0 info))
  157. end (move-marker end (nth 1 info))
  158. code (buffer-substring-no-properties beg end)
  159. lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
  160. (nth 2 info))
  161. lang (if (symbolp lang) (symbol-name lang) lang)
  162. single (nth 3 info)
  163. lfmt (nth 4 info)
  164. block-nindent (nth 5 info)
  165. lang-f (intern (concat lang "-mode"))
  166. begline (save-excursion (goto-char beg) (org-current-line)))
  167. (unless (functionp lang-f)
  168. (error "No such language mode: %s" lang-f))
  169. (org-goto-line line)
  170. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  171. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  172. (switch-to-buffer buffer)
  173. (when buffer
  174. (with-current-buffer buffer
  175. (if (boundp 'org-edit-src-overlay)
  176. (org-delete-overlay org-edit-src-overlay)))
  177. (kill-buffer buffer))
  178. (setq buffer (generate-new-buffer
  179. (org-src-construct-edit-buffer-name (buffer-name) lang)))
  180. (setq ovl (org-make-overlay beg end))
  181. (org-overlay-put ovl 'face 'secondary-selection)
  182. (org-overlay-put ovl 'edit-buffer buffer)
  183. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  184. (org-overlay-put ovl 'face 'secondary-selection)
  185. (org-overlay-put ovl
  186. 'keymap
  187. (let ((map (make-sparse-keymap)))
  188. (define-key map [mouse-1] 'org-edit-src-continue)
  189. map))
  190. (org-overlay-put ovl :read-only "Leave me alone")
  191. (switch-to-buffer buffer)
  192. (if (eq single 'macro-definition)
  193. (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
  194. (insert code)
  195. (remove-text-properties (point-min) (point-max)
  196. '(display nil invisible nil intangible nil))
  197. (unless preserve-indentation
  198. (setq total-nindent (or (org-do-remove-indentation) 0)))
  199. (let ((org-inhibit-startup t))
  200. (funcall lang-f))
  201. (set (make-local-variable 'org-edit-src-force-single-line) single)
  202. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  203. (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
  204. (when lfmt
  205. (set (make-local-variable 'org-coderef-label-format) lfmt))
  206. (when org-mode-p
  207. (goto-char (point-min))
  208. (while (re-search-forward "^," nil t)
  209. (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
  210. (replace-match "")))
  211. (org-goto-line (1+ (- line begline)))
  212. (org-move-to-column
  213. (if preserve-indentation col (max 0 (- col total-nindent))))
  214. (org-set-local 'org-edit-src-beg-marker beg)
  215. (org-set-local 'org-edit-src-end-marker end)
  216. (org-set-local 'org-edit-src-overlay ovl)
  217. (org-set-local 'org-edit-src-block-indentation block-nindent)
  218. (org-src-mode)
  219. (set-buffer-modified-p nil)
  220. (and org-edit-src-persistent-message
  221. (org-set-local 'header-line-format msg)))
  222. (message "%s" msg)
  223. t)))
  224. (defun org-edit-src-continue (e)
  225. (interactive "e")
  226. (mouse-set-point e)
  227. (let ((buf (get-char-property (point) 'edit-buffer)))
  228. (if buf (switch-to-buffer buf)
  229. (error "Something is wrong here"))))
  230. (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
  231. "Construct the buffer name for a source editing buffer"
  232. (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
  233. (defun org-edit-src-find-buffer (beg end)
  234. "Find a source editing buffer that is already editing the region BEG to END."
  235. (catch 'exit
  236. (mapc
  237. (lambda (b)
  238. (with-current-buffer b
  239. (if (and (string-match "\\`*Org Src " (buffer-name))
  240. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  241. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  242. (equal beg org-edit-src-beg-marker)
  243. (equal end org-edit-src-end-marker))
  244. (throw 'exit (current-buffer)))))
  245. (buffer-list))
  246. nil))
  247. (defun org-edit-fixed-width-region ()
  248. "Edit the fixed-width ascii drawing at point.
  249. This must be a region where each line starts with a colon followed by
  250. a space character.
  251. An new buffer is created and the fixed-width region is copied into it,
  252. and the buffer is switched into `artist-mode' for editing. When done,
  253. exit with \\[org-edit-src-exit]. The edited text will then replace
  254. the fragment in the Org-mode buffer."
  255. (interactive)
  256. (let ((line (org-current-line))
  257. (col (current-column))
  258. (case-fold-search t)
  259. (msg (substitute-command-keys
  260. "Edit, then exit with C-c ' (C-c and single quote)"))
  261. (org-mode-p (eq major-mode 'org-mode))
  262. (beg (make-marker))
  263. (end (make-marker))
  264. (preserve-indentation org-src-preserve-indentation)
  265. block-nindent ovl beg1 end1 code begline buffer)
  266. (beginning-of-line 1)
  267. (if (looking-at "[ \t]*[^:\n \t]")
  268. nil
  269. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  270. (setq beg1 (point) end1 beg1)
  271. (save-excursion
  272. (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
  273. (setq beg1 (point-at-bol 2))
  274. (setq beg1 (point))))
  275. (save-excursion
  276. (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
  277. (setq end1 (1- (match-beginning 0)))
  278. (setq end1 (point))))
  279. (org-goto-line line))
  280. (setq beg (move-marker beg beg1)
  281. end (move-marker end end1)
  282. code (buffer-substring-no-properties beg end)
  283. begline (save-excursion (goto-char beg) (org-current-line)))
  284. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  285. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  286. (switch-to-buffer buffer)
  287. (when buffer
  288. (with-current-buffer buffer
  289. (if (boundp 'org-edit-src-overlay)
  290. (org-delete-overlay org-edit-src-overlay)))
  291. (kill-buffer buffer))
  292. (setq buffer (generate-new-buffer
  293. (org-src-construct-edit-buffer-name
  294. (buffer-name) "Fixed Width")))
  295. (setq ovl (org-make-overlay beg end))
  296. (org-overlay-put ovl 'face 'secondary-selection)
  297. (org-overlay-put ovl 'edit-buffer buffer)
  298. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  299. (org-overlay-put ovl 'face 'secondary-selection)
  300. (org-overlay-put ovl
  301. 'keymap
  302. (let ((map (make-sparse-keymap)))
  303. (define-key map [mouse-1] 'org-edit-src-continue)
  304. map))
  305. (org-overlay-put ovl :read-only "Leave me alone")
  306. (switch-to-buffer buffer)
  307. (insert code)
  308. (remove-text-properties (point-min) (point-max)
  309. '(display nil invisible nil intangible nil))
  310. (setq block-nindent (or (org-do-remove-indentation) 0))
  311. (cond
  312. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  313. (fundamental-mode)
  314. (artist-mode 1))
  315. (t (funcall org-edit-fixed-width-region-mode)))
  316. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  317. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  318. (set (make-local-variable 'org-edit-src-picture) t)
  319. (goto-char (point-min))
  320. (while (re-search-forward "^[ \t]*: ?" nil t)
  321. (replace-match ""))
  322. (org-goto-line (1+ (- line begline)))
  323. (org-move-to-column (max 0 (- col block-nindent 2)))
  324. (org-set-local 'org-edit-src-beg-marker beg)
  325. (org-set-local 'org-edit-src-end-marker end)
  326. (org-set-local 'org-edit-src-overlay ovl)
  327. (org-set-local 'org-edit-src-block-indentation block-nindent)
  328. (org-set-local 'org-edit-src-content-indentation 0)
  329. (org-set-local 'org-src-preserve-indentation nil)
  330. (org-src-mode)
  331. (set-buffer-modified-p nil)
  332. (and org-edit-src-persistent-message
  333. (org-set-local 'header-line-format msg)))
  334. (message "%s" msg)
  335. t)))
  336. (defun org-edit-src-find-region-and-lang ()
  337. "Find the region and language for a local edit.
  338. Return a list with beginning and end of the region, a string representing
  339. the language, a switch telling if the content should be in a single line."
  340. (let ((re-list
  341. (append
  342. org-edit-src-region-extra
  343. '(
  344. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  345. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  346. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  347. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  348. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  349. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  350. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  351. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  352. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  353. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  354. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  355. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  356. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  357. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  358. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  359. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  360. ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
  361. "\n" "fundamental" macro-definition)
  362. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  363. )))
  364. (pos (point))
  365. re1 re2 single beg end lang lfmt match-re1 ind entry)
  366. (catch 'exit
  367. (while (setq entry (pop re-list))
  368. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  369. single (nth 3 entry))
  370. (save-excursion
  371. (if (or (looking-at re1)
  372. (re-search-backward re1 nil t))
  373. (progn
  374. (setq match-re1 (match-string 0))
  375. (setq beg (match-end 0)
  376. lang (org-edit-src-get-lang lang)
  377. lfmt (org-edit-src-get-label-format match-re1)
  378. ind (org-edit-src-get-indentation (match-beginning 0)))
  379. (if (and (re-search-forward re2 nil t)
  380. (>= (match-end 0) pos))
  381. (throw 'exit (list beg (match-beginning 0)
  382. lang single lfmt ind))))
  383. (if (or (looking-at re2)
  384. (re-search-forward re2 nil t))
  385. (progn
  386. (setq end (match-beginning 0))
  387. (if (and (re-search-backward re1 nil t)
  388. (<= (match-beginning 0) pos))
  389. (progn
  390. (setq lfmt (org-edit-src-get-label-format
  391. (match-string 0))
  392. ind (org-edit-src-get-indentation
  393. (match-beginning 0)))
  394. (throw 'exit
  395. (list (match-end 0) end
  396. (org-edit-src-get-lang lang)
  397. single lfmt ind))))))))))))
  398. (defun org-edit-src-get-lang (lang)
  399. "Extract the src language."
  400. (let ((m (match-string 0)))
  401. (cond
  402. ((stringp lang) lang)
  403. ((integerp lang) (match-string lang))
  404. ((and (eq lang 'lang)
  405. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  406. (match-string 1 m))
  407. ((and (eq lang 'style)
  408. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  409. (match-string 1 m))
  410. (t "fundamental"))))
  411. (defun org-edit-src-get-label-format (s)
  412. "Extract the label format."
  413. (save-match-data
  414. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  415. (match-string 1 s))))
  416. (defun org-edit-src-get-indentation (pos)
  417. "Count leading whitespace characters on line"
  418. (save-match-data
  419. (goto-char pos)
  420. (org-get-indentation)))
  421. (defun org-edit-src-exit ()
  422. "Exit special edit and protect problematic lines."
  423. (interactive)
  424. (unless org-edit-src-from-org-mode
  425. (error "This is not a sub-editing buffer, something is wrong..."))
  426. (let* ((beg org-edit-src-beg-marker)
  427. (end org-edit-src-end-marker)
  428. (ovl org-edit-src-overlay)
  429. (buffer (current-buffer))
  430. (single (org-bound-and-true-p org-edit-src-force-single-line))
  431. (macro (eq single 'macro-definition))
  432. (total-nindent (+ (or org-edit-src-block-indentation 0)
  433. org-edit-src-content-indentation))
  434. (preserve-indentation org-src-preserve-indentation)
  435. (delta 0) code line col indent)
  436. (untabify (point-min) (point-max))
  437. (save-excursion
  438. (goto-char (point-min))
  439. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  440. (unless macro
  441. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))))
  442. (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
  443. 1
  444. (org-current-line))
  445. col (current-column))
  446. (when single
  447. (goto-char (point-min))
  448. (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
  449. (goto-char (point-min))
  450. (let ((cnt 0))
  451. (while (re-search-forward "\n" nil t)
  452. (setq cnt (1+ cnt))
  453. (replace-match (if macro "\\n" " ") t t))
  454. (when (and macro (> cnt 0))
  455. (goto-char (point-max)) (insert "\\n")))
  456. (goto-char (point-min))
  457. (if (looking-at "\\s-*") (replace-match " ")))
  458. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  459. (goto-char (point-min))
  460. (while (re-search-forward
  461. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  462. (if (eq (org-current-line) line) (setq delta (1+ delta)))
  463. (replace-match ",\\1")))
  464. (when (org-bound-and-true-p org-edit-src-picture)
  465. (setq preserve-indentation nil)
  466. (untabify (point-min) (point-max))
  467. (goto-char (point-min))
  468. (while (re-search-forward "^" nil t)
  469. (replace-match ": ")))
  470. (unless (or single preserve-indentation (= total-nindent 0))
  471. (setq indent (make-string total-nindent ?\ ))
  472. (goto-char (point-min))
  473. (while (re-search-forward "^" nil t)
  474. (replace-match indent)))
  475. (if (org-bound-and-true-p org-edit-src-picture)
  476. (setq total-nindent (+ total-nindent 2)))
  477. (setq code (buffer-string))
  478. (set-buffer-modified-p nil)
  479. (switch-to-buffer (marker-buffer beg))
  480. (kill-buffer buffer)
  481. (goto-char beg)
  482. (delete-region beg end)
  483. (insert code)
  484. (goto-char beg)
  485. (if single (just-one-space))
  486. (org-goto-line (1- (+ (org-current-line) line)))
  487. (org-move-to-column (if preserve-indentation col (+ col total-nindent delta)))
  488. (move-marker beg nil)
  489. (move-marker end nil)))
  490. (defun org-edit-src-save ()
  491. "Save parent buffer with current state source-code buffer."
  492. (interactive)
  493. (save-window-excursion
  494. (let ((p (point)) (m (mark)) msg)
  495. (org-edit-src-exit)
  496. (save-buffer)
  497. (setq msg (current-message))
  498. (org-edit-src-code)
  499. (push-mark m 'nomessage)
  500. (goto-char (min p (point-max)))
  501. (message (or msg "")))))
  502. (defun org-src-mode-configure-edit-buffer ()
  503. (when org-edit-src-from-org-mode
  504. (setq buffer-offer-save t)
  505. (setq buffer-file-name
  506. (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
  507. "[" (buffer-name) "]"))
  508. (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
  509. '(org-edit-src-save))
  510. (org-add-hook 'kill-buffer-hook
  511. '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
  512. (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
  513. (provide 'org-src)
  514. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  515. ;;; org-src.el ends here