org-src.el 23 KB

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