org-list.el 111 KB

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