org-list.el 48 KB

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