org-list.el 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295
  1. ;;; org-list.el --- Plain lists for Org-mode
  2. ;;
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Bastien Guerry <bzg AT gnu DOT org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file contains the code dealing with plain lists in Org-mode.
  26. ;; The core concept behind lists is their structure. A structure is
  27. ;; a snapshot of the list, in the shape of a data tree (see
  28. ;; `org-list-struct').
  29. ;; Once the list structure is stored, it is possible to make changes
  30. ;; on it that will be mirrored to the real list or to get information
  31. ;; about the list, using accessors and methods provided in the
  32. ;; library. Most of them require the use of one or two helper
  33. ;; functions, namely `org-list-parents-alist' and
  34. ;; `org-list-prevs-alist'.
  35. ;; Structure is eventually applied to the buffer with
  36. ;; `org-list-write-struct'. This function repairs (bullets,
  37. ;; indentation, checkboxes) the list in the process. It should be
  38. ;; called near the end of any function working on structures.
  39. ;; Thus, a function applying to lists should usually follow this
  40. ;; template:
  41. ;; 1. Verify point is in a list and grab item beginning (with the same
  42. ;; function `org-in-item-p'). If the function requires the cursor
  43. ;; to be at item's bullet, `org-at-item-p' is more selective. It
  44. ;; is also possible to move point to the closest item with
  45. ;; `org-list-search-backward', or `org-list-search-forward',
  46. ;; applied to the function `org-item-beginning-re'.
  47. ;; 2. Get list structure with `org-list-struct'.
  48. ;; 3. Compute one, or both, helper functions,
  49. ;; (`org-list-parents-alist', `org-list-prevs-alist') depending on
  50. ;; needed accessors.
  51. ;; 4. Proceed with the modifications, using methods and accessors.
  52. ;; 5. Verify and apply structure to buffer, using
  53. ;; `org-list-write-struct'.
  54. ;; 6. If changes made to the list might have modified check-boxes,
  55. ;; call `org-update-checkbox-count-maybe'.
  56. ;; Computing a structure can be a costly operation on huge lists (a
  57. ;; few thousand lines long). Thus, code should follow the rule:
  58. ;; "collect once, use many". As a corollary, it is usually a bad idea
  59. ;; to use directly an interactive function inside the code, as those,
  60. ;; being independent entities, read the whole list structure another
  61. ;; time.
  62. ;;; Code:
  63. (eval-when-compile
  64. (require 'cl))
  65. (require 'org-macs)
  66. (require 'org-compat)
  67. (defvar org-M-RET-may-split-line)
  68. (defvar org-auto-align-tags)
  69. (defvar org-blank-before-new-entry)
  70. (defvar org-clock-string)
  71. (defvar org-closed-string)
  72. (defvar org-deadline-string)
  73. (defvar org-description-max-indent)
  74. (defvar org-drawers)
  75. (defvar org-odd-levels-only)
  76. (defvar org-scheduled-string)
  77. (defvar org-ts-regexp)
  78. (defvar org-ts-regexp-both)
  79. (declare-function org-at-heading-p "org" (&optional ignored))
  80. (declare-function org-before-first-heading-p "org" ())
  81. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  82. (declare-function org-combine-plists "org" (&rest plists))
  83. (declare-function org-count "org" (cl-item cl-seq))
  84. (declare-function org-current-level "org" ())
  85. (declare-function org-entry-get "org"
  86. (pom property &optional inherit literal-nil))
  87. (declare-function org-fix-tags-on-the-fly "org" ())
  88. (declare-function org-get-indentation "org" (&optional line))
  89. (declare-function org-icompleting-read "org" (&rest args))
  90. (declare-function org-in-block-p "org" (names))
  91. (declare-function org-in-regexp "org" (re &optional nlines visually))
  92. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  93. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  94. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  95. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  96. (declare-function org-level-increment "org" ())
  97. (declare-function org-narrow-to-subtree "org" ())
  98. (declare-function org-at-heading-p "org" (&optional invisible-ok))
  99. (declare-function org-previous-line-empty-p "org" ())
  100. (declare-function org-remove-if "org" (predicate seq))
  101. (declare-function org-reduced-level "org" (L))
  102. (declare-function org-show-subtree "org" ())
  103. (declare-function org-time-string-to-seconds "org" (s))
  104. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  105. (declare-function org-timer-item "org-timer" (&optional arg))
  106. (declare-function org-trim "org" (s))
  107. (declare-function org-uniquify "org" (list))
  108. (declare-function outline-invisible-p "outline" (&optional pos))
  109. (declare-function outline-flag-region "outline" (from to flag))
  110. (declare-function outline-next-heading "outline" ())
  111. (declare-function outline-previous-heading "outline" ())
  112. ;;; Configuration variables
  113. (defgroup org-plain-lists nil
  114. "Options concerning plain lists in Org-mode."
  115. :tag "Org Plain lists"
  116. :group 'org-structure)
  117. (defcustom org-cycle-include-plain-lists t
  118. "When t, make TAB cycle visibility on plain list items.
  119. Cycling plain lists works only when the cursor is on a plain list
  120. item. When the cursor is on an outline heading, plain lists are
  121. treated as text. This is the most stable way of handling this,
  122. which is why it is the default.
  123. When this is the symbol `integrate', then during cycling, plain
  124. list items will *temporarily* be interpreted as outline headlines
  125. with a level given by 1000+i where i is the indentation of the
  126. bullet. This setting can lead to strange effects when switching
  127. visibility to `children', because the first \"child\" in a
  128. subtree decides what children should be listed. If that first
  129. \"child\" is a plain list item with an implied large level
  130. number, all true children and grand children of the outline
  131. heading will be exposed in a children' view."
  132. :group 'org-plain-lists
  133. :type '(choice
  134. (const :tag "Never" nil)
  135. (const :tag "With cursor in plain list (recommended)" t)
  136. (const :tag "As children of outline headings" integrate)))
  137. (defcustom org-list-demote-modify-bullet nil
  138. "Default bullet type installed when demoting an item.
  139. This is an association list, for each bullet type, this alist will point
  140. to the bullet that should be used when this item is demoted.
  141. For example,
  142. (setq org-list-demote-modify-bullet
  143. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  144. will make
  145. + Movies
  146. + Silence of the Lambs
  147. + My Cousin Vinny
  148. + Books
  149. + The Hunt for Red October
  150. + The Road to Omaha
  151. into
  152. + Movies
  153. - Silence of the Lambs
  154. - My Cousin Vinny
  155. + Books
  156. - The Hunt for Red October
  157. - The Road to Omaha"
  158. :group 'org-plain-lists
  159. :type '(repeat
  160. (cons
  161. (choice :tag "If the current bullet is "
  162. (const "-")
  163. (const "+")
  164. (const "*")
  165. (const "1.")
  166. (const "1)"))
  167. (choice :tag "demotion will change it to"
  168. (const "-")
  169. (const "+")
  170. (const "*")
  171. (const "1.")
  172. (const "1)")))))
  173. (defcustom org-plain-list-ordered-item-terminator t
  174. "The character that makes a line with leading number an ordered list item.
  175. Valid values are ?. and ?\). To get both terminators, use t."
  176. :group 'org-plain-lists
  177. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  178. (const :tag "paren like in \"2)\"" ?\))
  179. (const :tag "both" t)))
  180. (defcustom org-alphabetical-lists nil
  181. "Non-nil means single character alphabetical bullets are allowed.
  182. Both uppercase and lowercase are handled. Lists with more than
  183. 26 items will fallback to standard numbering. Alphabetical
  184. counters like \"[@c]\" will be recognized."
  185. :group 'org-plain-lists
  186. :version "24.1"
  187. :type 'boolean)
  188. (defcustom org-list-two-spaces-after-bullet-regexp nil
  189. "A regular expression matching bullets that should have 2 spaces after them.
  190. When nil, no bullet will have two spaces after them. When
  191. a string, it will be used as a regular expression. When the
  192. bullet type of a list is changed, the new bullet type will be
  193. matched against this regexp. If it matches, there will be two
  194. spaces instead of one after the bullet in each item of the list."
  195. :group 'org-plain-lists
  196. :type '(choice
  197. (const :tag "never" nil)
  198. (regexp)))
  199. (defcustom org-empty-line-terminates-plain-lists nil
  200. "Non-nil means an empty line ends all plain list levels.
  201. Otherwise, two of them will be necessary."
  202. :group 'org-plain-lists
  203. :type 'boolean)
  204. (defcustom org-list-automatic-rules '((checkbox . t)
  205. (indent . t))
  206. "Non-nil means apply set of rules when acting on lists.
  207. By default, automatic actions are taken when using
  208. \\[org-meta-return], \\[org-metaright], \\[org-metaleft],
  209. \\[org-shiftmetaright], \\[org-shiftmetaleft],
  210. \\[org-ctrl-c-minus], \\[org-toggle-checkbox] or
  211. \\[org-insert-todo-heading]. You can disable individually these
  212. rules by setting them to nil. Valid rules are:
  213. checkbox when non-nil, checkbox statistics is updated each time
  214. you either insert a new checkbox or toggle a checkbox.
  215. indent when non-nil, indenting or outdenting list top-item
  216. with its subtree will move the whole list and
  217. outdenting a list whose bullet is * to column 0 will
  218. change that bullet to \"-\"."
  219. :group 'org-plain-lists
  220. :version "24.1"
  221. :type '(alist :tag "Sets of rules"
  222. :key-type
  223. (choice
  224. (const :tag "Checkbox" checkbox)
  225. (const :tag "Indent" indent))
  226. :value-type
  227. (boolean :tag "Activate" :value t)))
  228. (defcustom org-list-use-circular-motion nil
  229. "Non-nil means commands implying motion in lists should be cyclic.
  230. In that case, the item following the last item is the first one,
  231. and the item preceding the first item is the last one.
  232. This affects the behavior of \\[org-move-item-up],
  233. \\[org-move-item-down], \\[org-next-item] and
  234. \\[org-previous-item]."
  235. :group 'org-plain-lists
  236. :version "24.1"
  237. :type 'boolean)
  238. (defvar org-checkbox-statistics-hook nil
  239. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  240. This hook runs even if checkbox rule in
  241. `org-list-automatic-rules' does not apply, so it can be used to
  242. implement alternative ways of collecting statistics
  243. information.")
  244. (defcustom org-hierarchical-checkbox-statistics t
  245. "Non-nil means checkbox statistics counts only the state of direct children.
  246. When nil, all boxes below the cookie are counted.
  247. This can be set to nil on a per-node basis using a COOKIE_DATA property
  248. with the word \"recursive\" in the value."
  249. :group 'org-plain-lists
  250. :type 'boolean)
  251. (defcustom org-description-max-indent 20
  252. "Maximum indentation for the second line of a description list.
  253. When the indentation would be larger than this, it will become
  254. 5 characters instead."
  255. :group 'org-plain-lists
  256. :type 'integer)
  257. (defcustom org-list-indent-offset 0
  258. "Additional indentation for sub-items in a list.
  259. By setting this to a small number, usually 1 or 2, one can more
  260. clearly distinguish sub-items in a list."
  261. :group 'org-plain-lists
  262. :version "24.1"
  263. :type 'integer)
  264. (defcustom org-list-radio-list-templates
  265. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  266. % END RECEIVE ORGLST %n
  267. \\begin{comment}
  268. #+ORGLST: SEND %n org-list-to-latex
  269. -
  270. \\end{comment}\n")
  271. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  272. @c END RECEIVE ORGLST %n
  273. @ignore
  274. #+ORGLST: SEND %n org-list-to-texinfo
  275. -
  276. @end ignore\n")
  277. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  278. <!-- END RECEIVE ORGLST %n -->
  279. <!--
  280. #+ORGLST: SEND %n org-list-to-html
  281. -
  282. -->\n"))
  283. "Templates for radio lists in different major modes.
  284. All occurrences of %n in a template will be replaced with the name of the
  285. list, obtained by prompting the user."
  286. :group 'org-plain-lists
  287. :type '(repeat
  288. (list (symbol :tag "Major mode")
  289. (string :tag "Format"))))
  290. (defvar org-list-forbidden-blocks '("example" "verse" "src" "ascii" "beamer"
  291. "docbook" "html" "latex" "odt")
  292. "Names of blocks where lists are not allowed.
  293. Names must be in lower case.")
  294. (defvar org-list-export-context '(block inlinetask)
  295. "Context types where lists will be interpreted during export.
  296. Valid types are `drawer', `inlinetask' and `block'. More
  297. specifically, type `block' is determined by the variable
  298. `org-list-forbidden-blocks'.")
  299. ;;; Predicates and regexps
  300. (defconst org-list-end-re (if org-empty-line-terminates-plain-lists "^[ \t]*\n"
  301. "^[ \t]*\n[ \t]*\n")
  302. "Regex corresponding to the end of a list.
  303. It depends on `org-empty-line-terminates-plain-lists'.")
  304. (defconst org-list-full-item-re
  305. (concat "^[ \t]*\\(\\(?:[-+*]\\|\\(?:[0-9]+\\|[A-Za-z]\\)[.)]\\)\\(?:[ \t]+\\|$\\)\\)"
  306. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  307. "\\(?:\\(\\[[ X-]\\]\\)\\(?:[ \t]+\\|$\\)\\)?"
  308. "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?")
  309. "Matches a list item and puts everything into groups:
  310. group 1: bullet
  311. group 2: counter
  312. group 3: checkbox
  313. group 4: description tag")
  314. (defun org-item-re ()
  315. "Return the correct regular expression for plain lists."
  316. (let ((term (cond
  317. ((eq org-plain-list-ordered-item-terminator t) "[.)]")
  318. ((= org-plain-list-ordered-item-terminator ?\)) ")")
  319. ((= org-plain-list-ordered-item-terminator ?.) "\\.")
  320. (t "[.)]")))
  321. (alpha (if org-alphabetical-lists "\\|[A-Za-z]" "")))
  322. (concat "\\([ \t]*\\([-+]\\|\\(\\([0-9]+" alpha "\\)" term
  323. "\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")))
  324. (defsubst org-item-beginning-re ()
  325. "Regexp matching the beginning of a plain list item."
  326. (concat "^" (org-item-re)))
  327. (defun org-list-at-regexp-after-bullet-p (regexp)
  328. "Is point at a list item with REGEXP after bullet?"
  329. (and (org-at-item-p)
  330. (save-excursion
  331. (goto-char (match-end 0))
  332. (let ((counter-re (concat "\\(?:\\[@\\(?:start:\\)?"
  333. (if org-alphabetical-lists
  334. "\\([0-9]+\\|[A-Za-z]\\)"
  335. "[0-9]+")
  336. "\\][ \t]*\\)")))
  337. ;; Ignore counter if any
  338. (when (looking-at counter-re) (goto-char (match-end 0))))
  339. (looking-at regexp))))
  340. (defun org-list-in-valid-context-p ()
  341. "Is point in a context where lists are allowed?"
  342. (not (org-in-block-p org-list-forbidden-blocks)))
  343. (defun org-in-item-p ()
  344. "Return item beginning position when in a plain list, nil otherwise."
  345. (save-excursion
  346. (beginning-of-line)
  347. (let* ((case-fold-search t)
  348. (context (org-list-context))
  349. (lim-up (car context))
  350. (drawers-re (concat "^[ \t]*:\\("
  351. (mapconcat 'regexp-quote org-drawers "\\|")
  352. "\\):[ \t]*$"))
  353. (inlinetask-re (and (featurep 'org-inlinetask)
  354. (org-inlinetask-outline-regexp)))
  355. (item-re (org-item-re))
  356. ;; Indentation isn't meaningful when point starts at an empty
  357. ;; line or an inline task.
  358. (ind-ref (if (or (looking-at "^[ \t]*$")
  359. (and inlinetask-re (looking-at inlinetask-re)))
  360. 10000
  361. (org-get-indentation))))
  362. (cond
  363. ((eq (nth 2 context) 'invalid) nil)
  364. ((looking-at item-re) (point))
  365. (t
  366. ;; Detect if cursor in amidst `org-list-end-re'. First, count
  367. ;; number HL of hard lines it takes, then call `org-in-regexp'
  368. ;; to compute its boundaries END-BOUNDS. When point is
  369. ;; in-between, move cursor before regexp beginning.
  370. (let ((hl 0) (i -1) end-bounds)
  371. (when (and (progn
  372. (while (setq i (string-match
  373. "[\r\n]" org-list-end-re (1+ i)))
  374. (setq hl (1+ hl)))
  375. (setq end-bounds (org-in-regexp org-list-end-re hl)))
  376. (>= (point) (car end-bounds))
  377. (< (point) (cdr end-bounds)))
  378. (goto-char (car end-bounds))
  379. (forward-line -1)))
  380. ;; Look for an item, less indented that reference line.
  381. (catch 'exit
  382. (while t
  383. (let ((ind (org-get-indentation)))
  384. (cond
  385. ;; This is exactly what we want.
  386. ((and (looking-at item-re) (< ind ind-ref))
  387. (throw 'exit (point)))
  388. ;; At upper bound of search or looking at the end of a
  389. ;; previous list: search is over.
  390. ((<= (point) lim-up) (throw 'exit nil))
  391. ((looking-at org-list-end-re) (throw 'exit nil))
  392. ;; Skip blocks, drawers, inline-tasks, blank lines
  393. ((and (looking-at "^[ \t]*#\\+end_")
  394. (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
  395. ((and (looking-at "^[ \t]*:END:")
  396. (re-search-backward drawers-re lim-up t))
  397. (beginning-of-line))
  398. ((and inlinetask-re (looking-at inlinetask-re))
  399. (org-inlinetask-goto-beginning)
  400. (forward-line -1))
  401. ((looking-at "^[ \t]*$") (forward-line -1))
  402. ;; Text at column 0 cannot belong to a list: stop.
  403. ((zerop ind) (throw 'exit nil))
  404. ;; Normal text less indented than reference line, take
  405. ;; it as new reference.
  406. ((< ind ind-ref)
  407. (setq ind-ref ind)
  408. (forward-line -1))
  409. (t (forward-line -1)))))))))))
  410. (defun org-at-item-p ()
  411. "Is point in a line starting a hand-formatted item?"
  412. (save-excursion
  413. (beginning-of-line)
  414. (and (looking-at (org-item-re)) (org-list-in-valid-context-p))))
  415. (defun org-at-item-bullet-p ()
  416. "Is point at the bullet of a plain list item?"
  417. (and (org-at-item-p)
  418. (not (member (char-after) '(?\ ?\t)))
  419. (< (point) (match-end 0))))
  420. (defun org-at-item-timer-p ()
  421. "Is point at a line starting a plain list item with a timer?"
  422. (org-list-at-regexp-after-bullet-p
  423. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  424. (defun org-at-item-description-p ()
  425. "Is point at a description list item?"
  426. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::\\([ \t]+\\|$\\)"))
  427. (defun org-at-item-checkbox-p ()
  428. "Is point at a line starting a plain-list item with a checklet?"
  429. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  430. (defun org-at-item-counter-p ()
  431. "Is point at a line starting a plain-list item with a counter?"
  432. (and (org-at-item-p)
  433. (looking-at org-list-full-item-re)
  434. (match-string 2)))
  435. ;;; Structures and helper functions
  436. (defun org-list-context ()
  437. "Determine context, and its boundaries, around point.
  438. Context will be a cell like (MIN MAX CONTEXT) where MIN and MAX
  439. are boundaries and CONTEXT is a symbol among `drawer', `block',
  440. `invalid', `inlinetask' and nil.
  441. Contexts `block' and `invalid' refer to `org-list-forbidden-blocks'."
  442. (save-match-data
  443. (save-excursion
  444. (org-with-limited-levels
  445. (beginning-of-line)
  446. (let ((case-fold-search t) (pos (point)) beg end context-type
  447. ;; Get positions of surrounding headings. This is the
  448. ;; default context.
  449. (lim-up (or (save-excursion (and (ignore-errors (org-back-to-heading t))
  450. (point)))
  451. (point-min)))
  452. (lim-down (or (save-excursion (outline-next-heading)) (point-max))))
  453. ;; Is point inside a drawer?
  454. (let ((end-re "^[ \t]*:END:")
  455. ;; Can't use org-drawers-regexp as this function might
  456. ;; be called in buffers not in Org mode.
  457. (beg-re (concat "^[ \t]*:\\("
  458. (mapconcat 'regexp-quote org-drawers "\\|")
  459. "\\):[ \t]*$")))
  460. (when (save-excursion
  461. (and (not (looking-at beg-re))
  462. (not (looking-at end-re))
  463. (setq beg (and (re-search-backward beg-re lim-up t)
  464. (1+ (point-at-eol))))
  465. (setq end (or (and (re-search-forward end-re lim-down t)
  466. (1- (match-beginning 0)))
  467. lim-down))
  468. (>= end pos)))
  469. (setq lim-up beg lim-down end context-type 'drawer)))
  470. ;; Is point strictly in a block, and of which type?
  471. (let ((block-re "^[ \t]*#\\+\\(begin\\|end\\)_") type)
  472. (when (save-excursion
  473. (and (not (looking-at block-re))
  474. (setq beg (and (re-search-backward block-re lim-up t)
  475. (1+ (point-at-eol))))
  476. (looking-at "^[ \t]*#\\+begin_\\(\\S-+\\)")
  477. (setq type (downcase (match-string 1)))
  478. (goto-char beg)
  479. (setq end (or (and (re-search-forward block-re lim-down t)
  480. (1- (point-at-bol)))
  481. lim-down))
  482. (>= end pos)
  483. (equal (downcase (match-string 1)) "end")))
  484. (setq lim-up beg lim-down end
  485. context-type (if (member type org-list-forbidden-blocks)
  486. 'invalid 'block))))
  487. ;; Is point in an inlinetask?
  488. (when (and (featurep 'org-inlinetask)
  489. (save-excursion
  490. (let* ((beg-re (org-inlinetask-outline-regexp))
  491. (end-re (concat beg-re "END[ \t]*$")))
  492. (and (not (looking-at "^\\*+"))
  493. (setq beg (and (re-search-backward beg-re lim-up t)
  494. (1+ (point-at-eol))))
  495. (not (looking-at end-re))
  496. (setq end (and (re-search-forward end-re lim-down t)
  497. (1- (match-beginning 0))))
  498. (> (point) pos)))))
  499. (setq lim-up beg lim-down end context-type 'inlinetask))
  500. ;; Return context boundaries and type.
  501. (list lim-up lim-down context-type))))))
  502. (defun org-list-struct ()
  503. "Return structure of list at point.
  504. A list structure is an alist where key is point at item, and
  505. values are:
  506. 1. indentation,
  507. 2. bullet with trailing whitespace,
  508. 3. bullet counter, if any,
  509. 4. checkbox, if any,
  510. 5. description tag, if any,
  511. 6. position at item end.
  512. Thus the following list, where numbers in parens are
  513. point-at-bol:
  514. - [X] first item (1)
  515. 1. sub-item 1 (18)
  516. 5. [@5] sub-item 2 (34)
  517. some other text belonging to first item (55)
  518. - last item (97)
  519. + tag :: description (109)
  520. (131)
  521. will get the following structure:
  522. \(\(1 0 \"- \" nil \"[X]\" nil 97\)
  523. \(18 2 \"1. \" nil nil nil 34\)
  524. \(34 2 \"5. \" \"5\" nil nil 55\)
  525. \(97 0 \"- \" nil nil nil 131\)
  526. \(109 2 \"+ \" nil nil \"tag\" 131\)
  527. Assume point is at an item."
  528. (save-excursion
  529. (beginning-of-line)
  530. (let* ((case-fold-search t)
  531. (context (org-list-context))
  532. (lim-up (car context))
  533. (lim-down (nth 1 context))
  534. (text-min-ind 10000)
  535. (item-re (org-item-re))
  536. (drawers-re (concat "^[ \t]*:\\("
  537. (mapconcat 'regexp-quote org-drawers "\\|")
  538. "\\):[ \t]*$"))
  539. (inlinetask-re (and (featurep 'org-inlinetask)
  540. (org-inlinetask-outline-regexp)))
  541. (beg-cell (cons (point) (org-get-indentation)))
  542. ind itm-lst itm-lst-2 end-lst end-lst-2 struct
  543. (assoc-at-point
  544. (function
  545. ;; Return association at point.
  546. (lambda (ind)
  547. (looking-at org-list-full-item-re)
  548. (let ((bullet (match-string-no-properties 1)))
  549. (list (point)
  550. ind
  551. bullet
  552. (match-string-no-properties 2) ; counter
  553. (match-string-no-properties 3) ; checkbox
  554. ;; Description tag.
  555. (and (save-match-data (string-match "[-+*]" bullet))
  556. (match-string-no-properties 4)))))))
  557. (end-before-blank
  558. (function
  559. ;; Ensure list ends at the first blank line.
  560. (lambda ()
  561. (skip-chars-backward " \r\t\n")
  562. (min (1+ (point-at-eol)) lim-down)))))
  563. ;; 1. Read list from starting item to its beginning, and save
  564. ;; top item position and indentation in BEG-CELL. Also store
  565. ;; ending position of items in END-LST.
  566. (save-excursion
  567. (catch 'exit
  568. (while t
  569. (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
  570. (org-get-indentation))))
  571. (cond
  572. ((<= (point) lim-up)
  573. ;; At upward limit: if we ended at an item, store it,
  574. ;; else dismiss useless data recorded above BEG-CELL.
  575. ;; Jump to part 2.
  576. (throw 'exit
  577. (setq itm-lst
  578. (if (or (not (looking-at item-re))
  579. (get-text-property (point) 'org-example))
  580. (memq (assq (car beg-cell) itm-lst) itm-lst)
  581. (setq beg-cell (cons (point) ind))
  582. (cons (funcall assoc-at-point ind) itm-lst)))))
  583. ;; At a verbatim block, go before its beginning. Move
  584. ;; from eol to ensure `previous-single-property-change'
  585. ;; will return a value.
  586. ((get-text-property (point) 'org-example)
  587. (goto-char (previous-single-property-change
  588. (point-at-eol) 'org-example nil lim-up))
  589. (forward-line -1))
  590. ;; Looking at a list ending regexp. Dismiss useless
  591. ;; data recorded above BEG-CELL. Jump to part 2.
  592. ((looking-at org-list-end-re)
  593. (throw 'exit
  594. (setq itm-lst
  595. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  596. ;; Point is at an item. Add data to ITM-LST. It may
  597. ;; also end a previous item: save it in END-LST. If
  598. ;; ind is less or equal than BEG-CELL and there is no
  599. ;; end at this ind or lesser, this item becomes the new
  600. ;; BEG-CELL.
  601. ((looking-at item-re)
  602. (push (funcall assoc-at-point ind) itm-lst)
  603. (push (cons ind (point)) end-lst)
  604. (when (< ind text-min-ind) (setq beg-cell (cons (point) ind)))
  605. (forward-line -1))
  606. ;; Skip blocks, drawers, inline tasks, blank lines.
  607. ((and (looking-at "^[ \t]*#\\+end_")
  608. (re-search-backward "^[ \t]*#\\+begin_" lim-up t)))
  609. ((and (looking-at "^[ \t]*:END:")
  610. (re-search-backward drawers-re lim-up t))
  611. (beginning-of-line))
  612. ((and inlinetask-re (looking-at inlinetask-re))
  613. (org-inlinetask-goto-beginning)
  614. (forward-line -1))
  615. ((looking-at "^[ \t]*$")
  616. (forward-line -1))
  617. ;; From there, point is not at an item. Interpret
  618. ;; line's indentation:
  619. ;; - text at column 0 is necessarily out of any list.
  620. ;; Dismiss data recorded above BEG-CELL. Jump to
  621. ;; part 2.
  622. ;; - any other case may be an ending position for an
  623. ;; hypothetical item above. Store it and proceed.
  624. ((zerop ind)
  625. (throw 'exit
  626. (setq itm-lst
  627. (memq (assq (car beg-cell) itm-lst) itm-lst))))
  628. (t
  629. (when (< ind text-min-ind) (setq text-min-ind ind))
  630. (push (cons ind (point)) end-lst)
  631. (forward-line -1)))))))
  632. ;; 2. Read list from starting point to its end, that is until we
  633. ;; get out of context, or that a non-item line is less or
  634. ;; equally indented than BEG-CELL's cdr. Also, store ending
  635. ;; position of items in END-LST-2.
  636. (catch 'exit
  637. (while t
  638. (let ((ind (+ (or (get-text-property (point) 'original-indentation) 0)
  639. (org-get-indentation))))
  640. (cond
  641. ((>= (point) lim-down)
  642. ;; At downward limit: this is de facto the end of the
  643. ;; list. Save point as an ending position, and jump to
  644. ;; part 3.
  645. (throw 'exit
  646. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  647. ;; At a verbatim block, move to its end. Point is at bol
  648. ;; and 'org-example property is set by whole lines:
  649. ;; `next-single-property-change' always return a value.
  650. ((get-text-property (point) 'org-example)
  651. (goto-char
  652. (next-single-property-change (point) 'org-example nil lim-down)))
  653. ;; Looking at a list ending regexp. Save point as an
  654. ;; ending position and jump to part 3.
  655. ((looking-at org-list-end-re)
  656. (throw 'exit (push (cons 0 (point)) end-lst-2)))
  657. ((looking-at item-re)
  658. ;; Point is at an item. Add data to ITM-LST-2. It may
  659. ;; also end a previous item, so save it in END-LST-2.
  660. (push (funcall assoc-at-point ind) itm-lst-2)
  661. (push (cons ind (point)) end-lst-2)
  662. (forward-line 1))
  663. ;; Skip inline tasks and blank lines along the way
  664. ((and inlinetask-re (looking-at inlinetask-re))
  665. (org-inlinetask-goto-end))
  666. ((looking-at "^[ \t]*$")
  667. (forward-line 1))
  668. ;; Ind is lesser or equal than BEG-CELL's. The list is
  669. ;; over: store point as an ending position and jump to
  670. ;; part 3.
  671. ((<= ind (cdr beg-cell))
  672. (throw 'exit
  673. (push (cons 0 (funcall end-before-blank)) end-lst-2)))
  674. ;; Else, if ind is lesser or equal than previous item's,
  675. ;; this is an ending position: store it. In any case,
  676. ;; skip block or drawer at point, and move to next line.
  677. (t
  678. (when (<= ind (nth 1 (car itm-lst-2)))
  679. (push (cons ind (point)) end-lst-2))
  680. (cond
  681. ((and (looking-at "^[ \t]*#\\+begin_")
  682. (re-search-forward "^[ \t]*#\\+end_" lim-down t)))
  683. ((and (looking-at drawers-re)
  684. (re-search-forward "^[ \t]*:END:" lim-down t))))
  685. (forward-line 1))))))
  686. (setq struct (append itm-lst (cdr (nreverse itm-lst-2)))
  687. end-lst (append end-lst (cdr (nreverse end-lst-2))))
  688. ;; 3. Associate each item to its end position.
  689. (org-list-struct-assoc-end struct end-lst)
  690. ;; 4. Return STRUCT
  691. struct)))
  692. (defun org-list-struct-assoc-end (struct end-list)
  693. "Associate proper ending point to items in STRUCT.
  694. END-LIST is a pseudo-alist where car is indentation and cdr is
  695. ending position.
  696. This function modifies STRUCT."
  697. (let ((endings end-list))
  698. (mapc
  699. (lambda (elt)
  700. (let ((pos (car elt))
  701. (ind (nth 1 elt)))
  702. ;; Remove end candidates behind current item.
  703. (while (or (<= (cdar endings) pos))
  704. (pop endings))
  705. ;; Add end position to item assoc.
  706. (let ((old-end (nthcdr 6 elt))
  707. (new-end (assoc-default ind endings '<=)))
  708. (if old-end
  709. (setcar old-end new-end)
  710. (setcdr elt (append (cdr elt) (list new-end)))))))
  711. struct)))
  712. (defun org-list-prevs-alist (struct)
  713. "Return alist between item and previous item in STRUCT."
  714. (let ((item-end-alist (mapcar (lambda (e) (cons (car e) (nth 6 e)))
  715. struct)))
  716. (mapcar (lambda (e)
  717. (let ((prev (car (rassq (car e) item-end-alist))))
  718. (cons (car e) prev)))
  719. struct)))
  720. (defun org-list-parents-alist (struct)
  721. "Return alist between item and parent in STRUCT."
  722. (let* ((ind-to-ori (list (list (nth 1 (car struct)))))
  723. (top-item (org-list-get-top-point struct))
  724. (prev-pos (list top-item)))
  725. (cons prev-pos
  726. (mapcar (lambda (item)
  727. (let ((pos (car item))
  728. (ind (nth 1 item))
  729. (prev-ind (caar ind-to-ori)))
  730. (push pos prev-pos)
  731. (cond
  732. ((> prev-ind ind)
  733. ;; A sub-list is over. Find the associated
  734. ;; origin in IND-TO-ORI. If it cannot be
  735. ;; found (ill-formed list), set its parent as
  736. ;; the first item less indented. If there is
  737. ;; none, make it a top-level item.
  738. (setq ind-to-ori
  739. (or (member (assq ind ind-to-ori) ind-to-ori)
  740. (catch 'exit
  741. (mapc
  742. (lambda (e)
  743. (when (< (car e) ind)
  744. (throw 'exit (member e ind-to-ori))))
  745. ind-to-ori)
  746. (list (list ind)))))
  747. (cons pos (cdar ind-to-ori)))
  748. ;; A sub-list starts. Every item at IND will
  749. ;; have previous item as its parent.
  750. ((< prev-ind ind)
  751. (let ((origin (nth 1 prev-pos)))
  752. (push (cons ind origin) ind-to-ori)
  753. (cons pos origin)))
  754. ;; Another item in the same sub-list: it shares
  755. ;; the same parent as the previous item.
  756. (t (cons pos (cdar ind-to-ori))))))
  757. (cdr struct)))))
  758. ;;; Accessors
  759. (defsubst org-list-get-nth (n key struct)
  760. "Return the Nth value of KEY in STRUCT."
  761. (nth n (assq key struct)))
  762. (defun org-list-set-nth (n key struct new)
  763. "Set the Nth value of KEY in STRUCT to NEW.
  764. \nThis function modifies STRUCT."
  765. (setcar (nthcdr n (assq key struct)) new))
  766. (defsubst org-list-get-ind (item struct)
  767. "Return indentation of ITEM in STRUCT."
  768. (org-list-get-nth 1 item struct))
  769. (defun org-list-set-ind (item struct ind)
  770. "Set indentation of ITEM in STRUCT to IND.
  771. \nThis function modifies STRUCT."
  772. (org-list-set-nth 1 item struct ind))
  773. (defsubst org-list-get-bullet (item struct)
  774. "Return bullet of ITEM in STRUCT."
  775. (org-list-get-nth 2 item struct))
  776. (defun org-list-set-bullet (item struct bullet)
  777. "Set bullet of ITEM in STRUCT to BULLET.
  778. \nThis function modifies STRUCT."
  779. (org-list-set-nth 2 item struct bullet))
  780. (defsubst org-list-get-counter (item struct)
  781. "Return counter of ITEM in STRUCT."
  782. (org-list-get-nth 3 item struct))
  783. (defsubst org-list-get-checkbox (item struct)
  784. "Return checkbox of ITEM in STRUCT or nil."
  785. (org-list-get-nth 4 item struct))
  786. (defun org-list-set-checkbox (item struct checkbox)
  787. "Set checkbox of ITEM in STRUCT to CHECKBOX.
  788. \nThis function modifies STRUCT."
  789. (org-list-set-nth 4 item struct checkbox))
  790. (defsubst org-list-get-tag (item struct)
  791. "Return end position of ITEM in STRUCT."
  792. (org-list-get-nth 5 item struct))
  793. (defun org-list-get-item-end (item struct)
  794. "Return end position of ITEM in STRUCT."
  795. (org-list-get-nth 6 item struct))
  796. (defun org-list-get-item-end-before-blank (item struct)
  797. "Return point at end of ITEM in STRUCT, before any blank line.
  798. Point returned is at end of line."
  799. (save-excursion
  800. (goto-char (org-list-get-item-end item struct))
  801. (skip-chars-backward " \r\t\n")
  802. (point-at-eol)))
  803. (defun org-list-get-parent (item struct parents)
  804. "Return parent of ITEM or nil.
  805. STRUCT is the list structure. PARENTS is the alist of parents,
  806. as returned by `org-list-parents-alist'."
  807. (let ((parents (or parents (org-list-parents-alist struct))))
  808. (cdr (assq item parents))))
  809. (defun org-list-has-child-p (item struct)
  810. "Non-nil if ITEM has a child.
  811. STRUCT is the list structure.
  812. Value returned is the position of the first child of ITEM."
  813. (let ((ind (org-list-get-ind item struct))
  814. (child-maybe (car (nth 1 (member (assq item struct) struct)))))
  815. (when (and child-maybe
  816. (< ind (org-list-get-ind child-maybe struct)))
  817. child-maybe)))
  818. (defun org-list-get-next-item (item struct prevs)
  819. "Return next item in same sub-list as ITEM, or nil.
  820. STRUCT is the list structure. PREVS is the alist of previous
  821. items, as returned by `org-list-prevs-alist'."
  822. (car (rassq item prevs)))
  823. (defun org-list-get-prev-item (item struct prevs)
  824. "Return previous item in same sub-list as ITEM, or nil.
  825. STRUCT is the list structure. PREVS is the alist of previous
  826. items, as returned by `org-list-prevs-alist'."
  827. (cdr (assq item prevs)))
  828. (defun org-list-get-subtree (item struct)
  829. "List all items having ITEM as a common ancestor, or nil.
  830. STRUCT is the list structure."
  831. (let* ((item-end (org-list-get-item-end item struct))
  832. (sub-struct (cdr (member (assq item struct) struct)))
  833. subtree)
  834. (catch 'exit
  835. (mapc (lambda (e)
  836. (let ((pos (car e)))
  837. (if (< pos item-end) (push pos subtree) (throw 'exit nil))))
  838. sub-struct))
  839. (nreverse subtree)))
  840. (defun org-list-get-all-items (item struct prevs)
  841. "List all items in the same sub-list as ITEM.
  842. STRUCT is the list structure. PREVS is the alist of previous
  843. items, as returned by `org-list-prevs-alist'."
  844. (let ((prev-item item)
  845. (next-item item)
  846. before-item after-item)
  847. (while (setq prev-item (org-list-get-prev-item prev-item struct prevs))
  848. (push prev-item before-item))
  849. (while (setq next-item (org-list-get-next-item next-item struct prevs))
  850. (push next-item after-item))
  851. (append before-item (list item) (nreverse after-item))))
  852. (defun org-list-get-children (item struct parents)
  853. "List all children of ITEM, or nil.
  854. STRUCT is the list structure. PARENTS is the alist of parents,
  855. as returned by `org-list-parents-alist'."
  856. (let (all child)
  857. (while (setq child (car (rassq item parents)))
  858. (setq parents (cdr (member (assq child parents) parents)))
  859. (push child all))
  860. (nreverse all)))
  861. (defun org-list-get-top-point (struct)
  862. "Return point at beginning of list.
  863. STRUCT is the list structure."
  864. (caar struct))
  865. (defun org-list-get-bottom-point (struct)
  866. "Return point at bottom of list.
  867. STRUCT is the list structure."
  868. (apply 'max
  869. (mapcar (lambda (e) (org-list-get-item-end (car e) struct)) struct)))
  870. (defun org-list-get-list-begin (item struct prevs)
  871. "Return point at beginning of sub-list ITEM belongs.
  872. STRUCT is the list structure. PREVS is the alist of previous
  873. items, as returned by `org-list-prevs-alist'."
  874. (let ((first-item item) prev-item)
  875. (while (setq prev-item (org-list-get-prev-item first-item struct prevs))
  876. (setq first-item prev-item))
  877. first-item))
  878. (defalias 'org-list-get-first-item 'org-list-get-list-begin)
  879. (defun org-list-get-last-item (item struct prevs)
  880. "Return point at last item of sub-list ITEM belongs.
  881. STRUCT is the list structure. PREVS is the alist of previous
  882. items, as returned by `org-list-prevs-alist'."
  883. (let ((last-item item) next-item)
  884. (while (setq next-item (org-list-get-next-item last-item struct prevs))
  885. (setq last-item next-item))
  886. last-item))
  887. (defun org-list-get-list-end (item struct prevs)
  888. "Return point at end of sub-list ITEM belongs.
  889. STRUCT is the list structure. PREVS is the alist of previous
  890. items, as returned by `org-list-prevs-alist'."
  891. (org-list-get-item-end (org-list-get-last-item item struct prevs) struct))
  892. (defun org-list-get-list-type (item struct prevs)
  893. "Return the type of the list containing ITEM, as a symbol.
  894. STRUCT is the list structure. PREVS is the alist of previous
  895. items, as returned by `org-list-prevs-alist'.
  896. Possible types are `descriptive', `ordered' and `unordered'. The
  897. type is determined by the first item of the list."
  898. (let ((first (org-list-get-list-begin item struct prevs)))
  899. (cond
  900. ((string-match "[[:alnum:]]" (org-list-get-bullet first struct)) 'ordered)
  901. ((org-list-get-tag first struct) 'descriptive)
  902. (t 'unordered))))
  903. (defun org-list-get-item-number (item struct prevs parents)
  904. "Return ITEM's sequence number.
  905. STRUCT is the list structure. PREVS is the alist of previous
  906. items, as returned by `org-list-prevs-alist'. PARENTS is the
  907. alist of ancestors, as returned by `org-list-parents-alist'.
  908. Return value is a list of integers. Counters have an impact on
  909. that value."
  910. (let ((get-relative-number
  911. (function
  912. (lambda (item struct prevs)
  913. ;; Return relative sequence number of ITEM in the sub-list
  914. ;; it belongs. STRUCT is the list structure. PREVS is
  915. ;; the alist of previous items.
  916. (let ((seq 0) (pos item) counter)
  917. (while (and (not (setq counter (org-list-get-counter pos struct)))
  918. (setq pos (org-list-get-prev-item pos struct prevs)))
  919. (incf seq))
  920. (if (not counter) (1+ seq)
  921. (cond
  922. ((string-match "[A-Za-z]" counter)
  923. (+ (- (string-to-char (upcase (match-string 0 counter))) 64)
  924. seq))
  925. ((string-match "[0-9]+" counter)
  926. (+ (string-to-number (match-string 0 counter)) seq))
  927. (t (1+ seq)))))))))
  928. ;; Cons each parent relative number into return value (OUT).
  929. (let ((out (list (funcall get-relative-number item struct prevs)))
  930. (parent item))
  931. (while (setq parent (org-list-get-parent parent struct parents))
  932. (push (funcall get-relative-number parent struct prevs) out))
  933. ;; Return value.
  934. out)))
  935. ;;; Searching
  936. (defun org-list-search-generic (search re bound noerr)
  937. "Search a string in valid contexts for lists.
  938. Arguments SEARCH, RE, BOUND and NOERR are similar to those used
  939. in `re-search-forward'."
  940. (catch 'exit
  941. (let ((origin (point)))
  942. (while t
  943. ;; 1. No match: return to origin or bound, depending on NOERR.
  944. (unless (funcall search re bound noerr)
  945. (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
  946. nil)))
  947. ;; 2. Match in valid context: return point. Else, continue
  948. ;; searching.
  949. (when (org-list-in-valid-context-p) (throw 'exit (point)))))))
  950. (defun org-list-search-backward (regexp &optional bound noerror)
  951. "Like `re-search-backward' but stop only where lists are recognized.
  952. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  953. `re-search-backward'."
  954. (org-list-search-generic #'re-search-backward
  955. regexp (or bound (point-min)) noerror))
  956. (defun org-list-search-forward (regexp &optional bound noerror)
  957. "Like `re-search-forward' but stop only where lists are recognized.
  958. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  959. `re-search-forward'."
  960. (org-list-search-generic #'re-search-forward
  961. regexp (or bound (point-max)) noerror))
  962. ;;; Methods on structures
  963. (defsubst org-list-bullet-string (bullet)
  964. "Return BULLET with the correct number of whitespaces.
  965. It determines the number of whitespaces to append by looking at
  966. `org-list-two-spaces-after-bullet-regexp'."
  967. (save-match-data
  968. (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
  969. (string-match
  970. org-list-two-spaces-after-bullet-regexp bullet))
  971. " "
  972. " ")))
  973. (string-match "\\S-+\\([ \t]*\\)" bullet)
  974. (replace-match spaces nil nil bullet 1))))
  975. (defun org-list-swap-items (beg-A beg-B struct)
  976. "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
  977. Blank lines at the end of items are left in place. Item
  978. visibility is preserved. Return the new structure after the
  979. changes.
  980. Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
  981. to the same sub-list.
  982. This function modifies STRUCT."
  983. (save-excursion
  984. (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
  985. (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
  986. (end-A (org-list-get-item-end beg-A struct))
  987. (end-B (org-list-get-item-end beg-B struct))
  988. (size-A (- end-A-no-blank beg-A))
  989. (size-B (- end-B-no-blank beg-B))
  990. (body-A (buffer-substring beg-A end-A-no-blank))
  991. (body-B (buffer-substring beg-B end-B-no-blank))
  992. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
  993. (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
  994. (sub-B (cons beg-B (org-list-get-subtree beg-B struct)))
  995. ;; Store overlays responsible for visibility status. We
  996. ;; also need to store their boundaries as they will be
  997. ;; removed from buffer.
  998. (overlays (cons
  999. (mapcar (lambda (ov)
  1000. (list ov (overlay-start ov) (overlay-end ov)))
  1001. (overlays-in beg-A end-A))
  1002. (mapcar (lambda (ov)
  1003. (list ov (overlay-start ov) (overlay-end ov)))
  1004. (overlays-in beg-B end-B)))))
  1005. ;; 1. Move effectively items in buffer.
  1006. (goto-char beg-A)
  1007. (delete-region beg-A end-B-no-blank)
  1008. (insert (concat body-B between-A-no-blank-and-B body-A))
  1009. ;; 2. Now modify struct. No need to re-read the list, the
  1010. ;; transformation is just a shift of positions. Some special
  1011. ;; attention is required for items ending at END-A and END-B
  1012. ;; as empty spaces are not moved there. In others words,
  1013. ;; item BEG-A will end with whitespaces that were at the end
  1014. ;; of BEG-B and the same applies to BEG-B.
  1015. (mapc (lambda (e)
  1016. (let ((pos (car e)))
  1017. (cond
  1018. ((< pos beg-A))
  1019. ((memq pos sub-A)
  1020. (let ((end-e (nth 6 e)))
  1021. (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
  1022. (setcar (nthcdr 6 e)
  1023. (+ end-e (- end-B-no-blank end-A-no-blank)))
  1024. (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
  1025. ((memq pos sub-B)
  1026. (let ((end-e (nth 6 e)))
  1027. (setcar e (- (+ pos beg-A) beg-B))
  1028. (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
  1029. (when (= end-e end-B)
  1030. (setcar (nthcdr 6 e)
  1031. (+ beg-A size-B (- end-A end-A-no-blank))))))
  1032. ((< pos beg-B)
  1033. (let ((end-e (nth 6 e)))
  1034. (setcar e (+ pos (- size-B size-A)))
  1035. (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
  1036. struct)
  1037. (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
  1038. ;; Restore visibility status, by moving overlays to their new
  1039. ;; position.
  1040. (mapc (lambda (ov)
  1041. (move-overlay
  1042. (car ov)
  1043. (+ (nth 1 ov) (- (+ beg-B (- size-B size-A)) beg-A))
  1044. (+ (nth 2 ov) (- (+ beg-B (- size-B size-A)) beg-A))))
  1045. (car overlays))
  1046. (mapc (lambda (ov)
  1047. (move-overlay (car ov)
  1048. (+ (nth 1 ov) (- beg-A beg-B))
  1049. (+ (nth 2 ov) (- beg-A beg-B))))
  1050. (cdr overlays))
  1051. ;; Return structure.
  1052. struct)))
  1053. (defun org-list-separating-blank-lines-number (pos struct prevs)
  1054. "Return number of blank lines that should separate items in list.
  1055. POS is the position of point where `org-list-insert-item' was called.
  1056. STRUCT is the list structure. PREVS is the alist of previous
  1057. items, as returned by `org-list-prevs-alist'.
  1058. Assume point is at item's beginning. If the item is alone, apply
  1059. some heuristics to guess the result."
  1060. (save-excursion
  1061. (let ((item (point))
  1062. (insert-blank-p
  1063. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  1064. usr-blank
  1065. (count-blanks
  1066. (function
  1067. (lambda ()
  1068. ;; Count blank lines above beginning of line.
  1069. (save-excursion
  1070. (count-lines (goto-char (point-at-bol))
  1071. (progn (skip-chars-backward " \r\t\n")
  1072. (forward-line)
  1073. (point))))))))
  1074. (cond
  1075. ;; Trivial cases where there should be none.
  1076. ((or org-empty-line-terminates-plain-lists (not insert-blank-p)) 0)
  1077. ;; When `org-blank-before-new-entry' says so, it is 1.
  1078. ((eq insert-blank-p t) 1)
  1079. ;; `plain-list-item' is 'auto. Count blank lines separating
  1080. ;; neighbours items in list.
  1081. (t (let ((next-p (org-list-get-next-item item struct prevs)))
  1082. (cond
  1083. ;; Is there a next item?
  1084. (next-p (goto-char next-p)
  1085. (funcall count-blanks))
  1086. ;; Is there a previous item?
  1087. ((org-list-get-prev-item item struct prevs)
  1088. (funcall count-blanks))
  1089. ;; User inserted blank lines, trust him.
  1090. ((and (> pos (org-list-get-item-end-before-blank item struct))
  1091. (> (save-excursion (goto-char pos)
  1092. (setq usr-blank (funcall count-blanks)))
  1093. 0))
  1094. usr-blank)
  1095. ;; Are there blank lines inside the list so far?
  1096. ((save-excursion
  1097. (goto-char (org-list-get-top-point struct))
  1098. (org-list-search-forward
  1099. "^[ \t]*$" (org-list-get-item-end-before-blank item struct) t))
  1100. 1)
  1101. ;; Default choice: no blank line.
  1102. (t 0))))))))
  1103. (defun org-list-insert-item (pos struct prevs &optional checkbox after-bullet)
  1104. "Insert a new list item at POS and return the new structure.
  1105. If POS is before first character after bullet of the item, the
  1106. new item will be created before the current one.
  1107. STRUCT is the list structure. PREVS is the the alist of previous
  1108. items, as returned by `org-list-prevs-alist'.
  1109. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  1110. after the bullet. Cursor will be after this text once the
  1111. function ends.
  1112. This function modifies STRUCT."
  1113. (let ((case-fold-search t))
  1114. ;; 1. Get information about list: position of point with regards
  1115. ;; to item start (BEFOREP), blank lines number separating items
  1116. ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
  1117. (let* ((item (progn (goto-char pos) (goto-char (org-list-get-item-begin))))
  1118. (item-end (org-list-get-item-end item struct))
  1119. (item-end-no-blank (org-list-get-item-end-before-blank item struct))
  1120. (beforep
  1121. (progn
  1122. (looking-at org-list-full-item-re)
  1123. ;; Do not count tag in a non-descriptive list.
  1124. (<= pos (if (and (match-beginning 4)
  1125. (save-match-data
  1126. (string-match "[.)]" (match-string 1))))
  1127. (match-beginning 4)
  1128. (match-end 0)))))
  1129. (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
  1130. (blank-nb (org-list-separating-blank-lines-number
  1131. pos struct prevs))
  1132. ;; 2. Build the new item to be created. Concatenate same
  1133. ;; bullet as item, checkbox, text AFTER-BULLET if
  1134. ;; provided, and text cut from point to end of item
  1135. ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
  1136. ;; BEFOREP and SPLIT-LINE-P. The difference of size
  1137. ;; between what was cut and what was inserted in buffer
  1138. ;; is stored in SIZE-OFFSET.
  1139. (ind (org-list-get-ind item struct))
  1140. (ind-size (if indent-tabs-mode
  1141. (+ (/ ind tab-width) (mod ind tab-width))
  1142. ind))
  1143. (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
  1144. (box (when checkbox "[ ]"))
  1145. (text-cut
  1146. (and (not beforep) split-line-p
  1147. (progn
  1148. (goto-char pos)
  1149. ;; If POS is greater than ITEM-END, then point is
  1150. ;; in some white lines after the end of the list.
  1151. ;; Those must be removed, or they will be left,
  1152. ;; stacking up after the list.
  1153. (when (< item-end pos)
  1154. (delete-region (1- item-end) (point-at-eol)))
  1155. (skip-chars-backward " \r\t\n")
  1156. (setq pos (point))
  1157. (delete-and-extract-region pos item-end-no-blank))))
  1158. (body (concat bullet (when box (concat box " ")) after-bullet
  1159. (and text-cut
  1160. (if (string-match "\\`[ \t]+" text-cut)
  1161. (replace-match "" t t text-cut)
  1162. text-cut))))
  1163. (item-sep (make-string (1+ blank-nb) ?\n))
  1164. (item-size (+ ind-size (length body) (length item-sep)))
  1165. (size-offset (- item-size (length text-cut))))
  1166. ;; 4. Insert effectively item into buffer.
  1167. (goto-char item)
  1168. (org-indent-to-column ind)
  1169. (insert body item-sep)
  1170. ;; 5. Add new item to STRUCT.
  1171. (mapc (lambda (e)
  1172. (let ((p (car e)) (end (nth 6 e)))
  1173. (cond
  1174. ;; Before inserted item, positions don't change but
  1175. ;; an item ending after insertion has its end shifted
  1176. ;; by SIZE-OFFSET.
  1177. ((< p item)
  1178. (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
  1179. ;; Trivial cases where current item isn't split in
  1180. ;; two. Just shift every item after new one by
  1181. ;; ITEM-SIZE.
  1182. ((or beforep (not split-line-p))
  1183. (setcar e (+ p item-size))
  1184. (setcar (nthcdr 6 e) (+ end item-size)))
  1185. ;; Item is split in two: elements before POS are just
  1186. ;; shifted by ITEM-SIZE. In the case item would end
  1187. ;; after split POS, ending is only shifted by
  1188. ;; SIZE-OFFSET.
  1189. ((< p pos)
  1190. (setcar e (+ p item-size))
  1191. (if (< end pos)
  1192. (setcar (nthcdr 6 e) (+ end item-size))
  1193. (setcar (nthcdr 6 e) (+ end size-offset))))
  1194. ;; Elements after POS are moved into new item.
  1195. ;; Length of ITEM-SEP has to be removed as ITEM-SEP
  1196. ;; doesn't appear in buffer yet.
  1197. ((< p item-end)
  1198. (setcar e (+ p size-offset (- item pos (length item-sep))))
  1199. (if (= end item-end)
  1200. (setcar (nthcdr 6 e) (+ item item-size))
  1201. (setcar (nthcdr 6 e)
  1202. (+ end size-offset
  1203. (- item pos (length item-sep))))))
  1204. ;; Elements at ITEM-END or after are only shifted by
  1205. ;; SIZE-OFFSET.
  1206. (t (setcar e (+ p size-offset))
  1207. (setcar (nthcdr 6 e) (+ end size-offset))))))
  1208. struct)
  1209. (push (list item ind bullet nil box nil (+ item item-size)) struct)
  1210. (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
  1211. ;; 6. If not BEFOREP, new item must appear after ITEM, so
  1212. ;; exchange ITEM with the next item in list. Position cursor
  1213. ;; after bullet, counter, checkbox, and label.
  1214. (if beforep
  1215. (goto-char item)
  1216. (setq struct (org-list-swap-items item (+ item item-size) struct))
  1217. (goto-char (org-list-get-next-item
  1218. item struct (org-list-prevs-alist struct))))
  1219. struct)))
  1220. (defun org-list-delete-item (item struct)
  1221. "Remove ITEM from the list and return the new structure.
  1222. STRUCT is the list structure."
  1223. (let* ((end (org-list-get-item-end item struct))
  1224. (beg (if (= (org-list-get-bottom-point struct) end)
  1225. ;; If ITEM ends with the list, delete blank lines
  1226. ;; before it.
  1227. (save-excursion
  1228. (goto-char item)
  1229. (skip-chars-backward " \r\t\n")
  1230. (min (1+ (point-at-eol)) (point-max)))
  1231. item)))
  1232. ;; Remove item from buffer.
  1233. (delete-region beg end)
  1234. ;; Remove item from structure and shift others items accordingly.
  1235. ;; Don't forget to shift also ending position when appropriate.
  1236. (let ((size (- end beg)))
  1237. (delq nil (mapcar (lambda (e)
  1238. (let ((pos (car e)))
  1239. (cond
  1240. ((< pos item)
  1241. (let ((end-e (nth 6 e)))
  1242. (cond
  1243. ((< end-e item) e)
  1244. ((= end-e item)
  1245. (append (butlast e) (list beg)))
  1246. (t
  1247. (append (butlast e) (list (- end-e size)))))))
  1248. ((< pos end) nil)
  1249. (t
  1250. (cons (- pos size)
  1251. (append (butlast (cdr e))
  1252. (list (- (nth 6 e) size))))))))
  1253. struct)))))
  1254. (defun org-list-send-item (item dest struct)
  1255. "Send ITEM to destination DEST.
  1256. STRUCT is the list structure.
  1257. DEST can have various values.
  1258. If DEST is a buffer position, the function will assume it points
  1259. to another item in the same list as ITEM, and will move the
  1260. latter just before the former.
  1261. If DEST is `begin' (respectively `end'), ITEM will be moved at
  1262. the beginning (respectively end) of the list it belongs to.
  1263. If DEST is a string like \"N\", where N is an integer, ITEM will
  1264. be moved at the Nth position in the list.
  1265. If DEST is `kill', ITEM will be deleted and its body will be
  1266. added to the kill-ring.
  1267. If DEST is `delete', ITEM will be deleted.
  1268. Visibility of item is preserved.
  1269. This function returns, destructively, the new list structure."
  1270. (let* ((prevs (org-list-prevs-alist struct))
  1271. (item-end (org-list-get-item-end item struct))
  1272. ;; Grab full item body minus its bullet.
  1273. (body (org-trim
  1274. (buffer-substring
  1275. (save-excursion
  1276. (goto-char item)
  1277. (looking-at
  1278. (concat "[ \t]*"
  1279. (regexp-quote (org-list-get-bullet item struct))))
  1280. (match-end 0))
  1281. item-end)))
  1282. ;; Change DEST into a buffer position. A trick is needed
  1283. ;; when ITEM is meant to be sent at the end of the list.
  1284. ;; Indeed, by setting locally `org-M-RET-may-split-line' to
  1285. ;; nil and insertion point (INS-POINT) to the first line's
  1286. ;; end of the last item, we ensure the new item will be
  1287. ;; inserted after the last item, and not after any of its
  1288. ;; hypothetical sub-items.
  1289. (ins-point (cond
  1290. ((or (eq dest 'kill) (eq dest 'delete)))
  1291. ((eq dest 'begin)
  1292. (setq dest (org-list-get-list-begin item struct prevs)))
  1293. ((eq dest 'end)
  1294. (setq dest (org-list-get-list-end item struct prevs))
  1295. (save-excursion
  1296. (goto-char (org-list-get-last-item item struct prevs))
  1297. (point-at-eol)))
  1298. ((string-match "\\`[0-9]+\\'" dest)
  1299. (let* ((all (org-list-get-all-items item struct prevs))
  1300. (len (length all))
  1301. (index (mod (string-to-number dest) len)))
  1302. (if (not (zerop index))
  1303. (setq dest (nth (1- index) all))
  1304. ;; Send ITEM at the end of the list.
  1305. (setq dest (org-list-get-list-end item struct prevs))
  1306. (save-excursion
  1307. (goto-char
  1308. (org-list-get-last-item item struct prevs))
  1309. (point-at-eol)))))
  1310. (t dest)))
  1311. (org-M-RET-may-split-line nil)
  1312. ;; Store visibility.
  1313. (visibility (overlays-in item item-end)))
  1314. (cond
  1315. ((eq dest 'delete) (org-list-delete-item item struct))
  1316. ((eq dest 'kill)
  1317. (kill-new body)
  1318. (org-list-delete-item item struct))
  1319. ((and (integerp dest) (/= item ins-point))
  1320. (setq item (copy-marker item))
  1321. (setq struct (org-list-insert-item ins-point struct prevs nil body))
  1322. ;; 1. Structure returned by `org-list-insert-item' may not be
  1323. ;; accurate, as it cannot see sub-items included in BODY.
  1324. ;; Thus, first compute the real structure so far.
  1325. (let ((moved-items
  1326. (cons (marker-position item)
  1327. (org-list-get-subtree (marker-position item) struct)))
  1328. (new-end (org-list-get-item-end (point) struct))
  1329. (old-end (org-list-get-item-end (marker-position item) struct))
  1330. (new-item (point))
  1331. (shift (- (point) item)))
  1332. ;; 1.1. Remove the item just created in structure.
  1333. (setq struct (delete (assq new-item struct) struct))
  1334. ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
  1335. (setq struct (sort
  1336. (append
  1337. struct
  1338. (mapcar (lambda (e)
  1339. (let* ((cell (assq e struct))
  1340. (pos (car cell))
  1341. (end (nth 6 cell)))
  1342. (cons (+ pos shift)
  1343. (append (butlast (cdr cell))
  1344. (list (if (= end old-end)
  1345. new-end
  1346. (+ end shift)))))))
  1347. moved-items))
  1348. (lambda (e1 e2) (< (car e1) (car e2))))))
  1349. ;; 2. Restore visibility.
  1350. (mapc (lambda (ov)
  1351. (move-overlay ov
  1352. (+ (overlay-start ov) (- (point) item))
  1353. (+ (overlay-end ov) (- (point) item))))
  1354. visibility)
  1355. ;; 3. Eventually delete extra copy of the item and clean marker.
  1356. (prog1 (org-list-delete-item (marker-position item) struct)
  1357. (move-marker item nil)))
  1358. (t struct))))
  1359. (defun org-list-struct-outdent (start end struct parents)
  1360. "Outdent items between positions START and END.
  1361. STRUCT is the list structure. PARENTS is the alist of items'
  1362. parents, as returned by `org-list-parents-alist'.
  1363. START is included, END excluded."
  1364. (let* (acc
  1365. (out (lambda (cell)
  1366. (let* ((item (car cell))
  1367. (parent (cdr cell)))
  1368. (cond
  1369. ;; Item not yet in zone: keep association.
  1370. ((< item start) cell)
  1371. ;; Item out of zone: follow associations in ACC.
  1372. ((>= item end)
  1373. (let ((convert (and parent (assq parent acc))))
  1374. (if convert (cons item (cdr convert)) cell)))
  1375. ;; Item has no parent: error
  1376. ((not parent)
  1377. (error "Cannot outdent top-level items"))
  1378. ;; Parent is outdented: keep association.
  1379. ((>= parent start)
  1380. (push (cons parent item) acc) cell)
  1381. (t
  1382. ;; Parent isn't outdented: reparent to grand-parent.
  1383. (let ((grand-parent (org-list-get-parent
  1384. parent struct parents)))
  1385. (push (cons parent item) acc)
  1386. (cons item grand-parent))))))))
  1387. (mapcar out parents)))
  1388. (defun org-list-struct-indent (start end struct parents prevs)
  1389. "Indent items between positions START and END.
  1390. STRUCT is the list structure. PARENTS is the alist of parents
  1391. and PREVS is the alist of previous items, returned by,
  1392. respectively, `org-list-parents-alist' and
  1393. `org-list-prevs-alist'.
  1394. START is included and END excluded.
  1395. STRUCT may be modified if `org-list-demote-modify-bullet' matches
  1396. bullets between START and END."
  1397. (let* (acc
  1398. (set-assoc (lambda (cell) (push cell acc) cell))
  1399. (change-bullet-maybe
  1400. (function
  1401. (lambda (item)
  1402. (let ((new-bul-p
  1403. (cdr (assoc
  1404. ;; Normalize ordered bullets.
  1405. (let ((bul (org-trim
  1406. (org-list-get-bullet item struct))))
  1407. (cond ((string-match "[A-Z]\\." bul) "A.")
  1408. ((string-match "[A-Z])" bul) "A)")
  1409. ((string-match "[a-z]\\." bul) "a.")
  1410. ((string-match "[a-z])" bul) "a)")
  1411. ((string-match "[0-9]\\." bul) "1.")
  1412. ((string-match "[0-9])" bul) "1)")
  1413. (t bul)))
  1414. org-list-demote-modify-bullet))))
  1415. (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
  1416. (ind
  1417. (lambda (cell)
  1418. (let* ((item (car cell))
  1419. (parent (cdr cell)))
  1420. (cond
  1421. ;; Item not yet in zone: keep association.
  1422. ((< item start) cell)
  1423. ((>= item end)
  1424. ;; Item out of zone: follow associations in ACC.
  1425. (let ((convert (assq parent acc)))
  1426. (if convert (cons item (cdr convert)) cell)))
  1427. (t
  1428. ;; Item is in zone...
  1429. (let ((prev (org-list-get-prev-item item struct prevs)))
  1430. ;; Check if bullet needs to be changed.
  1431. (funcall change-bullet-maybe item)
  1432. (cond
  1433. ;; First item indented but not parent: error
  1434. ((and (not prev) (< parent start))
  1435. (error "Cannot indent the first item of a list"))
  1436. ;; First item and parent indented: keep same
  1437. ;; parent.
  1438. ((not prev) (funcall set-assoc cell))
  1439. ;; Previous item not indented: reparent to it.
  1440. ((< prev start) (funcall set-assoc (cons item prev)))
  1441. ;; Previous item indented: reparent like it.
  1442. (t
  1443. (funcall set-assoc
  1444. (cons item (cdr (assq prev acc)))))))))))))
  1445. (mapcar ind parents)))
  1446. ;;; Repairing structures
  1447. (defun org-list-use-alpha-bul-p (first struct prevs)
  1448. "Non-nil if list starting at FIRST can have alphabetical bullets.
  1449. STRUCT is list structure. PREVS is the alist of previous items,
  1450. as returned by `org-list-prevs-alist'."
  1451. (and org-alphabetical-lists
  1452. (catch 'exit
  1453. (let ((item first) (ascii 64) (case-fold-search nil))
  1454. ;; Pretend that bullets are uppercase and check if alphabet
  1455. ;; is sufficient, taking counters into account.
  1456. (while item
  1457. (let ((bul (org-list-get-bullet item struct))
  1458. (count (org-list-get-counter item struct)))
  1459. ;; Virtually determine current bullet
  1460. (if (and count (string-match "[a-zA-Z]" count))
  1461. ;; Counters are not case-sensitive.
  1462. (setq ascii (string-to-char (upcase count)))
  1463. (setq ascii (1+ ascii)))
  1464. ;; Test if bullet would be over z or Z.
  1465. (if (> ascii 90)
  1466. (throw 'exit nil)
  1467. (setq item (org-list-get-next-item item struct prevs)))))
  1468. ;; All items checked. All good.
  1469. t))))
  1470. (defun org-list-inc-bullet-maybe (bullet)
  1471. "Increment BULLET if applicable."
  1472. (let ((case-fold-search nil))
  1473. (cond
  1474. ;; Num bullet: increment it.
  1475. ((string-match "[0-9]+" bullet)
  1476. (replace-match
  1477. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1478. nil nil bullet))
  1479. ;; Alpha bullet: increment it.
  1480. ((string-match "[A-Za-z]" bullet)
  1481. (replace-match
  1482. (char-to-string (1+ (string-to-char (match-string 0 bullet))))
  1483. nil nil bullet))
  1484. ;; Unordered bullet: leave it.
  1485. (t bullet))))
  1486. (defun org-list-struct-fix-bul (struct prevs)
  1487. "Verify and correct bullets in STRUCT.
  1488. PREVS is the alist of previous items, as returned by
  1489. `org-list-prevs-alist'.
  1490. This function modifies STRUCT."
  1491. (let ((case-fold-search nil)
  1492. (fix-bul
  1493. (function
  1494. ;; Set bullet of ITEM in STRUCT, depending on the type of
  1495. ;; first item of the list, the previous bullet and counter
  1496. ;; if any.
  1497. (lambda (item)
  1498. (let* ((prev (org-list-get-prev-item item struct prevs))
  1499. (prev-bul (and prev (org-list-get-bullet prev struct)))
  1500. (counter (org-list-get-counter item struct))
  1501. (bullet (org-list-get-bullet item struct))
  1502. (alphap (and (not prev)
  1503. (org-list-use-alpha-bul-p item struct prevs))))
  1504. (org-list-set-bullet
  1505. item struct
  1506. (org-list-bullet-string
  1507. (cond
  1508. ;; Alpha counter in alpha list: use counter.
  1509. ((and prev counter
  1510. (string-match "[a-zA-Z]" counter)
  1511. (string-match "[a-zA-Z]" prev-bul))
  1512. ;; Use cond to be sure `string-match' is used in
  1513. ;; both cases.
  1514. (let ((real-count
  1515. (cond
  1516. ((string-match "[a-z]" prev-bul) (downcase counter))
  1517. ((string-match "[A-Z]" prev-bul) (upcase counter)))))
  1518. (replace-match real-count nil nil prev-bul)))
  1519. ;; Num counter in a num list: use counter.
  1520. ((and prev counter
  1521. (string-match "[0-9]+" counter)
  1522. (string-match "[0-9]+" prev-bul))
  1523. (replace-match counter nil nil prev-bul))
  1524. ;; No counter: increase, if needed, previous bullet.
  1525. (prev
  1526. (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
  1527. ;; Alpha counter at first item: use counter.
  1528. ((and counter (org-list-use-alpha-bul-p item struct prevs)
  1529. (string-match "[A-Za-z]" counter)
  1530. (string-match "[A-Za-z]" bullet))
  1531. (let ((real-count
  1532. (cond
  1533. ((string-match "[a-z]" bullet) (downcase counter))
  1534. ((string-match "[A-Z]" bullet) (upcase counter)))))
  1535. (replace-match real-count nil nil bullet)))
  1536. ;; Num counter at first item: use counter.
  1537. ((and counter
  1538. (string-match "[0-9]+" counter)
  1539. (string-match "[0-9]+" bullet))
  1540. (replace-match counter nil nil bullet))
  1541. ;; First bullet is alpha uppercase: use "A".
  1542. ((and alphap (string-match "[A-Z]" bullet))
  1543. (replace-match "A" nil nil bullet))
  1544. ;; First bullet is alpha lowercase: use "a".
  1545. ((and alphap (string-match "[a-z]" bullet))
  1546. (replace-match "a" nil nil bullet))
  1547. ;; First bullet is num: use "1".
  1548. ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
  1549. (replace-match "1" nil nil bullet))
  1550. ;; Not an ordered list: keep bullet.
  1551. (t bullet)))))))))
  1552. (mapc fix-bul (mapcar 'car struct))))
  1553. (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
  1554. "Verify and correct indentation in STRUCT.
  1555. PARENTS is the alist of parents, as returned by
  1556. `org-list-parents-alist'.
  1557. If numeric optional argument BULLET-SIZE is set, assume all
  1558. bullets in list have this length to determine new indentation.
  1559. This function modifies STRUCT."
  1560. (let* ((ancestor (org-list-get-top-point struct))
  1561. (top-ind (org-list-get-ind ancestor struct))
  1562. (new-ind
  1563. (lambda (item)
  1564. (let ((parent (org-list-get-parent item struct parents)))
  1565. (if parent
  1566. ;; Indent like parent + length of parent's bullet +
  1567. ;; sub-list offset.
  1568. (org-list-set-ind
  1569. item struct (+ (or bullet-size
  1570. (length
  1571. (org-list-get-bullet parent struct)))
  1572. (org-list-get-ind parent struct)
  1573. org-list-indent-offset))
  1574. ;; If no parent, indent like top-point.
  1575. (org-list-set-ind item struct top-ind))))))
  1576. (mapc new-ind (mapcar 'car (cdr struct)))))
  1577. (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
  1578. "Verify and correct checkboxes in STRUCT.
  1579. PARENTS is the alist of parents and PREVS is the alist of
  1580. previous items, as returned by, respectively,
  1581. `org-list-parents-alist' and `org-list-prevs-alist'.
  1582. If ORDERED is non-nil, a checkbox can only be checked when every
  1583. checkbox before it is checked too. If there was an attempt to
  1584. break this rule, the function will return the blocking item. In
  1585. all others cases, the return value will be nil.
  1586. This function modifies STRUCT."
  1587. (let ((all-items (mapcar 'car struct))
  1588. (set-parent-box
  1589. (function
  1590. (lambda (item)
  1591. (let* ((box-list
  1592. (mapcar (lambda (child)
  1593. (org-list-get-checkbox child struct))
  1594. (org-list-get-children item struct parents))))
  1595. (org-list-set-checkbox
  1596. item struct
  1597. (cond
  1598. ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
  1599. ((member "[-]" box-list) "[-]")
  1600. ((member "[X]" box-list) "[X]")
  1601. ((member "[ ]" box-list) "[ ]")
  1602. ;; Parent has no boxed child: leave box as-is.
  1603. (t (org-list-get-checkbox item struct))))))))
  1604. parent-list)
  1605. ;; 1. List all parents with a checkbox.
  1606. (mapc
  1607. (lambda (e)
  1608. (let* ((parent (org-list-get-parent e struct parents))
  1609. (parent-box-p (org-list-get-checkbox parent struct)))
  1610. (when (and parent-box-p (not (memq parent parent-list)))
  1611. (push parent parent-list))))
  1612. all-items)
  1613. ;; 2. Sort those parents by decreasing indentation.
  1614. (setq parent-list (sort parent-list
  1615. (lambda (e1 e2)
  1616. (> (org-list-get-ind e1 struct)
  1617. (org-list-get-ind e2 struct)))))
  1618. ;; 3. For each parent, get all children's checkboxes to determine
  1619. ;; and set its checkbox accordingly.
  1620. (mapc set-parent-box parent-list)
  1621. ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
  1622. (when ordered
  1623. (let* ((box-list
  1624. (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
  1625. (after-unchecked (member "[ ]" box-list)))
  1626. ;; There are boxes checked after an unchecked one: fix that.
  1627. (when (member "[X]" after-unchecked)
  1628. (let ((index (- (length struct) (length after-unchecked))))
  1629. (mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
  1630. (nthcdr index all-items))
  1631. ;; Verify once again the structure, without ORDERED.
  1632. (org-list-struct-fix-box struct parents prevs nil)
  1633. ;; Return blocking item.
  1634. (nth index all-items)))))))
  1635. (defun org-list-struct-fix-item-end (struct)
  1636. "Verify and correct each item end position in STRUCT.
  1637. This function modifies STRUCT."
  1638. (let (end-list acc-end)
  1639. (mapc (lambda (e)
  1640. (let* ((pos (car e))
  1641. (ind-pos (org-list-get-ind pos struct))
  1642. (end-pos (org-list-get-item-end pos struct)))
  1643. (unless (assq end-pos struct)
  1644. ;; To determine real ind of an ending position that is
  1645. ;; not at an item, we have to find the item it belongs
  1646. ;; to: it is the last item (ITEM-UP), whose ending is
  1647. ;; further than the position we're interested in.
  1648. (let ((item-up (assoc-default end-pos acc-end '>)))
  1649. (push (cons
  1650. ;; Else part is for the bottom point.
  1651. (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
  1652. end-pos)
  1653. end-list)))
  1654. (push (cons ind-pos pos) end-list)
  1655. (push (cons end-pos pos) acc-end)))
  1656. struct)
  1657. (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
  1658. (org-list-struct-assoc-end struct end-list)))
  1659. (defun org-list-struct-apply-struct (struct old-struct)
  1660. "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
  1661. OLD-STRUCT is the structure before any modifications, and STRUCT
  1662. the structure to be applied. The function will only modify parts
  1663. of the list which have changed.
  1664. Initial position of cursor is restored after the changes."
  1665. (let* ((origin (point-marker))
  1666. (inlinetask-re (and (featurep 'org-inlinetask)
  1667. (org-inlinetask-outline-regexp)))
  1668. (item-re (org-item-re))
  1669. (shift-body-ind
  1670. (function
  1671. ;; Shift the indentation between END and BEG by DELTA.
  1672. ;; Start from the line before END.
  1673. (lambda (end beg delta)
  1674. (goto-char end)
  1675. (skip-chars-backward " \r\t\n")
  1676. (beginning-of-line)
  1677. (while (or (> (point) beg)
  1678. (and (= (point) beg)
  1679. (not (looking-at item-re))))
  1680. (cond
  1681. ;; Skip inline tasks.
  1682. ((and inlinetask-re (looking-at inlinetask-re))
  1683. (org-inlinetask-goto-beginning))
  1684. ;; Shift only non-empty lines.
  1685. ((org-looking-at-p "^[ \t]*\\S-")
  1686. (let ((i (org-get-indentation)))
  1687. (org-indent-line-to (+ i delta)))))
  1688. (forward-line -1)))))
  1689. (modify-item
  1690. (function
  1691. ;; Replace ITEM first line elements with new elements from
  1692. ;; STRUCT, if appropriate.
  1693. (lambda (item)
  1694. (goto-char item)
  1695. (let* ((new-ind (org-list-get-ind item struct))
  1696. (old-ind (org-get-indentation))
  1697. (new-bul (org-list-bullet-string
  1698. (org-list-get-bullet item struct)))
  1699. (old-bul (org-list-get-bullet item old-struct))
  1700. (new-box (org-list-get-checkbox item struct)))
  1701. (looking-at org-list-full-item-re)
  1702. ;; a. Replace bullet
  1703. (unless (equal old-bul new-bul)
  1704. (replace-match new-bul nil nil nil 1))
  1705. ;; b. Replace checkbox.
  1706. (cond
  1707. ((equal (match-string 3) new-box))
  1708. ((and (match-string 3) new-box)
  1709. (replace-match new-box nil nil nil 3))
  1710. ((match-string 3)
  1711. (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
  1712. (replace-match "" nil nil nil 1))
  1713. (t (let ((counterp (match-end 2)))
  1714. (goto-char (if counterp (1+ counterp) (match-end 1)))
  1715. (insert (concat new-box (unless counterp " "))))))
  1716. ;; c. Indent item to appropriate column.
  1717. (unless (= new-ind old-ind)
  1718. (delete-region (goto-char (point-at-bol))
  1719. (progn (skip-chars-forward " \t") (point)))
  1720. (indent-to new-ind)))))))
  1721. ;; 1. First get list of items and position endings. We maintain
  1722. ;; two alists: ITM-SHIFT, determining indentation shift needed
  1723. ;; at item, and END-POS, a pseudo-alist where key is ending
  1724. ;; position and value point.
  1725. (let (end-list acc-end itm-shift all-ends sliced-struct)
  1726. (mapc (lambda (e)
  1727. (let* ((pos (car e))
  1728. (ind-pos (org-list-get-ind pos struct))
  1729. (ind-old (org-list-get-ind pos old-struct))
  1730. (bul-pos (org-list-get-bullet pos struct))
  1731. (bul-old (org-list-get-bullet pos old-struct))
  1732. (ind-shift (- (+ ind-pos (length bul-pos))
  1733. (+ ind-old (length bul-old))))
  1734. (end-pos (org-list-get-item-end pos old-struct)))
  1735. (push (cons pos ind-shift) itm-shift)
  1736. (unless (assq end-pos old-struct)
  1737. ;; To determine real ind of an ending position that
  1738. ;; is not at an item, we have to find the item it
  1739. ;; belongs to: it is the last item (ITEM-UP), whose
  1740. ;; ending is further than the position we're
  1741. ;; interested in.
  1742. (let ((item-up (assoc-default end-pos acc-end '>)))
  1743. (push (cons end-pos item-up) end-list)))
  1744. (push (cons end-pos pos) acc-end)))
  1745. old-struct)
  1746. ;; 2. Slice the items into parts that should be shifted by the
  1747. ;; same amount of indentation. The slices are returned in
  1748. ;; reverse order so changes modifying buffer do not change
  1749. ;; positions they refer to.
  1750. (setq all-ends (sort (append (mapcar 'car itm-shift)
  1751. (org-uniquify (mapcar 'car end-list)))
  1752. '<))
  1753. (while (cdr all-ends)
  1754. (let* ((up (pop all-ends))
  1755. (down (car all-ends))
  1756. (ind (if (assq up struct)
  1757. (cdr (assq up itm-shift))
  1758. (cdr (assq (cdr (assq up end-list)) itm-shift)))))
  1759. (push (list down up ind) sliced-struct)))
  1760. ;; 3. Shift each slice in buffer, provided delta isn't 0, from
  1761. ;; end to beginning. Take a special action when beginning is
  1762. ;; at item bullet.
  1763. (mapc (lambda (e)
  1764. (unless (zerop (nth 2 e)) (apply shift-body-ind e))
  1765. (let* ((beg (nth 1 e))
  1766. (cell (assq beg struct)))
  1767. (unless (or (not cell) (equal cell (assq beg old-struct)))
  1768. (funcall modify-item beg))))
  1769. sliced-struct))
  1770. ;; 4. Go back to initial position and clean marker.
  1771. (goto-char origin)
  1772. (move-marker origin nil)))
  1773. (defun org-list-write-struct (struct parents &optional old-struct)
  1774. "Correct bullets, checkboxes and indentation in list at point.
  1775. STRUCT is the list structure. PARENTS is the alist of parents,
  1776. as returned by `org-list-parents-alist'.
  1777. When non-nil, optional argument OLD-STRUCT is the reference
  1778. structure of the list. It should be provided whenever STRUCT
  1779. doesn't correspond anymore to the real list in buffer."
  1780. ;; Order of functions matters here: checkboxes and endings need
  1781. ;; correct indentation to be set, and indentation needs correct
  1782. ;; bullets.
  1783. ;;
  1784. ;; 0. Save a copy of structure before modifications
  1785. (let ((old-struct (or old-struct (copy-tree struct))))
  1786. ;; 1. Set a temporary, but coherent with PARENTS, indentation in
  1787. ;; order to get items endings and bullets properly
  1788. (org-list-struct-fix-ind struct parents 2)
  1789. ;; 2. Fix each item end to get correct prevs alist.
  1790. (org-list-struct-fix-item-end struct)
  1791. ;; 3. Get bullets right.
  1792. (let ((prevs (org-list-prevs-alist struct)))
  1793. (org-list-struct-fix-bul struct prevs)
  1794. ;; 4. Now get real indentation.
  1795. (org-list-struct-fix-ind struct parents)
  1796. ;; 5. Eventually fix checkboxes.
  1797. (org-list-struct-fix-box struct parents prevs))
  1798. ;; 6. Apply structure modifications to buffer.
  1799. (org-list-struct-apply-struct struct old-struct)))
  1800. ;;; Misc Tools
  1801. (defun org-apply-on-list (function init-value &rest args)
  1802. "Call FUNCTION on each item of the list at point.
  1803. FUNCTION must be called with at least one argument: INIT-VALUE,
  1804. that will contain the value returned by the function at the
  1805. previous item, plus ARGS extra arguments.
  1806. FUNCTION is applied on items in reverse order.
  1807. As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
  1808. will return the number of items in the current list.
  1809. Sublists of the list are skipped. Cursor is always at the
  1810. beginning of the item."
  1811. (let* ((struct (org-list-struct))
  1812. (prevs (org-list-prevs-alist struct))
  1813. (item (copy-marker (point-at-bol)))
  1814. (all (org-list-get-all-items (marker-position item) struct prevs))
  1815. (value init-value))
  1816. (mapc (lambda (e)
  1817. (goto-char e)
  1818. (setq value (apply function value args)))
  1819. (nreverse all))
  1820. (goto-char item)
  1821. (move-marker item nil)
  1822. value))
  1823. (defun org-list-set-item-visibility (item struct view)
  1824. "Set visibility of ITEM in STRUCT to VIEW.
  1825. Possible values are: `folded', `children' or `subtree'. See
  1826. `org-cycle' for more information."
  1827. (cond
  1828. ((eq view 'folded)
  1829. (let ((item-end (org-list-get-item-end-before-blank item struct)))
  1830. ;; Hide from eol
  1831. (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
  1832. item-end t)))
  1833. ((eq view 'children)
  1834. ;; First show everything.
  1835. (org-list-set-item-visibility item struct 'subtree)
  1836. ;; Then fold every child.
  1837. (let* ((parents (org-list-parents-alist struct))
  1838. (children (org-list-get-children item struct parents)))
  1839. (mapc (lambda (e)
  1840. (org-list-set-item-visibility e struct 'folded))
  1841. children)))
  1842. ((eq view 'subtree)
  1843. ;; Show everything
  1844. (let ((item-end (org-list-get-item-end item struct)))
  1845. (outline-flag-region item item-end nil)))))
  1846. (defun org-list-item-body-column (item)
  1847. "Return column at which body of ITEM should start."
  1848. (let (bpos bcol tpos tcol)
  1849. (save-excursion
  1850. (goto-char item)
  1851. (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?\\([ \t]+\\|$\\)")
  1852. (setq bpos (match-beginning 1) tpos (match-end 0)
  1853. bcol (progn (goto-char bpos) (current-column))
  1854. tcol (progn (goto-char tpos) (current-column)))
  1855. (when (> tcol (+ bcol org-description-max-indent))
  1856. (setq tcol (+ bcol 5))))
  1857. tcol))
  1858. ;;; Interactive functions
  1859. (defalias 'org-list-get-item-begin 'org-in-item-p)
  1860. (defun org-beginning-of-item ()
  1861. "Go to the beginning of the current item.
  1862. Throw an error when not in a list."
  1863. (interactive)
  1864. (let ((begin (org-in-item-p)))
  1865. (if begin (goto-char begin) (error "Not in an item"))))
  1866. (defun org-beginning-of-item-list ()
  1867. "Go to the beginning item of the current list or sublist.
  1868. Throw an error when not in a list."
  1869. (interactive)
  1870. (let ((begin (org-in-item-p)))
  1871. (if (not begin)
  1872. (error "Not in an item")
  1873. (goto-char begin)
  1874. (let* ((struct (org-list-struct))
  1875. (prevs (org-list-prevs-alist struct)))
  1876. (goto-char (org-list-get-list-begin begin struct prevs))))))
  1877. (defun org-end-of-item-list ()
  1878. "Go to the end of the current list or sublist.
  1879. Throw an error when not in a list."
  1880. (interactive)
  1881. (let ((begin (org-in-item-p)))
  1882. (if (not begin)
  1883. (error "Not in an item")
  1884. (goto-char begin)
  1885. (let* ((struct (org-list-struct))
  1886. (prevs (org-list-prevs-alist struct)))
  1887. (goto-char (org-list-get-list-end begin struct prevs))))))
  1888. (defun org-end-of-item ()
  1889. "Go to the end of the current item.
  1890. Throw an error when not in a list."
  1891. (interactive)
  1892. (let ((begin (org-in-item-p)))
  1893. (if (not begin)
  1894. (error "Not in an item")
  1895. (goto-char begin)
  1896. (let ((struct (org-list-struct)))
  1897. (goto-char (org-list-get-item-end begin struct))))))
  1898. (defun org-previous-item ()
  1899. "Move to the beginning of the previous item.
  1900. Throw an error when not in a list. Also throw an error when at
  1901. first item, unless `org-list-use-circular-motion' is non-nil."
  1902. (interactive)
  1903. (let ((item (org-in-item-p)))
  1904. (if (not item)
  1905. (error "Not in an item")
  1906. (goto-char item)
  1907. (let* ((struct (org-list-struct))
  1908. (prevs (org-list-prevs-alist struct))
  1909. (prevp (org-list-get-prev-item item struct prevs)))
  1910. (cond
  1911. (prevp (goto-char prevp))
  1912. (org-list-use-circular-motion
  1913. (goto-char (org-list-get-last-item item struct prevs)))
  1914. (t (error "On first item")))))))
  1915. (defun org-next-item ()
  1916. "Move to the beginning of the next item.
  1917. Throw an error when not in a list. Also throw an error when at
  1918. last item, unless `org-list-use-circular-motion' is non-nil."
  1919. (interactive)
  1920. (let ((item (org-in-item-p)))
  1921. (if (not item)
  1922. (error "Not in an item")
  1923. (goto-char item)
  1924. (let* ((struct (org-list-struct))
  1925. (prevs (org-list-prevs-alist struct))
  1926. (prevp (org-list-get-next-item item struct prevs)))
  1927. (cond
  1928. (prevp (goto-char prevp))
  1929. (org-list-use-circular-motion
  1930. (goto-char (org-list-get-first-item item struct prevs)))
  1931. (t (error "On last item")))))))
  1932. (defun org-move-item-down ()
  1933. "Move the item at point down, i.e. swap with following item.
  1934. Sub-items (items with larger indentation) are considered part of
  1935. the item, so this really moves item trees."
  1936. (interactive)
  1937. (unless (org-at-item-p) (error "Not at an item"))
  1938. (let* ((col (current-column))
  1939. (item (point-at-bol))
  1940. (struct (org-list-struct))
  1941. (prevs (org-list-prevs-alist struct))
  1942. (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
  1943. (unless (or next-item org-list-use-circular-motion)
  1944. (error "Cannot move this item further down"))
  1945. (if (not next-item)
  1946. (setq struct (org-list-send-item item 'begin struct))
  1947. (setq struct (org-list-swap-items item next-item struct))
  1948. (goto-char
  1949. (org-list-get-next-item item struct (org-list-prevs-alist struct))))
  1950. (org-list-write-struct struct (org-list-parents-alist struct))
  1951. (org-move-to-column col)))
  1952. (defun org-move-item-up ()
  1953. "Move the item at point up, i.e. swap with previous item.
  1954. Sub-items (items with larger indentation) are considered part of
  1955. the item, so this really moves item trees."
  1956. (interactive)
  1957. (unless (org-at-item-p) (error "Not at an item"))
  1958. (let* ((col (current-column))
  1959. (item (point-at-bol))
  1960. (struct (org-list-struct))
  1961. (prevs (org-list-prevs-alist struct))
  1962. (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
  1963. (unless (or prev-item org-list-use-circular-motion)
  1964. (error "Cannot move this item further up"))
  1965. (if (not prev-item)
  1966. (setq struct (org-list-send-item item 'end struct))
  1967. (setq struct (org-list-swap-items prev-item item struct)))
  1968. (org-list-write-struct struct (org-list-parents-alist struct))
  1969. (org-move-to-column col)))
  1970. (defun org-insert-item (&optional checkbox)
  1971. "Insert a new item at the current level.
  1972. If cursor is before first character after bullet of the item, the
  1973. new item will be created before the current one.
  1974. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  1975. Return t when things worked, nil when we are not in an item, or
  1976. item is invisible."
  1977. (let ((itemp (org-in-item-p))
  1978. (pos (point)))
  1979. ;; If cursor isn't is a list or if list is invisible, return nil.
  1980. (unless (or (not itemp)
  1981. (save-excursion
  1982. (goto-char itemp)
  1983. (outline-invisible-p)))
  1984. (if (save-excursion
  1985. (goto-char itemp)
  1986. (org-at-item-timer-p))
  1987. ;; Timer list: delegate to `org-timer-item'.
  1988. (progn (org-timer-item) t)
  1989. (let* ((struct (save-excursion (goto-char itemp)
  1990. (org-list-struct)))
  1991. (prevs (org-list-prevs-alist struct))
  1992. ;; If we're in a description list, ask for the new term.
  1993. (desc (when (eq (org-list-get-list-type itemp struct prevs)
  1994. 'descriptive)
  1995. (concat (read-string "Term: ") " :: "))))
  1996. (setq struct
  1997. (org-list-insert-item pos struct prevs checkbox desc))
  1998. (org-list-write-struct struct (org-list-parents-alist struct))
  1999. (when checkbox (org-update-checkbox-count-maybe))
  2000. (looking-at org-list-full-item-re)
  2001. (goto-char (if (and (match-beginning 4)
  2002. (save-match-data
  2003. (string-match "[.)]" (match-string 1))))
  2004. (match-beginning 4)
  2005. (match-end 0)))
  2006. t)))))
  2007. (defun org-list-repair ()
  2008. "Fix indentation, bullets and checkboxes is the list at point."
  2009. (interactive)
  2010. (unless (org-at-item-p) (error "This is not a list"))
  2011. (let* ((struct (org-list-struct))
  2012. (parents (org-list-parents-alist struct)))
  2013. (org-list-write-struct struct parents)))
  2014. (defun org-cycle-list-bullet (&optional which)
  2015. "Cycle through the different itemize/enumerate bullets.
  2016. This cycle the entire list level through the sequence:
  2017. `-' -> `+' -> `*' -> `1.' -> `1)'
  2018. If WHICH is a valid string, use that as the new bullet. If WHICH
  2019. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  2020. `previous', cycle backwards."
  2021. (interactive "P")
  2022. (unless (org-at-item-p) (error "Not at an item"))
  2023. (save-excursion
  2024. (beginning-of-line)
  2025. (let* ((struct (org-list-struct))
  2026. (parents (org-list-parents-alist struct))
  2027. (prevs (org-list-prevs-alist struct))
  2028. (list-beg (org-list-get-first-item (point) struct prevs))
  2029. (bullet (org-list-get-bullet list-beg struct))
  2030. (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
  2031. (case-fold-search nil)
  2032. (current (cond
  2033. ((string-match "[a-z]\\." bullet) "a.")
  2034. ((string-match "[a-z])" bullet) "a)")
  2035. ((string-match "[A-Z]\\." bullet) "A.")
  2036. ((string-match "[A-Z])" bullet) "A)")
  2037. ((string-match "\\." bullet) "1.")
  2038. ((string-match ")" bullet) "1)")
  2039. (t (org-trim bullet))))
  2040. ;; Compute list of possible bullets, depending on context.
  2041. (bullet-list
  2042. (append '("-" "+" )
  2043. ;; *-bullets are not allowed at column 0.
  2044. (unless (looking-at "\\S-") '("*"))
  2045. ;; Description items cannot be numbered.
  2046. (unless (or (eq org-plain-list-ordered-item-terminator ?\))
  2047. (org-at-item-description-p))
  2048. '("1."))
  2049. (unless (or (eq org-plain-list-ordered-item-terminator ?.)
  2050. (org-at-item-description-p))
  2051. '("1)"))
  2052. (unless (or (not alpha-p)
  2053. (eq org-plain-list-ordered-item-terminator ?\))
  2054. (org-at-item-description-p))
  2055. '("a." "A."))
  2056. (unless (or (not alpha-p)
  2057. (eq org-plain-list-ordered-item-terminator ?.)
  2058. (org-at-item-description-p))
  2059. '("a)" "A)"))))
  2060. (len (length bullet-list))
  2061. (item-index (- len (length (member current bullet-list))))
  2062. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  2063. (new (cond
  2064. ((member which bullet-list) which)
  2065. ((numberp which) (funcall get-value which))
  2066. ((eq 'previous which) (funcall get-value (1- item-index)))
  2067. (t (funcall get-value (1+ item-index))))))
  2068. ;; Use a short variation of `org-list-write-struct' as there's
  2069. ;; no need to go through all the steps.
  2070. (let ((old-struct (copy-tree struct)))
  2071. (org-list-set-bullet list-beg struct (org-list-bullet-string new))
  2072. (org-list-struct-fix-bul struct prevs)
  2073. (org-list-struct-fix-ind struct parents)
  2074. (org-list-struct-apply-struct struct old-struct)))))
  2075. (defun org-toggle-checkbox (&optional toggle-presence)
  2076. "Toggle the checkbox in the current line.
  2077. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  2078. double prefix, set checkbox to [-].
  2079. When there is an active region, toggle status or presence of the
  2080. first checkbox there, and make every item inside have the same
  2081. status or presence, respectively.
  2082. If the cursor is in a headline, apply this to all checkbox items
  2083. in the text below the heading, taking as reference the first item
  2084. in subtree, ignoring drawers."
  2085. (interactive "P")
  2086. (save-excursion
  2087. (let* (singlep
  2088. block-item
  2089. lim-up
  2090. lim-down
  2091. (drawer-re (concat "^[ \t]*:\\("
  2092. (mapconcat 'regexp-quote org-drawers "\\|")
  2093. "\\):[ \t]*$"))
  2094. (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
  2095. "\\|" org-deadline-string
  2096. "\\|" org-closed-string
  2097. "\\|" org-clock-string "\\)"
  2098. " *[[<]\\([^]>]+\\)[]>]"))
  2099. (orderedp (org-entry-get nil "ORDERED"))
  2100. (bounds
  2101. ;; In a region, start at first item in region.
  2102. (cond
  2103. ((org-region-active-p)
  2104. (let ((limit (region-end)))
  2105. (goto-char (region-beginning))
  2106. (if (org-list-search-forward (org-item-beginning-re) limit t)
  2107. (setq lim-up (point-at-bol))
  2108. (error "No item in region"))
  2109. (setq lim-down (copy-marker limit))))
  2110. ((org-at-heading-p)
  2111. ;; On an heading, start at first item after drawers and
  2112. ;; time-stamps (scheduled, etc.).
  2113. (let ((limit (save-excursion (outline-next-heading) (point))))
  2114. (forward-line 1)
  2115. (while (or (looking-at drawer-re) (looking-at keyword-re))
  2116. (if (looking-at keyword-re)
  2117. (forward-line 1)
  2118. (re-search-forward "^[ \t]*:END:" limit nil)))
  2119. (if (org-list-search-forward (org-item-beginning-re) limit t)
  2120. (setq lim-up (point-at-bol))
  2121. (error "No item in subtree"))
  2122. (setq lim-down (copy-marker limit))))
  2123. ;; Just one item: set SINGLEP flag.
  2124. ((org-at-item-p)
  2125. (setq singlep t)
  2126. (setq lim-up (point-at-bol)
  2127. lim-down (copy-marker (point-at-eol))))
  2128. (t (error "Not at an item or heading, and no active region"))))
  2129. ;; Determine the checkbox going to be applied to all items
  2130. ;; within bounds.
  2131. (ref-checkbox
  2132. (progn
  2133. (goto-char lim-up)
  2134. (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
  2135. (cond
  2136. ((equal toggle-presence '(16)) "[-]")
  2137. ((equal toggle-presence '(4))
  2138. (unless cbox "[ ]"))
  2139. ((equal "[X]" cbox) "[ ]")
  2140. (t "[X]"))))))
  2141. ;; When an item is found within bounds, grab the full list at
  2142. ;; point structure, then: (1) set check-box of all its items
  2143. ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
  2144. ;; whole list, (3) move point after the list.
  2145. (goto-char lim-up)
  2146. (while (and (< (point) lim-down)
  2147. (org-list-search-forward (org-item-beginning-re)
  2148. lim-down 'move))
  2149. (let* ((struct (org-list-struct))
  2150. (struct-copy (copy-tree struct))
  2151. (parents (org-list-parents-alist struct))
  2152. (prevs (org-list-prevs-alist struct))
  2153. (bottom (copy-marker (org-list-get-bottom-point struct)))
  2154. (items-to-toggle (org-remove-if
  2155. (lambda (e) (or (< e lim-up) (> e lim-down)))
  2156. (mapcar 'car struct))))
  2157. (mapc (lambda (e) (org-list-set-checkbox
  2158. e struct
  2159. ;; If there is no box at item, leave as-is
  2160. ;; unless function was called with C-u prefix.
  2161. (let ((cur-box (org-list-get-checkbox e struct)))
  2162. (if (or cur-box (equal toggle-presence '(4)))
  2163. ref-checkbox
  2164. cur-box))))
  2165. items-to-toggle)
  2166. (setq block-item (org-list-struct-fix-box
  2167. struct parents prevs orderedp))
  2168. ;; Report some problems due to ORDERED status of subtree.
  2169. ;; If only one box was being checked, throw an error, else,
  2170. ;; only signal problems.
  2171. (cond
  2172. ((and singlep block-item (> lim-up block-item))
  2173. (error
  2174. "Checkbox blocked because of unchecked box at line %d"
  2175. (org-current-line block-item)))
  2176. (block-item
  2177. (message
  2178. "Checkboxes were removed due to unchecked box at line %d"
  2179. (org-current-line block-item))))
  2180. (goto-char bottom)
  2181. (move-marker bottom nil)
  2182. (org-list-struct-apply-struct struct struct-copy)))
  2183. (move-marker lim-down nil)))
  2184. (org-update-checkbox-count-maybe))
  2185. (defun org-reset-checkbox-state-subtree ()
  2186. "Reset all checkboxes in an entry subtree."
  2187. (interactive "*")
  2188. (if (org-before-first-heading-p)
  2189. (error "Not inside a tree")
  2190. (save-restriction
  2191. (save-excursion
  2192. (org-narrow-to-subtree)
  2193. (org-show-subtree)
  2194. (goto-char (point-min))
  2195. (let ((end (point-max)))
  2196. (while (< (point) end)
  2197. (when (org-at-item-checkbox-p)
  2198. (replace-match "[ ]" t t nil 1))
  2199. (beginning-of-line 2)))
  2200. (org-update-checkbox-count-maybe 'all)))))
  2201. (defun org-update-checkbox-count (&optional all)
  2202. "Update the checkbox statistics in the current section.
  2203. This will find all statistic cookies like [57%] and [6/12] and
  2204. update them with the current numbers.
  2205. With optional prefix argument ALL, do this for the whole buffer."
  2206. (interactive "P")
  2207. (save-excursion
  2208. (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  2209. (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
  2210. (recursivep
  2211. (or (not org-hierarchical-checkbox-statistics)
  2212. (string-match "\\<recursive\\>"
  2213. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2214. (bounds (if all
  2215. (cons (point-min) (point-max))
  2216. (cons (or (ignore-errors (org-back-to-heading t) (point))
  2217. (point-min))
  2218. (save-excursion (outline-next-heading) (point)))))
  2219. (count-boxes
  2220. (function
  2221. ;; Return number of checked boxes and boxes of all types
  2222. ;; in all structures in STRUCTS. If RECURSIVEP is
  2223. ;; non-nil, also count boxes in sub-lists. If ITEM is
  2224. ;; nil, count across the whole structure, else count only
  2225. ;; across subtree whose ancestor is ITEM.
  2226. (lambda (item structs recursivep)
  2227. (let ((c-on 0) (c-all 0))
  2228. (mapc
  2229. (lambda (s)
  2230. (let* ((pre (org-list-prevs-alist s))
  2231. (par (org-list-parents-alist s))
  2232. (items
  2233. (cond
  2234. ((and recursivep item) (org-list-get-subtree item s))
  2235. (recursivep (mapcar 'car s))
  2236. (item (org-list-get-children item s par))
  2237. (t (org-list-get-all-items
  2238. (org-list-get-top-point s) s pre))))
  2239. (cookies (delq nil (mapcar
  2240. (lambda (e)
  2241. (org-list-get-checkbox e s))
  2242. items))))
  2243. (setq c-all (+ (length cookies) c-all)
  2244. c-on (+ (org-count "[X]" cookies) c-on))))
  2245. structs)
  2246. (cons c-on c-all)))))
  2247. (backup-end 1)
  2248. cookies-list structs-bak box-num)
  2249. (goto-char (car bounds))
  2250. ;; 1. Build an alist for each cookie found within BOUNDS. The
  2251. ;; key will be position at beginning of cookie and values
  2252. ;; ending position, format of cookie, and a cell whose car is
  2253. ;; number of checked boxes to report, and cdr total number of
  2254. ;; boxes.
  2255. (while (re-search-forward cookie-re (cdr bounds) t)
  2256. (catch 'skip
  2257. (save-excursion
  2258. (push
  2259. (list
  2260. (match-beginning 1) ; cookie start
  2261. (match-end 1) ; cookie end
  2262. (match-string 2) ; percent?
  2263. (cond ; boxes count
  2264. ;; Cookie is at an heading, but specifically for todo,
  2265. ;; not for checkboxes: skip it.
  2266. ((and (org-at-heading-p)
  2267. (string-match "\\<todo\\>"
  2268. (downcase
  2269. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2270. (throw 'skip nil))
  2271. ;; Cookie is at an heading, but all lists before next
  2272. ;; heading already have been read. Use data collected
  2273. ;; in STRUCTS-BAK. This should only happen when
  2274. ;; heading has more than one cookie on it.
  2275. ((and (org-at-heading-p)
  2276. (<= (save-excursion (outline-next-heading) (point))
  2277. backup-end))
  2278. (funcall count-boxes nil structs-bak recursivep))
  2279. ;; Cookie is at a fresh heading. Grab structure of
  2280. ;; every list containing a checkbox between point and
  2281. ;; next headline, and save them in STRUCTS-BAK.
  2282. ((org-at-heading-p)
  2283. (setq backup-end (save-excursion
  2284. (outline-next-heading) (point))
  2285. structs-bak nil)
  2286. (while (org-list-search-forward box-re backup-end 'move)
  2287. (let* ((struct (org-list-struct))
  2288. (bottom (org-list-get-bottom-point struct)))
  2289. (push struct structs-bak)
  2290. (goto-char bottom)))
  2291. (funcall count-boxes nil structs-bak recursivep))
  2292. ;; Cookie is at an item, and we already have list
  2293. ;; structure stored in STRUCTS-BAK.
  2294. ((and (org-at-item-p)
  2295. (< (point-at-bol) backup-end)
  2296. ;; Only lists in no special context are stored.
  2297. (not (nth 2 (org-list-context))))
  2298. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2299. ;; Cookie is at an item, but we need to compute list
  2300. ;; structure.
  2301. ((org-at-item-p)
  2302. (let ((struct (org-list-struct)))
  2303. (setq backup-end (org-list-get-bottom-point struct)
  2304. structs-bak (list struct)))
  2305. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2306. ;; Else, cookie found is at a wrong place. Skip it.
  2307. (t (throw 'skip nil))))
  2308. cookies-list))))
  2309. ;; 2. Apply alist to buffer, in reverse order so positions stay
  2310. ;; unchanged after cookie modifications.
  2311. (mapc (lambda (cookie)
  2312. (let* ((beg (car cookie))
  2313. (end (nth 1 cookie))
  2314. (percentp (nth 2 cookie))
  2315. (checked (car (nth 3 cookie)))
  2316. (total (cdr (nth 3 cookie)))
  2317. (new (if percentp
  2318. (format "[%d%%]" (/ (* 100 checked)
  2319. (max 1 total)))
  2320. (format "[%d/%d]" checked total))))
  2321. (goto-char beg)
  2322. (insert new)
  2323. (delete-region (point) (+ (point) (- end beg)))
  2324. (when org-auto-align-tags (org-fix-tags-on-the-fly))))
  2325. cookies-list))))
  2326. (defun org-get-checkbox-statistics-face ()
  2327. "Select the face for checkbox statistics.
  2328. The face will be `org-done' when all relevant boxes are checked.
  2329. Otherwise it will be `org-todo'."
  2330. (if (match-end 1)
  2331. (if (equal (match-string 1) "100%")
  2332. 'org-checkbox-statistics-done
  2333. 'org-checkbox-statistics-todo)
  2334. (if (and (> (match-end 2) (match-beginning 2))
  2335. (equal (match-string 2) (match-string 3)))
  2336. 'org-checkbox-statistics-done
  2337. 'org-checkbox-statistics-todo)))
  2338. (defun org-update-checkbox-count-maybe (&optional all)
  2339. "Update checkbox statistics unless turned off by user.
  2340. With an optional argument ALL, update them in the whole buffer."
  2341. (when (cdr (assq 'checkbox org-list-automatic-rules))
  2342. (org-update-checkbox-count all))
  2343. (run-hooks 'org-checkbox-statistics-hook))
  2344. (defvar org-last-indent-begin-marker (make-marker))
  2345. (defvar org-last-indent-end-marker (make-marker))
  2346. (defun org-list-indent-item-generic (arg no-subtree struct)
  2347. "Indent a local list item including its children.
  2348. When number ARG is a negative, item will be outdented, otherwise
  2349. it will be indented.
  2350. If a region is active, all items inside will be moved.
  2351. If NO-SUBTREE is non-nil, only indent the item itself, not its
  2352. children.
  2353. STRUCT is the list structure.
  2354. Return t if successful."
  2355. (save-excursion
  2356. (let* ((regionp (org-region-active-p))
  2357. (rbeg (and regionp (region-beginning)))
  2358. (rend (and regionp (region-end)))
  2359. (top (org-list-get-top-point struct))
  2360. (parents (org-list-parents-alist struct))
  2361. (prevs (org-list-prevs-alist struct))
  2362. ;; Are we going to move the whole list?
  2363. (specialp
  2364. (and (not regionp)
  2365. (= top (point-at-bol))
  2366. (cdr (assq 'indent org-list-automatic-rules))
  2367. (if no-subtree
  2368. (error
  2369. "First item of list cannot move without its subtree")
  2370. t))))
  2371. ;; Determine begin and end points of zone to indent. If moving
  2372. ;; more than one item, save them for subsequent moves.
  2373. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  2374. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  2375. (if regionp
  2376. (progn
  2377. (set-marker org-last-indent-begin-marker rbeg)
  2378. (set-marker org-last-indent-end-marker rend))
  2379. (set-marker org-last-indent-begin-marker (point-at-bol))
  2380. (set-marker org-last-indent-end-marker
  2381. (cond
  2382. (specialp (org-list-get-bottom-point struct))
  2383. (no-subtree (1+ (point-at-bol)))
  2384. (t (org-list-get-item-end (point-at-bol) struct))))))
  2385. (let* ((beg (marker-position org-last-indent-begin-marker))
  2386. (end (marker-position org-last-indent-end-marker)))
  2387. (cond
  2388. ;; Special case: moving top-item with indent rule.
  2389. (specialp
  2390. (let* ((level-skip (org-level-increment))
  2391. (offset (if (< arg 0) (- level-skip) level-skip))
  2392. (top-ind (org-list-get-ind beg struct))
  2393. (old-struct (copy-tree struct)))
  2394. (if (< (+ top-ind offset) 0)
  2395. (error "Cannot outdent beyond margin")
  2396. ;; Change bullet if necessary.
  2397. (when (and (= (+ top-ind offset) 0)
  2398. (string-match "*"
  2399. (org-list-get-bullet beg struct)))
  2400. (org-list-set-bullet beg struct
  2401. (org-list-bullet-string "-")))
  2402. ;; Shift every item by OFFSET and fix bullets. Then
  2403. ;; apply changes to buffer.
  2404. (mapc (lambda (e)
  2405. (let ((ind (org-list-get-ind (car e) struct)))
  2406. (org-list-set-ind (car e) struct (+ ind offset))))
  2407. struct)
  2408. (org-list-struct-fix-bul struct prevs)
  2409. (org-list-struct-apply-struct struct old-struct))))
  2410. ;; Forbidden move:
  2411. ((and (< arg 0)
  2412. ;; If only one item is moved, it mustn't have a child.
  2413. (or (and no-subtree
  2414. (not regionp)
  2415. (org-list-has-child-p beg struct))
  2416. ;; If a subtree or region is moved, the last item
  2417. ;; of the subtree mustn't have a child.
  2418. (let ((last-item (caar
  2419. (reverse
  2420. (org-remove-if
  2421. (lambda (e) (>= (car e) end))
  2422. struct)))))
  2423. (org-list-has-child-p last-item struct))))
  2424. (error "Cannot outdent an item without its children"))
  2425. ;; Normal shifting
  2426. (t
  2427. (let* ((new-parents
  2428. (if (< arg 0)
  2429. (org-list-struct-outdent beg end struct parents)
  2430. (org-list-struct-indent beg end struct parents prevs))))
  2431. (org-list-write-struct struct new-parents))
  2432. (org-update-checkbox-count-maybe))))))
  2433. t)
  2434. (defun org-outdent-item ()
  2435. "Outdent a local list item, but not its children.
  2436. If a region is active, all items inside will be moved."
  2437. (interactive)
  2438. (let ((regionp (org-region-active-p)))
  2439. (cond
  2440. ((or (org-at-item-p)
  2441. (and regionp
  2442. (save-excursion (goto-char (region-beginning))
  2443. (org-at-item-p))))
  2444. (let ((struct (if (not regionp) (org-list-struct)
  2445. (save-excursion (goto-char (region-beginning))
  2446. (org-list-struct)))))
  2447. (org-list-indent-item-generic -1 t struct)))
  2448. (regionp (error "Region not starting at an item"))
  2449. (t (error "Not at an item")))))
  2450. (defun org-indent-item ()
  2451. "Indent a local list item, but not its children.
  2452. If a region is active, all items inside will be moved."
  2453. (interactive)
  2454. (let ((regionp (org-region-active-p)))
  2455. (cond
  2456. ((or (org-at-item-p)
  2457. (and regionp
  2458. (save-excursion (goto-char (region-beginning))
  2459. (org-at-item-p))))
  2460. (let ((struct (if (not regionp) (org-list-struct)
  2461. (save-excursion (goto-char (region-beginning))
  2462. (org-list-struct)))))
  2463. (org-list-indent-item-generic 1 t struct)))
  2464. (regionp (error "Region not starting at an item"))
  2465. (t (error "Not at an item")))))
  2466. (defun org-outdent-item-tree ()
  2467. "Outdent a local list item including its children.
  2468. If a region is active, all items inside will be moved."
  2469. (interactive)
  2470. (let ((regionp (org-region-active-p)))
  2471. (cond
  2472. ((or (org-at-item-p)
  2473. (and regionp
  2474. (save-excursion (goto-char (region-beginning))
  2475. (org-at-item-p))))
  2476. (let ((struct (if (not regionp) (org-list-struct)
  2477. (save-excursion (goto-char (region-beginning))
  2478. (org-list-struct)))))
  2479. (org-list-indent-item-generic -1 nil struct)))
  2480. (regionp (error "Region not starting at an item"))
  2481. (t (error "Not at an item")))))
  2482. (defun org-indent-item-tree ()
  2483. "Indent a local list item including its children.
  2484. If a region is active, all items inside will be moved."
  2485. (interactive)
  2486. (let ((regionp (org-region-active-p)))
  2487. (cond
  2488. ((or (org-at-item-p)
  2489. (and regionp
  2490. (save-excursion (goto-char (region-beginning))
  2491. (org-at-item-p))))
  2492. (let ((struct (if (not regionp) (org-list-struct)
  2493. (save-excursion (goto-char (region-beginning))
  2494. (org-list-struct)))))
  2495. (org-list-indent-item-generic 1 nil struct)))
  2496. (regionp (error "Region not starting at an item"))
  2497. (t (error "Not at an item")))))
  2498. (defvar org-tab-ind-state)
  2499. (defun org-cycle-item-indentation ()
  2500. "Cycle levels of indentation of an empty item.
  2501. The first run indents the item, if applicable. Subsequent runs
  2502. outdent it at meaningful levels in the list. When done, item is
  2503. put back at its original position with its original bullet.
  2504. Return t at each successful move."
  2505. (when (org-at-item-p)
  2506. (let* ((org-adapt-indentation nil)
  2507. (struct (org-list-struct))
  2508. (ind (org-list-get-ind (point-at-bol) struct))
  2509. (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
  2510. ;; Accept empty items or if cycle has already started.
  2511. (when (or (eq last-command 'org-cycle-item-indentation)
  2512. (and (save-excursion
  2513. (beginning-of-line)
  2514. (looking-at org-list-full-item-re))
  2515. (>= (match-end 0) (save-excursion
  2516. (goto-char (org-list-get-item-end
  2517. (point-at-bol) struct))
  2518. (skip-chars-backward " \r\t\n")
  2519. (point)))))
  2520. (setq this-command 'org-cycle-item-indentation)
  2521. ;; When in the middle of the cycle, try to outdent first. If
  2522. ;; it fails, and point is still at initial position, indent.
  2523. ;; Else, re-create it at its original position.
  2524. (if (eq last-command 'org-cycle-item-indentation)
  2525. (cond
  2526. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2527. ((and (= ind (car org-tab-ind-state))
  2528. (ignore-errors (org-list-indent-item-generic 1 t struct))))
  2529. (t (delete-region (point-at-bol) (point-at-eol))
  2530. (org-indent-to-column (car org-tab-ind-state))
  2531. (insert (cdr org-tab-ind-state) " ")
  2532. ;; Break cycle
  2533. (setq this-command 'identity)))
  2534. ;; If a cycle is starting, remember indentation and bullet,
  2535. ;; then try to indent. If it fails, try to outdent.
  2536. (setq org-tab-ind-state (cons ind bullet))
  2537. (cond
  2538. ((ignore-errors (org-list-indent-item-generic 1 t struct)))
  2539. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2540. (t (error "Cannot move item"))))
  2541. t))))
  2542. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  2543. "Sort list items.
  2544. The cursor may be at any item of the list that should be sorted.
  2545. Sublists are not sorted. Checkboxes, if any, are ignored.
  2546. Sorting can be alphabetically, numerically, by date/time as given
  2547. by a time stamp, by a property or by priority.
  2548. Comparing entries ignores case by default. However, with an
  2549. optional argument WITH-CASE, the sorting considers case as well.
  2550. The command prompts for the sorting type unless it has been given
  2551. to the function through the SORTING-TYPE argument, which needs to
  2552. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  2553. meaning of each character:
  2554. n Numerically, by converting the beginning of the item to a number.
  2555. a Alphabetically. Only the first line of item is checked.
  2556. t By date/time, either the first active time stamp in the entry, if
  2557. any, or by the first inactive one. In a timer list, sort the timers.
  2558. Capital letters will reverse the sort order.
  2559. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
  2560. a function to be called with point at the beginning of the
  2561. record. It must return either a string or a number that should
  2562. serve as the sorting key for that record. It will then use
  2563. COMPARE-FUNC to compare entries."
  2564. (interactive "P")
  2565. (let* ((case-func (if with-case 'identity 'downcase))
  2566. (struct (org-list-struct))
  2567. (prevs (org-list-prevs-alist struct))
  2568. (start (org-list-get-list-begin (point-at-bol) struct prevs))
  2569. (end (org-list-get-list-end (point-at-bol) struct prevs))
  2570. (sorting-type
  2571. (progn
  2572. (message
  2573. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  2574. (read-char-exclusive)))
  2575. (getkey-func (and (= (downcase sorting-type) ?f)
  2576. (intern (org-icompleting-read "Sort using function: "
  2577. obarray 'fboundp t nil nil)))))
  2578. (message "Sorting items...")
  2579. (save-restriction
  2580. (narrow-to-region start end)
  2581. (goto-char (point-min))
  2582. (let* ((dcst (downcase sorting-type))
  2583. (case-fold-search nil)
  2584. (now (current-time))
  2585. (sort-func (cond
  2586. ((= dcst ?a) 'string<)
  2587. ((= dcst ?f) compare-func)
  2588. ((= dcst ?t) '<)))
  2589. (next-record (lambda ()
  2590. (skip-chars-forward " \r\t\n")
  2591. (beginning-of-line)))
  2592. (end-record (lambda ()
  2593. (goto-char (org-list-get-item-end-before-blank
  2594. (point) struct))))
  2595. (value-to-sort
  2596. (lambda ()
  2597. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  2598. (cond
  2599. ((= dcst ?n)
  2600. (string-to-number (buffer-substring (match-end 0)
  2601. (point-at-eol))))
  2602. ((= dcst ?a)
  2603. (funcall case-func
  2604. (buffer-substring (match-end 0) (point-at-eol))))
  2605. ((= dcst ?t)
  2606. (cond
  2607. ;; If it is a timer list, convert timer to seconds
  2608. ((org-at-item-timer-p)
  2609. (org-timer-hms-to-secs (match-string 1)))
  2610. ((or (re-search-forward org-ts-regexp (point-at-eol) t)
  2611. (re-search-forward org-ts-regexp-both
  2612. (point-at-eol) t))
  2613. (org-time-string-to-seconds (match-string 0)))
  2614. (t (org-float-time now))))
  2615. ((= dcst ?f)
  2616. (if getkey-func
  2617. (let ((value (funcall getkey-func)))
  2618. (if (stringp value)
  2619. (funcall case-func value)
  2620. value))
  2621. (error "Invalid key function `%s'" getkey-func)))
  2622. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  2623. (sort-subr (/= dcst sorting-type)
  2624. next-record
  2625. end-record
  2626. value-to-sort
  2627. nil
  2628. sort-func)
  2629. ;; Read and fix list again, as `sort-subr' probably destroyed
  2630. ;; its structure.
  2631. (org-list-repair)
  2632. (run-hooks 'org-after-sorting-entries-or-items-hook)
  2633. (message "Sorting items...done")))))
  2634. ;;; Send and receive lists
  2635. (defun org-list-parse-list (&optional delete)
  2636. "Parse the list at point and maybe DELETE it.
  2637. Return a list whose car is a symbol of list type, among
  2638. `ordered', `unordered' and `descriptive'. Then, each item is
  2639. a list whose car is counter, and cdr are strings and other
  2640. sub-lists. Inside strings, check-boxes are replaced by
  2641. \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
  2642. For example, the following list:
  2643. 1. first item
  2644. + sub-item one
  2645. + [X] sub-item two
  2646. more text in first item
  2647. 2. [@3] last item
  2648. will be parsed as:
  2649. \(ordered
  2650. \(nil \"first item\"
  2651. \(unordered
  2652. \(nil \"sub-item one\"\)
  2653. \(nil \"[CBON] sub-item two\"\)\)
  2654. \"more text in first item\"\)
  2655. \(3 \"last item\"\)\)
  2656. Point is left at list end."
  2657. (let* ((struct (org-list-struct))
  2658. (prevs (org-list-prevs-alist struct))
  2659. (parents (org-list-parents-alist struct))
  2660. (top (org-list-get-top-point struct))
  2661. (bottom (org-list-get-bottom-point struct))
  2662. out
  2663. parse-item ; for byte-compiler
  2664. (get-text
  2665. (function
  2666. ;; Return text between BEG and END, trimmed, with
  2667. ;; checkboxes replaced.
  2668. (lambda (beg end)
  2669. (let ((text (org-trim (buffer-substring beg end))))
  2670. (if (string-match "\\`\\[\\([-X ]\\)\\]" text)
  2671. (replace-match
  2672. (let ((box (match-string 1 text)))
  2673. (cond
  2674. ((equal box " ") "CBOFF")
  2675. ((equal box "-") "CBTRANS")
  2676. (t "CBON")))
  2677. t nil text 1)
  2678. text)))))
  2679. (parse-sublist
  2680. (function
  2681. ;; Return a list whose car is list type and cdr a list of
  2682. ;; items' body.
  2683. (lambda (e)
  2684. (cons (org-list-get-list-type (car e) struct prevs)
  2685. (mapcar parse-item e)))))
  2686. (parse-item
  2687. (function
  2688. ;; Return a list containing counter of item, if any, text
  2689. ;; and any sublist inside it.
  2690. (lambda (e)
  2691. (let ((start (save-excursion
  2692. (goto-char e)
  2693. (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
  2694. (match-end 0)))
  2695. ;; Get counter number. For alphabetic counter, get
  2696. ;; its position in the alphabet.
  2697. (counter (let ((c (org-list-get-counter e struct)))
  2698. (cond
  2699. ((not c) nil)
  2700. ((string-match "[A-Za-z]" c)
  2701. (- (string-to-char (upcase (match-string 0 c)))
  2702. 64))
  2703. ((string-match "[0-9]+" c)
  2704. (string-to-number (match-string 0 c))))))
  2705. (childp (org-list-has-child-p e struct))
  2706. (end (org-list-get-item-end e struct)))
  2707. ;; If item has a child, store text between bullet and
  2708. ;; next child, then recursively parse all sublists. At
  2709. ;; the end of each sublist, check for the presence of
  2710. ;; text belonging to the original item.
  2711. (if childp
  2712. (let* ((children (org-list-get-children e struct parents))
  2713. (body (list (funcall get-text start childp))))
  2714. (while children
  2715. (let* ((first (car children))
  2716. (sub (org-list-get-all-items first struct prevs))
  2717. (last-c (car (last sub)))
  2718. (last-end (org-list-get-item-end last-c struct)))
  2719. (push (funcall parse-sublist sub) body)
  2720. ;; Remove children from the list just parsed.
  2721. (setq children (cdr (member last-c children)))
  2722. ;; There is a chunk of text belonging to the
  2723. ;; item if last child doesn't end where next
  2724. ;; child starts or where item ends.
  2725. (unless (= (or (car children) end) last-end)
  2726. (push (funcall get-text
  2727. last-end (or (car children) end))
  2728. body))))
  2729. (cons counter (nreverse body)))
  2730. (list counter (funcall get-text start end))))))))
  2731. ;; Store output, take care of cursor position and deletion of
  2732. ;; list, then return output.
  2733. (setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
  2734. (goto-char top)
  2735. (when delete
  2736. (delete-region top bottom)
  2737. (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re))
  2738. (replace-match "")))
  2739. out))
  2740. (defun org-list-make-subtree ()
  2741. "Convert the plain list at point into a subtree."
  2742. (interactive)
  2743. (if (not (ignore-errors (goto-char (org-in-item-p))))
  2744. (error "Not in a list")
  2745. (let ((list (save-excursion (org-list-parse-list t))))
  2746. (insert (org-list-to-subtree list)))))
  2747. (defun org-list-insert-radio-list ()
  2748. "Insert a radio list template appropriate for this major mode."
  2749. (interactive)
  2750. (let* ((e (assq major-mode org-list-radio-list-templates))
  2751. (txt (nth 1 e))
  2752. name pos)
  2753. (unless e (error "No radio list setup defined for %s" major-mode))
  2754. (setq name (read-string "List name: "))
  2755. (while (string-match "%n" txt)
  2756. (setq txt (replace-match name t t txt)))
  2757. (or (bolp) (insert "\n"))
  2758. (setq pos (point))
  2759. (insert txt)
  2760. (goto-char pos)))
  2761. (defun org-list-send-list (&optional maybe)
  2762. "Send a transformed version of this list to the receiver position.
  2763. With argument MAYBE, fail quietly if no transformation is defined
  2764. for this list."
  2765. (interactive)
  2766. (catch 'exit
  2767. (unless (org-at-item-p) (error "Not at a list item"))
  2768. (save-excursion
  2769. (re-search-backward "#\\+ORGLST" nil t)
  2770. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  2771. (if maybe
  2772. (throw 'exit nil)
  2773. (error "Don't know how to transform this list"))))
  2774. (let* ((name (match-string 1))
  2775. (transform (intern (match-string 2)))
  2776. (bottom-point
  2777. (save-excursion
  2778. (re-search-forward
  2779. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  2780. (match-beginning 0)))
  2781. (top-point
  2782. (progn
  2783. (re-search-backward "#\\+ORGLST" nil t)
  2784. (re-search-forward (org-item-beginning-re) bottom-point t)
  2785. (match-beginning 0)))
  2786. (list (save-restriction
  2787. (narrow-to-region top-point bottom-point)
  2788. (org-list-parse-list)))
  2789. beg txt)
  2790. (unless (fboundp transform)
  2791. (error "No such transformation function %s" transform))
  2792. (let ((txt (funcall transform list)))
  2793. ;; Find the insertion place
  2794. (save-excursion
  2795. (goto-char (point-min))
  2796. (unless (re-search-forward
  2797. (concat "BEGIN RECEIVE ORGLST +"
  2798. name
  2799. "\\([ \t]\\|$\\)") nil t)
  2800. (error "Don't know where to insert translated list"))
  2801. (goto-char (match-beginning 0))
  2802. (beginning-of-line 2)
  2803. (setq beg (point))
  2804. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  2805. (error "Cannot find end of insertion region"))
  2806. (delete-region beg (point-at-bol))
  2807. (goto-char beg)
  2808. (insert txt "\n")))
  2809. (message "List converted and installed at receiver location"))))
  2810. (defsubst org-list-item-trim-br (item)
  2811. "Trim line breaks in a list ITEM."
  2812. (setq item (replace-regexp-in-string "\n +" " " item)))
  2813. (defun org-list-to-generic (list params)
  2814. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  2815. Valid parameters PARAMS are:
  2816. :ustart String to start an unordered list
  2817. :uend String to end an unordered list
  2818. :ostart String to start an ordered list
  2819. :oend String to end an ordered list
  2820. :dstart String to start a descriptive list
  2821. :dend String to end a descriptive list
  2822. :dtstart String to start a descriptive term
  2823. :dtend String to end a descriptive term
  2824. :ddstart String to start a description
  2825. :ddend String to end a description
  2826. :splice When set to t, return only list body lines, don't wrap
  2827. them into :[u/o]start and :[u/o]end. Default is nil.
  2828. :istart String to start a list item.
  2829. :icount String to start an item with a counter.
  2830. :iend String to end a list item
  2831. :isep String to separate items
  2832. :lsep String to separate sublists
  2833. :csep String to separate text from a sub-list
  2834. :cboff String to insert for an unchecked check-box
  2835. :cbon String to insert for a checked check-box
  2836. :cbtrans String to insert for a check-box in transitional state
  2837. :nobr Non-nil means remove line breaks in lists items.
  2838. Alternatively, each parameter can also be a form returning
  2839. a string. These sexp can use keywords `counter' and `depth',
  2840. representing respectively counter associated to the current
  2841. item, and depth of the current sub-list, starting at 0.
  2842. Obviously, `counter' is only available for parameters applying to
  2843. items."
  2844. (interactive)
  2845. (let* ((p params)
  2846. (splicep (plist-get p :splice))
  2847. (ostart (plist-get p :ostart))
  2848. (oend (plist-get p :oend))
  2849. (ustart (plist-get p :ustart))
  2850. (uend (plist-get p :uend))
  2851. (dstart (plist-get p :dstart))
  2852. (dend (plist-get p :dend))
  2853. (dtstart (plist-get p :dtstart))
  2854. (dtend (plist-get p :dtend))
  2855. (ddstart (plist-get p :ddstart))
  2856. (ddend (plist-get p :ddend))
  2857. (istart (plist-get p :istart))
  2858. (icount (plist-get p :icount))
  2859. (iend (plist-get p :iend))
  2860. (isep (plist-get p :isep))
  2861. (lsep (plist-get p :lsep))
  2862. (csep (plist-get p :csep))
  2863. (cbon (plist-get p :cbon))
  2864. (cboff (plist-get p :cboff))
  2865. (cbtrans (plist-get p :cbtrans))
  2866. (nobr (plist-get p :nobr))
  2867. export-sublist ; for byte-compiler
  2868. (export-item
  2869. (function
  2870. ;; Export an item ITEM of type TYPE, at DEPTH. First
  2871. ;; string in item is treated in a special way as it can
  2872. ;; bring extra information that needs to be processed.
  2873. (lambda (item type depth)
  2874. (let* ((counter (pop item))
  2875. (fmt (concat
  2876. (cond
  2877. ((eq type 'descriptive)
  2878. ;; Stick DTSTART to ISTART by
  2879. ;; left-trimming the latter.
  2880. (concat (let ((s (eval istart)))
  2881. (or (and (string-match "[ \t\n\r]+\\'" s)
  2882. (replace-match "" t t s))
  2883. istart))
  2884. "%s" (eval ddend)))
  2885. ((and counter (eq type 'ordered))
  2886. (concat (eval icount) "%s"))
  2887. (t (concat (eval istart) "%s")))
  2888. (eval iend)))
  2889. (first (car item)))
  2890. ;; Replace checkbox if any is found.
  2891. (cond
  2892. ((string-match "\\[CBON\\]" first)
  2893. (setq first (replace-match cbon t t first)))
  2894. ((string-match "\\[CBOFF\\]" first)
  2895. (setq first (replace-match cboff t t first)))
  2896. ((string-match "\\[CBTRANS\\]" first)
  2897. (setq first (replace-match cbtrans t t first))))
  2898. ;; Replace line breaks if required
  2899. (when nobr (setq first (org-list-item-trim-br first)))
  2900. ;; Insert descriptive term if TYPE is `descriptive'.
  2901. (when (eq type 'descriptive)
  2902. (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first))
  2903. (term (if complete
  2904. (save-match-data
  2905. (org-trim (match-string 1 first)))
  2906. "???"))
  2907. (desc (if complete
  2908. (org-trim (substring first (match-end 0)))
  2909. first)))
  2910. (setq first (concat (eval dtstart) term (eval dtend)
  2911. (eval ddstart) desc))))
  2912. (setcar item first)
  2913. (format fmt
  2914. (mapconcat (lambda (e)
  2915. (if (stringp e) e
  2916. (funcall export-sublist e (1+ depth))))
  2917. item (or (eval csep) "")))))))
  2918. (export-sublist
  2919. (function
  2920. ;; Export sublist SUB at DEPTH.
  2921. (lambda (sub depth)
  2922. (let* ((type (car sub))
  2923. (items (cdr sub))
  2924. (fmt (concat (cond
  2925. (splicep "%s")
  2926. ((eq type 'ordered)
  2927. (concat (eval ostart) "%s" (eval oend)))
  2928. ((eq type 'descriptive)
  2929. (concat (eval dstart) "%s" (eval dend)))
  2930. (t (concat (eval ustart) "%s" (eval uend))))
  2931. (eval lsep))))
  2932. (format fmt (mapconcat (lambda (e)
  2933. (funcall export-item e type depth))
  2934. items (or (eval isep) ""))))))))
  2935. (concat (funcall export-sublist list 0) "\n")))
  2936. (defun org-list-to-latex (list &optional params)
  2937. "Convert LIST into a LaTeX list.
  2938. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2939. with overruling parameters for `org-list-to-generic'."
  2940. (org-list-to-generic
  2941. list
  2942. (org-combine-plists
  2943. '(:splice nil :ostart "\\begin{enumerate}\n" :oend "\\end{enumerate}"
  2944. :ustart "\\begin{itemize}\n" :uend "\\end{itemize}"
  2945. :dstart "\\begin{description}\n" :dend "\\end{description}"
  2946. :dtstart "[" :dtend "] "
  2947. :istart "\\item " :iend "\n"
  2948. :icount (let ((enum (nth depth '("i" "ii" "iii" "iv"))))
  2949. (if enum
  2950. ;; LaTeX increments counter just before
  2951. ;; using it, so set it to the desired
  2952. ;; value, minus one.
  2953. (format "\\setcounter{enum%s}{%s}\n\\item "
  2954. enum (1- counter))
  2955. "\\item "))
  2956. :csep "\n"
  2957. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}"
  2958. :cbtrans "\\texttt{[-]}")
  2959. params)))
  2960. (defun org-list-to-html (list &optional params)
  2961. "Convert LIST into a HTML list.
  2962. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2963. with overruling parameters for `org-list-to-generic'."
  2964. (org-list-to-generic
  2965. list
  2966. (org-combine-plists
  2967. '(:splice nil :ostart "<ol>\n" :oend "\n</ol>"
  2968. :ustart "<ul>\n" :uend "\n</ul>"
  2969. :dstart "<dl>\n" :dend "\n</dl>"
  2970. :dtstart "<dt>" :dtend "</dt>\n"
  2971. :ddstart "<dd>" :ddend "</dd>"
  2972. :istart "<li>" :iend "</li>"
  2973. :icount (format "<li value=\"%s\">" counter)
  2974. :isep "\n" :lsep "\n" :csep "\n"
  2975. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>"
  2976. :cbtrans "<code>[-]</code>")
  2977. params)))
  2978. (defun org-list-to-texinfo (list &optional params)
  2979. "Convert LIST into a Texinfo list.
  2980. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2981. with overruling parameters for `org-list-to-generic'."
  2982. (org-list-to-generic
  2983. list
  2984. (org-combine-plists
  2985. '(:splice nil :ostart "@itemize @minus\n" :oend "@end itemize"
  2986. :ustart "@enumerate\n" :uend "@end enumerate"
  2987. :dstart "@table @asis\n" :dend "@end table"
  2988. :dtstart " " :dtend "\n"
  2989. :istart "@item\n" :iend "\n"
  2990. :icount "@item\n"
  2991. :csep "\n"
  2992. :cbon "@code{[X]}" :cboff "@code{[ ]}"
  2993. :cbtrans "@code{[-]}")
  2994. params)))
  2995. (defun org-list-to-subtree (list &optional params)
  2996. "Convert LIST into an Org subtree.
  2997. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2998. with overruling parameters for `org-list-to-generic'."
  2999. (let* ((rule (cdr (assq 'heading org-blank-before-new-entry)))
  3000. (level (org-reduced-level (or (org-current-level) 0)))
  3001. (blankp (or (eq rule t)
  3002. (and (eq rule 'auto)
  3003. (save-excursion
  3004. (outline-previous-heading)
  3005. (org-previous-line-empty-p)))))
  3006. (get-stars
  3007. (function
  3008. ;; Return the string for the heading, depending on depth D
  3009. ;; of current sub-list.
  3010. (lambda (d)
  3011. (let ((oddeven-level (+ level d 1)))
  3012. (concat (make-string (if org-odd-levels-only
  3013. (1- (* 2 oddeven-level))
  3014. oddeven-level)
  3015. ?*)
  3016. " "))))))
  3017. (org-list-to-generic
  3018. list
  3019. (org-combine-plists
  3020. '(:splice t
  3021. :dtstart " " :dtend " "
  3022. :istart (funcall get-stars depth)
  3023. :icount (funcall get-stars depth)
  3024. :isep (if blankp "\n\n" "\n")
  3025. :csep (if blankp "\n\n" "\n")
  3026. :cbon "DONE" :cboff "TODO" :cbtrans "TODO")
  3027. params))))
  3028. (provide 'org-list)
  3029. ;;; org-list.el ends here