org-src.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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
  173. switched to the correct language mode. When done, exit with
  174. \\[org-edit-src-exit]. This will remove the original code in the
  175. Org buffer, and replace it with the edited version. Optional
  176. argument CONTEXT is used by \\[org-edit-src-save] when calling
  177. this function. See \\[org-src-window-setup] to configure the
  178. display of windows containing the Org buffer and the code
  179. buffer."
  180. (interactive)
  181. (unless (eq context 'save)
  182. (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
  183. (let ((line (org-current-line))
  184. (col (current-column))
  185. (case-fold-search t)
  186. (info (org-edit-src-find-region-and-lang))
  187. (babel-info (org-babel-get-src-block-info))
  188. (org-mode-p (eq major-mode 'org-mode))
  189. (beg (make-marker))
  190. (end (make-marker))
  191. (preserve-indentation org-src-preserve-indentation)
  192. (allow-write-back-p (null code))
  193. block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg)
  194. (if (not info)
  195. nil
  196. (setq beg (move-marker beg (nth 0 info))
  197. end (move-marker end (nth 1 info))
  198. msg (if allow-write-back-p
  199. (substitute-command-keys
  200. "Edit, then exit with C-c ' (C-c and single quote)")
  201. "Exit with C-c ' (C-c and single quote)")
  202. code (or code (buffer-substring-no-properties beg end))
  203. lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
  204. (nth 2 info))
  205. lang (if (symbolp lang) (symbol-name lang) lang)
  206. single (nth 3 info)
  207. lfmt (nth 4 info)
  208. block-nindent (nth 5 info)
  209. lang-f (intern (concat lang "-mode"))
  210. begline (save-excursion (goto-char beg) (org-current-line)))
  211. (if (equal lang-f 'table.el-mode)
  212. (setq lang-f (lambda ()
  213. (text-mode)
  214. (if (org-bound-and-true-p flyspell-mode)
  215. (flyspell-mode -1))
  216. (table-recognize)
  217. (org-set-local 'org-edit-src-content-indentation 0))))
  218. (unless (functionp lang-f)
  219. (error "No such language mode: %s" lang-f))
  220. (org-goto-line line)
  221. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  222. (if org-src-ask-before-returning-to-edit-buffer
  223. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
  224. (org-src-switch-to-buffer buffer 'return)
  225. (when buffer
  226. (with-current-buffer buffer
  227. (if (boundp 'org-edit-src-overlay)
  228. (delete-overlay org-edit-src-overlay)))
  229. (kill-buffer buffer))
  230. (setq buffer (generate-new-buffer
  231. (or edit-buffer-name
  232. (org-src-construct-edit-buffer-name (buffer-name) lang))))
  233. (setq ovl (make-overlay beg end))
  234. (overlay-put ovl 'edit-buffer buffer)
  235. (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  236. (overlay-put ovl 'face 'secondary-selection)
  237. (overlay-put ovl
  238. 'keymap
  239. (let ((map (make-sparse-keymap)))
  240. (define-key map [mouse-1] 'org-edit-src-continue)
  241. map))
  242. (overlay-put ovl :read-only "Leave me alone")
  243. (org-src-switch-to-buffer buffer 'edit)
  244. (if (eq single 'macro-definition)
  245. (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
  246. (insert code)
  247. (remove-text-properties (point-min) (point-max)
  248. '(display nil invisible nil intangible nil))
  249. (unless preserve-indentation
  250. (setq total-nindent (or (org-do-remove-indentation) 0)))
  251. (let ((org-inhibit-startup t))
  252. (funcall lang-f))
  253. (set (make-local-variable 'org-edit-src-force-single-line) single)
  254. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  255. (set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p)
  256. (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
  257. (when babel-info
  258. (set (make-local-variable 'org-src-babel-info) babel-info))
  259. (when lfmt
  260. (set (make-local-variable 'org-coderef-label-format) lfmt))
  261. (when org-mode-p
  262. (goto-char (point-min))
  263. (while (re-search-forward "^," nil t)
  264. (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
  265. (replace-match "")))
  266. (org-goto-line (1+ (- line begline)))
  267. (org-move-to-column
  268. (if preserve-indentation col (max 0 (- col total-nindent))))
  269. (org-set-local 'org-edit-src-beg-marker beg)
  270. (org-set-local 'org-edit-src-end-marker end)
  271. (org-set-local 'org-edit-src-overlay ovl)
  272. (org-set-local 'org-edit-src-block-indentation block-nindent)
  273. (org-src-mode)
  274. (set-buffer-modified-p nil)
  275. (and org-edit-src-persistent-message
  276. (org-set-local 'header-line-format msg)))
  277. (unless quietp (message "%s" msg))
  278. t)))
  279. (defun org-edit-src-continue (e)
  280. (interactive "e")
  281. (mouse-set-point e)
  282. (let ((buf (get-char-property (point) 'edit-buffer)))
  283. (if buf (org-src-switch-to-buffer buf 'continue)
  284. (error "Something is wrong here"))))
  285. (defun org-src-switch-to-buffer (buffer context)
  286. (case org-src-window-setup
  287. ('current-window
  288. (switch-to-buffer buffer))
  289. ('other-window
  290. (switch-to-buffer-other-window buffer))
  291. ('other-frame
  292. (case context
  293. ('exit
  294. (let ((frame (selected-frame)))
  295. (switch-to-buffer-other-frame buffer)
  296. (delete-frame frame)))
  297. ('save
  298. (kill-buffer (current-buffer))
  299. (switch-to-buffer buffer))
  300. (t
  301. (switch-to-buffer-other-frame buffer))))
  302. ('reorganize-frame
  303. (if (eq context 'edit) (delete-other-windows))
  304. (org-switch-to-buffer-other-window buffer)
  305. (if (eq context 'exit) (delete-other-windows)))
  306. ('switch-invisibly
  307. (set-buffer buffer))
  308. (t
  309. (message "Invalid value %s for org-src-window-setup"
  310. (symbol-name org-src-window-setup))
  311. (switch-to-buffer buffer))))
  312. (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
  313. "Construct the buffer name for a source editing buffer."
  314. (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
  315. (defun org-edit-src-find-buffer (beg end)
  316. "Find a source editing buffer that is already editing the region BEG to END."
  317. (catch 'exit
  318. (mapc
  319. (lambda (b)
  320. (with-current-buffer b
  321. (if (and (string-match "\\`*Org Src " (buffer-name))
  322. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  323. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  324. (equal beg org-edit-src-beg-marker)
  325. (equal end org-edit-src-end-marker))
  326. (throw 'exit (current-buffer)))))
  327. (buffer-list))
  328. nil))
  329. (defun org-edit-fixed-width-region ()
  330. "Edit the fixed-width ascii drawing at point.
  331. This must be a region where each line starts with a colon followed by
  332. a space character.
  333. An new buffer is created and the fixed-width region is copied into it,
  334. and the buffer is switched into `artist-mode' for editing. When done,
  335. exit with \\[org-edit-src-exit]. The edited text will then replace
  336. the fragment in the Org-mode buffer."
  337. (interactive)
  338. (let ((line (org-current-line))
  339. (col (current-column))
  340. (case-fold-search t)
  341. (msg (substitute-command-keys
  342. "Edit, then exit with C-c ' (C-c and single quote)"))
  343. (org-mode-p (eq major-mode 'org-mode))
  344. (beg (make-marker))
  345. (end (make-marker))
  346. (preserve-indentation org-src-preserve-indentation)
  347. block-nindent ovl beg1 end1 code begline buffer)
  348. (beginning-of-line 1)
  349. (if (looking-at "[ \t]*[^:\n \t]")
  350. nil
  351. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  352. (setq beg1 (point) end1 beg1)
  353. (save-excursion
  354. (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
  355. (setq beg1 (point-at-bol 2))
  356. (setq beg1 (point))))
  357. (save-excursion
  358. (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
  359. (setq end1 (1- (match-beginning 0)))
  360. (setq end1 (point))))
  361. (org-goto-line line))
  362. (setq beg (move-marker beg beg1)
  363. end (move-marker end end1)
  364. code (buffer-substring-no-properties beg end)
  365. begline (save-excursion (goto-char beg) (org-current-line)))
  366. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  367. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  368. (switch-to-buffer buffer)
  369. (when buffer
  370. (with-current-buffer buffer
  371. (if (boundp 'org-edit-src-overlay)
  372. (delete-overlay org-edit-src-overlay)))
  373. (kill-buffer buffer))
  374. (setq buffer (generate-new-buffer
  375. (org-src-construct-edit-buffer-name
  376. (buffer-name) "Fixed Width")))
  377. (setq ovl (make-overlay beg end))
  378. (overlay-put ovl 'face 'secondary-selection)
  379. (overlay-put ovl 'edit-buffer buffer)
  380. (overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  381. (overlay-put ovl 'face 'secondary-selection)
  382. (overlay-put ovl
  383. 'keymap
  384. (let ((map (make-sparse-keymap)))
  385. (define-key map [mouse-1] 'org-edit-src-continue)
  386. map))
  387. (overlay-put ovl :read-only "Leave me alone")
  388. (switch-to-buffer buffer)
  389. (insert code)
  390. (remove-text-properties (point-min) (point-max)
  391. '(display nil invisible nil intangible nil))
  392. (setq block-nindent (or (org-do-remove-indentation) 0))
  393. (cond
  394. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  395. (fundamental-mode)
  396. (artist-mode 1))
  397. (t (funcall org-edit-fixed-width-region-mode)))
  398. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  399. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  400. (set (make-local-variable 'org-edit-src-picture) t)
  401. (goto-char (point-min))
  402. (while (re-search-forward "^[ \t]*: ?" nil t)
  403. (replace-match ""))
  404. (org-goto-line (1+ (- line begline)))
  405. (org-move-to-column (max 0 (- col block-nindent 2)))
  406. (org-set-local 'org-edit-src-beg-marker beg)
  407. (org-set-local 'org-edit-src-end-marker end)
  408. (org-set-local 'org-edit-src-overlay ovl)
  409. (org-set-local 'org-edit-src-block-indentation block-nindent)
  410. (org-set-local 'org-edit-src-content-indentation 0)
  411. (org-set-local 'org-src-preserve-indentation nil)
  412. (org-src-mode)
  413. (set-buffer-modified-p nil)
  414. (and org-edit-src-persistent-message
  415. (org-set-local 'header-line-format msg)))
  416. (message "%s" msg)
  417. t)))
  418. (defun org-edit-src-find-region-and-lang ()
  419. "Find the region and language for a local edit.
  420. Return a list with beginning and end of the region, a string representing
  421. the language, a switch telling if the content should be in a single line."
  422. (let ((re-list
  423. (append
  424. org-edit-src-region-extra
  425. '(
  426. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  427. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  428. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  429. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  430. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  431. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  432. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  433. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  434. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  435. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  436. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  437. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  438. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  439. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  440. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  441. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  442. ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
  443. "\n" "fundamental" macro-definition)
  444. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  445. )))
  446. (pos (point))
  447. re1 re2 single beg end lang lfmt match-re1 ind entry)
  448. (catch 'exit
  449. (while (setq entry (pop re-list))
  450. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  451. single (nth 3 entry))
  452. (save-excursion
  453. (if (or (looking-at re1)
  454. (re-search-backward re1 nil t))
  455. (progn
  456. (setq match-re1 (match-string 0))
  457. (setq beg (match-end 0)
  458. lang (org-edit-src-get-lang lang)
  459. lfmt (org-edit-src-get-label-format match-re1)
  460. ind (org-edit-src-get-indentation (match-beginning 0)))
  461. (if (and (re-search-forward re2 nil t)
  462. (>= (match-end 0) pos))
  463. (throw 'exit (list beg (match-beginning 0)
  464. lang single lfmt ind))))
  465. (if (or (looking-at re2)
  466. (re-search-forward re2 nil t))
  467. (progn
  468. (setq end (match-beginning 0))
  469. (if (and (re-search-backward re1 nil t)
  470. (<= (match-beginning 0) pos))
  471. (progn
  472. (setq lfmt (org-edit-src-get-label-format
  473. (match-string 0))
  474. ind (org-edit-src-get-indentation
  475. (match-beginning 0)))
  476. (throw 'exit
  477. (list (match-end 0) end
  478. (org-edit-src-get-lang lang)
  479. single lfmt ind)))))))))
  480. (when (org-at-table.el-p)
  481. (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
  482. (setq beg (1+ (point-at-eol)))
  483. (goto-char beg)
  484. (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
  485. (progn (goto-char (point-max)) (newline)))
  486. (setq end (point-at-bol))
  487. (setq ind (org-edit-src-get-indentation beg))
  488. (throw 'exit (list beg end 'table.el nil nil ind))))))
  489. (defun org-edit-src-get-lang (lang)
  490. "Extract the src language."
  491. (let ((m (match-string 0)))
  492. (cond
  493. ((stringp lang) lang)
  494. ((integerp lang) (match-string lang))
  495. ((and (eq lang 'lang)
  496. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  497. (match-string 1 m))
  498. ((and (eq lang 'style)
  499. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  500. (match-string 1 m))
  501. (t "fundamental"))))
  502. (defun org-edit-src-get-label-format (s)
  503. "Extract the label format."
  504. (save-match-data
  505. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  506. (match-string 1 s))))
  507. (defun org-edit-src-get-indentation (pos)
  508. "Count leading whitespace characters on line."
  509. (save-match-data
  510. (goto-char pos)
  511. (org-get-indentation)))
  512. (defun org-edit-src-exit (&optional context)
  513. "Exit special edit and protect problematic lines."
  514. (interactive)
  515. (unless (org-bound-and-true-p org-edit-src-from-org-mode)
  516. (error "This is not a sub-editing buffer, something is wrong"))
  517. (widen)
  518. (let* ((beg org-edit-src-beg-marker)
  519. (end org-edit-src-end-marker)
  520. (ovl org-edit-src-overlay)
  521. (buffer (current-buffer))
  522. (single (org-bound-and-true-p org-edit-src-force-single-line))
  523. (macro (eq single 'macro-definition))
  524. (total-nindent (+ (or org-edit-src-block-indentation 0)
  525. org-edit-src-content-indentation))
  526. (preserve-indentation org-src-preserve-indentation)
  527. (allow-write-back-p (org-bound-and-true-p org-edit-src-allow-write-back-p))
  528. (delta 0) code line col indent)
  529. (when allow-write-back-p
  530. (unless preserve-indentation (untabify (point-min) (point-max)))
  531. (save-excursion
  532. (goto-char (point-min))
  533. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  534. (unless macro
  535. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))))
  536. (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
  537. 1
  538. (org-current-line))
  539. col (current-column))
  540. (when allow-write-back-p
  541. (when single
  542. (goto-char (point-min))
  543. (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
  544. (goto-char (point-min))
  545. (let ((cnt 0))
  546. (while (re-search-forward "\n" nil t)
  547. (setq cnt (1+ cnt))
  548. (replace-match (if macro "\\n" " ") t t))
  549. (when (and macro (> cnt 0))
  550. (goto-char (point-max)) (insert "\\n")))
  551. (goto-char (point-min))
  552. (if (looking-at "\\s-*") (replace-match " ")))
  553. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  554. (goto-char (point-min))
  555. (while (re-search-forward
  556. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  557. (if (eq (org-current-line) line) (setq delta (1+ delta)))
  558. (replace-match ",\\1")))
  559. (when (org-bound-and-true-p org-edit-src-picture)
  560. (setq preserve-indentation nil)
  561. (untabify (point-min) (point-max))
  562. (goto-char (point-min))
  563. (while (re-search-forward "^" nil t)
  564. (replace-match ": ")))
  565. (unless (or single preserve-indentation (= total-nindent 0))
  566. (setq indent (make-string total-nindent ?\ ))
  567. (goto-char (point-min))
  568. (while (re-search-forward "^" nil t)
  569. (replace-match indent)))
  570. (if (org-bound-and-true-p org-edit-src-picture)
  571. (setq total-nindent (+ total-nindent 2)))
  572. (setq code (buffer-string))
  573. (set-buffer-modified-p nil))
  574. (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
  575. (kill-buffer buffer)
  576. (goto-char beg)
  577. (when allow-write-back-p
  578. (delete-region beg end)
  579. (insert code)
  580. (goto-char beg)
  581. (if single (just-one-space)))
  582. (if (memq t (mapcar (lambda (overlay)
  583. (eq (overlay-get overlay 'invisible)
  584. 'org-hide-block))
  585. (overlays-at (point))))
  586. ;; Block is hidden; put point at start of block
  587. (beginning-of-line 0)
  588. ;; Block is visible, put point where it was in the code buffer
  589. (org-goto-line (1- (+ (org-current-line) line)))
  590. (org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
  591. (move-marker beg nil)
  592. (move-marker end nil))
  593. (unless (eq context 'save)
  594. (when org-edit-src-saved-temp-window-config
  595. (set-window-configuration org-edit-src-saved-temp-window-config)
  596. (setq org-edit-src-saved-temp-window-config nil))))
  597. (defun org-edit-src-save ()
  598. "Save parent buffer with current state source-code buffer."
  599. (interactive)
  600. (let ((p (point)) (m (mark)) msg)
  601. (save-window-excursion
  602. (org-edit-src-exit 'save)
  603. (save-buffer)
  604. (setq msg (current-message))
  605. (if (eq org-src-window-setup 'other-frame)
  606. (let ((org-src-window-setup 'current-window))
  607. (org-edit-src-code 'save))
  608. (org-edit-src-code 'save)))
  609. (push-mark m 'nomessage)
  610. (goto-char (min p (point-max)))
  611. (message (or msg ""))))
  612. (defun org-src-mode-configure-edit-buffer ()
  613. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  614. (org-add-hook 'kill-buffer-hook
  615. '(lambda () (delete-overlay org-edit-src-overlay)) nil 'local)
  616. (if (org-bound-and-true-p org-edit-src-allow-write-back-p)
  617. (progn
  618. (setq buffer-offer-save t)
  619. (setq buffer-file-name
  620. (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
  621. "[" (buffer-name) "]"))
  622. (if (featurep 'xemacs)
  623. (progn
  624. (make-variable-buffer-local 'write-contents-hooks) ; needed only for 21.4
  625. (setq write-contents-hooks '(org-edit-src-save)))
  626. (setq write-contents-functions '(org-edit-src-save))))
  627. (setq buffer-read-only t))))
  628. (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
  629. (defun org-src-associate-babel-session (info)
  630. "Associate edit buffer with comint session."
  631. (interactive)
  632. (let ((session (cdr (assoc :session (nth 2 info)))))
  633. (and session (not (string= session "none"))
  634. (org-babel-comint-buffer-livep session)
  635. ((lambda (f) (and (fboundp f) (funcall f session)))
  636. (intern (format "org-babel-%s-associate-session" (nth 0 info)))))))
  637. (defun org-src-babel-configure-edit-buffer ()
  638. (when org-src-babel-info
  639. (org-src-associate-babel-session org-src-babel-info)))
  640. (org-add-hook 'org-src-mode-hook 'org-src-babel-configure-edit-buffer)
  641. (defmacro org-src-do-at-code-block (&rest body)
  642. "Execute a command from an edit buffer in the Org-mode buffer."
  643. `(let ((beg-marker org-edit-src-beg-marker))
  644. (if beg-marker
  645. (with-current-buffer (marker-buffer beg-marker)
  646. (goto-char (marker-position beg-marker))
  647. ,@body))))
  648. (defun org-src-do-key-sequence-at-code-block (&optional key)
  649. "Execute key sequence at code block in the source Org buffer.
  650. The command bound to KEY in the Org-babel key map is executed
  651. remotely with point temporarily at the start of the code block in
  652. the Org buffer.
  653. This command is not bound to a key by default, to avoid conflicts
  654. with language major mode bindings. To bind it to C-c @ in all
  655. language major modes, you could use
  656. (add-hook 'org-src-mode-hook
  657. (lambda () (define-key org-src-mode-map \"\\C-c@\"
  658. 'org-src-do-key-sequence-at-code-block)))
  659. In that case, for example, C-c @ t issued in code edit buffers
  660. would tangle the current Org code block, C-c @ e would execute
  661. the block and C-c @ h would display the other available
  662. Org-babel commands."
  663. (interactive "kOrg-babel key: ")
  664. (if (equal key (kbd "C-g")) (keyboard-quit)
  665. (org-edit-src-save)
  666. (org-src-do-at-code-block
  667. (call-interactively
  668. (lookup-key org-babel-map key)))))
  669. (defun org-src-font-lock-fontify-block (lang start end)
  670. "Fontify code block.
  671. This function is called by emacs automatic fontification, as long
  672. as `org-src-fontify-natively' is non-nil. For manual
  673. fontification of code blocks see `org-src-fontify-block' and
  674. `org-src-fontify-buffer'"
  675. (let* ((lang-mode (org-src-get-lang-mode lang))
  676. (string (buffer-substring-no-properties start end))
  677. (modified (buffer-modified-p))
  678. (org-buffer (current-buffer)) pos next)
  679. (remove-text-properties start end '(face nil))
  680. (with-temp-buffer
  681. (insert string)
  682. (funcall lang-mode)
  683. (font-lock-fontify-buffer)
  684. (setq pos (point-min))
  685. (while (setq next (next-single-property-change pos 'face))
  686. (put-text-property
  687. (+ start (1- pos)) (+ start next) 'face
  688. (get-text-property pos 'face) org-buffer)
  689. (setq pos next)))
  690. (add-text-properties
  691. start end
  692. '(font-lock-fontified t fontified t font-lock-multiline t))
  693. (set-buffer-modified-p modified))
  694. t) ;; Tell `org-fontify-meta-lines-and-blocks' that we fontified
  695. (defun org-src-fontify-block ()
  696. "Fontify code block at point."
  697. (interactive)
  698. (save-excursion
  699. (let ((org-src-fontify-natively t)
  700. (info (org-edit-src-find-region-and-lang)))
  701. (font-lock-fontify-region (nth 0 info) (nth 1 info)))))
  702. (defun org-src-fontify-buffer ()
  703. "Fontify all code blocks in the current buffer"
  704. (interactive)
  705. (org-babel-map-src-blocks nil
  706. (org-src-fontify-block)))
  707. (defun org-src-get-lang-mode (lang)
  708. "Return major mode that should be used for LANG.
  709. LANG is a string, and the returned major mode is a symbol."
  710. (intern
  711. (concat
  712. ((lambda (l) (if (symbolp l) (symbol-name l) l))
  713. (or (cdr (assoc lang org-src-lang-modes)) lang)) "-mode")))
  714. (provide 'org-src)
  715. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  716. ;;; org-src.el ends here