org-list.el 110 KB

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