org-list.el 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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.01trans
  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. ;;; Code:
  29. (eval-when-compile
  30. (require 'cl))
  31. (require 'org-macs)
  32. (require 'org-compat)
  33. (defvar org-blank-before-new-entry)
  34. (defvar org-M-RET-may-split-line)
  35. (defvar org-complex-heading-regexp)
  36. (defvar org-odd-levels-only)
  37. (declare-function org-invisible-p "org" ())
  38. (declare-function org-on-heading-p "org" (&optional invisible-ok))
  39. (declare-function outline-next-heading "outline" ())
  40. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  41. (declare-function org-back-over-empty-lines "org" ())
  42. (declare-function org-trim "org" (s))
  43. (declare-function org-get-indentation "org" (&optional line))
  44. (declare-function org-timer-item "org-timer" (&optional arg))
  45. (declare-function org-timer-hms-to-secs "org-timer" (hms))
  46. (declare-function org-combine-plists "org" (&rest plists))
  47. (declare-function org-entry-get "org"
  48. (pom property &optional inherit literal-nil))
  49. (declare-function org-narrow-to-subtree "org" ())
  50. (declare-function org-show-subtree "org" ())
  51. (defgroup org-plain-lists nil
  52. "Options concerning plain lists in Org-mode."
  53. :tag "Org Plain lists"
  54. :group 'org-structure)
  55. (defcustom org-cycle-include-plain-lists t
  56. "When t, make TAB cycle visibility on plain list items.
  57. Cycling plain lists works only when the cursor is on a plain list
  58. item. When the cursor is on an outline heading, plain lists are
  59. treated as text. This is the most stable way of handling this,
  60. which is why it is the default.
  61. When this is the symbol `integrate', then during cycling, plain
  62. list items will *temporarily* be interpreted as outline headlines
  63. with a level given by 1000+i where i is the indentation of the
  64. bullet. This setting can lead to strange effects when switching
  65. visibility to `children', because the first \"child\" in a
  66. subtree decides what children should be listed. If that first
  67. \"child\" is a plain list item with an implied large level
  68. number, all true children and grand children of the outline
  69. heading will be exposed in a children' view."
  70. :group 'org-plain-lists
  71. :type '(choice
  72. (const :tag "Never" nil)
  73. (const :tag "With cursor in plain list (recommended)" t)
  74. (const :tag "As children of outline headings" integrate)))
  75. (defcustom org-list-demote-modify-bullet nil
  76. "Default bullet type installed when demoting an item.
  77. This is an association list, for each bullet type, this alist will point
  78. to the bullet that should be used when this item is demoted.
  79. For example,
  80. (setq org-list-demote-modify-bullet
  81. '((\"+\" . \"-\") (\"-\" . \"+\") (\"*\" . \"+\")))
  82. will make
  83. + Movies
  84. + Silence of the Lambs
  85. + My Cousin Vinny
  86. + Books
  87. + The Hunt for Red October
  88. + The Road to Omaha
  89. into
  90. + Movies
  91. - Silence of the Lambs
  92. - My Cousin Vinny
  93. + Books
  94. - The Hunt for Red October
  95. - The Road to Omaha"
  96. :group 'org-plain-lists
  97. :type '(repeat
  98. (cons
  99. (choice :tag "If the current bullet is "
  100. (const "-")
  101. (const "+")
  102. (const "*")
  103. (const "1.")
  104. (const "1)"))
  105. (choice :tag "demotion will change it to"
  106. (const "-")
  107. (const "+")
  108. (const "*")
  109. (const "1.")
  110. (const "1)")))))
  111. (defcustom org-plain-list-ordered-item-terminator t
  112. "The character that makes a line with leading number an ordered list item.
  113. Valid values are ?. and ?\). To get both terminators, use t. While
  114. ?. may look nicer, it creates the danger that a line with leading
  115. number may be incorrectly interpreted as an item. ?\) therefore is
  116. the safe choice."
  117. :group 'org-plain-lists
  118. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  119. (const :tag "paren like in \"2)\"" ?\))
  120. (const :tab "both" t)))
  121. (defcustom org-list-two-spaces-after-bullet-regexp nil
  122. "A regular expression matching bullets that should have 2 spaces after them.
  123. When nil, no bullet will have two spaces after them.
  124. When a string, it will be used as a regular expression. When the
  125. bullet type of a list is changed, the new bullet type will be
  126. matched against this regexp. If it matches, there will be two
  127. spaces instead of one after the bullet in each item of the list."
  128. :group 'org-plain-lists
  129. :type '(choice
  130. (const :tag "never" nil)
  131. (regexp)))
  132. (defcustom org-list-ending-method 'regexp
  133. "Determine where plain lists should end.
  134. Valid values are: `regexp', `indent' or `both'.
  135. When set to `regexp', Org will look into two variables,
  136. `org-empty-line-terminates-plain-lists' and the more general
  137. `org-list-end-regexp', to know what will end lists. This is the
  138. default value.
  139. When set to `indent', indentation of the last non-blank line will
  140. determine if point is in a list. If that line is less indented
  141. than the previous item in the section, if any, list has ended.
  142. When set to `both', each of the preceding methods must confirm
  143. that point is in a list."
  144. :group 'org-plain-lists
  145. :type '(choice
  146. (const :tag "With a well defined ending (recommended)" regexp)
  147. (const :tag "With indentation of the current line" indent)
  148. (const :tag "With both methods" both)))
  149. (defcustom org-empty-line-terminates-plain-lists nil
  150. "Non-nil means an empty line ends all plain list levels.
  151. This variable only makes sense if `org-list-ending-method' is set
  152. to regexp or both. This is then equivalent to set
  153. `org-list-end-regexp' to \"^[ \\t]*$\"."
  154. :group 'org-plain-lists
  155. :type 'boolean)
  156. (defcustom org-list-end-regexp "^[ \t]*\n[ \t]*\n"
  157. "Regexp matching the end of all plain list levels.
  158. It must start with \"^\" and end with \"\\n\". It defaults to 2
  159. blank lines. `org-empty-line-terminates-plain-lists' has
  160. precedence over it."
  161. :group 'org-plain-lists
  162. :type 'string)
  163. (defcustom org-list-automatic-rules '((bullet . t)
  164. (checkbox . t)
  165. (indent . t)
  166. (insert . t))
  167. "Non-nil means apply set of rules when acting on lists.
  168. By default, automatic actions are taken when using
  169. \\[org-shiftmetaup], \\[org-shiftmetadown], \\[org-meta-return],
  170. \\[org-metaright], \\[org-metaleft], \\[org-shiftmetaright],
  171. \\[org-shiftmetaleft], \\[org-ctrl-c-minus],
  172. \\[org-toggle-checkbox] or \\[org-insert-todo-heading]. You can
  173. disable individually these rules by setting them to nil. Valid
  174. rules are:
  175. bullet when non-nil, cycling bullet do not allow lists at
  176. column 0 to have * as a bullet and descriptions lists
  177. to be numbered.
  178. checkbox when non-nil, checkbox statistics is updated each time
  179. you either insert a new checkbox or toggle a checkbox.
  180. It also prevents from inserting a checkbox in a
  181. description item.
  182. indent when non-nil, indenting or outdenting list top-item
  183. with its subtree will move the whole list and
  184. outdenting a list whose bullet is * to column 0 will
  185. change that bullet to -
  186. insert when non-nil, trying to insert an item inside a block
  187. will insert it right before the block instead of
  188. throwing an error."
  189. :group 'org-plain-lists
  190. :type '(alist :tag "Sets of rules"
  191. :key-type
  192. (choice
  193. (const :tag "Bullet" bullet)
  194. (const :tag "Checkbox" checkbox)
  195. (const :tag "Indent" indent)
  196. (const :tag "Insert" insert))
  197. :value-type
  198. (boolean :tag "Activate" :value t)))
  199. (defcustom org-hierarchical-checkbox-statistics t
  200. "Non-nil means checkbox statistics counts only the state of direct children.
  201. When nil, all boxes below the cookie are counted.
  202. This can be set to nil on a per-node basis using a COOKIE_DATA property
  203. with the word \"recursive\" in the value."
  204. :group 'org-plain-lists
  205. :type 'boolean)
  206. (defcustom org-description-max-indent 20
  207. "Maximum indentation for the second line of a description list.
  208. When the indentation would be larger than this, it will become
  209. 5 characters instead."
  210. :group 'org-plain-lists
  211. :type 'integer)
  212. (defcustom org-list-radio-list-templates
  213. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  214. % END RECEIVE ORGLST %n
  215. \\begin{comment}
  216. #+ORGLST: SEND %n org-list-to-latex
  217. -
  218. \\end{comment}\n")
  219. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  220. @c END RECEIVE ORGLST %n
  221. @ignore
  222. #+ORGLST: SEND %n org-list-to-texinfo
  223. -
  224. @end ignore\n")
  225. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  226. <!-- END RECEIVE ORGLST %n -->
  227. <!--
  228. #+ORGLST: SEND %n org-list-to-html
  229. -
  230. -->\n"))
  231. "Templates for radio lists in different major modes.
  232. All occurrences of %n in a template will be replaced with the name of the
  233. list, obtained by prompting the user."
  234. :group 'org-plain-lists
  235. :type '(repeat
  236. (list (symbol :tag "Major mode")
  237. (string :tag "Format"))))
  238. ;;; Internal functions
  239. (defun org-list-end-re ()
  240. "Return the regex corresponding to the end of a list.
  241. It depends on `org-empty-line-terminates-plain-lists'."
  242. (if org-empty-line-terminates-plain-lists
  243. "^[ \t]*\n"
  244. org-list-end-regexp))
  245. (defun org-item-re (&optional general)
  246. "Return the correct regular expression for plain lists.
  247. If GENERAL is non-nil, return the general regexp independent of the value
  248. of `org-plain-list-ordered-item-terminator'."
  249. (cond
  250. ((or general (eq org-plain-list-ordered-item-terminator t))
  251. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  252. ((= org-plain-list-ordered-item-terminator ?.)
  253. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  254. ((= org-plain-list-ordered-item-terminator ?\))
  255. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\([ \t]+\\|$\\)")
  256. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  257. (defconst org-item-beginning-re (concat "^" (org-item-re))
  258. "Regexp matching the beginning of a plain list item.")
  259. (defun org-list-terminator-between (min max &optional firstp)
  260. "Find the position of a list ender between MIN and MAX, or nil.
  261. This function looks for `org-list-end-re' outside a block.
  262. If FIRSTP in non-nil, return the point at the beginning of the
  263. nearest valid terminator from MIN. Otherwise, return the point at
  264. the end of the nearest terminator from MAX."
  265. (save-excursion
  266. (let* ((start (if firstp min max))
  267. (end (if firstp max min))
  268. (search-fun (if firstp
  269. #'org-search-forward-unenclosed
  270. #'org-search-backward-unenclosed))
  271. (list-end-p (progn
  272. (goto-char start)
  273. (funcall search-fun (org-list-end-re) end t))))
  274. ;; Is there a valid list terminator somewhere ?
  275. (and list-end-p
  276. ;; we want to be on the first line of the list ender
  277. (match-beginning 0)))))
  278. (defun org-list-maybe-skip-block (search limit)
  279. "Return non-nil value if point is in a block, skipping it on the way.
  280. It looks for the boundary of the block in SEARCH direction,
  281. stopping at LIMIT."
  282. (save-match-data
  283. (let ((case-fold-search t)
  284. (boundary (if (eq search 're-search-forward) 3 5)))
  285. (when (save-excursion
  286. (and (funcall search "^[ \t]*#\\+\\(begin\\|end\\)_" limit t)
  287. (= (length (match-string 1)) boundary)))
  288. ;; We're in a block: get out of it
  289. (goto-char (match-beginning 0))))))
  290. (defun org-list-search-unenclosed-generic (search re bound noerr)
  291. "Search a string outside blocks and protected places.
  292. Arguments SEARCH, RE, BOUND and NOERR are similar to those in
  293. `search-forward', `search-backward', `re-search-forward' and
  294. `re-search-backward'."
  295. (catch 'exit
  296. (let ((origin (point)))
  297. (while t
  298. ;; 1. No match: return to origin or bound, depending on NOERR.
  299. (unless (funcall search re bound noerr)
  300. (throw 'exit (and (goto-char (if (booleanp noerr) origin bound))
  301. nil)))
  302. ;; 2. Match not in block or protected: return point. Else
  303. ;; skip the block and carry on.
  304. (unless (or (get-text-property (match-beginning 0) 'org-protected)
  305. (org-list-maybe-skip-block search bound))
  306. (throw 'exit (point)))))))
  307. (defun org-search-backward-unenclosed (regexp &optional bound noerror)
  308. "Like `re-search-backward' but don't stop inside blocks or protected places.
  309. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  310. `re-search-backward'."
  311. (org-list-search-unenclosed-generic
  312. #'re-search-backward regexp (or bound (point-min)) noerror))
  313. (defun org-search-forward-unenclosed (regexp &optional bound noerror)
  314. "Like `re-search-forward' but don't stop inside blocks or protected places.
  315. Arguments REGEXP, BOUND and NOERROR are similar to those used in
  316. `re-search-forward'."
  317. (org-list-search-unenclosed-generic
  318. #'re-search-forward regexp (or bound (point-max)) noerror))
  319. (defun org-list-in-item-p-with-indent (limit)
  320. "Is the cursor inside a plain list?
  321. Plain lists are considered ending when a non-blank line is less
  322. indented than the previous item within LIMIT.
  323. Return the position of the previous item, if applicable."
  324. (save-excursion
  325. (cond
  326. ;; do not start searching inside a block...
  327. ((org-list-maybe-skip-block #'re-search-backward limit))
  328. ;; ... or at a blank line
  329. ((looking-at "^[ \t]*$")
  330. (skip-chars-backward " \r\t\n")
  331. (beginning-of-line)))
  332. (beginning-of-line)
  333. (or (and (org-at-item-p) (point-at-bol))
  334. (let* ((case-fold-search t)
  335. (pos (point))
  336. (ind-ref (org-get-indentation))
  337. ;; Is there an item above?
  338. (up-item-p (save-excursion
  339. (goto-char limit)
  340. (org-search-forward-unenclosed
  341. org-item-beginning-re pos t))))
  342. (and up-item-p
  343. (catch 'exit
  344. (while t
  345. (cond
  346. ((or (= (point) limit)
  347. (looking-at "^[ \t]*:END:"))
  348. (throw 'exit nil))
  349. ((looking-at "^[ \t]*$")
  350. (skip-chars-backward " \r\t\n")
  351. (beginning-of-line))
  352. ((looking-at "^[ \t]*#\\+end_")
  353. (re-search-backward "^[ \t]*#\\+begin_"))
  354. ((looking-at org-item-beginning-re)
  355. (if (< (org-get-indentation) ind-ref)
  356. (throw 'exit (point-at-bol))
  357. (forward-line -1)))
  358. (t
  359. (setq ind-ref (min (org-get-indentation) ind-ref))
  360. (forward-line -1))))))))))
  361. (defun org-list-in-item-p-with-regexp (limit)
  362. "Is the cursor inside a plain list?
  363. Plain lists end when `org-list-end-regexp' is matched, or at a
  364. blank line if `org-empty-line-terminates-plain-lists' is true.
  365. Argument LIMIT specifies the upper-bound of the search."
  366. (save-excursion
  367. (let* ((actual-pos (goto-char (point-at-eol)))
  368. ;; Moved to eol so current line can be matched by
  369. ;; `org-item-re'.
  370. (last-item-start (save-excursion
  371. (org-search-backward-unenclosed
  372. org-item-beginning-re limit t)))
  373. (list-ender (org-list-terminator-between
  374. last-item-start actual-pos)))
  375. ;; We are in a list when we are on an item line or when we can
  376. ;; find an item before point and there is no valid list ender
  377. ;; between it and the point.
  378. (and last-item-start
  379. (not list-ender)))))
  380. (defun org-list-top-point-with-regexp (limit)
  381. "Return point at the top level item in a list, or nil if not in a list.
  382. Argument LIMIT specifies the upper-bound of the search.
  383. List ending is determined by regexp. See
  384. `org-list-ending-method'. for more information."
  385. (save-excursion
  386. (and (org-list-in-item-p-with-regexp limit)
  387. (let ((pos (point-at-eol)))
  388. ;; Is there some list above this one ? If so, go to its ending.
  389. ;; Otherwise, go back to the heading above or bob.
  390. (goto-char (or (org-list-terminator-between limit pos) limit))
  391. ;; From there, search down our list.
  392. (org-search-forward-unenclosed org-item-beginning-re pos t)
  393. (point-at-bol)))))
  394. (defun org-list-bottom-point-with-regexp (limit)
  395. "Return point just before list ending or nil if not in a list.
  396. Argument LIMIT specifies the lower-bound of the search.
  397. List ending is determined by regexp. See
  398. `org-list-ending-method'. for more information."
  399. (save-excursion
  400. (and (org-in-item-p)
  401. (let ((pos (point)))
  402. ;; The list ending is either first point matching
  403. ;; `org-list-end-re', point at first white-line before next
  404. ;; heading, or eob.
  405. (or (org-list-terminator-between (min pos limit) limit t) limit)))))
  406. (defun org-list-top-point-with-indent (limit)
  407. "Return point at the top level in a list, or nil if not in a list.
  408. Argument LIMIT specifies the upper-bound of the search.
  409. List ending is determined by indentation of text. See
  410. `org-list-ending-method'. for more information."
  411. (save-excursion
  412. (let ((prev-p (org-list-in-item-p-with-indent limit))
  413. (case-fold-search t))
  414. (and prev-p
  415. (let ((item-ref (goto-char prev-p))
  416. (ind-ref 10000))
  417. (forward-line -1)
  418. (catch 'exit
  419. (while t
  420. (let ((ind (org-get-indentation)))
  421. (cond
  422. ((or (<= (point) limit)
  423. (looking-at "^[ \t]*:END:"))
  424. (throw 'exit item-ref))
  425. ((looking-at "^[ \t]*$")
  426. (skip-chars-backward " \r\t\n")
  427. (beginning-of-line))
  428. ((looking-at "^[ \t]*#\\+end_")
  429. (re-search-backward "^[ \t]*#\\+begin_"))
  430. ((not (looking-at org-item-beginning-re))
  431. (setq ind-ref (min ind ind-ref))
  432. (forward-line -1))
  433. ((>= ind ind-ref)
  434. (throw 'exit item-ref))
  435. (t
  436. (setq item-ref (point-at-bol) ind-ref 10000)
  437. (forward-line -1)))))))))))
  438. (defun org-list-bottom-point-with-indent (limit)
  439. "Return point just before list ending or nil if not in a list.
  440. Argument LIMIT specifies the lower-bound of the search.
  441. List ending is determined by the indentation of text. See
  442. `org-list-ending-method' for more information."
  443. (save-excursion
  444. (let* ((beg-item (org-in-item-p))
  445. (ind-ref (save-excursion
  446. (when beg-item
  447. (goto-char beg-item)
  448. (org-get-indentation))))
  449. (case-fold-search t))
  450. ;; do not start inside a block
  451. (org-list-maybe-skip-block #'re-search-forward limit)
  452. (beginning-of-line)
  453. (and beg-item
  454. (catch 'exit
  455. (while t
  456. (let ((ind (org-get-indentation)))
  457. (cond
  458. ((or (>= (point) limit)
  459. (looking-at "^[ \t]*:END:"))
  460. (throw 'exit (point)))
  461. ((looking-at "^[ \t]*$")
  462. (skip-chars-forward " \r\t\n")
  463. (beginning-of-line))
  464. ((looking-at org-item-beginning-re)
  465. (setq ind-ref (min ind ind-ref))
  466. (forward-line 1))
  467. ((<= ind ind-ref)
  468. (throw 'exit (point-at-bol)))
  469. ((looking-at "^[ \t]*#\\+begin_")
  470. (re-search-forward "[ \t]*#\\+end_")
  471. (forward-line 1))
  472. (t (forward-line 1))))))))))
  473. (defun org-list-at-regexp-after-bullet-p (regexp)
  474. "Is point at a list item with REGEXP after bullet?"
  475. (and (org-at-item-p)
  476. (save-excursion
  477. (goto-char (match-end 0))
  478. ;; Ignore counter if any
  479. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  480. (goto-char (match-end 0)))
  481. (looking-at regexp))))
  482. (defun org-list-get-item-same-level (search-fun pos limit pre-move)
  483. "Return point at the beginning of next item at the same level.
  484. Search items using function SEARCH-FUN, from POS to LIMIT. It
  485. uses PRE-MOVE before search. Return nil if no item was found."
  486. (save-excursion
  487. (goto-char pos)
  488. (let ((ind (progn
  489. (org-beginning-of-item)
  490. (org-get-indentation)))
  491. (start (point-at-bol)))
  492. ;; We don't want to match the current line.
  493. (funcall pre-move)
  494. ;; Skip any sublist on the way
  495. (while (and (funcall search-fun org-item-beginning-re limit t)
  496. (> (org-get-indentation) ind)))
  497. (when (and (/= (point-at-bol) start) ; Have we moved ?
  498. (= (org-get-indentation) ind))
  499. (point-at-bol)))))
  500. (defun org-list-insert-item-generic (pos &optional checkbox after-bullet)
  501. "Insert a new list item at POS.
  502. If POS is before first character after bullet of the item, the
  503. new item will be created before the current one.
  504. Insert a checkbox if CHECKBOX is non-nil, and string AFTER-BULLET
  505. after the bullet. Cursor will be after this text once the
  506. function ends."
  507. (goto-char pos)
  508. ;; Is point in a special block?
  509. (when (org-in-regexps-block-p
  510. "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  511. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  512. (if (not (cdr (assq 'insert org-list-automatic-rules)))
  513. ;; Rule in `org-list-automatic-rules' forbids insertion.
  514. (error "Cannot insert item inside a block")
  515. ;; Else, move before it prior to add a new item.
  516. (end-of-line)
  517. (re-search-backward "^[ \t]*#\\+\\(begin\\|BEGIN\\)_" nil t)
  518. (end-of-line 0)))
  519. (let* ((true-pos (point))
  520. (bullet (and (org-beginning-of-item)
  521. (org-list-bullet-string (org-get-bullet))))
  522. (ind (org-get-indentation))
  523. (before-p (progn
  524. ;; Description item: text starts after colons.
  525. (or (org-at-item-description-p)
  526. ;; At a checkbox: text starts after it.
  527. (org-at-item-checkbox-p)
  528. ;; Otherwise, text starts after bullet.
  529. (org-at-item-p))
  530. (<= true-pos (match-end 0))))
  531. ;; Guess number of blank lines used to separate items.
  532. (blank-lines-nb
  533. (let ((insert-blank-p
  534. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  535. usr-blank)
  536. (cond
  537. ;; Trivial cases where there should be none.
  538. ((or (and (not (eq org-list-ending-method 'indent))
  539. org-empty-line-terminates-plain-lists)
  540. (not insert-blank-p)) 0)
  541. ;; When `org-blank-before-new-entry' says so, it is 1.
  542. ((eq insert-blank-p t) 1)
  543. ;; plain-list-item is 'auto. Count blank lines separating
  544. ;; neighbours items in list.
  545. (t (let ((next-p (org-get-next-item (point)
  546. (org-list-bottom-point))))
  547. (cond
  548. ;; Is there a next item?
  549. (next-p (goto-char next-p)
  550. (org-back-over-empty-lines))
  551. ;; Is there a previous item?
  552. ((not (org-list-first-item-p)) (org-back-over-empty-lines))
  553. ;; User inserted blank lines, trust him
  554. ((and (> true-pos (org-end-of-item-before-blank))
  555. (> (save-excursion
  556. (goto-char true-pos)
  557. (skip-chars-backward " \t")
  558. (setq usr-blank (org-back-over-empty-lines))) 0))
  559. usr-blank)
  560. ;; Item alone: count lines separating it from parent, if any
  561. ((/= (org-list-top-point) (point-at-bol))
  562. (org-back-over-empty-lines))
  563. ;; Are there blank lines inside the item ?
  564. ((save-excursion
  565. (org-search-forward-unenclosed
  566. "^[ \t]*$" (org-end-of-item-before-blank) t)) 1)
  567. ;; No parent: no blank line.
  568. (t 0)))))))
  569. (insert-fun
  570. (lambda (text)
  571. ;; insert bullet above item in order to avoid bothering
  572. ;; with possible blank lines ending last item.
  573. (org-beginning-of-item)
  574. (indent-to-column ind)
  575. (insert (concat bullet (when checkbox "[ ] ") after-bullet))
  576. ;; Stay between after-bullet and before text.
  577. (save-excursion
  578. (insert (concat text (make-string (1+ blank-lines-nb) ?\n))))
  579. (unless before-p (org-move-item-down))
  580. (when checkbox (org-update-checkbox-count-maybe)))))
  581. (goto-char true-pos)
  582. (cond
  583. (before-p (funcall insert-fun nil)
  584. (org-list-repair) t)
  585. ;; Can't split item: insert bullet at the end of item.
  586. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  587. (funcall insert-fun nil) t)
  588. ;; else, insert a new bullet along with everything from point
  589. ;; down to last non-blank line of item.
  590. (t
  591. (delete-horizontal-space)
  592. ;; Get pos again in case previous command modified line.
  593. (let* ((pos (point))
  594. (end-before-blank (org-end-of-item-before-blank))
  595. (after-text
  596. (when (< pos end-before-blank)
  597. (prog1
  598. (delete-and-extract-region pos end-before-blank)
  599. ;; delete any blank line at and before point.
  600. (beginning-of-line)
  601. (while (looking-at "^[ \t]*$")
  602. (delete-region (point-at-bol) (1+ (point-at-eol)))
  603. (beginning-of-line 0))))))
  604. (funcall insert-fun after-text) t)))))
  605. (defvar org-last-indent-begin-marker (make-marker))
  606. (defvar org-last-indent-end-marker (make-marker))
  607. (defun org-list-indent-item-generic (arg no-subtree)
  608. "Indent a local list item including its children.
  609. When number ARG is a negative, item will be outdented, otherwise
  610. it will be indented.
  611. If a region is active, all items in it will be moved. If
  612. NO-SUBTREE is non-nil, only indent the item itself, not its
  613. children.
  614. Return t if successful."
  615. (save-restriction
  616. (unless (or (org-at-item-p)
  617. (and (org-region-active-p)
  618. (goto-char region-beginning)
  619. (org-at-item-p)))
  620. (error "Not on an item"))
  621. ;; Are we going to move the whole list?
  622. (let ((specialp (and (cdr (assq 'indent org-list-automatic-rules))
  623. (not no-subtree)
  624. (= (org-list-top-point) (point-at-bol)))))
  625. ;; Determine begin and end points of zone to indent. If moving by
  626. ;; subtrees, ensure we don't drag additional items on subsequent
  627. ;; moves.
  628. (unless (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  629. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  630. (if (org-region-active-p)
  631. (progn
  632. (set-marker org-last-indent-begin-marker (region-beginning))
  633. (set-marker org-last-indent-end-marker (region-end)))
  634. (set-marker org-last-indent-begin-marker (point-at-bol))
  635. (set-marker org-last-indent-end-marker
  636. (save-excursion
  637. (cond
  638. (specialp
  639. (org-list-bottom-point))
  640. (no-subtree
  641. (org-end-of-item-or-at-child))
  642. (t (org-end-of-item)))))))
  643. ;; Get everything ready
  644. (let* ((beg (marker-position org-last-indent-begin-marker))
  645. (end (marker-position org-last-indent-end-marker))
  646. (struct (progn
  647. (when specialp (narrow-to-region beg end))
  648. (org-list-struct beg end (< arg 0))))
  649. (origins (org-list-struct-origins struct))
  650. (beg-item (assq beg struct)))
  651. (cond
  652. ;; Special case: moving top-item with indent rule
  653. (specialp
  654. (let* ((level-skip (org-level-increment))
  655. (offset (if (< arg 0) (- level-skip) level-skip))
  656. (top-ind (nth 1 beg-item)))
  657. (if (< (+ top-ind offset) 0)
  658. (error "Cannot outdent beyond margin")
  659. ;; Change bullet if necessary
  660. (when (and (= (+ top-ind offset) 0)
  661. (string-match "*" (nth 2 beg-item)))
  662. (setcdr beg-item (list (nth 1 beg-item)
  663. (org-list-bullet-string "-"))))
  664. ;; Shift ancestor
  665. (let ((anc (car struct)))
  666. (setcdr anc (list (+ (nth 1 anc) offset) "" nil)))
  667. (org-list-struct-fix-struct struct origins)
  668. (org-list-struct-apply-struct struct))))
  669. ;; Forbidden move
  670. ((and (< arg 0)
  671. (or (and no-subtree
  672. (not (org-region-active-p))
  673. (org-list-struct-get-child beg-item struct))
  674. (let ((last-item (save-excursion
  675. (goto-char end)
  676. (skip-chars-backward " \r\t\n")
  677. (org-beginning-of-item)
  678. (org-list-struct-assoc-at-point))))
  679. (org-list-struct-get-child last-item struct))))
  680. (error "Cannot outdent an item without its children"))
  681. ;; Normal shifting
  682. (t
  683. (let* ((shifted-ori (if (< arg 0)
  684. (org-list-struct-outdent beg end origins)
  685. (org-list-struct-indent beg end origins struct))))
  686. (org-list-struct-fix-struct struct shifted-ori)
  687. (org-list-struct-apply-struct struct)))))))
  688. ;; Return value
  689. t)
  690. ;;; Predicates
  691. (defun org-in-item-p ()
  692. "Is the cursor inside a plain list?
  693. This checks `org-list-ending-method'."
  694. (unless (let ((outline-regexp org-outline-regexp)) (org-at-heading-p))
  695. (let* ((prev-head (save-excursion (outline-previous-heading)))
  696. (bound (if prev-head
  697. (or (save-excursion
  698. (let ((case-fold-search t))
  699. (re-search-backward "^[ \t]*:END:" prev-head t)))
  700. prev-head)
  701. (point-min))))
  702. (cond
  703. ((eq org-list-ending-method 'indent)
  704. (org-list-in-item-p-with-indent bound))
  705. ((eq org-list-ending-method 'both)
  706. (and (org-list-in-item-p-with-regexp bound)
  707. (org-list-in-item-p-with-indent bound)))
  708. (t (org-list-in-item-p-with-regexp bound))))))
  709. (defun org-list-first-item-p ()
  710. "Is this item the first item in a plain list?
  711. Assume point is at an item."
  712. (save-excursion
  713. (beginning-of-line)
  714. (let ((ind (org-get-indentation)))
  715. (or (not (org-search-backward-unenclosed
  716. org-item-beginning-re (org-list-top-point) t))
  717. (< (org-get-indentation) ind)))))
  718. (defun org-at-item-p ()
  719. "Is point in a line starting a hand-formatted item?"
  720. (save-excursion
  721. (beginning-of-line) (looking-at org-item-beginning-re)))
  722. (defun org-at-item-bullet-p ()
  723. "Is point at the bullet of a plain list item?"
  724. (and (org-at-item-p)
  725. (not (member (char-after) '(?\ ?\t)))
  726. (< (point) (match-end 0))))
  727. (defun org-at-item-timer-p ()
  728. "Is point at a line starting a plain list item with a timer?"
  729. (org-list-at-regexp-after-bullet-p
  730. "\\([0-9]+:[0-9]+:[0-9]+\\)[ \t]+::[ \t]+"))
  731. (defun org-at-item-description-p ()
  732. "Is point at a description list item?"
  733. (org-list-at-regexp-after-bullet-p "\\(\\S-.+\\)[ \t]+::[ \t]+"))
  734. (defun org-at-item-checkbox-p ()
  735. "Is point at a line starting a plain-list item with a checklet?"
  736. (org-list-at-regexp-after-bullet-p "\\(\\[[- X]\\]\\)[ \t]+"))
  737. (defun org-checkbox-blocked-p ()
  738. "Is the current checkbox blocked from for being checked now?
  739. A checkbox is blocked if all of the following conditions are fulfilled:
  740. 1. The checkbox is not checked already.
  741. 2. The current entry has the ORDERED property set.
  742. 3. There is an unchecked checkbox in this entry before the current line."
  743. (catch 'exit
  744. (save-match-data
  745. (save-excursion
  746. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  747. (when (equal (match-string 1) "[X]")
  748. ;; the box is already checked!
  749. (throw 'exit nil))
  750. (let ((end (point-at-bol)))
  751. (condition-case nil (org-back-to-heading t)
  752. (error (throw 'exit nil)))
  753. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  754. (when (org-search-forward-unenclosed
  755. "^[ \t]*[-+*0-9.)]+[ \t]+\\(\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[[- ]\\]" end t)
  756. (org-current-line)))))))
  757. ;;; Navigate
  758. (defun org-list-top-point ()
  759. "Return point at the top level in a list, or nil if not in a list."
  760. (let* ((prev-head (save-excursion (outline-previous-heading)))
  761. (bound (if prev-head
  762. (or (save-excursion
  763. (let ((case-fold-search t))
  764. (re-search-backward "^[ \t]*:END:" prev-head t)))
  765. prev-head)
  766. (point-min))))
  767. (cond
  768. ((eq org-list-ending-method 'indent)
  769. (org-list-top-point-with-indent bound))
  770. ((eq org-list-ending-method 'both)
  771. (let ((top-re (org-list-top-point-with-regexp bound))
  772. (top-ind (org-list-top-point-with-indent bound)))
  773. (if (and top-re top-ind)
  774. (max top-re top-ind)
  775. (or top-re top-ind))))
  776. (t (org-list-top-point-with-regexp bound)))))
  777. (defun org-list-bottom-point ()
  778. "Return point just before list ending or nil if not in a list."
  779. (let* ((next-head (save-excursion
  780. (and (let ((outline-regexp org-outline-regexp))
  781. ;; Use default regexp because folding
  782. ;; changes OUTLINE-REGEXP.
  783. (outline-next-heading))
  784. (skip-chars-backward " \r\t\n")
  785. (1+ (point-at-eol)))))
  786. (limit (or (save-excursion
  787. (and (re-search-forward "^[ \t]*:END:" next-head t)
  788. (point-at-bol)))
  789. next-head
  790. (point-max))))
  791. (cond
  792. ((eq org-list-ending-method 'indent)
  793. (org-list-bottom-point-with-indent limit))
  794. ((eq org-list-ending-method 'both)
  795. (let ((bottom-re (org-list-bottom-point-with-regexp limit))
  796. (bottom-ind (org-list-bottom-point-with-indent limit)))
  797. (if (and bottom-re bottom-ind)
  798. (min bottom-re bottom-ind)
  799. (or bottom-re bottom-ind))))
  800. (t (org-list-bottom-point-with-regexp limit)))))
  801. (defun org-beginning-of-item ()
  802. "Go to the beginning of the current hand-formatted item.
  803. If the cursor is not in an item, throw an error. Return point."
  804. (interactive)
  805. (if (not (org-in-item-p))
  806. (error "Not in an item")
  807. ;; Possibly match the current line.
  808. (end-of-line)
  809. (org-search-backward-unenclosed org-item-beginning-re nil t)
  810. (goto-char (point-at-bol))))
  811. (defun org-end-of-item ()
  812. "Go to the end of the current hand-formatted item.
  813. If the cursor is not in an item, throw an error. Return point."
  814. (interactive)
  815. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  816. (if next-p (goto-char next-p) (org-end-of-item-list))))
  817. (defun org-end-of-item-or-at-child ()
  818. "Move to the end of the item text, stops before the first child if any."
  819. (let ((limit (org-list-bottom-point)))
  820. (end-of-line)
  821. (goto-char
  822. (if (org-search-forward-unenclosed org-item-beginning-re limit t)
  823. (point-at-bol)
  824. limit))))
  825. (defun org-end-of-item-before-blank ()
  826. "Return point at end of item, before any blank line.
  827. Point returned is at eol."
  828. (save-excursion
  829. (org-end-of-item)
  830. (skip-chars-backward " \r\t\n")
  831. (point-at-eol)))
  832. (defun org-get-next-item (pos limit)
  833. "Get the point of the next item at the same level as POS.
  834. Stop searching at LIMIT. Return nil if no item is found. This
  835. function does not move point."
  836. (org-list-get-item-same-level
  837. #'org-search-forward-unenclosed pos limit #'end-of-line))
  838. (defun org-get-previous-item (pos limit)
  839. "Get the point of the previous item at the same level as POS.
  840. Stop searching at LIMIT. Return nil if no item is found. This
  841. function does not move point."
  842. (org-list-get-item-same-level
  843. #'org-search-backward-unenclosed pos limit #'beginning-of-line))
  844. (defun org-next-item ()
  845. "Move to the beginning of the next item.
  846. Item is at the same level in the current plain list. Error if not
  847. in a plain list, or if this is the last item in the list."
  848. (interactive)
  849. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  850. (if next-p (goto-char next-p) (error "On last item"))))
  851. (defun org-previous-item ()
  852. "Move to the beginning of the previous item.
  853. Item is at the same level in the current plain list. Error if not
  854. in a plain list, or if this is the first item in the list."
  855. (interactive)
  856. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  857. (if prev-p (goto-char prev-p) (error "On first item"))))
  858. (defun org-beginning-of-item-list ()
  859. "Go to the beginning item of the current list or sublist.
  860. Return point."
  861. (interactive)
  862. (let ((limit (org-list-top-point))
  863. prev-p)
  864. (while (setq prev-p (org-get-previous-item (point) limit))
  865. (goto-char prev-p))
  866. (goto-char (point-at-bol))))
  867. (defun org-end-of-item-list ()
  868. "Go to the end of the current list or sublist.
  869. Return point."
  870. (interactive)
  871. (org-beginning-of-item)
  872. (let ((limit (org-list-bottom-point))
  873. (ind (org-get-indentation)))
  874. (while (and (/= (point) limit)
  875. (>= (org-get-indentation) ind))
  876. (org-search-forward-unenclosed org-item-beginning-re limit 'move))
  877. (if (= (point) limit) limit (goto-char (point-at-bol)))))
  878. ;;; Manipulate
  879. (defun org-list-exchange-items (beg-A beg-B)
  880. "Swap item starting at BEG-A with item starting at BEG-B.
  881. Blank lines at the end of items are left in place. Assumes BEG-A
  882. is lesser than BEG-B."
  883. (save-excursion
  884. (let* ((end-of-item-no-blank (lambda (pos)
  885. (goto-char pos)
  886. (goto-char (org-end-of-item-before-blank))))
  887. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  888. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  889. (body-A (buffer-substring beg-A end-A-no-blank))
  890. (body-B (buffer-substring beg-B end-B-no-blank))
  891. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  892. (goto-char beg-A)
  893. (delete-region beg-A end-B-no-blank)
  894. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  895. (defun org-move-item-down ()
  896. "Move the plain list item at point down, i.e. swap with following item.
  897. Subitems (items with larger indentation) are considered part of the item,
  898. so this really moves item trees."
  899. (interactive)
  900. (let ((pos (point))
  901. (col (current-column))
  902. (actual-item (org-beginning-of-item))
  903. (next-item (org-get-next-item (point) (org-list-bottom-point))))
  904. (if (not next-item)
  905. (progn
  906. (goto-char pos)
  907. (error "Cannot move this item further down"))
  908. (org-list-exchange-items actual-item next-item)
  909. (org-list-repair)
  910. (org-next-item)
  911. (move-to-column col))))
  912. (defun org-move-item-up ()
  913. "Move the plain list item at point up, i.e. swap with previous item.
  914. Subitems (items with larger indentation) are considered part of the item,
  915. so this really moves item trees."
  916. (interactive)
  917. (let ((pos (point))
  918. (col (current-column))
  919. (actual-item (org-beginning-of-item))
  920. (prev-item (org-get-previous-item (point) (org-list-top-point))))
  921. (if (not prev-item)
  922. (progn
  923. (goto-char pos)
  924. (error "Cannot move this item further up"))
  925. (org-list-exchange-items prev-item actual-item)
  926. (org-list-repair)
  927. (move-to-column col))))
  928. (defun org-insert-item (&optional checkbox)
  929. "Insert a new item at the current level.
  930. If cursor is before first character after bullet of the item, the
  931. new item will be created before the current one.
  932. If CHECKBOX is non-nil, add a checkbox next to the bullet.
  933. Return t when things worked, nil when we are not in an item, or
  934. item is invisible."
  935. (unless (or (not (org-in-item-p))
  936. (org-invisible-p))
  937. (if (save-excursion
  938. (org-beginning-of-item)
  939. (org-at-item-timer-p))
  940. ;; Timer list: delegate to `org-timer-item'.
  941. (progn (org-timer-item) t)
  942. ;; if we're in a description list, ask for the new term.
  943. (let ((desc-text (when (save-excursion
  944. (and (org-beginning-of-item)
  945. (org-at-item-description-p)))
  946. (concat (read-string "Term: ") " :: "))))
  947. ;; Don't insert a checkbox if checkbox rule is applied and it
  948. ;; is a description item.
  949. (org-list-insert-item-generic
  950. (point) (and checkbox
  951. (or (not desc-text)
  952. (not (cdr (assq 'checkbox org-list-automatic-rules)))))
  953. desc-text)))))
  954. ;;; Structures
  955. ;; The idea behind structures is to avoid moving back and forth in the
  956. ;; buffer on costly operations like indenting or fixing bullets.
  957. ;; It achieves this by taking a snapshot of an interesting part of the
  958. ;; list, in the shape of an alist, with `org-list-struct'.
  959. ;; It then proceeds to changes directly on the alist. When those are
  960. ;; done, `org-list-struct-apply-struct' applies the changes in the
  961. ;; buffer.
  962. (defun org-list-struct-assoc-at-point ()
  963. "Return the structure association at point.
  964. It is a cons-cell whose key is point and values are indentation,
  965. bullet string and bullet counter, if any."
  966. (save-excursion
  967. (beginning-of-line)
  968. (list (point-at-bol)
  969. (org-get-indentation)
  970. (progn
  971. (looking-at "^[ \t]*\\([-+*0-9.)]+[ \t]+\\)")
  972. (match-string 1))
  973. (progn
  974. (goto-char (match-end 0))
  975. (and (looking-at "\\[@\\(?:start:\\)?\\([0-9]+\\)\\]")
  976. (match-string 1))))))
  977. (defun org-list-struct (begin end &optional outdent)
  978. "Return the structure containing the list between BEGIN and END.
  979. A structure is an alist where key is point of item and values
  980. are, in that order, indentation, bullet string and value of
  981. counter, if any. A structure contains every list and sublist that
  982. has items between BEGIN and END along with their common ancestor.
  983. If no such ancestor can be found, the function will add a virtual
  984. ancestor at position 0.
  985. If OUTDENT is non-nil, it will also grab all of the parent list
  986. and the grand-parent. Setting OUTDENT to t is mandatory when next
  987. change is an outdent."
  988. (save-excursion
  989. (let* ((top (org-list-top-point))
  990. (bottom (org-list-bottom-point))
  991. struct
  992. (extend
  993. (lambda (struct)
  994. (let* ((ind-min (apply 'min (mapcar 'cadr struct)))
  995. (begin (caar struct))
  996. (end (caar (last struct)))
  997. pre-list post-list)
  998. (goto-char begin)
  999. ;; Find beginning of most outdented list (min list)
  1000. (while (and (org-search-backward-unenclosed
  1001. org-item-beginning-re top t)
  1002. (>= (org-get-indentation) ind-min))
  1003. (setq pre-list (cons (org-list-struct-assoc-at-point)
  1004. pre-list)))
  1005. ;; Now get the parent. If none, add a virtual ancestor
  1006. (if (< (org-get-indentation) ind-min)
  1007. (setq pre-list (cons (org-list-struct-assoc-at-point)
  1008. pre-list))
  1009. (setq pre-list (cons (list 0 (org-get-indentation) "" nil)
  1010. pre-list)))
  1011. ;; Find end of min list
  1012. (goto-char end)
  1013. (end-of-line)
  1014. (while (and (org-search-forward-unenclosed
  1015. org-item-beginning-re bottom 'move)
  1016. (>= (org-get-indentation) ind-min))
  1017. (setq post-list (cons (org-list-struct-assoc-at-point)
  1018. post-list)))
  1019. ;; Is list is malformed? If some items are less
  1020. ;; indented that top-item, add them anyhow.
  1021. (when (and (= (caar pre-list) 0) (org-at-item-p))
  1022. (setq post-list (cons (org-list-struct-assoc-at-point)
  1023. post-list))
  1024. (while (org-search-forward-unenclosed
  1025. org-item-beginning-re bottom t)
  1026. (setq post-list (cons (org-list-struct-assoc-at-point)
  1027. post-list))))
  1028. (append pre-list struct (reverse post-list))))))
  1029. ;; Here we start: first get the core zone...
  1030. (goto-char end)
  1031. (while (org-search-backward-unenclosed org-item-beginning-re begin t)
  1032. (setq struct (cons (org-list-struct-assoc-at-point) struct)))
  1033. ;; ... then, extend it to make it a structure...
  1034. (let ((extended (funcall extend struct)))
  1035. ;; ... twice when OUTDENT is non-nil and struct still can be
  1036. ;; extended
  1037. (if (and outdent (> (caar extended) 0))
  1038. (funcall extend extended)
  1039. extended)))))
  1040. (defun org-list-struct-origins (struct)
  1041. "Return an alist where key is item's position and value parent's.
  1042. STRUCT is the list's structure looked up."
  1043. (let* ((struct-rev (reverse struct))
  1044. (acc (list (cons (nth 1 (car struct)) 0)))
  1045. (prev-item (lambda (item)
  1046. (car (nth 1 (member (assq item struct) struct-rev)))))
  1047. (get-origins
  1048. (lambda (item)
  1049. (let* ((item-pos (car item))
  1050. (ind (nth 1 item))
  1051. (prev-ind (caar acc)))
  1052. (cond
  1053. ;; List closing.
  1054. ((> prev-ind ind)
  1055. (let ((current-origin (or (member (assq ind acc) acc)
  1056. ;; needed if top-point is
  1057. ;; not the most outdented
  1058. (last acc))))
  1059. (setq acc current-origin)
  1060. (cons item-pos (cdar acc))))
  1061. ;; New list
  1062. ((< prev-ind ind)
  1063. (let ((origin (funcall prev-item item-pos)))
  1064. (setq acc (cons (cons ind origin) acc))
  1065. (cons item-pos origin)))
  1066. ;; Current list going on
  1067. (t (cons item-pos (cdar acc))))))))
  1068. (cons '(0 . 0) (mapcar get-origins (cdr struct)))))
  1069. (defun org-list-struct-get-parent (item struct origins)
  1070. "Return parent association of ITEM in STRUCT or nil.
  1071. ORIGINS is the alist of parents. See `org-list-struct-origins'."
  1072. (let* ((parent-pos (cdr (assq (car item) origins))))
  1073. (when (> parent-pos 0) (assq parent-pos struct))))
  1074. (defun org-list-struct-get-child (item struct)
  1075. "Return child association of ITEM in STRUCT or nil."
  1076. (let ((ind (nth 1 item))
  1077. (next-item (cadr (member item struct))))
  1078. (when (and next-item (> (nth 1 next-item) ind)) next-item)))
  1079. (defun org-list-struct-fix-bul (struct origins)
  1080. "Verify and correct bullets for every association in STRUCT.
  1081. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1082. This function modifies STRUCT."
  1083. (let* (acc
  1084. (init-bul (lambda (item)
  1085. (let ((counter (nth 3 item))
  1086. (bullet (org-list-bullet-string (nth 2 item))))
  1087. (cond
  1088. ((and (string-match "[0-9]+" bullet) counter)
  1089. (replace-match counter nil nil bullet))
  1090. ((string-match "[0-9]+" bullet)
  1091. (replace-match "1" nil nil bullet))
  1092. (t bullet)))))
  1093. (set-bul (lambda (item bullet)
  1094. (setcdr item (list (nth 1 item) bullet (nth 3 item)))))
  1095. (get-bul (lambda (item bullet)
  1096. (let* ((counter (nth 3 item)))
  1097. (if (and counter (string-match "[0-9]+" bullet))
  1098. (replace-match counter nil nil bullet)
  1099. bullet))))
  1100. (fix-bul
  1101. (lambda (item) struct
  1102. (let* ((parent (cdr (assq (car item) origins)))
  1103. (orig-ref (assq parent acc)))
  1104. (if orig-ref
  1105. ;; Continuing previous list
  1106. (let* ((prev-bul (cdr orig-ref))
  1107. (new-bul (funcall get-bul item prev-bul)))
  1108. (setcdr orig-ref (org-list-inc-bullet-maybe new-bul))
  1109. (funcall set-bul item new-bul))
  1110. ;; A new list is starting
  1111. (let ((new-bul (funcall init-bul item)))
  1112. (funcall set-bul item new-bul)
  1113. (setq acc (cons (cons parent
  1114. (org-list-inc-bullet-maybe new-bul))
  1115. acc))))))))
  1116. (mapc fix-bul (cdr struct))))
  1117. (defun org-list-struct-fix-ind (struct origins)
  1118. "Verify and correct indentation for every association in STRUCT.
  1119. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1120. This function modifies STRUCT."
  1121. (let* ((headless (cdr struct))
  1122. (ancestor (car struct))
  1123. (top-ind (+ (nth 1 ancestor) (length (nth 2 ancestor))))
  1124. (new-ind
  1125. (lambda (item)
  1126. (let* ((parent (org-list-struct-get-parent item headless origins)))
  1127. (if parent
  1128. ;; Indent like parent + length of parent's bullet
  1129. (setcdr item (cons (+ (length (nth 2 parent)) (nth 1 parent))
  1130. (cddr item)))
  1131. ;; If no parent, indent like top-point
  1132. (setcdr item (cons top-ind (cddr item))))))))
  1133. (mapc new-ind headless)))
  1134. (defun org-list-struct-fix-struct (struct origins)
  1135. "Return STRUCT with correct bullets and indentation.
  1136. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1137. Only elements of STRUCT that have changed are returned."
  1138. (let ((old (copy-alist struct)))
  1139. (org-list-struct-fix-bul struct origins)
  1140. (org-list-struct-fix-ind struct origins)
  1141. (delq nil (mapcar (lambda (e) (when (not (equal (pop old) e)) e)) struct))))
  1142. (defun org-list-struct-outdent (start end origins)
  1143. "Outdent items in a structure.
  1144. Items are indented when their key is between START, included, and
  1145. END, excluded.
  1146. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1147. STRUCT is the concerned structure."
  1148. (let* (acc
  1149. (out (lambda (cell)
  1150. (let* ((item (car cell))
  1151. (parent (cdr cell)))
  1152. (cond
  1153. ;; Item not yet in zone: keep association
  1154. ((< item start) cell)
  1155. ;; Item out of zone: follow associations in acc
  1156. ((>= item end)
  1157. (let ((convert (assq parent acc)))
  1158. (if convert (cons item (cdr convert)) cell)))
  1159. ;; Item has no parent: error
  1160. ((<= parent 0)
  1161. (error "Cannot outdent top-level items"))
  1162. ;; Parent is outdented: keep association
  1163. ((>= parent start)
  1164. (setq acc (cons (cons parent item) acc)) cell)
  1165. (t
  1166. ;; Parent isn't outdented: reparent to grand-parent
  1167. (let ((grand-parent (cdr (assq parent origins))))
  1168. (setq acc (cons (cons parent item) acc))
  1169. (cons item grand-parent))))))))
  1170. (mapcar out origins)))
  1171. (defun org-list-struct-indent (start end origins struct)
  1172. "Indent items in a structure.
  1173. Items are indented when their key is between START, included, and
  1174. END, excluded.
  1175. ORIGINS is the alist of parents. See `org-list-struct-origins'.
  1176. STRUCT is the concerned structure. It may be modified if
  1177. `org-list-demote-modify-bullet' matches bullets between START and
  1178. END."
  1179. (let* (acc
  1180. (orig-rev (reverse origins))
  1181. (get-prev-item
  1182. (lambda (cell parent)
  1183. (car (rassq parent (cdr (memq cell orig-rev))))))
  1184. (set-assoc
  1185. (lambda (cell)
  1186. (setq acc (cons cell acc)) cell))
  1187. (change-bullet-maybe
  1188. (lambda (item)
  1189. (let* ((full-item (assq item struct))
  1190. (item-bul (org-trim (nth 2 full-item)))
  1191. (new-bul-p (cdr (assoc item-bul org-list-demote-modify-bullet))))
  1192. (when new-bul-p
  1193. ;; new bullet is stored without space to ensure item
  1194. ;; will be modified
  1195. (setcdr full-item
  1196. (list (nth 1 full-item)
  1197. new-bul-p
  1198. (nth 3 full-item)))))))
  1199. (ind
  1200. (lambda (cell)
  1201. (let* ((item (car cell))
  1202. (parent (cdr cell)))
  1203. (cond
  1204. ;; Item not yet in zone: keep association
  1205. ((< item start) cell)
  1206. ((>= item end)
  1207. ;; Item out of zone: follow associations in acc
  1208. (let ((convert (assq parent acc)))
  1209. (if convert (cons item (cdr convert)) cell)))
  1210. (t
  1211. ;; Item is in zone...
  1212. (let ((prev (funcall get-prev-item cell parent)))
  1213. ;; Check if bullet needs to be changed
  1214. (funcall change-bullet-maybe item)
  1215. (cond
  1216. ;; First item indented but not parent: error
  1217. ((and (or (not prev) (= prev 0)) (< parent start))
  1218. (error "Cannot indent the first item of a list"))
  1219. ;; First item and parent indented: keep same parent
  1220. ((or (not prev) (= prev 0))
  1221. (funcall set-assoc cell))
  1222. ;; Previous item not indented: reparent to it
  1223. ((< prev start)
  1224. (funcall set-assoc (cons item prev)))
  1225. ;; Previous item indented: reparent like it
  1226. (t
  1227. (funcall set-assoc (cons item
  1228. (cdr (assq prev acc)))))))))))))
  1229. (mapcar ind origins)))
  1230. (defun org-list-struct-apply-struct (struct)
  1231. "Apply modifications to list so it mirrors STRUCT.
  1232. Initial position is restored after the changes."
  1233. (let* ((pos (copy-marker (point)))
  1234. (ancestor (caar struct))
  1235. (modify
  1236. (lambda (item)
  1237. (goto-char (car item))
  1238. (let* ((new-ind (nth 1 item))
  1239. (new-bul (org-list-bullet-string (nth 2 item)))
  1240. (old-ind (org-get-indentation))
  1241. (old-bul (progn
  1242. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1243. (match-string 1)))
  1244. (old-body-ind (+ (length old-bul) old-ind))
  1245. (new-body-ind (+ (length new-bul) new-ind)))
  1246. ;; 1. Shift item's body
  1247. (unless (= old-body-ind new-body-ind)
  1248. (org-shift-item-indentation (- new-body-ind old-body-ind)))
  1249. ;; 2. Replace bullet
  1250. (unless (equal new-bul old-bul)
  1251. (save-excursion
  1252. (looking-at "[ \t]*\\(\\S-+[ \t]*\\)")
  1253. (replace-match new-bul nil nil nil 1)))
  1254. ;; 3. Indent item to appropriate column
  1255. (unless (= new-ind old-ind)
  1256. (delete-region (point-at-bol)
  1257. (progn
  1258. (skip-chars-forward " \t")
  1259. (point)))
  1260. (indent-to new-ind)))))
  1261. ;; Remove ancestor if it is left.
  1262. (struct-to-apply (if (or (not ancestor) (= 0 ancestor))
  1263. (cdr struct)
  1264. struct)))
  1265. ;; Apply changes from bottom to top
  1266. (mapc modify (nreverse struct-to-apply))
  1267. (goto-char pos)))
  1268. ;;; Indentation
  1269. (defun org-get-string-indentation (s)
  1270. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  1271. (let ((n -1) (i 0) (w tab-width) c)
  1272. (catch 'exit
  1273. (while (< (setq n (1+ n)) (length s))
  1274. (setq c (aref s n))
  1275. (cond ((= c ?\ ) (setq i (1+ i)))
  1276. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  1277. (t (throw 'exit t)))))
  1278. i))
  1279. (defun org-shift-item-indentation (delta)
  1280. "Shift the indentation in current item by DELTA.
  1281. Sub-items are not moved."
  1282. (save-excursion
  1283. (let ((beg (point-at-bol))
  1284. (end (org-end-of-item-or-at-child)))
  1285. (beginning-of-line (unless (eolp) 0))
  1286. (while (> (point) beg)
  1287. (when (looking-at "[ \t]*\\S-")
  1288. ;; this is not an empty line
  1289. (let ((i (org-get-indentation)))
  1290. (when (and (> i 0) (> (+ i delta) 0))
  1291. (indent-line-to (+ i delta)))))
  1292. (beginning-of-line 0)))))
  1293. (defun org-outdent-item ()
  1294. "Outdent a local list item, but not its children."
  1295. (interactive)
  1296. (org-list-indent-item-generic -1 t))
  1297. (defun org-indent-item ()
  1298. "Indent a local list item, but not its children."
  1299. (interactive)
  1300. (org-list-indent-item-generic 1 t))
  1301. (defun org-outdent-item-tree ()
  1302. "Outdent a local list item including its children."
  1303. (interactive)
  1304. (org-list-indent-item-generic -1 nil))
  1305. (defun org-indent-item-tree ()
  1306. "Indent a local list item including its children."
  1307. (interactive)
  1308. (org-list-indent-item-generic 1 nil))
  1309. (defvar org-tab-ind-state)
  1310. (defun org-cycle-item-indentation ()
  1311. "Cycle levels of indentation of an empty item.
  1312. The first run indent the item, if applicable. Subsequents runs
  1313. outdent it at meaningful levels in the list. When done, item is
  1314. put back at its original position with its original bullet.
  1315. Return t at each successful move.
  1316. The item must be empty."
  1317. (let ((org-adapt-indentation nil))
  1318. (when (and (or (org-at-item-description-p)
  1319. (org-at-item-checkbox-p)
  1320. (org-at-item-p))
  1321. (>= (match-end 0) (save-excursion
  1322. (org-end-of-item-or-at-child)
  1323. (skip-chars-backward " \r\t\n")
  1324. (point))))
  1325. (setq this-command 'org-cycle-item-indentation)
  1326. ;; When in the middle of the cycle, try to outdent first. If it
  1327. ;; fails, and point is still at initial position, indent. Else,
  1328. ;; go back to original position.
  1329. (if (eq last-command 'org-cycle-item-indentation)
  1330. (cond
  1331. ((ignore-errors (org-outdent-item)))
  1332. ((and (= (org-get-indentation) (car org-tab-ind-state))
  1333. (ignore-errors (org-indent-item))))
  1334. (t (back-to-indentation)
  1335. (indent-to-column (car org-tab-ind-state))
  1336. (end-of-line)
  1337. (org-list-repair (cdr org-tab-ind-state))
  1338. ;; Break cycle
  1339. (setq this-command 'identity)))
  1340. ;; If a cycle is starting, remember indentation and bullet,
  1341. ;; then try to indent. If it fails, try to outdent.
  1342. (setq org-tab-ind-state
  1343. (cons (org-get-indentation) (org-get-bullet)))
  1344. (cond
  1345. ((ignore-errors (org-indent-item)))
  1346. ((ignore-errors (org-outdent-item)))
  1347. (t (error "Cannot move item"))))
  1348. t)))
  1349. ;;; Bullets
  1350. (defun org-get-bullet ()
  1351. "Return the bullet of the item at point.
  1352. Assume cursor is at an item."
  1353. (and (looking-at "[ \t]*\\(\\S-+\\)") (match-string 1)))
  1354. (defun org-list-bullet-string (bullet)
  1355. "Return BULLET with the correct number of whitespaces.
  1356. It determines the number of whitespaces to append by looking at
  1357. `org-list-two-spaces-after-bullet-regexp'."
  1358. (save-match-data
  1359. (string-match "\\S-+\\([ \t]*\\)" bullet)
  1360. (replace-match
  1361. (save-match-data
  1362. (concat
  1363. " "
  1364. ;; Do we need to concat another white space ?
  1365. (when (and org-list-two-spaces-after-bullet-regexp
  1366. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  1367. " ")))
  1368. nil nil bullet 1)))
  1369. (defun org-list-inc-bullet-maybe (bullet)
  1370. "Increment BULLET if applicable."
  1371. (if (string-match "[0-9]+" bullet)
  1372. (replace-match
  1373. (number-to-string (1+ (string-to-number (match-string 0 bullet))))
  1374. nil nil bullet)
  1375. bullet))
  1376. (defun org-list-repair (&optional force-bullet)
  1377. "Make sure all items are correctly indented, with the right bullet.
  1378. This function scans the list at point, along with any sublist.
  1379. If FORCE-BULLET is a string, ensure all items in list share this
  1380. bullet, or a logical successor in the case of an ordered list.
  1381. Item's body is not indented, only shifted with the bullet."
  1382. (interactive)
  1383. (unless (org-at-item-p) (error "This is not a list"))
  1384. (let* ((struct (org-list-struct (point-at-bol) (point-at-eol)))
  1385. (origins (org-list-struct-origins struct))
  1386. fixed-struct)
  1387. (if (stringp force-bullet)
  1388. (let ((begin (nth 1 struct)))
  1389. (setcdr begin (list (nth 1 begin)
  1390. (org-list-bullet-string force-bullet)
  1391. (nth 3 begin)))
  1392. (setq fixed-struct
  1393. (cons begin (org-list-struct-fix-struct struct origins))))
  1394. (setq fixed-struct (org-list-struct-fix-struct struct origins)))
  1395. (org-list-struct-apply-struct fixed-struct)))
  1396. (defun org-cycle-list-bullet (&optional which)
  1397. "Cycle through the different itemize/enumerate bullets.
  1398. This cycle the entire list level through the sequence:
  1399. `-' -> `+' -> `*' -> `1.' -> `1)'
  1400. If WHICH is a valid string, use that as the new bullet. If WHICH
  1401. is an integer, 0 means `-', 1 means `+' etc. If WHICH is
  1402. 'previous, cycle backwards."
  1403. (interactive "P")
  1404. (org-preserve-lc
  1405. (let* ((bullet (progn (org-beginning-of-item-list)
  1406. (org-get-bullet)))
  1407. (current (cond
  1408. ((string-match "\\." bullet) "1.")
  1409. ((string-match ")" bullet) "1)")
  1410. (t bullet)))
  1411. (bullet-rule-p (cdr (assq 'bullet org-list-automatic-rules)))
  1412. (bullet-list (append '("-" "+" )
  1413. ;; *-bullets are not allowed at column 0
  1414. (unless (and bullet-rule-p
  1415. (looking-at "\\S-")) '("*"))
  1416. ;; Description items cannot be numbered
  1417. (unless (and bullet-rule-p
  1418. (or (eq org-plain-list-ordered-item-terminator ?.)
  1419. (org-at-item-description-p))) '("1)"))
  1420. (unless (and bullet-rule-p
  1421. (or (eq org-plain-list-ordered-item-terminator ?\))
  1422. (org-at-item-description-p))) '("1."))))
  1423. (len (length bullet-list))
  1424. (item-index (- len (length (member current bullet-list))))
  1425. (get-value (lambda (index) (nth (mod index len) bullet-list)))
  1426. (new (cond
  1427. ((member which bullet-list) which)
  1428. ((numberp which) (funcall get-value which))
  1429. ((eq 'previous which) (funcall get-value (1- item-index)))
  1430. (t (funcall get-value (1+ item-index))))))
  1431. (org-list-repair new))))
  1432. ;;; Checkboxes
  1433. (defun org-toggle-checkbox (&optional toggle-presence)
  1434. "Toggle the checkbox in the current line.
  1435. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes. With
  1436. double prefix, set checkbox to [-].
  1437. When there is an active region, toggle status or presence of the
  1438. checkbox in the first line, and make every item in the region
  1439. have the same status or presence, respectively.
  1440. If the cursor is in a headline, apply this to all checkbox items
  1441. in the text below the heading, taking as reference the first item
  1442. in subtree."
  1443. (interactive "P")
  1444. ;; Bounds is a list of type (beg end single-p) where single-p is t
  1445. ;; when `org-toggle-checkbox' is applied to a single item. Only
  1446. ;; toggles on single items will return errors.
  1447. (let* ((bounds
  1448. (cond
  1449. ((org-region-active-p)
  1450. (list (region-beginning) (region-end) nil))
  1451. ((org-on-heading-p)
  1452. ;; In this case, reference line is the first item in subtree
  1453. (let ((limit (save-excursion (outline-next-heading) (point))))
  1454. (save-excursion
  1455. (org-search-forward-unenclosed
  1456. org-item-beginning-re limit 'move)
  1457. (list (point) limit nil))))
  1458. ((org-at-item-p)
  1459. (list (point-at-bol) (point-at-eol) t))
  1460. (t (error "Not at an item or heading, and no active region"))))
  1461. ;; marker is needed because deleting checkboxes will change END
  1462. (end (copy-marker (nth 1 bounds)))
  1463. (single-p (nth 2 bounds))
  1464. (ref-presence (save-excursion
  1465. (goto-char (car bounds))
  1466. (org-at-item-checkbox-p)))
  1467. (ref-status (equal (match-string 1) "[X]"))
  1468. (act-on-item
  1469. (lambda (ref-pres ref-stat)
  1470. (if (equal toggle-presence '(4))
  1471. (cond
  1472. ((and ref-pres (org-at-item-checkbox-p))
  1473. (replace-match ""))
  1474. ((and (not ref-pres)
  1475. (not (org-at-item-checkbox-p))
  1476. (org-at-item-p))
  1477. (goto-char (match-end 0))
  1478. ;; Ignore counter, if any
  1479. (when (looking-at "\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?")
  1480. (goto-char (match-end 0)))
  1481. (let ((desc-p (and (org-at-item-description-p)
  1482. (cdr (assq 'checkbox org-list-automatic-rules)))))
  1483. (cond
  1484. ((and single-p desc-p)
  1485. (error "Cannot add a checkbox in a description list"))
  1486. ((not desc-p) (insert "[ ] "))))))
  1487. (let ((blocked (org-checkbox-blocked-p)))
  1488. (cond
  1489. ((and blocked single-p)
  1490. (error "Checkbox blocked because of unchecked box in line %d" blocked))
  1491. (blocked nil)
  1492. ((org-at-item-checkbox-p)
  1493. (replace-match
  1494. (cond ((equal toggle-presence '(16)) "[-]")
  1495. (ref-stat "[ ]")
  1496. (t "[X]"))
  1497. t t nil 1))))))))
  1498. (save-excursion
  1499. (beginning-of-line)
  1500. (while (< (point) end)
  1501. (funcall act-on-item ref-presence ref-status)
  1502. (org-search-forward-unenclosed org-item-beginning-re end 'move)))
  1503. (org-update-checkbox-count-maybe)))
  1504. (defun org-reset-checkbox-state-subtree ()
  1505. "Reset all checkboxes in an entry subtree."
  1506. (interactive "*")
  1507. (save-restriction
  1508. (save-excursion
  1509. (org-narrow-to-subtree)
  1510. (org-show-subtree)
  1511. (goto-char (point-min))
  1512. (let ((end (point-max)))
  1513. (while (< (point) end)
  1514. (when (org-at-item-checkbox-p)
  1515. (replace-match "[ ]" t t nil 1))
  1516. (beginning-of-line 2))))
  1517. (org-update-checkbox-count-maybe)))
  1518. (defvar org-checkbox-statistics-hook nil
  1519. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  1520. This hook runs even if checkbox rule in
  1521. `org-list-automatic-rules' does not apply, so it can be used to
  1522. implement alternative ways of collecting statistics
  1523. information.")
  1524. (defun org-update-checkbox-count-maybe ()
  1525. "Update checkbox statistics unless turned off by user."
  1526. (when (cdr (assq 'checkbox org-list-automatic-rules))
  1527. (org-update-checkbox-count))
  1528. (run-hooks 'org-checkbox-statistics-hook))
  1529. (defun org-update-checkbox-count (&optional all)
  1530. "Update the checkbox statistics in the current section.
  1531. This will find all statistic cookies like [57%] and [6/12] and update them
  1532. with the current numbers. With optional prefix argument ALL, do this for
  1533. the whole buffer."
  1534. (interactive "P")
  1535. (save-excursion
  1536. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  1537. (beg (condition-case nil
  1538. (progn (org-back-to-heading) (point))
  1539. (error (point-min))))
  1540. (end (move-marker (make-marker)
  1541. (progn (outline-next-heading) (point))))
  1542. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  1543. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  1544. (re-find (concat re "\\|" re-box))
  1545. beg-cookie end-cookie is-percent c-on c-off lim new
  1546. eline curr-ind next-ind continue-from startsearch
  1547. (recursive
  1548. (or (not org-hierarchical-checkbox-statistics)
  1549. (string-match "\\<recursive\\>"
  1550. (or (ignore-errors
  1551. (org-entry-get nil "COOKIE_DATA"))
  1552. ""))))
  1553. (cstat 0))
  1554. (when all
  1555. (goto-char (point-min))
  1556. (outline-next-heading)
  1557. (setq beg (point) end (point-max)))
  1558. (goto-char end)
  1559. ;; find each statistics cookie
  1560. (while (and (org-search-backward-unenclosed re-find beg t)
  1561. (not (save-match-data
  1562. (and (org-on-heading-p)
  1563. (string-match "\\<todo\\>"
  1564. (downcase
  1565. (or (org-entry-get
  1566. nil "COOKIE_DATA")
  1567. "")))))))
  1568. (setq beg-cookie (match-beginning 1)
  1569. end-cookie (match-end 1)
  1570. cstat (+ cstat (if end-cookie 1 0))
  1571. startsearch (point-at-eol)
  1572. continue-from (match-beginning 0)
  1573. is-percent (match-beginning 2)
  1574. lim (cond
  1575. ((org-on-heading-p) (outline-next-heading) (point))
  1576. ((org-at-item-p) (org-end-of-item) (point))
  1577. (t nil))
  1578. c-on 0
  1579. c-off 0)
  1580. (when lim
  1581. ;; find first checkbox for this cookie and gather
  1582. ;; statistics from all that are at this indentation level
  1583. (goto-char startsearch)
  1584. (if (org-search-forward-unenclosed re-box lim t)
  1585. (progn
  1586. (org-beginning-of-item)
  1587. (setq curr-ind (org-get-indentation))
  1588. (setq next-ind curr-ind)
  1589. (while (and (bolp) (org-at-item-p)
  1590. (if recursive
  1591. (<= curr-ind next-ind)
  1592. (= curr-ind next-ind)))
  1593. (setq eline (point-at-eol))
  1594. (if (org-search-forward-unenclosed re-box eline t)
  1595. (if (member (match-string 2) '("[ ]" "[-]"))
  1596. (setq c-off (1+ c-off))
  1597. (setq c-on (1+ c-on))))
  1598. (if (not recursive)
  1599. ;; org-get-next-item goes through list-enders
  1600. ;; with proper limit.
  1601. (goto-char (or (org-get-next-item (point) lim) lim))
  1602. (end-of-line)
  1603. (when (org-search-forward-unenclosed
  1604. org-item-beginning-re lim t)
  1605. (beginning-of-line)))
  1606. (setq next-ind (org-get-indentation)))))
  1607. (goto-char continue-from)
  1608. ;; update cookie
  1609. (when end-cookie
  1610. (setq new (if is-percent
  1611. (format "[%d%%]" (/ (* 100 c-on)
  1612. (max 1 (+ c-on c-off))))
  1613. (format "[%d/%d]" c-on (+ c-on c-off))))
  1614. (goto-char beg-cookie)
  1615. (insert new)
  1616. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1617. ;; update items checkbox if it has one
  1618. (when (org-at-item-p)
  1619. (org-beginning-of-item)
  1620. (when (and (> (+ c-on c-off) 0)
  1621. (org-search-forward-unenclosed re-box (point-at-eol) t))
  1622. (setq beg-cookie (match-beginning 2)
  1623. end-cookie (match-end 2))
  1624. (delete-region beg-cookie end-cookie)
  1625. (goto-char beg-cookie)
  1626. (cond ((= c-off 0) (insert "[X]"))
  1627. ((= c-on 0) (insert "[ ]"))
  1628. (t (insert "[-]")))
  1629. )))
  1630. (goto-char continue-from))
  1631. (when (interactive-p)
  1632. (message "Checkbox statistics updated %s (%d places)"
  1633. (if all "in entire file" "in current outline entry") cstat)))))
  1634. (defun org-get-checkbox-statistics-face ()
  1635. "Select the face for checkbox statistics.
  1636. The face will be `org-done' when all relevant boxes are checked.
  1637. Otherwise it will be `org-todo'."
  1638. (if (match-end 1)
  1639. (if (equal (match-string 1) "100%")
  1640. 'org-checkbox-statistics-done
  1641. 'org-checkbox-statistics-todo)
  1642. (if (and (> (match-end 2) (match-beginning 2))
  1643. (equal (match-string 2) (match-string 3)))
  1644. 'org-checkbox-statistics-done
  1645. 'org-checkbox-statistics-todo)))
  1646. ;;; Misc Tools
  1647. (defun org-apply-on-list (function init-value &rest args)
  1648. "Call FUNCTION for each item of a the list under point.
  1649. FUNCTION must be called with at least one argument: INIT-VALUE,
  1650. that will contain the value returned by the function at the
  1651. previous item, plus ARGS extra arguments.
  1652. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1653. will return the number of items in the current list.
  1654. Sublists of the list are skipped. Cursor is always at the
  1655. beginning of the item."
  1656. (let* ((pos (copy-marker (point)))
  1657. (end (copy-marker (org-list-bottom-point)))
  1658. (next-p (copy-marker (save-excursion (org-beginning-of-item-list))))
  1659. (value init-value))
  1660. (while (< next-p end)
  1661. (goto-char next-p)
  1662. (set-marker next-p (or (org-get-next-item (point) end) end))
  1663. (setq value (apply function value args)))
  1664. (goto-char pos)
  1665. value))
  1666. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1667. "Sort plain list items.
  1668. The cursor may be at any item of the list that should be sorted.
  1669. Sublists are not sorted. Checkboxes, if any, are ignored.
  1670. Sorting can be alphabetically, numerically, by date/time as given by
  1671. a time stamp, by a property or by priority.
  1672. Comparing entries ignores case by default. However, with an
  1673. optional argument WITH-CASE, the sorting considers case as well.
  1674. The command prompts for the sorting type unless it has been given
  1675. to the function through the SORTING-TYPE argument, which needs to
  1676. be a character, \(?n ?N ?a ?A ?t ?T ?f ?F). Here is the precise
  1677. meaning of each character:
  1678. n Numerically, by converting the beginning of the item to a number.
  1679. a Alphabetically. Only the first line of item is checked.
  1680. t By date/time, either the first active time stamp in the entry, if
  1681. any, or by the first inactive one. In a timer list, sort the timers.
  1682. Capital letters will reverse the sort order.
  1683. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1684. function to be called with point at the beginning of the record.
  1685. It must return either a string or a number that should serve as
  1686. the sorting key for that record. It will then use COMPARE-FUNC to
  1687. compare entries."
  1688. (interactive "P")
  1689. (let* ((case-func (if with-case 'identity 'downcase))
  1690. (start (org-beginning-of-item-list))
  1691. (end (save-excursion (org-end-of-item-list)))
  1692. (sorting-type
  1693. (progn
  1694. (message
  1695. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1696. (read-char-exclusive)))
  1697. (getkey-func (and (= (downcase sorting-type) ?f)
  1698. (org-icompleting-read "Sort using function: "
  1699. obarray 'fboundp t nil nil)
  1700. (intern getkey-func))))
  1701. (message "Sorting items...")
  1702. (save-restriction
  1703. (narrow-to-region start end)
  1704. (let* ((dcst (downcase sorting-type))
  1705. (case-fold-search nil)
  1706. (now (current-time))
  1707. (sort-func (cond
  1708. ((= dcst ?a) 'string<)
  1709. ((= dcst ?f) compare-func)
  1710. ((= dcst ?t) '<)
  1711. (t nil)))
  1712. (begin-record (lambda ()
  1713. (skip-chars-forward " \r\t\n")
  1714. (beginning-of-line)))
  1715. (end-record (lambda ()
  1716. (goto-char (org-end-of-item-before-blank))))
  1717. (value-to-sort
  1718. (lambda ()
  1719. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1720. (cond
  1721. ((= dcst ?n)
  1722. (string-to-number (buffer-substring (match-end 0)
  1723. (point-at-eol))))
  1724. ((= dcst ?a)
  1725. (buffer-substring (match-end 0) (point-at-eol)))
  1726. ((= dcst ?t)
  1727. (cond
  1728. ;; If it is a timer list, convert timer to seconds
  1729. ((org-at-item-timer-p)
  1730. (org-timer-hms-to-secs (match-string 1)))
  1731. ((or (org-search-forward-unenclosed org-ts-regexp
  1732. (point-at-eol) t)
  1733. (org-search-forward-unenclosed org-ts-regexp-both
  1734. (point-at-eol) t))
  1735. (org-time-string-to-seconds (match-string 0)))
  1736. (t (org-float-time now))))
  1737. ((= dcst ?f)
  1738. (if getkey-func
  1739. (let ((value (funcall getkey-func)))
  1740. (if (stringp value)
  1741. (funcall case-func value)
  1742. value))
  1743. (error "Invalid key function `%s'" getkey-func)))
  1744. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1745. (sort-subr (/= dcst sorting-type)
  1746. begin-record
  1747. end-record
  1748. value-to-sort
  1749. nil
  1750. sort-func)
  1751. (org-list-repair)
  1752. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1753. (message "Sorting items...done")))))
  1754. ;;; Send and receive lists
  1755. (defun org-list-parse-list (&optional delete)
  1756. "Parse the list at point and maybe DELETE it.
  1757. Return a list containing first level items as strings and
  1758. sublevels as a list of strings."
  1759. (let* ((start (goto-char (org-list-top-point)))
  1760. (end (org-list-bottom-point))
  1761. output itemsep ltype)
  1762. (while (org-search-forward-unenclosed org-item-beginning-re end t)
  1763. (save-excursion
  1764. (beginning-of-line)
  1765. (setq ltype (cond ((looking-at-p "^[ \t]*[0-9]") 'ordered)
  1766. ((org-at-item-description-p) 'descriptive)
  1767. (t 'unordered))))
  1768. (let* ((indent1 (org-get-indentation))
  1769. (nextitem (or (org-get-next-item (point) end) end))
  1770. (item (org-trim (buffer-substring (point)
  1771. (org-end-of-item-or-at-child))))
  1772. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1773. (item (if (string-match
  1774. "^\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\[\\([xX ]\\)\\]"
  1775. item)
  1776. (replace-match (if (equal (match-string 1 item) " ")
  1777. "CBOFF"
  1778. "CBON")
  1779. t nil item 1)
  1780. item)))
  1781. (push item output)
  1782. (when (> nextindent indent1)
  1783. (save-restriction
  1784. (narrow-to-region (point) nextitem)
  1785. (push (org-list-parse-list) output)))))
  1786. (when delete
  1787. (delete-region start end)
  1788. (save-match-data
  1789. (when (and (not (eq org-list-ending-method 'indent))
  1790. (looking-at (org-list-end-re)))
  1791. (replace-match "\n"))))
  1792. (setq output (nreverse output))
  1793. (push ltype output)))
  1794. (defun org-list-make-subtree ()
  1795. "Convert the plain list at point into a subtree."
  1796. (interactive)
  1797. (if (not (org-in-item-p))
  1798. (error "Not in a list")
  1799. (goto-char (org-list-top-point))
  1800. (let ((list (org-list-parse-list t)) nstars)
  1801. (save-excursion
  1802. (if (ignore-errors
  1803. (org-back-to-heading))
  1804. (progn (org-search-forward-unenclosed
  1805. org-complex-heading-regexp nil t)
  1806. (setq nstars (length (match-string 1))))
  1807. (setq nstars 0)))
  1808. (org-list-make-subtrees list (1+ nstars)))))
  1809. (defun org-list-make-subtrees (list level)
  1810. "Convert LIST into subtrees starting at LEVEL."
  1811. (if (symbolp (car list))
  1812. (org-list-make-subtrees (cdr list) level)
  1813. (mapcar (lambda (item)
  1814. (if (stringp item)
  1815. (insert (make-string
  1816. (if org-odd-levels-only
  1817. (1- (* 2 level)) level) ?*) " " item "\n")
  1818. (org-list-make-subtrees item (1+ level))))
  1819. list)))
  1820. (defun org-list-insert-radio-list ()
  1821. "Insert a radio list template appropriate for this major mode."
  1822. (interactive)
  1823. (let* ((e (assq major-mode org-list-radio-list-templates))
  1824. (txt (nth 1 e))
  1825. name pos)
  1826. (unless e (error "No radio list setup defined for %s" major-mode))
  1827. (setq name (read-string "List name: "))
  1828. (while (string-match "%n" txt)
  1829. (setq txt (replace-match name t t txt)))
  1830. (or (bolp) (insert "\n"))
  1831. (setq pos (point))
  1832. (insert txt)
  1833. (goto-char pos)))
  1834. (defun org-list-send-list (&optional maybe)
  1835. "Send a transformed version of this list to the receiver position.
  1836. With argument MAYBE, fail quietly if no transformation is defined for
  1837. this list."
  1838. (interactive)
  1839. (catch 'exit
  1840. (unless (org-at-item-p) (error "Not at a list"))
  1841. (save-excursion
  1842. (re-search-backward "#\\+ORGLST" nil t)
  1843. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1844. (if maybe
  1845. (throw 'exit nil)
  1846. (error "Don't know how to transform this list"))))
  1847. (let* ((name (match-string 1))
  1848. (transform (intern (match-string 2)))
  1849. (bottom-point
  1850. (save-excursion
  1851. (re-search-forward
  1852. "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" nil t)
  1853. (match-beginning 0)))
  1854. (top-point
  1855. (progn
  1856. (re-search-backward "#\\+ORGLST" nil t)
  1857. (re-search-forward org-item-beginning-re bottom-point t)
  1858. (match-beginning 0)))
  1859. (list (save-restriction
  1860. (narrow-to-region top-point bottom-point)
  1861. (org-list-parse-list)))
  1862. beg txt)
  1863. (unless (fboundp transform)
  1864. (error "No such transformation function %s" transform))
  1865. (let ((txt (funcall transform list)))
  1866. ;; Find the insertion place
  1867. (save-excursion
  1868. (goto-char (point-min))
  1869. (unless (re-search-forward
  1870. (concat "BEGIN RECEIVE ORGLST +"
  1871. name
  1872. "\\([ \t]\\|$\\)") nil t)
  1873. (error "Don't know where to insert translated list"))
  1874. (goto-char (match-beginning 0))
  1875. (beginning-of-line 2)
  1876. (setq beg (point))
  1877. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1878. (error "Cannot find end of insertion region"))
  1879. (delete-region beg (point-at-bol))
  1880. (goto-char beg)
  1881. (insert txt "\n")))
  1882. (message "List converted and installed at receiver location"))))
  1883. (defun org-list-to-generic (list params)
  1884. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1885. Valid parameters PARAMS are
  1886. :ustart String to start an unordered list
  1887. :uend String to end an unordered list
  1888. :ostart String to start an ordered list
  1889. :oend String to end an ordered list
  1890. :dstart String to start a descriptive list
  1891. :dend String to end a descriptive list
  1892. :dtstart String to start a descriptive term
  1893. :dtend String to end a descriptive term
  1894. :ddstart String to start a description
  1895. :ddend String to end a description
  1896. :splice When set to t, return only list body lines, don't wrap
  1897. them into :[u/o]start and :[u/o]end. Default is nil.
  1898. :istart String to start a list item
  1899. :iend String to end a list item
  1900. :isep String to separate items
  1901. :lsep String to separate sublists
  1902. :cboff String to insert for an unchecked checkbox
  1903. :cbon String to insert for a checked checkbox"
  1904. (interactive)
  1905. (let* ((p params) sublist
  1906. (splicep (plist-get p :splice))
  1907. (ostart (plist-get p :ostart))
  1908. (oend (plist-get p :oend))
  1909. (ustart (plist-get p :ustart))
  1910. (uend (plist-get p :uend))
  1911. (dstart (plist-get p :dstart))
  1912. (dend (plist-get p :dend))
  1913. (dtstart (plist-get p :dtstart))
  1914. (dtend (plist-get p :dtend))
  1915. (ddstart (plist-get p :ddstart))
  1916. (ddend (plist-get p :ddend))
  1917. (istart (plist-get p :istart))
  1918. (iend (plist-get p :iend))
  1919. (isep (plist-get p :isep))
  1920. (lsep (plist-get p :lsep))
  1921. (cbon (plist-get p :cbon))
  1922. (cboff (plist-get p :cboff)))
  1923. (let ((wrapper
  1924. (cond ((eq (car list) 'ordered)
  1925. (concat ostart "\n%s" oend "\n"))
  1926. ((eq (car list) 'unordered)
  1927. (concat ustart "\n%s" uend "\n"))
  1928. ((eq (car list) 'descriptive)
  1929. (concat dstart "\n%s" dend "\n"))))
  1930. rtn term defstart defend)
  1931. (while (setq sublist (pop list))
  1932. (cond ((symbolp sublist) nil)
  1933. ((stringp sublist)
  1934. (when (string-match "^\\(\\S-+\\)[ \t]+::" sublist)
  1935. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1936. (match-string 1 sublist))))
  1937. (setq sublist (concat ddstart
  1938. (org-trim (substring sublist
  1939. (match-end 0)))
  1940. ddend)))
  1941. (if (string-match "\\[CBON\\]" sublist)
  1942. (setq sublist (replace-match cbon t t sublist)))
  1943. (if (string-match "\\[CBOFF\\]" sublist)
  1944. (setq sublist (replace-match cboff t t sublist)))
  1945. (if (string-match "\\[-\\]" sublist)
  1946. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1947. (setq rtn (concat rtn istart term sublist iend isep)))
  1948. (t (setq rtn (concat rtn ;; previous list
  1949. lsep ;; list separator
  1950. (org-list-to-generic sublist p)
  1951. lsep ;; list separator
  1952. )))))
  1953. (format wrapper rtn))))
  1954. (defun org-list-to-latex (list &optional params)
  1955. "Convert LIST into a LaTeX list.
  1956. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1957. with overruling parameters for `org-list-to-generic'."
  1958. (org-list-to-generic
  1959. list
  1960. (org-combine-plists
  1961. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1962. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1963. :dstart "\\begin{description}" :dend "\\end{description}"
  1964. :dtstart "[" :dtend "]"
  1965. :ddstart "" :ddend ""
  1966. :istart "\\item " :iend ""
  1967. :isep "\n" :lsep "\n"
  1968. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1969. params)))
  1970. (defun org-list-to-html (list &optional params)
  1971. "Convert LIST into a HTML list.
  1972. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1973. with overruling parameters for `org-list-to-generic'."
  1974. (org-list-to-generic
  1975. list
  1976. (org-combine-plists
  1977. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1978. :ustart "<ul>" :uend "</ul>"
  1979. :dstart "<dl>" :dend "</dl>"
  1980. :dtstart "<dt>" :dtend "</dt>"
  1981. :ddstart "<dd>" :ddend "</dd>"
  1982. :istart "<li>" :iend "</li>"
  1983. :isep "\n" :lsep "\n"
  1984. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1985. params)))
  1986. (defun org-list-to-texinfo (list &optional params)
  1987. "Convert LIST into a Texinfo list.
  1988. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1989. with overruling parameters for `org-list-to-generic'."
  1990. (org-list-to-generic
  1991. list
  1992. (org-combine-plists
  1993. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1994. :ustart "@enumerate" :uend "@end enumerate"
  1995. :dstart "@table" :dend "@end table"
  1996. :dtstart "@item " :dtend "\n"
  1997. :ddstart "" :ddend ""
  1998. :istart "@item\n" :iend ""
  1999. :isep "\n" :lsep "\n"
  2000. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  2001. params)))
  2002. (provide 'org-list)
  2003. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  2004. ;;; org-list.el ends here