org-src.el 17 KB

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