org-list.el 111 KB

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