org-list.el 47 KB

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