org-list.el 75 KB

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