org-src.el 27 KB

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