org-list.el 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. ;;; org-list.el --- Plain lists for Org-mode
  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.28trans
  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 plain lists in Org-mode.
  28. ;;; Code:
  29. (require 'org-macs)
  30. (require 'org-compat)
  31. (defvar org-blank-before-new-entry)
  32. (defvar org-M-RET-may-split-line)
  33. (declare-function org-invisible-p "org" ())
  34. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  35. (declare-function outline-next-heading "outline" ())
  36. (declare-function outline-back-to-heading "outline" (&optional invisible-ok))
  37. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  38. (declare-function org-back-over-empty-lines "org" ())
  39. (declare-function org-skip-whitespace "org" ())
  40. (declare-function org-trim "org" (s))
  41. (declare-function org-get-indentation "org" (&optional line))
  42. (declare-function org-timer-item "org-timer" (&optional arg))
  43. (declare-function org-combine-plists "org" (&rest plists))
  44. (declare-function org-entry-get "org" (pom property &optional inherit))
  45. (declare-function org-narrow-to-subtree "org" ())
  46. (declare-function org-show-subtree "org" ())
  47. (defgroup org-plain-lists nil
  48. "Options concerning plain lists in Org-mode."
  49. :tag "Org Plain lists"
  50. :group 'org-structure)
  51. (defcustom org-cycle-include-plain-lists t
  52. "When t, make TAB cycle visibility on plain list items.
  53. Cycling plain lists works only when the cursor is on a plain list
  54. item. When the cursor is on an outline heading, plain lists are
  55. treated as text. This is the most stable way of handling this,
  56. which is why it is the default.
  57. When this is the symbol `integrate', then during cycling, plain
  58. list items will *temporarily* be interpreted as outline headlines
  59. with a level given by 1000+i where i is the indentation of the
  60. bullet. This setting can lead to strange effects when switching
  61. visibility to `children', because the first \"child\" in a
  62. subtree decides what children should be listed. If that first
  63. \"child\" is a plain list item with an implied large level
  64. number, all true children and grand children of the outline
  65. heading will be exposed in a children' view."
  66. :group 'org-plain-lists
  67. :type '(choice
  68. (const :tag "Never" nil)
  69. (const :tag "With cursor in plain list (recommended)" t)
  70. (const :tag "As children of outline headings" integrate)))
  71. (defcustom org-plain-list-ordered-item-terminator t
  72. "The character that makes a line with leading number an ordered list item.
  73. Valid values are ?. and ?\). To get both terminators, use t. While
  74. ?. may look nicer, it creates the danger that a line with leading
  75. number may be incorrectly interpreted as an item. ?\) therefore is
  76. the safe choice."
  77. :group 'org-plain-lists
  78. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  79. (const :tag "paren like in \"2)\"" ?\))
  80. (const :tab "both" t)))
  81. (defcustom org-list-two-spaces-after-bullet-regexp nil
  82. "A regular expression matching bullets that should have 2 spaces after them.
  83. When nil, no bullet will have two spaces after them.
  84. When a string, it will be used as a regular expression. When the bullet
  85. type of a list is changed, the new bullet type will be matched against this
  86. regexp. If it matches, there will be two spaces instead of one after
  87. the bullet in each item of he list."
  88. :group 'org-plain-list
  89. :type '(choice
  90. (const :tag "never" nil)
  91. (regexp)))
  92. (defcustom org-empty-line-terminates-plain-lists nil
  93. "Non-nil means, an empty line ends all plain list levels.
  94. When nil, empty lines are part of the preceding item."
  95. :group 'org-plain-lists
  96. :type 'boolean)
  97. (defcustom org-auto-renumber-ordered-lists t
  98. "Non-nil means, automatically renumber ordered plain lists.
  99. Renumbering happens when the sequence have been changed with
  100. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  101. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  102. :group 'org-plain-lists
  103. :type 'boolean)
  104. (defcustom org-provide-checkbox-statistics t
  105. "Non-nil means, update checkbox statistics after insert and toggle.
  106. When this is set, checkbox statistics is updated each time you either insert
  107. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  108. with \\[org-ctrl-c-ctrl-c\\]."
  109. :group 'org-plain-lists
  110. :type 'boolean)
  111. (defcustom org-hierarchical-checkbox-statistics t
  112. "Non-nil means, checkbox statistics counts only the state of direct children.
  113. When nil, all boxes below the cookie are counted."
  114. :group 'org-plain-lists
  115. :type 'boolean)
  116. (defcustom org-description-max-indent 20
  117. "Maximum indentation for the second line of a description list.
  118. When the indentation would be larger than this, it will become
  119. 5 characters instead."
  120. :group 'org-plain-lists
  121. :type 'integer)
  122. (defvar org-list-beginning-re
  123. "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) +\\(.*\\)$")
  124. (defcustom org-list-radio-list-templates
  125. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  126. % END RECEIVE ORGLST %n
  127. \\begin{comment}
  128. #+ORGLST: SEND %n org-list-to-latex
  129. | | |
  130. \\end{comment}\n")
  131. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  132. @c END RECEIVE ORGLST %n
  133. @ignore
  134. #+ORGLST: SEND %n org-list-to-texinfo
  135. | | |
  136. @end ignore\n")
  137. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  138. <!-- END RECEIVE ORGLST %n -->
  139. <!--
  140. #+ORGLST: SEND %n org-list-to-html
  141. | | |
  142. -->\n"))
  143. "Templates for radio lists in different major modes.
  144. All occurrences of %n in a template will be replaced with the name of the
  145. list, obtained by prompting the user."
  146. :group 'org-plain-lists
  147. :type '(repeat
  148. (list (symbol :tag "Major mode")
  149. (string :tag "Format"))))
  150. ;;;; Plain list items, including checkboxes
  151. ;;; Plain list items
  152. (defun org-at-item-p ()
  153. "Is point in a line starting a hand-formatted item?"
  154. (let ((llt org-plain-list-ordered-item-terminator))
  155. (save-excursion
  156. (goto-char (point-at-bol))
  157. (looking-at
  158. (cond
  159. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  160. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  161. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  162. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  163. (defun org-at-item-bullet-p ()
  164. "Is point at the bullet of a plain list item?"
  165. (and (org-at-item-p)
  166. (not (member (char-after) '(?\ ?\t)))
  167. (< (point) (match-end 0))))
  168. (defun org-in-item-p ()
  169. "It the cursor inside a plain list item.
  170. Does not have to be the first line."
  171. (save-excursion
  172. (condition-case nil
  173. (progn
  174. (org-beginning-of-item)
  175. (org-at-item-p)
  176. t)
  177. (error nil))))
  178. (defun org-insert-item (&optional checkbox)
  179. "Insert a new item at the current level.
  180. Return t when things worked, nil when we are not in an item."
  181. (when (save-excursion
  182. (condition-case nil
  183. (progn
  184. (org-beginning-of-item)
  185. (org-at-item-p)
  186. (if (org-invisible-p) (error "Invisible item"))
  187. t)
  188. (error nil)))
  189. (let* ((bul (match-string 0))
  190. (descp (save-excursion (goto-char (match-beginning 0))
  191. (beginning-of-line 1)
  192. (save-match-data
  193. (and (looking-at "[ \t]*\\(.*?\\) ::")
  194. (match-string 1)))))
  195. (empty-line-p (save-excursion
  196. (goto-char (match-beginning 0))
  197. (and (not (bobp))
  198. (or (beginning-of-line 0) t)
  199. (save-match-data
  200. (looking-at "[ \t]*$")))))
  201. (timerp (and descp
  202. (save-match-data
  203. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$"
  204. descp))))
  205. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  206. (match-end 0)))
  207. (blank-a (if org-empty-line-terminates-plain-lists
  208. nil
  209. (cdr (assq 'plain-list-item org-blank-before-new-entry))))
  210. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  211. pos)
  212. (if descp (setq checkbox nil))
  213. (if timerp
  214. (progn (org-timer-item) t)
  215. (cond
  216. ((and (org-at-item-p) (<= (point) eow))
  217. ;; before the bullet
  218. (beginning-of-line 1)
  219. (open-line (if blank 2 1)))
  220. ((<= (point) eow)
  221. (beginning-of-line 1))
  222. (t
  223. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  224. (end-of-line 1)
  225. (delete-horizontal-space))
  226. (newline (if blank 2 1))))
  227. (insert bul
  228. (if checkbox "[ ]" "")
  229. (if descp (concat (if checkbox " " "")
  230. (read-string "Term: ") " :: ") ""))
  231. (just-one-space)
  232. (setq pos (point))
  233. (end-of-line 1)
  234. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  235. (org-maybe-renumber-ordered-list)
  236. (and checkbox (org-update-checkbox-count-maybe))
  237. t)))
  238. ;;; Checkboxes
  239. (defun org-at-item-checkbox-p ()
  240. "Is point at a line starting a plain-list item with a checklet?"
  241. (and (org-at-item-p)
  242. (save-excursion
  243. (goto-char (match-end 0))
  244. (skip-chars-forward " \t")
  245. (looking-at "\\[[- X]\\]"))))
  246. (defun org-toggle-checkbox (&optional toggle-presence)
  247. "Toggle the checkbox in the current line.
  248. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  249. With double prefix, set checkbox to [-].
  250. When there is an active region, toggle status or presence of the checkbox
  251. in the first line, and make every item in the region have the same
  252. status or presence, respectively.
  253. If the cursor is in a headline, apply this to all checkbox items in the
  254. text below the heading."
  255. (interactive "P")
  256. (catch 'exit
  257. (let (beg end status first-present first-status blocked)
  258. (cond
  259. ((org-region-active-p)
  260. (setq beg (region-beginning) end (region-end)))
  261. ((org-on-heading-p)
  262. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  263. ((org-at-item-checkbox-p)
  264. (save-excursion
  265. (if (equal toggle-presence '(4))
  266. (progn
  267. (replace-match "")
  268. (goto-char (match-beginning 0))
  269. (just-one-space))
  270. (when (setq blocked (org-checkbox-blocked-p))
  271. (error "Checkbox blocked because of unchecked box in line %d"
  272. blocked))
  273. (replace-match
  274. (cond ((equal toggle-presence '(16)) "[-]")
  275. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  276. (t "[ ]"))
  277. t t)))
  278. (throw 'exit t))
  279. ((org-at-item-p)
  280. ;; add a checkbox
  281. (save-excursion
  282. (goto-char (match-end 0))
  283. (insert "[ ] "))
  284. (throw 'exit t))
  285. (t (error "Not at a checkbox or heading, and no active region")))
  286. (setq end (move-marker (make-marker) end))
  287. (save-excursion
  288. (goto-char beg)
  289. (setq first-present (org-at-item-checkbox-p)
  290. first-status
  291. (save-excursion
  292. (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
  293. (equal (match-string 1) "[X]"))))
  294. (while (< (point) end)
  295. (if toggle-presence
  296. (cond
  297. ((and first-present (org-at-item-checkbox-p))
  298. (save-excursion
  299. (replace-match "")
  300. (goto-char (match-beginning 0))
  301. (just-one-space)))
  302. ((and (not first-present) (not (org-at-item-checkbox-p))
  303. (org-at-item-p))
  304. (save-excursion
  305. (goto-char (match-end 0))
  306. (insert "[ ] "))))
  307. (when (org-at-item-checkbox-p)
  308. (setq status (equal (match-string 0) "[X]"))
  309. (replace-match
  310. (if first-status "[ ]" "[X]") t t)))
  311. (beginning-of-line 2)))))
  312. (org-update-checkbox-count-maybe))
  313. (defun org-reset-checkbox-state-subtree ()
  314. "Reset all checkboxes in an entry subtree."
  315. (interactive "*")
  316. (save-restriction
  317. (save-excursion
  318. (org-narrow-to-subtree)
  319. (org-show-subtree)
  320. (goto-char (point-min))
  321. (let ((end (point-max)))
  322. (while (< (point) end)
  323. (when (org-at-item-checkbox-p)
  324. (replace-match "[ ]" t t))
  325. (beginning-of-line 2))))
  326. (org-update-checkbox-count-maybe)))
  327. (defun org-checkbox-blocked-p ()
  328. "Is the current checkbox blocked from for being checked now?
  329. A checkbox is blocked if all of the following conditions are fulfilled:
  330. 1. The checkbox is not checked already.
  331. 2. The current entry has the ORDERED property set.
  332. 3. There is an unchecked checkbox in this entry before the current line."
  333. (catch 'exit
  334. (save-match-data
  335. (save-excursion
  336. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  337. (when (equal (match-string 0) "[X]")
  338. ;; the box is already checked!
  339. (throw 'exit nil))
  340. (let ((end (point-at-bol)))
  341. (condition-case nil (org-back-to-heading t)
  342. (error (throw 'exit nil)))
  343. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  344. (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  345. (org-current-line)
  346. nil))))))
  347. (defvar org-checkbox-statistics-hook nil
  348. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  349. This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
  350. be used to implement alternative ways of collecting statistics information.")
  351. (defun org-update-checkbox-count-maybe ()
  352. "Update checkbox statistics unless turned off by user."
  353. (when org-provide-checkbox-statistics
  354. (org-update-checkbox-count))
  355. (run-hooks 'org-checkbox-statistics-hook))
  356. (defun org-update-checkbox-count (&optional all)
  357. "Update the checkbox statistics in the current section.
  358. This will find all statistic cookies like [57%] and [6/12] and update them
  359. with the current numbers. With optional prefix argument ALL, do this for
  360. the whole buffer."
  361. (interactive "P")
  362. (save-excursion
  363. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  364. (beg (condition-case nil
  365. (progn (org-back-to-heading) (point))
  366. (error (point-min))))
  367. (end (move-marker (make-marker)
  368. (progn (outline-next-heading) (point))))
  369. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  370. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  371. (re-find (concat re "\\|" re-box))
  372. beg-cookie end-cookie is-percent c-on c-off lim
  373. eline curr-ind next-ind continue-from startsearch
  374. (recursive
  375. (or (not org-hierarchical-checkbox-statistics)
  376. (string-match "\\<recursive\\>"
  377. (or (org-entry-get nil "COOKIE_DATA") ""))))
  378. (cstat 0)
  379. )
  380. (when all
  381. (goto-char (point-min))
  382. (outline-next-heading)
  383. (setq beg (point) end (point-max)))
  384. (goto-char end)
  385. ;; find each statistics cookie
  386. (while (and (re-search-backward re-find beg t)
  387. (not (save-match-data
  388. (and (org-on-heading-p)
  389. (string-match "\\<todo\\>"
  390. (downcase
  391. (or (org-entry-get
  392. nil "COOKIE_DATA")
  393. "")))))))
  394. (setq beg-cookie (match-beginning 1)
  395. end-cookie (match-end 1)
  396. cstat (+ cstat (if end-cookie 1 0))
  397. startsearch (point-at-eol)
  398. continue-from (match-beginning 0)
  399. is-percent (match-beginning 2)
  400. lim (cond
  401. ((org-on-heading-p) (outline-next-heading) (point))
  402. ((org-at-item-p) (org-end-of-item) (point))
  403. (t nil))
  404. c-on 0
  405. c-off 0)
  406. (when lim
  407. ;; find first checkbox for this cookie and gather
  408. ;; statistics from all that are at this indentation level
  409. (goto-char startsearch)
  410. (if (re-search-forward re-box lim t)
  411. (progn
  412. (org-beginning-of-item)
  413. (setq curr-ind (org-get-indentation))
  414. (setq next-ind curr-ind)
  415. (while (and (bolp) (org-at-item-p)
  416. (if recursive
  417. (<= curr-ind next-ind)
  418. (= curr-ind next-ind)))
  419. (save-excursion (end-of-line) (setq eline (point)))
  420. (if (re-search-forward re-box eline t)
  421. (if (member (match-string 2) '("[ ]" "[-]"))
  422. (setq c-off (1+ c-off))
  423. (setq c-on (1+ c-on))
  424. )
  425. )
  426. (if (not recursive)
  427. (org-end-of-item)
  428. (end-of-line)
  429. (when (re-search-forward org-list-beginning-re lim t)
  430. (beginning-of-line)))
  431. (setq next-ind (org-get-indentation))
  432. )))
  433. (goto-char continue-from)
  434. ;; update cookie
  435. (when end-cookie
  436. (delete-region beg-cookie end-cookie)
  437. (goto-char beg-cookie)
  438. (insert
  439. (if is-percent
  440. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  441. (format "[%d/%d]" c-on (+ c-on c-off)))))
  442. ;; update items checkbox if it has one
  443. (when (org-at-item-p)
  444. (org-beginning-of-item)
  445. (when (and (> (+ c-on c-off) 0)
  446. (re-search-forward re-box (point-at-eol) t))
  447. (setq beg-cookie (match-beginning 2)
  448. end-cookie (match-end 2))
  449. (delete-region beg-cookie end-cookie)
  450. (goto-char beg-cookie)
  451. (cond ((= c-off 0) (insert "[X]"))
  452. ((= c-on 0) (insert "[ ]"))
  453. (t (insert "[-]")))
  454. )))
  455. (goto-char continue-from))
  456. (when (interactive-p)
  457. (message "Checkbox statistics updated %s (%d places)"
  458. (if all "in entire file" "in current outline entry") cstat)))))
  459. (defun org-get-checkbox-statistics-face ()
  460. "Select the face for checkbox statistics.
  461. The face will be `org-done' when all relevant boxes are checked. Otherwise
  462. it will be `org-todo'."
  463. (if (match-end 1)
  464. (if (equal (match-string 1) "100%")
  465. 'org-checkbox-statistics-done
  466. 'org-checkbox-statistics-todo)
  467. (if (and (> (match-end 2) (match-beginning 2))
  468. (equal (match-string 2) (match-string 3)))
  469. 'org-checkbox-statistics-done
  470. 'org-checkbox-statistics-todo)))
  471. (defun org-beginning-of-item ()
  472. "Go to the beginning of the current hand-formatted item.
  473. If the cursor is not in an item, throw an error."
  474. (interactive)
  475. (let ((pos (point))
  476. (limit (save-excursion
  477. (condition-case nil
  478. (progn
  479. (org-back-to-heading)
  480. (beginning-of-line 2) (point))
  481. (error (point-min)))))
  482. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  483. ind ind1)
  484. (if (org-at-item-p)
  485. (beginning-of-line 1)
  486. (beginning-of-line 1)
  487. (skip-chars-forward " \t")
  488. (setq ind (current-column))
  489. (if (catch 'exit
  490. (while t
  491. (beginning-of-line 0)
  492. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  493. (if (looking-at "[ \t]*$")
  494. (setq ind1 ind-empty)
  495. (skip-chars-forward " \t")
  496. (setq ind1 (current-column)))
  497. (if (< ind1 ind)
  498. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  499. nil
  500. (goto-char pos)
  501. (error "Not in an item")))))
  502. (defun org-end-of-item ()
  503. "Go to the end of the current hand-formatted item.
  504. If the cursor is not in an item, throw an error."
  505. (interactive)
  506. (let* ((pos (point))
  507. ind1
  508. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  509. (limit (save-excursion (outline-next-heading) (point)))
  510. (ind (save-excursion
  511. (org-beginning-of-item)
  512. (skip-chars-forward " \t")
  513. (current-column)))
  514. (end (catch 'exit
  515. (while t
  516. (beginning-of-line 2)
  517. (if (eobp) (throw 'exit (point)))
  518. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  519. (if (looking-at "[ \t]*$")
  520. (setq ind1 ind-empty)
  521. (skip-chars-forward " \t")
  522. (setq ind1 (current-column)))
  523. (if (<= ind1 ind)
  524. (throw 'exit (point-at-bol)))))))
  525. (if end
  526. (goto-char end)
  527. (goto-char pos)
  528. (error "Not in an item"))))
  529. (defun org-next-item ()
  530. "Move to the beginning of the next item in the current plain list.
  531. Error if not at a plain list, or if this is the last item in the list."
  532. (interactive)
  533. (let (ind ind1 (pos (point)))
  534. (org-beginning-of-item)
  535. (setq ind (org-get-indentation))
  536. (org-end-of-item)
  537. (setq ind1 (org-get-indentation))
  538. (unless (and (org-at-item-p) (= ind ind1))
  539. (goto-char pos)
  540. (error "On last item"))))
  541. (defun org-previous-item ()
  542. "Move to the beginning of the previous item in the current plain list.
  543. Error if not at a plain list, or if this is the first item in the list."
  544. (interactive)
  545. (let (beg ind ind1 (pos (point)))
  546. (org-beginning-of-item)
  547. (setq beg (point))
  548. (setq ind (org-get-indentation))
  549. (goto-char beg)
  550. (catch 'exit
  551. (while t
  552. (beginning-of-line 0)
  553. (if (looking-at "[ \t]*$")
  554. nil
  555. (if (<= (setq ind1 (org-get-indentation)) ind)
  556. (throw 'exit t)))))
  557. (condition-case nil
  558. (if (or (not (org-at-item-p))
  559. (< ind1 (1- ind)))
  560. (error "")
  561. (org-beginning-of-item))
  562. (error (goto-char pos)
  563. (error "On first item")))))
  564. (defun org-first-list-item-p ()
  565. "Is this heading the item in a plain list?"
  566. (unless (org-at-item-p)
  567. (error "Not at a plain list item"))
  568. (org-beginning-of-item)
  569. (= (point) (save-excursion (org-beginning-of-item-list))))
  570. (defun org-move-item-down ()
  571. "Move the plain list item at point down, i.e. swap with following item.
  572. Subitems (items with larger indentation) are considered part of the item,
  573. so this really moves item trees."
  574. (interactive)
  575. (let ((col (current-column))
  576. (pos (point))
  577. beg beg0 end end0 ind ind1 txt ne-end ne-beg)
  578. (org-beginning-of-item)
  579. (setq beg0 (point))
  580. (save-excursion
  581. (setq ne-beg (org-back-over-empty-lines))
  582. (setq beg (point)))
  583. (goto-char beg0)
  584. (setq ind (org-get-indentation))
  585. (org-end-of-item)
  586. (setq end0 (point))
  587. (setq ind1 (org-get-indentation))
  588. (setq ne-end (org-back-over-empty-lines))
  589. (setq end (point))
  590. (goto-char beg0)
  591. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  592. ;; include less whitespace
  593. (save-excursion
  594. (goto-char beg)
  595. (forward-line (- ne-beg ne-end))
  596. (setq beg (point))))
  597. (goto-char end0)
  598. (if (and (org-at-item-p) (= ind ind1))
  599. (progn
  600. (org-end-of-item)
  601. (org-back-over-empty-lines)
  602. (setq txt (buffer-substring beg end))
  603. (save-excursion
  604. (delete-region beg end))
  605. (setq pos (point))
  606. (insert txt)
  607. (goto-char pos) (org-skip-whitespace)
  608. (org-maybe-renumber-ordered-list)
  609. (move-to-column col))
  610. (goto-char pos)
  611. (move-to-column col)
  612. (error "Cannot move this item further down"))))
  613. (defun org-move-item-up (arg)
  614. "Move the plain list item at point up, i.e. swap with previous item.
  615. Subitems (items with larger indentation) are considered part of the item,
  616. so this really moves item trees."
  617. (interactive "p")
  618. (let ((col (current-column)) (pos (point))
  619. beg beg0 end ind ind1 txt
  620. ne-beg ne-ins ins-end)
  621. (org-beginning-of-item)
  622. (setq beg0 (point))
  623. (setq ind (org-get-indentation))
  624. (save-excursion
  625. (setq ne-beg (org-back-over-empty-lines))
  626. (setq beg (point)))
  627. (goto-char beg0)
  628. (org-end-of-item)
  629. (org-back-over-empty-lines)
  630. (setq end (point))
  631. (goto-char beg0)
  632. (catch 'exit
  633. (while t
  634. (beginning-of-line 0)
  635. (if (looking-at "[ \t]*$")
  636. (if org-empty-line-terminates-plain-lists
  637. (progn
  638. (goto-char pos)
  639. (error "Cannot move this item further up"))
  640. nil)
  641. (if (<= (setq ind1 (org-get-indentation)) ind)
  642. (throw 'exit t)))))
  643. (condition-case nil
  644. (org-beginning-of-item)
  645. (error (goto-char beg0)
  646. (move-to-column col)
  647. (error "Cannot move this item further up")))
  648. (setq ind1 (org-get-indentation))
  649. (if (and (org-at-item-p) (= ind ind1))
  650. (progn
  651. (setq ne-ins (org-back-over-empty-lines))
  652. (setq txt (buffer-substring beg end))
  653. (save-excursion
  654. (delete-region beg end))
  655. (setq pos (point))
  656. (insert txt)
  657. (setq ins-end (point))
  658. (goto-char pos) (org-skip-whitespace)
  659. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  660. ;; Move whitespace back to beginning
  661. (save-excursion
  662. (goto-char ins-end)
  663. (let ((kill-whole-line t))
  664. (kill-line (- ne-ins ne-beg)) (point)))
  665. (insert (make-string (- ne-ins ne-beg) ?\n)))
  666. (org-maybe-renumber-ordered-list)
  667. (move-to-column col))
  668. (goto-char pos)
  669. (move-to-column col)
  670. (error "Cannot move this item further up"))))
  671. (defun org-maybe-renumber-ordered-list ()
  672. "Renumber the ordered list at point if setup allows it.
  673. This tests the user option `org-auto-renumber-ordered-lists' before
  674. doing the renumbering."
  675. (interactive)
  676. (when (and org-auto-renumber-ordered-lists
  677. (org-at-item-p))
  678. (if (match-beginning 3)
  679. (org-renumber-ordered-list 1)
  680. (org-fix-bullet-type))))
  681. (defun org-maybe-renumber-ordered-list-safe ()
  682. (condition-case nil
  683. (save-excursion
  684. (org-maybe-renumber-ordered-list))
  685. (error nil)))
  686. (defun org-cycle-list-bullet (&optional which)
  687. "Cycle through the different itemize/enumerate bullets.
  688. This cycle the entire list level through the sequence:
  689. `-' -> `+' -> `*' -> `1.' -> `1)'
  690. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  691. 0 means `-', 1 means `+' etc."
  692. (interactive "P")
  693. (org-preserve-lc
  694. (org-beginning-of-item-list)
  695. (org-at-item-p)
  696. (beginning-of-line 1)
  697. (let ((current (match-string 0))
  698. (prevp (eq which 'previous))
  699. new old)
  700. (setq new (cond
  701. ((and (numberp which)
  702. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  703. ((string-match "-" current) (if prevp "1)" "+"))
  704. ((string-match "\\+" current)
  705. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  706. ((string-match "\\*" current) (if prevp "+" "1."))
  707. ((string-match "\\." current)
  708. (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
  709. ((string-match ")" current) (if prevp "1." "-"))
  710. (t (error "This should not happen"))))
  711. (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
  712. (setq old (match-string 2))
  713. (replace-match (concat "\\1" new)))
  714. (org-shift-item-indentation (- (length new) (length old)))
  715. (org-fix-bullet-type)
  716. (org-maybe-renumber-ordered-list))))
  717. (defun org-get-string-indentation (s)
  718. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  719. (let ((n -1) (i 0) (w tab-width) c)
  720. (catch 'exit
  721. (while (< (setq n (1+ n)) (length s))
  722. (setq c (aref s n))
  723. (cond ((= c ?\ ) (setq i (1+ i)))
  724. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  725. (t (throw 'exit t)))))
  726. i))
  727. (defun org-renumber-ordered-list (arg)
  728. "Renumber an ordered plain list.
  729. Cursor needs to be in the first line of an item, the line that starts
  730. with something like \"1.\" or \"2)\"."
  731. (interactive "p")
  732. (unless (and (org-at-item-p)
  733. (match-beginning 3))
  734. (error "This is not an ordered list"))
  735. (let ((line (org-current-line))
  736. (col (current-column))
  737. (ind (org-get-string-indentation
  738. (buffer-substring (point-at-bol) (match-beginning 3))))
  739. ;; (term (substring (match-string 3) -1))
  740. ind1 (n (1- arg))
  741. fmt bobp old new)
  742. ;; find where this list begins
  743. (org-beginning-of-item-list)
  744. (setq bobp (bobp))
  745. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  746. (setq fmt (concat "%d" (or (match-string 1) ".")))
  747. (beginning-of-line 0)
  748. ;; walk forward and replace these numbers
  749. (catch 'exit
  750. (while t
  751. (catch 'next
  752. (if bobp (setq bobp nil) (beginning-of-line 2))
  753. (if (eobp) (throw 'exit nil))
  754. (if (looking-at "[ \t]*$") (throw 'next nil))
  755. (skip-chars-forward " \t") (setq ind1 (current-column))
  756. (if (> ind1 ind) (throw 'next t))
  757. (if (< ind1 ind) (throw 'exit t))
  758. (if (not (org-at-item-p)) (throw 'exit nil))
  759. (setq old (match-string 2))
  760. (delete-region (match-beginning 2) (match-end 2))
  761. (goto-char (match-beginning 2))
  762. (insert (setq new (format fmt (setq n (1+ n)))))
  763. (org-shift-item-indentation (- (length new) (length old))))))
  764. (goto-line line)
  765. (org-move-to-column col)))
  766. (defun org-fix-bullet-type ()
  767. "Make sure all items in this list have the same bullet as the first item.
  768. Also, fix the indentation."
  769. (interactive)
  770. (unless (org-at-item-p) (error "This is not a list"))
  771. (let ((line (org-current-line))
  772. (col (current-column))
  773. (ind (current-indentation))
  774. ind1 bullet oldbullet)
  775. ;; find where this list begins
  776. (org-beginning-of-item-list)
  777. (beginning-of-line 1)
  778. ;; find out what the bullet type is
  779. (looking-at "[ \t]*\\(\\S-+\\)")
  780. (setq bullet (concat (match-string 1) " "))
  781. (if (and org-list-two-spaces-after-bullet-regexp
  782. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  783. (setq bullet (concat bullet " ")))
  784. ;; walk forward and replace these numbers
  785. (beginning-of-line 0)
  786. (catch 'exit
  787. (while t
  788. (catch 'next
  789. (beginning-of-line 2)
  790. (if (eobp) (throw 'exit nil))
  791. (if (looking-at "[ \t]*$") (throw 'next nil))
  792. (skip-chars-forward " \t") (setq ind1 (current-column))
  793. (if (> ind1 ind) (throw 'next t))
  794. (if (< ind1 ind) (throw 'exit t))
  795. (if (not (org-at-item-p)) (throw 'exit nil))
  796. (skip-chars-forward " \t")
  797. (looking-at "\\S-+ *")
  798. (setq oldbullet (match-string 0))
  799. (unless (equal bullet oldbullet) (replace-match bullet))
  800. (org-shift-item-indentation (- (length bullet) (length oldbullet))))))
  801. (goto-line line)
  802. (org-move-to-column col)
  803. (if (string-match "[0-9]" bullet)
  804. (org-renumber-ordered-list 1))))
  805. (defun org-shift-item-indentation (delta)
  806. "Shift the indentation in current item by DELTA."
  807. (save-excursion
  808. (let ((beg (point-at-bol))
  809. (end (progn (org-end-of-item) (point)))
  810. i)
  811. (goto-char end)
  812. (beginning-of-line 0)
  813. (while (> (point) beg)
  814. (when (looking-at "[ \t]*\\S-")
  815. ;; this is not an empty line
  816. (setq i (org-get-indentation))
  817. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  818. (indent-line-to i)))
  819. (beginning-of-line 0)))))
  820. (defun org-beginning-of-item-list ()
  821. "Go to the beginning of the current item list.
  822. I.e. to the first item in this list."
  823. (interactive)
  824. (org-beginning-of-item)
  825. (let ((pos (point-at-bol))
  826. (ind (org-get-indentation))
  827. ind1)
  828. ;; find where this list begins
  829. (catch 'exit
  830. (while t
  831. (catch 'next
  832. (beginning-of-line 0)
  833. (if (looking-at "[ \t]*$")
  834. (throw (if (bobp) 'exit 'next) t))
  835. (skip-chars-forward " \t") (setq ind1 (current-column))
  836. (if (or (< ind1 ind)
  837. (and (= ind1 ind)
  838. (not (org-at-item-p)))
  839. (and (= (point-at-bol) (point-min))
  840. (setq pos (point-min))))
  841. (throw 'exit t)
  842. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  843. (goto-char pos)))
  844. (defun org-end-of-item-list ()
  845. "Go to the end of the current item list.
  846. I.e. to the text after the last item."
  847. (interactive)
  848. (org-beginning-of-item)
  849. (let ((pos (point-at-bol))
  850. (ind (org-get-indentation))
  851. ind1)
  852. ;; find where this list begins
  853. (catch 'exit
  854. (while t
  855. (catch 'next
  856. (beginning-of-line 2)
  857. (if (looking-at "[ \t]*$")
  858. (if (eobp)
  859. (progn (setq pos (point)) (throw 'exit t))
  860. (throw 'next t)))
  861. (skip-chars-forward " \t") (setq ind1 (current-column))
  862. (if (or (< ind1 ind)
  863. (and (= ind1 ind)
  864. (not (org-at-item-p)))
  865. (eobp))
  866. (progn
  867. (setq pos (point-at-bol))
  868. (throw 'exit t))))))
  869. (goto-char pos)))
  870. (defvar org-last-indent-begin-marker (make-marker))
  871. (defvar org-last-indent-end-marker (make-marker))
  872. (defun org-outdent-item (arg)
  873. "Outdent a local list item."
  874. (interactive "p")
  875. (org-indent-item (- arg)))
  876. (defun org-indent-item (arg)
  877. "Indent a local list item."
  878. (interactive "p")
  879. (and (org-region-active-p) (org-cursor-to-region-beginning))
  880. (unless (org-at-item-p)
  881. (error "Not on an item"))
  882. (save-excursion
  883. (let (beg end ind ind1 tmp delta ind-down ind-up)
  884. (setq end (and (org-region-active-p) (region-end)))
  885. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  886. (setq beg org-last-indent-begin-marker
  887. end org-last-indent-end-marker)
  888. (org-beginning-of-item)
  889. (setq beg (move-marker org-last-indent-begin-marker (point)))
  890. (org-end-of-item)
  891. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  892. (goto-char beg)
  893. (setq tmp (org-item-indent-positions)
  894. ind (car tmp)
  895. ind-down (nth 2 tmp)
  896. ind-up (nth 1 tmp)
  897. delta (if (> arg 0)
  898. (if ind-down (- ind-down ind) 2)
  899. (if ind-up (- ind-up ind) -2)))
  900. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  901. (while (< (point) end)
  902. (beginning-of-line 1)
  903. (skip-chars-forward " \t") (setq ind1 (current-column))
  904. (delete-region (point-at-bol) (point))
  905. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  906. (beginning-of-line 2))))
  907. (org-fix-bullet-type)
  908. (org-maybe-renumber-ordered-list-safe)
  909. (save-excursion
  910. (beginning-of-line 0)
  911. (condition-case nil (org-beginning-of-item) (error nil))
  912. (org-maybe-renumber-ordered-list-safe)))
  913. (defun org-item-indent-positions ()
  914. "Return indentation for plain list items.
  915. This returns a list with three values: The current indentation, the
  916. parent indentation and the indentation a child should have.
  917. Assumes cursor in item line."
  918. (let* ((bolpos (point-at-bol))
  919. (ind (org-get-indentation))
  920. ind-down ind-up pos)
  921. (save-excursion
  922. (org-beginning-of-item-list)
  923. (skip-chars-backward "\n\r \t")
  924. (when (org-in-item-p)
  925. (org-beginning-of-item)
  926. (setq ind-up (org-get-indentation))))
  927. (setq pos (point))
  928. (save-excursion
  929. (cond
  930. ((and (condition-case nil (progn (org-previous-item) t)
  931. (error nil))
  932. (or (forward-char 1) t)
  933. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  934. (setq ind-down (org-get-indentation)))
  935. ((and (goto-char pos)
  936. (org-at-item-p))
  937. (goto-char (match-end 0))
  938. (skip-chars-forward " \t")
  939. (setq ind-down (current-column)))))
  940. (list ind ind-up ind-down)))
  941. ;;; Send and receive lists
  942. (defun org-list-parse-list (&optional delete)
  943. "Parse the list at point and maybe DELETE it.
  944. Return a list containing first level items as strings and
  945. sublevels as a list of strings."
  946. (let* ((item-beginning (org-list-item-beginning))
  947. (start (car item-beginning))
  948. (end (org-list-end (cdr item-beginning)))
  949. output itemsep ltype)
  950. (while (re-search-forward org-list-beginning-re end t)
  951. (goto-char (match-beginning 3))
  952. (save-match-data
  953. (cond ((string-match "[0-9]" (match-string 2))
  954. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  955. ltype 'ordered))
  956. ((string-match "^.*::" (match-string 0))
  957. (setq itemsep "[-+]" ltype 'descriptive))
  958. (t (setq itemsep "[-+]" ltype 'unordered))))
  959. (let* ((indent1 (match-string 1))
  960. (nextitem (save-excursion
  961. (save-match-data
  962. (or (and (re-search-forward
  963. (concat "^" indent1 itemsep " *?") end t)
  964. (match-beginning 0)) end))))
  965. (item (buffer-substring
  966. (point)
  967. (or (and (re-search-forward
  968. org-list-beginning-re end t)
  969. (goto-char (match-beginning 0)))
  970. (goto-char end))))
  971. (nextindent (match-string 1))
  972. (item (org-trim item))
  973. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  974. (replace-match (if (equal (match-string 1 item) " ")
  975. "[CBOFF]"
  976. "[CBON]")
  977. t nil item)
  978. item)))
  979. (push item output)
  980. (when (> (length nextindent)
  981. (length indent1))
  982. (narrow-to-region (point) nextitem)
  983. (push (org-list-parse-list) output)
  984. (widen))))
  985. (when delete (delete-region start end))
  986. (setq output (nreverse output))
  987. (push ltype output)))
  988. (defun org-list-item-beginning ()
  989. "Find the beginning of the list item.
  990. Return a cons which car is the beginning position of the item and
  991. cdr is the indentation string."
  992. (save-excursion
  993. (if (not (or (looking-at org-list-beginning-re)
  994. (re-search-backward
  995. org-list-beginning-re nil t)))
  996. (progn (goto-char (point-min)) (point))
  997. (cons (match-beginning 0) (match-string 1)))))
  998. (defun org-list-end (indent)
  999. "Return the position of the end of the list.
  1000. INDENT is the indentation of the list, as a string."
  1001. (save-excursion
  1002. (catch 'exit
  1003. (while (or (looking-at org-list-beginning-re)
  1004. (looking-at (concat "^" indent "[ \t]+\\|^$"))
  1005. (> (or (get-text-property (point) 'original-indentation) -1)
  1006. (length indent)))
  1007. (if (eq (point) (point-max))
  1008. (throw 'exit (point-max)))
  1009. (forward-line 1)))
  1010. (point)))
  1011. (defun org-list-insert-radio-list ()
  1012. "Insert a radio list template appropriate for this major mode."
  1013. (interactive)
  1014. (let* ((e (assq major-mode org-list-radio-list-templates))
  1015. (txt (nth 1 e))
  1016. name pos)
  1017. (unless e (error "No radio list setup defined for %s" major-mode))
  1018. (setq name (read-string "List name: "))
  1019. (while (string-match "%n" txt)
  1020. (setq txt (replace-match name t t txt)))
  1021. (or (bolp) (insert "\n"))
  1022. (setq pos (point))
  1023. (insert txt)
  1024. (goto-char pos)))
  1025. (defun org-list-send-list (&optional maybe)
  1026. "Send a tranformed version of this list to the receiver position.
  1027. With argument MAYBE, fail quietly if no transformation is defined for
  1028. this list."
  1029. (interactive)
  1030. (catch 'exit
  1031. (unless (org-at-item-p) (error "Not at a list"))
  1032. (save-excursion
  1033. (goto-char (car (org-list-item-beginning)))
  1034. (beginning-of-line 0)
  1035. (unless (looking-at "#\\+ORGLST: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  1036. (if maybe
  1037. (throw 'exit nil)
  1038. (error "Don't know how to transform this list"))))
  1039. (let* ((name (match-string 1))
  1040. (item-beginning (org-list-item-beginning))
  1041. (transform (intern (match-string 2)))
  1042. (txt (buffer-substring-no-properties
  1043. (car item-beginning)
  1044. (org-list-end (cdr item-beginning))))
  1045. (list (org-list-parse-list))
  1046. beg)
  1047. (unless (fboundp transform)
  1048. (error "No such transformation function %s" transform))
  1049. (setq txt (funcall transform list))
  1050. ;; Find the insertion place
  1051. (save-excursion
  1052. (goto-char (point-min))
  1053. (unless (re-search-forward
  1054. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1055. (error "Don't know where to insert translated list"))
  1056. (goto-char (match-beginning 0))
  1057. (beginning-of-line 2)
  1058. (setq beg (point))
  1059. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1060. (error "Cannot find end of insertion region"))
  1061. (beginning-of-line 1)
  1062. (delete-region beg (point))
  1063. (goto-char beg)
  1064. (insert txt "\n"))
  1065. (message "List converted and installed at receiver location"))))
  1066. (defun org-list-to-generic (list params)
  1067. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1068. Valid parameters PARAMS are
  1069. :ustart String to start an unordered list
  1070. :uend String to end an unordered list
  1071. :ostart String to start an ordered list
  1072. :oend String to end an ordered list
  1073. :dstart String to start a descriptive list
  1074. :dend String to end a descriptive list
  1075. :dtstart String to start a descriptive term
  1076. :dtend String to end a descriptive term
  1077. :ddstart String to start a description
  1078. :ddend String to end a description
  1079. :splice When set to t, return only list body lines, don't wrap
  1080. them into :[u/o]start and :[u/o]end. Default is nil.
  1081. :istart String to start a list item
  1082. :iend String to end a list item
  1083. :isep String to separate items
  1084. :lsep String to separate sublists
  1085. :cboff String to insert for an unchecked checkbox
  1086. :cbon String to insert for a checked checkbox"
  1087. (interactive)
  1088. (let* ((p params) sublist
  1089. (splicep (plist-get p :splice))
  1090. (ostart (plist-get p :ostart))
  1091. (oend (plist-get p :oend))
  1092. (ustart (plist-get p :ustart))
  1093. (uend (plist-get p :uend))
  1094. (dstart (plist-get p :dstart))
  1095. (dend (plist-get p :dend))
  1096. (dtstart (plist-get p :dtstart))
  1097. (dtend (plist-get p :dtend))
  1098. (ddstart (plist-get p :ddstart))
  1099. (ddend (plist-get p :ddend))
  1100. (istart (plist-get p :istart))
  1101. (iend (plist-get p :iend))
  1102. (isep (plist-get p :isep))
  1103. (lsep (plist-get p :lsep))
  1104. (cbon (plist-get p :cbon))
  1105. (cboff (plist-get p :cboff)))
  1106. (let ((wrapper
  1107. (cond ((eq (car list) 'ordered)
  1108. (concat ostart "\n%s" oend "\n"))
  1109. ((eq (car list) 'unordered)
  1110. (concat ustart "\n%s" uend "\n"))
  1111. ((eq (car list) 'descriptive)
  1112. (concat dstart "\n%s" dend "\n"))))
  1113. rtn term defstart defend)
  1114. (while (setq sublist (pop list))
  1115. (cond ((symbolp sublist) nil)
  1116. ((stringp sublist)
  1117. (when (string-match "^\\(.*\\) ::" sublist)
  1118. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1119. (match-string 1 sublist))))
  1120. (setq sublist (substring sublist (1+ (length term)))))
  1121. (if (string-match "\\[CBON\\]" sublist)
  1122. (setq sublist (replace-match cbon t t sublist)))
  1123. (if (string-match "\\[CBOFF\\]" sublist)
  1124. (setq sublist (replace-match cboff t t sublist)))
  1125. (setq rtn (concat rtn istart term ddstart
  1126. sublist ddend iend isep)))
  1127. (t (setq rtn (concat rtn ;; previous list
  1128. lsep ;; list separator
  1129. (org-list-to-generic sublist p)
  1130. lsep ;; list separator
  1131. )))))
  1132. (format wrapper rtn))))
  1133. (defun org-list-to-latex (list &optional params)
  1134. "Convert LIST into a LaTeX list.
  1135. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1136. with overruling parameters for `org-list-to-generic'."
  1137. (org-list-to-generic
  1138. list
  1139. (org-combine-plists
  1140. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1141. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1142. :dstart "\\begin{description}" :dend "\\end{description}"
  1143. :dtstart "[" :dtend "]"
  1144. :ddstart "" :ddend ""
  1145. :istart "\\item " :iend ""
  1146. :isep "\n" :lsep "\n"
  1147. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1148. params)))
  1149. (defun org-list-to-html (list &optional params)
  1150. "Convert LIST into a HTML list.
  1151. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1152. with overruling parameters for `org-list-to-generic'."
  1153. (org-list-to-generic
  1154. list
  1155. (org-combine-plists
  1156. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1157. :ustart "<ul>" :uend "</ul>"
  1158. :dstart "<dl>" :dend "</dl>"
  1159. :dtstart "<dt>" :dtend "</dt>"
  1160. :ddstart "<dd>" :ddend "</dd>"
  1161. :istart "<li>" :iend "</li>"
  1162. :isep "\n" :lsep "\n"
  1163. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1164. params)))
  1165. (defun org-list-to-texinfo (list &optional params)
  1166. "Convert LIST into a Texinfo list.
  1167. LIST is as returnd by `org-list-parse-list'. PARAMS is a property list
  1168. with overruling parameters for `org-list-to-generic'."
  1169. (org-list-to-generic
  1170. list
  1171. (org-combine-plists
  1172. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1173. :ustart "@enumerate" :uend "@end enumerate"
  1174. :dstart "@table" :dend "@end table"
  1175. :dtstart "@item " :dtend "\n"
  1176. :ddstart "" :ddend ""
  1177. :istart "@item\n" :iend ""
  1178. :isep "\n" :lsep "\n"
  1179. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1180. params)))
  1181. (provide 'org-list)
  1182. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1183. ;;; org-list.el ends here