org-src.el 31 KB

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