org-list.el 99 KB

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