org-list.el 110 KB

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