org-src.el 25 KB

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