org-list.el 127 KB

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