org-src.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. ;;; org-src.el --- Source code examples in Org
  2. ;;
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  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: TAG=6.35c
  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)
  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 code 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. code (buffer-substring-no-properties beg end)
  191. lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
  192. (nth 2 info))
  193. lang (if (symbolp lang) (symbol-name lang) lang)
  194. single (nth 3 info)
  195. lfmt (nth 4 info)
  196. block-nindent (nth 5 info)
  197. lang-f (intern (concat lang "-mode"))
  198. begline (save-excursion (goto-char beg) (org-current-line)))
  199. (if (equal lang-f 'table.el-mode)
  200. (setq lang-f (lambda ()
  201. (text-mode)
  202. (if (org-bound-and-true-p flyspell-mode)
  203. (flyspell-mode -1))
  204. (table-recognize)
  205. (org-set-local 'org-edit-src-content-indentation 0))))
  206. (unless (functionp lang-f)
  207. (error "No such language mode: %s" lang-f))
  208. (org-goto-line line)
  209. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  210. (if org-src-ask-before-returning-to-edit-buffer
  211. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: ") t))
  212. (org-src-switch-to-buffer buffer 'return)
  213. (when buffer
  214. (with-current-buffer buffer
  215. (if (boundp 'org-edit-src-overlay)
  216. (org-delete-overlay org-edit-src-overlay)))
  217. (kill-buffer buffer))
  218. (setq buffer (generate-new-buffer
  219. (org-src-construct-edit-buffer-name (buffer-name) lang)))
  220. (setq ovl (org-make-overlay beg end))
  221. (org-overlay-put ovl 'edit-buffer buffer)
  222. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  223. (org-overlay-put ovl 'face 'secondary-selection)
  224. (org-overlay-put ovl
  225. 'keymap
  226. (let ((map (make-sparse-keymap)))
  227. (define-key map [mouse-1] 'org-edit-src-continue)
  228. map))
  229. (org-overlay-put ovl :read-only "Leave me alone")
  230. (org-src-switch-to-buffer buffer 'edit)
  231. (if (eq single 'macro-definition)
  232. (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
  233. (insert code)
  234. (remove-text-properties (point-min) (point-max)
  235. '(display nil invisible nil intangible nil))
  236. (unless preserve-indentation
  237. (setq total-nindent (or (org-do-remove-indentation) 0)))
  238. (let ((org-inhibit-startup t))
  239. (funcall lang-f))
  240. (set (make-local-variable 'org-edit-src-force-single-line) single)
  241. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  242. (set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
  243. (when lfmt
  244. (set (make-local-variable 'org-coderef-label-format) lfmt))
  245. (when org-mode-p
  246. (goto-char (point-min))
  247. (while (re-search-forward "^," nil t)
  248. (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
  249. (replace-match "")))
  250. (org-goto-line (1+ (- line begline)))
  251. (org-move-to-column
  252. (if preserve-indentation col (max 0 (- col total-nindent))))
  253. (org-set-local 'org-edit-src-beg-marker beg)
  254. (org-set-local 'org-edit-src-end-marker end)
  255. (org-set-local 'org-edit-src-overlay ovl)
  256. (org-set-local 'org-edit-src-block-indentation block-nindent)
  257. (org-src-mode)
  258. (set-buffer-modified-p nil)
  259. (and org-edit-src-persistent-message
  260. (org-set-local 'header-line-format msg)))
  261. (message "%s" msg)
  262. t)))
  263. (defun org-edit-src-continue (e)
  264. (interactive "e")
  265. (mouse-set-point e)
  266. (let ((buf (get-char-property (point) 'edit-buffer)))
  267. (if buf (org-src-switch-to-buffer buf 'continue)
  268. (error "Something is wrong here"))))
  269. (defun org-src-switch-to-buffer (buffer context)
  270. (case org-src-window-setup
  271. ('current-window
  272. (switch-to-buffer buffer))
  273. ('other-window
  274. (switch-to-buffer-other-window buffer))
  275. ('other-frame
  276. (case context
  277. ('exit
  278. (let ((frame (selected-frame)))
  279. (switch-to-buffer-other-frame buffer)
  280. (delete-frame frame)))
  281. ('save
  282. (kill-buffer (current-buffer))
  283. (switch-to-buffer buffer))
  284. (t
  285. (switch-to-buffer-other-frame buffer))))
  286. ('reorganize-frame
  287. (if (eq context 'edit) (delete-other-windows))
  288. (org-switch-to-buffer-other-window buffer)
  289. (if (eq context 'exit) (delete-other-windows)))
  290. (t
  291. (message "Invalid value %s for org-src-window-setup"
  292. (symbol-name org-src-window-setup))
  293. (switch-to-buffer buffer))))
  294. (defun org-src-construct-edit-buffer-name (org-buffer-name lang)
  295. "Construct the buffer name for a source editing buffer"
  296. (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
  297. (defun org-edit-src-find-buffer (beg end)
  298. "Find a source editing buffer that is already editing the region BEG to END."
  299. (catch 'exit
  300. (mapc
  301. (lambda (b)
  302. (with-current-buffer b
  303. (if (and (string-match "\\`*Org Src " (buffer-name))
  304. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  305. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  306. (equal beg org-edit-src-beg-marker)
  307. (equal end org-edit-src-end-marker))
  308. (throw 'exit (current-buffer)))))
  309. (buffer-list))
  310. nil))
  311. (defun org-edit-fixed-width-region ()
  312. "Edit the fixed-width ascii drawing at point.
  313. This must be a region where each line starts with a colon followed by
  314. a space character.
  315. An new buffer is created and the fixed-width region is copied into it,
  316. and the buffer is switched into `artist-mode' for editing. When done,
  317. exit with \\[org-edit-src-exit]. The edited text will then replace
  318. the fragment in the Org-mode buffer."
  319. (interactive)
  320. (let ((line (org-current-line))
  321. (col (current-column))
  322. (case-fold-search t)
  323. (msg (substitute-command-keys
  324. "Edit, then exit with C-c ' (C-c and single quote)"))
  325. (org-mode-p (eq major-mode 'org-mode))
  326. (beg (make-marker))
  327. (end (make-marker))
  328. (preserve-indentation org-src-preserve-indentation)
  329. block-nindent ovl beg1 end1 code begline buffer)
  330. (beginning-of-line 1)
  331. (if (looking-at "[ \t]*[^:\n \t]")
  332. nil
  333. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  334. (setq beg1 (point) end1 beg1)
  335. (save-excursion
  336. (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
  337. (setq beg1 (point-at-bol 2))
  338. (setq beg1 (point))))
  339. (save-excursion
  340. (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
  341. (setq end1 (1- (match-beginning 0)))
  342. (setq end1 (point))))
  343. (org-goto-line line))
  344. (setq beg (move-marker beg beg1)
  345. end (move-marker end end1)
  346. code (buffer-substring-no-properties beg end)
  347. begline (save-excursion (goto-char beg) (org-current-line)))
  348. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  349. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  350. (switch-to-buffer buffer)
  351. (when buffer
  352. (with-current-buffer buffer
  353. (if (boundp 'org-edit-src-overlay)
  354. (org-delete-overlay org-edit-src-overlay)))
  355. (kill-buffer buffer))
  356. (setq buffer (generate-new-buffer
  357. (org-src-construct-edit-buffer-name
  358. (buffer-name) "Fixed Width")))
  359. (setq ovl (org-make-overlay beg end))
  360. (org-overlay-put ovl 'face 'secondary-selection)
  361. (org-overlay-put ovl 'edit-buffer buffer)
  362. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  363. (org-overlay-put ovl 'face 'secondary-selection)
  364. (org-overlay-put ovl
  365. 'keymap
  366. (let ((map (make-sparse-keymap)))
  367. (define-key map [mouse-1] 'org-edit-src-continue)
  368. map))
  369. (org-overlay-put ovl :read-only "Leave me alone")
  370. (switch-to-buffer buffer)
  371. (insert code)
  372. (remove-text-properties (point-min) (point-max)
  373. '(display nil invisible nil intangible nil))
  374. (setq block-nindent (or (org-do-remove-indentation) 0))
  375. (cond
  376. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  377. (fundamental-mode)
  378. (artist-mode 1))
  379. (t (funcall org-edit-fixed-width-region-mode)))
  380. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  381. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  382. (set (make-local-variable 'org-edit-src-picture) t)
  383. (goto-char (point-min))
  384. (while (re-search-forward "^[ \t]*: ?" nil t)
  385. (replace-match ""))
  386. (org-goto-line (1+ (- line begline)))
  387. (org-move-to-column (max 0 (- col block-nindent 2)))
  388. (org-set-local 'org-edit-src-beg-marker beg)
  389. (org-set-local 'org-edit-src-end-marker end)
  390. (org-set-local 'org-edit-src-overlay ovl)
  391. (org-set-local 'org-edit-src-block-indentation block-nindent)
  392. (org-set-local 'org-edit-src-content-indentation 0)
  393. (org-set-local 'org-src-preserve-indentation nil)
  394. (org-src-mode)
  395. (set-buffer-modified-p nil)
  396. (and org-edit-src-persistent-message
  397. (org-set-local 'header-line-format msg)))
  398. (message "%s" msg)
  399. t)))
  400. (defun org-edit-src-find-region-and-lang ()
  401. "Find the region and language for a local edit.
  402. Return a list with beginning and end of the region, a string representing
  403. the language, a switch telling if the content should be in a single line."
  404. (let ((re-list
  405. (append
  406. org-edit-src-region-extra
  407. '(
  408. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  409. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  410. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  411. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  412. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  413. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  414. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  415. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  416. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  417. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  418. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  419. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  420. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  421. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  422. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  423. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  424. ("^[ \t]*#\\+macro:[ \t]+\\S-+\\( \\|$\\)"
  425. "\n" "fundamental" macro-definition)
  426. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  427. )))
  428. (pos (point))
  429. re1 re2 single beg end lang lfmt match-re1 ind entry)
  430. (catch 'exit
  431. (when (org-at-table.el-p)
  432. (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
  433. (setq beg (1+ (point-at-eol)))
  434. (goto-char beg)
  435. (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
  436. (progn (goto-char (point-max)) (newline)))
  437. (setq end (point-at-bol))
  438. (setq ind (org-edit-src-get-indentation beg))
  439. (throw 'exit (list beg end 'table.el nil nil ind)))
  440. (while (setq entry (pop re-list))
  441. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  442. single (nth 3 entry))
  443. (save-excursion
  444. (if (or (looking-at re1)
  445. (re-search-backward re1 nil t))
  446. (progn
  447. (setq match-re1 (match-string 0))
  448. (setq beg (match-end 0)
  449. lang (org-edit-src-get-lang lang)
  450. lfmt (org-edit-src-get-label-format match-re1)
  451. ind (org-edit-src-get-indentation (match-beginning 0)))
  452. (if (and (re-search-forward re2 nil t)
  453. (>= (match-end 0) pos))
  454. (throw 'exit (list beg (match-beginning 0)
  455. lang single lfmt ind))))
  456. (if (or (looking-at re2)
  457. (re-search-forward re2 nil t))
  458. (progn
  459. (setq end (match-beginning 0))
  460. (if (and (re-search-backward re1 nil t)
  461. (<= (match-beginning 0) pos))
  462. (progn
  463. (setq lfmt (org-edit-src-get-label-format
  464. (match-string 0))
  465. ind (org-edit-src-get-indentation
  466. (match-beginning 0)))
  467. (throw 'exit
  468. (list (match-end 0) end
  469. (org-edit-src-get-lang lang)
  470. single lfmt ind))))))))))))
  471. (defun org-edit-src-get-lang (lang)
  472. "Extract the src language."
  473. (let ((m (match-string 0)))
  474. (cond
  475. ((stringp lang) lang)
  476. ((integerp lang) (match-string lang))
  477. ((and (eq lang 'lang)
  478. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  479. (match-string 1 m))
  480. ((and (eq lang 'style)
  481. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  482. (match-string 1 m))
  483. (t "fundamental"))))
  484. (defun org-edit-src-get-label-format (s)
  485. "Extract the label format."
  486. (save-match-data
  487. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  488. (match-string 1 s))))
  489. (defun org-edit-src-get-indentation (pos)
  490. "Count leading whitespace characters on line"
  491. (save-match-data
  492. (goto-char pos)
  493. (org-get-indentation)))
  494. (defun org-edit-src-exit (&optional context)
  495. "Exit special edit and protect problematic lines."
  496. (interactive)
  497. (unless org-edit-src-from-org-mode
  498. (error "This is not a sub-editing buffer, something is wrong..."))
  499. (widen)
  500. (let* ((beg org-edit-src-beg-marker)
  501. (end org-edit-src-end-marker)
  502. (ovl org-edit-src-overlay)
  503. (buffer (current-buffer))
  504. (single (org-bound-and-true-p org-edit-src-force-single-line))
  505. (macro (eq single 'macro-definition))
  506. (total-nindent (+ (or org-edit-src-block-indentation 0)
  507. org-edit-src-content-indentation))
  508. (preserve-indentation org-src-preserve-indentation)
  509. (delta 0) code line col indent)
  510. (unless preserve-indentation (untabify (point-min) (point-max)))
  511. (save-excursion
  512. (goto-char (point-min))
  513. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  514. (unless macro
  515. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))))
  516. (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
  517. 1
  518. (org-current-line))
  519. col (current-column))
  520. (when single
  521. (goto-char (point-min))
  522. (if (re-search-forward "\\s-+\\'" nil t) (replace-match ""))
  523. (goto-char (point-min))
  524. (let ((cnt 0))
  525. (while (re-search-forward "\n" nil t)
  526. (setq cnt (1+ cnt))
  527. (replace-match (if macro "\\n" " ") t t))
  528. (when (and macro (> cnt 0))
  529. (goto-char (point-max)) (insert "\\n")))
  530. (goto-char (point-min))
  531. (if (looking-at "\\s-*") (replace-match " ")))
  532. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  533. (goto-char (point-min))
  534. (while (re-search-forward
  535. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  536. (if (eq (org-current-line) line) (setq delta (1+ delta)))
  537. (replace-match ",\\1")))
  538. (when (org-bound-and-true-p org-edit-src-picture)
  539. (setq preserve-indentation nil)
  540. (untabify (point-min) (point-max))
  541. (goto-char (point-min))
  542. (while (re-search-forward "^" nil t)
  543. (replace-match ": ")))
  544. (unless (or single preserve-indentation (= total-nindent 0))
  545. (setq indent (make-string total-nindent ?\ ))
  546. (goto-char (point-min))
  547. (while (re-search-forward "^" nil t)
  548. (replace-match indent)))
  549. (if (org-bound-and-true-p org-edit-src-picture)
  550. (setq total-nindent (+ total-nindent 2)))
  551. (setq code (buffer-string))
  552. (set-buffer-modified-p nil)
  553. (org-src-switch-to-buffer (marker-buffer beg) (or context 'exit))
  554. (kill-buffer buffer)
  555. (goto-char beg)
  556. (delete-region beg end)
  557. (insert code)
  558. (goto-char beg)
  559. (if single (just-one-space))
  560. (if (memq t (mapcar (lambda (overlay)
  561. (eq (org-overlay-get overlay 'invisible)
  562. 'org-hide-block))
  563. (org-overlays-at (point))))
  564. ;; Block is hidden; put point at start of block
  565. (beginning-of-line 0)
  566. ;; Block is visible, put point where it was in the code buffer
  567. (org-goto-line (1- (+ (org-current-line) line)))
  568. (org-move-to-column (if preserve-indentation col (+ col total-nindent delta))))
  569. (move-marker beg nil)
  570. (move-marker end nil))
  571. (unless (eq context 'save)
  572. (when org-edit-src-saved-temp-window-config
  573. (set-window-configuration org-edit-src-saved-temp-window-config)
  574. (setq org-edit-src-saved-temp-window-config nil))))
  575. (defun org-edit-src-save ()
  576. "Save parent buffer with current state source-code buffer."
  577. (interactive)
  578. (let ((p (point)) (m (mark)) msg)
  579. (save-window-excursion
  580. (org-edit-src-exit 'save)
  581. (save-buffer)
  582. (setq msg (current-message))
  583. (if (eq org-src-window-setup 'other-frame)
  584. (let ((org-src-window-setup 'current-window))
  585. (org-edit-src-code 'save))
  586. (org-edit-src-code 'save)))
  587. (push-mark m 'nomessage)
  588. (goto-char (min p (point-max)))
  589. (message (or msg ""))))
  590. (defun org-src-mode-configure-edit-buffer ()
  591. (when org-edit-src-from-org-mode
  592. (setq buffer-offer-save t)
  593. (setq buffer-file-name
  594. (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
  595. "[" (buffer-name) "]"))
  596. (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
  597. '(org-edit-src-save))
  598. (org-add-hook 'kill-buffer-hook
  599. '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
  600. (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
  601. (provide 'org-src)
  602. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  603. ;;; org-src.el ends here