org-src.el 24 KB

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