org-list.el 120 KB

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