org-src.el 16 KB

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