org-list.el 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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-skip-whitespace "org" ())
  43. (declare-function org-trim "org" (s))
  44. (declare-function org-get-indentation "org" (&optional line))
  45. (declare-function org-timer-item "org-timer" (&optional arg))
  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 he list."
  128. :group 'org-plain-lists
  129. :type '(choice
  130. (const :tag "never" nil)
  131. (regexp)))
  132. (defcustom org-empty-line-terminates-plain-lists nil
  133. "Non-nil means an empty line ends all plain list levels.
  134. Otherwise it will take two blank lines to end them."
  135. :group 'org-plain-lists
  136. :type 'boolean)
  137. (defcustom org-auto-renumber-ordered-lists t
  138. "Non-nil means automatically renumber ordered plain lists.
  139. Renumbering happens when the sequence have been changed with
  140. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  141. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  142. :group 'org-plain-lists
  143. :type 'boolean)
  144. (defcustom org-provide-checkbox-statistics t
  145. "Non-nil means update checkbox statistics after insert and toggle.
  146. When this is set, checkbox statistics is updated each time you
  147. either insert a new checkbox with \\[org-insert-todo-heading] or
  148. toggle a checkbox with \\[org-ctrl-c-ctrl-c]."
  149. :group 'org-plain-lists
  150. :type 'boolean)
  151. (defcustom org-hierarchical-checkbox-statistics t
  152. "Non-nil means checkbox statistics counts only the state of direct children.
  153. When nil, all boxes below the cookie are counted.
  154. This can be set to nil on a per-node basis using a COOKIE_DATA property
  155. with the word \"recursive\" in the value."
  156. :group 'org-plain-lists
  157. :type 'boolean)
  158. (defcustom org-description-max-indent 20
  159. "Maximum indentation for the second line of a description list.
  160. When the indentation would be larger than this, it will become
  161. 5 characters instead."
  162. :group 'org-plain-lists
  163. :type 'integer)
  164. (defcustom org-list-radio-list-templates
  165. '((latex-mode "% BEGIN RECEIVE ORGLST %n
  166. % END RECEIVE ORGLST %n
  167. \\begin{comment}
  168. #+ORGLST: SEND %n org-list-to-latex
  169. -
  170. \\end{comment}\n")
  171. (texinfo-mode "@c BEGIN RECEIVE ORGLST %n
  172. @c END RECEIVE ORGLST %n
  173. @ignore
  174. #+ORGLST: SEND %n org-list-to-texinfo
  175. -
  176. @end ignore\n")
  177. (html-mode "<!-- BEGIN RECEIVE ORGLST %n -->
  178. <!-- END RECEIVE ORGLST %n -->
  179. <!--
  180. #+ORGLST: SEND %n org-list-to-html
  181. -
  182. -->\n"))
  183. "Templates for radio lists in different major modes.
  184. All occurrences of %n in a template will be replaced with the name of the
  185. list, obtained by prompting the user."
  186. :group 'org-plain-lists
  187. :type '(repeat
  188. (list (symbol :tag "Major mode")
  189. (string :tag "Format"))))
  190. ;;; Internal functions
  191. (defun org-list-end-re ()
  192. "Return the regex corresponding to the end of a list.
  193. It depends on `org-empty-line-terminates-plain-lists'."
  194. (if org-empty-line-terminates-plain-lists
  195. "^\\([ \t]*\n\\)+"
  196. "^[ \t]*\n\\([ \t]*\n\\)+"))
  197. (defun org-item-re (&optional general)
  198. "Return the correct regular expression for plain lists.
  199. If GENERAL is non-nil, return the general regexp independent of the value
  200. of `org-plain-list-ordered-item-terminator'."
  201. (cond
  202. ((or general (eq org-plain-list-ordered-item-terminator t))
  203. "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  204. ((= org-plain-list-ordered-item-terminator ?.)
  205. "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  206. ((= org-plain-list-ordered-item-terminator ?\))
  207. "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  208. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'"))))
  209. (defun org-list-terminator-between (min max &optional firstp)
  210. "Find the position of a list ender between MIN and MAX, or nil.
  211. This function looks for `org-list-end-re' not matching a block.
  212. If FIRSTP in non-nil, return the point at the beginning of the
  213. nearest valid terminator from min. Otherwise, return the point at
  214. the end of the nearest terminator from max."
  215. (save-excursion
  216. (let* ((start (if firstp min max))
  217. (end (if firstp max min))
  218. (search-fun (if firstp
  219. #'org-search-forward-unenclosed
  220. #'org-search-backward-unenclosed))
  221. (list-end-p (progn
  222. (goto-char start)
  223. (funcall search-fun (org-list-end-re) end))))
  224. ;; Is there a valid list terminator somewhere ?
  225. (and list-end-p
  226. ;; we want to be on the first line of the list ender
  227. (match-beginning 0)))))
  228. (defun org-search-backward-unenclosed (regexp &optional bound noerror count)
  229. "Like `re-search-backward' but don't stop inside blocks or throw errors.
  230. Optional fourth argument COUNT searches for that many occurrences,
  231. valid or not, then makes sure the last one is valid."
  232. (let ((origin (point)))
  233. (cond
  234. ;; nothing found: return nil
  235. ((not (re-search-backward regexp bound (or noerror t) count)) nil)
  236. ;; match is not enclosed: return point.
  237. ((not (save-match-data
  238. (or (org-in-regexps-block-p "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  239. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  240. (org-in-regexps-block-p "^[ \t]*\\\\begin{\\([a-zA-Z0-9_]+\\)}"
  241. '(concat "^[ \t]*\\\\end{" (match-string 1) "}")))))
  242. (point))
  243. (t
  244. ;; else: we start again, searching one more occurrence away.
  245. (goto-char origin)
  246. (org-search-backward-unenclosed regexp bound noerror (1+ (or count 1)))))))
  247. (defun org-search-forward-unenclosed (regexp &optional bound noerror count)
  248. "Like `re-search-forward' but don't stop inside blocks or throw errors.
  249. Optional fourth argument COUNT searches for that many occurrences,
  250. valid or not, then makes sure the last one is valid."
  251. (let ((origin (point)))
  252. (cond
  253. ;; nothing found: return nil
  254. ((not (re-search-forward regexp bound (or noerror t) count)) nil)
  255. ;; match is not enclosed: return point.
  256. ((not (save-match-data
  257. (or (org-in-regexps-block-p "^[ \t]*#\\+\\(begin\\|BEGIN\\)_\\([a-zA-Z0-9_]+\\)"
  258. '(concat "^[ \t]*#\\+\\(end\\|END\\)_" (match-string 2)))
  259. (org-in-regexps-block-p "^[ \t]*\\\\begin{\\([a-zA-Z0-9_]+\\)}"
  260. '(concat "^[ \t]*\\\\end{" (match-string 1) "}")))))
  261. (point))
  262. ;; else: we start again, searching one more occurrence away.
  263. (t
  264. (goto-char origin)
  265. (org-search-forward-unenclosed regexp bound noerror (1+ (or count 1)))))))
  266. (defun org-get-item-same-level-internal (search-fun pos limit pre-move)
  267. "Return point at the beginning of next item at the same level.
  268. Search items using function SEARCH-FUN, from POS to LIMIT. It
  269. uses PRE-MOVE before searches. Return nil if no item was found.
  270. Internal use only. Prefer `org-get-next-item' and
  271. `org-get-previous-item' for cleaner code."
  272. (save-excursion
  273. (when pos (goto-char pos))
  274. (let* ((begin (point))
  275. (ind (progn
  276. (org-beginning-of-item)
  277. (org-get-indentation)))
  278. (start (point-at-bol)))
  279. ;; we don't want to match the current line.
  280. (funcall pre-move)
  281. ;; we skip any sublist on the way
  282. (while (and (funcall search-fun (org-item-re) limit)
  283. (> (org-get-indentation) ind))
  284. (funcall pre-move))
  285. (when (and (/= (point-at-bol) start) ; Have we moved ?
  286. (= (org-get-indentation) ind))
  287. (point-at-bol)))))
  288. ;;; Predicates
  289. (defun org-in-item-p ()
  290. "Is the cursor inside a plain list ?"
  291. (save-restriction
  292. (save-excursion
  293. (widen)
  294. ;; we move to eol so that the current line can be matched by
  295. ;; `org-item-re'.
  296. (let* ((limit (or (save-excursion (outline-previous-heading)) (point-min)))
  297. (actual-pos (goto-char (point-at-eol)))
  298. (last-item-start (save-excursion
  299. (org-search-backward-unenclosed (org-item-re) limit)))
  300. (list-ender (org-list-terminator-between last-item-start actual-pos)))
  301. ;; We are in a list when we are on an item line or we can find
  302. ;; an item before and there is no valid list ender between us
  303. ;; and the item found.
  304. (and last-item-start
  305. (not list-ender))))))
  306. (defun org-first-list-item-p ()
  307. "Is this heading the first item in a plain list?"
  308. (unless (org-at-item-p)
  309. (error "Not at a plain list item"))
  310. (save-excursion
  311. (= (save-excursion (org-beginning-of-item)) (org-beginning-of-item-list))))
  312. (defun org-at-item-p ()
  313. "Is point in a line starting a hand-formatted item?"
  314. (save-excursion
  315. (goto-char (point-at-bol))
  316. (looking-at (org-item-re))))
  317. (defun org-at-item-bullet-p ()
  318. "Is point at the bullet of a plain list item?"
  319. (and (org-at-item-p)
  320. (not (member (char-after) '(?\ ?\t)))
  321. (< (point) (match-end 0))))
  322. (defun org-at-item-checkbox-p ()
  323. "Is point at a line starting a plain-list item with a checklet?"
  324. (and (org-at-item-p)
  325. (save-excursion
  326. (goto-char (match-end 0))
  327. (skip-chars-forward " \t")
  328. (looking-at "\\[[- X]\\]"))))
  329. (defun org-checkbox-blocked-p ()
  330. "Is the current checkbox blocked from for being checked now?
  331. A checkbox is blocked if all of the following conditions are fulfilled:
  332. 1. The checkbox is not checked already.
  333. 2. The current entry has the ORDERED property set.
  334. 3. There is an unchecked checkbox in this entry before the current line."
  335. (catch 'exit
  336. (save-match-data
  337. (save-excursion
  338. (unless (org-at-item-checkbox-p) (throw 'exit nil))
  339. (when (equal (match-string 0) "[X]")
  340. ;; the box is already checked!
  341. (throw 'exit nil))
  342. (let ((end (point-at-bol)))
  343. (condition-case nil (org-back-to-heading t)
  344. (error (throw 'exit nil)))
  345. (unless (org-entry-get nil "ORDERED") (throw 'exit nil))
  346. (if (re-search-forward "^[ \t]*[-+*0-9.)] \\[[- ]\\]" end t)
  347. (org-current-line)
  348. nil))))))
  349. ;;; Navigate
  350. (defun org-list-top-point ()
  351. "Return point at the top level item in a list, or nil if not in a list."
  352. (save-excursion
  353. (and (org-in-item-p)
  354. (let ((pos (point-at-eol))
  355. (bound (or (outline-previous-heading) (point-min))))
  356. ;; Is there some list above this one ? If so, go to its ending.
  357. ;; Otherwise, go back to the heading above or bob.
  358. (goto-char (or (org-list-terminator-between bound pos) bound))
  359. ;; From there, search down our list.
  360. (org-search-forward-unenclosed (org-item-re) pos)
  361. (point-at-bol)))))
  362. (defun org-list-bottom-point ()
  363. "Return point just before list ending or nil if not in a list."
  364. (save-excursion
  365. (and (org-in-item-p)
  366. (let ((pos (org-beginning-of-item))
  367. (bound (or (and (outline-next-heading)
  368. (skip-chars-backward " \t\r\n")
  369. (1+ (point-at-eol)))
  370. (point-max))))
  371. ;; The list ending is either first point matching
  372. ;; org-list-end-re, point at first white-line before next
  373. ;; heading, or eob.
  374. (or (org-list-terminator-between pos bound t) bound)))))
  375. (defun org-beginning-of-item ()
  376. "Go to the beginning of the current hand-formatted item.
  377. If the cursor is not in an item, throw an error. Return point."
  378. (interactive)
  379. (if (org-in-item-p)
  380. (if (org-at-item-p)
  381. (progn (beginning-of-line 1)
  382. (point))
  383. (org-search-backward-unenclosed (org-item-re))
  384. (goto-char (point-at-bol)))
  385. (error "Not in an item")))
  386. (defun org-end-of-item ()
  387. "Go to the end of the current hand-formatted item.
  388. If the cursor is not in an item, throw an error."
  389. (interactive)
  390. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  391. (cond ((not (org-in-item-p))
  392. (error "Not in an item"))
  393. (next-p
  394. (goto-char next-p))
  395. (t
  396. (org-end-of-item-list)))))
  397. (defun org-end-of-item-text-before-children ()
  398. "Move to the end of the item text, stops before the first child if any.
  399. Assumes that the cursor is in the first line of an item."
  400. (let ((limit (org-list-bottom-point)))
  401. (end-of-line)
  402. (goto-char
  403. (if (org-search-forward-unenclosed (org-item-re) limit)
  404. (point-at-bol)
  405. limit))))
  406. (defun org-end-of-item-before-blank ()
  407. "Return point at end of item, before any blank line.
  408. Point returned is at eol."
  409. (save-excursion
  410. (org-end-of-item)
  411. (skip-chars-backward " \r\t\n")
  412. (point-at-eol)))
  413. (defun org-get-next-item (pos limit)
  414. "Get the point of the next item at the same level as POS.
  415. Stop searching at LIMIT. Return nil if no item is found. This
  416. function does not move point."
  417. (org-get-item-same-level-internal
  418. #'org-search-forward-unenclosed
  419. pos
  420. limit
  421. #'end-of-line))
  422. (defun org-get-previous-item (pos limit)
  423. "Get the point of the previous item at the same level as POS.
  424. Stop searching at LIMIT. Return nil if no item is found. This
  425. function does not move point."
  426. (org-get-item-same-level-internal
  427. #'org-search-backward-unenclosed
  428. pos
  429. limit
  430. #'beginning-of-line))
  431. (defun org-next-item ()
  432. "Move to the beginning of the next item.
  433. Item is at the same level in the current plain list. Error if not
  434. in a plain list, or if this is the last item in the list."
  435. (interactive)
  436. (let ((next-p (org-get-next-item (point) (org-list-bottom-point))))
  437. (if next-p
  438. (goto-char next-p)
  439. (error "On last item"))))
  440. (defun org-previous-item ()
  441. "Move to the beginning of the previous item.
  442. Item is at the same level in the current plain list. Error if not
  443. in a plain list, or if this is the first item in the list."
  444. (interactive)
  445. (let ((prev-p (org-get-previous-item (point) (org-list-top-point))))
  446. (if prev-p
  447. (goto-char prev-p)
  448. (error "On first item"))))
  449. (defun org-beginning-of-item-list ()
  450. "Go to the beginning item of the current list or sublist.
  451. Return point."
  452. (interactive)
  453. (let ((limit (org-list-top-point))
  454. (move-up (lambda (pos bound)
  455. ;; prev-p: any item of same level before ?
  456. (let ((prev-p (org-get-previous-item pos bound)))
  457. ;; recurse until no more item of the same level
  458. ;; can be found.
  459. (if prev-p
  460. (funcall move-up prev-p bound)
  461. pos)))))
  462. ;; Go to the last item found and at bol in case we didn't move
  463. (goto-char (funcall move-up (point) limit))
  464. (goto-char (point-at-bol))))
  465. (defun org-end-of-item-list ()
  466. "Go to the end of the current list or sublist.
  467. Return point."
  468. (interactive)
  469. (org-beginning-of-item)
  470. (let ((limit (org-list-bottom-point))
  471. (ind (org-get-indentation))
  472. (get-last-item (lambda (pos bound)
  473. ;; next-p: any item of same level after ?
  474. (let ((next-p (org-get-next-item pos bound)))
  475. ;; recurse until no more item of the same level
  476. ;; can be found.
  477. (if next-p
  478. (funcall get-last-item next-p bound)
  479. pos)))))
  480. ;; Move to the last item of every list or sublist encountered, and
  481. ;; down to bol of a higher-level item, or limit.
  482. (while (and (/= (point) limit)
  483. (>= (org-get-indentation) ind))
  484. (goto-char (funcall get-last-item (point) limit))
  485. (end-of-line)
  486. (when (org-search-forward-unenclosed (org-item-re) limit 'move)
  487. (beginning-of-line)))
  488. (point)))
  489. ;;; Manipulate
  490. (defun org-list-exchange-items (beg-A beg-B)
  491. "Swap item starting at BEG-A with item starting at BEG-B.
  492. Blank lines at the end of items are left in place. Assumes
  493. BEG-A is lesser than BEG-B."
  494. (save-excursion
  495. (let* ((end-of-item-no-blank (lambda (pos)
  496. (goto-char pos)
  497. (goto-char (org-end-of-item-before-blank))))
  498. (end-A-no-blank (funcall end-of-item-no-blank beg-A))
  499. (end-B-no-blank (funcall end-of-item-no-blank beg-B))
  500. (body-A (buffer-substring beg-A end-A-no-blank))
  501. (body-B (buffer-substring beg-B end-B-no-blank))
  502. (between-A-no-blank-and-B (buffer-substring end-A-no-blank beg-B)))
  503. (goto-char beg-A)
  504. (delete-region beg-A end-B-no-blank)
  505. (insert (concat body-B between-A-no-blank-and-B body-A)))))
  506. (defun org-move-item-down ()
  507. "Move the plain list item at point down, i.e. swap with following item.
  508. Subitems (items with larger indentation) are considered part of the item,
  509. so this really moves item trees."
  510. (interactive)
  511. (let ((pos (point))
  512. (col (current-column))
  513. (actual-item (org-beginning-of-item))
  514. (next-item (org-get-next-item (point) (save-excursion (org-end-of-item-list)))))
  515. (if (not next-item)
  516. (progn
  517. (goto-char pos)
  518. (error "Cannot move this item further down"))
  519. (org-list-exchange-items actual-item next-item)
  520. (org-maybe-renumber-ordered-list)
  521. (org-next-item)
  522. (move-to-column col))))
  523. (defun org-move-item-up ()
  524. "Move the plain list item at point up, i.e. swap with previous item.
  525. Subitems (items with larger indentation) are considered part of the item,
  526. so this really moves item trees."
  527. (interactive)
  528. (let ((pos (point))
  529. (col (current-column))
  530. (actual-item (org-beginning-of-item))
  531. (prev-item (org-get-previous-item (point) (save-excursion (org-beginning-of-item-list)))))
  532. (if (not prev-item)
  533. (progn
  534. (goto-char pos)
  535. (error "Cannot move this item further up"))
  536. (org-list-exchange-items prev-item actual-item)
  537. (org-maybe-renumber-ordered-list)
  538. (move-to-column col))))
  539. (defun org-insert-item (&optional checkbox)
  540. "Insert a new item at the current level.
  541. If cursor is before first character after bullet of the item, the
  542. new item will be created before the current one. Return t when
  543. things worked, nil when we are not in an item, or we are inside a
  544. block, or item is invisible."
  545. (unless (or (not (org-in-item-p))
  546. (org-invisible-p)
  547. (org-in-regexps-block-p "^[ \t]*#\\+begin_\\([a-zA-Z]\\)"
  548. '(concat "^[ \t]*#\\+end_" (match-string 1)))
  549. (org-in-regexps-block-p "^[ \t]*\\\\begin{\\([a-zA-Z]\\)}"
  550. '(concat "^[ \t]*\\\\end{" (match-string 1) "}")))
  551. (let* ((pos (point))
  552. (before-p (and (org-at-item-p)
  553. (<= (point) (match-end 0))))
  554. (item-start (org-beginning-of-item))
  555. (bullet-init (and (looking-at (org-item-re))
  556. (match-string 0)))
  557. (description-p (and (looking-at "[ \t]*\\(.*?\\) ::")
  558. (match-string 1)))
  559. (timer-p (and description-p
  560. (string-match "^[-+*][ \t]+[0-9]+:[0-9]+:[0-9]+$" description-p)))
  561. ;; Guess number of blank lines used to separate items.
  562. (blank-lines-nb (let* ((insert-blank-p
  563. (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  564. (limit (save-excursion (org-end-of-item-list)))
  565. (next-item-p (org-get-next-item (point) limit)))
  566. (cond
  567. ;; cases where there should be no blank line.
  568. ((or (not insert-blank-p)
  569. org-empty-line-terminates-plain-lists) 0)
  570. ;; If there's a next item, count blank
  571. ;; lines between current and next item.
  572. (next-item-p (and (goto-char next-item-p)
  573. (org-back-over-empty-lines)))
  574. ;; if we're not on the first item, there
  575. ;; is one above. Count blank lines between.
  576. ((not (org-first-list-item-p)) (org-back-over-empty-lines))
  577. ;; Only one item list: can't guess.
  578. ;; Follow `org-blank-before-new-entry'
  579. ((eq insert-blank-p 'auto) 0)
  580. (t 1))))
  581. (insert-fun (lambda (&optional string-after-bullet)
  582. ;; insert bullet above item in order to avoid
  583. ;; bothering with possible blank lines ending
  584. ;; last item
  585. (org-beginning-of-item)
  586. (insert (concat bullet-init
  587. (when checkbox "[ ] ")
  588. (when (and description-p (not timer-p))
  589. (concat (read-string "Term: ") " :: "))))
  590. (save-excursion
  591. (insert (concat string-after-bullet
  592. (make-string (1+ blank-lines-nb) ?\n))))
  593. (unless before-p (org-move-item-down)))))
  594. (goto-char pos)
  595. (cond
  596. ;; if we're adding a timer, delegate to `org-timer-item'.
  597. (timer-p (org-timer-item) t)
  598. (before-p
  599. (funcall insert-fun)
  600. ;; Renumber in this case, as we're not moving down.
  601. (org-maybe-renumber-ordered-list) t)
  602. ;; if we can't split item, just insert bullet at the end of
  603. ;; item.
  604. ((not (org-get-alist-option org-M-RET-may-split-line 'item))
  605. (funcall insert-fun) t)
  606. ;; else, insert a new bullet along with everything from point
  607. ;; down to last non-blank line of item
  608. (t
  609. (delete-horizontal-space)
  610. ;; get pos again in case previous command changed line.
  611. (let* ((pos (point))
  612. (end-before-blank (org-end-of-item-before-blank))
  613. (after-bullet (when (< pos end-before-blank)
  614. (prog1
  615. (buffer-substring pos end-before-blank)
  616. (delete-region pos end-before-blank)))))
  617. (funcall insert-fun after-bullet) t))))))
  618. ;;; Indentation
  619. (defun org-get-string-indentation (s)
  620. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  621. (let ((n -1) (i 0) (w tab-width) c)
  622. (catch 'exit
  623. (while (< (setq n (1+ n)) (length s))
  624. (setq c (aref s n))
  625. (cond ((= c ?\ ) (setq i (1+ i)))
  626. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  627. (t (throw 'exit t)))))
  628. i))
  629. (defvar org-suppress-item-indentation) ; dynamically scoped parameter
  630. (defun org-shift-item-indentation (delta)
  631. "Shift the indentation in current item by DELTA."
  632. (unless (org-bound-and-true-p org-suppress-item-indentation)
  633. (save-excursion
  634. (let ((beg (point-at-bol))
  635. (end (progn (org-end-of-item) (point)))
  636. i)
  637. (goto-char end)
  638. (beginning-of-line 0)
  639. (while (> (point) beg)
  640. (when (looking-at "[ \t]*\\S-")
  641. ;; this is not an empty line
  642. (setq i (org-get-indentation))
  643. (if (and (> i 0) (> (setq i (+ i delta)) 0))
  644. (indent-line-to i)))
  645. (beginning-of-line 0))))))
  646. (defvar org-last-indent-begin-marker (make-marker))
  647. (defvar org-last-indent-end-marker (make-marker))
  648. (defun org-outdent-item (arg)
  649. "Outdent a local list item, but not its children."
  650. (interactive "p")
  651. (org-indent-item-tree (- arg) 'no-subtree))
  652. (defun org-indent-item (arg)
  653. "Indent a local list item, but not its children."
  654. (interactive "p")
  655. (org-indent-item-tree arg 'no-subtree))
  656. (defun org-outdent-item-tree (arg &optional no-subtree)
  657. "Outdent a local list item including its children.
  658. If NO-SUBTREE is set, only outdent the item itself, not its children."
  659. (interactive "p")
  660. (org-indent-item-tree (- arg) no-subtree))
  661. (defun org-indent-item-tree (arg &optional no-subtree)
  662. "Indent a local list item including its children.
  663. If NO-SUBTREE is set, only indent the item itself, not its children."
  664. (interactive "p")
  665. (and (org-region-active-p) (org-cursor-to-region-beginning))
  666. (unless (org-at-item-p)
  667. (error "Not on an item"))
  668. (let ((origin-ind (save-excursion
  669. (goto-char (org-list-top-point))
  670. (org-get-indentation)))
  671. beg end ind ind1 ind-bul delta ind-down ind-up firstp)
  672. (setq firstp (org-first-list-item-p))
  673. (save-excursion
  674. (setq end (and (org-region-active-p) (region-end)))
  675. (if (and (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  676. (memq this-command '(org-shiftmetaright org-shiftmetaleft)))
  677. (setq beg org-last-indent-begin-marker
  678. end org-last-indent-end-marker)
  679. (org-beginning-of-item)
  680. (setq beg (move-marker org-last-indent-begin-marker (point)))
  681. (if no-subtree
  682. (org-end-of-item-text-before-children)
  683. (org-end-of-item))
  684. (setq end (move-marker org-last-indent-end-marker (or end (point)))))
  685. (goto-char beg)
  686. (setq ind-bul (org-item-indent-positions)
  687. ind (caar ind-bul)
  688. ind-down (car (nth 2 ind-bul))
  689. ind-up (car (nth 1 ind-bul))
  690. delta (if (> arg 0)
  691. (if ind-down (- ind-down ind) 2)
  692. (if ind-up (- ind-up ind) -2)))
  693. (if (and (< (+ delta ind) origin-ind)
  694. ;; verify we're not at the top level item
  695. (/= (point-at-bol) (org-list-top-point)))
  696. (error "Cannot outdent beyond top level item"))
  697. (while (< (point) end)
  698. (beginning-of-line 1)
  699. (skip-chars-forward " \t") (setq ind1 (current-column))
  700. (delete-region (point-at-bol) (point))
  701. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  702. (beginning-of-line 2)))
  703. (org-fix-bullet-type
  704. (and (> arg 0)
  705. (not firstp)
  706. (cdr (assoc (cdr (nth 0 ind-bul)) org-list-demote-modify-bullet))))
  707. (org-maybe-renumber-ordered-list-safe)
  708. (save-excursion
  709. (beginning-of-line 0)
  710. (ignore-errors (org-beginning-of-item))
  711. (org-maybe-renumber-ordered-list-safe))))
  712. (defun org-item-indent-positions ()
  713. "Return indentation for plain list items.
  714. This returns a list with three values: The current indentation, the
  715. parent indentation and the indentation a child should have.
  716. Assumes cursor in item line."
  717. (let* ((bolpos (point-at-bol))
  718. (ind (org-get-indentation))
  719. (bullet (org-get-bullet))
  720. ind-down ind-up bullet-up bullet-down pos)
  721. (save-excursion
  722. (org-beginning-of-item-list)
  723. (skip-chars-backward "\n\r \t")
  724. (when (org-in-item-p)
  725. (org-beginning-of-item)
  726. (let ((prev-indent (org-get-indentation)))
  727. (when (< prev-indent ind)
  728. (setq ind-up prev-indent)
  729. (setq bullet-up (org-get-bullet))))))
  730. (setq pos (point))
  731. (save-excursion
  732. (cond
  733. ((and (ignore-errors (progn (org-previous-item) t))
  734. (or (end-of-line) t)
  735. (org-search-forward-unenclosed (org-item-re) bolpos t))
  736. (setq ind-down (org-get-indentation)
  737. bullet-down (org-get-bullet)))
  738. ((and (goto-char pos)
  739. (org-at-item-p))
  740. (goto-char (match-end 0))
  741. (skip-chars-forward " \t")
  742. (setq ind-down (current-column)
  743. bullet-down (org-get-bullet)))))
  744. (if (and bullet-down (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-down))
  745. (setq bullet-down (concat "1" (match-string 1 bullet-down))))
  746. (if (and bullet-up (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet-up))
  747. (setq bullet-up (concat "1" (match-string 1 bullet-up))))
  748. (if (and bullet (string-match "\\`[0-9]+\\(\\.\\|)\\)\\'" bullet))
  749. (setq bullet (concat "1" (match-string 1 bullet))))
  750. (list (cons ind bullet)
  751. (cons ind-up bullet-up)
  752. (cons ind-down bullet-down))))
  753. (defvar org-tab-ind-state) ; defined in org.el
  754. (defun org-cycle-item-indentation ()
  755. (let ((org-suppress-item-indentation t)
  756. (org-adapt-indentation nil))
  757. (cond
  758. ((and (looking-at "[ \t]*$")
  759. (org-looking-back "^\\([ \t]*\\)\\([-+*]\\|[0-9]+[).]\\)[ \t]+"))
  760. (setq this-command 'org-cycle-item-indentation)
  761. (if (eq last-command 'org-cycle-item-indentation)
  762. (condition-case nil
  763. (progn (org-outdent-item 1)
  764. (if (equal org-tab-ind-state (org-get-indentation))
  765. (org-outdent-item 1))
  766. (end-of-line 1))
  767. (error
  768. (progn
  769. (while (< (org-get-indentation) org-tab-ind-state)
  770. (progn (org-indent-item 1) (end-of-line 1)))
  771. (setq this-command 'org-cycle))))
  772. (setq org-tab-ind-state (org-get-indentation))
  773. (org-indent-item 1))
  774. t))))
  775. ;;; Bullets
  776. (defun org-get-bullet ()
  777. (save-excursion
  778. (goto-char (point-at-bol))
  779. (and (looking-at
  780. "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\(\\*\\)\\)\\( \\|$\\)")
  781. (or (match-string 2) (match-string 4)))))
  782. (defun org-fix-bullet-type (&optional force-bullet)
  783. "Make sure all items in this list have the same bullet as the first item.
  784. Also, fix the indentation."
  785. (interactive)
  786. (unless (org-at-item-p) (error "This is not a list"))
  787. (org-preserve-lc
  788. (let* ((bullet
  789. (progn
  790. (org-beginning-of-item-list)
  791. (looking-at "[ \t]*\\(\\S-+\\)")
  792. (concat (or force-bullet (match-string 1)) " "
  793. ;; do we need to concat another white space ?
  794. (when (and org-list-two-spaces-after-bullet-regexp
  795. (string-match org-list-two-spaces-after-bullet-regexp bullet))
  796. " "))))
  797. (replace-bullet
  798. (lambda (result bullet)
  799. (let* ((old (progn
  800. (skip-chars-forward " \t")
  801. (looking-at "\\S-+ *")
  802. (match-string 0))))
  803. (unless (equal bullet old)
  804. (replace-match bullet)
  805. ;; when bullet lengths are differents, move the whole
  806. ;; sublist accordingly
  807. (org-shift-item-indentation (- (length bullet) (length old))))))))
  808. (org-apply-on-list replace-bullet nil bullet)
  809. ;; fix item numbers if necessary
  810. (when (string-match "[0-9]" bullet) (org-renumber-ordered-list)))))
  811. (defun org-renumber-ordered-list (&optional arg)
  812. "Renumber an ordered plain list.
  813. Cursor needs to be in the first line of an item, the line that starts
  814. with something like \"1.\" or \"2)\". Start to count at ARG or 1."
  815. (interactive "p")
  816. (unless (and (org-at-item-p)
  817. (match-beginning 3))
  818. (error "This is not an ordered list"))
  819. (org-preserve-lc
  820. (let* ((offset (progn
  821. (org-beginning-of-item)
  822. (or (and (looking-at "[ \t]*\\[@start:\\([0-9]+\\)")
  823. (string-to-number (match-string 1)))
  824. arg
  825. 1)))
  826. (item-fmt (progn
  827. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  828. (concat "%d" (or (match-string 1) "."))))
  829. ;; Here is the function applied at each item of the list.
  830. (renumber-item (lambda (counter off fmt)
  831. (let* ((new (format fmt (+ counter off)))
  832. (old (progn
  833. (looking-at (org-item-re))
  834. (match-string 2)))
  835. (begin (match-beginning 2))
  836. (end (match-end 2)))
  837. (delete-region begin end)
  838. (goto-char begin)
  839. (insert new)
  840. ;; In case item number went from 9. to 10.
  841. ;; or the other way.
  842. (org-shift-item-indentation (- (length new) (length old)))
  843. (1+ counter)))))
  844. (org-apply-on-list renumber-item 0 offset item-fmt))))
  845. (defun org-maybe-renumber-ordered-list ()
  846. "Renumber the ordered list at point if setup allows it.
  847. This tests the user option `org-auto-renumber-ordered-lists' before
  848. doing the renumbering."
  849. (interactive)
  850. (when (and org-auto-renumber-ordered-lists
  851. (org-at-item-p))
  852. (if (match-beginning 3)
  853. (org-renumber-ordered-list 1)
  854. (org-fix-bullet-type))))
  855. (defun org-maybe-renumber-ordered-list-safe ()
  856. (ignore-errors
  857. (save-excursion
  858. (org-maybe-renumber-ordered-list))))
  859. (defun org-cycle-list-bullet (&optional which)
  860. "Cycle through the different itemize/enumerate bullets.
  861. This cycle the entire list level through the sequence:
  862. `-' -> `+' -> `*' -> `1.' -> `1)'
  863. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  864. 0 means `-', 1 means `+' etc."
  865. (interactive "P")
  866. (org-preserve-lc
  867. (let* ((current (progn
  868. (org-beginning-of-item-list)
  869. (org-at-item-p)
  870. (match-string 0)))
  871. (prevp (eq which 'previous))
  872. (new (cond
  873. ((and (numberp which)
  874. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  875. ((string-match "-" current) (if prevp "1)" "+"))
  876. ((string-match "\\+" current)
  877. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  878. ((string-match "\\*" current) (if prevp "+" "1."))
  879. ((string-match "\\." current)
  880. (if prevp (if (looking-at "\\S-") "+" "*") "1)"))
  881. ((string-match ")" current) (if prevp "1." "-"))
  882. (t (error "This should not happen"))))
  883. (old (and (looking-at "\\([ \t]*\\)\\(\\S-+\\)")
  884. (match-string 2))))
  885. (replace-match (concat "\\1" new))
  886. (org-shift-item-indentation (- (length new) (length old)))
  887. (org-fix-bullet-type)
  888. (org-maybe-renumber-ordered-list))))
  889. ;;; Checkboxes
  890. (defun org-toggle-checkbox (&optional toggle-presence)
  891. "Toggle the checkbox in the current line.
  892. With prefix arg TOGGLE-PRESENCE, add or remove checkboxes.
  893. With double prefix, set checkbox to [-].
  894. When there is an active region, toggle status or presence of the checkbox
  895. in the first line, and make every item in the region have the same
  896. status or presence, respectively.
  897. If the cursor is in a headline, apply this to all checkbox items in the
  898. text below the heading."
  899. (interactive "P")
  900. (catch 'exit
  901. (let (beg end status first-present first-status blocked)
  902. (cond
  903. ((org-region-active-p)
  904. (setq beg (region-beginning) end (region-end)))
  905. ((org-on-heading-p)
  906. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  907. ((org-at-item-checkbox-p)
  908. (save-excursion
  909. (if (equal toggle-presence '(4))
  910. (progn
  911. (replace-match "")
  912. (goto-char (match-beginning 0))
  913. (just-one-space))
  914. (when (setq blocked (org-checkbox-blocked-p))
  915. (error "Checkbox blocked because of unchecked box in line %d"
  916. blocked))
  917. (replace-match
  918. (cond ((equal toggle-presence '(16)) "[-]")
  919. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  920. (t "[ ]"))
  921. t t)))
  922. (throw 'exit t))
  923. ((org-at-item-p)
  924. ;; add a checkbox
  925. (save-excursion
  926. (goto-char (match-end 0))
  927. (insert "[ ] "))
  928. (throw 'exit t))
  929. (t (error "Not at a checkbox or heading, and no active region")))
  930. (setq end (move-marker (make-marker) end))
  931. (save-excursion
  932. (goto-char beg)
  933. (setq first-present (org-at-item-checkbox-p)
  934. first-status
  935. (save-excursion
  936. (and (re-search-forward "[ \t]\\(\\[[ X]\\]\\)" end t)
  937. (equal (match-string 1) "[X]"))))
  938. (while (< (point) end)
  939. (if toggle-presence
  940. (cond
  941. ((and first-present (org-at-item-checkbox-p))
  942. (save-excursion
  943. (replace-match "")
  944. (goto-char (match-beginning 0))
  945. (just-one-space)))
  946. ((and (not first-present) (not (org-at-item-checkbox-p))
  947. (org-at-item-p))
  948. (save-excursion
  949. (goto-char (match-end 0))
  950. (insert "[ ] "))))
  951. (when (org-at-item-checkbox-p)
  952. (setq status (equal (match-string 0) "[X]"))
  953. (replace-match
  954. (if first-status "[ ]" "[X]") t t)))
  955. (beginning-of-line 2)))))
  956. (org-update-checkbox-count-maybe))
  957. (defun org-reset-checkbox-state-subtree ()
  958. "Reset all checkboxes in an entry subtree."
  959. (interactive "*")
  960. (save-restriction
  961. (save-excursion
  962. (org-narrow-to-subtree)
  963. (org-show-subtree)
  964. (goto-char (point-min))
  965. (let ((end (point-max)))
  966. (while (< (point) end)
  967. (when (org-at-item-checkbox-p)
  968. (replace-match "[ ]" t t))
  969. (beginning-of-line 2))))
  970. (org-update-checkbox-count-maybe)))
  971. (defvar org-checkbox-statistics-hook nil
  972. "Hook that is run whenever Org thinks checkbox statistics should be updated.
  973. This hook runs even if `org-provide-checkbox-statistics' is nil, to it can
  974. be used to implement alternative ways of collecting statistics information.")
  975. (defun org-update-checkbox-count-maybe ()
  976. "Update checkbox statistics unless turned off by user."
  977. (when org-provide-checkbox-statistics
  978. (org-update-checkbox-count))
  979. (run-hooks 'org-checkbox-statistics-hook))
  980. (defun org-update-checkbox-count (&optional all)
  981. "Update the checkbox statistics in the current section.
  982. This will find all statistic cookies like [57%] and [6/12] and update them
  983. with the current numbers. With optional prefix argument ALL, do this for
  984. the whole buffer."
  985. (interactive "P")
  986. (save-excursion
  987. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  988. (beg (condition-case nil
  989. (progn (org-back-to-heading) (point))
  990. (error (point-min))))
  991. (end (move-marker (make-marker)
  992. (progn (outline-next-heading) (point))))
  993. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  994. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  995. (re-find (concat re "\\|" re-box))
  996. beg-cookie end-cookie is-percent c-on c-off lim new
  997. eline curr-ind next-ind continue-from startsearch
  998. (recursive
  999. (or (not org-hierarchical-checkbox-statistics)
  1000. (string-match "\\<recursive\\>"
  1001. (or (ignore-errors
  1002. (org-entry-get nil "COOKIE_DATA"))
  1003. ""))))
  1004. (cstat 0)
  1005. )
  1006. (when all
  1007. (goto-char (point-min))
  1008. (outline-next-heading)
  1009. (setq beg (point) end (point-max)))
  1010. (goto-char end)
  1011. ;; find each statistics cookie
  1012. (while (and (re-search-backward re-find beg t)
  1013. (not (save-match-data
  1014. (and (org-on-heading-p)
  1015. (string-match "\\<todo\\>"
  1016. (downcase
  1017. (or (org-entry-get
  1018. nil "COOKIE_DATA")
  1019. "")))))))
  1020. (setq beg-cookie (match-beginning 1)
  1021. end-cookie (match-end 1)
  1022. cstat (+ cstat (if end-cookie 1 0))
  1023. startsearch (point-at-eol)
  1024. continue-from (match-beginning 0)
  1025. is-percent (match-beginning 2)
  1026. lim (cond
  1027. ((org-on-heading-p) (outline-next-heading) (point))
  1028. ((org-at-item-p) (org-end-of-item) (point))
  1029. (t nil))
  1030. c-on 0
  1031. c-off 0)
  1032. (when lim
  1033. ;; find first checkbox for this cookie and gather
  1034. ;; statistics from all that are at this indentation level
  1035. (goto-char startsearch)
  1036. (if (re-search-forward re-box lim t)
  1037. (progn
  1038. (org-beginning-of-item)
  1039. (setq curr-ind (org-get-indentation))
  1040. (setq next-ind curr-ind)
  1041. (while (and (bolp) (org-at-item-p)
  1042. (if recursive
  1043. (<= curr-ind next-ind)
  1044. (= curr-ind next-ind)))
  1045. (save-excursion (end-of-line) (setq eline (point)))
  1046. (if (re-search-forward re-box eline t)
  1047. (if (member (match-string 2) '("[ ]" "[-]"))
  1048. (setq c-off (1+ c-off))
  1049. (setq c-on (1+ c-on))))
  1050. (if (not recursive)
  1051. ;; org-get-next-item goes through list-enders
  1052. ;; with proper limit.
  1053. (goto-char (or (org-get-next-item (point) lim) lim))
  1054. (end-of-line)
  1055. (when (org-search-forward-unenclosed (org-item-re) lim t)
  1056. (beginning-of-line)))
  1057. (setq next-ind (org-get-indentation)))))
  1058. (goto-char continue-from)
  1059. ;; update cookie
  1060. (when end-cookie
  1061. (setq new (if is-percent
  1062. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  1063. (format "[%d/%d]" c-on (+ c-on c-off))))
  1064. (goto-char beg-cookie)
  1065. (insert new)
  1066. (delete-region (point) (+ (point) (- end-cookie beg-cookie))))
  1067. ;; update items checkbox if it has one
  1068. (when (org-at-item-p)
  1069. (org-beginning-of-item)
  1070. (when (and (> (+ c-on c-off) 0)
  1071. (re-search-forward re-box (point-at-eol) t))
  1072. (setq beg-cookie (match-beginning 2)
  1073. end-cookie (match-end 2))
  1074. (delete-region beg-cookie end-cookie)
  1075. (goto-char beg-cookie)
  1076. (cond ((= c-off 0) (insert "[X]"))
  1077. ((= c-on 0) (insert "[ ]"))
  1078. (t (insert "[-]")))
  1079. )))
  1080. (goto-char continue-from))
  1081. (when (interactive-p)
  1082. (message "Checkbox statistics updated %s (%d places)"
  1083. (if all "in entire file" "in current outline entry") cstat)))))
  1084. (defun org-get-checkbox-statistics-face ()
  1085. "Select the face for checkbox statistics.
  1086. The face will be `org-done' when all relevant boxes are checked.
  1087. Otherwise it will be `org-todo'."
  1088. (if (match-end 1)
  1089. (if (equal (match-string 1) "100%")
  1090. 'org-checkbox-statistics-done
  1091. 'org-checkbox-statistics-todo)
  1092. (if (and (> (match-end 2) (match-beginning 2))
  1093. (equal (match-string 2) (match-string 3)))
  1094. 'org-checkbox-statistics-done
  1095. 'org-checkbox-statistics-todo)))
  1096. ;;; Misc Tools
  1097. (defun org-apply-on-list (function init-value &rest args)
  1098. "Call FUNCTION for each item of a the list under point.
  1099. FUNCTION must be called with at least one argument : a return
  1100. value that will contain the value returned by the function at
  1101. the previous item, plus ARGS extra arguments. INIT-VALUE will be
  1102. the value passed to the function at the first item of the list.
  1103. As an example, (org-apply-on-list (lambda (result) (1+ result)) 0)
  1104. will return the number of items in the current list.
  1105. Sublists of the list are skipped. Cursor is always at the
  1106. beginning of the item."
  1107. (save-excursion
  1108. (let ((move-down-action
  1109. (lambda (pos value &rest args)
  1110. (goto-char pos)
  1111. (let ((return-value (apply function value args))
  1112. ;; we need to recompute each time end of list in case
  1113. ;; function modified list.
  1114. (next-p (org-get-next-item pos (org-end-of-item-list))))
  1115. (if next-p
  1116. (apply move-down-action next-p return-value args)
  1117. return-value)))))
  1118. (apply move-down-action (org-beginning-of-item-list) init-value args))))
  1119. (defun org-sort-list (&optional with-case sorting-type getkey-func compare-func)
  1120. "Sort plain list items.
  1121. The cursor may be at any item of the list that should be sorted.
  1122. Sublists are not sorted.
  1123. Sorting can be alphabetically, numerically, by date/time as given by
  1124. a time stamp, by a property or by priority.
  1125. The command prompts for the sorting type unless it has been given to the
  1126. function through the SORTING-TYPE argument, which needs to be a character,
  1127. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  1128. precise meaning of each character:
  1129. n Numerically, by converting the beginning of the entry/item to a number.
  1130. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  1131. t By date/time, either the first active time stamp in the entry, or, if
  1132. none exist, by the first inactive one.
  1133. In items, only the first line will be checked.
  1134. Capital letters will reverse the sort order.
  1135. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a
  1136. function to be called with point at the beginning of the record.
  1137. It must return either a string or a number that should serve as
  1138. the sorting key for that record.
  1139. Comparing entries ignores case by default. However, with an
  1140. optional argument WITH-CASE, the sorting considers case as well."
  1141. (interactive "P")
  1142. (let* ((case-func (if with-case 'identity 'downcase))
  1143. (start (org-beginning-of-item-list))
  1144. (end (save-excursion (org-end-of-item-list)))
  1145. (sorting-type
  1146. (progn
  1147. (message
  1148. "Sort plain list: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:")
  1149. (read-char-exclusive)))
  1150. (getkey-func (and (= (downcase sorting-type) ?f)
  1151. (org-icompleting-read "Sort using function: "
  1152. obarray 'fboundp t nil nil)
  1153. (intern getkey-func))))
  1154. (message "Sorting items...")
  1155. (save-restriction
  1156. (narrow-to-region start end)
  1157. (let* ((dcst (downcase sorting-type))
  1158. (case-fold-search nil)
  1159. (now (current-time))
  1160. (sort-func (cond
  1161. ((= dcst ?a) 'string<)
  1162. ((= dcst ?f) compare-func)
  1163. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  1164. (t nil)))
  1165. (begin-record (lambda ()
  1166. (skip-chars-forward " \r\t\n")
  1167. (beginning-of-line)))
  1168. (end-record (lambda ()
  1169. (goto-char (org-end-of-item-before-blank))))
  1170. (value-to-sort (lambda nil
  1171. (when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
  1172. (cond
  1173. ((= dcst ?n)
  1174. (string-to-number (buffer-substring (match-end 0)
  1175. (point-at-eol))))
  1176. ((= dcst ?a)
  1177. (buffer-substring (match-end 0) (point-at-eol)))
  1178. ((= dcst ?t)
  1179. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  1180. (re-search-forward org-ts-regexp-both
  1181. (point-at-eol) t))
  1182. (org-time-string-to-seconds (match-string 0))
  1183. (org-float-time now)))
  1184. ((= dcst ?f)
  1185. (if getkey-func
  1186. (let ((value (funcall getkey-func)))
  1187. (if (stringp value)
  1188. (funcall case-func value)
  1189. value))
  1190. (error "Invalid key function `%s'" getkey-func)))
  1191. (t (error "Invalid sorting type `%c'" sorting-type)))))))
  1192. (sort-subr (/= dcst sorting-type) begin-record end-record value-to-sort nil sort-func)
  1193. (org-maybe-renumber-ordered-list)
  1194. (run-hooks 'org-after-sorting-entries-or-items-hook)
  1195. (message "Sorting items...done")))))
  1196. ;;; Send and receive lists
  1197. (defun org-list-parse-list (&optional delete)
  1198. "Parse the list at point and maybe DELETE it.
  1199. Return a list containing first level items as strings and
  1200. sublevels as a list of strings."
  1201. (let* ((start (goto-char (org-list-top-point)))
  1202. (end (org-list-bottom-point))
  1203. output itemsep ltype)
  1204. (while (org-search-forward-unenclosed (org-item-re) end t)
  1205. (save-excursion
  1206. (beginning-of-line)
  1207. (cond ((looking-at-p "^[ \t]*[0-9]")
  1208. (setq itemsep "[0-9]+\\(?:\\.\\|)\\)"
  1209. ltype 'ordered))
  1210. ((looking-at-p "^.*::")
  1211. (setq itemsep "[-+*]" ltype 'descriptive))
  1212. (t (setq itemsep "[-+*]" ltype 'unordered))))
  1213. (let* ((indent1 (org-get-indentation))
  1214. (nextitem (or (org-get-next-item (point) end) end))
  1215. (item (org-trim (buffer-substring (point) (org-end-of-item-text-before-children))))
  1216. (nextindent (if (= (point) end) 0 (org-get-indentation)))
  1217. (item (if (string-match "^\\[\\([xX ]\\)\\]" item)
  1218. (replace-match (if (equal (match-string 1 item) " ")
  1219. "[CBOFF]"
  1220. "[CBON]")
  1221. t nil item)
  1222. item)))
  1223. (push item output)
  1224. (when (> nextindent indent1)
  1225. (save-restriction
  1226. (narrow-to-region (point) nextitem)
  1227. (push (org-list-parse-list) output)))))
  1228. (when delete
  1229. (delete-region start end)
  1230. (save-match-data
  1231. (when (looking-at (org-list-end-re))
  1232. (replace-match "\n"))))
  1233. (setq output (nreverse output))
  1234. (push ltype output)))
  1235. (defun org-list-make-subtree ()
  1236. "Convert the plain list at point into a subtree."
  1237. (interactive)
  1238. (goto-char (org-list-top-point))
  1239. (let ((list (org-list-parse-list t)) nstars)
  1240. (save-excursion
  1241. (if (ignore-errors
  1242. (org-back-to-heading))
  1243. (progn (re-search-forward org-complex-heading-regexp nil t)
  1244. (setq nstars (length (match-string 1))))
  1245. (setq nstars 0)))
  1246. (org-list-make-subtrees list (1+ nstars))))
  1247. (defun org-list-make-subtrees (list level)
  1248. "Convert LIST into subtrees starting at LEVEL."
  1249. (if (symbolp (car list))
  1250. (org-list-make-subtrees (cdr list) level)
  1251. (mapcar (lambda (item)
  1252. (if (stringp item)
  1253. (insert (make-string
  1254. (if org-odd-levels-only
  1255. (1- (* 2 level)) level) ?*) " " item "\n")
  1256. (org-list-make-subtrees item (1+ level))))
  1257. list)))
  1258. (defun org-list-insert-radio-list ()
  1259. "Insert a radio list template appropriate for this major mode."
  1260. (interactive)
  1261. (let* ((e (assq major-mode org-list-radio-list-templates))
  1262. (txt (nth 1 e))
  1263. name pos)
  1264. (unless e (error "No radio list setup defined for %s" major-mode))
  1265. (setq name (read-string "List name: "))
  1266. (while (string-match "%n" txt)
  1267. (setq txt (replace-match name t t txt)))
  1268. (or (bolp) (insert "\n"))
  1269. (setq pos (point))
  1270. (insert txt)
  1271. (goto-char pos)))
  1272. (defun org-list-send-list (&optional maybe)
  1273. "Send a transformed version of this list to the receiver position.
  1274. With argument MAYBE, fail quietly if no transformation is defined for
  1275. this list."
  1276. (interactive)
  1277. (catch 'exit
  1278. (unless (org-at-item-p) (error "Not at a list"))
  1279. (save-excursion
  1280. (goto-char (org-list-top-point))
  1281. (beginning-of-line 0)
  1282. (unless (looking-at "[ \t]*#\\+ORGLST[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
  1283. (if maybe
  1284. (throw 'exit nil)
  1285. (error "Don't know how to transform this list"))))
  1286. (let* ((name (match-string 1))
  1287. (transform (intern (match-string 2)))
  1288. (top-point (org-list-top-point))
  1289. (bottom-point
  1290. (save-excursion
  1291. (goto-char (org-list-bottom-point))
  1292. (re-search-backward "\\(\\\\end{comment}\\|@end ignore\\|-->\\)" top-point t)))
  1293. (list (save-restriction
  1294. (narrow-to-region top-point bottom-point)
  1295. (org-list-parse-list)))
  1296. beg txt)
  1297. (unless (fboundp transform)
  1298. (error "No such transformation function %s" transform))
  1299. (let ((txt (funcall transform list)))
  1300. ;; Find the insertion place
  1301. (save-excursion
  1302. (goto-char (point-min))
  1303. (unless (re-search-forward
  1304. (concat "BEGIN RECEIVE ORGLST +" name "\\([ \t]\\|$\\)") nil t)
  1305. (error "Don't know where to insert translated list"))
  1306. (goto-char (match-beginning 0))
  1307. (beginning-of-line 2)
  1308. (setq beg (point))
  1309. (unless (re-search-forward (concat "END RECEIVE ORGLST +" name) nil t)
  1310. (error "Cannot find end of insertion region"))
  1311. (beginning-of-line 1)
  1312. (delete-region beg (point))
  1313. (goto-char beg)
  1314. (insert txt "\n")))
  1315. (message "List converted and installed at receiver location"))))
  1316. (defun org-list-to-generic (list params)
  1317. "Convert a LIST parsed through `org-list-parse-list' to other formats.
  1318. Valid parameters PARAMS are
  1319. :ustart String to start an unordered list
  1320. :uend String to end an unordered list
  1321. :ostart String to start an ordered list
  1322. :oend String to end an ordered list
  1323. :dstart String to start a descriptive list
  1324. :dend String to end a descriptive list
  1325. :dtstart String to start a descriptive term
  1326. :dtend String to end a descriptive term
  1327. :ddstart String to start a description
  1328. :ddend String to end a description
  1329. :splice When set to t, return only list body lines, don't wrap
  1330. them into :[u/o]start and :[u/o]end. Default is nil.
  1331. :istart String to start a list item
  1332. :iend String to end a list item
  1333. :isep String to separate items
  1334. :lsep String to separate sublists
  1335. :cboff String to insert for an unchecked checkbox
  1336. :cbon String to insert for a checked checkbox"
  1337. (interactive)
  1338. (let* ((p params) sublist
  1339. (splicep (plist-get p :splice))
  1340. (ostart (plist-get p :ostart))
  1341. (oend (plist-get p :oend))
  1342. (ustart (plist-get p :ustart))
  1343. (uend (plist-get p :uend))
  1344. (dstart (plist-get p :dstart))
  1345. (dend (plist-get p :dend))
  1346. (dtstart (plist-get p :dtstart))
  1347. (dtend (plist-get p :dtend))
  1348. (ddstart (plist-get p :ddstart))
  1349. (ddend (plist-get p :ddend))
  1350. (istart (plist-get p :istart))
  1351. (iend (plist-get p :iend))
  1352. (isep (plist-get p :isep))
  1353. (lsep (plist-get p :lsep))
  1354. (cbon (plist-get p :cbon))
  1355. (cboff (plist-get p :cboff)))
  1356. (let ((wrapper
  1357. (cond ((eq (car list) 'ordered)
  1358. (concat ostart "\n%s" oend "\n"))
  1359. ((eq (car list) 'unordered)
  1360. (concat ustart "\n%s" uend "\n"))
  1361. ((eq (car list) 'descriptive)
  1362. (concat dstart "\n%s" dend "\n"))))
  1363. rtn term defstart defend)
  1364. (while (setq sublist (pop list))
  1365. (cond ((symbolp sublist) nil)
  1366. ((stringp sublist)
  1367. (when (string-match "^\\(.*\\) ::" sublist)
  1368. (setq term (org-trim (format (concat dtstart "%s" dtend)
  1369. (match-string 1 sublist))))
  1370. (setq sublist (concat ddstart
  1371. (org-trim (substring sublist (match-end 0)))
  1372. ddend)))
  1373. (if (string-match "\\[CBON\\]" sublist)
  1374. (setq sublist (replace-match cbon t t sublist)))
  1375. (if (string-match "\\[CBOFF\\]" sublist)
  1376. (setq sublist (replace-match cboff t t sublist)))
  1377. (if (string-match "\\[-\\]" sublist)
  1378. (setq sublist (replace-match "$\\boxminus$" t t sublist)))
  1379. (setq rtn (concat rtn istart term sublist iend isep)))
  1380. (t (setq rtn (concat rtn ;; previous list
  1381. lsep ;; list separator
  1382. (org-list-to-generic sublist p)
  1383. lsep ;; list separator
  1384. )))))
  1385. (format wrapper rtn))))
  1386. (defun org-list-to-latex (list &optional params)
  1387. "Convert LIST into a LaTeX list.
  1388. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1389. with overruling parameters for `org-list-to-generic'."
  1390. (org-list-to-generic
  1391. list
  1392. (org-combine-plists
  1393. '(:splicep nil :ostart "\\begin{enumerate}" :oend "\\end{enumerate}"
  1394. :ustart "\\begin{itemize}" :uend "\\end{itemize}"
  1395. :dstart "\\begin{description}" :dend "\\end{description}"
  1396. :dtstart "[" :dtend "]"
  1397. :ddstart "" :ddend ""
  1398. :istart "\\item " :iend ""
  1399. :isep "\n" :lsep "\n"
  1400. :cbon "\\texttt{[X]}" :cboff "\\texttt{[ ]}")
  1401. params)))
  1402. (defun org-list-to-html (list &optional params)
  1403. "Convert LIST into a HTML list.
  1404. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1405. with overruling parameters for `org-list-to-generic'."
  1406. (org-list-to-generic
  1407. list
  1408. (org-combine-plists
  1409. '(:splicep nil :ostart "<ol>" :oend "</ol>"
  1410. :ustart "<ul>" :uend "</ul>"
  1411. :dstart "<dl>" :dend "</dl>"
  1412. :dtstart "<dt>" :dtend "</dt>"
  1413. :ddstart "<dd>" :ddend "</dd>"
  1414. :istart "<li>" :iend "</li>"
  1415. :isep "\n" :lsep "\n"
  1416. :cbon "<code>[X]</code>" :cboff "<code>[ ]</code>")
  1417. params)))
  1418. (defun org-list-to-texinfo (list &optional params)
  1419. "Convert LIST into a Texinfo list.
  1420. LIST is as returned by `org-list-parse-list'. PARAMS is a property list
  1421. with overruling parameters for `org-list-to-generic'."
  1422. (org-list-to-generic
  1423. list
  1424. (org-combine-plists
  1425. '(:splicep nil :ostart "@itemize @minus" :oend "@end itemize"
  1426. :ustart "@enumerate" :uend "@end enumerate"
  1427. :dstart "@table" :dend "@end table"
  1428. :dtstart "@item " :dtend "\n"
  1429. :ddstart "" :ddend ""
  1430. :istart "@item\n" :iend ""
  1431. :isep "\n" :lsep "\n"
  1432. :cbon "@code{[X]}" :cboff "@code{[ ]}")
  1433. params)))
  1434. (provide 'org-list)
  1435. ;; arch-tag: 73cf50c1-200f-4d1d-8a53-4e842a5b11c8
  1436. ;;; org-list.el ends here