org-list.el 120 KB

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