org-list.el 110 KB

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