org-list.el 105 KB

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