org-list.el 99 KB

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