org-src.el 26 KB

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