org-list.el 54 KB

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