org-list.el 111 KB

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