org-src.el 31 KB

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