org-list.el 118 KB

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