org-list.el 116 KB

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