org-list.el 56 KB

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