org-list.el 79 KB

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