org-list.el 45 KB

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