org-list.el 118 KB

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