org-list.el 118 KB

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