org-list.el 130 KB

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