org-list.el 56 KB

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