org-src.el 23 KB

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