org-list.el 55 KB

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