org-list.el 46 KB

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