org-list.el 95 KB

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