org-src.el 25 KB

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