org-src.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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.27trans
  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. (defcustom org-edit-src-region-extra nil
  32. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  33. For examples see the function `org-edit-src-find-region-and-lang'.
  34. The regular expression identifying the begin marker should end with a newline,
  35. and the regexp marking the end line should start with a newline, to make sure
  36. there are kept outside the narrowed region."
  37. :group 'org-edit-structure
  38. :type '(repeat
  39. (list
  40. (regexp :tag "begin regexp")
  41. (regexp :tag "end regexp")
  42. (choice :tag "language"
  43. (string :tag "specify")
  44. (integer :tag "from match group")
  45. (const :tag "from `lang' element")
  46. (const :tag "from `style' element")))))
  47. (defcustom org-coderef-label-format "(ref:%s)"
  48. "The default coderef format.
  49. This format string will be used to search for coderef labels in literal
  50. examples (EXAMPLE and SRC blocks). The format can be overwritten
  51. an individual literal example with the -f option, like
  52. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  53. ...
  54. #+END_SRC
  55. If you want to use this for HTML export, make sure that the format does
  56. not introduce special font-locking, and avoid the HTML special
  57. characters `<', `>', and `&'. The reason for this restriction is that
  58. the labels are searched for only after htmlize has done its job."
  59. :group 'org-edit-structure ; FIXME this is not in the right group
  60. :type 'string)
  61. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  62. "The mode that should be used to edit fixed-width regions.
  63. These are the regions where each line starts with a colon."
  64. :group 'org-edit-structure
  65. :type '(choice
  66. (const artist-mode)
  67. (const picture-mode)
  68. (const fundamental-mode)
  69. (function :tag "Other (specify)")))
  70. (defcustom org-edit-src-persistent-message t
  71. "Non-nil means show persistent exit help message while editing src examples.
  72. The message is shown in the header-line, which will be created in the
  73. first line of the window showing the editing buffer.
  74. When nil, the message will only be shown intermittently in the echo area."
  75. :group 'org-edit-structure
  76. :type 'boolean)
  77. (defvar org-protecting-blocks
  78. '("src" "example" "latex" "ascii" "html" "docbook")
  79. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  80. This is needed for font-lock setup.")
  81. ;;; Editing source examples
  82. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  83. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  84. (define-key org-exit-edit-mode-map "\C-x\C-s" 'org-edit-src-save)
  85. (defvar org-edit-src-force-single-line nil)
  86. (defvar org-edit-src-from-org-mode nil)
  87. (defvar org-edit-src-picture nil)
  88. (defvar org-edit-src-beg-marker nil)
  89. (defvar org-edit-src-end-marker nil)
  90. (defvar org-edit-src-overlay nil)
  91. (defvar org-edit-src-nindent nil)
  92. (define-minor-mode org-exit-edit-mode
  93. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  94. (defun org-edit-src-code ()
  95. "Edit the source code example at point.
  96. The example is copied to a separate buffer, and that buffer is switched
  97. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  98. This will remove the original code in the Org buffer, and replace it with
  99. the edited version."
  100. (interactive)
  101. (let ((line (org-current-line))
  102. (case-fold-search t)
  103. (msg (substitute-command-keys
  104. "Edit, then exit with C-c ' (C-c and single quote)"))
  105. (info (org-edit-src-find-region-and-lang))
  106. (org-mode-p (eq major-mode 'org-mode))
  107. (beg (make-marker))
  108. (end (make-marker))
  109. nindent ovl lang lang-f single lfmt code begline buffer)
  110. (if (not info)
  111. nil
  112. (setq beg (move-marker beg (nth 0 info))
  113. end (move-marker end (nth 1 info))
  114. code (buffer-substring-no-properties beg end)
  115. lang (nth 2 info)
  116. single (nth 3 info)
  117. lfmt (nth 4 info)
  118. lang-f (intern (concat lang "-mode"))
  119. begline (save-excursion (goto-char beg) (org-current-line)))
  120. (unless (functionp lang-f)
  121. (error "No such language mode: %s" lang-f))
  122. (goto-line line)
  123. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  124. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  125. (switch-to-buffer buffer)
  126. (when buffer
  127. (with-current-buffer buffer
  128. (if (boundp 'org-edit-src-overlay)
  129. (org-delete-overlay org-edit-src-overlay)))
  130. (kill-buffer buffer))
  131. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  132. (setq ovl (org-make-overlay beg end))
  133. (org-overlay-put ovl 'face 'secondary-selection)
  134. (org-overlay-put ovl 'edit-buffer buffer)
  135. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  136. (org-overlay-put ovl 'face 'secondary-selection)
  137. (org-overlay-put ovl
  138. 'keymap
  139. (let ((map (make-sparse-keymap)))
  140. (define-key map [mouse-1] 'org-edit-src-continue)
  141. map))
  142. (org-overlay-put ovl :read-only "Leave me alone")
  143. (switch-to-buffer buffer)
  144. (insert code)
  145. (remove-text-properties (point-min) (point-max)
  146. '(display nil invisible nil intangible nil))
  147. (setq nindent (org-do-remove-indentation))
  148. (let ((org-inhibit-startup t))
  149. (funcall lang-f))
  150. (set (make-local-variable 'org-edit-src-force-single-line) single)
  151. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  152. (when lfmt
  153. (set (make-local-variable 'org-coderef-label-format) lfmt))
  154. (when org-mode-p
  155. (goto-char (point-min))
  156. (while (re-search-forward "^," nil t)
  157. (replace-match "")))
  158. (goto-line (1+ (- line begline)))
  159. (org-exit-edit-mode)
  160. (org-set-local 'org-edit-src-beg-marker beg)
  161. (org-set-local 'org-edit-src-end-marker end)
  162. (org-set-local 'org-edit-src-overlay ovl)
  163. (org-set-local 'org-edit-src-nindent nindent)
  164. (and org-edit-src-persistent-message
  165. (org-set-local 'header-line-format msg)))
  166. (message "%s" msg)
  167. t)))
  168. (defun org-edit-src-continue (e)
  169. (interactive "e")
  170. (mouse-set-point e)
  171. (let ((buf (get-char-property (point) 'edit-buffer)))
  172. (if buf (switch-to-buffer buf)
  173. (error "Something is wrong here"))))
  174. (defun org-edit-src-find-buffer (beg end)
  175. "Find a source editing buffer that is already editing the region BEG to END."
  176. (catch 'exit
  177. (mapc
  178. (lambda (b)
  179. (with-current-buffer b
  180. (if (and (string-match "\\`*Org Edit " (buffer-name))
  181. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  182. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  183. (equal beg org-edit-src-beg-marker)
  184. (equal end org-edit-src-end-marker))
  185. (throw 'exit (current-buffer)))))
  186. (buffer-list))
  187. nil))
  188. (defun org-edit-fixed-width-region ()
  189. "Edit the fixed-width ascii drawing at point.
  190. This must be a region where each line starts with a colon followed by
  191. a space character.
  192. An new buffer is created and the fixed-width region is copied into it,
  193. and the buffer is switched into `artist-mode' for editing. When done,
  194. exit with \\[org-edit-src-exit]. The edited text will then replace
  195. the fragment in the Org-mode buffer."
  196. (interactive)
  197. (let ((line (org-current-line))
  198. (case-fold-search t)
  199. (msg (substitute-command-keys
  200. "Edit, then exit with C-c ' (C-c and single quote)"))
  201. (org-mode-p (eq major-mode 'org-mode))
  202. (beg (make-marker))
  203. (end (make-marker))
  204. nindent ovl beg1 end1 code begline buffer)
  205. (beginning-of-line 1)
  206. (if (looking-at "[ \t]*[^:\n \t]")
  207. nil
  208. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  209. (setq beg1 (point) end1 beg1)
  210. (save-excursion
  211. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  212. (setq beg1 (point-at-bol 2))
  213. (setq beg1 (point))))
  214. (save-excursion
  215. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  216. (setq end1 (1- (match-beginning 0)))
  217. (setq end1 (point))))
  218. (goto-line line))
  219. (setq beg (move-marker beg beg1)
  220. end (move-marker end end1)
  221. code (buffer-substring-no-properties beg end)
  222. begline (save-excursion (goto-char beg) (org-current-line)))
  223. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  224. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  225. (switch-to-buffer buffer)
  226. (when buffer
  227. (with-current-buffer buffer
  228. (if (boundp 'org-edit-src-overlay)
  229. (org-delete-overlay org-edit-src-overlay)))
  230. (kill-buffer buffer))
  231. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  232. (setq ovl (org-make-overlay beg end))
  233. (org-overlay-put ovl 'face 'secondary-selection)
  234. (org-overlay-put ovl 'edit-buffer buffer)
  235. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  236. (org-overlay-put ovl 'face 'secondary-selection)
  237. (org-overlay-put ovl
  238. 'keymap
  239. (let ((map (make-sparse-keymap)))
  240. (define-key map [mouse-1] 'org-edit-src-continue)
  241. map))
  242. (org-overlay-put ovl :read-only "Leave me alone")
  243. (switch-to-buffer buffer)
  244. (insert code)
  245. (remove-text-properties (point-min) (point-max)
  246. '(display nil invisible nil intangible nil))
  247. (setq nindent (org-do-remove-indentation))
  248. (cond
  249. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  250. (fundamental-mode)
  251. (artist-mode 1))
  252. (t (funcall org-edit-fixed-width-region-mode)))
  253. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  254. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  255. (set (make-local-variable 'org-edit-src-picture) t)
  256. (goto-char (point-min))
  257. (while (re-search-forward "^[ \t]*: ?" nil t)
  258. (replace-match ""))
  259. (goto-line (1+ (- line begline)))
  260. (org-exit-edit-mode)
  261. (org-set-local 'org-edit-src-beg-marker beg)
  262. (org-set-local 'org-edit-src-end-marker end)
  263. (org-set-local 'org-edit-src-overlay ovl)
  264. (org-set-local 'org-edit-src-nindent nindent)
  265. (and org-edit-src-persistent-message
  266. (org-set-local 'header-line-format msg)))
  267. (message "%s" msg)
  268. t)))
  269. (defun org-edit-src-find-region-and-lang ()
  270. "Find the region and language for a local edit.
  271. Return a list with beginning and end of the region, a string representing
  272. the language, a switch telling of the content should be in a single line."
  273. (let ((re-list
  274. (append
  275. org-edit-src-region-extra
  276. '(
  277. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  278. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  279. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  280. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  281. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  282. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  283. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  284. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  285. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  286. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  287. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  288. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  289. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  290. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  291. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  292. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  293. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  294. )))
  295. (pos (point))
  296. re1 re2 single beg end lang lfmt match-re1 ind)
  297. (catch 'exit
  298. (while (setq entry (pop re-list))
  299. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  300. single (nth 3 entry))
  301. (save-excursion
  302. (if (or (looking-at re1)
  303. (re-search-backward re1 nil t))
  304. (progn
  305. (setq match-re1 (match-string 0))
  306. (setq beg (match-end 0)
  307. lang (org-edit-src-get-lang lang)
  308. lfmt (org-edit-src-get-label-format match-re1))
  309. (if (and (re-search-forward re2 nil t)
  310. (>= (match-end 0) pos))
  311. (throw 'exit (list beg (match-beginning 0)
  312. lang single lfmt))))
  313. (if (or (looking-at re2)
  314. (re-search-forward re2 nil t))
  315. (progn
  316. (setq end (match-beginning 0))
  317. (if (and (re-search-backward re1 nil t)
  318. (<= (match-beginning 0) pos))
  319. (progn
  320. (setq lfmt (org-edit-src-get-label-format
  321. (match-string 0)))
  322. (throw 'exit
  323. (list (match-end 0) end
  324. (org-edit-src-get-lang lang)
  325. single lfmt))))))))))))
  326. (defun org-edit-src-get-lang (lang)
  327. "Extract the src language."
  328. (let ((m (match-string 0)))
  329. (cond
  330. ((stringp lang) lang)
  331. ((integerp lang) (match-string lang))
  332. ((and (eq lang 'lang)
  333. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  334. (match-string 1 m))
  335. ((and (eq lang 'style)
  336. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  337. (match-string 1 m))
  338. (t "fundamental"))))
  339. (defun org-edit-src-get-label-format (s)
  340. "Extract the label format."
  341. (save-match-data
  342. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  343. (match-string 1 s))))
  344. (defun org-edit-src-exit ()
  345. "Exit special edit and protect problematic lines."
  346. (interactive)
  347. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  348. (error "This is not an sub-editing buffer, something is wrong..."))
  349. (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
  350. 1
  351. (org-current-line)))
  352. (beg org-edit-src-beg-marker)
  353. (end org-edit-src-end-marker)
  354. (ovl org-edit-src-overlay)
  355. (buffer (current-buffer))
  356. (nindent org-edit-src-nindent)
  357. code)
  358. (goto-char (point-min))
  359. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  360. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  361. (when (org-bound-and-true-p org-edit-src-force-single-line)
  362. (goto-char (point-min))
  363. (while (re-search-forward "\n" nil t)
  364. (replace-match " "))
  365. (goto-char (point-min))
  366. (if (looking-at "\\s-*") (replace-match " "))
  367. (if (re-search-forward "\\s-+\\'" nil t)
  368. (replace-match "")))
  369. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  370. (goto-char (point-min))
  371. (while (re-search-forward
  372. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  373. (replace-match ",\\1")))
  374. (when (org-bound-and-true-p org-edit-src-picture)
  375. (untabify (point-min) (point-max))
  376. (goto-char (point-min))
  377. (while (re-search-forward "^" nil t)
  378. (replace-match ": ")))
  379. (when nindent
  380. (setq nindent (make-string nindent ?\ ))
  381. (goto-char (point-min))
  382. (while (re-search-forward "^" nil t)
  383. (replace-match nindent)))
  384. (setq code (buffer-string))
  385. (switch-to-buffer (marker-buffer beg))
  386. (kill-buffer buffer)
  387. (goto-char beg)
  388. (org-delete-overlay ovl)
  389. (delete-region beg end)
  390. (insert code)
  391. (goto-char beg)
  392. (goto-line (1- (+ (org-current-line) line)))
  393. (move-marker beg nil)
  394. (move-marker end nil)))
  395. (defun org-edit-src-save ()
  396. "Save parent buffer with current state source-code buffer."
  397. (interactive)
  398. (let ((p (point)) (m (mark)) msg)
  399. (org-edit-src-exit)
  400. (save-buffer)
  401. (setq msg (current-message))
  402. (org-edit-src-code)
  403. (push-mark m 'nomessage)
  404. (goto-char (min p (point-max)))
  405. (message (or msg ""))))
  406. (provide 'org-src)
  407. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  408. ;;; org-src.el ends here