org-list.el 120 KB

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