org-list.el 98 KB

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