org-list.el 119 KB

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