org-list.el 119 KB

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