org-list.el 98 KB

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