org-index.el 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  1. ;;; org-index.el --- A personal index for org and beyond
  2. ;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
  3. ;; Author: Marc Ihm <org-index@2484.de>
  4. ;; Version: 4.2.1
  5. ;; Keywords: outlines index
  6. ;; This file is not part of GNU Emacs.
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Purpose:
  22. ;;
  23. ;; Fast search for selected org headings and things outside of org.
  24. ;;
  25. ;; This package creates and updates an index table of headings or
  26. ;; keywords, references and ids, where each line points to a heading
  27. ;; within org or references something outside. This table is sorted by
  28. ;; usage count, so that frequently used lines appear among the first
  29. ;; search results.
  30. ;;
  31. ;; References are essentially small numbers (e.g. 'R237' or '--455--'), as
  32. ;; created by this package; they are well suited to be used outside of
  33. ;; org, e.g. in folder names, issue trackers or on printed documents.
  34. ;;
  35. ;; On first invocation org-index will guide you to create a dedicated node
  36. ;; for its index table and its configuration flags.
  37. ;;
  38. ;; For basic usage, subcommands 'add' and 'occur' are most important.
  39. ;;
  40. ;;
  41. ;; Setup:
  42. ;;
  43. ;; - Add these lines to your .emacs:
  44. ;;
  45. ;; (require 'org-index)
  46. ;; (org-index-default-keybindings) ; optional
  47. ;;
  48. ;; - Restart your Emacs to make these lines effective.
  49. ;;
  50. ;; - Invoke `org-index', which will assist in creating your index
  51. ;; table. The variable org-index-id will be persisted within your
  52. ;; customization file (typically .emacs).
  53. ;;
  54. ;;
  55. ;; Further reading:
  56. ;;
  57. ;; See the documentation of `org-index', which can also be read
  58. ;; by invoking `org-index' and choosing the help-command.
  59. ;;
  60. ;;
  61. ;; Updates:
  62. ;;
  63. ;; The latest tested version of this file can always be found at:
  64. ;;
  65. ;; http://orgmode.org/cgit.cgi/org-mode.git/plain/contrib/lisp/org-index.el
  66. ;;; Change Log:
  67. ;; [2015-03-18 We] Version 4.2.1
  68. ;; - No garbage in kill-ring
  69. ;; - No recentering after add
  70. ;;
  71. ;; [2015-03-08 Su] Version 4.2.0
  72. ;; - Reference numbers for subcommands can be passed as a prefix argument
  73. ;; - Renamed subcommand 'point' to 'ping'
  74. ;; - New variable org-index-default-keybindings-list with a list of
  75. ;; default keybindings for org-index-default-keybindings
  76. ;; - Added new column level
  77. ;; - removed flags get-category-on-add and get-heading-on-add
  78. ;;
  79. ;; [2015-03-05 Th] Version 4.1.1 and 4.1.2
  80. ;; - org-mark-ring is now used more consistently
  81. ;; - Bugfix when going to a heading by ref
  82. ;;
  83. ;; [2015-02-26 Th] Version 4.0.0 and 4.1.0:
  84. ;; - Removed command "leave"; rather go back with org-mark-ring-goto
  85. ;; - Property "org-index-ref" is no longer used or needed
  86. ;; - Renamed column "link" to "id"
  87. ;; - Added maintainance options to find duplicate rows, to check ids,
  88. ;; update index or remove property org-index-ref from nodes
  89. ;; - New command point
  90. ;; - Shortened versin history
  91. ;;
  92. ;; [2014-12-07 Sa] to [2015-01-31 Sa] Version 3.0.0 to 3.2.0:
  93. ;; - Complete sorting of index only occurs in idle-timer
  94. ;; - New command "maintain" with some subcommands
  95. ;; - Rewrote command "occur" with overlays in an indirect buffer
  96. ;; - introduced variable org-index-version
  97. ;; - Command "add" updates index, if node is already present
  98. ;; - New commands "add" and "delete" to easily add and remove
  99. ;; the current node to or from your index.
  100. ;; - New command "example" to create an example index.
  101. ;; - Moved flags to a list within the same node as the index table;
  102. ;; this breaks compatibility to prior versions of the package.
  103. ;; - Several new flags that are explained within index node.
  104. ;; - Removed commands "reuse", "missing", "put", "goto",
  105. ;; "update", "link", "fill", "unhighlight"
  106. ;; - New function `org-index-default-keybindings'
  107. ;;
  108. ;; [2012-12-07 Fr] to [2014-04-26 Sa] Version 2.0.0 to 2.4.3:
  109. ;; - New functions org-index-new-line and org-index-get-line
  110. ;; offer access to org-index from other lisp programs
  111. ;; - Regression tests with ert
  112. ;; - Renamed from "org-favtable" to "org-index"
  113. ;; - Added an assistant to set up the index table
  114. ;; - occur is now incremental, searching as you type
  115. ;; - Integrated with org-mark-ring-goto
  116. ;; - Added full support for ids
  117. ;; - Renamed the package from "org-reftable" to "org-favtable"
  118. ;; - Additional columns are required (e.g. "link"). Error messages will
  119. ;; guide you
  120. ;; - Ask user explicitly, which command to invoke
  121. ;; - Renamed the package from "org-refer-by-number" to "org-reftable"
  122. ;;
  123. ;; [2011-12-10 Sa] to [2012-09-22 Sa] Version Version 1.2.0 to 1.5.0:
  124. ;; - New command "sort" to sort a buffer or region by reference number
  125. ;; - New commands "highlight" and "unhighlight" to mark references
  126. ;; - New command "head" to find a headline with a reference number
  127. ;; - New commands occur and multi-occur
  128. ;; - Started this Change Log
  129. ;;; Code:
  130. (require 'org-table)
  131. (require 'cl)
  132. (defcustom org-index-id nil
  133. "Id of the Org-mode node, which contains the index table."
  134. :group 'org
  135. :group 'org-index)
  136. ;; Version of this package
  137. (defvar org-index-version "4.2.1" "Version of `org-index', format is major.minor.bugfix, where \"major\" is a change in index-table and \"minor\" are new features.")
  138. ;; Variables to hold the configuration of the index table
  139. (defvar org-index--maxref nil "Maximum number from reference table (e.g. '153').")
  140. (defvar org-index--head nil "Any header before number (e.g. 'R').")
  141. (defvar org-index--tail nil "Tail after number (e.g. '}' or ')'.")
  142. (defvar org-index--numcols nil "Number of columns in index table.")
  143. (defvar org-index--ref-regex nil "Regular expression to match a reference.")
  144. (defvar org-index--ref-format nil "Format, that can print a reference.")
  145. (defvar org-index--columns nil "Columns of index-table.")
  146. (defvar org-index--special-columns nil "Columns with flags, that may appear only once.")
  147. (defvar org-index--flagged-columns nil "Columns with flags, that may appear multiple times.")
  148. (defvar org-index--buffer nil "Buffer of index table.")
  149. (defvar org-index--point nil "Position at start of headline of index table.")
  150. (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
  151. (defvar org-index--headings nil "Headlines of index-table as a string.")
  152. (defvar org-index--headings-visible nil "Visible part of headlines of index-table as a string.")
  153. (defvar org-index--keymap nil "Keymap for shortcuts for some commands of `org-index'. Filled and activated by `org-index-default-keybings'.")
  154. ;; Variables to hold context and state
  155. (defvar org-index--last-ref nil "Last reference created or visited.")
  156. (defvar org-index--category-before nil "Category of node before.")
  157. (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
  158. (defvar org-index--below-cursor nil "Word below cursor.")
  159. (defvar org-index--within-node nil "True, if we are within node of the index table.")
  160. (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
  161. (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
  162. (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
  163. (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
  164. (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
  165. (defvar org-index--last-sort nil "Last column, the index has been sorted after.")
  166. (defvar org-index--sort-timer nil "Timer to sort index in correct order.")
  167. (defvar org-index--aligned nil "Remember for this Emacs session, if table has been aligned at least once.")
  168. ;; static information for this program package
  169. (defconst org-index--commands '(occur add delete head ping enter ref help example sort multi-occur highlight maintain) "List of commands available.")
  170. (defconst org-index--required-flags '(sort) "Flags that are required.")
  171. (defconst org-index--single-flags '(sort point-on-add yank-after-add shift-ref-and-date-on-add) "Flags, that may only appear once; these can appear as special-columns.")
  172. (defconst org-index--multiple-flags '(edit-on-add) "Flags, that might appear multiple times.")
  173. (defconst org-index--all-flags (append org-index--single-flags org-index--multiple-flags) "All flags.")
  174. (defconst org-index--required-headings '(ref id created last-accessed count) "All required headings.")
  175. (defconst org-index--valid-headings (append org-index--required-headings '(keywords category level)) "All valid headings.")
  176. (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
  177. (defconst org-index--sort-idle-delay 300 "Delay in seconds after which buffer will sorted.")
  178. (defvar org-index-default-keybindings-list '(("a" . 'add) ("i " . nil) ("o" . 'occur) ("a" . 'add) ("d" . 'delete) ("h" . 'head) ("e" . 'enter) ("p." . 'ping) ("r" . 'ref) ("?" . 'help)) "One-letter short cuts for selected subcommands of `org-index', put in effect by `org-index-default-keybindings'")
  179. (defconst org-index--sample-flags
  180. "
  181. - columns-and-flags :: associate columns of index table with flags. Do not remove.
  182. - ref
  183. - yank-after-add
  184. - category
  185. - edit-on-add
  186. - keywords
  187. - edit-on-add
  188. - point-on-add
  189. - count
  190. - sort
  191. - last-accessed
  192. - created
  193. - id
  194. - all-columns-explained :: All columns of the index table and their meaning.
  195. - ref :: The reference number; will be generated automatically.
  196. - id :: id of the node, that this line represents
  197. - created :: When has this entry been created ?
  198. - last-accessed :: When has this entry been accessed last ?
  199. - count :: How many times has this entry been picked ?
  200. - keywords :: Optional column, suggested to keep a list of keywords,
  201. which may match your input during occur. While adding a line to your index,
  202. this column will be filled with the nodes heading.
  203. - category :: (optional) column to store the category of newly added nodes.
  204. - level :: Nesting level of node
  205. - Any name starting with a dot (`.') :: No predefined meaning,
  206. depends on its flags.
  207. - all-flags-explained :: All flags, that can be associated with columns.
  208. - sort :: Sort whole table according to this column.
  209. - yank-after-add :: This column will be yanked after picking this line during
  210. occur.
  211. - edit-on-add :: This field will be presented for editing, when adding
  212. a new line to your index.
  213. - point-on-add :: Point will land here, when adding a new line, e.g. with
  214. command ref.
  215. - shift-ref-and-date-on-add :: Remove leading reference and timestamp on add."
  216. "A sample string of flags.")
  217. (defmacro org-index--on (column value &rest body)
  218. "Execute the forms in BODY with point on index line whose COLUMN is VALUE.
  219. The value returned is the value of the last form in BODY or nil,
  220. if VALUE cannot be found."
  221. (declare (indent 2) (debug t))
  222. (let ((pointvar (make-symbol "point")) ; avoid clash with same-named variables in body
  223. (foundvar (make-symbol "found"))
  224. (retvar (make-symbol "ret")))
  225. `(save-current-buffer
  226. (set-buffer org-index--buffer)
  227. (setq ,pointvar (point))
  228. (setq ,foundvar nil)
  229. (setq ,retvar nil)
  230. (setq ,foundvar (org-index--go ,column ,value))
  231. (when ,foundvar
  232. (setq ,retvar (progn ,@body)))
  233. (goto-char ,pointvar)
  234. ,retvar)))
  235. (defun org-index (&optional command search-ref arg)
  236. "Fast search for selected org headings and things outside of org.
  237. This package creates and updates an index table of headings or
  238. keywords, references and ids, where each line points to a heading
  239. within org or references something outside. This table is sorted by
  240. usage count, so that frequently used lines appear among the first
  241. search results.
  242. References are essentially small numbers (e.g. 'R237' or '--455--'), as
  243. created by this package; they are well suited to be used outside of
  244. org, e.g. in folder names, issue trackers or on printed documents.
  245. On first invocation `org-index' will guide you to create a dedicated node
  246. for its index table and its configuration flags.
  247. For basic usage, subcommands 'add' and 'occur' are most important.
  248. This is version 4.2.1 of org-index.el.
  249. \\<org-mode-map>
  250. The function `org-index' operates on a dedicated table, the index
  251. table, which lives within its own Org-mode node. The table and
  252. its containing node will be created, when you first invoke
  253. `org-index'. The node also contains a commented list, describing
  254. the columns of the index table and their associated flags. The
  255. node is found through its id, which is stored within the variable
  256. `org-index-id'.
  257. The function `org-index' is the only interactive function of this
  258. package and its main entry point; it will present you with a list
  259. of subcommands to choose from:
  260. occur: Incremental search, that shows matching lines from the
  261. index table. It is updated after every keystroke. You may
  262. enter a list of words seperated by space or comma (`,'), to
  263. select lines that contain all of the given words.
  264. add: Add the current node to your index, so that it can be
  265. found through the subcommand \"occur\". Update index,
  266. if node has already been present.
  267. delete: Delete the current node from your index.
  268. head: Ask for a reference number and search for this heading.
  269. enter: Enter index table and maybe go to a specific reference;
  270. use `org-mark-ring-goto' (\\[org-mark-ring-goto]) to go back.
  271. ping: Echo line from index table for current node or first of
  272. its ancestor from index.
  273. ref: Create a new reference.
  274. help: Show this text.
  275. example: Create a temporary index, that will not be saved, but
  276. may serve as an example.
  277. sort: Sort lines in index, in region or buffer by contained
  278. reference, or sort index by count, reference or last access.
  279. multi-occur: Apply Emacs standard `multi-occur' operation on all
  280. `org-mode' buffers to search for the given reference.
  281. highlight: Highlight or unhiglight references in active region or buffer.
  282. Call with prefix argument (`C-u') to remove highlights.
  283. maintain: Offers some choices to check, update or fix your index.
  284. If you invoke `org-index' for the first time, an assistant will be
  285. invoked, that helps you to create your own, commented index.
  286. Use `org-index-default-keybindings' to establish convenient
  287. keyboard shortcuts.
  288. See the commented list of flags within your index node for ways to
  289. modify the behaviour of org-index.
  290. A numeric prefix argument is used as a reference number for
  291. commands, that need one (e.g. 'head').
  292. Optional arguments for use from elisp: COMMAND is a symbol naming
  293. the command to execute. SEARCH-REF specifies a reference to
  294. search for, if needed. ARG allows passing in a prefix argument
  295. as in interactive calls."
  296. (interactive "i\ni\nP")
  297. (let (search-id ; id to search for
  298. sort-what ; sort what ?
  299. kill-new-text ; text that will be appended to kill ring
  300. message-text) ; text that will be issued as an explanation
  301. ;;
  302. ;; Initialize and parse
  303. ;;
  304. ;; creates index table, if necessary
  305. (org-index--verify-id)
  306. ;; Get configuration of index table
  307. (org-index--parse-table)
  308. ;; store context information
  309. (org-index--retrieve-context)
  310. ;;
  311. ;; Arrange for proper sorting of index
  312. ;;
  313. ;; lets assume, that it has been sorted this way (we try hard to make sure)
  314. (unless org-index--last-sort (setq org-index--last-sort (org-index--special-column 'sort)))
  315. ;; rearrange for index beeing sorted into default sort order after 300 secs of idle time
  316. (unless org-index--sort-timer
  317. (setq org-index--sort-timer
  318. (run-with-idle-timer org-index--sort-idle-delay t 'org-index--sort-silent)))
  319. ;;
  320. ;; Find out, what we are supposed to do
  321. ;;
  322. ;; check or read command
  323. (if command
  324. (unless (memq command org-index--commands)
  325. (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
  326. command (mapconcat 'symbol-name org-index--commands ",")))
  327. (setq command (intern (org-completing-read
  328. "Please choose: "
  329. (mapcar 'symbol-name org-index--commands)))))
  330. ;;
  331. ;; Get search string, if required; process possible sources one after
  332. ;; another (lisp argument, prefix argumen, user input).
  333. ;;
  334. ;; Try prefix, if no lisp argument given
  335. (if (and (not search-ref)
  336. (numberp arg))
  337. (setq search-ref (format "%s%d%s" org-index--head arg org-index--tail)))
  338. ;; These actions really need a search string and may even prompt for it
  339. (when (memq command '(enter head multi-occur))
  340. ;; search from surrounding text ?
  341. (unless search-ref
  342. (if org-index--within-node
  343. (if (org-at-table-p)
  344. (setq search-ref (org-index--get-or-set-field 'ref)))
  345. (if (and org-index--below-cursor
  346. (string-match (concat "\\(" org-index--ref-regex "\\)")
  347. org-index--below-cursor))
  348. (setq search-ref (match-string 1 org-index--below-cursor)))))
  349. ;; If we still do not have a search string, ask user explicitly
  350. (unless search-ref
  351. (if (eq command 'enter)
  352. (let ((r (org-index--read-search-for-enter)))
  353. (setq search-ref (car r))
  354. (setq search-id (cdr r)))
  355. (setq search-ref (read-from-minibuffer "Search reference number: "))))
  356. ;; Clean up search string
  357. (when search-ref
  358. (setq search-ref (org-trim search-ref))
  359. (if (string-match "^[0-9]+$" search-ref)
  360. (setq search-ref (concat org-index--head search-ref org-index--tail)))
  361. (if (string= search-ref "") (setq search-ref nil)))
  362. (if (and (not search-ref)
  363. (not (eq command 'enter)))
  364. (error "Command %s needs a reference number" command)))
  365. ;;
  366. ;; Command sort needs to know in advance, what to sort for
  367. ;;
  368. (when (eq command 'sort)
  369. (setq sort-what (intern (org-completing-read "You may sort:\n - index : your index table by various columns\n - region : the active region by contained reference\n - buffer : the whole current buffer\nPlease choose what to sort: " (list "index" "region" "buffer") nil t))))
  370. ;;
  371. ;; Enter table
  372. ;;
  373. ;; Arrange for beeing able to return
  374. (when (and (memq command '(occur head enter ref example sort maintain))
  375. (not (string= (buffer-name) org-index--occur-buffer-name)))
  376. (org-mark-ring-push))
  377. ;; These commands will leave user in index table after they are finished
  378. (when (or (memq command '(enter ref maintain))
  379. (and (eq command 'sort)
  380. (eq sort-what 'index)))
  381. (pop-to-buffer-same-window org-index--buffer)
  382. (goto-char org-index--point)
  383. (org-index--unfold-buffer))
  384. ;;
  385. ;; Actually do, what is requested
  386. ;;
  387. (cond
  388. ((eq command 'help)
  389. ;; bring up help-buffer for this function
  390. (describe-function 'org-index))
  391. ((eq command 'multi-occur)
  392. ;; Construct list of all org-buffers
  393. (let (buff org-buffers)
  394. (dolist (buff (buffer-list))
  395. (set-buffer buff)
  396. (if (string= major-mode "org-mode")
  397. (setq org-buffers (cons buff org-buffers))))
  398. ;; Do multi-occur
  399. (multi-occur org-buffers (org-index--make-guarded-search search-ref))
  400. ;; Present results
  401. (if (get-buffer "*Occur*")
  402. (progn
  403. (setq message-text (format "multi-occur for '%s'" search-ref))
  404. (other-window 1)
  405. (toggle-truncate-lines 1))
  406. (setq message-text (format "Did not find '%s'" search-ref)))))
  407. ((eq command 'add)
  408. (let ((r (org-index--do-add-or-update)))
  409. (setq message-text (car r))
  410. (setq kill-new-text (cdr r))))
  411. ((eq command 'delete)
  412. (setq message-text (org-index--do-delete)))
  413. ((eq command 'head)
  414. (if (and org-index--within-node
  415. (org-at-table-p))
  416. (setq search-id (org-index--get-or-set-field 'id)))
  417. (setq search-id (or search-id (org-index--id-from-ref search-ref)))
  418. (setq message-text
  419. (if search-id
  420. (org-index--do-head search-ref search-id)
  421. (message "Current line has no id."))))
  422. ((eq command 'enter)
  423. (goto-char org-index--below-hline)
  424. (setq message-text
  425. (if search-ref
  426. (if (org-index--go 'ref search-ref)
  427. (progn
  428. (org-index--update-current-line)
  429. (org-table-goto-column (org-index--column-num 'ref))
  430. (format "Found index line '%s'" search-ref))
  431. (format "Did not find index line with reference '%s'" search-ref))
  432. (if search-id
  433. (if (org-index--go 'id search-id)
  434. (progn
  435. (org-index--update-current-line)
  436. (org-table-goto-column (org-index--column-num 'ref))
  437. (format "Found index line '%s'" (org-index--get-or-set-field 'ref)))
  438. (format "Did not find index line with id '%s'" search-id))
  439. ;; simply go into table
  440. (setq message-text "At index table"))))
  441. (recenter))
  442. ((eq command 'ping)
  443. (let ((moved-up 0) id info reached-top)
  444. (unless (string= major-mode "org-mode") (error "No node at point"))
  445. ;; take id from current node or reference
  446. (setq id (if search-ref
  447. (org-index--id-from-ref search-ref)
  448. (org-id-get)))
  449. ;; move up until we find a node in index
  450. (save-excursion
  451. (outline-back-to-heading)
  452. (while (not (or info
  453. reached-top))
  454. (if id
  455. (setq info (org-index--on 'id id
  456. (mapcar (lambda (x) (org-index--get-or-set-field x))
  457. (list 'ref 'count 'created 'last-accessed 'category 'keywords 'ref)))))
  458. (setq reached-top (= (org-outline-level) 1))
  459. (unless (or info
  460. reached-top)
  461. (outline-up-heading 1 t)
  462. (incf moved-up))
  463. (setq id (org-id-get))))
  464. (if info
  465. (progn
  466. (setq message-text
  467. (apply 'format
  468. (append (list "'%s'%shas been accessed %s times between %s and %s; category is '%s', keywords are '%s'"
  469. (pop info)
  470. (if (> moved-up 0) (format " (parent node, %d level up) " moved-up) " "))
  471. info)))
  472. (setq kill-new-text (car (last info))))
  473. (setq message-text "Neither this node nor any of its parents is part of index"))))
  474. ((eq command 'occur)
  475. (set-buffer org-index--buffer)
  476. (org-index--do-occur))
  477. ((eq command 'ref)
  478. (let (new)
  479. ;; add a new row
  480. (setq new (org-index--create-new-line))
  481. ;; fill special columns with standard values
  482. (org-table-goto-column (org-index--column-num 'ref))
  483. (insert new)
  484. (setq org-index--last-ref new)
  485. ;; goto point-field or first empty one or first field
  486. (if (org-index--special-column 'point-on-add)
  487. (org-table-goto-column (org-index--column-num (org-index--special-column 'point-on-add)))
  488. (unless (catch 'empty
  489. (dotimes (col org-index--numcols)
  490. (org-table-goto-column (+ col 1))
  491. (if (string= (org-trim (org-table-get-field)) "")
  492. (throw 'empty t))))
  493. ;; none found, goto first
  494. (org-table-goto-column 1)))
  495. (if org-index--active-region (setq kill-new-text org-index--active-region))
  496. (setq message-text (format "Adding a new row with ref '%s'" new))))
  497. ((eq command 'sort)
  498. (let ((columns (list "ref" "count" "created" "last-accessed" "id"))
  499. sort groups-and-counts)
  500. (cond
  501. ((eq sort-what 'index)
  502. (setq sort
  503. (intern
  504. (org-icompleting-read
  505. "Please choose column to sort index table: "
  506. (append (copy-list columns) (list "group-by"))
  507. nil t nil nil (symbol-name (org-index--special-column 'sort)))))
  508. (when (eq sort 'group-by)
  509. (setq sort
  510. (intern
  511. (org-icompleting-read
  512. "Please choose column to group index table by: "
  513. columns
  514. nil t nil nil (symbol-name (org-index--special-column 'sort)))))
  515. (setq groups-and-counts (org-index--collect-sort-groups sort)))
  516. (org-index--do-sort-index sort (first groups-and-counts))
  517. (org-table-goto-column (org-index--column-num sort))
  518. ;; When saving index, it should again be sorted correctly
  519. (with-current-buffer org-index--buffer
  520. (add-hook 'before-save-hook 'org-index--sort-silent t))
  521. (setq message-text
  522. (format
  523. (concat "Your index has been sorted temporarily by %s and will be sorted again by %s after %d seconds of idle time"
  524. (if groups-and-counts
  525. "; %d groups with equal %s and a total of %d lines have been found"
  526. ""))
  527. (symbol-name sort)
  528. (org-index--special-column 'sort)
  529. org-index--sort-idle-delay
  530. (second groups-and-counts)
  531. (symbol-name sort)
  532. (third groups-and-counts))))
  533. ((memq sort-what '(region buffer))
  534. (org-index--do-sort-lines sort-what)
  535. (setq message-text (format "Sorted %s by contained references" sort-what))))))
  536. ((eq command 'highlight)
  537. (let ((where "buffer"))
  538. (save-excursion
  539. (save-restriction
  540. (when (and transient-mark-mode
  541. mark-active)
  542. (narrow-to-region (region-beginning) (region-end))
  543. (setq where "region"))
  544. (if arg
  545. (progn
  546. (unhighlight-regexp org-index--ref-regex)
  547. (setq message-text (format "Removed highlights for references in %s" where)))
  548. (highlight-regexp org-index--ref-regex 'isearch)
  549. (setq message-text (format "Highlighted references in %s" where)))))))
  550. ((eq command 'maintain)
  551. (setq message-text (org-index--do-maintain)))
  552. ((eq command 'example)
  553. (if (y-or-n-p "This assistant will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
  554. (org-index--create-index t)))
  555. (t (error "Unknown subcommand '%s'" command)))
  556. ;; tell, what we have done and what can be yanked
  557. (if kill-new-text (setq kill-new-text
  558. (substring-no-properties kill-new-text)))
  559. (if (string= kill-new-text "") (setq kill-new-text nil))
  560. (let ((m (concat
  561. message-text
  562. (if (and message-text kill-new-text)
  563. " and r"
  564. (if kill-new-text "R" ""))
  565. (if kill-new-text (format "eady to yank '%s'." kill-new-text) (if message-text "." "")))))
  566. (unless (string= m "")
  567. (message m)
  568. (setq org-index--message-text m)))
  569. (if kill-new-text (kill-new kill-new-text))))
  570. (defun org-index-default-keybindings (&optional prefix)
  571. "Set default keybindings for `org-index'.
  572. Invoke subcommands of org index with a single key
  573. sequence. Establish the common prefix key 'C-c i' which should be
  574. followed by the first letter of a subcommand.
  575. The ist of letters and subcommands is specified in within
  576. `org-index-default-keybindings-list'.
  577. See `org-index' for a description of all subcommands.
  578. Optional argument PREFIX specifies common prefix, defaults to 'C-c i'"
  579. (interactive)
  580. (define-prefix-command 'org-index--keymap)
  581. ;; prefix command
  582. (global-set-key (kbd (or prefix "C-c i")) 'org-index--keymap)
  583. ;; loop over subcommands
  584. (mapcar
  585. (lambda (x)
  586. ;; loop over letters, that invoke the same subcommand
  587. (mapcar (lambda (c)
  588. (define-key org-index--keymap (kbd (char-to-string c))
  589. `(lambda (arg) (interactive "P")
  590. (message nil)
  591. (org-index ,(cdr x) nil arg))))
  592. (car x)))
  593. org-index-default-keybindings-list))
  594. (defun org-index-new-line (&rest keys-values)
  595. "Create a new line within the index table, returning its reference.
  596. The function takes a varying number of argument pairs; each pair
  597. is a symbol for an existing column heading followed by its value.
  598. The return value is the new reference.
  599. Example:
  600. (message \"Created reference %s\"
  601. (org-index-new-line 'keywords \"foo bar\" 'category \"baz\"))
  602. Optional argument KEYS-VALUES specifies content of new line."
  603. (org-index--verify-id)
  604. (org-index--parse-table)
  605. (car (apply 'org-index--do-new-line keys-values)))
  606. (defun org-index--do-new-line (&rest keys-values)
  607. "Do the work for `org-index-new-line'.
  608. Optional argument KEYS-VALUES specifies content of new line."
  609. (save-excursion
  610. (org-index--retrieve-context)
  611. (with-current-buffer org-index--buffer
  612. (goto-char org-index--point)
  613. ;; check arguments early; they might come from lisp-user
  614. (let ((kvs keys-values)
  615. k v)
  616. (while kvs
  617. (setq k (car kvs))
  618. (setq v (cadr kvs))
  619. (if (eq k 'ref)
  620. (unless (memq v '(t nil))
  621. (error "Column 'ref' accepts only \"t\" or \"nil\""))
  622. (if (or (not (symbolp k))
  623. (and (symbolp v) (not (eq v t)) (not (eq v nil))))
  624. (error "Arguments must be alternation of key and value")))
  625. (unless (org-index--column-num k)
  626. (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
  627. (setq kvs (cddr kvs))))
  628. (let (ref yank)
  629. ;; create new line
  630. (setq ref (org-index--create-new-line))
  631. (plist-put keys-values 'ref ref)
  632. ;; fill columns
  633. (let ((kvs keys-values)
  634. k v n)
  635. (while kvs
  636. (setq k (car kvs))
  637. (setq v (cadr kvs))
  638. (org-table-goto-column (org-index--column-num k))
  639. (insert (org-trim v))
  640. (setq kvs (cddr kvs))))
  641. ;; align and fontify line
  642. (org-index--promote-current-line)
  643. (org-index--align-and-fontify-current-line)
  644. ;; get column to yank
  645. (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add)))
  646. (cons ref yank)))))
  647. (defun org-index-get-line (column value)
  648. "Retrieve an existing line within the index table by ref or id.
  649. Return its contents as a property list.
  650. The function `plist-get' may be used to retrieve specific elements
  651. from the result.
  652. Example:
  653. (plist-get (org-index-get-line 'ref \"R12\") 'count)
  654. retrieves the value of the count-column for reference number 12.
  655. Argument COLUMN is a symbol, either ref or id,
  656. argument VALUE specifies the value to search for."
  657. ;; check arguments
  658. (unless (memq column '(ref id))
  659. (error "Argument column can only be 'ref' or 'id'"))
  660. (unless value
  661. (error "Need a value to search for"))
  662. (org-index--verify-id)
  663. (org-index--parse-table)
  664. (org-index--get-line column value))
  665. (defun org-index--get-line (column value)
  666. "Find a line by ID, return its contents.
  667. Argument COLUMN and VALUE specify line to get."
  668. (let (content)
  669. (org-index--on
  670. column value
  671. (mapc (lambda (x)
  672. (if (and (numberp (cdr x))
  673. (> (cdr x) 0))
  674. (setq content (cons (car x) (cons (or (org-index--get-or-set-field (car x)) "") content)))))
  675. (reverse org-index--columns)))
  676. content))
  677. (defun org-index--delete-line (id)
  678. "Delete a line specified by ID."
  679. (let (content)
  680. (org-index--on
  681. 'id id
  682. (let ((start (line-beginning-position)))
  683. (beginning-of-line)
  684. (forward-line)
  685. (delete-region start (point))
  686. t))))
  687. (defun org-index--ref-from-id (id)
  688. "Get reference from line ID."
  689. (org-index--on 'id id (org-index--get-or-set-field 'ref)))
  690. (defun org-index--id-from-ref (ref)
  691. "Get id from line REF."
  692. (org-index--on 'ref ref (org-index--get-or-set-field 'id)))
  693. (defun org-index--read-search-for-enter ()
  694. "Special input routine for command enter."
  695. ;; Accept single char commands or switch to reading a sequence of digits
  696. (let (char prompt search-ref search-id)
  697. ;; start with short prompt but give more help on next iteration
  698. (setq prompt "Please specify, where to go in index (0-9.,space,backspace,return or ? for help): ")
  699. ;; read one character
  700. (while (not (memq char (append (number-sequence ?0 ?9) (list ?\d ?\b ?\r ?\j ?\s ?.))))
  701. (setq char (read-char prompt))
  702. (setq prompt "Go to index table and specific position. Digits specify a reference number to got to, <space> goes to top of index, <backspace> or <delete> to last line created and <return> or `.' to index line of current node. Please choose: "))
  703. (if (memq char (number-sequence ?0 ?9))
  704. ;; read rest of digits
  705. (setq search-ref (read-from-minibuffer "Search reference number: " (char-to-string char))))
  706. ;; decode single chars
  707. (if (memq char '(?\r ?\n ?.)) (setq search-id (org-id-get)))
  708. (if (memq char '(?\d ?\b)) (setq search-ref (number-to-string org-index--maxref)))
  709. (cons search-ref search-id)))
  710. (defun org-index--verify-id ()
  711. "Check, that we have a valid id."
  712. ;; Check id
  713. (unless org-index-id
  714. (let ((answer (org-completing-read "Cannot find an index (org-index-id is not set). You may:\n - read-help : to learn more about org-index\n - create-index : invoke an assistant to create an initial index\nPlease choose: " (list "read-help" "create-index") nil t nil nil "read-help")))
  715. (if (string= "create-index" answer)
  716. (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet.")
  717. (describe-function 'org-index))))
  718. ;; Find node
  719. (let (marker)
  720. (setq marker (org-id-find org-index-id 'marker))
  721. (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
  722. ; Try again with new node
  723. (setq marker (org-id-find org-index-id 'marker))
  724. (unless marker (error "Could not create node"))
  725. (setq org-index--buffer (marker-buffer marker)
  726. org-index--point (marker-position marker))
  727. (move-marker marker nil)))
  728. (defun org-index--retrieve-context ()
  729. "Collect context information before starting with command."
  730. ;; Get the content of the active region or the word under cursor
  731. (setq org-index--active-region
  732. (if (and transient-mark-mode mark-active)
  733. (buffer-substring (region-beginning) (region-end))
  734. nil))
  735. (setq org-index--below-cursor (thing-at-point 'symbol))
  736. ;; get category of current node
  737. (setq org-index--category-before
  738. (save-excursion ; workaround: org-get-category does not give category when at end of buffer
  739. (beginning-of-line)
  740. (org-get-category (point) t)))
  741. ;; Find out, if we are within index table or not
  742. (setq org-index--within-node (string= (org-id-get) org-index-id)))
  743. (defun org-index--parse-table ()
  744. "Parse content of index table."
  745. (let (ref-field
  746. id-field
  747. initial-point
  748. end-of-headings
  749. start-of-headings)
  750. (with-current-buffer org-index--buffer
  751. (setq org-index--maxref 0)
  752. (setq initial-point (point))
  753. (org-index--go-below-hline)
  754. ;; align and fontify table once for this emacs session
  755. (unless org-index--aligned
  756. (org-table-align) ; needs to happen before fontification to be effective ?
  757. (let ((is-modified (buffer-modified-p))
  758. (below (point)))
  759. (while (org-at-table-p)
  760. (forward-line))
  761. (font-lock-fontify-region below (point))
  762. (org-index--go-below-hline)
  763. (setq org-index--aligned t)
  764. (set-buffer-modified-p is-modified)))
  765. (org-index--go-below-hline)
  766. (setq org-index--below-hline (point-marker))
  767. (beginning-of-line)
  768. ;; get headings to display during occur
  769. (setq end-of-headings (point))
  770. (while (org-at-table-p) (forward-line -1))
  771. (forward-line)
  772. (setq start-of-headings (point))
  773. (setq org-index--headings-visible (substring-no-properties (org-index--copy-visible start-of-headings end-of-headings)))
  774. (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
  775. ;; count columns
  776. (org-table-goto-column 100)
  777. (setq org-index--numcols (- (org-table-current-column) 1))
  778. ;; go to top of table
  779. (while (org-at-table-p)
  780. (forward-line -1))
  781. (forward-line)
  782. ;; parse line of headings
  783. (org-index--parse-headings)
  784. ;; parse list of flags
  785. (goto-char org-index--point)
  786. (org-index--parse-flags)
  787. ;; Retrieve any decorations around the number within the first nonempty ref-field
  788. (goto-char org-index--below-hline)
  789. (while (and (org-at-table-p)
  790. (not (setq ref-field (org-index--get-or-set-field 'ref))))
  791. (forward-line))
  792. ;; Some Checking
  793. (unless ref-field
  794. (org-index--report-index-error "Reference column is empty"))
  795. (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
  796. (org-index--report-index-error
  797. "First reference in index table ('%s') does not contain a number" ref-field))
  798. ;; These are the decorations used within the first ref of index
  799. (setq org-index--head (match-string 1 ref-field))
  800. (setq org-index--tail (match-string 3 ref-field))
  801. (setq org-index--ref-regex (concat (regexp-quote org-index--head)
  802. "\\([0-9]+\\)"
  803. (regexp-quote org-index--tail)))
  804. (setq org-index--ref-format (concat org-index--head "%d" org-index--tail))
  805. ;; Go through table to find maximum number and do some checking
  806. (let ((ref 0))
  807. (while (org-at-table-p)
  808. (setq ref-field (org-index--get-or-set-field 'ref))
  809. (setq id-field (org-index--get-or-set-field 'id))
  810. (when (and (not ref-field)
  811. (not id-field))
  812. (kill-whole-line)
  813. (message "Removing line from index-table with both ref and id empty"))
  814. (if ref-field
  815. (if (string-match org-index--ref-regex ref-field)
  816. ;; grab number
  817. (setq ref (string-to-number (match-string 1 ref-field)))
  818. (kill-whole-line)
  819. (message "Removing line from index-table whose ref does not contain a number")))
  820. ;; check, if higher ref
  821. (if (> ref org-index--maxref) (setq org-index--maxref ref))
  822. (forward-line 1)))
  823. ;; go back to initial position
  824. (goto-char initial-point))))
  825. (defun org-index--do-maintain ()
  826. "Choose among and perform some tasks to maintain index."
  827. (let ((check-what) (max-mini-window-height 1.0) message-text)
  828. (setq check-what (intern (org-completing-read "These checks and fixes are available:\n - statistics : compute statistics about index table\n - check : check ids by visiting their nodes\n - duplicates : check index for duplicate rows (any column)\n - clean : remove obsolete property org-index-id\n - update : update content of index lines, with an id \nPlease choose: " (list "statistics" "check" "duplicates" "clean" "update") nil t nil nil "statistics")))
  829. (message nil)
  830. (cond
  831. ((eq check-what 'check)
  832. (setq message-text (or (org-index--check-ids)
  833. "No problems found")))
  834. ((eq check-what 'statistics)
  835. (setq message-text (org-index--do-statistics)))
  836. ((eq check-what 'duplicates)
  837. (setq message-text "Finding duplcates can be done by sorting your index appropriately: Choose 'group-by' and select a column; rows will then be sorted together, if they have the same value within the coosen column."))
  838. ((eq check-what 'clean)
  839. (let ((lines 0))
  840. (org-map-entries
  841. (lambda ()
  842. (when (org-entry-get (point) "org-index-ref")
  843. (incf lines)
  844. (org-entry-delete (point) "org-index-ref")))
  845. nil 'agenda)
  846. (setq message-text (format "Removed property 'org-index-ref' from %d lines" lines))))
  847. ((eq check-what 'update)
  848. (if (y-or-n-p "Updating your index will overwrite certain columns with content from the associated heading and category. If unsure, you may try this for a single, already existing line of your index by doing `add' from within your index. Are you SURE to proceed for ALL INDEX LINES ? ")
  849. (setq message-text (org-index--update-all-lines))
  850. (setq message-text "Canceled."))))
  851. message-text))
  852. (defun org-index--do-sort-index (sort &optional groups)
  853. "Sort index table according to SORT, optinally with GROUPS."
  854. (let ((is-modified (buffer-modified-p))
  855. top
  856. bottom
  857. ref-field
  858. count-field)
  859. (unless buffer-read-only
  860. (message "Sorting table for %s..." (symbol-name sort))
  861. (undo-boundary)
  862. (let ((message-log-max nil)) ; we have just issued a message, dont need those of sort-subr
  863. ;; get boundaries of table
  864. (goto-char org-index--below-hline)
  865. (forward-line 0)
  866. (setq top (point))
  867. (while (org-at-table-p) (forward-line))
  868. ;; kill all empty rows at bottom
  869. (while (progn
  870. (forward-line -1)
  871. (org-table-goto-column 1)
  872. (and
  873. (not (org-index--get-or-set-field 'ref))
  874. (not (org-index--get-or-set-field 'id))))
  875. (org-table-kill-row))
  876. (forward-line 1)
  877. (setq bottom (point))
  878. ;; sort lines
  879. (save-restriction
  880. (narrow-to-region top bottom)
  881. (goto-char top)
  882. (sort-subr t
  883. 'forward-line
  884. 'end-of-line
  885. (lambda ()
  886. (concat
  887. (if groups
  888. (format "%06d-" (cdr (assoc (org-index--get-or-set-field sort) groups)))
  889. "")
  890. (org-index--get-sort-key sort t)))
  891. nil
  892. 'string<)
  893. (goto-char (point-min))
  894. ;; restore modification state
  895. (set-buffer-modified-p is-modified)))
  896. (setq org-index--last-sort sort))))
  897. (defun org-index--collect-sort-groups (sort)
  898. "Collect groups to SORT for."
  899. (let ((count-groups 0) (count-lines 0)
  900. groups key key-value)
  901. (org-index--on
  902. nil nil
  903. (while (org-at-table-p)
  904. (setq key (org-index--get-or-set-field sort))
  905. (setq key-value (assoc key groups))
  906. (if key-value
  907. (progn
  908. (incf (cdr key-value)))
  909. (setq groups (cons (cons key 1) groups)))
  910. (forward-line)))
  911. (mapc (lambda (x) (when (> (cdr x) 1)
  912. (incf count-groups)
  913. (incf count-lines (cdr x))))
  914. groups)
  915. (list groups count-groups count-lines)))
  916. (defun org-index--do-sort-lines (what)
  917. "Sort lines in WHAT according to contained reference."
  918. (save-restriction
  919. (cond
  920. ((eq what 'region)
  921. (if (region-active-p)
  922. (narrow-to-region (region-beginning) (region-end))
  923. (error "No active region, cannot sort")))
  924. ((eq what 'buffer)
  925. (unless (y-or-n-p "Sort whole current buffer ? ")
  926. (error "Canceled"))
  927. (narrow-to-region (point-min) (point-max))))
  928. (goto-char (point-min))
  929. (sort-subr nil 'forward-line 'end-of-line
  930. (lambda ()
  931. (if (looking-at (concat ".*"
  932. (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
  933. (string-to-number (match-string 1))
  934. 0)))))
  935. (defun org-index--go-below-hline ()
  936. "Move below hline in index-table."
  937. (let ((count 0)
  938. (errstring (format "index table within node %s" org-index-id)))
  939. (goto-char org-index--point)
  940. ;; go to heading of node
  941. (while (not (org-at-heading-p)) (forward-line -1))
  942. (forward-line 1)
  943. ;; go to first table, but make sure we do not get into another node
  944. (while (and (not (org-at-table-p))
  945. (not (org-at-heading-p))
  946. (not (eobp)))
  947. (forward-line))
  948. ;; check, if there really is a table
  949. (unless (org-at-table-p)
  950. (org-index--create-missing-index "Cannot find %s." errstring))
  951. ;; go just after hline
  952. (while (and (not (org-at-table-hline-p))
  953. (org-at-table-p))
  954. (forward-line))
  955. (forward-line)
  956. ;; and check
  957. (unless (org-at-table-p)
  958. (org-index--report-index-error "Cannot find a hline within %s" errstring))
  959. (org-table-goto-column 1)))
  960. (defun org-index--parse-headings ()
  961. "Parse headings of index table."
  962. (let (field ;; field content
  963. field-symbol ;; and as a symbol
  964. found)
  965. (setq org-index--columns nil)
  966. ;; For each column
  967. (dotimes (col org-index--numcols)
  968. (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
  969. (if (string= field "")
  970. (error "Heading of column cannot be empty"))
  971. (if (and (not (string= (substring field 0 1) "."))
  972. (not (member (intern field) org-index--valid-headings)))
  973. (if (string= field "link")
  974. ;; Ask user to migrate his index to new version (since [2015-02-11 Mi])
  975. (progn
  976. ;; pop to index buffer
  977. (pop-to-buffer-same-window org-index--buffer)
  978. (goto-char org-index--below-hline)
  979. (org-reveal t)
  980. ;; go to column
  981. (while (org-at-table-p)
  982. (forward-line -1))
  983. (forward-line)
  984. (org-table-goto-column (+ 1 col))
  985. (error "Column 'link' should be named 'id' with recent versions of org-index,\nplease adjust your table (cursor is already positioned right)"))
  986. (error "Column name '%s' is not a valid heading (custom headings may start with a dot, e.g. '.foo')" field)))
  987. (setq field-symbol (intern field))
  988. ;; check if heading has already appeared
  989. (if (assoc field-symbol org-index--columns)
  990. (org-index--report-index-error
  991. "'%s' appears two times as column heading" (downcase field))
  992. ;; add it to list at front, reverse later
  993. (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
  994. (setq org-index--columns (reverse org-index--columns))
  995. ;; check if all necessary headings have appeared
  996. (mapc (lambda (head)
  997. (unless (cdr (assoc head org-index--columns))
  998. (org-index--report-index-error "No column has heading '%s'" head)))
  999. org-index--required-headings))
  1000. (defun org-index--parse-flags ()
  1001. "Parse list of flags in index table."
  1002. (let (parent parent-is-comment child)
  1003. ;; reset configuration variables
  1004. (setq org-index--special-columns nil)
  1005. (setq org-index--flagged-columns nil)
  1006. (org-index--goto-list "columns-and-flags" t)
  1007. (forward-line 1)
  1008. ;; outer loop over columns
  1009. (while (and (setq parent (org-index--parse-list-item))
  1010. parent
  1011. (> (cdr (assoc :indent parent)) 0))
  1012. (setq parent-is-comment (member (cdr (assoc :text parent)) '("all-columns-explained" "all-flags-explained")))
  1013. ;; check, that we have a valid heading
  1014. (unless (or parent-is-comment
  1015. (assoc (cdr (assoc :sym parent)) org-index--columns))
  1016. (when (string= "link" (cdr (assoc :text parent)))
  1017. (pop-to-buffer-same-window org-index--buffer)
  1018. (org-reveal t)
  1019. (error "Flag 'link' should be named 'id' with recent versions of org-index,\nplease adjust this flag (cursor is already positioned right)"))
  1020. (org-index--report-index-error "'%s' appears within flags, but not as a index column. " (cdr (assoc :text parent))))
  1021. ;; inner loop over children
  1022. (while (and (forward-line 1)
  1023. (setq child (org-index--parse-list-item))
  1024. child
  1025. (> (cdr (assoc :indent child))
  1026. (cdr (assoc :indent parent))))
  1027. (unless parent-is-comment
  1028. ;; check, that we have a valid flag
  1029. (unless (memq (cdr (assoc :sym child)) org-index--all-flags)
  1030. (org-index--report-index-error "'%s' is not a valid flag" (cdr (assoc :text child))))
  1031. ;; process flag with respect to current index-column
  1032. (if (memq (cdr (assoc :sym child)) org-index--single-flags)
  1033. ;; Check, that none of org-index--single-flags appears twice
  1034. (if (assoc (cdr (assoc :sym child)) org-index--special-columns)
  1035. (org-index--report-index-error
  1036. "More than one column is marked with flag '%s'" (cdr (assoc :text child)))
  1037. ;; add it to list
  1038. (setq org-index--special-columns (cons (cons (cdr (assoc :sym child)) (cdr (assoc :sym parent)))
  1039. org-index--special-columns))))
  1040. ;; all flags are stored in org-index--flagged-columns
  1041. (let ((l (assoc (cdr (assoc :sym child)) org-index--flagged-columns))) ;; list of flag and columns, that carry this flag
  1042. (unless l
  1043. ;; no list of columns with this flag is present, create one
  1044. (setq org-index--flagged-columns
  1045. (cons (cons (cdr (assoc :sym child)) nil)
  1046. org-index--flagged-columns))
  1047. (setq l (car org-index--flagged-columns)))
  1048. ;; prepend this column to list of columns with this flag
  1049. (setcdr l (cons (cdr (assoc :sym parent)) (cdr l)))))))
  1050. ;; check, that all needed flags have been specified
  1051. (mapc (lambda (x)
  1052. (unless (assoc x org-index--special-columns)
  1053. (org-index--report-index-error "Required flag '%s' does not appear" (substring (symbol-name x) 1))))
  1054. org-index--required-flags)))
  1055. (defun org-index--goto-list (name &optional required non-top)
  1056. "Goto list NAME (maybe NON-TOP Level) in index node, err if REQUIRED list is not present."
  1057. (goto-char org-index--point)
  1058. ;; go to heading of node
  1059. (while (not (org-at-heading-p)) (forward-line -1))
  1060. (forward-line 1)
  1061. ;; go to named list
  1062. (while (and (not (let ((item (org-index--parse-list-item)))
  1063. (if item
  1064. (and (or non-top (= (cdr (assoc :indent item)) 0)) ;; accept only toplevel ?
  1065. (string= (cdr (assoc :text item)) name)) ;; with requested name
  1066. nil)))
  1067. (not (org-at-table-p))
  1068. (not (org-at-heading-p))
  1069. (not (eobp)))
  1070. (forward-line 1))
  1071. (if (org-at-item-p)
  1072. t
  1073. (if required
  1074. (org-index--report-index-error "Could not find required list '%s'" name)
  1075. nil)))
  1076. (defun org-index--parse-list-item ()
  1077. "Parse a list item into an assoc array (indent, checkbox, text, value)."
  1078. ;; matche full list-item, maybe with checkbox and double-colon
  1079. (if (looking-at org-list-full-item-re)
  1080. ;; retrieve interesting parts of list item from match data
  1081. (let (indent checkbox text value next-line)
  1082. (setq indent
  1083. (- (save-excursion (goto-char (match-beginning 1)) (current-column)) ; first column
  1084. (save-match-data (org-current-level)) ; indent-level
  1085. 1))
  1086. (setq checkbox (match-string 3))
  1087. (setq text (match-string 4))
  1088. (set (if text 'value 'text) (buffer-substring (match-end 0) (line-end-position))) ; regexp did not capture this
  1089. ;; peek ahead, if item continues on next line
  1090. (forward-line 1)
  1091. (if (looking-at org-list-full-item-re)
  1092. (forward-line -1) ; already at next item; go back
  1093. (setq next-line (buffer-substring (line-beginning-position) (line-end-position))))
  1094. ;; clean up strings
  1095. (mapc (lambda (x)
  1096. (if (stringp (symbol-value x))
  1097. (set x (org-trim (substring-no-properties (symbol-value x))))))
  1098. '(text value next-line))
  1099. (if next-line (setq text (concat text " " next-line))) ; append next line if
  1100. (list (cons :indent indent) (cons :text text) (cons :value value) (cons :sym (intern text))))
  1101. nil))
  1102. (defun org-index--create-missing-index (&rest reasons)
  1103. "Create a new empty index table with detailed explanation. Argument REASONS explains why."
  1104. (org-index--ask-before-create-index "Cannot find your index table: "
  1105. "new permanent" "."
  1106. reasons)
  1107. (org-index--create-index))
  1108. (defun org-index--report-index-error (&rest reasons)
  1109. "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
  1110. (when org-index--buffer
  1111. (pop-to-buffer-same-window org-index--buffer)
  1112. (goto-char org-index--below-hline)
  1113. (org-reveal t))
  1114. (org-index--ask-before-create-index "The existing index contains this error: "
  1115. "temporary" ", to compare with."
  1116. reasons)
  1117. (org-index--create-index t t))
  1118. (defun org-index--ask-before-create-index (explanation type for-what reasons)
  1119. ; checkdoc-params: (explanation type for-what reasons)
  1120. "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
  1121. (let (reason prompt)
  1122. (setq reason (apply 'format reasons))
  1123. (setq prompt (concat explanation reason "\n\n"
  1124. "However, this assistant can help you to create a "
  1125. type " index with detailed comments" for-what "\n\n"
  1126. "Do you want to proceed ?"))
  1127. (unless (let ((max-mini-window-height 1.0))
  1128. (y-or-n-p prompt))
  1129. (error (concat explanation reason)))))
  1130. (defun org-index--create-index (&optional temporary compare)
  1131. "Create a new empty index table with detailed explanation.
  1132. specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
  1133. (let (buffer
  1134. title
  1135. firstref
  1136. id)
  1137. (if temporary
  1138. (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
  1139. (buffer-name "*org-index-example-index*"))
  1140. (setq buffer (get-buffer-create buffer-name))
  1141. (with-current-buffer buffer
  1142. ;; but it needs a file for its index to be found
  1143. (unless (string= (buffer-file-name) file-name)
  1144. (set-visited-file-name file-name))
  1145. (rename-buffer buffer-name) ; name is change by line above
  1146. (erase-buffer)
  1147. (org-mode)))
  1148. (setq buffer (get-buffer (org-completing-read "Please choose the buffer, where the new node for the index table should be created; the new node will be inserted at its end.\n\nBuffer: " (mapcar 'buffer-name (org-buffer-list))))))
  1149. (setq title (read-from-minibuffer "Please enter the title of the index node: "))
  1150. (while (progn
  1151. (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is a number preceeded by some non-digit chars and optionally followed by some more non-digit chars, e.g. 'R1', '-1-' or '#1#' (and your initial number does not need to be '1'). The format of your reference-numbers only needs to make sense for yourself, so that you can spot it easily in your texts or write it on a piece of paper; it should however not already appear to frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose: "))
  1152. (let (desc)
  1153. (when (string-match "[[:blank:]]" firstref)
  1154. (setq desc "Contains whitespace"))
  1155. (when (string-match "[[:cntrl:]]" firstref)
  1156. (setq desc "Contains control characters"))
  1157. (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
  1158. ;; firstref not okay, report details
  1159. (setq desc
  1160. (cond ((string= firstref "") "is empty")
  1161. ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
  1162. ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
  1163. ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
  1164. )))
  1165. (if desc
  1166. (progn
  1167. (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
  1168. t)
  1169. nil))))
  1170. (with-current-buffer buffer
  1171. (goto-char (point-max))
  1172. (insert (format "* %s %s\n" firstref title))
  1173. (if temporary
  1174. (insert "
  1175. Below you find your temporary index table, which WILL NOT LAST LONGER
  1176. THAN YOUR CURRENT EMACS SESSION.
  1177. ")
  1178. (insert "
  1179. Below you find your initial index table, which will grow over time.
  1180. "))
  1181. (insert "
  1182. You may start using it by adding some lines. Just move to
  1183. another heading, invoke `org-index' and choose the command
  1184. 'add'. After adding a few nodes, try the command 'occur'
  1185. to search among them.
  1186. To gain further insight you may invoke the subcommand 'help', or
  1187. read the description of `org-index'.
  1188. Within the index table below, dhe sequence of columns does not
  1189. matter. You may reorder them in any way you please. Columns are
  1190. found by their heading. You may also add your own columns,
  1191. which should start with a dot (e.g. '.custom').
  1192. Following this explanations you will find the item-list
  1193. `columns-and-flags', which influences the behaviour of
  1194. `org-index'. See the explanations which are part of this list.
  1195. This node needs not be a top level node; its name is completely
  1196. at your choice; it is found through its ID only.
  1197. ")
  1198. (unless temporary
  1199. (insert "
  1200. Remark: These lines of explanation can be removed at any time.
  1201. "))
  1202. (setq id (org-id-get-create))
  1203. (insert (format "
  1204. %s
  1205. | ref | category | keywords | count | last-accessed | created | id |
  1206. | | | | | | | <4> |
  1207. |-----+-----------+----------+-------+---------------+---------+------|
  1208. | %s | | %s | | | %s | %s |
  1209. "
  1210. org-index--sample-flags
  1211. firstref
  1212. "This node"
  1213. (with-temp-buffer (org-insert-time-stamp nil nil t))
  1214. id))
  1215. ;; make sure, that node can be found
  1216. (org-id-add-location id (buffer-file-name))
  1217. (setq buffer-save-without-query t)
  1218. (basic-save-buffer)
  1219. (while (not (org-at-table-p)) (forward-line -1))
  1220. (unless buffer-read-only (org-table-align))
  1221. (while (not (org-at-heading-p)) (forward-line -1))
  1222. ;; read back some info about new index
  1223. (let ((org-index-id id))
  1224. (org-index--verify-id))
  1225. ;; remember at least for this session
  1226. (setq org-index-id id)
  1227. ;; present results to user
  1228. (if temporary
  1229. (progn
  1230. ;; Present existing and temporary index together
  1231. (when compare
  1232. (pop-to-buffer-same-window org-index--buffer)
  1233. (goto-char org-index--point)
  1234. (org-index--unfold-buffer)
  1235. (delete-other-windows)
  1236. (select-window (split-window-vertically)))
  1237. ;; show new index
  1238. (pop-to-buffer-same-window buffer)
  1239. (org-id-goto id)
  1240. (org-index--unfold-buffer)
  1241. (if compare
  1242. (error "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
  1243. (message "This is your new temporary index.")))
  1244. (progn
  1245. ;; Only show the new index
  1246. (pop-to-buffer-same-window buffer)
  1247. (delete-other-windows)
  1248. (org-id-goto id)
  1249. (org-index--unfold-buffer)
  1250. (if (y-or-n-p "This is your new index table. It is already set for this Emacs session, so you may try it out. Do you want to save its id to make it available for future Emacs sessions too ? ")
  1251. (progn
  1252. (customize-save-variable 'org-index-id id)
  1253. (error "Saved org-index-id '%s' to %s" id (or custom-file
  1254. user-init-file)))
  1255. (let (sq)
  1256. (setq sq (format "(setq org-index-id \"%s\")" id))
  1257. (kill-new sq)
  1258. (error "Did not make the id of this new index permanent; you may want to put\n\n %s\n\ninto your own initialization; it is copied already, just yank it" sq))))))))
  1259. (defun org-index--unfold-buffer ()
  1260. "Helper function to unfold buffer."
  1261. (org-show-context)
  1262. (org-show-subtree)
  1263. (recenter 1)
  1264. (save-excursion
  1265. (org-back-to-heading)
  1266. (forward-line) ;; on property drawer
  1267. (org-cycle)
  1268. (org-index--goto-list "columns-and-flags")
  1269. (org-cycle)))
  1270. (defun org-index--update-line (&optional ref-or-id)
  1271. "Update columns count and last-accessed in line REF-OR-ID."
  1272. (let ((newcount 0)
  1273. initial)
  1274. (with-current-buffer org-index--buffer
  1275. (unless buffer-read-only
  1276. ;; search reference or id, if given (or assume, that we are already positioned right)
  1277. (when ref-or-id
  1278. (setq initial (point))
  1279. (goto-char org-index--below-hline)
  1280. (while (and (org-at-table-p)
  1281. (not (or (string= ref-or-id (org-index--get-or-set-field 'ref))
  1282. (string= ref-or-id (org-index--get-or-set-field 'id)))))
  1283. (forward-line)))
  1284. (if (not (org-at-table-p))
  1285. (error "Did not find reference or id '%s'" ref-or-id)
  1286. (org-index--update-current-line))
  1287. (if initial (goto-char initial))))))
  1288. (defun org-index--update-current-line ()
  1289. "Update current lines columns count and last-accessed."
  1290. (let (newcount (count-field (org-index--get-or-set-field 'count)))
  1291. ;; update count field only if number or empty
  1292. (when (or (not count-field)
  1293. (string-match "^[0-9]+$" count-field))
  1294. (setq newcount (+ 1 (string-to-number (or count-field "0"))))
  1295. (org-index--get-or-set-field 'count
  1296. (number-to-string newcount)))
  1297. ;; update timestamp
  1298. (org-table-goto-column (org-index--column-num 'last-accessed))
  1299. (org-table-blank-field)
  1300. (org-insert-time-stamp nil t t)
  1301. ;; move line according to new content
  1302. (org-index--promote-current-line)
  1303. (org-index--align-and-fontify-current-line)))
  1304. (defun org-index--align-and-fontify-current-line ()
  1305. "Make current line blend well among others."
  1306. (let ((line (substring-no-properties (delete-and-extract-region (line-beginning-position) (line-end-position)))))
  1307. ;; create minimum table with fixed-width columns to align and fontiry new line
  1308. (insert (with-temp-buffer
  1309. (org-set-font-lock-defaults)
  1310. (insert org-index--headings-visible)
  1311. (goto-char (point-min))
  1312. ;; fill columns, so that aligning cannot shrink them
  1313. (search-forward "|")
  1314. (replace-string " " "." nil (point) (line-end-position))
  1315. (replace-string ".|." " | " nil (line-beginning-position) (line-end-position))
  1316. (replace-string "|." "| " nil (line-beginning-position) (line-end-position))
  1317. (goto-char (point-max))
  1318. (insert line)
  1319. (forward-line 0)
  1320. (org-table-align)
  1321. (font-lock-fontify-region (point-min) (point-max))
  1322. (goto-char (point-max))
  1323. (forward-line -1)
  1324. (buffer-substring (line-beginning-position) (line-end-position))))))
  1325. (defun org-index--promote-current-line ()
  1326. "Move current line up in table according to changed sort fields."
  1327. (let (begin end key
  1328. (to-skip 0))
  1329. (forward-line 0) ; stay at beginning of line
  1330. (setq key (org-index--get-sort-key))
  1331. (setq begin (point))
  1332. (setq end (line-beginning-position 2))
  1333. (forward-line -1)
  1334. (while (and (org-at-table-p)
  1335. (not (org-at-table-hline-p))
  1336. (string< (org-index--get-sort-key) key))
  1337. (incf to-skip)
  1338. (forward-line -1))
  1339. (forward-line 1)
  1340. ;; insert line at new position
  1341. (when (> to-skip 0)
  1342. (insert (delete-and-extract-region begin end))
  1343. (forward-line -1))))
  1344. (defun org-index--get-sort-key (&optional sort with-ref)
  1345. "Get value for sorting from column SORT, optional WITH-REF."
  1346. (let (ref
  1347. ref-field
  1348. key)
  1349. (unless sort (setq sort org-index--last-sort)) ; use default value
  1350. (when (or with-ref
  1351. (eq sort 'ref))
  1352. ;; get reference with leading zeroes, so it can be
  1353. ;; sorted as text
  1354. (setq ref-field (org-index--get-or-set-field 'ref))
  1355. (string-match org-index--ref-regex ref-field)
  1356. (setq ref (format
  1357. "%06d"
  1358. (string-to-number
  1359. (or (match-string 1 ref-field)
  1360. "0")))))
  1361. (setq key
  1362. (cond
  1363. ((eq sort 'count)
  1364. (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))
  1365. ((eq sort 'ref)
  1366. ref)
  1367. ((eq sort 'id)
  1368. (org-index--get-or-set-field sort))
  1369. ((eq sort 'last-accessed)
  1370. (org-index--get-or-set-field sort))
  1371. ((eq sort 'created)
  1372. (org-index--get-or-set-field sort))
  1373. (t (error "This is a bug: unmatched case '%s'" sort))))
  1374. (if with-ref (setq key (concat key ref)))
  1375. key))
  1376. (defun org-index--get-or-set-field (key &optional value)
  1377. "Retrieve field KEY from index table or set it to VALUE."
  1378. (let (field)
  1379. (save-excursion
  1380. (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value)))
  1381. (if (string= field "") (setq field nil))
  1382. (org-no-properties field))))
  1383. (defun org-index--column-num (key)
  1384. "Return number of column KEY."
  1385. (if (numberp key)
  1386. key
  1387. (cdr (assoc key org-index--columns))))
  1388. (defun org-index--special-column (key)
  1389. "Return column (not a number) for special column KEY."
  1390. (cdr (assoc key org-index--special-columns)))
  1391. (defun org-index--flag-p (flag column)
  1392. "Check if COLUMN has FLAG set."
  1393. (unless (memq flag org-index--all-flags)
  1394. (error (format "Internal error: unknown flag %s" (symbol-name flag))))
  1395. (memq column (assoc flag org-index--flagged-columns)))
  1396. (defun org-index--make-guarded-search (ref &optional dont-quote)
  1397. "Make robust search string from REF; DONT-QUOTE it, if requested."
  1398. (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
  1399. (defun org-index--do-statistics ()
  1400. "Compute statistics about index table."
  1401. (let ((total 0)
  1402. ref-field ref min max message)
  1403. ;; go through table and remove all refs, that we see
  1404. (goto-char org-index--below-hline)
  1405. (while (org-at-table-p)
  1406. ;; get ref-field and number
  1407. (setq ref-field (org-index--get-or-set-field 'ref))
  1408. (if (and ref-field
  1409. (string-match org-index--ref-regex ref-field))
  1410. (setq ref (string-to-number (match-string 1 ref-field))))
  1411. ;; record min and max
  1412. (if (or (not min) (< ref min)) (setq min ref))
  1413. (if (or (not max) (> ref max)) (setq max ref))
  1414. ;; count
  1415. (setq total (1+ total))
  1416. (forward-line))
  1417. (setq message (format "First reference is %s, last %s; %d values in between, %d of them are used (%d percent)"
  1418. (format org-index--ref-format min)
  1419. (format org-index--ref-format max)
  1420. (1+ (- max min))
  1421. total
  1422. (truncate (* 100 (/ (float total) (1+ (- max min)))))
  1423. ))
  1424. (goto-char org-index--below-hline)
  1425. message))
  1426. (defun org-index--do-add-or-update ()
  1427. "For current node or current line in index, add a new line to index table or update existing."
  1428. (let* (id ref args yank ref-and-yank)
  1429. ;; do the same things from within index and from outside
  1430. (if org-index--within-node
  1431. (progn
  1432. (unless (org-at-table-p)
  1433. (error "Within index node but not on table"))
  1434. (setq id (org-index--get-or-set-field 'id))
  1435. (setq ref (org-index--get-or-set-field 'ref))
  1436. (setq args (org-index--collect-values-for-add-update-remote id))
  1437. (org-index--write-fields-for-add-update args)
  1438. (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add)))
  1439. (cons (format "Updated index line %s" ref) yank))
  1440. (unless (org-at-heading-p)
  1441. (error "Not at headline"))
  1442. (setq id (org-id-get-create))
  1443. (setq ref (org-index--on 'id id (org-index--get-or-set-field 'ref)))
  1444. (setq args (org-index--collect-values-for-add-update id ref))
  1445. (if ref
  1446. ;; already have a ref, find it in index and update fields
  1447. (let ((kvs args)
  1448. found-and-message)
  1449. (org-index--on
  1450. 'ref ref
  1451. (org-index--write-fields-for-add-update args)
  1452. (setq yank (org-index--get-or-set-field (org-index--special-column 'yank-after-add))))
  1453. (cons (format "Updated index line %s" ref) yank))
  1454. ;; no ref here, create new line in index
  1455. (setq ref-and-yank (apply 'org-index--do-new-line args))
  1456. (cons (format "Added index line %s" (car ref-and-yank)) (concat (cdr ref-and-yank) " "))))))
  1457. (defun org-index--check-ids ()
  1458. "Check, that ids really point to a node."
  1459. (let ((lines 0)
  1460. id ids marker)
  1461. (goto-char org-index--below-hline)
  1462. (catch 'problem
  1463. (while (org-at-table-p)
  1464. (when (setq id (org-index--get-or-set-field 'id))
  1465. ;; check for double ids
  1466. (when (member id ids)
  1467. (org-table-goto-column (org-index--column-num 'id))
  1468. (throw 'problem "This id appears twice in index; please use command 'maintain' to check for duplicate ids"))
  1469. (incf lines)
  1470. (setq ids (cons id ids))
  1471. ;; check, if id is valid
  1472. (setq marker (org-id-find id t))
  1473. (unless marker
  1474. (org-table-goto-column (org-index--column-num 'id))
  1475. (throw 'problem "This id cannot be found")))
  1476. (forward-line))
  1477. (goto-char org-index--below-hline)
  1478. nil)))
  1479. (defun org-index--update-all-lines ()
  1480. "Update all lines of index at once."
  1481. (let ((lines 0)
  1482. id ref kvs)
  1483. ;; check for double ids
  1484. (or
  1485. (org-index--check-ids)
  1486. (progn
  1487. (goto-char org-index--below-hline)
  1488. (while (org-at-table-p)
  1489. ;; update single line
  1490. (when (setq id (org-index--get-or-set-field 'id))
  1491. (setq ref (org-index--get-or-set-field 'ref))
  1492. (setq kvs (org-index--collect-values-for-add-update-remote id))
  1493. (org-index--write-fields-for-add-update kvs)
  1494. (incf lines))
  1495. (forward-line))
  1496. (goto-char org-index--below-hline)
  1497. (org-table-align)
  1498. (format "Updated %d lines" lines)))))
  1499. (defun org-index--collect-values-for-add-update (id &optional silent category)
  1500. "Collect values for adding or updating line specified by ID, do not ask if SILENT, use CATEGORY, if given."
  1501. (let ((args (list 'ref t 'id id))
  1502. content)
  1503. (dolist (col-num org-index--columns)
  1504. (setq content "")
  1505. (if (eq (car col-num) 'keywords)
  1506. (setq content (nth 4 (org-heading-components))))
  1507. (if (eq (car col-num) 'category)
  1508. (setq content (or category org-index--category-before)))
  1509. (if (eq (car col-num) 'level)
  1510. (setq content (number-to-string (org-outline-level))))
  1511. ;; Shift ref and timestamp ?
  1512. (if (org-index--flag-p 'shift-ref-and-date-on-add (car col-num))
  1513. (dotimes (i 2)
  1514. (if (or (string-match (concat "^\\s-*" org-index--ref-regex) content)
  1515. (string-match (concat org-ts-regexp-both) content))
  1516. (setq content (substring content (match-end 0))))))
  1517. (if (and (not silent) ; do not edit, if heading has already been added
  1518. (org-index--flag-p 'edit-on-add (car col-num)))
  1519. (setq content (read-from-minibuffer
  1520. (format "Edit text for column '%s': " (symbol-name (car col-num)))
  1521. content)))
  1522. (if (not (string= content ""))
  1523. (setq args (append (list (car col-num) content) args))))
  1524. args))
  1525. (defun org-index--collect-values-for-add-update-remote (id)
  1526. "Wrap `org-index--collect-values-for-add-update' by prior moving to remote node identified by ID."
  1527. (let (marker point args)
  1528. (setq marker (org-id-find id t))
  1529. ;; enter buffer and collect information
  1530. (with-current-buffer (marker-buffer marker)
  1531. (setq point (point))
  1532. (goto-char marker)
  1533. (setq args (org-index--collect-values-for-add-update id t (org-get-category (point) t)))
  1534. (goto-char point))
  1535. args))
  1536. (defun org-index--write-fields-for-add-update (kvs)
  1537. "Update current line with values from KVS (keys-values)."
  1538. (while kvs
  1539. (unless (eq (car kvs) 'ref)
  1540. (org-index--get-or-set-field (car kvs) (org-trim (cadr kvs))))
  1541. (setq kvs (cddr kvs))))
  1542. (defun org-index--do-delete ()
  1543. "Perform command delete."
  1544. (unless (org-at-heading-p)
  1545. (error "Not at headline"))
  1546. (let* ((id (org-entry-get (point) "ID"))
  1547. (ref (org-index--ref-from-id id)))
  1548. ;; maybe delete from heading
  1549. (if ref
  1550. (save-excursion
  1551. (end-of-line)
  1552. (let ((end (point)))
  1553. (beginning-of-line)
  1554. (when (search-forward ref end t)
  1555. (delete-char (- (length ref)))
  1556. (just-one-space)))))
  1557. ;; delete from index table
  1558. (if (org-index--delete-line id)
  1559. (format "Deleted index line %s" ref)
  1560. (format "Did not find id %s in index" id))))
  1561. (defun org-index--go (&optional column value)
  1562. "Position cursor on index line where COLUMN equals VALUE.
  1563. Return t or nil, leave point on line or at top of table, needs to be in buffer initially."
  1564. (let (found text)
  1565. (unless (eq (current-buffer) org-index--buffer)
  1566. (error "This is a bug: Not in index buffer"))
  1567. ;; loop over lines
  1568. (goto-char org-index--below-hline)
  1569. (if column
  1570. (progn
  1571. (forward-line -1)
  1572. (while (and (not found)
  1573. (forward-line)
  1574. (org-at-table-p))
  1575. (setq found (string= value (org-index--get-or-set-field column)))))
  1576. (setq found t))
  1577. ;; return value
  1578. (if found
  1579. t
  1580. (goto-char org-index--below-hline)
  1581. nil)))
  1582. (defun org-index--do-head (ref id &optional other)
  1583. "Perform command head: Find node with REF or ID and present it.
  1584. If OTHER in separate window."
  1585. (setq org-index--last-ref ref)
  1586. (let (message marker)
  1587. (setq marker (org-id-find id t))
  1588. (if marker
  1589. (progn
  1590. (org-index--update-line id)
  1591. (let (cb)
  1592. (if other
  1593. (progn
  1594. (setq cb (current-buffer))
  1595. (pop-to-buffer (marker-buffer marker)))
  1596. (pop-to-buffer-same-window (marker-buffer marker)))
  1597. (goto-char marker)
  1598. (org-reveal t)
  1599. (org-show-entry)
  1600. (recenter))
  1601. (setq message (format "Found headline %s" ref)))
  1602. (setq message (format "Did not find headline %s" ref)))))
  1603. (defun org-index--do-occur ()
  1604. "Perform command occur."
  1605. (let ((word "") ; last word to search for growing and shrinking on keystrokes
  1606. (prompt "Search for: ")
  1607. (lines-wanted (window-body-height))
  1608. (lines-found 0) ; number of lines found
  1609. words ; list words that should match
  1610. occur-buffer
  1611. begin ; position of first line
  1612. narrow ; start of narrowed buffer
  1613. help-text ; cons with help text short and long
  1614. key-help ; for keys with special function
  1615. search-text ; description of text to search for
  1616. done ; true, if loop is done
  1617. in-c-backspace ; true, while processing C-backspace
  1618. show-headings ; true, if headings should be shown
  1619. help-overlay ; Overlay with help text
  1620. last-point ; Last position before end of search
  1621. key ; input from user
  1622. key-sequence) ; as a sequence
  1623. ;; make and show buffer
  1624. (if (get-buffer org-index--occur-buffer-name)
  1625. (kill-buffer org-index--occur-buffer-name))
  1626. (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
  1627. (pop-to-buffer-same-window occur-buffer)
  1628. ;; avoid modifying direct buffer
  1629. (setq buffer-read-only t)
  1630. (toggle-truncate-lines 1)
  1631. (setq font-lock-keywords-case-fold-search t)
  1632. (setq case-fold-search t)
  1633. ;; reset stack and overlays
  1634. (setq org-index--occur-stack nil)
  1635. (setq org-index--occur-tail-overlay nil)
  1636. ;; narrow to table rows and one line before
  1637. (goto-char (marker-position org-index--below-hline))
  1638. (forward-line 0)
  1639. (setq begin (point))
  1640. (forward-line -1)
  1641. (setq narrow (point))
  1642. (while (org-at-table-p)
  1643. (forward-line))
  1644. (narrow-to-region narrow (point))
  1645. (goto-char (point-min))
  1646. (forward-line)
  1647. ;; initialize help text
  1648. (setq help-text (cons
  1649. "Incremental occur; `?' toggles help and headlines.\n"
  1650. (concat
  1651. (org-index--wrap
  1652. (concat
  1653. "Normal keys add to search word; <space> or <comma> start additional word; <backspace> erases last char, <C-backspace> last word; <return> jumps to heading, <tab> jumps to heading in other window; all other keys end search.\n"))
  1654. org-index--headings)))
  1655. ;; insert overlays for help text and to cover unsearched lines
  1656. (setq help-overlay (make-overlay (point-min) begin))
  1657. (overlay-put help-overlay 'display (car help-text))
  1658. (overlay-put help-overlay 'face 'org-agenda-dimmed-todo-face)
  1659. (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
  1660. (overlay-put org-index--occur-tail-overlay 'invisible t)
  1661. (while (not done)
  1662. (if in-c-backspace
  1663. (setq key "<backspace>")
  1664. (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
  1665. ;; read key
  1666. (setq key-sequence
  1667. (vector (read-key
  1668. (format "%s%s%s"
  1669. prompt
  1670. search-text
  1671. (if (string= search-text "") "" " ")))))
  1672. (setq key (key-description key-sequence)))
  1673. (cond
  1674. ((string= key "<C-backspace>")
  1675. (setq in-c-backspace t))
  1676. ((member key (list "<backspace>" "DEL")) ; erase last char
  1677. (if (= (length word) 0)
  1678. ;; nothing more to delete from current word; try next
  1679. (progn
  1680. (setq word (car words))
  1681. (setq words (cdr words))
  1682. (setq in-c-backspace nil))
  1683. ;; unhighlight longer match
  1684. (unhighlight-regexp (regexp-quote word))
  1685. ;; some chars are left; shorten word
  1686. (setq word (substring word 0 -1))
  1687. (when (= (length word) 0) ; when nothing left, use next word from list
  1688. (setq word (car words))
  1689. (setq words (cdr words))
  1690. (setq in-c-backspace nil))
  1691. ;; free top list of overlays and remove list
  1692. (setq lines-found (or (org-index--unhide) lines-wanted))
  1693. (move-overlay org-index--occur-tail-overlay
  1694. (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  1695. (point-max))
  1696. (point-max))
  1697. ;; highlight shorter word
  1698. (unless (= (length word) 0)
  1699. (highlight-regexp (regexp-quote word) 'isearch))
  1700. ;; make sure, point is still visible
  1701. (goto-char begin)))
  1702. ((member key (list "SPC" ",")) ; space or comma: enter an additional search word
  1703. ;; push current word and clear, no need to change display
  1704. (setq words (cons word words))
  1705. (setq word ""))
  1706. ((string= key "?") ; question mark: toggle display of headlines and help
  1707. (setq help-text (cons (cdr help-text) (car help-text)))
  1708. (overlay-put help-overlay 'display (car help-text)))
  1709. ((and (= (length key) 1)
  1710. (aref printable-chars (elt key 0))) ; any printable char: add to current search word
  1711. ;; unhighlight short word
  1712. (unless (= (length word) 0)
  1713. (unhighlight-regexp (regexp-quote word)))
  1714. ;; add to word
  1715. (setq word (concat word key))
  1716. ;; make overlays to hide lines, that do not match longer word any more
  1717. (goto-char begin)
  1718. (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted))
  1719. (move-overlay org-index--occur-tail-overlay
  1720. (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
  1721. (point-max))
  1722. (point-max))
  1723. (goto-char begin)
  1724. ;; highlight longer word
  1725. (highlight-regexp (regexp-quote word) 'isearch)
  1726. ;; make sure, point is on a visible line
  1727. (line-move -1 t)
  1728. (line-move 1 t))
  1729. ;; anything else terminates loop
  1730. (t (setq done t))))
  1731. ;; put back input event, that caused the loop to end
  1732. (unless (string= key "C-g")
  1733. (setq unread-command-events (listify-key-sequence key-sequence))
  1734. (message key))
  1735. ;; postprocessing
  1736. (setq last-point (point))
  1737. ;; For performance reasons do not show matching lines for rest of table. So no code here.
  1738. ;; make permanent copy
  1739. ;; copy visible lines
  1740. (let ((lines-collected 0)
  1741. keymap line all-lines end-of-head)
  1742. (setq cursor-type t)
  1743. (goto-char begin)
  1744. ;; collect all visible lines
  1745. (while (and (not (eobp))
  1746. (< lines-collected lines-wanted))
  1747. ;; skip over invisible lines
  1748. (while (and (invisible-p (point))
  1749. (not (eobp)))
  1750. (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
  1751. (setq line (buffer-substring (line-beginning-position) (line-end-position)))
  1752. (unless (string= line "")
  1753. (incf lines-collected)
  1754. (setq all-lines (cons (concat line
  1755. "\n")
  1756. all-lines)))
  1757. (forward-line 1))
  1758. (kill-buffer org-index--occur-buffer-name) ; cannot keep this buffer; might become stale soon
  1759. ;; create new buffer
  1760. (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
  1761. (pop-to-buffer-same-window occur-buffer)
  1762. (insert org-index--headings)
  1763. (setq end-of-head (point))
  1764. ;; insert into new buffer
  1765. (save-excursion
  1766. (apply 'insert (reverse all-lines))
  1767. (if (= lines-collected lines-wanted)
  1768. (insert "\n(more lines omitted)\n")))
  1769. (org-mode)
  1770. (setq truncate-lines t)
  1771. (if (org-at-table-p) (org-table-align))
  1772. (font-lock-fontify-buffer)
  1773. ;; prepare help text
  1774. (setq org-index--occur-help-overlay (make-overlay (point-min) end-of-head))
  1775. (setq org-index--occur-help-text
  1776. (cons
  1777. (org-index--wrap
  1778. (concat "Search is done; `?' toggles help and headlines.\n"))
  1779. (concat
  1780. (org-index--wrap (format (concat "Search is done. "
  1781. (if (< lines-collected lines-wanted)
  1782. " Showing all %d matches for "
  1783. " Showing one window of matches for ")
  1784. "\"" search-text
  1785. "\". <return> jumps to heading, <tab> jumps to heading in other window, <S-return> to matching line in index, <space> increments count.\n" )
  1786. (length all-lines)))
  1787. org-index--headings)))
  1788. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
  1789. (overlay-put org-index--occur-help-overlay 'face 'org-agenda-dimmed-todo-face)
  1790. ;; highlight words
  1791. (setq case-fold-search t)
  1792. (setq font-lock-keywords-case-fold-search t)
  1793. (mapc (lambda (w) (unless (or (not w) (string= w "")) (highlight-regexp (regexp-quote w) 'isearch)))
  1794. (cons word words))
  1795. (setq buffer-read-only t)
  1796. ;; install keyboard-shortcuts
  1797. (setq keymap (make-sparse-keymap))
  1798. (set-keymap-parent keymap org-mode-map)
  1799. (mapc (lambda (x) (define-key keymap (kbd x)
  1800. (lambda () (interactive)
  1801. (message "%s" (org-index--occur-to-head)))))
  1802. (list "<return>" "RET"))
  1803. (define-key keymap (kbd "<tab>")
  1804. (lambda () (interactive)
  1805. (message (org-index--occur-to-head t))))
  1806. (define-key keymap (kbd "SPC")
  1807. (lambda () (interactive)
  1808. ;; increment in index
  1809. (let ((ref (org-index--get-or-set-field 'ref))
  1810. count)
  1811. (org-index--on
  1812. 'ref ref
  1813. (setq count (+ 1 (string-to-number (org-index--get-or-set-field 'count))))
  1814. (org-index--get-or-set-field 'count (number-to-string count))
  1815. (org-index--promote-current-line)
  1816. (org-index--align-and-fontify-current-line))
  1817. ;; increment in this buffer
  1818. (let ((inhibit-read-only t))
  1819. (org-index--get-or-set-field 'count (number-to-string count)))
  1820. (message "Incremented count to %d" count))))
  1821. (define-key keymap (kbd "<S-return>")
  1822. (lambda () (interactive)
  1823. (org-index 'enter (org-index--get-or-set-field 'ref))))
  1824. (define-key keymap (kbd "?")
  1825. (lambda () (interactive)
  1826. (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
  1827. (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
  1828. (use-local-map keymap))))
  1829. (defun org-index--wrap (text)
  1830. "Wrap TEXT at fill column."
  1831. (with-temp-buffer
  1832. (insert text)
  1833. (fill-region (point-min) (point-max) nil t)
  1834. (buffer-string)))
  1835. (defun org-index--occur-to-head (&optional other)
  1836. "Helper for `org-index--occur', find heading with ref or id; if OTHER, in other window."
  1837. (let ((ref (org-index--get-or-set-field 'ref))
  1838. (id (org-index--get-or-set-field 'id)))
  1839. (if id
  1840. (org-index--do-head ref id other)
  1841. (message "Current line has no id."))))
  1842. (defun org-index--hide-with-overlays (words lines-wanted)
  1843. "Hide text that is currently visible and does not match WORDS by creating overlays; leave LINES-WANTED lines visible."
  1844. (let ((lines-found 0)
  1845. (end-of-visible (point))
  1846. overlay overlays start matched)
  1847. ;; main loop
  1848. (while (and (not (eobp))
  1849. (< lines-found lines-wanted))
  1850. ;; skip invisible lines
  1851. (while (and (not (eobp))
  1852. (and
  1853. (invisible-p (point))
  1854. (< (point) (overlay-start org-index--occur-tail-overlay))))
  1855. (goto-char (overlay-end (car (overlays-at (point))))))
  1856. ;; find stretch of lines, that are currently visible but should be invisible now
  1857. (setq matched nil)
  1858. (setq start (point))
  1859. (while (and (not (eobp))
  1860. (not
  1861. (and
  1862. (invisible-p (point))
  1863. (< (point) (overlay-start org-index--occur-tail-overlay))))
  1864. (not (and (org-index--test-words words)
  1865. (setq matched t)))) ; for its side effect
  1866. (forward-line 1))
  1867. ;; create overlay to hide this stretch
  1868. (when (< start (point)) ; avoid creating an empty overlay
  1869. (setq overlay (make-overlay start (point)))
  1870. (overlay-put overlay 'invisible t)
  1871. (setq overlays (cons overlay overlays)))
  1872. ;; skip and count line, that matched
  1873. (when matched
  1874. (forward-line 1)
  1875. (setq end-of-visible (point))
  1876. (incf lines-found)))
  1877. ;; put new list on top of stack
  1878. (setq org-index--occur-stack
  1879. (cons (list (cons :overlays overlays)
  1880. (cons :end-of-visible end-of-visible)
  1881. (cons :lines lines-found))
  1882. org-index--occur-stack))
  1883. lines-found))
  1884. (defun org-index--unhide ()
  1885. "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
  1886. (when org-index--occur-stack
  1887. ;; delete overlays and make visible again
  1888. (mapc (lambda (y)
  1889. (delete-overlay y))
  1890. (cdr (assoc :overlays (car org-index--occur-stack))))
  1891. ;; remove from stack
  1892. (setq org-index--occur-stack (cdr org-index--occur-stack))
  1893. ;; return number of lines, that are now visible
  1894. (if org-index--occur-stack (cdr (assoc :lines (car org-index--occur-stack))))))
  1895. (defun org-index--test-words (words)
  1896. "Test current line for match against WORDS."
  1897. (let (line)
  1898. (setq line (downcase (buffer-substring (line-beginning-position) (line-beginning-position 2))))
  1899. (catch 'not-found
  1900. (dolist (w words)
  1901. (or (search w line)
  1902. (throw 'not-found nil)))
  1903. t)))
  1904. (defun org-index--create-new-line ()
  1905. "Do the common work for `org-index-new-line' and `org-index'."
  1906. (let (new)
  1907. ;; construct new reference
  1908. (unless new
  1909. (setq new (format "%s%d%s" org-index--head (1+ org-index--maxref) org-index--tail)))
  1910. ;; insert ref or id as last or first line, depending on sort-column
  1911. (goto-char org-index--below-hline)
  1912. (if (eq (org-index--special-column 'sort) 'count)
  1913. (progn
  1914. (while (org-at-table-p)
  1915. (forward-line))
  1916. (forward-line -1)
  1917. (org-table-insert-row t))
  1918. (org-table-insert-row))
  1919. ;; insert some of the standard values
  1920. (org-table-goto-column (org-index--column-num 'created))
  1921. (org-insert-time-stamp nil nil t)
  1922. (org-table-goto-column (org-index--column-num 'count))
  1923. (insert "1")
  1924. new))
  1925. (defun org-index--sort-silent ()
  1926. "Sort index for default column to remove any effects of temporary sorting."
  1927. (save-excursion
  1928. (org-index--verify-id)
  1929. (org-index--parse-table)
  1930. (org-index--on nil nil
  1931. (org-index--do-sort-index (org-index--special-column 'sort))
  1932. (org-table-align)
  1933. (remove-hook 'before-save-hook 'org-index--sort-silent))))
  1934. (defun org-index--copy-visible (beg end)
  1935. "Copy the visible parts of the region without adding it to kill-ring; copy of `org-copy-visible'"
  1936. (let (snippets s)
  1937. (save-excursion
  1938. (save-restriction
  1939. (narrow-to-region beg end)
  1940. (setq s (goto-char (point-min)))
  1941. (while (not (= (point) (point-max)))
  1942. (goto-char (org-find-invisible))
  1943. (push (buffer-substring s (point)) snippets)
  1944. (setq s (goto-char (org-find-visible))))))
  1945. (apply 'concat (nreverse snippets))))
  1946. (provide 'org-index)
  1947. ;; Local Variables:
  1948. ;; fill-column: 75
  1949. ;; comment-column: 50
  1950. ;; End:
  1951. ;;; org-index.el ends here