org-src.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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
  101. windows.
  102. other-window Use `switch-to-buffer-other-window' to display edit buffer.
  103. reorganize-frame Show only two windows on the current frame, the current
  104. window and the edit buffer. When exiting the edit buffer,
  105. return to one window.
  106. other-frame Use `switch-to-buffer-other-frame' to display edit buffer.
  107. Also, when exiting the edit buffer, kill that frame."
  108. :group 'org-edit-structure
  109. :type '(choice
  110. (const current-window)
  111. (const other-frame)
  112. (const other-window)
  113. (const reorganize-frame)))
  114. (defvar org-src-mode-hook nil
  115. "Hook run after Org switched a source code snippet to its Emacs mode.
  116. This hook will run
  117. - when editing a source code snippet with \"C-c '\".
  118. - When formatting a source code snippet for export with htmlize.
  119. You may want to use this hook for example to turn off `outline-minor-mode'
  120. or similar things which you want to have when editing a source code file,
  121. but which mess up the display of a snippet in Org exported files.")
  122. (defcustom org-src-lang-modes
  123. '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
  124. ("asymptote" . asy) ("dot" . fundamental))
  125. "Alist mapping languages to their major mode.
  126. The key is the language name, the value is the string that should
  127. be inserted as the name of the major mode. For many languages this is
  128. simple, but for language where this is not the case, this variable
  129. provides a way to simplify things on the user side.
  130. For example, there is no ocaml-mode in Emacs, but the mode to use is
  131. `tuareg-mode'."
  132. :group 'org-edit-structure
  133. :type '(repeat
  134. (cons
  135. (string "Language name")
  136. (symbol "Major mode"))))
  137. ;;; Editing source examples
  138. (defvar org-src-mode-map (make-sparse-keymap))
  139. (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
  140. (defvar org-edit-src-force-single-line nil)
  141. (defvar org-edit-src-from-org-mode nil)
  142. (defvar org-edit-src-picture nil)
  143. (defvar org-edit-src-beg-marker nil)
  144. (defvar org-edit-src-end-marker nil)
  145. (defvar org-edit-src-overlay nil)
  146. (defvar org-edit-src-block-indentation nil)
  147. (defvar org-src-ask-before-returning-to-edit-buffer t
  148. "If nil, when org-edit-src code is used on a block that already
  149. has an active edit buffer, it will switch to that edit buffer
  150. immediately; otherwise it will ask whether you want to return
  151. to the existing edit buffer.")
  152. (define-minor-mode org-src-mode
  153. "Minor mode for language major mode buffers generated by org.
  154. This minor mode is turned on in two situations:
  155. - when editing a source code snippet with \"C-c '\".
  156. - When formatting a source code snippet for export with htmlize.
  157. There is a mode hook, and keybindings for `org-edit-src-exit' and
  158. `org-edit-src-save'")
  159. (defun org-edit-src-code ()
  160. "Edit the source code example at point.
  161. The example is copied to a separate buffer, and that buffer is switched
  162. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  163. This will remove the original code in the Org buffer, and replace it with
  164. the edited version."
  165. (interactive)
  166. (let ((line (org-current-line))
  167. (col (current-column))
  168. (case-fold-search t)
  169. (msg (substitute-command-keys
  170. "Edit, then exit with C-c ' (C-c and single quote)"))
  171. (info (org-edit-src-find-region-and-lang))
  172. (org-mode-p (eq major-mode 'org-mode))
  173. (beg (make-marker))
  174. (end (make-marker))
  175. (preserve-indentation org-src-preserve-indentation)
  176. block-nindent total-nindent ovl lang lang-f single lfmt code begline buffer)
  177. (if (not info)
  178. nil
  179. (setq beg (move-marker beg (nth 0 info))
  180. end (move-marker end (nth 1 info))
  181. code (buffer-substring-no-properties beg end)
  182. lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
  183. (nth 2 info))
  184. lang (if (symbolp lang) (symbol-name lang) lang)
  185. single (nth 3 info)
  186. lfmt (nth 4 info)
  187. block-nindent (nth 5 info)
  188. lang-f (intern (concat lang "-mode"))
  189. begline (save-excursion (goto-char beg) (org-current-line)))
  190. (unless (functionp lang-f)
  191. (error "No such language mode: %s" lang-f))
  192. (org-goto-line line)
  193. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  194. (if org-src-ask-before-returning-to-edit-buffer
  195. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
  196. (org-src-switch-to-buffer buffer 'return)
  197. (when buffer
  198. (with-current-buffer buffer
  199. (if (boundp 'org-edit-src-overlay)
  200. (org-delete-overlay org-edit-src-overlay)))
  201. (kill-buffer buffer))
  202. (setq buffer (generate-new-buffer
  203. (org-src-construct-edit-buffer-name (buffer-name) lang)))
  204. (setq ovl (org-make-overlay beg end))
  205. (org-overlay-put ovl 'edit-buffer buffer)
  206. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  207. (org-overlay-put ovl 'face 'secondary-selection)
  208. (org-overlay-put ovl
  209. 'keymap
  210. (let ((map (make-sparse-keymap)))
  211. (define-key map [mouse-1] 'org-edit-src-continue)
  212. map))
  213. (org-overlay-put ovl :read-only "Leave me alone")
  214. (org-src-switch-to-buffer buffer 'edit)
  215. (if (eq single 'macro-definition)
  216. (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
  217. (insert code)
  218. (remove-text-properties (point-min) (point-max)
  219. '(display nil invisible nil intangible nil))
  220. (unless preserve-indentation
  221. (setq total-nindent (or (org-do-remove-indentation) 0)))
  222. (let ((org-inhibit-startup t))
  223. (funcall lang-f))
  224. (set (make-local-variable 'org-edit-src-force-single-line) single)
  225. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  226. (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
  227. (when lfmt
  228. (set (make-local-variable 'org-coderef-label-format) lfmt))
  229. (when org-mode-p
  230. (goto-char (point-min))
  231. (while (re-search-forward "^," nil t)
  232. (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
  233. (replace-match "")))
  234. (org-goto-line (1+ (- line begline)))
  235. (org-move-to-column
  236. (if preserve-indentation col (max 0 (- col total-nindent))))
  237. (org-set-local 'org-edit-src-beg-marker beg)
  238. (org-set-local 'org-edit-src-end-marker end)
  239. (org-set-local 'org-edit-src-overlay ovl)
  240. (org-set-local 'org-edit-src-block-indentation block-nindent)
  241. (org-src-mode)
  242. (set-buffer-modified-p nil)
  243. (and org-edit-src-persistent-message
  244. (org-set-local 'header-line-format msg)))
  245. (message "%s" msg)
  246. t)))
  247. (defun org-edit-src-continue (e)
  248. (interactive "e")
  249. (mouse-set-point e)
  250. (let ((buf (get-char-property (point) 'edit-buffer)))
  251. (if buf (org-src-switch-to-buffer buf 'continue)
  252. (error "Something is wrong here"))))
  253. (defun org-src-switch-to-buffer (buffer context)
  254. (case org-src-window-setup
  255. ('current-window
  256. (switch-to-buffer buffer))
  257. ('other-window
  258. (switch-to-buffer-other-window buffer))
  259. ('other-frame
  260. (case context
  261. ('exit
  262. (let ((frame (selected-frame)))
  263. (switch-to-buffer-other-frame buffer)
  264. (delete-frame frame)))
  265. ('save
  266. (kill-buffer (current-buffer))
  267. (switch-to-buffer buffer))
  268. (t
  269. (switch-to-buffer-other-frame buffer))))
  270. ('reorganize-frame
  271. (if (eq context 'edit) (delete-other-windows))
  272. (org-switch-to-buffer-other-window buffer)
  273. (if (eq context 'exit) (delete-other-windows)))
  274. (t
  275. (message "Invalid value %s for org-src-window-setup"
  276. (symbol-name org-src-window-setup))
  277. (switch-to-buffer buffer))))
  278. (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
  279. "Construct the buffer name for a source editing buffer"
  280. (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
  281. (defun org-edit-src-find-buffer (beg end)
  282. "Find a source editing buffer that is already editing the region BEG to END."
  283. (catch 'exit
  284. (mapc
  285. (lambda (b)
  286. (with-current-buffer b
  287. (if (and (string-match "\\`*Org Src " (buffer-name))
  288. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  289. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  290. (equal beg org-edit-src-beg-marker)
  291. (equal end org-edit-src-end-marker))
  292. (throw 'exit (current-buffer)))))
  293. (buffer-list))
  294. nil))
  295. (defun org-edit-fixed-width-region ()
  296. "Edit the fixed-width ascii drawing at point.
  297. This must be a region where each line starts with a colon followed by
  298. a space character.
  299. An new buffer is created and the fixed-width region is copied into it,
  300. and the buffer is switched into `artist-mode' for editing. When done,
  301. exit with \\[org-edit-src-exit]. The edited text will then replace
  302. the fragment in the Org-mode buffer."
  303. (interactive)
  304. (let ((line (org-current-line))
  305. (col (current-column))
  306. (case-fold-search t)
  307. (msg (substitute-command-keys
  308. "Edit, then exit with C-c ' (C-c and single quote)"))
  309. (org-mode-p (eq major-mode 'org-mode))
  310. (beg (make-marker))
  311. (end (make-marker))
  312. (preserve-indentation org-src-preserve-indentation)
  313. block-nindent ovl beg1 end1 code begline buffer)
  314. (beginning-of-line 1)
  315. (if (looking-at "[ \t]*[^:\n \t]")
  316. nil
  317. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  318. (setq beg1 (point) end1 beg1)
  319. (save-excursion
  320. (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
  321. (setq beg1 (point-at-bol 2))
  322. (setq beg1 (point))))
  323. (save-excursion
  324. (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
  325. (setq end1 (1- (match-beginning 0)))
  326. (setq end1 (point))))
  327. (org-goto-line line))
  328. (setq beg (move-marker beg beg1)
  329. end (move-marker end end1)
  330. code (buffer-substring-no-properties beg end)
  331. begline (save-excursion (goto-char beg) (org-current-line)))
  332. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  333. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  334. (switch-to-buffer buffer)
  335. (when buffer
  336. (with-current-buffer buffer
  337. (if (boundp 'org-edit-src-overlay)
  338. (org-delete-overlay org-edit-src-overlay)))
  339. (kill-buffer buffer))
  340. (setq buffer (generate-new-buffer
  341. (org-src-construct-edit-buffer-name
  342. (buffer-name) "Fixed Width")))
  343. (setq ovl (org-make-overlay beg end))
  344. (org-overlay-put ovl 'face 'secondary-selection)
  345. (org-overlay-put ovl 'edit-buffer buffer)
  346. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  347. (org-overlay-put ovl 'face 'secondary-selection)
  348. (org-overlay-put ovl
  349. 'keymap
  350. (let ((map (make-sparse-keymap)))
  351. (define-key map [mouse-1] 'org-edit-src-continue)
  352. map))
  353. (org-overlay-put ovl :read-only "Leave me alone")
  354. (switch-to-buffer buffer)
  355. (insert code)
  356. (remove-text-properties (point-min) (point-max)
  357. '(display nil invisible nil intangible nil))
  358. (setq block-nindent (or (org-do-remove-indentation) 0))
  359. (cond
  360. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  361. (fundamental-mode)
  362. (artist-mode 1))
  363. (t (funcall org-edit-fixed-width-region-mode)))
  364. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  365. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  366. (set (make-local-variable 'org-edit-src-picture) t)
  367. (goto-char (point-min))
  368. (while (re-search-forward "^[ \t]*: ?" nil t)
  369. (replace-match ""))
  370. (org-goto-line (1+ (- line begline)))
  371. (org-move-to-column (max 0 (- col block-nindent 2)))
  372. (org-set-local 'org-edit-src-beg-marker beg)
  373. (org-set-local 'org-edit-src-end-marker end)
  374. (org-set-local 'org-edit-src-overlay ovl)
  375. (org-set-local 'org-edit-src-block-indentation block-nindent)
  376. (org-set-local 'org-edit-src-content-indentation 0)
  377. (org-set-local 'org-src-preserve-indentation nil)
  378. (org-src-mode)
  379. (set-buffer-modified-p nil)
  380. (and org-edit-src-persistent-message
  381. (org-set-local 'header-line-format msg)))
  382. (message "%s" msg)
  383. t)))
  384. (defun org-edit-src-find-region-and-lang ()
  385. "Find the region and language for a local edit.
  386. Return a list with beginning and end of the region, a string representing
  387. the language, a switch telling if the content should be in a single line."
  388. (let ((re-list
  389. (append
  390. org-edit-src-region-extra
  391. '(
  392. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  393. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  394. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  395. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  396. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  397. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  398. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  399. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  400. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  401. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  402. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  403. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  404. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  405. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  406. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  407. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  408. ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
  409. "\n" "fundamental" macro-definition)
  410. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  411. )))
  412. (pos (point))
  413. re1 re2 single beg end lang lfmt match-re1 ind entry)
  414. (catch 'exit
  415. (while (setq entry (pop re-list))
  416. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  417. single (nth 3 entry))
  418. (save-excursion
  419. (if (or (looking-at re1)
  420. (re-search-backward re1 nil t))
  421. (progn
  422. (setq match-re1 (match-string 0))
  423. (setq beg (match-end 0)
  424. lang (org-edit-src-get-lang lang)
  425. lfmt (org-edit-src-get-label-format match-re1)
  426. ind (org-edit-src-get-indentation (match-beginning 0)))
  427. (if (and (re-search-forward re2 nil t)
  428. (>= (match-end 0) pos))
  429. (throw 'exit (list beg (match-beginning 0)
  430. lang single lfmt ind))))
  431. (if (or (looking-at re2)
  432. (re-search-forward re2 nil t))
  433. (progn
  434. (setq end (match-beginning 0))
  435. (if (and (re-search-backward re1 nil t)
  436. (<= (match-beginning 0) pos))
  437. (progn
  438. (setq lfmt (org-edit-src-get-label-format
  439. (match-string 0))
  440. ind (org-edit-src-get-indentation
  441. (match-beginning 0)))
  442. (throw 'exit
  443. (list (match-end 0) end
  444. (org-edit-src-get-lang lang)
  445. single lfmt ind))))))))))))
  446. (defun org-edit-src-get-lang (lang)
  447. "Extract the src language."
  448. (let ((m (match-string 0)))
  449. (cond
  450. ((stringp lang) lang)
  451. ((integerp lang) (match-string lang))
  452. ((and (eq lang 'lang)
  453. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  454. (match-string 1 m))
  455. ((and (eq lang 'style)
  456. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  457. (match-string 1 m))
  458. (t "fundamental"))))
  459. (defun org-edit-src-get-label-format (s)
  460. "Extract the label format."
  461. (save-match-data
  462. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  463. (match-string 1 s))))
  464. (defun org-edit-src-get-indentation (pos)
  465. "Count leading whitespace characters on line"
  466. (save-match-data
  467. (goto-char pos)
  468. (org-get-indentation)))
  469. (defun org-edit-src-exit (&optional context)
  470. "Exit special edit and protect problematic lines."
  471. (interactive)
  472. (unless org-edit-src-from-org-mode
  473. (error "This is not a sub-editing buffer, something is wrong..."))
  474. (let* ((beg org-edit-src-beg-marker)
  475. (end org-edit-src-end-marker)
  476. (ovl org-edit-src-overlay)
  477. (buffer (current-buffer))
  478. (single (org-bound-and-true-p org-edit-src-force-single-line))
  479. (macro (eq single 'macro-definition))
  480. (total-nindent (+ (or org-edit-src-block-indentation 0)
  481. org-edit-src-content-indentation))
  482. (preserve-indentation org-src-preserve-indentation)
  483. (delta 0) code line col indent)
  484. (untabify (point-min) (point-max))
  485. (save-excursion
  486. (goto-char (point-min))
  487. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  488. (unless macro
  489. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))))
  490. (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
  491. 1
  492. (org-current-line))
  493. col (current-column))
  494. (when single
  495. (goto-char (point-min))
  496. (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
  497. (goto-char (point-min))
  498. (let ((cnt 0))
  499. (while (re-search-forward "\n" nil t)
  500. (setq cnt (1+ cnt))
  501. (replace-match (if macro "\\n" " ") t t))
  502. (when (and macro (> cnt 0))
  503. (goto-char (point-max)) (insert "\\n")))
  504. (goto-char (point-min))
  505. (if (looking-at "\\s-*") (replace-match " ")))
  506. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  507. (goto-char (point-min))
  508. (while (re-search-forward
  509. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  510. (if (eq (org-current-line) line) (setq delta (1+ delta)))
  511. (replace-match ",\\1")))
  512. (when (org-bound-and-true-p org-edit-src-picture)
  513. (setq preserve-indentation nil)
  514. (untabify (point-min) (point-max))
  515. (goto-char (point-min))
  516. (while (re-search-forward "^" nil t)
  517. (replace-match ": ")))
  518. (unless (or single preserve-indentation (= total-nindent 0))
  519. (setq indent (make-string total-nindent ?\ ))
  520. (goto-char (point-min))
  521. (while (re-search-forward "^" nil t)
  522. (replace-match indent)))
  523. (if (org-bound-and-true-p org-edit-src-picture)
  524. (setq total-nindent (+ total-nindent 2)))
  525. (setq code (buffer-string))
  526. (set-buffer-modified-p nil)
  527. (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
  528. (kill-buffer buffer)
  529. (goto-char beg)
  530. (delete-region beg end)
  531. (insert code)
  532. (goto-char beg)
  533. (if single (just-one-space))
  534. (org-goto-line (1- (+ (org-current-line) line)))
  535. (org-move-to-column (if preserve-indentation col (+ col total-nindent delta)))
  536. (move-marker beg nil)
  537. (move-marker end nil)))
  538. (defun org-edit-src-save ()
  539. "Save parent buffer with current state source-code buffer."
  540. (interactive)
  541. (let ((p (point)) (m (mark)) msg)
  542. (save-window-excursion
  543. (org-edit-src-exit 'save)
  544. (save-buffer)
  545. (setq msg (current-message))
  546. (if (eq org-src-window-setup 'other-frame)
  547. (let ((org-src-window-setup 'current-window))
  548. (org-edit-src-code))
  549. (org-edit-src-code)))
  550. (push-mark m 'nomessage)
  551. (goto-char (min p (point-max)))
  552. (message (or msg ""))))
  553. (defun org-src-mode-configure-edit-buffer ()
  554. (when org-edit-src-from-org-mode
  555. (setq buffer-offer-save t)
  556. (setq buffer-file-name
  557. (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
  558. "[" (buffer-name) "]"))
  559. (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
  560. '(org-edit-src-save))
  561. (org-add-hook 'kill-buffer-hook
  562. '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
  563. (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
  564. (provide 'org-src)
  565. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  566. ;;; org-src.el ends here