org-list.el 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245
  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. (defun org-list-get-item-number (item struct prevs parents)
  908. "Return ITEM's sequence number.
  909. STRUCT is the list structure. PREVS is the alist of previous
  910. items, as returned by `org-list-prevs-alist'. PARENTS is the
  911. alist of ancestors, as returned by `org-list-parents-alist'.
  912. Return value is a list of integers. Counters have an impact on
  913. that value."
  914. (let ((get-relative-number
  915. (function
  916. (lambda (item struct prevs)
  917. ;; Return relative sequence number of ITEM in the sub-list
  918. ;; it belongs. STRUCT is the list structure. PREVS is
  919. ;; the alist of previous items.
  920. (let ((seq 0) (pos item) counter)
  921. (while (and (not (setq counter (org-list-get-counter pos struct)))
  922. (setq pos (org-list-get-prev-item pos struct prevs)))
  923. (incf seq))
  924. (if (not counter) (1+ seq)
  925. (cond
  926. ((string-match "[A-Za-z]" counter)
  927. (+ (- (string-to-char (upcase (match-string 0 counter))) 64)
  928. seq))
  929. ((string-match "[0-9]+" counter)
  930. (+ (string-to-number (match-string 0 counter)) seq))
  931. (t (1+ seq)))))))))
  932. ;; Cons each parent relative number into return value (OUT).
  933. (let ((out (list (funcall get-relative-number item struct prevs)))
  934. (parent item))
  935. (while (setq parent (org-list-get-parent parent struct parents))
  936. (push (funcall get-relative-number parent struct prevs) out))
  937. ;; Return value.
  938. out)))
  939. ;;; Searching
  940. (defun org-list-search-generic (search re bound noerr)
  941. "Search a string in valid contexts for lists.
  942. Arguments SEARCH, RE, BOUND and NOERR are similar to those used
  943. in `re-search-forward'."
  944. (catch 'exit
  945. (let ((origin (point)))
  946. (while t
  947. ;; 1. No match: return to origin or bound, depending on NOERR.
  948. (unless (funcall search re bound noerr)
  949. (throw 'exit (and (goto-char (if (memq noerr '(t nil)) origin bound))
  950. nil)))
  951. ;; 2. Match in valid context: return point. Else, continue
  952. ;; searching.
  953. (when (org-list-in-valid-context-p) (throw 'exit (point)))))))
  954. (defun org-list-search-backward (regexp &optional bound noerror)
  955. "Like `re-search-backward' but stop only where lists are recognized.
  956. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  957. `re-search-backward'."
  958. (org-list-search-generic #'re-search-backward
  959. regexp (or bound (point-min)) noerror))
  960. (defun org-list-search-forward (regexp &optional bound noerror)
  961. "Like `re-search-forward' but stop only where lists are recognized.
  962. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  963. `re-search-forward'."
  964. (org-list-search-generic #'re-search-forward
  965. regexp (or bound (point-max)) noerror))
  966. ;;; Methods on structures
  967. (defsubst org-list-bullet-string (bullet)
  968. "Return BULLET with the correct number of whitespaces.
  969. It determines the number of whitespaces to append by looking at
  970. `org-list-two-spaces-after-bullet-regexp'."
  971. (save-match-data
  972. (let ((spaces (if (and org-list-two-spaces-after-bullet-regexp
  973. (string-match
  974. org-list-two-spaces-after-bullet-regexp bullet))
  975. " "
  976. " ")))
  977. (string-match "\\S-+\\([ \t]*\\)" bullet)
  978. (replace-match spaces nil nil bullet 1))))
  979. (defun org-list-swap-items (beg-A beg-B struct)
  980. "Swap item starting at BEG-A with item starting at BEG-B in STRUCT.
  981. Blank lines at the end of items are left in place. Return the
  982. new structure after the changes.
  983. Assume BEG-A is lesser than BEG-B and that BEG-A and BEG-B belong
  984. to the same sub-list.
  985. This function modifies STRUCT."
  986. (save-excursion
  987. (let* ((end-A-no-blank (org-list-get-item-end-before-blank beg-A struct))
  988. (end-B-no-blank (org-list-get-item-end-before-blank beg-B struct))
  989. (end-A (org-list-get-item-end beg-A struct))
  990. (end-B (org-list-get-item-end beg-B struct))
  991. (size-A (- end-A-no-blank beg-A))
  992. (size-B (- end-B-no-blank beg-B))
  993. (body-A (buffer-substring beg-A end-A-no-blank))
  994. (body-B (buffer-substring beg-B end-B-no-blank))
  995. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B))
  996. (sub-A (cons beg-A (org-list-get-subtree beg-A struct)))
  997. (sub-B (cons beg-B (org-list-get-subtree beg-B struct))))
  998. ;; 1. Move effectively items in buffer.
  999. (goto-char beg-A)
  1000. (delete-region beg-A end-B-no-blank)
  1001. (insert (concat body-B between-A-no-blank-and-B body-A))
  1002. ;; 2. Now modify struct. No need to re-read the list, the
  1003. ;; transformation is just a shift of positions. Some special
  1004. ;; attention is required for items ending at END-A and END-B
  1005. ;; as empty spaces are not moved there. In others words,
  1006. ;; item BEG-A will end with whitespaces that were at the end
  1007. ;; of BEG-B and the same applies to BEG-B.
  1008. (mapc (lambda (e)
  1009. (let ((pos (car e)))
  1010. (cond
  1011. ((< pos beg-A))
  1012. ((memq pos sub-A)
  1013. (let ((end-e (nth 6 e)))
  1014. (setcar e (+ pos (- end-B-no-blank end-A-no-blank)))
  1015. (setcar (nthcdr 6 e)
  1016. (+ end-e (- end-B-no-blank end-A-no-blank)))
  1017. (when (= end-e end-A) (setcar (nthcdr 6 e) end-B))))
  1018. ((memq pos sub-B)
  1019. (let ((end-e (nth 6 e)))
  1020. (setcar e (- (+ pos beg-A) beg-B))
  1021. (setcar (nthcdr 6 e) (+ end-e (- beg-A beg-B)))
  1022. (when (= end-e end-B)
  1023. (setcar (nthcdr 6 e)
  1024. (+ beg-A size-B (- end-A end-A-no-blank))))))
  1025. ((< pos beg-B)
  1026. (let ((end-e (nth 6 e)))
  1027. (setcar e (+ pos (- size-B size-A)))
  1028. (setcar (nthcdr 6 e) (+ end-e (- size-B size-A))))))))
  1029. struct)
  1030. (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))))
  1031. (defun org-list-separating-blank-lines-number (pos struct prevs)
  1032. "Return number of blank lines that should separate items in list.
  1033. POS is the position of point where `org-list-insert-item' was called.
  1034. STRUCT is the list structure. PREVS is the alist of previous
  1035. items, as returned by `org-list-prevs-alist'.
  1036. Assume point is at item's beginning. If the item is alone, apply
  1037. some heuristics to guess the result."
  1038. (save-excursion
  1039. (let ((item (point))
  1040. (insert-blank-p
  1041. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  1042. usr-blank
  1043. (count-blanks
  1044. (function
  1045. (lambda ()
  1046. ;; Count blank lines above beginning of line.
  1047. (save-excursion
  1048. (count-lines (goto-char (point-at-bol))
  1049. (progn (skip-chars-backward " \r\t\n")
  1050. (forward-line)
  1051. (point))))))))
  1052. (cond
  1053. ;; Trivial cases where there should be none.
  1054. ((or org-empty-line-terminates-plain-lists (not insert-blank-p)) 0)
  1055. ;; When `org-blank-before-new-entry' says so, it is 1.
  1056. ((eq insert-blank-p t) 1)
  1057. ;; `plain-list-item' is 'auto. Count blank lines separating
  1058. ;; neighbours items in list.
  1059. (t (let ((next-p (org-list-get-next-item item struct prevs)))
  1060. (cond
  1061. ;; Is there a next item?
  1062. (next-p (goto-char next-p)
  1063. (funcall count-blanks))
  1064. ;; Is there a previous item?
  1065. ((org-list-get-prev-item item struct prevs)
  1066. (funcall count-blanks))
  1067. ;; User inserted blank lines, trust him.
  1068. ((and (> pos (org-list-get-item-end-before-blank item struct))
  1069. (> (save-excursion (goto-char pos)
  1070. (setq usr-blank (funcall count-blanks)))
  1071. 0))
  1072. usr-blank)
  1073. ;; Are there blank lines inside the list so far?
  1074. ((save-excursion
  1075. (goto-char (org-list-get-top-point struct))
  1076. (org-list-search-forward
  1077. "^[ \t]*$" (org-list-get-item-end-before-blank item struct) t))
  1078. 1)
  1079. ;; Default choice: no blank line.
  1080. (t 0))))))))
  1081. (defun org-list-insert-item (pos struct prevs &optional checkbox after-bullet)
  1082. "Insert a new list item at POS and return the new structure.
  1083. If POS is before first character after bullet of the item, the
  1084. new item will be created before the current one.
  1085. STRUCT is the list structure. PREVS is the the alist of previous
  1086. items, as returned by `org-list-prevs-alist'.
  1087. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  1088. after the bullet. Cursor will be after this text once the
  1089. function ends.
  1090. This function modifies STRUCT."
  1091. (let ((case-fold-search t))
  1092. ;; 1. Get information about list: position of point with regards
  1093. ;; to item start (BEFOREP), blank lines number separating items
  1094. ;; (BLANK-NB), if we're allowed to (SPLIT-LINE-P).
  1095. (let* ((item (progn (goto-char pos) (goto-char (org-list-get-item-begin))))
  1096. (item-end (org-list-get-item-end item struct))
  1097. (item-end-no-blank (org-list-get-item-end-before-blank item struct))
  1098. (beforep (and (looking-at org-list-full-item-re)
  1099. (<= pos (match-end 0))))
  1100. (split-line-p (org-get-alist-option org-M-RET-may-split-line 'item))
  1101. (blank-nb (org-list-separating-blank-lines-number
  1102. pos struct prevs))
  1103. ;; 2. Build the new item to be created. Concatenate same
  1104. ;; bullet as item, checkbox, text AFTER-BULLET if
  1105. ;; provided, and text cut from point to end of item
  1106. ;; (TEXT-CUT) to form item's BODY. TEXT-CUT depends on
  1107. ;; BEFOREP and SPLIT-LINE-P. The difference of size
  1108. ;; between what was cut and what was inserted in buffer
  1109. ;; is stored in SIZE-OFFSET.
  1110. (ind (org-list-get-ind item struct))
  1111. (ind-size (if indent-tabs-mode
  1112. (+ (/ ind tab-width) (mod ind tab-width))
  1113. ind))
  1114. (bullet (org-list-bullet-string (org-list-get-bullet item struct)))
  1115. (box (when checkbox "[ ]"))
  1116. (text-cut
  1117. (and (not beforep) split-line-p
  1118. (progn
  1119. (goto-char pos)
  1120. ;; If POS is greater than ITEM-END, then point is
  1121. ;; in some white lines after the end of the list.
  1122. ;; Those must be removed, or they will be left,
  1123. ;; stacking up after the list.
  1124. (when (< item-end pos)
  1125. (delete-region (1- item-end) (point-at-eol)))
  1126. (skip-chars-backward " \r\t\n")
  1127. (setq pos (point))
  1128. (delete-and-extract-region pos item-end-no-blank))))
  1129. (body (concat bullet (when box (concat box " ")) after-bullet
  1130. (and text-cut
  1131. (if (string-match "\\`[ \t]+" text-cut)
  1132. (replace-match "" t t text-cut)
  1133. text-cut))))
  1134. (item-sep (make-string (1+ blank-nb) ?\n))
  1135. (item-size (+ ind-size (length body) (length item-sep)))
  1136. (size-offset (- item-size (length text-cut))))
  1137. ;; 4. Insert effectively item into buffer.
  1138. (goto-char item)
  1139. (org-indent-to-column ind)
  1140. (insert body item-sep)
  1141. ;; 5. Add new item to STRUCT.
  1142. (mapc (lambda (e)
  1143. (let ((p (car e)) (end (nth 6 e)))
  1144. (cond
  1145. ;; Before inserted item, positions don't change but
  1146. ;; an item ending after insertion has its end shifted
  1147. ;; by SIZE-OFFSET.
  1148. ((< p item)
  1149. (when (> end item) (setcar (nthcdr 6 e) (+ end size-offset))))
  1150. ;; Trivial cases where current item isn't split in
  1151. ;; two. Just shift every item after new one by
  1152. ;; ITEM-SIZE.
  1153. ((or beforep (not split-line-p))
  1154. (setcar e (+ p item-size))
  1155. (setcar (nthcdr 6 e) (+ end item-size)))
  1156. ;; Item is split in two: elements before POS are just
  1157. ;; shifted by ITEM-SIZE. In the case item would end
  1158. ;; after split POS, ending is only shifted by
  1159. ;; SIZE-OFFSET.
  1160. ((< p pos)
  1161. (setcar e (+ p item-size))
  1162. (if (< end pos)
  1163. (setcar (nthcdr 6 e) (+ end item-size))
  1164. (setcar (nthcdr 6 e) (+ end size-offset))))
  1165. ;; Elements after POS are moved into new item.
  1166. ;; Length of ITEM-SEP has to be removed as ITEM-SEP
  1167. ;; doesn't appear in buffer yet.
  1168. ((< p item-end)
  1169. (setcar e (+ p size-offset (- item pos (length item-sep))))
  1170. (if (= end item-end)
  1171. (setcar (nthcdr 6 e) (+ item item-size))
  1172. (setcar (nthcdr 6 e)
  1173. (+ end size-offset
  1174. (- item pos (length item-sep))))))
  1175. ;; Elements at ITEM-END or after are only shifted by
  1176. ;; SIZE-OFFSET.
  1177. (t (setcar e (+ p size-offset))
  1178. (setcar (nthcdr 6 e) (+ end size-offset))))))
  1179. struct)
  1180. (push (list item ind bullet nil box nil (+ item item-size)) struct)
  1181. (setq struct (sort struct (lambda (e1 e2) (< (car e1) (car e2)))))
  1182. ;; 6. If not BEFOREP, new item must appear after ITEM, so
  1183. ;; exchange ITEM with the next item in list. Position cursor
  1184. ;; after bullet, counter, checkbox, and label.
  1185. (if beforep
  1186. (goto-char item)
  1187. (setq struct (org-list-swap-items item (+ item item-size) struct))
  1188. (goto-char (org-list-get-next-item
  1189. item struct (org-list-prevs-alist struct))))
  1190. struct)))
  1191. (defun org-list-delete-item (item struct)
  1192. "Remove ITEM from the list and return the new structure.
  1193. STRUCT is the list structure."
  1194. (let* ((end (org-list-get-item-end item struct))
  1195. (beg (if (= (org-list-get-bottom-point struct) end)
  1196. ;; If ITEM ends with the list, delete blank lines
  1197. ;; before it.
  1198. (save-excursion
  1199. (goto-char item)
  1200. (skip-chars-backward " \r\t\n")
  1201. (min (1+ (point-at-eol)) (point-max)))
  1202. item)))
  1203. ;; Remove item from buffer.
  1204. (delete-region beg end)
  1205. ;; Remove item from structure and shift others items accordingly.
  1206. ;; Don't forget to shift also ending position when appropriate.
  1207. (let ((size (- end beg)))
  1208. (delq nil (mapcar (lambda (e)
  1209. (let ((pos (car e)))
  1210. (cond
  1211. ((< pos item)
  1212. (let ((end-e (nth 6 e)))
  1213. (cond
  1214. ((< end-e item) e)
  1215. ((= end-e item)
  1216. (append (butlast e) (list beg)))
  1217. (t
  1218. (append (butlast e) (list (- end-e size)))))))
  1219. ((< pos end) nil)
  1220. (t
  1221. (cons (- pos size)
  1222. (append (butlast (cdr e))
  1223. (list (- (nth 6 e) size))))))))
  1224. struct)))))
  1225. (defun org-list-send-item (item dest struct)
  1226. "Send ITEM to destination DEST.
  1227. STRUCT is the list structure.
  1228. DEST can have various values.
  1229. If DEST is a buffer position, the function will assume it points
  1230. to another item in the same list as ITEM, and will move the
  1231. latter just before the former.
  1232. If DEST is `begin' \(respectively `end'\), ITEM will be moved at
  1233. the beginning \(respectively end\) of the list it belongs to.
  1234. If DEST is a string like \"N\", where N is an integer, ITEM will
  1235. be moved at the Nth position in the list.
  1236. If DEST is `kill', ITEM will be deleted and its body will be
  1237. added to the kill-ring.
  1238. If DEST is `delete', ITEM will be deleted.
  1239. This function returns, destructively, the new list structure."
  1240. (let* ((prevs (org-list-prevs-alist struct))
  1241. (item-end (org-list-get-item-end item struct))
  1242. ;; Grab full item body minus its bullet.
  1243. (body (org-trim
  1244. (buffer-substring
  1245. (save-excursion
  1246. (goto-char item)
  1247. (looking-at
  1248. (concat "[ \t]*"
  1249. (regexp-quote (org-list-get-bullet item struct))))
  1250. (match-end 0))
  1251. item-end)))
  1252. ;; Change DEST into a buffer position. A trick is needed
  1253. ;; when ITEM is meant to be sent at the end of the list.
  1254. ;; Indeed, by setting locally `org-M-RET-may-split-line' to
  1255. ;; nil and insertion point (INS-POINT) to the first line's
  1256. ;; end of the last item, we ensure the new item will be
  1257. ;; inserted after the last item, and not after any of its
  1258. ;; hypothetical sub-items.
  1259. (ins-point (cond
  1260. ((or (eq dest 'kill) (eq dest 'delete)))
  1261. ((eq dest 'begin)
  1262. (setq dest (org-list-get-list-begin item struct prevs)))
  1263. ((eq dest 'end)
  1264. (setq dest (org-list-get-list-end item struct prevs))
  1265. (save-excursion
  1266. (goto-char (org-list-get-last-item item struct prevs))
  1267. (point-at-eol)))
  1268. ((string-match "\\`[0-9]+\\'" dest)
  1269. (let* ((all (org-list-get-all-items item struct prevs))
  1270. (len (length all))
  1271. (index (mod (string-to-number dest) len)))
  1272. (if (not (zerop index))
  1273. (setq dest (nth (1- index) all))
  1274. ;; Send ITEM at the end of the list.
  1275. (setq dest (org-list-get-list-end item struct prevs))
  1276. (save-excursion
  1277. (goto-char
  1278. (org-list-get-last-item item struct prevs))
  1279. (point-at-eol)))))
  1280. (t dest)))
  1281. (org-M-RET-may-split-line nil))
  1282. (cond
  1283. ((eq dest 'delete) (org-list-delete-item item struct))
  1284. ((eq dest 'kill)
  1285. (kill-new body)
  1286. (org-list-delete-item item struct))
  1287. ((and (integerp dest) (/= item ins-point))
  1288. (setq item (copy-marker item))
  1289. (setq struct (org-list-insert-item ins-point struct prevs nil body))
  1290. ;; 1. Structure returned by `org-list-insert-item' may not be
  1291. ;; accurate, as it cannot see sub-items included in BODY.
  1292. ;; Thus, first compute the real structure so far.
  1293. (let ((moved-items
  1294. (cons (marker-position item)
  1295. (org-list-get-subtree (marker-position item) struct)))
  1296. (new-end (org-list-get-item-end (point) struct))
  1297. (old-end (org-list-get-item-end (marker-position item) struct))
  1298. (new-item (point))
  1299. (shift (- (point) item)))
  1300. ;; 1.1. Remove the item just created in structure.
  1301. (setq struct (delete (assq new-item struct) struct))
  1302. ;; 1.2. Copy ITEM and any of its sub-items at NEW-ITEM.
  1303. (setq struct (sort
  1304. (append
  1305. struct
  1306. (mapcar (lambda (e)
  1307. (let* ((cell (assq e struct))
  1308. (pos (car cell))
  1309. (end (nth 6 cell)))
  1310. (cons (+ pos shift)
  1311. (append (butlast (cdr cell))
  1312. (list (if (= end old-end)
  1313. new-end
  1314. (+ end shift)))))))
  1315. moved-items))
  1316. (lambda (e1 e2) (< (car e1) (car e2))))))
  1317. ;; 2. Eventually delete extra copy of the item and clean marker.
  1318. (prog1
  1319. (org-list-delete-item (marker-position item) struct)
  1320. (move-marker item nil)))
  1321. (t struct))))
  1322. (defun org-list-struct-outdent (start end struct parents)
  1323. "Outdent items between positions START and END.
  1324. STRUCT is the list structure. PARENTS is the alist of items'
  1325. parents, as returned by `org-list-parents-alist'.
  1326. START is included, END excluded."
  1327. (let* (acc
  1328. (out (lambda (cell)
  1329. (let* ((item (car cell))
  1330. (parent (cdr cell)))
  1331. (cond
  1332. ;; Item not yet in zone: keep association.
  1333. ((< item start) cell)
  1334. ;; Item out of zone: follow associations in ACC.
  1335. ((>= item end)
  1336. (let ((convert (and parent (assq parent acc))))
  1337. (if convert (cons item (cdr convert)) cell)))
  1338. ;; Item has no parent: error
  1339. ((not parent)
  1340. (error "Cannot outdent top-level items"))
  1341. ;; Parent is outdented: keep association.
  1342. ((>= parent start)
  1343. (push (cons parent item) acc) cell)
  1344. (t
  1345. ;; Parent isn't outdented: reparent to grand-parent.
  1346. (let ((grand-parent (org-list-get-parent
  1347. parent struct parents)))
  1348. (push (cons parent item) acc)
  1349. (cons item grand-parent))))))))
  1350. (mapcar out parents)))
  1351. (defun org-list-struct-indent (start end struct parents prevs)
  1352. "Indent items between positions START and END.
  1353. STRUCT is the list structure. PARENTS is the alist of parents
  1354. and PREVS is the alist of previous items, returned by,
  1355. respectively, `org-list-parents-alist' and
  1356. `org-list-prevs-alist'.
  1357. START is included and END excluded.
  1358. STRUCT may be modified if `org-list-demote-modify-bullet' matches
  1359. bullets between START and END."
  1360. (let* (acc
  1361. (set-assoc (lambda (cell) (push cell acc) cell))
  1362. (change-bullet-maybe
  1363. (function
  1364. (lambda (item)
  1365. (let* ((bul (org-trim (org-list-get-bullet item struct)))
  1366. (new-bul-p (cdr (assoc bul org-list-demote-modify-bullet))))
  1367. (when new-bul-p (org-list-set-bullet item struct new-bul-p))))))
  1368. (ind
  1369. (lambda (cell)
  1370. (let* ((item (car cell))
  1371. (parent (cdr cell)))
  1372. (cond
  1373. ;; Item not yet in zone: keep association.
  1374. ((< item start) cell)
  1375. ((>= item end)
  1376. ;; Item out of zone: follow associations in ACC.
  1377. (let ((convert (assq parent acc)))
  1378. (if convert (cons item (cdr convert)) cell)))
  1379. (t
  1380. ;; Item is in zone...
  1381. (let ((prev (org-list-get-prev-item item struct prevs)))
  1382. ;; Check if bullet needs to be changed.
  1383. (funcall change-bullet-maybe item)
  1384. (cond
  1385. ;; First item indented but not parent: error
  1386. ((and (not prev) (< parent start))
  1387. (error "Cannot indent the first item of a list"))
  1388. ;; First item and parent indented: keep same
  1389. ;; parent.
  1390. ((not prev) (funcall set-assoc cell))
  1391. ;; Previous item not indented: reparent to it.
  1392. ((< prev start) (funcall set-assoc (cons item prev)))
  1393. ;; Previous item indented: reparent like it.
  1394. (t
  1395. (funcall set-assoc
  1396. (cons item (cdr (assq prev acc)))))))))))))
  1397. (mapcar ind parents)))
  1398. ;;; Repairing structures
  1399. (defun org-list-use-alpha-bul-p (first struct prevs)
  1400. "Non-nil if list starting at FIRST can have alphabetical bullets.
  1401. STRUCT is list structure. PREVS is the alist of previous items,
  1402. as returned by `org-list-prevs-alist'."
  1403. (and org-alphabetical-lists
  1404. (catch 'exit
  1405. (let ((item first) (ascii 64) (case-fold-search nil))
  1406. ;; Pretend that bullets are uppercase and check if alphabet
  1407. ;; is sufficient, taking counters into account.
  1408. (while item
  1409. (let ((bul (org-list-get-bullet item struct))
  1410. (count (org-list-get-counter item struct)))
  1411. ;; Virtually determine current bullet
  1412. (if (and count (string-match "[a-zA-Z]" count))
  1413. ;; Counters are not case-sensitive.
  1414. (setq ascii (string-to-char (upcase count)))
  1415. (setq ascii (1+ ascii)))
  1416. ;; Test if bullet would be over z or Z.
  1417. (if (> ascii 90)
  1418. (throw 'exit nil)
  1419. (setq item (org-list-get-next-item item struct prevs)))))
  1420. ;; All items checked. All good.
  1421. t))))
  1422. (defun org-list-inc-bullet-maybe (bullet)
  1423. "Increment BULLET if applicable."
  1424. (let ((case-fold-search nil))
  1425. (cond
  1426. ;; Num bullet: increment it.
  1427. ((string-match "[0-9]+" bullet)
  1428. (replace-match
  1429. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1430. nil nil bullet))
  1431. ;; Alpha bullet: increment it.
  1432. ((string-match "[A-Za-z]" bullet)
  1433. (replace-match
  1434. (char-to-string (1+ (string-to-char (match-string 0 bullet))))
  1435. nil nil bullet))
  1436. ;; Unordered bullet: leave it.
  1437. (t bullet))))
  1438. (defun org-list-struct-fix-bul (struct prevs)
  1439. "Verify and correct bullets in STRUCT.
  1440. PREVS is the alist of previous items, as returned by
  1441. `org-list-prevs-alist'.
  1442. This function modifies STRUCT."
  1443. (let ((case-fold-search nil)
  1444. (fix-bul
  1445. (function
  1446. ;; Set bullet of ITEM in STRUCT, depending on the type of
  1447. ;; first item of the list, the previous bullet and counter
  1448. ;; if any.
  1449. (lambda (item)
  1450. (let* ((prev (org-list-get-prev-item item struct prevs))
  1451. (prev-bul (and prev (org-list-get-bullet prev struct)))
  1452. (counter (org-list-get-counter item struct))
  1453. (bullet (org-list-get-bullet item struct))
  1454. (alphap (and (not prev)
  1455. (org-list-use-alpha-bul-p item struct prevs))))
  1456. (org-list-set-bullet
  1457. item struct
  1458. (org-list-bullet-string
  1459. (cond
  1460. ;; Alpha counter in alpha list: use counter.
  1461. ((and prev counter
  1462. (string-match "[a-zA-Z]" counter)
  1463. (string-match "[a-zA-Z]" prev-bul))
  1464. ;; Use cond to be sure `string-match' is used in
  1465. ;; both cases.
  1466. (let ((real-count
  1467. (cond
  1468. ((string-match "[a-z]" prev-bul) (downcase counter))
  1469. ((string-match "[A-Z]" prev-bul) (upcase counter)))))
  1470. (replace-match real-count nil nil prev-bul)))
  1471. ;; Num counter in a num list: use counter.
  1472. ((and prev counter
  1473. (string-match "[0-9]+" counter)
  1474. (string-match "[0-9]+" prev-bul))
  1475. (replace-match counter nil nil prev-bul))
  1476. ;; No counter: increase, if needed, previous bullet.
  1477. (prev
  1478. (org-list-inc-bullet-maybe (org-list-get-bullet prev struct)))
  1479. ;; Alpha counter at first item: use counter.
  1480. ((and counter (org-list-use-alpha-bul-p item struct prevs)
  1481. (string-match "[A-Za-z]" counter)
  1482. (string-match "[A-Za-z]" bullet))
  1483. (let ((real-count
  1484. (cond
  1485. ((string-match "[a-z]" bullet) (downcase counter))
  1486. ((string-match "[A-Z]" bullet) (upcase counter)))))
  1487. (replace-match real-count nil nil bullet)))
  1488. ;; Num counter at first item: use counter.
  1489. ((and counter
  1490. (string-match "[0-9]+" counter)
  1491. (string-match "[0-9]+" bullet))
  1492. (replace-match counter nil nil bullet))
  1493. ;; First bullet is alpha uppercase: use "A".
  1494. ((and alphap (string-match "[A-Z]" bullet))
  1495. (replace-match "A" nil nil bullet))
  1496. ;; First bullet is alpha lowercase: use "a".
  1497. ((and alphap (string-match "[a-z]" bullet))
  1498. (replace-match "a" nil nil bullet))
  1499. ;; First bullet is num: use "1".
  1500. ((string-match "\\([0-9]+\\|[A-Za-z]\\)" bullet)
  1501. (replace-match "1" nil nil bullet))
  1502. ;; Not an ordered list: keep bullet.
  1503. (t bullet)))))))))
  1504. (mapc fix-bul (mapcar 'car struct))))
  1505. (defun org-list-struct-fix-ind (struct parents &optional bullet-size)
  1506. "Verify and correct indentation in STRUCT.
  1507. PARENTS is the alist of parents, as returned by
  1508. `org-list-parents-alist'.
  1509. If numeric optional argument BULLET-SIZE is set, assume all
  1510. bullets in list have this length to determine new indentation.
  1511. This function modifies STRUCT."
  1512. (let* ((ancestor (org-list-get-top-point struct))
  1513. (top-ind (org-list-get-ind ancestor struct))
  1514. (new-ind
  1515. (lambda (item)
  1516. (let ((parent (org-list-get-parent item struct parents)))
  1517. (if parent
  1518. ;; Indent like parent + length of parent's bullet +
  1519. ;; sub-list offset.
  1520. (org-list-set-ind
  1521. item struct (+ (or bullet-size
  1522. (length
  1523. (org-list-get-bullet parent struct)))
  1524. (org-list-get-ind parent struct)
  1525. org-list-indent-offset))
  1526. ;; If no parent, indent like top-point.
  1527. (org-list-set-ind item struct top-ind))))))
  1528. (mapc new-ind (mapcar 'car (cdr struct)))))
  1529. (defun org-list-struct-fix-box (struct parents prevs &optional ordered)
  1530. "Verify and correct checkboxes in STRUCT.
  1531. PARENTS is the alist of parents and PREVS is the alist of
  1532. previous items, as returned by, respectively,
  1533. `org-list-parents-alist' and `org-list-prevs-alist'.
  1534. If ORDERED is non-nil, a checkbox can only be checked when every
  1535. checkbox before it is checked too. If there was an attempt to
  1536. break this rule, the function will return the blocking item. In
  1537. all others cases, the return value will be nil.
  1538. This function modifies STRUCT."
  1539. (let ((all-items (mapcar 'car struct))
  1540. (set-parent-box
  1541. (function
  1542. (lambda (item)
  1543. (let* ((box-list
  1544. (mapcar (lambda (child)
  1545. (org-list-get-checkbox child struct))
  1546. (org-list-get-children item struct parents))))
  1547. (org-list-set-checkbox
  1548. item struct
  1549. (cond
  1550. ((and (member "[ ]" box-list) (member "[X]" box-list)) "[-]")
  1551. ((member "[-]" box-list) "[-]")
  1552. ((member "[X]" box-list) "[X]")
  1553. ((member "[ ]" box-list) "[ ]")
  1554. ;; Parent has no boxed child: leave box as-is.
  1555. (t (org-list-get-checkbox item struct))))))))
  1556. parent-list)
  1557. ;; 1. List all parents with a checkbox.
  1558. (mapc
  1559. (lambda (e)
  1560. (let* ((parent (org-list-get-parent e struct parents))
  1561. (parent-box-p (org-list-get-checkbox parent struct)))
  1562. (when (and parent-box-p (not (memq parent parent-list)))
  1563. (push parent parent-list))))
  1564. all-items)
  1565. ;; 2. Sort those parents by decreasing indentation.
  1566. (setq parent-list (sort parent-list
  1567. (lambda (e1 e2)
  1568. (> (org-list-get-ind e1 struct)
  1569. (org-list-get-ind e2 struct)))))
  1570. ;; 3. For each parent, get all children's checkboxes to determine
  1571. ;; and set its checkbox accordingly.
  1572. (mapc set-parent-box parent-list)
  1573. ;; 4. If ORDERED is set, see if we need to uncheck some boxes.
  1574. (when ordered
  1575. (let* ((box-list
  1576. (mapcar (lambda (e) (org-list-get-checkbox e struct)) all-items))
  1577. (after-unchecked (member "[ ]" box-list)))
  1578. ;; There are boxes checked after an unchecked one: fix that.
  1579. (when (member "[X]" after-unchecked)
  1580. (let ((index (- (length struct) (length after-unchecked))))
  1581. (mapc (lambda (e) (org-list-set-checkbox e struct "[ ]"))
  1582. (nthcdr index all-items))
  1583. ;; Verify once again the structure, without ORDERED.
  1584. (org-list-struct-fix-box struct parents prevs nil)
  1585. ;; Return blocking item.
  1586. (nth index all-items)))))))
  1587. (defun org-list-struct-fix-item-end (struct)
  1588. "Verify and correct each item end position in STRUCT.
  1589. This function modifies STRUCT."
  1590. (let (end-list acc-end)
  1591. (mapc (lambda (e)
  1592. (let* ((pos (car e))
  1593. (ind-pos (org-list-get-ind pos struct))
  1594. (end-pos (org-list-get-item-end pos struct)))
  1595. (unless (assq end-pos struct)
  1596. ;; To determine real ind of an ending position that is
  1597. ;; not at an item, we have to find the item it belongs
  1598. ;; to: it is the last item (ITEM-UP), whose ending is
  1599. ;; further than the position we're interested in.
  1600. (let ((item-up (assoc-default end-pos acc-end '>)))
  1601. (push (cons
  1602. ;; Else part is for the bottom point.
  1603. (if item-up (+ (org-list-get-ind item-up struct) 2) 0)
  1604. end-pos)
  1605. end-list)))
  1606. (push (cons ind-pos pos) end-list)
  1607. (push (cons end-pos pos) acc-end)))
  1608. struct)
  1609. (setq end-list (sort end-list (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
  1610. (org-list-struct-assoc-end struct end-list)))
  1611. (defun org-list-struct-apply-struct (struct old-struct)
  1612. "Apply set difference between STRUCT and OLD-STRUCT to the buffer.
  1613. OLD-STRUCT is the structure before any modifications, and STRUCT
  1614. the structure to be applied. The function will only modify parts
  1615. of the list which have changed.
  1616. Initial position of cursor is restored after the changes."
  1617. (let* ((origin (point-marker))
  1618. (inlinetask-re (and (featurep 'org-inlinetask)
  1619. (org-inlinetask-outline-regexp)))
  1620. (item-re (org-item-re))
  1621. (box-rule-p (cdr (assq 'checkbox org-list-automatic-rules)))
  1622. (shift-body-ind
  1623. (function
  1624. ;; Shift the indentation between END and BEG by DELTA.
  1625. ;; Start from the line before END.
  1626. (lambda (end beg delta)
  1627. (goto-char end)
  1628. (skip-chars-backward " \r\t\n")
  1629. (beginning-of-line)
  1630. (while (or (> (point) beg)
  1631. (and (= (point) beg)
  1632. (not (looking-at item-re))))
  1633. (cond
  1634. ;; Skip inline tasks.
  1635. ((and inlinetask-re (looking-at inlinetask-re))
  1636. (org-inlinetask-goto-beginning))
  1637. ;; Shift only non-empty lines.
  1638. ((org-looking-at-p "^[ \t]*\\S-")
  1639. (let ((i (org-get-indentation)))
  1640. (org-indent-line-to (+ i delta)))))
  1641. (forward-line -1)))))
  1642. (modify-item
  1643. (function
  1644. ;; Replace ITEM first line elements with new elements from
  1645. ;; STRUCT, if appropriate.
  1646. (lambda (item)
  1647. (goto-char item)
  1648. (let* ((new-ind (org-list-get-ind item struct))
  1649. (old-ind (org-get-indentation))
  1650. (new-bul (org-list-bullet-string
  1651. (org-list-get-bullet item struct)))
  1652. (old-bul (org-list-get-bullet item old-struct))
  1653. (new-box (org-list-get-checkbox item struct)))
  1654. (looking-at org-list-full-item-re)
  1655. ;; a. Replace bullet
  1656. (unless (equal old-bul new-bul)
  1657. (replace-match new-bul nil nil nil 1))
  1658. ;; b. Replace checkbox.
  1659. (cond
  1660. ((and new-box box-rule-p
  1661. (save-match-data (org-at-item-description-p)))
  1662. (message "Cannot add a checkbox to a description list item"))
  1663. ((equal (match-string 3) new-box))
  1664. ((and (match-string 3) new-box)
  1665. (replace-match new-box nil nil nil 3))
  1666. ((match-string 3)
  1667. (looking-at ".*?\\([ \t]*\\[[ X-]\\]\\)")
  1668. (replace-match "" nil nil nil 1))
  1669. (t (let ((counterp (match-end 2)))
  1670. (goto-char (if counterp (1+ counterp) (match-end 1)))
  1671. (insert (concat new-box (unless counterp " "))))))
  1672. ;; c. Indent item to appropriate column.
  1673. (unless (= new-ind old-ind)
  1674. (delete-region (goto-char (point-at-bol))
  1675. (progn (skip-chars-forward " \t") (point)))
  1676. (indent-to new-ind)))))))
  1677. ;; 1. First get list of items and position endings. We maintain
  1678. ;; two alists: ITM-SHIFT, determining indentation shift needed
  1679. ;; at item, and END-POS, a pseudo-alist where key is ending
  1680. ;; position and value point.
  1681. (let (end-list acc-end itm-shift all-ends sliced-struct)
  1682. (mapc (lambda (e)
  1683. (let* ((pos (car e))
  1684. (ind-pos (org-list-get-ind pos struct))
  1685. (ind-old (org-list-get-ind pos old-struct))
  1686. (bul-pos (org-list-get-bullet pos struct))
  1687. (bul-old (org-list-get-bullet pos old-struct))
  1688. (ind-shift (- (+ ind-pos (length bul-pos))
  1689. (+ ind-old (length bul-old))))
  1690. (end-pos (org-list-get-item-end pos old-struct)))
  1691. (push (cons pos ind-shift) itm-shift)
  1692. (unless (assq end-pos old-struct)
  1693. ;; To determine real ind of an ending position that
  1694. ;; is not at an item, we have to find the item it
  1695. ;; belongs to: it is the last item (ITEM-UP), whose
  1696. ;; ending is further than the position we're
  1697. ;; interested in.
  1698. (let ((item-up (assoc-default end-pos acc-end '>)))
  1699. (push (cons end-pos item-up) end-list)))
  1700. (push (cons end-pos pos) acc-end)))
  1701. old-struct)
  1702. ;; 2. Slice the items into parts that should be shifted by the
  1703. ;; same amount of indentation. The slices are returned in
  1704. ;; reverse order so changes modifying buffer do not change
  1705. ;; positions they refer to.
  1706. (setq all-ends (sort (append (mapcar 'car itm-shift)
  1707. (org-uniquify (mapcar 'car end-list)))
  1708. '<))
  1709. (while (cdr all-ends)
  1710. (let* ((up (pop all-ends))
  1711. (down (car all-ends))
  1712. (ind (if (assq up struct)
  1713. (cdr (assq up itm-shift))
  1714. (cdr (assq (cdr (assq up end-list)) itm-shift)))))
  1715. (push (list down up ind) sliced-struct)))
  1716. ;; 3. Shift each slice in buffer, provided delta isn't 0, from
  1717. ;; end to beginning. Take a special action when beginning is
  1718. ;; at item bullet.
  1719. (mapc (lambda (e)
  1720. (unless (zerop (nth 2 e)) (apply shift-body-ind e))
  1721. (let* ((beg (nth 1 e))
  1722. (cell (assq beg struct)))
  1723. (unless (or (not cell) (equal cell (assq beg old-struct)))
  1724. (funcall modify-item beg))))
  1725. sliced-struct))
  1726. ;; 4. Go back to initial position and clean marker.
  1727. (goto-char origin)
  1728. (move-marker origin nil)))
  1729. (defun org-list-write-struct (struct parents &optional old-struct)
  1730. "Correct bullets, checkboxes and indentation in list at point.
  1731. STRUCT is the list structure. PARENTS is the alist of parents,
  1732. as returned by `org-list-parents-alist'.
  1733. When non-nil, optional argument OLD-STRUCT is the reference
  1734. structure of the list. It should be provided whenever STRUCT
  1735. doesn't correspond anymore to the real list in buffer."
  1736. ;; Order of functions matters here: checkboxes and endings need
  1737. ;; correct indentation to be set, and indentation needs correct
  1738. ;; bullets.
  1739. ;;
  1740. ;; 0. Save a copy of structure before modifications
  1741. (let ((old-struct (or old-struct (copy-tree struct))))
  1742. ;; 1. Set a temporary, but coherent with PARENTS, indentation in
  1743. ;; order to get items endings and bullets properly
  1744. (org-list-struct-fix-ind struct parents 2)
  1745. ;; 2. Fix each item end to get correct prevs alist.
  1746. (org-list-struct-fix-item-end struct)
  1747. ;; 3. Get bullets right.
  1748. (let ((prevs (org-list-prevs-alist struct)))
  1749. (org-list-struct-fix-bul struct prevs)
  1750. ;; 4. Now get real indentation.
  1751. (org-list-struct-fix-ind struct parents)
  1752. ;; 5. Eventually fix checkboxes.
  1753. (org-list-struct-fix-box struct parents prevs))
  1754. ;; 6. Apply structure modifications to buffer.
  1755. (org-list-struct-apply-struct struct old-struct)))
  1756. ;;; Misc Tools
  1757. (defun org-apply-on-list (function init-value &rest args)
  1758. "Call FUNCTION on each item of the list at point.
  1759. FUNCTION must be called with at least one argument: INIT-VALUE,
  1760. that will contain the value returned by the function at the
  1761. previous item, plus ARGS extra arguments.
  1762. FUNCTION is applied on items in reverse order.
  1763. As an example, \(org-apply-on-list \(lambda \(result\) \(1+ result\)\) 0\)
  1764. will return the number of items in the current list.
  1765. Sublists of the list are skipped. Cursor is always at the
  1766. beginning of the item."
  1767. (let* ((struct (org-list-struct))
  1768. (prevs (org-list-prevs-alist struct))
  1769. (item (copy-marker (point-at-bol)))
  1770. (all (org-list-get-all-items (marker-position item) struct prevs))
  1771. (value init-value))
  1772. (mapc (lambda (e)
  1773. (goto-char e)
  1774. (setq value (apply function value args)))
  1775. (nreverse all))
  1776. (goto-char item)
  1777. (move-marker item nil)
  1778. value))
  1779. (defun org-list-set-item-visibility (item struct view)
  1780. "Set visibility of ITEM in STRUCT to VIEW.
  1781. Possible values are: `folded', `children' or `subtree'. See
  1782. `org-cycle' for more information."
  1783. (cond
  1784. ((eq view 'folded)
  1785. (let ((item-end (org-list-get-item-end-before-blank item struct)))
  1786. ;; Hide from eol
  1787. (outline-flag-region (save-excursion (goto-char item) (point-at-eol))
  1788. item-end t)))
  1789. ((eq view 'children)
  1790. ;; First show everything.
  1791. (org-list-set-item-visibility item struct 'subtree)
  1792. ;; Then fold every child.
  1793. (let* ((parents (org-list-parents-alist struct))
  1794. (children (org-list-get-children item struct parents)))
  1795. (mapc (lambda (e)
  1796. (org-list-set-item-visibility e struct 'folded))
  1797. children)))
  1798. ((eq view 'subtree)
  1799. ;; Show everything
  1800. (let ((item-end (org-list-get-item-end item struct)))
  1801. (outline-flag-region item item-end nil)))))
  1802. (defun org-list-item-body-column (item)
  1803. "Return column at which body of ITEM should start."
  1804. (let (bpos bcol tpos tcol)
  1805. (save-excursion
  1806. (goto-char item)
  1807. (looking-at "[ \t]*\\(\\S-+\\)\\(.*[ \t]+::\\)?[ \t]+")
  1808. (setq bpos (match-beginning 1) tpos (match-end 0)
  1809. bcol (progn (goto-char bpos) (current-column))
  1810. tcol (progn (goto-char tpos) (current-column)))
  1811. (when (> tcol (+ bcol org-description-max-indent))
  1812. (setq tcol (+ bcol 5))))
  1813. tcol))
  1814. ;;; Interactive functions
  1815. (defalias 'org-list-get-item-begin 'org-in-item-p)
  1816. (defun org-beginning-of-item ()
  1817. "Go to the beginning of the current item.
  1818. Throw an error when not in a list."
  1819. (interactive)
  1820. (let ((begin (org-in-item-p)))
  1821. (if begin (goto-char begin) (error "Not in an item"))))
  1822. (defun org-beginning-of-item-list ()
  1823. "Go to the beginning item of the current list or sublist.
  1824. Throw an error when not in a list."
  1825. (interactive)
  1826. (let ((begin (org-in-item-p)))
  1827. (if (not begin)
  1828. (error "Not in an item")
  1829. (goto-char begin)
  1830. (let* ((struct (org-list-struct))
  1831. (prevs (org-list-prevs-alist struct)))
  1832. (goto-char (org-list-get-list-begin begin struct prevs))))))
  1833. (defun org-end-of-item-list ()
  1834. "Go to the end of the current list or sublist.
  1835. Throw an error when not in a list."
  1836. (interactive)
  1837. (let ((begin (org-in-item-p)))
  1838. (if (not begin)
  1839. (error "Not in an item")
  1840. (goto-char begin)
  1841. (let* ((struct (org-list-struct))
  1842. (prevs (org-list-prevs-alist struct)))
  1843. (goto-char (org-list-get-list-end begin struct prevs))))))
  1844. (defun org-end-of-item ()
  1845. "Go to the end of the current item.
  1846. Throw an error when not in a list."
  1847. (interactive)
  1848. (let ((begin (org-in-item-p)))
  1849. (if (not begin)
  1850. (error "Not in an item")
  1851. (goto-char begin)
  1852. (let ((struct (org-list-struct)))
  1853. (goto-char (org-list-get-item-end begin struct))))))
  1854. (defun org-previous-item ()
  1855. "Move to the beginning of the previous item.
  1856. Throw an error when not in a list. Also throw an error when at
  1857. first item, unless `org-list-use-circular-motion' is non-nil."
  1858. (interactive)
  1859. (let ((item (org-in-item-p)))
  1860. (if (not item)
  1861. (error "Not in an item")
  1862. (goto-char item)
  1863. (let* ((struct (org-list-struct))
  1864. (prevs (org-list-prevs-alist struct))
  1865. (prevp (org-list-get-prev-item item struct prevs)))
  1866. (cond
  1867. (prevp (goto-char prevp))
  1868. (org-list-use-circular-motion
  1869. (goto-char (org-list-get-last-item item struct prevs)))
  1870. (t (error "On first item")))))))
  1871. (defun org-next-item ()
  1872. "Move to the beginning of the next item.
  1873. Throw an error when not in a list. Also throw an error when at
  1874. last item, unless `org-list-use-circular-motion' is non-nil."
  1875. (interactive)
  1876. (let ((item (org-in-item-p)))
  1877. (if (not item)
  1878. (error "Not in an item")
  1879. (goto-char item)
  1880. (let* ((struct (org-list-struct))
  1881. (prevs (org-list-prevs-alist struct))
  1882. (prevp (org-list-get-next-item item struct prevs)))
  1883. (cond
  1884. (prevp (goto-char prevp))
  1885. (org-list-use-circular-motion
  1886. (goto-char (org-list-get-first-item item struct prevs)))
  1887. (t (error "On last item")))))))
  1888. (defun org-move-item-down ()
  1889. "Move the item at point down, i.e. swap with following 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. (next-item (org-list-get-next-item (point-at-bol) struct prevs)))
  1899. (unless (or next-item org-list-use-circular-motion)
  1900. (error "Cannot move this item further down"))
  1901. (if (not next-item)
  1902. (setq struct (org-list-send-item item 'begin struct))
  1903. (setq struct (org-list-swap-items item next-item struct))
  1904. (goto-char
  1905. (org-list-get-next-item item struct (org-list-prevs-alist struct))))
  1906. (org-list-write-struct struct (org-list-parents-alist struct))
  1907. (org-move-to-column col)))
  1908. (defun org-move-item-up ()
  1909. "Move the item at point up, i.e. swap with previous item.
  1910. Sub-items (items with larger indentation) are considered part of
  1911. the item, so this really moves item trees."
  1912. (interactive)
  1913. (unless (org-at-item-p) (error "Not at an item"))
  1914. (let* ((col (current-column))
  1915. (item (point-at-bol))
  1916. (struct (org-list-struct))
  1917. (prevs (org-list-prevs-alist struct))
  1918. (prev-item (org-list-get-prev-item (point-at-bol) struct prevs)))
  1919. (unless (or prev-item org-list-use-circular-motion)
  1920. (error "Cannot move this item further up"))
  1921. (if (not prev-item)
  1922. (setq struct (org-list-send-item item 'end struct))
  1923. (setq struct (org-list-swap-items prev-item item struct)))
  1924. (org-list-write-struct struct (org-list-parents-alist struct))
  1925. (org-move-to-column col)))
  1926. (defun org-insert-item (&optional checkbox)
  1927. "Insert a new item at the current level.
  1928. If cursor is before first character after bullet of the item, the
  1929. new item will be created before the current one.
  1930. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  1931. Return t when things worked, nil when we are not in an item, or
  1932. item is invisible."
  1933. (let ((itemp (org-in-item-p))
  1934. (pos (point)))
  1935. ;; If cursor isn't is a list or if list is invisible, return nil.
  1936. (unless (or (not itemp)
  1937. (save-excursion
  1938. (goto-char itemp)
  1939. (outline-invisible-p)))
  1940. (if (save-excursion
  1941. (goto-char itemp)
  1942. (org-at-item-timer-p))
  1943. ;; Timer list: delegate to `org-timer-item'.
  1944. (progn (org-timer-item) t)
  1945. (let* ((struct (save-excursion (goto-char itemp)
  1946. (org-list-struct)))
  1947. (prevs (org-list-prevs-alist struct))
  1948. ;; If we're in a description list, ask for the new term.
  1949. (desc (when (org-list-get-tag itemp struct)
  1950. (concat (read-string "Term: ") " :: ")))
  1951. ;; Don't insert a checkbox if checkbox rule is applied
  1952. ;; and it is a description item.
  1953. (checkp (and checkbox
  1954. (or (not desc)
  1955. (not (cdr (assq 'checkbox
  1956. org-list-automatic-rules)))))))
  1957. (setq struct
  1958. (org-list-insert-item pos struct prevs checkp desc))
  1959. (org-list-write-struct struct (org-list-parents-alist struct))
  1960. (when checkp (org-update-checkbox-count-maybe))
  1961. (looking-at org-list-full-item-re)
  1962. (goto-char (match-end 0))
  1963. t)))))
  1964. (defun org-mark-list ()
  1965. "Mark the current list.
  1966. If this is a sublist, only mark the sublist."
  1967. (interactive)
  1968. (let* ((item (org-list-get-item-begin))
  1969. (struct (org-list-struct))
  1970. (prevs (org-list-prevs-alist struct))
  1971. (lbeg (org-list-get-list-begin item struct prevs))
  1972. (lend (org-list-get-list-end item struct prevs)))
  1973. (push-mark lend nil t)
  1974. (goto-char lbeg)))
  1975. (defun org-list-repair ()
  1976. "Fix indentation, bullets and checkboxes is the list at point."
  1977. (interactive)
  1978. (unless (org-at-item-p) (error "This is not a list"))
  1979. (let* ((struct (org-list-struct))
  1980. (parents (org-list-parents-alist struct)))
  1981. (org-list-write-struct struct parents)))
  1982. (defun org-cycle-list-bullet (&optional which)
  1983. "Cycle through the different itemize/enumerate bullets.
  1984. This cycle the entire list level through the sequence:
  1985. `-' -> `+' -> `*' -> `1.' -> `1)'
  1986. If WHICH is a valid string, use that as the new bullet. If WHICH
  1987. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1988. `previous', cycle backwards."
  1989. (interactive "P")
  1990. (unless (org-at-item-p) (error "Not at an item"))
  1991. (save-excursion
  1992. (beginning-of-line)
  1993. (let* ((struct (org-list-struct))
  1994. (parents (org-list-parents-alist struct))
  1995. (prevs (org-list-prevs-alist struct))
  1996. (list-beg (org-list-get-first-item (point) struct prevs))
  1997. (bullet (org-list-get-bullet list-beg struct))
  1998. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1999. (alpha-p (org-list-use-alpha-bul-p list-beg struct prevs))
  2000. (case-fold-search nil)
  2001. (current (cond
  2002. ((string-match "[a-z]\\." bullet) "a.")
  2003. ((string-match "[a-z])" bullet) "a)")
  2004. ((string-match "[A-Z]\\." bullet) "A.")
  2005. ((string-match "[A-Z])" bullet) "A)")
  2006. ((string-match "\\." bullet) "1.")
  2007. ((string-match ")" bullet) "1)")
  2008. (t (org-trim bullet))))
  2009. ;; Compute list of possible bullets, depending on context.
  2010. (bullet-list
  2011. (append '("-" "+" )
  2012. ;; *-bullets are not allowed at column 0.
  2013. (unless (and bullet-rule-p
  2014. (looking-at "\\S-")) '("*"))
  2015. ;; Description items cannot be numbered.
  2016. (unless (or (eq org-plain-list-ordered-item-terminator ?\))
  2017. (and bullet-rule-p (org-at-item-description-p)))
  2018. '("1."))
  2019. (unless (or (eq org-plain-list-ordered-item-terminator ?.)
  2020. (and bullet-rule-p (org-at-item-description-p)))
  2021. '("1)"))
  2022. (unless (or (not alpha-p)
  2023. (eq org-plain-list-ordered-item-terminator ?\))
  2024. (and bullet-rule-p (org-at-item-description-p)))
  2025. '("a." "A."))
  2026. (unless (or (not alpha-p)
  2027. (eq org-plain-list-ordered-item-terminator ?.)
  2028. (and bullet-rule-p (org-at-item-description-p)))
  2029. '("a)" "A)"))))
  2030. (len (length bullet-list))
  2031. (item-index (- len (length (member current bullet-list))))
  2032. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  2033. (new (cond
  2034. ((member which bullet-list) which)
  2035. ((numberp which) (funcall get-value which))
  2036. ((eq 'previous which) (funcall get-value (1- item-index)))
  2037. (t (funcall get-value (1+ item-index))))))
  2038. ;; Use a short variation of `org-list-write-struct' as there's
  2039. ;; no need to go through all the steps.
  2040. (let ((old-struct (copy-tree struct)))
  2041. (org-list-set-bullet list-beg struct (org-list-bullet-string new))
  2042. (org-list-struct-fix-bul struct prevs)
  2043. (org-list-struct-fix-ind struct parents)
  2044. (org-list-struct-apply-struct struct old-struct)))))
  2045. (defun org-toggle-checkbox (&optional toggle-presence)
  2046. "Toggle the checkbox in the current line.
  2047. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  2048. double prefix, set checkbox to [-].
  2049. When there is an active region, toggle status or presence of the
  2050. first checkbox there, and make every item inside have the same
  2051. status or presence, respectively.
  2052. If the cursor is in a headline, apply this to all checkbox items
  2053. in the text below the heading, taking as reference the first item
  2054. in subtree, ignoring drawers."
  2055. (interactive "P")
  2056. (save-excursion
  2057. (let* (singlep
  2058. block-item
  2059. lim-up
  2060. lim-down
  2061. (drawer-re (concat "^[ \t]*:\\("
  2062. (mapconcat 'regexp-quote org-drawers "\\|")
  2063. "\\):[ \t]*$"))
  2064. (keyword-re (concat "^[ \t]*\\<\\(" org-scheduled-string
  2065. "\\|" org-deadline-string
  2066. "\\|" org-closed-string
  2067. "\\|" org-clock-string "\\)"
  2068. " *[[<]\\([^]>]+\\)[]>]"))
  2069. (orderedp (org-entry-get nil "ORDERED"))
  2070. (bounds
  2071. ;; In a region, start at first item in region.
  2072. (cond
  2073. ((org-region-active-p)
  2074. (let ((limit (region-end)))
  2075. (goto-char (region-beginning))
  2076. (if (org-list-search-forward (org-item-beginning-re) limit t)
  2077. (setq lim-up (point-at-bol))
  2078. (error "No item in region"))
  2079. (setq lim-down (copy-marker limit))))
  2080. ((org-at-heading-p)
  2081. ;; On an heading, start at first item after drawers and
  2082. ;; time-stamps (scheduled, etc.).
  2083. (let ((limit (save-excursion (outline-next-heading) (point))))
  2084. (forward-line 1)
  2085. (while (or (looking-at drawer-re) (looking-at keyword-re))
  2086. (if (looking-at keyword-re)
  2087. (forward-line 1)
  2088. (re-search-forward "^[ \t]*:END:" limit nil)))
  2089. (if (org-list-search-forward (org-item-beginning-re) limit t)
  2090. (setq lim-up (point-at-bol))
  2091. (error "No item in subtree"))
  2092. (setq lim-down (copy-marker limit))))
  2093. ;; Just one item: set SINGLEP flag.
  2094. ((org-at-item-p)
  2095. (setq singlep t)
  2096. (setq lim-up (point-at-bol)
  2097. lim-down (copy-marker (point-at-eol))))
  2098. (t (error "Not at an item or heading, and no active region"))))
  2099. ;; Determine the checkbox going to be applied to all items
  2100. ;; within bounds.
  2101. (ref-checkbox
  2102. (progn
  2103. (goto-char lim-up)
  2104. (let ((cbox (and (org-at-item-checkbox-p) (match-string 1))))
  2105. (cond
  2106. ((equal toggle-presence '(16)) "[-]")
  2107. ((equal toggle-presence '(4))
  2108. (unless cbox "[ ]"))
  2109. ((equal "[X]" cbox) "[ ]")
  2110. (t "[X]"))))))
  2111. ;; When an item is found within bounds, grab the full list at
  2112. ;; point structure, then: (1) set check-box of all its items
  2113. ;; within bounds to REF-CHECKBOX, (2) fix check-boxes of the
  2114. ;; whole list, (3) move point after the list.
  2115. (goto-char lim-up)
  2116. (while (and (< (point) lim-down)
  2117. (org-list-search-forward (org-item-beginning-re)
  2118. lim-down 'move))
  2119. (let* ((struct (org-list-struct))
  2120. (struct-copy (copy-tree struct))
  2121. (parents (org-list-parents-alist struct))
  2122. (prevs (org-list-prevs-alist struct))
  2123. (bottom (copy-marker (org-list-get-bottom-point struct)))
  2124. (items-to-toggle (org-remove-if
  2125. (lambda (e) (or (< e lim-up) (> e lim-down)))
  2126. (mapcar 'car struct))))
  2127. (mapc (lambda (e) (org-list-set-checkbox
  2128. e struct
  2129. ;; If there is no box at item, leave as-is
  2130. ;; unless function was called with C-u prefix.
  2131. (let ((cur-box (org-list-get-checkbox e struct)))
  2132. (if (or cur-box (equal toggle-presence '(4)))
  2133. ref-checkbox
  2134. cur-box))))
  2135. items-to-toggle)
  2136. (setq block-item (org-list-struct-fix-box
  2137. struct parents prevs orderedp))
  2138. ;; Report some problems due to ORDERED status of subtree.
  2139. ;; If only one box was being checked, throw an error, else,
  2140. ;; only signal problems.
  2141. (cond
  2142. ((and singlep block-item (> lim-up block-item))
  2143. (error
  2144. "Checkbox blocked because of unchecked box at line %d"
  2145. (org-current-line block-item)))
  2146. (block-item
  2147. (message
  2148. "Checkboxes were removed due to unchecked box at line %d"
  2149. (org-current-line block-item))))
  2150. (goto-char bottom)
  2151. (move-marker bottom nil)
  2152. (org-list-struct-apply-struct struct struct-copy)))
  2153. (move-marker lim-down nil)))
  2154. (org-update-checkbox-count-maybe))
  2155. (defun org-reset-checkbox-state-subtree ()
  2156. "Reset all checkboxes in an entry subtree."
  2157. (interactive "*")
  2158. (if (org-before-first-heading-p)
  2159. (error "Not inside a tree")
  2160. (save-restriction
  2161. (save-excursion
  2162. (org-narrow-to-subtree)
  2163. (org-show-subtree)
  2164. (goto-char (point-min))
  2165. (let ((end (point-max)))
  2166. (while (< (point) end)
  2167. (when (org-at-item-checkbox-p)
  2168. (replace-match "[ ]" t t nil 1))
  2169. (beginning-of-line 2)))
  2170. (org-update-checkbox-count-maybe 'all)))))
  2171. (defun org-update-checkbox-count (&optional all)
  2172. "Update the checkbox statistics in the current section.
  2173. This will find all statistic cookies like [57%] and [6/12] and
  2174. update them with the current numbers.
  2175. With optional prefix argument ALL, do this for the whole buffer."
  2176. (interactive "P")
  2177. (save-excursion
  2178. (let ((cookie-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  2179. (box-re "^[ \t]*\\([-+*]\\|\\([0-9]+\\|[A-Za-z]\\)[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?\\(\\[[- X]\\]\\)")
  2180. (recursivep
  2181. (or (not org-hierarchical-checkbox-statistics)
  2182. (string-match "\\<recursive\\>"
  2183. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2184. (bounds (if all
  2185. (cons (point-min) (point-max))
  2186. (cons (or (ignore-errors (org-back-to-heading t) (point))
  2187. (point-min))
  2188. (save-excursion (outline-next-heading) (point)))))
  2189. (count-boxes
  2190. (function
  2191. ;; Return number of checked boxes and boxes of all types
  2192. ;; in all structures in STRUCTS. If RECURSIVEP is
  2193. ;; non-nil, also count boxes in sub-lists. If ITEM is
  2194. ;; nil, count across the whole structure, else count only
  2195. ;; across subtree whose ancestor is ITEM.
  2196. (lambda (item structs recursivep)
  2197. (let ((c-on 0) (c-all 0))
  2198. (mapc
  2199. (lambda (s)
  2200. (let* ((pre (org-list-prevs-alist s))
  2201. (par (org-list-parents-alist s))
  2202. (items
  2203. (cond
  2204. ((and recursivep item) (org-list-get-subtree item s))
  2205. (recursivep (mapcar 'car s))
  2206. (item (org-list-get-children item s par))
  2207. (t (org-list-get-all-items
  2208. (org-list-get-top-point s) s pre))))
  2209. (cookies (delq nil (mapcar
  2210. (lambda (e)
  2211. (org-list-get-checkbox e s))
  2212. items))))
  2213. (setq c-all (+ (length cookies) c-all)
  2214. c-on (+ (org-count "[X]" cookies) c-on))))
  2215. structs)
  2216. (cons c-on c-all)))))
  2217. (backup-end 1)
  2218. cookies-list structs-bak box-num)
  2219. (goto-char (car bounds))
  2220. ;; 1. Build an alist for each cookie found within BOUNDS. The
  2221. ;; key will be position at beginning of cookie and values
  2222. ;; ending position, format of cookie, and a cell whose car is
  2223. ;; number of checked boxes to report, and cdr total number of
  2224. ;; boxes.
  2225. (while (re-search-forward cookie-re (cdr bounds) t)
  2226. (catch 'skip
  2227. (save-excursion
  2228. (push
  2229. (list
  2230. (match-beginning 1) ; cookie start
  2231. (match-end 1) ; cookie end
  2232. (match-string 2) ; percent?
  2233. (cond ; boxes count
  2234. ;; Cookie is at an heading, but specifically for todo,
  2235. ;; not for checkboxes: skip it.
  2236. ((and (org-at-heading-p)
  2237. (string-match "\\<todo\\>"
  2238. (downcase
  2239. (or (org-entry-get nil "COOKIE_DATA") ""))))
  2240. (throw 'skip nil))
  2241. ;; Cookie is at an heading, but all lists before next
  2242. ;; heading already have been read. Use data collected
  2243. ;; in STRUCTS-BAK. This should only happen when
  2244. ;; heading has more than one cookie on it.
  2245. ((and (org-at-heading-p)
  2246. (<= (save-excursion (outline-next-heading) (point))
  2247. backup-end))
  2248. (funcall count-boxes nil structs-bak recursivep))
  2249. ;; Cookie is at a fresh heading. Grab structure of
  2250. ;; every list containing a checkbox between point and
  2251. ;; next headline, and save them in STRUCTS-BAK.
  2252. ((org-at-heading-p)
  2253. (setq backup-end (save-excursion
  2254. (outline-next-heading) (point))
  2255. structs-bak nil)
  2256. (while (org-list-search-forward box-re backup-end 'move)
  2257. (let* ((struct (org-list-struct))
  2258. (bottom (org-list-get-bottom-point struct)))
  2259. (push struct structs-bak)
  2260. (goto-char bottom)))
  2261. (funcall count-boxes nil structs-bak recursivep))
  2262. ;; Cookie is at an item, and we already have list
  2263. ;; structure stored in STRUCTS-BAK.
  2264. ((and (org-at-item-p)
  2265. (< (point-at-bol) backup-end)
  2266. ;; Only lists in no special context are stored.
  2267. (not (nth 2 (org-list-context))))
  2268. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2269. ;; Cookie is at an item, but we need to compute list
  2270. ;; structure.
  2271. ((org-at-item-p)
  2272. (let ((struct (org-list-struct)))
  2273. (setq backup-end (org-list-get-bottom-point struct)
  2274. structs-bak (list struct)))
  2275. (funcall count-boxes (point-at-bol) structs-bak recursivep))
  2276. ;; Else, cookie found is at a wrong place. Skip it.
  2277. (t (throw 'skip nil))))
  2278. cookies-list))))
  2279. ;; 2. Apply alist to buffer, in reverse order so positions stay
  2280. ;; unchanged after cookie modifications.
  2281. (mapc (lambda (cookie)
  2282. (let* ((beg (car cookie))
  2283. (end (nth 1 cookie))
  2284. (percentp (nth 2 cookie))
  2285. (checked (car (nth 3 cookie)))
  2286. (total (cdr (nth 3 cookie)))
  2287. (new (if percentp
  2288. (format "[%d%%]" (/ (* 100 checked)
  2289. (max 1 total)))
  2290. (format "[%d/%d]" checked total))))
  2291. (goto-char beg)
  2292. (insert new)
  2293. (delete-region (point) (+ (point) (- end beg)))
  2294. (when org-auto-align-tags (org-fix-tags-on-the-fly))))
  2295. cookies-list))))
  2296. (defun org-get-checkbox-statistics-face ()
  2297. "Select the face for checkbox statistics.
  2298. The face will be `org-done' when all relevant boxes are checked.
  2299. Otherwise it will be `org-todo'."
  2300. (if (match-end 1)
  2301. (if (equal (match-string 1) "100%")
  2302. 'org-checkbox-statistics-done
  2303. 'org-checkbox-statistics-todo)
  2304. (if (and (> (match-end 2) (match-beginning 2))
  2305. (equal (match-string 2) (match-string 3)))
  2306. 'org-checkbox-statistics-done
  2307. 'org-checkbox-statistics-todo)))
  2308. (defun org-update-checkbox-count-maybe (&optional all)
  2309. "Update checkbox statistics unless turned off by user.
  2310. With an optional argument ALL, update them in the whole buffer."
  2311. (when (cdr (assq 'checkbox org-list-automatic-rules))
  2312. (org-update-checkbox-count all))
  2313. (run-hooks 'org-checkbox-statistics-hook))
  2314. (defvar org-last-indent-begin-marker (make-marker))
  2315. (defvar org-last-indent-end-marker (make-marker))
  2316. (defun org-list-indent-item-generic (arg no-subtree struct)
  2317. "Indent a local list item including its children.
  2318. When number ARG is a negative, item will be outdented, otherwise
  2319. it will be indented.
  2320. If a region is active, all items inside will be moved.
  2321. If NO-SUBTREE is non-nil, only indent the item itself, not its
  2322. children.
  2323. STRUCT is the list structure.
  2324. Return t if successful."
  2325. (save-excursion
  2326. (beginning-of-line)
  2327. (let* ((regionp (org-region-active-p))
  2328. (rbeg (and regionp (region-beginning)))
  2329. (rend (and regionp (region-end)))
  2330. (top (org-list-get-top-point struct))
  2331. (parents (org-list-parents-alist struct))
  2332. (prevs (org-list-prevs-alist struct))
  2333. ;; Are we going to move the whole list?
  2334. (specialp
  2335. (and (= top (point))
  2336. (cdr (assq 'indent org-list-automatic-rules))
  2337. (if no-subtree
  2338. (error
  2339. "First item of list cannot move without its subtree")
  2340. t))))
  2341. ;; Determine begin and end points of zone to indent. If moving
  2342. ;; more than one item, save them for subsequent moves.
  2343. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  2344. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  2345. (if regionp
  2346. (progn
  2347. (set-marker org-last-indent-begin-marker rbeg)
  2348. (set-marker org-last-indent-end-marker rend))
  2349. (set-marker org-last-indent-begin-marker (point))
  2350. (set-marker org-last-indent-end-marker
  2351. (cond
  2352. (specialp (org-list-get-bottom-point struct))
  2353. (no-subtree (1+ (point)))
  2354. (t (org-list-get-item-end (point) struct))))))
  2355. (let* ((beg (marker-position org-last-indent-begin-marker))
  2356. (end (marker-position org-last-indent-end-marker)))
  2357. (cond
  2358. ;; Special case: moving top-item with indent rule.
  2359. (specialp
  2360. (let* ((level-skip (org-level-increment))
  2361. (offset (if (< arg 0) (- level-skip) level-skip))
  2362. (top-ind (org-list-get-ind beg struct))
  2363. (old-struct (copy-tree struct)))
  2364. (if (< (+ top-ind offset) 0)
  2365. (error "Cannot outdent beyond margin")
  2366. ;; Change bullet if necessary.
  2367. (when (and (= (+ top-ind offset) 0)
  2368. (string-match "*"
  2369. (org-list-get-bullet beg struct)))
  2370. (org-list-set-bullet beg struct
  2371. (org-list-bullet-string "-")))
  2372. ;; Shift every item by OFFSET and fix bullets. Then
  2373. ;; apply changes to buffer.
  2374. (mapc (lambda (e)
  2375. (let ((ind (org-list-get-ind (car e) struct)))
  2376. (org-list-set-ind (car e) struct (+ ind offset))))
  2377. struct)
  2378. (org-list-struct-fix-bul struct prevs)
  2379. (org-list-struct-apply-struct struct old-struct))))
  2380. ;; Forbidden move:
  2381. ((and (< arg 0)
  2382. ;; If only one item is moved, it mustn't have a child.
  2383. (or (and no-subtree
  2384. (not regionp)
  2385. (org-list-has-child-p beg struct))
  2386. ;; If a subtree or region is moved, the last item
  2387. ;; of the subtree mustn't have a child.
  2388. (let ((last-item (caar
  2389. (reverse
  2390. (org-remove-if
  2391. (lambda (e) (>= (car e) end))
  2392. struct)))))
  2393. (org-list-has-child-p last-item struct))))
  2394. (error "Cannot outdent an item without its children"))
  2395. ;; Normal shifting
  2396. (t
  2397. (let* ((new-parents
  2398. (if (< arg 0)
  2399. (org-list-struct-outdent beg end struct parents)
  2400. (org-list-struct-indent beg end struct parents prevs))))
  2401. (org-list-write-struct struct new-parents))
  2402. (org-update-checkbox-count-maybe))))))
  2403. t)
  2404. (defun org-outdent-item ()
  2405. "Outdent a local list item, but not its children.
  2406. If a region is active, all items inside will be moved."
  2407. (interactive)
  2408. (if (org-at-item-p)
  2409. (let ((struct (org-list-struct)))
  2410. (org-list-indent-item-generic -1 t struct))
  2411. (error "Not at an item")))
  2412. (defun org-indent-item ()
  2413. "Indent a local list item, but not its children.
  2414. If a region is active, all items inside will be moved."
  2415. (interactive)
  2416. (if (org-at-item-p)
  2417. (let ((struct (org-list-struct)))
  2418. (org-list-indent-item-generic 1 t struct))
  2419. (error "Not at an item")))
  2420. (defun org-outdent-item-tree ()
  2421. "Outdent a local list item including its children.
  2422. If a region is active, all items inside will be moved."
  2423. (interactive)
  2424. (let ((regionp (org-region-active-p)))
  2425. (cond
  2426. ((or (org-at-item-p)
  2427. (and (org-region-active-p)
  2428. (goto-char (region-beginning))
  2429. (org-at-item-p)))
  2430. (let ((struct (org-list-struct)))
  2431. (org-list-indent-item-generic -1 nil struct)))
  2432. (regionp (error "Region not starting at an item"))
  2433. (t (error "Not at an item")))))
  2434. (defun org-indent-item-tree ()
  2435. "Indent a local list item including its children.
  2436. If a region is active, all items inside will be moved."
  2437. (interactive)
  2438. (let ((regionp (org-region-active-p)))
  2439. (cond
  2440. ((or (org-at-item-p)
  2441. (and (org-region-active-p)
  2442. (goto-char (region-beginning))
  2443. (org-at-item-p)))
  2444. (let ((struct (org-list-struct)))
  2445. (org-list-indent-item-generic 1 nil struct)))
  2446. (regionp (error "Region not starting at an item"))
  2447. (t (error "Not at an item")))))
  2448. (defvar org-tab-ind-state)
  2449. (defun org-cycle-item-indentation ()
  2450. "Cycle levels of indentation of an empty item.
  2451. The first run indents the item, if applicable. Subsequent runs
  2452. outdent it at meaningful levels in the list. When done, item is
  2453. put back at its original position with its original bullet.
  2454. Return t at each successful move."
  2455. (when (org-at-item-p)
  2456. (let* ((org-adapt-indentation nil)
  2457. (struct (org-list-struct))
  2458. (ind (org-list-get-ind (point-at-bol) struct))
  2459. (bullet (org-trim (buffer-substring (point-at-bol) (point-at-eol)))))
  2460. ;; Accept empty items or if cycle has already started.
  2461. (when (or (eq last-command 'org-cycle-item-indentation)
  2462. (and (save-excursion
  2463. (beginning-of-line)
  2464. (looking-at org-list-full-item-re))
  2465. (>= (match-end 0) (save-excursion
  2466. (goto-char (org-list-get-item-end
  2467. (point-at-bol) struct))
  2468. (skip-chars-backward " \r\t\n")
  2469. (point)))))
  2470. (setq this-command 'org-cycle-item-indentation)
  2471. ;; When in the middle of the cycle, try to outdent first. If
  2472. ;; it fails, and point is still at initial position, indent.
  2473. ;; Else, re-create it at its original position.
  2474. (if (eq last-command 'org-cycle-item-indentation)
  2475. (cond
  2476. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2477. ((and (= ind (car org-tab-ind-state))
  2478. (ignore-errors (org-list-indent-item-generic 1 t struct))))
  2479. (t (delete-region (point-at-bol) (point-at-eol))
  2480. (org-indent-to-column (car org-tab-ind-state))
  2481. (insert (cdr org-tab-ind-state) " ")
  2482. ;; Break cycle
  2483. (setq this-command 'identity)))
  2484. ;; If a cycle is starting, remember indentation and bullet,
  2485. ;; then try to indent. If it fails, try to outdent.
  2486. (setq org-tab-ind-state (cons ind bullet))
  2487. (cond
  2488. ((ignore-errors (org-list-indent-item-generic 1 t struct)))
  2489. ((ignore-errors (org-list-indent-item-generic -1 t struct)))
  2490. (t (error "Cannot move item"))))
  2491. t))))
  2492. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  2493. "Sort list items.
  2494. The cursor may be at any item of the list that should be sorted.
  2495. Sublists are not sorted. Checkboxes, if any, are ignored.
  2496. Sorting can be alphabetically, numerically, by date/time as given
  2497. by a time stamp, by a property or by priority.
  2498. Comparing entries ignores case by default. However, with an
  2499. optional argument WITH-CASE, the sorting considers case as well.
  2500. The command prompts for the sorting type unless it has been given
  2501. to the function through the SORTING-TYPE argument, which needs to
  2502. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  2503. meaning of each character:
  2504. n Numerically, by converting the beginning of the item to a number.
  2505. a Alphabetically. Only the first line of item is checked.
  2506. t By date/time, either the first active time stamp in the entry, if
  2507. any, or by the first inactive one. In a timer list, sort the timers.
  2508. Capital letters will reverse the sort order.
  2509. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
  2510. a function to be called with point at the beginning of the
  2511. record. It must return either a string or a number that should
  2512. serve as the sorting key for that record. It will then use
  2513. COMPARE-FUNC to compare entries."
  2514. (interactive "P")
  2515. (let* ((case-func (if with-case 'identity 'downcase))
  2516. (struct (org-list-struct))
  2517. (prevs (org-list-prevs-alist struct))
  2518. (start (org-list-get-list-begin (point-at-bol) struct prevs))
  2519. (end (org-list-get-list-end (point-at-bol) struct prevs))
  2520. (sorting-type
  2521. (progn
  2522. (message
  2523. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  2524. (read-char-exclusive)))
  2525. (getkey-func (and (= (downcase sorting-type) ?f)
  2526. (intern (org-icompleting-read "Sort using function: "
  2527. obarray 'fboundp t nil nil)))))
  2528. (message "Sorting items...")
  2529. (save-restriction
  2530. (narrow-to-region start end)
  2531. (goto-char (point-min))
  2532. (let* ((dcst (downcase sorting-type))
  2533. (case-fold-search nil)
  2534. (now (current-time))
  2535. (sort-func (cond
  2536. ((= dcst ?a) 'string<)
  2537. ((= dcst ?f) compare-func)
  2538. ((= dcst ?t) '<)
  2539. (t nil)))
  2540. (next-record (lambda ()
  2541. (skip-chars-forward " \r\t\n")
  2542. (beginning-of-line)))
  2543. (end-record (lambda ()
  2544. (goto-char (org-list-get-item-end-before-blank
  2545. (point) struct))))
  2546. (value-to-sort
  2547. (lambda ()
  2548. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  2549. (cond
  2550. ((= dcst ?n)
  2551. (string-to-number (buffer-substring (match-end 0)
  2552. (point-at-eol))))
  2553. ((= dcst ?a)
  2554. (funcall case-func
  2555. (buffer-substring (match-end 0) (point-at-eol))))
  2556. ((= dcst ?t)
  2557. (cond
  2558. ;; If it is a timer list, convert timer to seconds
  2559. ((org-at-item-timer-p)
  2560. (org-timer-hms-to-secs (match-string 1)))
  2561. ((or (re-search-forward org-ts-regexp (point-at-eol) t)
  2562. (re-search-forward org-ts-regexp-both
  2563. (point-at-eol) t))
  2564. (org-time-string-to-seconds (match-string 0)))
  2565. (t (org-float-time now))))
  2566. ((= dcst ?f)
  2567. (if getkey-func
  2568. (let ((value (funcall getkey-func)))
  2569. (if (stringp value)
  2570. (funcall case-func value)
  2571. value))
  2572. (error "Invalid key function `%s'" getkey-func)))
  2573. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  2574. (sort-subr (/= dcst sorting-type)
  2575. next-record
  2576. end-record
  2577. value-to-sort
  2578. nil
  2579. sort-func)
  2580. ;; Read and fix list again, as `sort-subr' probably destroyed
  2581. ;; its structure.
  2582. (org-list-repair)
  2583. (run-hooks 'org-after-sorting-entries-or-items-hook)
  2584. (message "Sorting items...done")))))
  2585. ;;; Send and receive lists
  2586. (defun org-list-parse-list (&optional delete)
  2587. "Parse the list at point and maybe DELETE it.
  2588. Return a list whose car is a symbol of list type, among
  2589. `ordered', `unordered' and `descriptive'. Then, each item is
  2590. a list whose car is counter, and cdr are strings and other
  2591. sub-lists. Inside strings, check-boxes are replaced by
  2592. \"[CBON]\", \"[CBOFF]\" and \"[CBTRANS]\".
  2593. For example, the following list:
  2594. 1. first item
  2595. + sub-item one
  2596. + [X] sub-item two
  2597. more text in first item
  2598. 2. [@3] last item
  2599. will be parsed as:
  2600. \(ordered
  2601. \(nil \"first item\"
  2602. \(unordered
  2603. \(nil \"sub-item one\"\)
  2604. \(nil \"[CBON] sub-item two\"\)\)
  2605. \"more text in first item\"\)
  2606. \(3 \"last item\"\)\)
  2607. Point is left at list end."
  2608. (let* ((struct (org-list-struct))
  2609. (prevs (org-list-prevs-alist struct))
  2610. (parents (org-list-parents-alist struct))
  2611. (top (org-list-get-top-point struct))
  2612. (bottom (org-list-get-bottom-point struct))
  2613. out
  2614. parse-item ; for byte-compiler
  2615. (get-text
  2616. (function
  2617. ;; Return text between BEG and END, trimmed, with
  2618. ;; checkboxes replaced.
  2619. (lambda (beg end)
  2620. (let ((text (org-trim (buffer-substring beg end))))
  2621. (if (string-match "\\`\\[\\([-X ]\\)\\]" text)
  2622. (replace-match
  2623. (let ((box (match-string 1 text)))
  2624. (cond
  2625. ((equal box " ") "CBOFF")
  2626. ((equal box "-") "CBTRANS")
  2627. (t "CBON")))
  2628. t nil text 1)
  2629. text)))))
  2630. (parse-sublist
  2631. (function
  2632. ;; Return a list whose car is list type and cdr a list of
  2633. ;; items' body.
  2634. (lambda (e)
  2635. (cons (org-list-get-list-type (car e) struct prevs)
  2636. (mapcar parse-item e)))))
  2637. (parse-item
  2638. (function
  2639. ;; Return a list containing counter of item, if any, text
  2640. ;; and any sublist inside it.
  2641. (lambda (e)
  2642. (let ((start (save-excursion
  2643. (goto-char e)
  2644. (looking-at "[ \t]*\\S-+\\([ \t]+\\[@\\(start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?[ \t]*")
  2645. (match-end 0)))
  2646. ;; Get counter number. For alphabetic counter, get
  2647. ;; its position in the alphabet.
  2648. (counter (let ((c (org-list-get-counter e struct)))
  2649. (cond
  2650. ((not c) nil)
  2651. ((string-match "[A-Za-z]" c)
  2652. (- (string-to-char (upcase (match-string 0 c)))
  2653. 64))
  2654. ((string-match "[0-9]+" c)
  2655. (string-to-number (match-string 0 c))))))
  2656. (childp (org-list-has-child-p e struct))
  2657. (end (org-list-get-item-end e struct)))
  2658. ;; If item has a child, store text between bullet and
  2659. ;; next child, then recursively parse all sublists. At
  2660. ;; the end of each sublist, check for the presence of
  2661. ;; text belonging to the original item.
  2662. (if childp
  2663. (let* ((children (org-list-get-children e struct parents))
  2664. (body (list (funcall get-text start childp))))
  2665. (while children
  2666. (let* ((first (car children))
  2667. (sub (org-list-get-all-items first struct prevs))
  2668. (last-c (car (last sub)))
  2669. (last-end (org-list-get-item-end last-c struct)))
  2670. (push (funcall parse-sublist sub) body)
  2671. ;; Remove children from the list just parsed.
  2672. (setq children (cdr (member last-c children)))
  2673. ;; There is a chunk of text belonging to the
  2674. ;; item if last child doesn't end where next
  2675. ;; child starts or where item ends.
  2676. (unless (= (or (car children) end) last-end)
  2677. (push (funcall get-text
  2678. last-end (or (car children) end))
  2679. body))))
  2680. (cons counter (nreverse body)))
  2681. (list counter (funcall get-text start end))))))))
  2682. ;; Store output, take care of cursor position and deletion of
  2683. ;; list, then return output.
  2684. (setq out (funcall parse-sublist (org-list-get-all-items top struct prevs)))
  2685. (goto-char top)
  2686. (when delete
  2687. (delete-region top bottom)
  2688. (when (and (not (looking-at "[ \t]*$")) (looking-at org-list-end-re))
  2689. (replace-match "")))
  2690. out))
  2691. (defun org-list-make-subtree ()
  2692. "Convert the plain list at point into a subtree."
  2693. (interactive)
  2694. (if (not (ignore-errors (goto-char (org-in-item-p))))
  2695. (error "Not in a list")
  2696. (let ((list (save-excursion (org-list-parse-list t))))
  2697. (insert (org-list-to-subtree list)))))
  2698. (defun org-list-insert-radio-list ()
  2699. "Insert a radio list template appropriate for this major mode."
  2700. (interactive)
  2701. (let* ((e (assq major-mode org-list-radio-list-templates))
  2702. (txt (nth 1 e))
  2703. name pos)
  2704. (unless e (error "No radio list setup defined for %s" major-mode))
  2705. (setq name (read-string "List name: "))
  2706. (while (string-match "%n" txt)
  2707. (setq txt (replace-match name t t txt)))
  2708. (or (bolp) (insert "\n"))
  2709. (setq pos (point))
  2710. (insert txt)
  2711. (goto-char pos)))
  2712. (defun org-list-send-list (&optional maybe)
  2713. "Send a transformed version of this list to the receiver position.
  2714. With argument MAYBE, fail quietly if no transformation is defined
  2715. for this list."
  2716. (interactive)
  2717. (catch 'exit
  2718. (unless (org-at-item-p) (error "Not at a list item"))
  2719. (save-excursion
  2720. (re-search-backward "#\\+ORGLST" nil t)
  2721. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  2722. (if maybe
  2723. (throw 'exit nil)
  2724. (error "Don't know how to transform this list"))))
  2725. (let* ((name (match-string 1))
  2726. (transform (intern (match-string 2)))
  2727. (bottom-point
  2728. (save-excursion
  2729. (re-search-forward
  2730. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  2731. (match-beginning 0)))
  2732. (top-point
  2733. (progn
  2734. (re-search-backward "#\\+ORGLST" nil t)
  2735. (re-search-forward (org-item-beginning-re) bottom-point t)
  2736. (match-beginning 0)))
  2737. (list (save-restriction
  2738. (narrow-to-region top-point bottom-point)
  2739. (org-list-parse-list)))
  2740. beg txt)
  2741. (unless (fboundp transform)
  2742. (error "No such transformation function %s" transform))
  2743. (let ((txt (funcall transform list)))
  2744. ;; Find the insertion place
  2745. (save-excursion
  2746. (goto-char (point-min))
  2747. (unless (re-search-forward
  2748. (concat "BEGIN RECEIVE ORGLST +"
  2749. name
  2750. "\\([ \t]\\|$\\)") nil t)
  2751. (error "Don't know where to insert translated list"))
  2752. (goto-char (match-beginning 0))
  2753. (beginning-of-line 2)
  2754. (setq beg (point))
  2755. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  2756. (error "Cannot find end of insertion region"))
  2757. (delete-region beg (point-at-bol))
  2758. (goto-char beg)
  2759. (insert txt "\n")))
  2760. (message "List converted and installed at receiver location"))))
  2761. (defsubst org-list-item-trim-br (item)
  2762. "Trim line breaks in a list ITEM."
  2763. (setq item (replace-regexp-in-string "\n +" " " item)))
  2764. (defun org-list-to-generic (list params)
  2765. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  2766. Valid parameters PARAMS are:
  2767. :ustart String to start an unordered list
  2768. :uend String to end an unordered list
  2769. :ostart String to start an ordered list
  2770. :oend String to end an ordered list
  2771. :dstart String to start a descriptive list
  2772. :dend String to end a descriptive list
  2773. :dtstart String to start a descriptive term
  2774. :dtend String to end a descriptive term
  2775. :ddstart String to start a description
  2776. :ddend String to end a description
  2777. :splice When set to t, return only list body lines, don't wrap
  2778. them into :[u/o]start and :[u/o]end. Default is nil.
  2779. :istart String to start a list item.
  2780. :icount String to start an item with a counter.
  2781. :iend String to end a list item
  2782. :isep String to separate items
  2783. :lsep String to separate sublists
  2784. :csep String to separate text from a sub-list
  2785. :cboff String to insert for an unchecked check-box
  2786. :cbon String to insert for a checked check-box
  2787. :cbtrans String to insert for a check-box in transitional state
  2788. :nobr Non-nil means remove line breaks in lists items.
  2789. Alternatively, each parameter can also be a form returning
  2790. a string. These sexp can use keywords `counter' and `depth',
  2791. representing respectively counter associated to the current
  2792. item, and depth of the current sub-list, starting at 0.
  2793. Obviously, `counter' is only available for parameters applying to
  2794. items."
  2795. (interactive)
  2796. (let* ((p params)
  2797. (splicep (plist-get p :splice))
  2798. (ostart (plist-get p :ostart))
  2799. (oend (plist-get p :oend))
  2800. (ustart (plist-get p :ustart))
  2801. (uend (plist-get p :uend))
  2802. (dstart (plist-get p :dstart))
  2803. (dend (plist-get p :dend))
  2804. (dtstart (plist-get p :dtstart))
  2805. (dtend (plist-get p :dtend))
  2806. (ddstart (plist-get p :ddstart))
  2807. (ddend (plist-get p :ddend))
  2808. (istart (plist-get p :istart))
  2809. (icount (plist-get p :icount))
  2810. (iend (plist-get p :iend))
  2811. (isep (plist-get p :isep))
  2812. (lsep (plist-get p :lsep))
  2813. (csep (plist-get p :csep))
  2814. (cbon (plist-get p :cbon))
  2815. (cboff (plist-get p :cboff))
  2816. (cbtrans (plist-get p :cbtrans))
  2817. (nobr (plist-get p :nobr))
  2818. export-sublist ; for byte-compiler
  2819. (export-item
  2820. (function
  2821. ;; Export an item ITEM of type TYPE, at DEPTH. First
  2822. ;; string in item is treated in a special way as it can
  2823. ;; bring extra information that needs to be processed.
  2824. (lambda (item type depth)
  2825. (let* ((counter (pop item))
  2826. (fmt (concat
  2827. (cond
  2828. ((eq type 'descriptive)
  2829. ;; Stick DTSTART to ISTART by
  2830. ;; left-trimming the latter.
  2831. (concat (let ((s (eval istart)))
  2832. (or (and (string-match "[ \t\n\r]+\\'" s)
  2833. (replace-match "" t t s))
  2834. istart))
  2835. "%s" (eval ddend)))
  2836. ((and counter (eq type 'ordered))
  2837. (concat (eval icount) "%s"))
  2838. (t (concat (eval istart) "%s")))
  2839. (eval iend)))
  2840. (first (car item)))
  2841. ;; Replace checkbox if any is found.
  2842. (cond
  2843. ((string-match "\\[CBON\\]" first)
  2844. (setq first (replace-match cbon t t first)))
  2845. ((string-match "\\[CBOFF\\]" first)
  2846. (setq first (replace-match cboff t t first)))
  2847. ((string-match "\\[CBTRANS\\]" first)
  2848. (setq first (replace-match cbtrans t t first))))
  2849. ;; Replace line breaks if required
  2850. (when nobr (setq first (org-list-item-trim-br first)))
  2851. ;; Insert descriptive term if TYPE is `descriptive'.
  2852. (when (eq type 'descriptive)
  2853. (let* ((complete (string-match "^\\(.*\\)[ \t]+::" first))
  2854. (term (if complete
  2855. (save-match-data
  2856. (org-trim (match-string 1 first)))
  2857. "???"))
  2858. (desc (if complete
  2859. (org-trim (substring first (match-end 0)))
  2860. first)))
  2861. (setq first (concat (eval dtstart) term (eval dtend)
  2862. (eval ddstart) desc))))
  2863. (setcar item first)
  2864. (format fmt
  2865. (mapconcat (lambda (e)
  2866. (if (stringp e) e
  2867. (funcall export-sublist e (1+ depth))))
  2868. item (or (eval csep) "")))))))
  2869. (export-sublist
  2870. (function
  2871. ;; Export sublist SUB at DEPTH.
  2872. (lambda (sub depth)
  2873. (let* ((type (car sub))
  2874. (items (cdr sub))
  2875. (fmt (concat (cond
  2876. (splicep "%s")
  2877. ((eq type 'ordered)
  2878. (concat (eval ostart) "%s" (eval oend)))
  2879. ((eq type 'descriptive)
  2880. (concat (eval dstart) "%s" (eval dend)))
  2881. (t (concat (eval ustart) "%s" (eval uend))))
  2882. (eval lsep))))
  2883. (format fmt (mapconcat (lambda (e)
  2884. (funcall export-item e type depth))
  2885. items (or (eval isep) ""))))))))
  2886. (concat (funcall export-sublist list 0) "\n")))
  2887. (defun org-list-to-latex (list &optional params)
  2888. "Convert LIST into a LaTeX list.
  2889. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2890. with overruling parameters for `org-list-to-generic'."
  2891. (org-list-to-generic
  2892. list
  2893. (org-combine-plists
  2894. '(:splice nil :ostart "\\begin{enumerate}\n" :oend "\\end{enumerate}"
  2895. :ustart "\\begin{itemize}\n" :uend "\\end{itemize}"
  2896. :dstart "\\begin{description}\n" :dend "\\end{description}"
  2897. :dtstart "[" :dtend "] "
  2898. :istart "\\item " :iend "\n"
  2899. :icount (let ((enum (nth depth '("i" "ii" "iii" "iv"))))
  2900. (if enum
  2901. ;; LaTeX increments counter just before
  2902. ;; using it, so set it to the desired
  2903. ;; value, minus one.
  2904. (format "\\setcounter{enum%s}{%s}\n\\item "
  2905. enum (1- counter))
  2906. "\\item "))
  2907. :csep "\n"
  2908. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}"
  2909. :cbtrans "\\texttt{[-]}")
  2910. params)))
  2911. (defun org-list-to-html (list &optional params)
  2912. "Convert LIST into a HTML list.
  2913. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2914. with overruling parameters for `org-list-to-generic'."
  2915. (org-list-to-generic
  2916. list
  2917. (org-combine-plists
  2918. '(:splice nil :ostart "<ol>\n" :oend "\n</ol>"
  2919. :ustart "<ul>\n" :uend "\n</ul>"
  2920. :dstart "<dl>\n" :dend "\n</dl>"
  2921. :dtstart "<dt>" :dtend "</dt>\n"
  2922. :ddstart "<dd>" :ddend "</dd>"
  2923. :istart "<li>" :iend "</li>"
  2924. :icount (format "<li value=\"%s\">" counter)
  2925. :isep "\n" :lsep "\n" :csep "\n"
  2926. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>"
  2927. :cbtrans "<code>[-]</code>")
  2928. params)))
  2929. (defun org-list-to-texinfo (list &optional params)
  2930. "Convert LIST into a Texinfo list.
  2931. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2932. with overruling parameters for `org-list-to-generic'."
  2933. (org-list-to-generic
  2934. list
  2935. (org-combine-plists
  2936. '(:splice nil :ostart "@itemize @minus\n" :oend "@end itemize"
  2937. :ustart "@enumerate\n" :uend "@end enumerate"
  2938. :dstart "@table @asis\n" :dend "@end table"
  2939. :dtstart " " :dtend "\n"
  2940. :istart "@item\n" :iend "\n"
  2941. :icount "@item\n"
  2942. :csep "\n"
  2943. :cbon "@code{[X]}" :cboff "@code{[ ]}"
  2944. :cbtrans "@code{[-]}")
  2945. params)))
  2946. (defun org-list-to-subtree (list &optional params)
  2947. "Convert LIST into an Org subtree.
  2948. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  2949. with overruling parameters for `org-list-to-generic'."
  2950. (let* ((rule (cdr (assq 'heading org-blank-before-new-entry)))
  2951. (level (org-reduced-level (or (org-current-level) 0)))
  2952. (blankp (or (eq rule t)
  2953. (and (eq rule 'auto)
  2954. (save-excursion
  2955. (outline-previous-heading)
  2956. (org-previous-line-empty-p)))))
  2957. (get-stars
  2958. (function
  2959. ;; Return the string for the heading, depending on depth D
  2960. ;; of current sub-list.
  2961. (lambda (d)
  2962. (let ((oddeven-level (+ level d 1)))
  2963. (concat (make-string (if org-odd-levels-only
  2964. (1- (* 2 oddeven-level))
  2965. oddeven-level)
  2966. ?*)
  2967. " "))))))
  2968. (org-list-to-generic
  2969. list
  2970. (org-combine-plists
  2971. '(:splice t
  2972. :dtstart " " :dtend " "
  2973. :istart (funcall get-stars depth)
  2974. :icount (funcall get-stars depth)
  2975. :isep (if blankp "\n\n" "\n")
  2976. :csep (if blankp "\n\n" "\n")
  2977. :cbon "DONE" :cboff "TODO" :cbtrans "TODO")
  2978. params))))
  2979. (provide 'org-list)
  2980. ;;; org-list.el ends here