org-src.el 31 KB

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