org-list.el 127 KB

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