org-src.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. ;; Keywords: outlines, hypermedia, calendar, wp
  9. ;; Homepage: http://orgmode.org
  10. ;; Version: 6.30trans
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;; This file contains the code dealing with source code examples in Org-mode.
  28. ;;; Code:
  29. (require 'org-macs)
  30. (require 'org-compat)
  31. (declare-function org-do-remove-indentation "org" (&optional n))
  32. (declare-function org-get-indentation "org" (&optional line))
  33. (defcustom org-edit-src-region-extra nil
  34. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  35. For examples see the function `org-edit-src-find-region-and-lang'.
  36. The regular expression identifying the begin marker should end with a newline,
  37. and the regexp marking the end line should start with a newline, to make sure
  38. there are kept outside the narrowed region."
  39. :group 'org-edit-structure
  40. :type '(repeat
  41. (list
  42. (regexp :tag "begin regexp")
  43. (regexp :tag "end regexp")
  44. (choice :tag "language"
  45. (string :tag "specify")
  46. (integer :tag "from match group")
  47. (const :tag "from `lang' element")
  48. (const :tag "from `style' element")))))
  49. (defcustom org-coderef-label-format "(ref:%s)"
  50. "The default coderef format.
  51. This format string will be used to search for coderef labels in literal
  52. examples (EXAMPLE and SRC blocks). The format can be overwritten in
  53. an individual literal example with the -f option, like
  54. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  55. ...
  56. #+END_SRC
  57. If you want to use this for HTML export, make sure that the format does
  58. not introduce special font-locking, and avoid the HTML special
  59. characters `<', `>', and `&'. The reason for this restriction is that
  60. the labels are searched for only after htmlize has done its job."
  61. :group 'org-edit-structure ; FIXME this is not in the right group
  62. :type 'string)
  63. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  64. "The mode that should be used to edit fixed-width regions.
  65. These are the regions where each line starts with a colon."
  66. :group 'org-edit-structure
  67. :type '(choice
  68. (const artist-mode)
  69. (const picture-mode)
  70. (const fundamental-mode)
  71. (function :tag "Other (specify)")))
  72. (defcustom org-edit-src-content-indentation 2
  73. "Indentation for the content is a source code block.
  74. This should be the number of spaces added to the indentation of the #+begin
  75. line in order to compute the indentation of the block content after
  76. editing it with \\[org-edit-src-code]."
  77. :group 'org-edit-structure
  78. :type 'integer)
  79. (defcustom org-edit-src-persistent-message t
  80. "Non-nil means show persistent exit help message while editing src examples.
  81. The message is shown in the header-line, which will be created in the
  82. first line of the window showing the editing buffer.
  83. When nil, the message will only be shown intermittently in the echo area."
  84. :group 'org-edit-structure
  85. :type 'boolean)
  86. (defvar org-src-mode-hook nil
  87. "Hook run after Org switched a source code snippet to its Emacs mode.
  88. This hook will run
  89. - when editing a source code snippet with \"C-c '\".
  90. - When formatting a source code snippet for export with htmlize.
  91. You may want to use this hook for example to turn off `outline-minor-mode'
  92. or similar things which you want to have when editing a source code file,
  93. but which mess up the display of a snippet in Org exported files.")
  94. (defcustom org-src-lang-modes
  95. '(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist))
  96. "Alist mapping languages to their major mode.
  97. The key is the language name, the value is the string that should
  98. be inserted as the name of the major mode. For many languages this is
  99. simple, but for language where this is not the case, this variable
  100. provides a way to simplify things on the user side.
  101. For example, there is no ocaml-mode in Emacs, but the mode to use is
  102. `tuareg-mode'."
  103. :group 'org-edit-structure
  104. :type '(repeat
  105. (cons
  106. (string "Language name")
  107. (symbol "Major mode"))))
  108. ;;; Editing source examples
  109. (defvar org-src-mode-map (make-sparse-keymap))
  110. (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
  111. (defvar org-edit-src-force-single-line nil)
  112. (defvar org-edit-src-from-org-mode nil)
  113. (defvar org-edit-src-picture nil)
  114. (defvar org-edit-src-beg-marker nil)
  115. (defvar org-edit-src-end-marker nil)
  116. (defvar org-edit-src-overlay nil)
  117. (defvar org-edit-src-nindent nil)
  118. (define-minor-mode org-src-mode
  119. "Minor mode for language major mode buffers generated by org.
  120. This minor mode is turned on in two situations:
  121. - when editing a source code snippet with \"C-c '\".
  122. - When formatting a source code snippet for export with htmlize.
  123. There is a mode hook, and keybindings for `org-edit-src-exit' and
  124. `org-edit-src-save'")
  125. (defun org-edit-src-code ()
  126. "Edit the source code example at point.
  127. The example is copied to a separate buffer, and that buffer is switched
  128. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  129. This will remove the original code in the Org buffer, and replace it with
  130. the edited version."
  131. (interactive)
  132. (let ((line (org-current-line))
  133. (case-fold-search t)
  134. (msg (substitute-command-keys
  135. "Edit, then exit with C-c ' (C-c and single quote)"))
  136. (info (org-edit-src-find-region-and-lang))
  137. (org-mode-p (eq major-mode 'org-mode))
  138. (beg (make-marker))
  139. (end (make-marker))
  140. nindent ovl lang lang-f single lfmt code begline buffer)
  141. (if (not info)
  142. nil
  143. (setq beg (move-marker beg (nth 0 info))
  144. end (move-marker end (nth 1 info))
  145. code (buffer-substring-no-properties beg end)
  146. lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
  147. (nth 2 info))
  148. lang (if (symbolp lang) (symbol-name lang) lang)
  149. single (nth 3 info)
  150. lfmt (nth 4 info)
  151. nindent (nth 5 info)
  152. lang-f (intern (concat lang "-mode"))
  153. begline (save-excursion (goto-char beg) (org-current-line)))
  154. (unless (functionp lang-f)
  155. (error "No such language mode: %s" lang-f))
  156. (org-goto-line line)
  157. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  158. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  159. (switch-to-buffer buffer)
  160. (when buffer
  161. (with-current-buffer buffer
  162. (if (boundp 'org-edit-src-overlay)
  163. (org-delete-overlay org-edit-src-overlay)))
  164. (kill-buffer buffer))
  165. (setq buffer (generate-new-buffer
  166. (concat "*Org Src " (file-name-nondirectory buffer-file-name) "[" lang "]*")))
  167. (setq ovl (org-make-overlay beg end))
  168. (org-overlay-put ovl 'face 'secondary-selection)
  169. (org-overlay-put ovl 'edit-buffer buffer)
  170. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  171. (org-overlay-put ovl 'face 'secondary-selection)
  172. (org-overlay-put ovl
  173. 'keymap
  174. (let ((map (make-sparse-keymap)))
  175. (define-key map [mouse-1] 'org-edit-src-continue)
  176. map))
  177. (org-overlay-put ovl :read-only "Leave me alone")
  178. (switch-to-buffer buffer)
  179. (insert code)
  180. (remove-text-properties (point-min) (point-max)
  181. '(display nil invisible nil intangible nil))
  182. (org-do-remove-indentation)
  183. (let ((org-inhibit-startup t))
  184. (funcall lang-f))
  185. (set (make-local-variable 'org-edit-src-force-single-line) single)
  186. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  187. (when lfmt
  188. (set (make-local-variable 'org-coderef-label-format) lfmt))
  189. (when org-mode-p
  190. (goto-char (point-min))
  191. (while (re-search-forward "^," nil t)
  192. (replace-match "")))
  193. (org-goto-line (1+ (- line begline)))
  194. (org-set-local 'org-edit-src-beg-marker beg)
  195. (org-set-local 'org-edit-src-end-marker end)
  196. (org-set-local 'org-edit-src-overlay ovl)
  197. (org-set-local 'org-edit-src-nindent nindent)
  198. (org-src-mode)
  199. (set-buffer-modified-p nil)
  200. (and org-edit-src-persistent-message
  201. (org-set-local 'header-line-format msg)))
  202. (message "%s" msg)
  203. t)))
  204. (defun org-edit-src-continue (e)
  205. (interactive "e")
  206. (mouse-set-point e)
  207. (let ((buf (get-char-property (point) 'edit-buffer)))
  208. (if buf (switch-to-buffer buf)
  209. (error "Something is wrong here"))))
  210. (defun org-edit-src-find-buffer (beg end)
  211. "Find a source editing buffer that is already editing the region BEG to END."
  212. (catch 'exit
  213. (mapc
  214. (lambda (b)
  215. (with-current-buffer b
  216. (if (and (string-match "\\`*Org Edit " (buffer-name))
  217. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  218. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  219. (equal beg org-edit-src-beg-marker)
  220. (equal end org-edit-src-end-marker))
  221. (throw 'exit (current-buffer)))))
  222. (buffer-list))
  223. nil))
  224. (defun org-edit-fixed-width-region ()
  225. "Edit the fixed-width ascii drawing at point.
  226. This must be a region where each line starts with a colon followed by
  227. a space character.
  228. An new buffer is created and the fixed-width region is copied into it,
  229. and the buffer is switched into `artist-mode' for editing. When done,
  230. exit with \\[org-edit-src-exit]. The edited text will then replace
  231. the fragment in the Org-mode buffer."
  232. (interactive)
  233. (let ((line (org-current-line))
  234. (case-fold-search t)
  235. (msg (substitute-command-keys
  236. "Edit, then exit with C-c ' (C-c and single quote)"))
  237. (org-mode-p (eq major-mode 'org-mode))
  238. (beg (make-marker))
  239. (end (make-marker))
  240. nindent ovl beg1 end1 code begline buffer)
  241. (beginning-of-line 1)
  242. (if (looking-at "[ \t]*[^:\n \t]")
  243. nil
  244. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  245. (setq beg1 (point) end1 beg1)
  246. (save-excursion
  247. (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
  248. (setq beg1 (point-at-bol 2))
  249. (setq beg1 (point))))
  250. (save-excursion
  251. (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
  252. (setq end1 (1- (match-beginning 0)))
  253. (setq end1 (point))))
  254. (org-goto-line line))
  255. (setq beg (move-marker beg beg1)
  256. end (move-marker end end1)
  257. code (buffer-substring-no-properties beg end)
  258. begline (save-excursion (goto-char beg) (org-current-line)))
  259. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  260. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  261. (switch-to-buffer buffer)
  262. (when buffer
  263. (with-current-buffer buffer
  264. (if (boundp 'org-edit-src-overlay)
  265. (org-delete-overlay org-edit-src-overlay)))
  266. (kill-buffer buffer))
  267. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  268. (setq ovl (org-make-overlay beg end))
  269. (org-overlay-put ovl 'face 'secondary-selection)
  270. (org-overlay-put ovl 'edit-buffer buffer)
  271. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  272. (org-overlay-put ovl 'face 'secondary-selection)
  273. (org-overlay-put ovl
  274. 'keymap
  275. (let ((map (make-sparse-keymap)))
  276. (define-key map [mouse-1] 'org-edit-src-continue)
  277. map))
  278. (org-overlay-put ovl :read-only "Leave me alone")
  279. (switch-to-buffer buffer)
  280. (insert code)
  281. (remove-text-properties (point-min) (point-max)
  282. '(display nil invisible nil intangible nil))
  283. (setq nindent (org-do-remove-indentation))
  284. (cond
  285. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  286. (fundamental-mode)
  287. (artist-mode 1))
  288. (t (funcall org-edit-fixed-width-region-mode)))
  289. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  290. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  291. (set (make-local-variable 'org-edit-src-picture) t)
  292. (goto-char (point-min))
  293. (while (re-search-forward "^[ \t]*: ?" nil t)
  294. (replace-match ""))
  295. (org-goto-line (1+ (- line begline)))
  296. (org-set-local 'org-edit-src-beg-marker beg)
  297. (org-set-local 'org-edit-src-end-marker end)
  298. (org-set-local 'org-edit-src-overlay ovl)
  299. (org-set-local 'org-edit-src-nindent nindent)
  300. (org-src-mode)
  301. (set-buffer-modified-p nil)
  302. (and org-edit-src-persistent-message
  303. (org-set-local 'header-line-format msg)))
  304. (message "%s" msg)
  305. t)))
  306. (defun org-edit-src-find-region-and-lang ()
  307. "Find the region and language for a local edit.
  308. Return a list with beginning and end of the region, a string representing
  309. the language, a switch telling of the content should be in a single line."
  310. (let ((re-list
  311. (append
  312. org-edit-src-region-extra
  313. '(
  314. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  315. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  316. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  317. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  318. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  319. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  320. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  321. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  322. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  323. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  324. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  325. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  326. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  327. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  328. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  329. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  330. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  331. )))
  332. (pos (point))
  333. re1 re2 single beg end lang lfmt match-re1 ind entry)
  334. (catch 'exit
  335. (while (setq entry (pop re-list))
  336. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  337. single (nth 3 entry))
  338. (save-excursion
  339. (if (or (looking-at re1)
  340. (re-search-backward re1 nil t))
  341. (progn
  342. (setq match-re1 (match-string 0))
  343. (setq beg (match-end 0)
  344. lang (org-edit-src-get-lang lang)
  345. lfmt (org-edit-src-get-label-format match-re1)
  346. ind (org-edit-src-get-indentation (match-beginning 0)))
  347. (if (and (re-search-forward re2 nil t)
  348. (>= (match-end 0) pos))
  349. (throw 'exit (list beg (match-beginning 0)
  350. lang single lfmt ind))))
  351. (if (or (looking-at re2)
  352. (re-search-forward re2 nil t))
  353. (progn
  354. (setq end (match-beginning 0))
  355. (if (and (re-search-backward re1 nil t)
  356. (<= (match-beginning 0) pos))
  357. (progn
  358. (setq lfmt (org-edit-src-get-label-format
  359. (match-string 0))
  360. ind (org-edit-src-get-indentation
  361. (match-beginning 0)))
  362. (throw 'exit
  363. (list (match-end 0) end
  364. (org-edit-src-get-lang lang)
  365. single lfmt ind))))))))))))
  366. (defun org-edit-src-get-lang (lang)
  367. "Extract the src language."
  368. (let ((m (match-string 0)))
  369. (cond
  370. ((stringp lang) lang)
  371. ((integerp lang) (match-string lang))
  372. ((and (eq lang 'lang)
  373. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  374. (match-string 1 m))
  375. ((and (eq lang 'style)
  376. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  377. (match-string 1 m))
  378. (t "fundamental"))))
  379. (defun org-edit-src-get-label-format (s)
  380. "Extract the label format."
  381. (save-match-data
  382. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  383. (match-string 1 s))))
  384. (defun org-edit-src-get-indentation (pos)
  385. "Extract the label format."
  386. (save-match-data
  387. (goto-char pos)
  388. (org-get-indentation)))
  389. (defun org-edit-src-exit ()
  390. "Exit special edit and protect problematic lines."
  391. (interactive)
  392. (unless org-edit-src-from-org-mode
  393. (error "This is not a sub-editing buffer, something is wrong..."))
  394. (let ((beg org-edit-src-beg-marker)
  395. (end org-edit-src-end-marker)
  396. (ovl org-edit-src-overlay)
  397. (buffer (current-buffer))
  398. (nindent org-edit-src-nindent)
  399. code line)
  400. (untabify (point-min) (point-max))
  401. (save-excursion
  402. (goto-char (point-min))
  403. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  404. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))
  405. (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
  406. 1
  407. (org-current-line)))
  408. (when (org-bound-and-true-p org-edit-src-force-single-line)
  409. (goto-char (point-min))
  410. (while (re-search-forward "\n" nil t)
  411. (replace-match " "))
  412. (goto-char (point-min))
  413. (if (looking-at "\\s-*") (replace-match " "))
  414. (if (re-search-forward "\\s-+\\'" nil t)
  415. (replace-match "")))
  416. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  417. (goto-char (point-min))
  418. (while (re-search-forward
  419. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  420. (replace-match ",\\1")))
  421. (when (org-bound-and-true-p org-edit-src-picture)
  422. (untabify (point-min) (point-max))
  423. (goto-char (point-min))
  424. (while (re-search-forward "^" nil t)
  425. (replace-match ": ")))
  426. (when nindent
  427. (setq nindent (make-string (+ org-edit-src-content-indentation nindent)
  428. ?\ ))
  429. (goto-char (point-min))
  430. (while (re-search-forward "^" nil t)
  431. (replace-match nindent)))
  432. (setq code (buffer-string))
  433. (set-buffer-modified-p nil)
  434. (switch-to-buffer (marker-buffer beg))
  435. (kill-buffer buffer)
  436. (goto-char beg)
  437. (delete-region beg end)
  438. (insert code)
  439. (goto-char beg)
  440. (org-goto-line (1- (+ (org-current-line) line)))
  441. (move-marker beg nil)
  442. (move-marker end nil)))
  443. (defun org-edit-src-save ()
  444. "Save parent buffer with current state source-code buffer."
  445. (interactive)
  446. (let ((p (point)) (m (mark)) msg)
  447. (org-edit-src-exit)
  448. (save-buffer)
  449. (setq msg (current-message))
  450. (org-edit-src-code)
  451. (push-mark m 'nomessage)
  452. (goto-char (min p (point-max)))
  453. (message (or msg ""))))
  454. (defun org-src-mode-configure-edit-buffer ()
  455. (when org-edit-src-from-org-mode
  456. (setq buffer-offer-save t)
  457. (setq buffer-file-name
  458. (concat (buffer-file-name (marker-buffer org-edit-src-beg-marker))
  459. "[" (buffer-name) "]"))
  460. (set (if (featurep 'xemacs) 'write-contents-hooks 'write-contents-functions)
  461. '(org-edit-src-save))
  462. (org-add-hook 'kill-buffer-hook
  463. '(lambda () (org-delete-overlay org-edit-src-overlay)) nil 'local)))
  464. (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
  465. (provide 'org-src)
  466. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  467. ;;; org-src.el ends here