org-list.el 107 KB

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