org-list.el 56 KB

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