org-list.el 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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-trim "org" (s))
  43. (declare-function org-get-indentation "org" (&optional line))
  44. (declare-function org-timer-item "org-timer" (&optional arg))
  45. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  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-list-ending-method 'regexp
  133. "Determine where plain lists should end.
  134. Valid values are symbols 'regexp, 'indent or 'both.
  135. When set to 'regexp, Org will look into two variables,
  136. `org-empty-line-terminates-plain-lists' and the more general
  137. `org-list-end-regexp', to know what will end lists. This is the
  138. default value.
  139. When set to 'indent, indentation of the last non-blank line will
  140. determine if point is in a list. If that line is less indented
  141. than the previous item in the section, if any, list has ended.
  142. When set to 'both, each of the preceding methods must confirm
  143. that point is in a list."
  144. :group 'org-plain-lists
  145. :type '(choice
  146. (const :tag "With a well defined ending (recommended)" regexp)
  147. (const :tag "With indentation of the current line" indent)
  148. (const :tag "With both methods" both)))
  149. (defcustom org-empty-line-terminates-plain-lists nil
  150. "Non-nil means an empty line ends all plain list levels.
  151. This variable only makes sense if `org-list-ending-method' is set
  152. to 'regexp or 'both. This is then equivalent to set
  153. `org-list-end-regexp' to \"^[ \\t]*$\"."
  154. :group 'org-plain-lists
  155. :type 'boolean)
  156. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  157. "Regexp matching the end of all plain list levels.
  158. It must start with \"^\" and end with \"\\n\". It defaults to 2
  159. blank lines. `org-empty-line-terminates-plain-lists' has
  160. precedence over it."
  161. :group 'org-plain-lists
  162. :type 'string)
  163. (defcustom org-list-automatic-rules '((bullet . t)
  164. (checkbox . t)
  165. (indent . t)
  166. (insert . t)
  167. (renumber . t))
  168. "Non-nil means apply set of rules when acting on lists.
  169. By default, automatic actions are taken when using
  170. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  171. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  172. \\[org-shiftmetaleft], \\[org-ctrl-c-minus],
  173. \\[org-toggle-checkbox] or \\[org-insert-todo-heading]. You can
  174. disable individually these rules by setting them to nil. Valid
  175. rules are:
  176. bullet when non-nil, cycling bullet do not allow lists at
  177. column 0 to have * as a bullet and descriptions lists
  178. to be numbered.
  179. checkbox when non-nil, checkbox statistics is updated each time
  180. you either insert a new checkbox or toggle a checkbox.
  181. It also prevents from inserting a checkbox in a
  182. description item.
  183. indent when non-nil indenting or outdenting list top-item with
  184. its subtree will move the whole list and outdenting a
  185. list whose bullet is * to column 0 will change that
  186. bullet to -.
  187. insert when non-nil, trying to insert an item inside a block
  188. will insert it right before the block instead of
  189. throwing an error.
  190. renumber when non-nil, renumber ordered plain lists whenever it
  191. is modified. You can always use \\[org-ctrl-c-ctrl-c]
  192. to trigger renumbering."
  193. :group 'org-plain-lists
  194. :type '(alist :tag "Sets of rules"
  195. :key-type
  196. (choice
  197. (const :tag "Bullet" bullet)
  198. (const :tag "Checkbox" checkbox)
  199. (const :tag "Indent" indent)
  200. (const :tag "Insert" insert)
  201. (const :tag "Renumber" renumber))
  202. :value-type
  203. (boolean :tag "Activate" :value t)))
  204. (defcustom org-hierarchical-checkbox-statistics t
  205. "Non-nil means checkbox statistics counts only the state of direct children.
  206. When nil, all boxes below the cookie are counted.
  207. This can be set to nil on a per-node basis using a COOKIE_DATA property
  208. with the word \"recursive\" in the value."
  209. :group 'org-plain-lists
  210. :type 'boolean)
  211. (defcustom org-description-max-indent 20
  212. "Maximum indentation for the second line of a description list.
  213. When the indentation would be larger than this, it will become
  214. 5 characters instead."
  215. :group 'org-plain-lists
  216. :type 'integer)
  217. (defcustom org-list-radio-list-templates
  218. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  219. % END RECEIVE ORGLST %n
  220. \\begin{comment}
  221. #+ORGLST: SEND %n org-list-to-latex
  222. -
  223. \\end{comment}\n")
  224. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  225. @c END RECEIVE ORGLST %n
  226. @ignore
  227. #+ORGLST: SEND %n org-list-to-texinfo
  228. -
  229. @end ignore\n")
  230. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  231. <!-- END RECEIVE ORGLST %n -->
  232. <!--
  233. #+ORGLST: SEND %n org-list-to-html
  234. -
  235. -->\n"))
  236. "Templates for radio lists in different major modes.
  237. All occurrences of %n in a template will be replaced with the name of the
  238. list, obtained by prompting the user."
  239. :group 'org-plain-lists
  240. :type '(repeat
  241. (list (symbol :tag "Major mode")
  242. (string :tag "Format"))))
  243. ;;; Internal functions
  244. (defun org-list-end-re ()
  245. "Return the regex corresponding to the end of a list.
  246. It depends on `org-empty-line-terminates-plain-lists'."
  247. (if org-empty-line-terminates-plain-lists
  248. "^[ \t]*\n"
  249. org-list-end-regexp))
  250. (defun org-item-re (&optional general)
  251. "Return the correct regular expression for plain lists.
  252. If GENERAL is non-nil, return the general regexp independent of the value
  253. of `org-plain-list-ordered-item-terminator'."
  254. (cond
  255. ((or general (eq org-plain-list-ordered-item-terminator t))
  256. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  257. ((= org-plain-list-ordered-item-terminator ?.)
  258. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  259. ((= org-plain-list-ordered-item-terminator ?\))
  260. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  261. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  262. (defconst org-item-beginning-re (concat "^" (org-item-re))
  263. "Regexp matching the beginning of a plain list item.")
  264. (defun org-list-terminator-between (min max &optional firstp)
  265. "Find the position of a list ender between MIN and MAX, or nil.
  266. This function looks for `org-list-end-re' outside a block.
  267. If FIRSTP in non-nil, return the point at the beginning of the
  268. nearest valid terminator from min. Otherwise, return the point at
  269. the end of the nearest terminator from max."
  270. (save-excursion
  271. (let* ((start (if firstp min max))
  272. (end (if firstp max min))
  273. (search-fun (if firstp
  274. #'org-search-forward-unenclosed
  275. #'org-search-backward-unenclosed))
  276. (list-end-p (progn
  277. (goto-char start)
  278. (funcall search-fun (org-list-end-re) end t))))
  279. ;; Is there a valid list terminator somewhere ?
  280. (and list-end-p
  281. ;; we want to be on the first line of the list ender
  282. (match-beginning 0)))))
  283. (defun org-list-maybe-skip-block (search limit)
  284. "Return non-nil value if point is in a block, skipping it on the way.
  285. It looks for the boundary of the block in SEARCH direction."
  286. (save-match-data
  287. (let ((case-fold-search t)
  288. (boundary (if (eq search 're-search-forward) 3 5)))
  289. (when (save-excursion
  290. (and (funcall search "^[ \t]*#\\+\\(begin\\|end\\)_" limit t)
  291. (= (length (match-string 1)) boundary)))
  292. ;; We're in a block: get out of it
  293. (goto-char (match-beginning 0))))))
  294. (defun org-list-search-unenclosed-generic (search re bound noerr)
  295. "Search for RE with SEARCH outside blocks and protected places."
  296. (catch 'exit
  297. (let ((origin (point)))
  298. (while t
  299. ;; 1. No match: return to origin or bound, depending on NOERR.
  300. (unless (funcall search re bound noerr)
  301. (throw 'exit (and (goto-char (if (booleanp noerr) origin bound))
  302. nil)))
  303. ;; 2. Match not in block or protected: return point. Else
  304. ;; skip the block and carry on.
  305. (unless (or (get-text-property (match-beginning 0) 'org-protected)
  306. (org-list-maybe-skip-block search bound))
  307. (throw 'exit (point)))))))
  308. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  309. "Like `re-search-backward' but don't stop inside blocks or protected places."
  310. (org-list-search-unenclosed-generic
  311. #'re-search-backward regexp (or bound (point-min)) noerror))
  312. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  313. "Like `re-search-forward' but don't stop inside blocks or protected places."
  314. (org-list-search-unenclosed-generic
  315. #'re-search-forward regexp (or bound (point-max)) noerror))
  316. (defun org-list-in-item-p-with-indent (limit)
  317. "Is the cursor inside a plain list?
  318. Plain lists are considered ending when a non-blank line is less
  319. indented than the previous item within LIMIT.
  320. Return the position of the previous item, if applicable."
  321. (save-excursion
  322. (beginning-of-line)
  323. ;; do not start searching at a blank line or inside a block
  324. (while (or (and (org-list-maybe-skip-block #'re-search-backward limit)
  325. (goto-char (1- (point-at-bol))))
  326. (looking-at "^[ \t]*$"))
  327. (skip-chars-backward " \r\t\n")
  328. (beginning-of-line))
  329. (or (and (org-at-item-p) (point-at-bol))
  330. (let ((ind (org-get-indentation)))
  331. (catch 'exit
  332. (while t
  333. (cond
  334. ((or (bobp) (< (point) limit)) (throw 'exit nil))
  335. ;; skip blank lines..
  336. ((and (not (looking-at "[ \t]*$"))
  337. ;; blocks...
  338. (not (org-list-maybe-skip-block
  339. #'re-search-backward limit))
  340. ;; and items more indented.
  341. (< (org-get-indentation) ind))
  342. (throw 'exit (and (org-at-item-p) (point-at-bol))))
  343. (t (beginning-of-line 0)))))))))
  344. (defun org-list-in-item-p-with-regexp (limit)
  345. "Is the cursor inside a plain list?
  346. Plain lists end when `org-list-end-regexp' is matched, or at a
  347. blank line if `org-empty-line-terminates-plain-lists' is true."
  348. (save-excursion
  349. (let* ((actual-pos (goto-char (point-at-eol)))
  350. ;; Moved to eol so current line can be matched by
  351. ;; `org-item-re'.
  352. (last-item-start (save-excursion
  353. (org-search-backward-unenclosed
  354. org-item-beginning-re limit t)))
  355. (list-ender (org-list-terminator-between
  356. last-item-start actual-pos)))
  357. ;; We are in a list when we are on an item line or when we can
  358. ;; find an item before point and there is no valid list ender
  359. ;; between it and the point.
  360. (and last-item-start
  361. (not list-ender)))))
  362. (defun org-list-top-point-with-regexp (limit)
  363. "Return point at the top level item in a list, or nil if not in a list.
  364. List ending is determined by regexp. See
  365. `org-list-ending-method'. for more information."
  366. (save-excursion
  367. (and (org-list-in-item-p-with-regexp limit)
  368. (let ((pos (point-at-eol)))
  369. ;; Is there some list above this one ? If so, go to its ending.
  370. ;; Otherwise, go back to the heading above or bob.
  371. (goto-char (or (org-list-terminator-between limit pos) limit))
  372. ;; From there, search down our list.
  373. (org-search-forward-unenclosed org-item-beginning-re pos t)
  374. (point-at-bol)))))
  375. (defun org-list-bottom-point-with-regexp (limit)
  376. "Return point just before list ending or nil if not in a list.
  377. List ending is determined by regexp. See
  378. `org-list-ending-method'. for more information."
  379. (save-excursion
  380. (and (org-in-item-p)
  381. (let ((pos (point)))
  382. ;; The list ending is either first point matching
  383. ;; `org-list-end-re', point at first white-line before next
  384. ;; heading, or eob.
  385. (or (org-list-terminator-between (min pos limit) limit t) limit)))))
  386. (defun org-list-top-point-with-indent (limit)
  387. "Return point just before list ending or nil if not in a list.
  388. List ending is determined by indentation of text. See
  389. `org-list-ending-method'. for more information."
  390. (save-excursion
  391. (let ((prev-p (org-list-in-item-p-with-indent limit)))
  392. (and prev-p
  393. (catch 'exit
  394. (while t
  395. (cond
  396. ((not prev-p) (throw 'exit (1+ (point-at-eol))))
  397. ((= limit prev-p) (throw 'exit limit))
  398. (t
  399. (goto-char prev-p)
  400. (beginning-of-line 0)
  401. (setq prev-p (org-list-in-item-p-with-indent limit))))))))))
  402. (defun org-list-bottom-point-with-indent (limit)
  403. "Return point just before list ending or nil if not in a list.
  404. List ending is determined by the indentation of text. See
  405. `org-list-ending-method' for more information."
  406. (save-excursion
  407. (let* ((ind (save-excursion
  408. (ignore-errors (org-beginning-of-item))
  409. (org-get-indentation)))
  410. (end-item (lambda ()
  411. (save-excursion
  412. (catch 'end
  413. (while t
  414. (beginning-of-line 2)
  415. (cond
  416. ((>= (point) limit) (throw 'end limit))
  417. ((or (looking-at "^[ \t]*$")
  418. (org-list-maybe-skip-block
  419. #'re-search-forward limit)
  420. (> (org-get-indentation) ind)))
  421. (t (throw 'end (point-at-bol))))))))))
  422. (and (org-in-item-p)
  423. (catch 'exit
  424. (while t
  425. (goto-char (funcall end-item))
  426. (if (looking-at org-item-beginning-re)
  427. (setq ind (org-get-indentation))
  428. (skip-chars-backward " \r\t\n")
  429. (throw 'exit (1+ (point-at-eol))))))))))
  430. (defun org-list-at-regexp-after-bullet-p (regexp)
  431. "Is point at a list item with REGEXP after bullet?"
  432. (and (org-at-item-p)
  433. (save-excursion
  434. (goto-char (match-end 0))
  435. ;; Ignore counter if any
  436. (when (looking-at "\\(?:\\[@start:[0-9]+\\][ \t]*\\)?")
  437. (goto-char (match-end 0)))
  438. (looking-at regexp))))
  439. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  440. "Return point at the beginning of next item at the same level.
  441. Search items using function SEARCH-FUN, from POS to LIMIT. It
  442. uses PRE-MOVE before search. Return nil if no item was found."
  443. (save-excursion
  444. (goto-char pos)
  445. (let ((ind (progn
  446. (org-beginning-of-item)
  447. (org-get-indentation)))
  448. (start (point-at-bol)))
  449. ;; We don't want to match the current line.
  450. (funcall pre-move)
  451. ;; Skip any sublist on the way
  452. (while (and (funcall search-fun org-item-beginning-re limit t)
  453. (> (org-get-indentation) ind)))
  454. (when (and (/= (point-at-bol) start) ; Have we moved ?
  455. (= (org-get-indentation) ind))
  456. (point-at-bol)))))
  457. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  458. "Insert a new list item at POS.
  459. If POS is before first character after bullet of the item, the
  460. new item will be created before the current one.
  461. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  462. after the bullet. Cursor will be after this text once the
  463. function ends."
  464. (goto-char pos)
  465. ;; Is point in a special block?
  466. (when (org-in-regexps-block-p
  467. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  468. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  469. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  470. ;; Rule in `org-list-automatic-rules' forbids insertion.
  471. (error "Cannot insert item inside a block.")
  472. ;; Else, move before it prior to add a new item.
  473. (end-of-line)
  474. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  475. (end-of-line 0)))
  476. (let* ((true-pos (point))
  477. (bullet (and (org-beginning-of-item)
  478. (org-list-bullet-string (org-get-bullet))))
  479. (ind (org-get-indentation))
  480. (before-p (progn
  481. ;; Description item: text starts after colons.
  482. (or (org-at-item-description-p)
  483. ;; At a checkbox: text starts after it.
  484. (org-at-item-checkbox-p)
  485. ;; Otherwise, text starts after bullet.
  486. (org-at-item-p))
  487. (<= true-pos (match-end 0))))
  488. ;; Guess number of blank lines used to separate items.
  489. (blank-lines-nb
  490. (let ((insert-blank-p
  491. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  492. usr-blank)
  493. (cond
  494. ;; Trivial cases where there should be none.
  495. ((or (and (not (eq org-list-ending-method 'indent))
  496. org-empty-line-terminates-plain-lists)
  497. (not insert-blank-p)) 0)
  498. ;; When `org-blank-before-new-entry' says so, it is 1.
  499. ((eq insert-blank-p t) 1)
  500. ;; plain-list-item is 'auto. Count blank lines separating
  501. ;; neighbours items in list.
  502. (t (let ((next-p (org-get-next-item (point)
  503. (org-list-bottom-point))))
  504. (cond
  505. ;; Is there a next item?
  506. (next-p (goto-char next-p)
  507. (org-back-over-empty-lines))
  508. ;; Is there a previous item?
  509. ((not (org-list-first-item-p)) (org-back-over-empty-lines))
  510. ;; User inserted blank lines, trust him
  511. ((and (> true-pos (org-end-of-item-before-blank))
  512. (> (save-excursion
  513. (goto-char true-pos)
  514. (skip-chars-backward " \t")
  515. (setq usr-blank (org-back-over-empty-lines))) 0))
  516. usr-blank)
  517. ;; Item alone: count lines separating it from parent, if any
  518. ((/= (org-list-top-point) (point-at-bol))
  519. (org-back-over-empty-lines))
  520. ;; Are there blank lines inside the item ?
  521. ((save-excursion
  522. (org-search-forward-unenclosed
  523. "^[ \t]*$" (org-end-of-item-before-blank) t)) 1)
  524. ;; No parent: no blank line.
  525. (t 0)))))))
  526. (insert-fun
  527. (lambda (text)
  528. ;; insert bullet above item in order to avoid bothering
  529. ;; with possible blank lines ending last item.
  530. (org-beginning-of-item)
  531. (indent-to-column ind)
  532. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  533. ;; Stay between after-bullet and before text.
  534. (save-excursion
  535. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  536. (unless before-p (org-move-item-down))
  537. (when checkbox (org-update-checkbox-count-maybe)))))
  538. (goto-char true-pos)
  539. (cond
  540. (before-p (funcall insert-fun nil)
  541. ;; Not taking advantage of renumbering while moving
  542. ;; down. Need to call it directly.
  543. (org-list-repair) t)
  544. ;; Can't split item: insert bullet at the end of item.
  545. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  546. (funcall insert-fun nil) t)
  547. ;; else, insert a new bullet along with everything from point
  548. ;; down to last non-blank line of item.
  549. (t
  550. (delete-horizontal-space)
  551. ;; Get pos again in case previous command modified line.
  552. (let* ((pos (point))
  553. (end-before-blank (org-end-of-item-before-blank))
  554. (after-text
  555. (when (< pos end-before-blank)
  556. (prog1
  557. (delete-and-extract-region pos end-before-blank)
  558. ;; delete any blank line at and before point.
  559. (beginning-of-line)
  560. (while (looking-at "^[ \t]*$")
  561. (delete-region (point-at-bol) (1+ (point-at-eol)))
  562. (beginning-of-line 0))))))
  563. (funcall insert-fun after-text) t)))))
  564. ;;; Predicates
  565. (defun org-in-item-p ()
  566. "Is the cursor inside a plain list?
  567. This checks `org-list-ending-method'."
  568. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  569. (let ((bound (or (save-excursion (outline-previous-heading))
  570. (point-min))))
  571. (cond
  572. ((eq org-list-ending-method 'indent)
  573. (org-list-in-item-p-with-indent bound))
  574. ((eq org-list-ending-method 'both)
  575. (and (org-list-in-item-p-with-indent bound)
  576. (org-list-in-item-p-with-regexp bound)))
  577. (t (org-list-in-item-p-with-regexp bound))))))
  578. (defun org-list-first-item-p ()
  579. "Is this item the first item in a plain list?
  580. Assume point is at an item."
  581. (save-excursion
  582. (beginning-of-line)
  583. (let ((ind (org-get-indentation)))
  584. (or (not (org-search-backward-unenclosed
  585. org-item-beginning-re (org-list-top-point) t))
  586. (< (org-get-indentation) ind)))))
  587. (defun org-at-item-p ()
  588. "Is point in a line starting a hand-formatted item?"
  589. (save-excursion
  590. (beginning-of-line) (looking-at org-item-beginning-re)))
  591. (defun org-at-item-bullet-p ()
  592. "Is point at the bullet of a plain list item?"
  593. (and (org-at-item-p)
  594. (not (member (char-after) '(?\ ?\t)))
  595. (< (point) (match-end 0))))
  596. (defun org-at-item-timer-p ()
  597. "Is point at a line starting a plain list item with a timer?"
  598. (org-list-at-regexp-after-bullet-p
  599. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  600. (defun org-at-item-description-p ()
  601. "Is point at a description list item?"
  602. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  603. (defun org-at-item-checkbox-p ()
  604. "Is point at a line starting a plain-list item with a checklet?"
  605. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  606. (defun org-checkbox-blocked-p ()
  607. "Is the current checkbox blocked from for being checked now?
  608. A checkbox is blocked if all of the following conditions are fulfilled:
  609. 1. The checkbox is not checked already.
  610. 2. The current entry has the ORDERED property set.
  611. 3. There is an unchecked checkbox in this entry before the current line."
  612. (catch 'exit
  613. (save-match-data
  614. (save-excursion
  615. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  616. (when (equal (match-string 1) "[X]")
  617. ;; the box is already checked!
  618. (throw 'exit nil))
  619. (let ((end (point-at-bol)))
  620. (condition-case nil (org-back-to-heading t)
  621. (error (throw 'exit nil)))
  622. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  623. (when (org-search-forward-unenclosed
  624. "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@start:[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
  625. (org-current-line)))))))
  626. ;;; Navigate
  627. (defun org-list-top-point ()
  628. (let ((limit (or (save-excursion (outline-previous-heading))
  629. (point-min))))
  630. (cond
  631. ((eq org-list-ending-method 'indent)
  632. (org-list-top-point-with-indent limit))
  633. ((eq org-list-ending-method 'both)
  634. (max (org-list-top-point-with-regexp limit)
  635. (org-list-top-point-with-indent limit)))
  636. (t (org-list-top-point-with-regexp limit)))))
  637. (defun org-list-bottom-point ()
  638. (let ((limit (or (save-excursion
  639. (and (let ((outline-regexp org-outline-regexp))
  640. ;; Use default regexp because folding
  641. ;; changes OUTLINE-REGEXP.
  642. (outline-next-heading))
  643. (skip-chars-backward " \r\t\n")
  644. (1+ (point-at-eol))))
  645. (point-max))))
  646. (cond
  647. ((eq org-list-ending-method 'indent)
  648. (org-list-bottom-point-with-indent limit))
  649. ((eq org-list-ending-method 'both)
  650. (min (org-list-bottom-point-with-regexp limit)
  651. (org-list-bottom-point-with-indent limit)))
  652. (t (org-list-bottom-point-with-regexp limit)))))
  653. (defun org-beginning-of-item ()
  654. "Go to the beginning of the current hand-formatted item.
  655. If the cursor is not in an item, throw an error. Return point."
  656. (interactive)
  657. (if (not (org-in-item-p))
  658. (error "Not in an item")
  659. ;; Possibly match the current line.
  660. (end-of-line)
  661. (org-search-backward-unenclosed org-item-beginning-re nil t)
  662. (goto-char (point-at-bol))))
  663. (defun org-end-of-item ()
  664. "Go to the end of the current hand-formatted item.
  665. If the cursor is not in an item, throw an error. Return point."
  666. (interactive)
  667. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  668. (if next-p (goto-char next-p) (org-end-of-item-list))))
  669. (defun org-end-of-item-or-at-child ()
  670. "Move to the end of the item text, stops before the first child if any."
  671. (let ((limit (org-list-bottom-point)))
  672. (end-of-line)
  673. (goto-char
  674. (if (org-search-forward-unenclosed org-item-beginning-re limit t)
  675. (point-at-bol)
  676. limit))))
  677. (defun org-end-of-item-before-blank ()
  678. "Return point at end of item, before any blank line.
  679. Point returned is at eol."
  680. (save-excursion
  681. (org-end-of-item)
  682. (skip-chars-backward " \r\t\n")
  683. (point-at-eol)))
  684. (defun org-get-next-item (pos limit)
  685. "Get the point of the next item at the same level as POS.
  686. Stop searching at LIMIT. Return nil if no item is found. This
  687. function does not move point."
  688. (org-list-get-item-same-level
  689. #'org-search-forward-unenclosed pos limit #'end-of-line))
  690. (defun org-get-previous-item (pos limit)
  691. "Get the point of the previous item at the same level as POS.
  692. Stop searching at LIMIT. Return nil if no item is found. This
  693. function does not move point."
  694. (org-list-get-item-same-level
  695. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  696. (defun org-next-item ()
  697. "Move to the beginning of the next item.
  698. Item is at the same level in the current plain list. Error if not
  699. in a plain list, or if this is the last item in the list."
  700. (interactive)
  701. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  702. (if next-p (goto-char next-p) (error "On last item"))))
  703. (defun org-previous-item ()
  704. "Move to the beginning of the previous item.
  705. Item is at the same level in the current plain list. Error if not
  706. in a plain list, or if this is the first item in the list."
  707. (interactive)
  708. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  709. (if prev-p (goto-char prev-p) (error "On first item"))))
  710. (defun org-beginning-of-item-list ()
  711. "Go to the beginning item of the current list or sublist.
  712. Return point."
  713. (interactive)
  714. (let ((limit (org-list-top-point))
  715. prev-p)
  716. (while (setq prev-p (org-get-previous-item (point) limit))
  717. (goto-char prev-p))
  718. (goto-char (point-at-bol))))
  719. (defun org-end-of-item-list ()
  720. "Go to the end of the current list or sublist.
  721. Return point."
  722. (interactive)
  723. (org-beginning-of-item)
  724. (let ((limit (org-list-bottom-point))
  725. (ind (org-get-indentation)))
  726. (while (and (/= (point) limit)
  727. (>= (org-get-indentation) ind))
  728. (org-search-forward-unenclosed org-item-beginning-re limit 'move))
  729. (if (= (point) limit) limit (goto-char (point-at-bol)))))
  730. ;;; Manipulate
  731. (defun org-list-exchange-items (beg-A beg-B)
  732. "Swap item starting at BEG-A with item starting at BEG-B.
  733. Blank lines at the end of items are left in place. Assumes
  734. BEG-A is lesser than BEG-B."
  735. (save-excursion
  736. (let* ((end-of-item-no-blank (lambda (pos)
  737. (goto-char pos)
  738. (goto-char (org-end-of-item-before-blank))))
  739. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  740. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  741. (body-A (buffer-substring beg-A end-A-no-blank))
  742. (body-B (buffer-substring beg-B end-B-no-blank))
  743. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  744. (goto-char beg-A)
  745. (delete-region beg-A end-B-no-blank)
  746. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  747. (defun org-move-item-down ()
  748. "Move the plain list item at point down, i.e. swap with following item.
  749. Subitems (items with larger indentation) are considered part of the item,
  750. so this really moves item trees."
  751. (interactive)
  752. (let ((pos (point))
  753. (col (current-column))
  754. (actual-item (org-beginning-of-item))
  755. (next-item (org-get-next-item (point) (org-list-bottom-point))))
  756. (if (not next-item)
  757. (progn
  758. (goto-char pos)
  759. (error "Cannot move this item further down"))
  760. (org-list-exchange-items actual-item next-item)
  761. (org-list-repair)
  762. (org-next-item)
  763. (move-to-column col))))
  764. (defun org-move-item-up ()
  765. "Move the plain list item at point up, i.e. swap with previous item.
  766. Subitems (items with larger indentation) are considered part of the item,
  767. so this really moves item trees."
  768. (interactive)
  769. (let ((pos (point))
  770. (col (current-column))
  771. (actual-item (org-beginning-of-item))
  772. (prev-item (org-get-previous-item (point) (org-list-top-point))))
  773. (if (not prev-item)
  774. (progn
  775. (goto-char pos)
  776. (error "Cannot move this item further up"))
  777. (org-list-exchange-items prev-item actual-item)
  778. (org-list-repair)
  779. (move-to-column col))))
  780. (defun org-insert-item (&optional checkbox)
  781. "Insert a new item at the current level.
  782. If cursor is before first character after bullet of the item, the
  783. new item will be created before the current one. Return t when
  784. things worked, nil when we are not in an item, or item is
  785. invisible."
  786. (unless (or (not (org-in-item-p))
  787. (org-invisible-p))
  788. (if (save-excursion
  789. (org-beginning-of-item)
  790. (org-at-item-timer-p))
  791. ;; Timer list: delegate to `org-timer-item'.
  792. (progn (org-timer-item) t)
  793. ;; if we're in a description list, ask for the new term.
  794. (let ((desc-text (when (save-excursion
  795. (and (org-beginning-of-item)
  796. (org-at-item-description-p)))
  797. (concat (read-string "Term: ") " :: "))))
  798. ;; Don't insert a checkbox if checkbox rule is applied and it
  799. ;; is a description item.
  800. (org-list-insert-item-generic
  801. (point) (and checkbox
  802. (or (not desc-text)
  803. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  804. desc-text)))))
  805. ;;; Structures
  806. ;; The idea behind structures is to avoid moving back and forth in the
  807. ;; buffer on costly operations like indenting or fixing bullets.
  808. ;; It achieves this by taking a snapshot of an interesting part of the
  809. ;; list, in the shape of an alist, with `org-list-struct'.
  810. ;; It then proceeds to changes directly on the alist. When those are
  811. ;; done, `org-list-struct-apply-struct' applies the changes in the
  812. ;; buffer.
  813. (defun org-list-struct-assoc-at-point ()
  814. "Return the structure association at point.
  815. It is a cons-cell whose key is point and values are indentation,
  816. bullet string and bullet counter, if any."
  817. (save-excursion
  818. (beginning-of-line)
  819. (list (point-at-bol)
  820. (org-get-indentation)
  821. (progn
  822. (looking-at "^[ \t]*\\([-+*0-9.)]+[ \t]+\\)")
  823. (match-string 1))
  824. (progn
  825. (goto-char (match-end 0))
  826. (and (looking-at "\\[@start:\\([0-9]+\\)\\]")
  827. (match-string 1))))))
  828. (defun org-list-struct (begin end &optional outdent)
  829. "Return the structure containing the list between BEGIN and END.
  830. A structure is an alist where key is point of item and values
  831. are, in that order, indentation, bullet string and value of
  832. counter, if any. A structure contains every list and sublist that
  833. has items between BEGIN and END along with their common ancestor.
  834. If no such ancestor can be found, the function will add a virtual
  835. ancestor at position 0.
  836. If OUTDENT is non-nil, it will also grab all of the parent list
  837. and the grand-parent. Setting OUTDENT to t is mandatory when next
  838. change is an outdent."
  839. (save-excursion
  840. (let* ((top (org-list-top-point))
  841. (bottom (org-list-bottom-point))
  842. struct
  843. (extend
  844. (lambda (struct)
  845. (let* ((ind-min (apply 'min (mapcar 'cadr struct)))
  846. (begin (caar struct))
  847. (end (caar (last struct)))
  848. pre-list post-list)
  849. (goto-char begin)
  850. ;; Find beginning of most outdented list (min list)
  851. (while (and (org-search-backward-unenclosed
  852. org-item-beginning-re top t)
  853. (>= (org-get-indentation) ind-min))
  854. (setq pre-list (cons (org-list-struct-assoc-at-point)
  855. pre-list)))
  856. ;; Now get the parent. If none, add a virtual ancestor
  857. (if (< (org-get-indentation) ind-min)
  858. (setq pre-list (cons (org-list-struct-assoc-at-point)
  859. pre-list))
  860. (setq pre-list (cons (list 0 (org-get-indentation) "" nil)
  861. pre-list)))
  862. ;; Find end of min list
  863. (goto-char end)
  864. (end-of-line)
  865. (while (and (org-search-forward-unenclosed
  866. org-item-beginning-re bottom 'move)
  867. (>= (org-get-indentation) ind-min))
  868. (setq post-list (cons (org-list-struct-assoc-at-point)
  869. post-list)))
  870. ;; Is list is malformed? If some items are less
  871. ;; indented that top-item, add them anyhow.
  872. (when (and (= (caar pre-list) 0) (org-at-item-p))
  873. (setq post-list (cons (org-list-struct-assoc-at-point)
  874. post-list))
  875. (while (org-search-forward-unenclosed
  876. org-item-beginning-re bottom t)
  877. (setq post-list (cons (org-list-struct-assoc-at-point)
  878. post-list))))
  879. (append pre-list struct (reverse post-list))))))
  880. ;; Here we start: first get the core zone...
  881. (goto-char end)
  882. (while (org-search-backward-unenclosed org-item-beginning-re begin t)
  883. (setq struct (cons (org-list-struct-assoc-at-point) struct)))
  884. ;; ... then, extend it to make it a structure...
  885. (let ((extended (funcall extend struct)))
  886. ;; ... twice when OUTDENT is non-nil and struct still can be
  887. ;; extended
  888. (if (and outdent (> (caar extended) 0))
  889. (funcall extend extended)
  890. extended)))))
  891. (defun org-list-struct-origins (struct)
  892. "Return an alist where key is item's position and value parent's."
  893. (let* ((struct-rev (reverse struct))
  894. (acc (list (cons (nth 1 (car struct)) 0)))
  895. (prev-item (lambda (item)
  896. (car (nth 1 (member (assq item struct) struct-rev)))))
  897. (get-origins
  898. (lambda (item)
  899. (let* ((item-pos (car item))
  900. (ind (nth 1 item))
  901. (prev-ind (caar acc)))
  902. (cond
  903. ;; List closing.
  904. ((> prev-ind ind)
  905. (let ((current-origin (or (member (assq ind acc) acc)
  906. ;; needed if top-point is
  907. ;; not the most outdented
  908. (last acc))))
  909. (setq acc current-origin)
  910. (cons item-pos (cdar acc))))
  911. ;; New list
  912. ((< prev-ind ind)
  913. (let ((origin (funcall prev-item item-pos)))
  914. (setq acc (cons (cons ind origin) acc))
  915. (cons item-pos origin)))
  916. ;; Current list going on
  917. (t (cons item-pos (cdar acc))))))))
  918. (cons '(0 . 0) (mapcar get-origins (cdr struct)))))
  919. (defun org-list-struct-get-parent (item struct origins)
  920. "Return parent association of ITEM in STRUCT or nil."
  921. (let* ((parent-pos (cdr (assq (car item) origins))))
  922. (when (> parent-pos 0) (assq parent-pos struct))))
  923. (defun org-list-struct-get-child (item struct)
  924. "Return child association of ITEM in STRUCT or nil."
  925. (let ((ind (nth 1 item))
  926. (next-item (cadr (member item struct))))
  927. (when (and next-item (> (nth 1 next-item) ind)) next-item)))
  928. (defun org-list-struct-fix-bul (struct origins)
  929. "Verify and correct bullets for every association in STRUCT.
  930. This function modifies STRUCT."
  931. (let* (acc
  932. (init-bul (lambda (item)
  933. (let ((counter (nth 3 item))
  934. (bullet (org-list-bullet-string (nth 2 item))))
  935. (cond
  936. ((and (string-match "[0-9]+" bullet) counter)
  937. (replace-match counter nil nil bullet))
  938. ((string-match "[0-9]+" bullet)
  939. (replace-match "1" nil nil bullet))
  940. (t bullet)))))
  941. (set-bul (lambda (item bullet)
  942. (setcdr item (list (nth 1 item) bullet (nth 3 item)))))
  943. (get-bul (lambda (item bullet)
  944. (let* ((counter (nth 3 item)))
  945. (if (and counter (string-match "[0-9]+" bullet))
  946. (replace-match counter nil nil bullet)
  947. bullet))))
  948. (fix-bul
  949. (lambda (item) struct
  950. (let* ((parent (cdr (assq (car item) origins)))
  951. (orig-ref (assq parent acc)))
  952. (if orig-ref
  953. ;; Continuing previous list
  954. (let* ((prev-bul (cdr orig-ref))
  955. (new-bul (funcall get-bul item prev-bul)))
  956. (setcdr orig-ref (org-list-inc-bullet-maybe new-bul))
  957. (funcall set-bul item new-bul))
  958. ;; A new list is starting
  959. (let ((new-bul (funcall init-bul item)))
  960. (funcall set-bul item new-bul)
  961. (setq acc (cons (cons parent
  962. (org-list-inc-bullet-maybe new-bul))
  963. acc))))))))
  964. (mapc fix-bul (cdr struct))))
  965. (defun org-list-struct-fix-ind (struct origins)
  966. "Verify and correct indentation for every association in STRUCT.
  967. This function modifies STRUCT."
  968. (let* ((headless (cdr struct))
  969. (ancestor (car struct))
  970. (top-ind (+ (nth 1 ancestor) (length (nth 2 ancestor))))
  971. (new-ind
  972. (lambda (item)
  973. (let* ((parent (org-list-struct-get-parent item headless origins)))
  974. (if parent
  975. ;; Indent like parent + length of parent's bullet
  976. (setcdr item (cons (+ (length (nth 2 parent)) (nth 1 parent))
  977. (cddr item)))
  978. ;; If no parent, indent like top-point
  979. (setcdr item (cons top-ind (cddr item))))))))
  980. (mapc new-ind headless)))
  981. (defun org-list-struct-fix-struct (struct origins)
  982. "Return STRUCT with correct bullets and indentation.
  983. Only elements of STRUCT that have changed are returned."
  984. (let ((old (copy-alist struct)))
  985. (org-list-struct-fix-bul struct origins)
  986. (org-list-struct-fix-ind struct origins)
  987. (delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
  988. (defun org-list-struct-outdent (start end origins)
  989. "Outdent items in ORIGINS between BEGIN and END.
  990. BEGIN is included and END excluded."
  991. (let* (acc
  992. (out (lambda (cell)
  993. (let* ((item (car cell))
  994. (parent (cdr cell)))
  995. (cond
  996. ;; Item not yet in zone: keep association
  997. ((< item start) cell)
  998. ;; Item out of zone: follow associations in acc
  999. ((>= item end)
  1000. (let ((convert (assq parent acc)))
  1001. (if convert (cons item (cdr convert)) cell)))
  1002. ;; Item has no parent: error
  1003. ((<= parent 0)
  1004. (error "Cannot outdent top-level items"))
  1005. ;; Parent is outdented: keep association
  1006. ((>= parent start)
  1007. (setq acc (cons (cons parent item) acc)) cell)
  1008. (t
  1009. ;; Parent isn't outdented: reparent to grand-parent
  1010. (let ((grand-parent (cdr (assq parent origins))))
  1011. (setq acc (cons (cons parent item) acc))
  1012. (cons item grand-parent))))))))
  1013. (mapcar out origins)))
  1014. (defun org-list-struct-indent (start end origins struct)
  1015. "Indent items in ORIGINS between BEGIN and END.
  1016. BEGIN is included and END excluded.
  1017. STRUCT may be modified if `org-list-demote-modify-bullet' is
  1018. concerning bullets between START and END."
  1019. (let* (acc
  1020. (orig-rev (reverse origins))
  1021. (get-prev-item
  1022. (lambda (cell parent)
  1023. (car (rassq parent (cdr (memq cell orig-rev))))))
  1024. (set-assoc
  1025. (lambda (cell)
  1026. (setq acc (cons cell acc)) cell))
  1027. (change-bullet-maybe
  1028. (lambda (item)
  1029. (let* ((full-item (assq item struct))
  1030. (item-bul (org-trim (nth 2 full-item)))
  1031. (new-bul-p (cdr (assoc item-bul org-list-demote-modify-bullet))))
  1032. (when new-bul-p
  1033. ;; new bullet is stored without space to ensure item
  1034. ;; will be modified
  1035. (setcdr full-item
  1036. (list (nth 1 full-item)
  1037. new-bul-p
  1038. (nth 3 full-item)))))))
  1039. (ind
  1040. (lambda (cell)
  1041. (let* ((item (car cell))
  1042. (parent (cdr cell)))
  1043. (cond
  1044. ;; Item not yet in zone: keep association
  1045. ((< item start) cell)
  1046. ((>= item end)
  1047. ;; Item out of zone: follow associations in acc
  1048. (let ((convert (assq parent acc)))
  1049. (if convert (cons item (cdr convert)) cell)))
  1050. (t
  1051. ;; Item is in zone...
  1052. (let ((prev (funcall get-prev-item cell parent)))
  1053. ;; Check if bullet needs to be changed
  1054. (funcall change-bullet-maybe item)
  1055. (cond
  1056. ;; First item indented but not parent: error
  1057. ((and (or (not prev) (= prev 0)) (< parent start))
  1058. (error "Cannot indent the first item of a list"))
  1059. ;; First item and parent indented: keep same parent
  1060. ((or (not prev) (= prev 0))
  1061. (funcall set-assoc cell))
  1062. ;; Previous item not indented: reparent to it
  1063. ((< prev start)
  1064. (funcall set-assoc (cons item prev)))
  1065. ;; Previous item indented: reparent like it
  1066. (t
  1067. (funcall set-assoc (cons item
  1068. (cdr (assq prev acc)))))))))))))
  1069. (mapcar ind origins)))
  1070. (defun org-list-struct-apply-struct (struct)
  1071. "Apply modifications to list so it mirrors STRUCT.
  1072. Initial position is restored after the changes."
  1073. (let* ((pos (copy-marker (point)))
  1074. (ancestor (caar struct))
  1075. (modify
  1076. (lambda (item)
  1077. (goto-char (car item))
  1078. (let* ((new-ind (nth 1 item))
  1079. (new-bul (org-list-bullet-string (nth 2 item)))
  1080. (old-ind (org-get-indentation))
  1081. (old-bul (progn
  1082. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1083. (match-string 1)))
  1084. (old-body-ind (+ (length old-bul) old-ind))
  1085. (new-body-ind (+ (length new-bul) new-ind)))
  1086. ;; 1. Shift item's body
  1087. (unless (= old-body-ind new-body-ind)
  1088. (org-shift-item-indentation (- new-body-ind old-body-ind)))
  1089. ;; 2. Replace bullet
  1090. (unless (equal new-bul old-bul)
  1091. (save-excursion
  1092. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1093. (replace-match new-bul nil nil nil 1)))
  1094. ;; 3. Indent item to appropriate column
  1095. (unless (= new-ind old-ind)
  1096. (delete-region (point-at-bol)
  1097. (progn
  1098. (skip-chars-forward " \t")
  1099. (point)))
  1100. (indent-to new-ind)))))
  1101. ;; Remove ancestor if it is left.
  1102. (struct-to-apply (if (or (not ancestor) (= 0 ancestor))
  1103. (cdr struct)
  1104. struct)))
  1105. ;; Apply changes from bottom to top
  1106. (mapc modify (nreverse struct-to-apply))
  1107. (goto-char pos)))
  1108. ;;; Indentation
  1109. (defun org-get-string-indentation (s)
  1110. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  1111. (let ((n -1) (i 0) (w tab-width) c)
  1112. (catch 'exit
  1113. (while (< (setq n (1+ n)) (length s))
  1114. (setq c (aref s n))
  1115. (cond ((= c ?\ ) (setq i (1+ i)))
  1116. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  1117. (t (throw 'exit t)))))
  1118. i))
  1119. (defun org-shift-item-indentation (delta)
  1120. "Shift the indentation in current item by DELTA.
  1121. Sub-items are not moved."
  1122. (save-excursion
  1123. (let ((beg (point-at-bol))
  1124. (end (org-end-of-item-or-at-child)))
  1125. (beginning-of-line (unless (eolp) 0))
  1126. (while (> (point) beg)
  1127. (when (looking-at "[ \t]*\\S-")
  1128. ;; this is not an empty line
  1129. (let ((i (org-get-indentation)))
  1130. (when (and (> i 0) (> (+ i delta) 0))
  1131. (indent-line-to (+ i delta)))))
  1132. (beginning-of-line 0)))))
  1133. (defun org-outdent-item (arg)
  1134. "Outdent a local list item, but not its children."
  1135. (interactive "p")
  1136. (org-indent-item-tree (- arg) t))
  1137. (defun org-indent-item (arg)
  1138. "Indent a local list item, but not its children."
  1139. (interactive "p")
  1140. (org-indent-item-tree arg t))
  1141. (defun org-outdent-item-tree (arg &optional no-subtree)
  1142. "Outdent a local list item including its children.
  1143. If NO-SUBTREE is set, only outdent the item itself, not its children."
  1144. (interactive "p")
  1145. (org-indent-item-tree (- arg) no-subtree))
  1146. (defvar org-last-indent-begin-marker (make-marker))
  1147. (defvar org-last-indent-end-marker (make-marker))
  1148. (defun org-indent-item-tree (arg &optional no-subtree)
  1149. "Indent a local list item including its children.
  1150. If NO-SUBTREE is set, only indent the item itself, not its
  1151. children. Return t if successful."
  1152. (interactive "p")
  1153. (unless (org-at-item-p)
  1154. (error "Not on an item"))
  1155. ;; Determine begin and end points of zone to indent. If moving by
  1156. ;; subtrees, ensure we don't drag additional items on subsequent
  1157. ;; moves.
  1158. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  1159. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  1160. (if (org-region-active-p)
  1161. (progn
  1162. (set-marker org-last-indent-begin-marker (region-beginning))
  1163. (set-marker org-last-indent-end-marker (region-end)))
  1164. (set-marker org-last-indent-begin-marker
  1165. (save-excursion (org-beginning-of-item)))
  1166. (set-marker org-last-indent-end-marker
  1167. (save-excursion
  1168. (if no-subtree
  1169. (org-end-of-item-or-at-child)
  1170. (org-end-of-item))))))
  1171. ;; Get everything ready
  1172. (let* ((beg (marker-position org-last-indent-begin-marker))
  1173. (end (marker-position org-last-indent-end-marker))
  1174. (struct (org-list-struct beg end (< arg 0)))
  1175. (origins (org-list-struct-origins struct))
  1176. (beg-item (assq beg struct))
  1177. (end-item (save-excursion
  1178. (goto-char end)
  1179. (skip-chars-backward " \r\t\n")
  1180. (org-beginning-of-item)
  1181. (org-list-struct-assoc-at-point)))
  1182. (top (org-list-top-point)))
  1183. (cond
  1184. ;; Special case: moving top-item with indent rule
  1185. ((and (= top beg) (cdr (assq 'indent org-list-automatic-rules)))
  1186. (let* ((level-skip (org-level-increment))
  1187. (offset (if (< arg 0) (- level-skip) level-skip))
  1188. (top-ind (nth 1 beg-item)))
  1189. (if (< (+ top-ind offset) 0)
  1190. (error "Cannot outdent beyond margin")
  1191. ;; Change bullet if necessary
  1192. (when (and (= (+ top-ind offset) 0)
  1193. (string-match "*" (nth 2 beg-item)))
  1194. (setcdr beg-item (list (nth 1 beg-item)
  1195. (org-list-bullet-string "-"))))
  1196. ;; Shift ancestor
  1197. (let ((anc (car struct)))
  1198. (setcdr anc (list (+ (nth 1 anc) offset) "" nil)))
  1199. (org-list-struct-fix-struct struct origins)
  1200. (org-list-struct-apply-struct struct))))
  1201. ;; Forbidden move
  1202. ((and (< arg 0)
  1203. (or (and no-subtree
  1204. (not (org-region-active-p))
  1205. (org-list-struct-get-child beg-item struct))
  1206. (org-list-struct-get-child end-item struct)))
  1207. (error "Cannot outdent an item without its children"))
  1208. ;; Normal shifting
  1209. (t
  1210. (let* ((shifted-ori (if (< arg 0)
  1211. (org-list-struct-outdent beg end origins)
  1212. (org-list-struct-indent beg end origins struct))))
  1213. (org-list-struct-fix-struct struct shifted-ori)
  1214. (org-list-struct-apply-struct struct)))))
  1215. ;; Return value
  1216. t)
  1217. (defvar org-tab-ind-state)
  1218. (defun org-cycle-item-indentation ()
  1219. (let ((org-adapt-indentation nil))
  1220. (when (and (or (org-at-item-description-p)
  1221. (org-at-item-checkbox-p)
  1222. (org-at-item-p))
  1223. (>= (match-end 0) (save-excursion
  1224. (org-end-of-item-or-at-child)
  1225. (skip-chars-backward " \r\t\n")
  1226. (point))))
  1227. (setq this-command 'org-cycle-item-indentation)
  1228. ;; When in the middle of the cycle, try to outdent first. If it
  1229. ;; fails, and point is still at initial position, indent. Else,
  1230. ;; go back to original position.
  1231. (if (eq last-command 'org-cycle-item-indentation)
  1232. (cond
  1233. ((ignore-errors (org-indent-item -1)))
  1234. ((and (= (org-get-indentation) (car org-tab-ind-state))
  1235. (ignore-errors (org-indent-item 1))))
  1236. (t (back-to-indentation)
  1237. (indent-to-column (car org-tab-ind-state))
  1238. (end-of-line)
  1239. (org-list-repair (cdr org-tab-ind-state))
  1240. ;; Break cycle
  1241. (setq this-command 'identity)))
  1242. ;; If a cycle is starting, remember indentation and bullet,
  1243. ;; then try to indent. If it fails, try to outdent.
  1244. (setq org-tab-ind-state
  1245. (cons (org-get-indentation) (org-get-bullet)))
  1246. (cond
  1247. ((ignore-errors (org-indent-item 1)))
  1248. ((ignore-errors (org-indent-item -1)))
  1249. (t (error "Cannot move item"))))
  1250. t)))
  1251. ;;; Bullets
  1252. (defun org-get-bullet ()
  1253. "Return the bullet of the item at point.
  1254. Assume cursor is at an item."
  1255. (and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1)))
  1256. (defun org-list-bullet-string (bullet)
  1257. "Return BULLET with the correct number of whitespaces.
  1258. It determines the number of whitespaces to append by looking at
  1259. `org-list-two-spaces-after-bullet-regexp'."
  1260. (save-match-data
  1261. (string-match "\\S-+\\([ \t]*\\)" bullet)
  1262. (replace-match
  1263. (save-match-data
  1264. (concat
  1265. " "
  1266. ;; Do we need to concat another white space ?
  1267. (when (and org-list-two-spaces-after-bullet-regexp
  1268. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  1269. " ")))
  1270. nil nil bullet 1)))
  1271. (defun org-list-inc-bullet-maybe (bullet)
  1272. "Increment numbered bullets."
  1273. (if (string-match "[0-9]+" bullet)
  1274. (replace-match
  1275. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1276. nil nil bullet)
  1277. bullet))
  1278. (defun org-list-repair (&optional force-bullet)
  1279. "Make sure all items are correctly indented, with the right bullet.
  1280. This function scans the list at point, along with any sublist.
  1281. If the string FORCE-BULLET is provided, ensure all items in list
  1282. share this bullet, or a logical successor in an ordered list.
  1283. Item's body is not indented, only shifted with the bullet."
  1284. (interactive)
  1285. (unless (org-at-item-p) (error "This is not a list"))
  1286. (let* ((struct (org-list-struct (point-at-bol) (point-at-eol)))
  1287. (origins (org-list-struct-origins struct))
  1288. fixed-struct)
  1289. (if force-bullet
  1290. (let ((begin (nth 1 struct)))
  1291. (setcdr begin (list (nth 1 begin)
  1292. (org-list-bullet-string force-bullet)
  1293. (nth 3 begin)))
  1294. (setq fixed-struct
  1295. (cons begin (org-list-struct-fix-struct struct origins))))
  1296. (setq fixed-struct (org-list-struct-fix-struct struct origins)))
  1297. (org-list-struct-apply-struct fixed-struct)))
  1298. ;; For backward compatibility
  1299. (defalias 'org-fix-bullet-type 'org-list-repair)
  1300. (defalias 'org-renumber-ordered-list 'org-list-repair)
  1301. (defalias 'org-maybe-renumber-ordered-list 'org-list-repair)
  1302. (defun org-cycle-list-bullet (&optional which)
  1303. "Cycle through the different itemize/enumerate bullets.
  1304. This cycle the entire list level through the sequence:
  1305. `-' -> `+' -> `*' -> `1.' -> `1)'
  1306. If WHICH is a valid string, use that as the new bullet. If WHICH
  1307. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1308. 'previous, cycle backwards."
  1309. (interactive "P")
  1310. (org-preserve-lc
  1311. (let* ((bullet (progn (org-beginning-of-item-list)
  1312. (org-get-bullet)))
  1313. (current (cond
  1314. ((string-match "\\." bullet) "1.")
  1315. ((string-match ")" bullet) "1)")
  1316. (t bullet)))
  1317. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1318. (bullet-list (append '("-" "+" )
  1319. ;; *-bullets are not allowed at column 0
  1320. (unless (and bullet-rule-p
  1321. (looking-at "\\S-")) '("*"))
  1322. ;; Description items cannot be numbered
  1323. (unless (and bullet-rule-p
  1324. (or (eq org-plain-list-ordered-item-terminator ?.)
  1325. (org-at-item-description-p))) '("1)"))
  1326. (unless (and bullet-rule-p
  1327. (or (eq org-plain-list-ordered-item-terminator ?\))
  1328. (org-at-item-description-p))) '("1."))))
  1329. (len (length bullet-list))
  1330. (item-index (- len (length (member current bullet-list))))
  1331. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1332. (new (cond
  1333. ((member which bullet-list) which)
  1334. ((numberp which) (funcall get-value which))
  1335. ((eq 'previous which) (funcall get-value (1- item-index)))
  1336. (t (funcall get-value (1+ item-index))))))
  1337. (org-list-repair new))))
  1338. ;;; Checkboxes
  1339. (defun org-toggle-checkbox (&optional toggle-presence)
  1340. "Toggle the checkbox in the current line.
  1341. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1342. double prefix, set checkbox to [-].
  1343. When there is an active region, toggle status or presence of the
  1344. checkbox in the first line, and make every item in the region
  1345. have the same status or presence, respectively.
  1346. If the cursor is in a headline, apply this to all checkbox items
  1347. in the text below the heading, taking as reference the first item
  1348. in subtree."
  1349. (interactive "P")
  1350. ;; Bounds is a list of type (beg end single-p) where single-p is t
  1351. ;; when `org-toggle-checkbox' is applied to a single item. Only
  1352. ;; toggles on single items will return errors.
  1353. (let* ((bounds
  1354. (cond
  1355. ((org-region-active-p)
  1356. (list (region-beginning) (region-end) nil))
  1357. ((org-on-heading-p)
  1358. ;; In this case, reference line is the first item in subtree
  1359. (let ((limit (save-excursion (outline-next-heading) (point))))
  1360. (save-excursion
  1361. (org-search-forward-unenclosed
  1362. org-item-beginning-re limit 'move)
  1363. (list (point) limit nil))))
  1364. ((org-at-item-p)
  1365. (list (point-at-bol) (point-at-eol) t))
  1366. (t (error "Not at an item or heading, and no active region"))))
  1367. ;; marker is needed because deleting checkboxes will change END
  1368. (end (copy-marker (nth 1 bounds)))
  1369. (single-p (nth 2 bounds))
  1370. (ref-presence (save-excursion
  1371. (goto-char (car bounds))
  1372. (org-at-item-checkbox-p)))
  1373. (ref-status (equal (match-string 1) "[X]"))
  1374. (act-on-item
  1375. (lambda (ref-pres ref-stat)
  1376. (if (equal toggle-presence '(4))
  1377. (cond
  1378. ((and ref-pres (org-at-item-checkbox-p))
  1379. (replace-match ""))
  1380. ((and (not ref-pres)
  1381. (not (org-at-item-checkbox-p))
  1382. (org-at-item-p))
  1383. (goto-char (match-end 0))
  1384. ;; Ignore counter, if any
  1385. (when (looking-at "\\(?:\\[@start:[0-9]+\\][ \t]*\\)?")
  1386. (goto-char (match-end 0)))
  1387. (let ((desc-p (and (org-at-item-description-p)
  1388. (cdr (assq 'checkbox org-list-automatic-rules)))))
  1389. (cond
  1390. ((and single-p desc-p)
  1391. (error "Cannot add a checkbox in a description list"))
  1392. ((not desc-p) (insert "[ ] "))))))
  1393. (let ((blocked (org-checkbox-blocked-p)))
  1394. (cond
  1395. ((and blocked single-p)
  1396. (error "Checkbox blocked because of unchecked box in line %d" blocked))
  1397. (blocked nil)
  1398. ((org-at-item-checkbox-p)
  1399. (replace-match
  1400. (cond ((equal toggle-presence '(16)) "[-]")
  1401. (ref-stat "[ ]")
  1402. (t "[X]"))
  1403. t t nil 1))))))))
  1404. (save-excursion
  1405. (beginning-of-line)
  1406. (while (< (point) end)
  1407. (funcall act-on-item ref-presence ref-status)
  1408. (org-search-forward-unenclosed org-item-beginning-re end 'move)))
  1409. (org-update-checkbox-count-maybe)))
  1410. (defun org-reset-checkbox-state-subtree ()
  1411. "Reset all checkboxes in an entry subtree."
  1412. (interactive "*")
  1413. (save-restriction
  1414. (save-excursion
  1415. (org-narrow-to-subtree)
  1416. (org-show-subtree)
  1417. (goto-char (point-min))
  1418. (let ((end (point-max)))
  1419. (while (< (point) end)
  1420. (when (org-at-item-checkbox-p)
  1421. (replace-match "[ ]" t t nil 1))
  1422. (beginning-of-line 2))))
  1423. (org-update-checkbox-count-maybe)))
  1424. (defvar org-checkbox-statistics-hook nil
  1425. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1426. This hook runs even if 'checkbox rules in
  1427. `org-list-automatic-rules' do not apply, so it can be used to
  1428. implement alternative ways of collecting statistics
  1429. information.")
  1430. (defun org-update-checkbox-count-maybe ()
  1431. "Update checkbox statistics unless turned off by user."
  1432. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1433. (org-update-checkbox-count))
  1434. (run-hooks 'org-checkbox-statistics-hook))
  1435. (defun org-update-checkbox-count (&optional all)
  1436. "Update the checkbox statistics in the current section.
  1437. This will find all statistic cookies like [57%] and [6/12] and update them
  1438. with the current numbers. With optional prefix argument ALL, do this for
  1439. the whole buffer."
  1440. (interactive "P")
  1441. (save-excursion
  1442. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1443. (beg (condition-case nil
  1444. (progn (org-back-to-heading) (point))
  1445. (error (point-min))))
  1446. (end (move-marker (make-marker)
  1447. (progn (outline-next-heading) (point))))
  1448. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1449. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  1450. (re-find (concat re "\\|" re-box))
  1451. beg-cookie end-cookie is-percent c-on c-off lim new
  1452. eline curr-ind next-ind continue-from startsearch
  1453. (recursive
  1454. (or (not org-hierarchical-checkbox-statistics)
  1455. (string-match "\\<recursive\\>"
  1456. (or (ignore-errors
  1457. (org-entry-get nil "COOKIE_DATA"))
  1458. ""))))
  1459. (cstat 0))
  1460. (when all
  1461. (goto-char (point-min))
  1462. (outline-next-heading)
  1463. (setq beg (point) end (point-max)))
  1464. (goto-char end)
  1465. ;; find each statistics cookie
  1466. (while (and (org-search-backward-unenclosed re-find beg t)
  1467. (not (save-match-data
  1468. (and (org-on-heading-p)
  1469. (string-match "\\<todo\\>"
  1470. (downcase
  1471. (or (org-entry-get
  1472. nil "COOKIE_DATA")
  1473. "")))))))
  1474. (setq beg-cookie (match-beginning 1)
  1475. end-cookie (match-end 1)
  1476. cstat (+ cstat (if end-cookie 1 0))
  1477. startsearch (point-at-eol)
  1478. continue-from (match-beginning 0)
  1479. is-percent (match-beginning 2)
  1480. lim (cond
  1481. ((org-on-heading-p) (outline-next-heading) (point))
  1482. ((org-at-item-p) (org-end-of-item) (point))
  1483. (t nil))
  1484. c-on 0
  1485. c-off 0)
  1486. (when lim
  1487. ;; find first checkbox for this cookie and gather
  1488. ;; statistics from all that are at this indentation level
  1489. (goto-char startsearch)
  1490. (if (org-search-forward-unenclosed re-box lim t)
  1491. (progn
  1492. (org-beginning-of-item)
  1493. (setq curr-ind (org-get-indentation))
  1494. (setq next-ind curr-ind)
  1495. (while (and (bolp) (org-at-item-p)
  1496. (if recursive
  1497. (<= curr-ind next-ind)
  1498. (= curr-ind next-ind)))
  1499. (setq eline (point-at-eol))
  1500. (if (org-search-forward-unenclosed re-box eline t)
  1501. (if (member (match-string 2) '("[ ]" "[-]"))
  1502. (setq c-off (1+ c-off))
  1503. (setq c-on (1+ c-on))))
  1504. (if (not recursive)
  1505. ;; org-get-next-item goes through list-enders
  1506. ;; with proper limit.
  1507. (goto-char (or (org-get-next-item (point) lim) lim))
  1508. (end-of-line)
  1509. (when (org-search-forward-unenclosed
  1510. org-item-beginning-re lim t)
  1511. (beginning-of-line)))
  1512. (setq next-ind (org-get-indentation)))))
  1513. (goto-char continue-from)
  1514. ;; update cookie
  1515. (when end-cookie
  1516. (setq new (if is-percent
  1517. (format "[%d%%]" (/ (* 100 c-on)
  1518. (max 1 (+ c-on c-off))))
  1519. (format "[%d/%d]" c-on (+ c-on c-off))))
  1520. (goto-char beg-cookie)
  1521. (insert new)
  1522. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1523. ;; update items checkbox if it has one
  1524. (when (org-at-item-p)
  1525. (org-beginning-of-item)
  1526. (when (and (> (+ c-on c-off) 0)
  1527. (org-search-forward-unenclosed re-box (point-at-eol) t))
  1528. (setq beg-cookie (match-beginning 2)
  1529. end-cookie (match-end 2))
  1530. (delete-region beg-cookie end-cookie)
  1531. (goto-char beg-cookie)
  1532. (cond ((= c-off 0) (insert "[X]"))
  1533. ((= c-on 0) (insert "[ ]"))
  1534. (t (insert "[-]")))
  1535. )))
  1536. (goto-char continue-from))
  1537. (when (interactive-p)
  1538. (message "Checkbox statistics updated %s (%d places)"
  1539. (if all "in entire file" "in current outline entry") cstat)))))
  1540. (defun org-get-checkbox-statistics-face ()
  1541. "Select the face for checkbox statistics.
  1542. The face will be `org-done' when all relevant boxes are checked.
  1543. Otherwise it will be `org-todo'."
  1544. (if (match-end 1)
  1545. (if (equal (match-string 1) "100%")
  1546. 'org-checkbox-statistics-done
  1547. 'org-checkbox-statistics-todo)
  1548. (if (and (> (match-end 2) (match-beginning 2))
  1549. (equal (match-string 2) (match-string 3)))
  1550. 'org-checkbox-statistics-done
  1551. 'org-checkbox-statistics-todo)))
  1552. ;;; Misc Tools
  1553. (defun org-apply-on-list (function init-value &rest args)
  1554. "Call FUNCTION for each item of a the list under point.
  1555. FUNCTION must be called with at least one argument: a return
  1556. value that will contain the value returned by the function at the
  1557. previous item, plus ARGS extra arguments. INIT-VALUE will be the
  1558. value passed to the function at the first item of the list.
  1559. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1560. will return the number of items in the current list.
  1561. Sublists of the list are skipped. Cursor is always at the
  1562. beginning of the item."
  1563. (let* ((pos (copy-marker (point)))
  1564. (end (copy-marker (org-list-bottom-point)))
  1565. (next-p (copy-marker (save-excursion (org-beginning-of-item-list))))
  1566. (value init-value))
  1567. (while (< next-p end)
  1568. (goto-char next-p)
  1569. (set-marker next-p (or (org-get-next-item (point) end) end))
  1570. (setq value (apply function value args)))
  1571. (goto-char pos)
  1572. value))
  1573. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1574. "Sort plain list items.
  1575. The cursor may be at any item of the list that should be sorted.
  1576. Sublists are not sorted. Checkboxes, if any, are ignored.
  1577. Sorting can be alphabetically, numerically, by date/time as given by
  1578. a time stamp, by a property or by priority.
  1579. The command prompts for the sorting type unless it has been given
  1580. to the function through the SORTING-TYPE argument, which needs to
  1581. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1582. meaning of each character:
  1583. n Numerically, by converting the beginning of the item to a number.
  1584. a Alphabetically. Only the first line of item is checked.
  1585. t By date/time, either the first active time stamp in the entry, if
  1586. any, or by the first inactive one. In a timer list, sort the timers.
  1587. Capital letters will reverse the sort order.
  1588. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1589. function to be called with point at the beginning of the record.
  1590. It must return either a string or a number that should serve as
  1591. the sorting key for that record.
  1592. Comparing entries ignores case by default. However, with an
  1593. optional argument WITH-CASE, the sorting considers case as well."
  1594. (interactive "P")
  1595. (let* ((case-func (if with-case 'identity 'downcase))
  1596. (start (org-beginning-of-item-list))
  1597. (end (save-excursion (org-end-of-item-list)))
  1598. (sorting-type
  1599. (progn
  1600. (message
  1601. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1602. (read-char-exclusive)))
  1603. (getkey-func (and (= (downcase sorting-type) ?f)
  1604. (org-icompleting-read "Sort using function: "
  1605. obarray 'fboundp t nil nil)
  1606. (intern getkey-func))))
  1607. (message "Sorting items...")
  1608. (save-restriction
  1609. (narrow-to-region start end)
  1610. (let* ((dcst (downcase sorting-type))
  1611. (case-fold-search nil)
  1612. (now (current-time))
  1613. (sort-func (cond
  1614. ((= dcst ?a) 'string<)
  1615. ((= dcst ?f) compare-func)
  1616. ((= dcst ?t) '<)
  1617. (t nil)))
  1618. (begin-record (lambda ()
  1619. (skip-chars-forward " \r\t\n")
  1620. (beginning-of-line)))
  1621. (end-record (lambda ()
  1622. (goto-char (org-end-of-item-before-blank))))
  1623. (value-to-sort
  1624. (lambda ()
  1625. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1626. (cond
  1627. ((= dcst ?n)
  1628. (string-to-number (buffer-substring (match-end 0)
  1629. (point-at-eol))))
  1630. ((= dcst ?a)
  1631. (buffer-substring (match-end 0) (point-at-eol)))
  1632. ((= dcst ?t)
  1633. (cond
  1634. ;; If it is a timer list, convert timer to seconds
  1635. ((org-at-item-timer-p)
  1636. (org-timer-hms-to-secs (match-string 1)))
  1637. ((or (org-search-forward-unenclosed org-ts-regexp
  1638. (point-at-eol) t)
  1639. (org-search-forward-unenclosed org-ts-regexp-both
  1640. (point-at-eol) t))
  1641. (org-time-string-to-seconds (match-string 0)))
  1642. (t (org-float-time now))))
  1643. ((= dcst ?f)
  1644. (if getkey-func
  1645. (let ((value (funcall getkey-func)))
  1646. (if (stringp value)
  1647. (funcall case-func value)
  1648. value))
  1649. (error "Invalid key function `%s'" getkey-func)))
  1650. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1651. (sort-subr (/= dcst sorting-type)
  1652. begin-record
  1653. end-record
  1654. value-to-sort
  1655. nil
  1656. sort-func)
  1657. (org-list-repair)
  1658. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1659. (message "Sorting items...done")))))
  1660. ;;; Send and receive lists
  1661. (defun org-list-parse-list (&optional delete)
  1662. "Parse the list at point and maybe DELETE it.
  1663. Return a list containing first level items as strings and
  1664. sublevels as a list of strings."
  1665. (let* ((start (goto-char (org-list-top-point)))
  1666. (end (org-list-bottom-point))
  1667. output itemsep ltype)
  1668. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1669. (save-excursion
  1670. (beginning-of-line)
  1671. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1672. ((org-at-item-description-p) 'descriptive)
  1673. (t 'unordered))))
  1674. (let* ((indent1 (org-get-indentation))
  1675. (nextitem (or (org-get-next-item (point) end) end))
  1676. (item (org-trim (buffer-substring (point)
  1677. (org-end-of-item-or-at-child))))
  1678. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1679. (item (if (string-match
  1680. "^\\(?:\\[@start:[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
  1681. item)
  1682. (replace-match (if (equal (match-string 1 item) " ")
  1683. "CBOFF"
  1684. "CBON")
  1685. t nil item 1)
  1686. item)))
  1687. (push item output)
  1688. (when (> nextindent indent1)
  1689. (save-restriction
  1690. (narrow-to-region (point) nextitem)
  1691. (push (org-list-parse-list) output)))))
  1692. (when delete
  1693. (delete-region start end)
  1694. (save-match-data
  1695. (when (and (not (eq org-list-ending-method 'indent))
  1696. (looking-at (org-list-end-re)))
  1697. (replace-match "\n"))))
  1698. (setq output (nreverse output))
  1699. (push ltype output)))
  1700. (defun org-list-make-subtree ()
  1701. "Convert the plain list at point into a subtree."
  1702. (interactive)
  1703. (if (not (org-in-item-p))
  1704. (error "Not in a list.")
  1705. (goto-char (org-list-top-point))
  1706. (let ((list (org-list-parse-list t)) nstars)
  1707. (save-excursion
  1708. (if (ignore-errors
  1709. (org-back-to-heading))
  1710. (progn (org-search-forward-unenclosed
  1711. org-complex-heading-regexp nil t)
  1712. (setq nstars (length (match-string 1))))
  1713. (setq nstars 0)))
  1714. (org-list-make-subtrees list (1+ nstars)))))
  1715. (defun org-list-make-subtrees (list level)
  1716. "Convert LIST into subtrees starting at LEVEL."
  1717. (if (symbolp (car list))
  1718. (org-list-make-subtrees (cdr list) level)
  1719. (mapcar (lambda (item)
  1720. (if (stringp item)
  1721. (insert (make-string
  1722. (if org-odd-levels-only
  1723. (1- (* 2 level)) level) ?*) " " item "\n")
  1724. (org-list-make-subtrees item (1+ level))))
  1725. list)))
  1726. (defun org-list-insert-radio-list ()
  1727. "Insert a radio list template appropriate for this major mode."
  1728. (interactive)
  1729. (let* ((e (assq major-mode org-list-radio-list-templates))
  1730. (txt (nth 1 e))
  1731. name pos)
  1732. (unless e (error "No radio list setup defined for %s" major-mode))
  1733. (setq name (read-string "List name: "))
  1734. (while (string-match "%n" txt)
  1735. (setq txt (replace-match name t t txt)))
  1736. (or (bolp) (insert "\n"))
  1737. (setq pos (point))
  1738. (insert txt)
  1739. (goto-char pos)))
  1740. (defun org-list-send-list (&optional maybe)
  1741. "Send a transformed version of this list to the receiver position.
  1742. With argument MAYBE, fail quietly if no transformation is defined for
  1743. this list."
  1744. (interactive)
  1745. (catch 'exit
  1746. (unless (org-at-item-p) (error "Not at a list"))
  1747. (save-excursion
  1748. (re-search-backward "#\\+ORGLST" nil t)
  1749. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1750. (if maybe
  1751. (throw 'exit nil)
  1752. (error "Don't know how to transform this list"))))
  1753. (let* ((name (match-string 1))
  1754. (transform (intern (match-string 2)))
  1755. (bottom-point
  1756. (save-excursion
  1757. (re-search-forward
  1758. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1759. (match-beginning 0)))
  1760. (top-point
  1761. (progn
  1762. (re-search-backward "#\\+ORGLST" nil t)
  1763. (re-search-forward org-item-beginning-re bottom-point t)
  1764. (match-beginning 0)))
  1765. (list (save-restriction
  1766. (narrow-to-region top-point bottom-point)
  1767. (org-list-parse-list)))
  1768. beg txt)
  1769. (unless (fboundp transform)
  1770. (error "No such transformation function %s" transform))
  1771. (let ((txt (funcall transform list)))
  1772. ;; Find the insertion place
  1773. (save-excursion
  1774. (goto-char (point-min))
  1775. (unless (re-search-forward
  1776. (concat "BEGIN RECEIVE ORGLST +"
  1777. name
  1778. "\\([ \t]\\|$\\)") nil t)
  1779. (error "Don't know where to insert translated list"))
  1780. (goto-char (match-beginning 0))
  1781. (beginning-of-line 2)
  1782. (setq beg (point))
  1783. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1784. (error "Cannot find end of insertion region"))
  1785. (delete-region beg (point-at-bol))
  1786. (goto-char beg)
  1787. (insert txt "\n")))
  1788. (message "List converted and installed at receiver location"))))
  1789. (defun org-list-to-generic (list params)
  1790. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1791. Valid parameters PARAMS are
  1792. :ustart String to start an unordered list
  1793. :uend String to end an unordered list
  1794. :ostart String to start an ordered list
  1795. :oend String to end an ordered list
  1796. :dstart String to start a descriptive list
  1797. :dend String to end a descriptive list
  1798. :dtstart String to start a descriptive term
  1799. :dtend String to end a descriptive term
  1800. :ddstart String to start a description
  1801. :ddend String to end a description
  1802. :splice When set to t, return only list body lines, don't wrap
  1803. them into :[u/o]start and :[u/o]end. Default is nil.
  1804. :istart String to start a list item
  1805. :iend String to end a list item
  1806. :isep String to separate items
  1807. :lsep String to separate sublists
  1808. :cboff String to insert for an unchecked checkbox
  1809. :cbon String to insert for a checked checkbox"
  1810. (interactive)
  1811. (let* ((p params) sublist
  1812. (splicep (plist-get p :splice))
  1813. (ostart (plist-get p :ostart))
  1814. (oend (plist-get p :oend))
  1815. (ustart (plist-get p :ustart))
  1816. (uend (plist-get p :uend))
  1817. (dstart (plist-get p :dstart))
  1818. (dend (plist-get p :dend))
  1819. (dtstart (plist-get p :dtstart))
  1820. (dtend (plist-get p :dtend))
  1821. (ddstart (plist-get p :ddstart))
  1822. (ddend (plist-get p :ddend))
  1823. (istart (plist-get p :istart))
  1824. (iend (plist-get p :iend))
  1825. (isep (plist-get p :isep))
  1826. (lsep (plist-get p :lsep))
  1827. (cbon (plist-get p :cbon))
  1828. (cboff (plist-get p :cboff)))
  1829. (let ((wrapper
  1830. (cond ((eq (car list) 'ordered)
  1831. (concat ostart "\n%s" oend "\n"))
  1832. ((eq (car list) 'unordered)
  1833. (concat ustart "\n%s" uend "\n"))
  1834. ((eq (car list) 'descriptive)
  1835. (concat dstart "\n%s" dend "\n"))))
  1836. rtn term defstart defend)
  1837. (while (setq sublist (pop list))
  1838. (cond ((symbolp sublist) nil)
  1839. ((stringp sublist)
  1840. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  1841. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1842. (match-string 1 sublist))))
  1843. (setq sublist (concat ddstart
  1844. (org-trim (substring sublist
  1845. (match-end 0)))
  1846. ddend)))
  1847. (if (string-match "\\[CBON\\]" sublist)
  1848. (setq sublist (replace-match cbon t t sublist)))
  1849. (if (string-match "\\[CBOFF\\]" sublist)
  1850. (setq sublist (replace-match cboff t t sublist)))
  1851. (if (string-match "\\[-\\]" sublist)
  1852. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1853. (setq rtn (concat rtn istart term sublist iend isep)))
  1854. (t (setq rtn (concat rtn ;; previous list
  1855. lsep ;; list separator
  1856. (org-list-to-generic sublist p)
  1857. lsep ;; list separator
  1858. )))))
  1859. (format wrapper rtn))))
  1860. (defun org-list-to-latex (list &optional params)
  1861. "Convert LIST into a LaTeX list.
  1862. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1863. with overruling parameters for `org-list-to-generic'."
  1864. (org-list-to-generic
  1865. list
  1866. (org-combine-plists
  1867. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1868. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1869. :dstart "\\begin{description}" :dend "\\end{description}"
  1870. :dtstart "[" :dtend "]"
  1871. :ddstart "" :ddend ""
  1872. :istart "\\item " :iend ""
  1873. :isep "\n" :lsep "\n"
  1874. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1875. params)))
  1876. (defun org-list-to-html (list &optional params)
  1877. "Convert LIST into a HTML list.
  1878. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1879. with overruling parameters for `org-list-to-generic'."
  1880. (org-list-to-generic
  1881. list
  1882. (org-combine-plists
  1883. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1884. :ustart "<ul>" :uend "</ul>"
  1885. :dstart "<dl>" :dend "</dl>"
  1886. :dtstart "<dt>" :dtend "</dt>"
  1887. :ddstart "<dd>" :ddend "</dd>"
  1888. :istart "<li>" :iend "</li>"
  1889. :isep "\n" :lsep "\n"
  1890. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1891. params)))
  1892. (defun org-list-to-texinfo (list &optional params)
  1893. "Convert LIST into a Texinfo list.
  1894. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1895. with overruling parameters for `org-list-to-generic'."
  1896. (org-list-to-generic
  1897. list
  1898. (org-combine-plists
  1899. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1900. :ustart "@enumerate" :uend "@end enumerate"
  1901. :dstart "@table" :dend "@end table"
  1902. :dtstart "@item " :dtend "\n"
  1903. :ddstart "" :ddend ""
  1904. :istart "@item\n" :iend ""
  1905. :isep "\n" :lsep "\n"
  1906. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1907. params)))
  1908. (provide 'org-list)
  1909. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1910. ;;; org-list.el ends here