org-src.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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.29a
  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. ;;; Editing source examples
  95. (defvar org-src-mode-map (make-sparse-keymap))
  96. (define-key org-src-mode-map "\C-c'" 'org-edit-src-exit)
  97. (define-key org-src-mode-map "\C-x\C-s" 'org-edit-src-save)
  98. (defvar org-edit-src-force-single-line nil)
  99. (defvar org-edit-src-from-org-mode nil)
  100. (defvar org-edit-src-picture nil)
  101. (defvar org-edit-src-beg-marker nil)
  102. (defvar org-edit-src-end-marker nil)
  103. (defvar org-edit-src-overlay nil)
  104. (defvar org-edit-src-nindent nil)
  105. (define-minor-mode org-src-mode
  106. "Minor mode for language major mode buffers generated by org.
  107. This minor mode is turned on in two situations:
  108. - when editing a source code snippet with \"C-c '\".
  109. - When formatting a source code snippet for export with htmlize.
  110. There is a mode hook, and keybindings for `org-edit-src-exit' and
  111. `org-edit-src-save'")
  112. (defun org-edit-src-code ()
  113. "Edit the source code example at point.
  114. The example is copied to a separate buffer, and that buffer is switched
  115. to the correct language mode. When done, exit with \\[org-edit-src-exit].
  116. This will remove the original code in the Org buffer, and replace it with
  117. the edited version."
  118. (interactive)
  119. (let ((line (org-current-line))
  120. (case-fold-search t)
  121. (msg (substitute-command-keys
  122. "Edit, then exit with C-c ' (C-c and single quote)"))
  123. (info (org-edit-src-find-region-and-lang))
  124. (org-mode-p (eq major-mode 'org-mode))
  125. (beg (make-marker))
  126. (end (make-marker))
  127. nindent ovl lang lang-f single lfmt code begline buffer)
  128. (if (not info)
  129. nil
  130. (setq beg (move-marker beg (nth 0 info))
  131. end (move-marker end (nth 1 info))
  132. code (buffer-substring-no-properties beg end)
  133. lang (nth 2 info)
  134. single (nth 3 info)
  135. lfmt (nth 4 info)
  136. nindent (nth 5 info)
  137. lang-f (intern (concat lang "-mode"))
  138. begline (save-excursion (goto-char beg) (org-current-line)))
  139. (unless (functionp lang-f)
  140. (error "No such language mode: %s" lang-f))
  141. (goto-line line)
  142. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  143. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  144. (switch-to-buffer buffer)
  145. (when buffer
  146. (with-current-buffer buffer
  147. (if (boundp 'org-edit-src-overlay)
  148. (org-delete-overlay org-edit-src-overlay)))
  149. (kill-buffer buffer))
  150. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  151. (setq ovl (org-make-overlay beg end))
  152. (org-overlay-put ovl 'face 'secondary-selection)
  153. (org-overlay-put ovl 'edit-buffer buffer)
  154. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  155. (org-overlay-put ovl 'face 'secondary-selection)
  156. (org-overlay-put ovl
  157. 'keymap
  158. (let ((map (make-sparse-keymap)))
  159. (define-key map [mouse-1] 'org-edit-src-continue)
  160. map))
  161. (org-overlay-put ovl :read-only "Leave me alone")
  162. (switch-to-buffer buffer)
  163. (insert code)
  164. (remove-text-properties (point-min) (point-max)
  165. '(display nil invisible nil intangible nil))
  166. (org-do-remove-indentation)
  167. (let ((org-inhibit-startup t))
  168. (funcall lang-f)
  169. (org-src-mode))
  170. (set (make-local-variable 'org-edit-src-force-single-line) single)
  171. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  172. (when lfmt
  173. (set (make-local-variable 'org-coderef-label-format) lfmt))
  174. (when org-mode-p
  175. (goto-char (point-min))
  176. (while (re-search-forward "^," nil t)
  177. (replace-match "")))
  178. (goto-line (1+ (- line begline)))
  179. (org-set-local 'org-edit-src-beg-marker beg)
  180. (org-set-local 'org-edit-src-end-marker end)
  181. (org-set-local 'org-edit-src-overlay ovl)
  182. (org-set-local 'org-edit-src-nindent nindent)
  183. (and org-edit-src-persistent-message
  184. (org-set-local 'header-line-format msg)))
  185. (message "%s" msg)
  186. t)))
  187. (defun org-edit-src-continue (e)
  188. (interactive "e")
  189. (mouse-set-point e)
  190. (let ((buf (get-char-property (point) 'edit-buffer)))
  191. (if buf (switch-to-buffer buf)
  192. (error "Something is wrong here"))))
  193. (defun org-edit-src-find-buffer (beg end)
  194. "Find a source editing buffer that is already editing the region BEG to END."
  195. (catch 'exit
  196. (mapc
  197. (lambda (b)
  198. (with-current-buffer b
  199. (if (and (string-match "\\`*Org Edit " (buffer-name))
  200. (local-variable-p 'org-edit-src-beg-marker (current-buffer))
  201. (local-variable-p 'org-edit-src-end-marker (current-buffer))
  202. (equal beg org-edit-src-beg-marker)
  203. (equal end org-edit-src-end-marker))
  204. (throw 'exit (current-buffer)))))
  205. (buffer-list))
  206. nil))
  207. (defun org-edit-fixed-width-region ()
  208. "Edit the fixed-width ascii drawing at point.
  209. This must be a region where each line starts with a colon followed by
  210. a space character.
  211. An new buffer is created and the fixed-width region is copied into it,
  212. and the buffer is switched into `artist-mode' for editing. When done,
  213. exit with \\[org-edit-src-exit]. The edited text will then replace
  214. the fragment in the Org-mode buffer."
  215. (interactive)
  216. (let ((line (org-current-line))
  217. (case-fold-search t)
  218. (msg (substitute-command-keys
  219. "Edit, then exit with C-c ' (C-c and single quote)"))
  220. (org-mode-p (eq major-mode 'org-mode))
  221. (beg (make-marker))
  222. (end (make-marker))
  223. nindent ovl beg1 end1 code begline buffer)
  224. (beginning-of-line 1)
  225. (if (looking-at "[ \t]*[^:\n \t]")
  226. nil
  227. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  228. (setq beg1 (point) end1 beg1)
  229. (save-excursion
  230. (if (re-search-backward "^[ \t]*[^: \t]" nil 'move)
  231. (setq beg1 (point-at-bol 2))
  232. (setq beg1 (point))))
  233. (save-excursion
  234. (if (re-search-forward "^[ \t]*[^: \t]" nil 'move)
  235. (setq end1 (1- (match-beginning 0)))
  236. (setq end1 (point))))
  237. (goto-line line))
  238. (setq beg (move-marker beg beg1)
  239. end (move-marker end end1)
  240. code (buffer-substring-no-properties beg end)
  241. begline (save-excursion (goto-char beg) (org-current-line)))
  242. (if (and (setq buffer (org-edit-src-find-buffer beg end))
  243. (y-or-n-p "Return to existing edit buffer? [n] will revert changes: "))
  244. (switch-to-buffer buffer)
  245. (when buffer
  246. (with-current-buffer buffer
  247. (if (boundp 'org-edit-src-overlay)
  248. (org-delete-overlay org-edit-src-overlay)))
  249. (kill-buffer buffer))
  250. (setq buffer (generate-new-buffer "*Org Edit Src Example*"))
  251. (setq ovl (org-make-overlay beg end))
  252. (org-overlay-put ovl 'face 'secondary-selection)
  253. (org-overlay-put ovl 'edit-buffer buffer)
  254. (org-overlay-put ovl 'help-echo "Click with mouse-1 to switch to buffer editing this segment")
  255. (org-overlay-put ovl 'face 'secondary-selection)
  256. (org-overlay-put ovl
  257. 'keymap
  258. (let ((map (make-sparse-keymap)))
  259. (define-key map [mouse-1] 'org-edit-src-continue)
  260. map))
  261. (org-overlay-put ovl :read-only "Leave me alone")
  262. (switch-to-buffer buffer)
  263. (insert code)
  264. (remove-text-properties (point-min) (point-max)
  265. '(display nil invisible nil intangible nil))
  266. (setq nindent (org-do-remove-indentation))
  267. (cond
  268. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  269. (fundamental-mode)
  270. (artist-mode 1))
  271. (t (funcall org-edit-fixed-width-region-mode)))
  272. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  273. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  274. (set (make-local-variable 'org-edit-src-picture) t)
  275. (goto-char (point-min))
  276. (while (re-search-forward "^[ \t]*: ?" nil t)
  277. (replace-match ""))
  278. (goto-line (1+ (- line begline)))
  279. (org-src-mode)
  280. (org-set-local 'org-edit-src-beg-marker beg)
  281. (org-set-local 'org-edit-src-end-marker end)
  282. (org-set-local 'org-edit-src-overlay ovl)
  283. (org-set-local 'org-edit-src-nindent nindent)
  284. (and org-edit-src-persistent-message
  285. (org-set-local 'header-line-format msg)))
  286. (message "%s" msg)
  287. t)))
  288. (defun org-edit-src-find-region-and-lang ()
  289. "Find the region and language for a local edit.
  290. Return a list with beginning and end of the region, a string representing
  291. the language, a switch telling of the content should be in a single line."
  292. (let ((re-list
  293. (append
  294. org-edit-src-region-extra
  295. '(
  296. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  297. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  298. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  299. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  300. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  301. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  302. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  303. ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
  304. ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
  305. ("^[ \t]*#\\+html:" "\n" "html" single-line)
  306. ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
  307. ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
  308. ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
  309. ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
  310. ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
  311. ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
  312. ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
  313. )))
  314. (pos (point))
  315. re1 re2 single beg end lang lfmt match-re1 ind entry)
  316. (catch 'exit
  317. (while (setq entry (pop re-list))
  318. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  319. single (nth 3 entry))
  320. (save-excursion
  321. (if (or (looking-at re1)
  322. (re-search-backward re1 nil t))
  323. (progn
  324. (setq match-re1 (match-string 0))
  325. (setq beg (match-end 0)
  326. lang (org-edit-src-get-lang lang)
  327. lfmt (org-edit-src-get-label-format match-re1)
  328. ind (org-edit-src-get-indentation (match-beginning 0)))
  329. (if (and (re-search-forward re2 nil t)
  330. (>= (match-end 0) pos))
  331. (throw 'exit (list beg (match-beginning 0)
  332. lang single lfmt ind))))
  333. (if (or (looking-at re2)
  334. (re-search-forward re2 nil t))
  335. (progn
  336. (setq end (match-beginning 0))
  337. (if (and (re-search-backward re1 nil t)
  338. (<= (match-beginning 0) pos))
  339. (progn
  340. (setq lfmt (org-edit-src-get-label-format
  341. (match-string 0))
  342. ind (org-edit-src-get-indentation
  343. (match-beginning 0)))
  344. (throw 'exit
  345. (list (match-end 0) end
  346. (org-edit-src-get-lang lang)
  347. single lfmt ind))))))))))))
  348. (defun org-edit-src-get-lang (lang)
  349. "Extract the src language."
  350. (let ((m (match-string 0)))
  351. (cond
  352. ((stringp lang) lang)
  353. ((integerp lang) (match-string lang))
  354. ((and (eq lang 'lang)
  355. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  356. (match-string 1 m))
  357. ((and (eq lang 'style)
  358. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  359. (match-string 1 m))
  360. (t "fundamental"))))
  361. (defun org-edit-src-get-label-format (s)
  362. "Extract the label format."
  363. (save-match-data
  364. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  365. (match-string 1 s))))
  366. (defun org-edit-src-get-indentation (pos)
  367. "Extract the label format."
  368. (save-match-data
  369. (goto-char pos)
  370. (org-get-indentation)))
  371. (defun org-edit-src-exit ()
  372. "Exit special edit and protect problematic lines."
  373. (interactive)
  374. (unless (string-match "\\`*Org Edit " (buffer-name (current-buffer)))
  375. (error "This is not an sub-editing buffer, something is wrong..."))
  376. (let ((beg org-edit-src-beg-marker)
  377. (end org-edit-src-end-marker)
  378. (ovl org-edit-src-overlay)
  379. (buffer (current-buffer))
  380. (nindent org-edit-src-nindent)
  381. code line)
  382. (untabify (point-min) (point-max))
  383. (save-excursion
  384. (goto-char (point-min))
  385. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  386. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match "")))
  387. (setq line (if (org-bound-and-true-p org-edit-src-force-single-line)
  388. 1
  389. (org-current-line)))
  390. (when (org-bound-and-true-p org-edit-src-force-single-line)
  391. (goto-char (point-min))
  392. (while (re-search-forward "\n" nil t)
  393. (replace-match " "))
  394. (goto-char (point-min))
  395. (if (looking-at "\\s-*") (replace-match " "))
  396. (if (re-search-forward "\\s-+\\'" nil t)
  397. (replace-match "")))
  398. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  399. (goto-char (point-min))
  400. (while (re-search-forward
  401. (if (org-mode-p) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
  402. (replace-match ",\\1")))
  403. (when (org-bound-and-true-p org-edit-src-picture)
  404. (untabify (point-min) (point-max))
  405. (goto-char (point-min))
  406. (while (re-search-forward "^" nil t)
  407. (replace-match ": ")))
  408. (when nindent
  409. (setq nindent (make-string (+ org-edit-src-content-indentation nindent)
  410. ?\ ))
  411. (goto-char (point-min))
  412. (while (re-search-forward "^" nil t)
  413. (replace-match nindent)))
  414. (setq code (buffer-string))
  415. (switch-to-buffer (marker-buffer beg))
  416. (kill-buffer buffer)
  417. (goto-char beg)
  418. (org-delete-overlay ovl)
  419. (delete-region beg end)
  420. (insert code)
  421. (goto-char beg)
  422. (goto-line (1- (+ (org-current-line) line)))
  423. (move-marker beg nil)
  424. (move-marker end nil)))
  425. (defun org-edit-src-save ()
  426. "Save parent buffer with current state source-code buffer."
  427. (interactive)
  428. (let ((p (point)) (m (mark)) msg)
  429. (org-edit-src-exit)
  430. (save-buffer)
  431. (setq msg (current-message))
  432. (org-edit-src-code)
  433. (push-mark m 'nomessage)
  434. (goto-char (min p (point-max)))
  435. (message (or msg ""))))
  436. (provide 'org-src)
  437. ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
  438. ;;; org-src.el ends here