org-list.el 120 KB

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