org-src.el 23 KB

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