Add "vala" to org-babel-load-languages
[org-mode/org-tableheadings.git] / contrib / lisp / org-index.el
blob69eedb4e061356800a636a9b865e707ee155139b
1 ;;; org-index.el --- A personal adaptive index for org
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 ;; Author: Marc Ihm <org-index@2484.de>
6 ;; Version: 5.4.1
7 ;; Keywords: outlines index
9 ;; This file is not part of GNU Emacs.
11 ;;; License:
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Purpose:
30 ;; Fast search for selected org nodes and things outside of org.
32 ;; org-index creates and updates an index table with keywords; each line
33 ;; either points to a heading in org, references something outside or
34 ;; carries a snippet of text to yank. When searching the index, the set
35 ;; of matching lines is updated with every keystroke; results are sorted
36 ;; by usage count and date, so that frequently used entries appear first
37 ;; in the list of results.
39 ;; References are decorated numbers (e.g. 'R237' or '--455--'); they are
40 ;; well suited to be used outside of org, e.g. in folder names, ticket
41 ;; systems or on printed documents.
43 ;; On first invocation org-index will assist you in creating the index
44 ;; table.
46 ;; To start using your index, invoke subcommands 'add', 'ref' and 'yank'
47 ;; to create entries and 'occur' to find them.
50 ;; Setup:
52 ;; - Place this file in a directory of your load-path,
53 ;; e.g. org-mode/contrib/lisp.
55 ;; - Add these lines to your .emacs:
57 ;; (require 'org-index)
59 ;; - Restart your Emacs to make this effective.
61 ;; - Invoke `org-index'; on first run it will assist in creating your
62 ;; index table.
64 ;; - Optionally invoke `M-x org-customize', group 'Org Index', to tune
65 ;; some settings, e.g. the global prefix key 'C-c i'.
68 ;; Further information:
70 ;; - Watch the screencast at http://2484.de/org-index.html.
72 ;; - See the documentation of `org-index', which can also be read by
73 ;; invoking `org-index' and choosing the command help or '?'.
76 ;; Updates:
78 ;; The latest published version of this file can always be found at:
80 ;; http://orgmode.org/w/?p=org-mode.git;a=blob_plain;f=contrib/lisp/org-index.el;hb=HEAD
82 ;; Development version under:
84 ;; https://github.com/marcIhm/org-index
86 ;;; Change Log:
88 ;; [2017-05-27 Sa] Version 5.4.1
89 ;; - Dedicated submenu for focus operations
90 ;; - Occur accepts a numeric argument as a day span
91 ;; - New customization `org-index-clock-into-focus'
92 ;; - Fixed delay after choosing an index line
93 ;; - Bugfixes
95 ;; [2017-03-26 Su] Version 5.3.0
96 ;; - Focused can now be on a list of nodes (instead of a single one)
97 ;; - Cleaned up undeclared dependencies
99 ;; [2017-02-18 Sa] Version 5.2.3
100 ;; - New command 'focus'
101 ;; - Speeded up org-index--parse-table with the stored property "max-ref"
102 ;; - Speeded up org-index--on with search
103 ;; - Added org-index-prepare-when-idle
104 ;; - Fixed compatibility issue with emacs 24 (font-lock-ensure)
105 ;; - Added more customizations
106 ;; - Bugfixes
108 ;; [2016-10-19 We] Version 5.1.4
109 ;; - Bugfixes
111 ;; [2016-08-26 Fr] Version 5.1.3
112 ;; - Offering help during query for subcommands
113 ;; - Removed org-index-default-keybindings
114 ;; - Renamed subcommand multi-occur to find-ref
115 ;; - Subcommands add needs no longer be invoked from heading
116 ;; - Many Bugfixes
118 ;; [2015-12-29 Tu] Version 5.0.2
119 ;; - New commands yank, column and edit
120 ;; - New column tags
121 ;; - All columns are now required
122 ;; - References are now optional
123 ;; - Subcommand enter has been renamed to index
124 ;; - Subcommands kill and edit can be invoked from an occur buffer
125 ;; - Many Bugfixes
126 ;; - Added link to screencast
128 ;; [2015-08-20 Th] Version 4.3.0
129 ;; - Configuration is done now via standard customize
130 ;; - New sorting strategy 'mixed'
131 ;; - Silenced some compiler warnings
133 ;; [2015-03-18 We] Version 4.2.1
134 ;; - No garbage in kill-ring
135 ;; - No recentering after add
137 ;; [2015-03-08 Su] Version 4.2.0
138 ;; - Reference numbers for subcommands can be passed as a prefix argument
139 ;; - New variable org-index-default-keybindings-list with a list of
140 ;; default keybindings for org-index-default-keybindings
141 ;; - Added new column level
142 ;; - removed flags get-category-on-add and get-heading-on-add
144 ;; [2015-02-26 Th] to [2015-03-05 Th] Version 4.0.0 to 4.1.2
145 ;; - Removed command "leave"; rather go back with org-mark-ring-goto
146 ;; - Renamed column "link" to "id"
147 ;; - Added maintainance options to find duplicate rows, to check ids,
148 ;; update index or remove property org-index-ref from nodes
149 ;; - Shortened versin history
151 ;; [2014-12-08 Mo] to [2015-01-31 Sa] Version 3.0.0 to 3.2.0:
152 ;; - Complete sorting of index only occurs in idle-timer
153 ;; - New command "maintain" with some subcommands
154 ;; - Rewrote command "occur" with overlays in an indirect buffer
155 ;; - Command "add" updates index, if node is already present
156 ;; - New commands "add" and "delete" to easily add and remove
157 ;; the current node to or from your index.
158 ;; - New command "example" to create an example index.
159 ;; - Several new flags that are explained within index node.
160 ;; - Removed commands "reuse", "missing", "put", "goto",
161 ;; "update", "link", "fill", "unhighlight"
162 ;; - New function `org-index-default-keybindings'
164 ;; [2012-12-07 Fr] to [2014-04-26 Sa] Version 2.0.0 to 2.4.3:
165 ;; - New functions org-index-new-line and org-index-get-line
166 ;; offer access to org-index from other lisp programs
167 ;; - Regression tests with ert
168 ;; - Renamed from "org-favtable" to "org-index"
169 ;; - Added an assistant to set up the index table
170 ;; - occur is now incremental, searching as you type
171 ;; - Integrated with org-mark-ring-goto
172 ;; - Added full support for ids
173 ;; - Renamed the package from "org-reftable" to "org-favtable"
174 ;; - Additional columns are required (e.g. "link"). Error messages will
175 ;; guide you
176 ;; - Ask user explicitly, which command to invoke
177 ;; - Renamed the package from "org-refer-by-number" to "org-reftable"
179 ;; [2011-12-10 Sa] to [2012-09-22 Sa] Version Version 1.2.0 to 1.5.0:
180 ;; - New command "sort" to sort a buffer or region by reference number
181 ;; - New commands "highlight" and "unhighlight" to mark references
182 ;; - New command "head" to find a headline with a reference number
183 ;; - New commands occur and multi-occur
184 ;; - Started this Change Log
186 ;;; Code:
188 (require 'org-table)
189 (require 'org-id)
190 (require 'cl-lib)
191 (require 'widget)
193 ;; Version of this package
194 (defvar org-index-version "5.4.1" "Version of `org-index', format is major.minor.bugfix, where \"major\" are incompatible changes and \"minor\" are new features.")
196 ;; customizable options
197 (defgroup org-index nil
198 "Options concerning the optional index for org."
199 :tag "Org Index"
200 :group 'org)
202 (defcustom org-index-id nil
203 "Id of the Org-mode node, which contains the index table."
204 :group 'org-index)
206 (defcustom org-index-sort-by 'mixed
207 "Strategy for sorting index table (and whence entries in occur).
208 Valid values are:
210 last-access Sort index by date and time of last access; show
211 more recent entries first.
212 count Sort by usage count; more often used entries first.
213 mixed First, show all index entries, which have been
214 used today; sort them by last access. Then show
215 older entries sorted by usage count."
216 :group 'org-index
217 :set (lambda (s v)
218 (set-default s v)
219 (if (and org-index-id
220 org-index--buffer
221 (functionp 'org-index--sort-silent))
222 (org-index--sort-silent)))
223 :initialize 'custom-initialize-default
224 :type '(choice
225 (const last-accessed)
226 (const count)
227 (const mixed)))
229 (defcustom org-index-dispatch-key "\x03i"
230 "Key to invoke ‘org-index-dispatch’, which is the central entry function for ‘org-index’."
231 :group 'org-index
232 :initialize 'custom-initialize-set
233 :set (lambda (var val)
234 (set-default var val)
235 (global-set-key org-index-dispatch-key 'org-index-dispatch))
236 :type 'key-sequence)
238 (defcustom org-index-idle-delay 68
239 "Delay in seconds after which buffer will sorted or fontified when Emacs is idle."
240 :group 'org-index
241 :type 'integer)
243 (defcustom org-index-prepare-when-idle nil
244 "Fontify and sort index-table when idle to make first call faster.
245 You only need this if your index has grown so large, that first
246 invocation of `org-index' needs a noticable amount of time."
247 :group 'org-index
248 :initialize 'custom-initialize-set
249 :set (lambda (var val)
250 (set-default var val)
251 (when val
252 (setq org-index--align-interactive 200)
253 (run-with-idle-timer org-index-idle-delay nil 'org-index--idle-prepare)))
254 :type 'boolean)
256 (defcustom org-index-yank-after-add 'ref
257 "Specifies which column should be yanked after adding a new index row.
258 Valid values are some columns of index table."
259 :group 'org-index
260 :type '(choice
261 (const ref)
262 (const category)
263 (const keywords)))
265 (defcustom org-index-copy-heading-to-keywords t
266 "When adding a new node to index: Copy heading to keywords-column ?"
267 :group 'org-index
268 :type '(choice (const :tag "Yes" t)
269 (const :tag "No" nil)))
271 (defcustom org-index-strip-ref-and-date-from-heading t
272 "When adding a node to index: strip leading ref or timestamps ?
274 This can be useful, if you have the habit of adding refs and
275 dates to the start of your headings; then, if you change your
276 heading and want to update your index, you do not need to remove
277 those pieces."
278 :group 'org-index
279 :type '(choice (const :tag "Yes" t)
280 (const :tag "No" nil)))
282 (defcustom org-index-edit-on-add '(category keywords)
283 "List of columns to edit when adding a new row."
284 :group 'org-index
285 :type '(repeat (choice
286 (const category)
287 (const keywords))))
289 (defcustom org-index-edit-on-yank '(keywords yank)
290 "List of columns to edit when adding new text to yank."
291 :group 'org-index
292 :type '(repeat (choice
293 (const yank)
294 (const category)
295 (const keywords))))
297 (defcustom org-index-edit-on-ref '(category keywords)
298 "List of columns to edit when adding new ref."
299 :group 'org-index
300 :type '(repeat (choice
301 (const category)
302 (const keywords))))
304 (defcustom org-index-clock-into-focus nil
305 "Clock into focused node."
306 :group 'org-index
307 :type 'boolean)
309 ;; Variables to hold the configuration of the index table
310 (defvar org-index--maxrefnum nil "Maximum number from reference table, e.g. 153.")
311 (defvar org-index--head nil "Header before number (e.g. 'R').")
312 (defvar org-index--tail nil "Tail after number (e.g. '}' or ')'.")
313 (defvar org-index--numcols nil "Number of columns in index table.")
314 (defvar org-index--ref-regex nil "Regular expression to match a reference.")
315 (defvar org-index--ref-format nil "Format, that can print a reference.")
316 (defvar org-index--columns nil "Columns of index-table.")
317 (defvar org-index--buffer nil "Buffer of index table.")
318 (defvar org-index--point nil "Position at start of headline of index table.")
319 (defvar org-index--below-hline nil "Position of first cell in first line below hline.")
320 (defvar org-index--saved-positions nil "Saved positions within current buffer and index buffer; filled by ‘org-index--save-positions’.")
321 (defvar org-index--headings nil "Headlines of index-table as a string.")
322 (defvar org-index--headings-visible nil "Visible part of headlines of index-table as a string.")
323 (defvar org-index--ids-focused-nodes nil "Ids of focused node (if any).")
324 (defvar org-index--id-last-goto-focus nil "Id of last node, that has been focused to.")
326 ;; Variables to hold context and state
327 (defvar org-index--last-fingerprint nil "Fingerprint of last line created.")
328 (defvar org-index--category-before nil "Category of node before.")
329 (defvar org-index--active-region nil "Active region, initially. I.e. what has been marked.")
330 (defvar org-index--below-cursor nil "Word below cursor.")
331 (defvar org-index--within-index-node nil "True, if we are within node of the index table.")
332 (defvar org-index--within-occur nil "True, if we are within the occur-buffer.")
333 (defvar org-index--message-text nil "Text that was issued as an explanation; helpful for regression tests.")
334 (defvar org-index--occur-help-text nil "Text for help in occur buffer.")
335 (defvar org-index--occur-help-overlay nil "Overlay for help in occur buffer.")
336 (defvar org-index--occur-stack nil "Stack with overlays for hiding lines.")
337 (defvar org-index--occur-tail-overlay nil "Overlay to cover invisible lines.")
338 (defvar org-index--occur-lines-collected 0 "Number of lines collected in occur buffer; helpful for tests.")
339 (defvar org-index--last-sort-assumed nil "Last column, the index has been sorted after (best guess).")
340 (defvar org-index--sort-timer nil "Timer to sort index in correct order.")
341 (defvar org-index--aligned 0 "For this Emacs session: remember number of table lines aligned.")
342 (defvar org-index--align-interactive most-positive-fixnum "Number of rows to align in ‘org-index--parse-table’.")
343 (defvar org-index--edit-widgets nil "List of widgets used to edit.")
344 (defvar org-index--context-index nil "Position and line used for index in edit buffer.")
345 (defvar org-index--context-occur nil "Position and line used for occur in edit buffer.")
346 (defvar org-index--context-node nil "Buffer and position for node in edit buffer.")
347 (defvar org-index--short-help-buffer-name "*org-index commands*" "Name of buffer to display short help.")
348 (defvar org-index--display-short-help nil "True, if short help should be displayed.")
349 (defvar org-index--short-help-displayed nil "True, if short help message has been displayed.")
350 (defvar org-index--prefix-arg nil "True, if prefix argument has been received during input.")
351 (defvar org-index--minibuffer-saved-key nil "Temporarily save entry of minibuffer keymap.")
352 (defvar org-index--after-focus-timer nil "Timer to clock in or update focused node after a delay.")
353 (defvar org-index--after-focus-context nil "Context for after focus action.")
354 (defvar org-index--set-focus-time nil "Last time-value, when focus has been set.")
356 ;; static information for this program package
357 (defconst org-index--commands '(occur add kill head ping index ref yank column edit help short-help focus example sort find-ref highlight maintain) "List of commands available.")
358 (defconst org-index--valid-headings '(ref id created last-accessed count keywords category level yank tags) "All valid headings.")
359 (defconst org-index--occur-buffer-name "*org-index-occur*" "Name of occur buffer.")
360 (defconst org-index--edit-buffer-name "*org-index-edit*" "Name of edit buffer.")
361 (defvar org-index--short-help-text nil "Cache for result of `org-index--get-short-help-text.")
362 (defvar org-index--shortcut-chars nil "Cache for result of `org-index--get-shortcut-chars.")
363 (defvar org-index--after-focus-delay 10 "Number of seconds to wait before invoking after-focus action.")
366 (defmacro org-index--on (column value &rest body)
367 "Execute the forms in BODY with point on index line whose COLUMN is VALUE.
368 The value returned is the value of the last form in BODY or nil,
369 if VALUE cannot be found."
370 (declare (indent 2) (debug t))
371 (let ((pointvar (make-symbol "point")) ; avoid clash with same-named variables in body
372 (foundvar (make-symbol "found"))
373 (retvar (make-symbol "ret")))
374 `(save-current-buffer
375 (let ((,pointvar (point))
376 ,foundvar
377 ,retvar)
379 (set-buffer org-index--buffer)
381 (setq ,foundvar (org-index--go ,column ,value))
382 (when ,foundvar
383 (setq ,retvar (progn ,@body)))
385 (goto-char ,pointvar)
387 ,retvar))))
390 (defun org-index (&optional command search-ref arg)
391 "Fast search-index for selected org nodes and things outside.
393 This function creates and updates an index table with keywords;
394 each line either points to a heading in org, references something
395 outside or carries a snippet of text to yank. The index table is
396 searched for keywords by means of an incremental occur; results
397 are sorted by usage count and date, so that frequently used
398 entries appear first.
400 References are decorated numbers (e.g. 'R237' or '--455--'); they are
401 well suited to be used outside of org, e.g. in folder names, ticket
402 systems or on printed documents.
404 On first invocation this function will help to create a dedicated node
405 for its index table.
407 To start building up your index, use subcommands 'add', 'ref' and
408 'yank' to create entries and use 'occur' to find them.
410 This is version 5.4.1 of org-index.el.
413 The function `org-index' is the only interactive function of this
414 package and its main entry point; it will present you with a list
415 of subcommands to choose from:
417 \(Note the one-letter shortcuts, e.g. [o]; used like `\\[org-index-dispatch] o'.)
419 occur: [o] Incrementally show matching lines from index.
420 Result is updated after every keystroke. You may enter a
421 list of words seperated by space or comma (`,'), to select
422 lines that contain all of the given words. With a numeric
423 prefix argument, show lines, which have been accessed at
424 most this many days ago.
426 add: [a] Add the current node to index.
427 So that (e.g.) it can be found through the subcommand
428 'occur'. Update index, if node is already present.
430 kill: [k] Kill (delete) the current node from index.
431 Can be invoked from index, from occur or from a headline.
433 head: [h] Search for heading, by ref or from index line.
434 If invoked from within index table, go to associated
435 node (if any), otherwise ask for ref to search.
437 index: [i] Enter index table and maybe go to a specific reference.
438 Use `org-mark-ring-goto' (\\[org-mark-ring-goto]) to go back.
440 ping: [p] Echo line from index table for current node.
441 If current node is not in index, than search among its
442 parents.
444 ref: [r] Create a new index line with a reference.
445 This line will not be associated with a node.
447 yank: [y] Store a new string, that can be yanked from occur.
448 The index line will not be associated with a node.
450 column: [c] From within index table: read char and jump to column.
451 Shortcut for column movement; stays within one index line.
453 edit: [e] Present current line in edit buffer.
454 Can be invoked from index, from occur or from a headline.
456 focus: [f] Return to first focused node; repeat to see them all.
457 The focused nodes are kept in a short list; they need not be
458 part of the index though. This command visits one focus node
459 after the other, as long as you invoke it in quick succession
460 and without moving to other nodes; otherwise it returns to
461 the focus node, where you left off. Finally, with a prefix
462 argument, this command offers more options, e.g. to set focus
463 in the first place.
465 help: Show complete help text of `org-index'.
466 I.e. this text.
468 short-help: [?] Show this one-line description of each subcommand.
469 I.e. from the complete help, show only the first line for each
470 subcommand.
472 example: Create an example index, that will not be saved.
473 May serve as an example.
475 sort: Sort lines in index, in region or buffer.
476 Region or buffer can be sorted by contained reference; Index
477 by count, reference or last access.
479 find-ref: Search for given reference in all org-buffers.
480 A wrapper to employ Emacs standard `multi-occur' function;
481 asks for reference.
483 highlight: Highlight or unhighlight all references.
484 Operates on active region or whole buffer. Call with prefix
485 argument (`C-u') to remove highlights.
487 maintain: Index maintainance.
488 Offers some choices to check, update or fix your index.
490 If you invoke `org-index' for the first time, an assistant will be
491 invoked, that helps you to create your own index.
493 Invoke `org-customize' to tweak the behaviour of `org-index'.
495 This includes the global key `org-index-dispatch-key' to invoke
496 the most important subcommands with one additional key.
498 A numeric prefix argument is used as a reference number for
499 commands, that need one (e.g. 'head') or to modify their
500 behaviour (e.g. 'occur').
502 Also, a single prefix argument may be specified just before the
503 final character (e.g. like `C-c i C-u f') or by just typing an
504 upper case letter (e.g. `C-c i F').
506 Use from elisp: Optional argument COMMAND is a symbol naming the
507 command to execute. SEARCH-REF specifies a reference to search
508 for, if needed. ARG allows passing in a prefix argument as in
509 interactive calls."
511 (interactive "i\ni\nP")
513 (let (search-id ; id to search for
514 search-fingerprint ; fingerprint to search for
515 sort-what ; sort what ?
516 kill-new-text ; text that will be appended to kill ring
517 message-text) ; text that will be issued as an explanation
519 (catch 'new-index
522 ;; Initialize and parse
525 ;; creates index table, if necessary
526 (org-index--verify-id)
528 ;; Get configuration of index table
529 (org-index--parse-table org-index--align-interactive t)
531 ;; store context information
532 (org-index--retrieve-context)
536 ;; Arrange for proper sorting of index
539 ;; lets assume, that it has been sorted this way (we try hard to make sure)
540 (unless org-index--last-sort-assumed (setq org-index--last-sort-assumed org-index-sort-by))
541 ;; rearrange for index beeing sorted into default sort order after 300 secs of idle time
542 (unless org-index--sort-timer
543 (setq org-index--sort-timer
544 (run-with-idle-timer org-index-idle-delay t 'org-index--sort-silent)))
548 ;; Find out, what we are supposed to do
551 ;; Check or read command
552 (if (and command (not (eq command 'short-help)))
553 (unless (memq command org-index--commands)
554 (error "Unknown command '%s' passed as argument, valid choices are any of these symbols: %s"
555 command (mapconcat 'symbol-name org-index--commands ",")))
557 ;; read command; if requested display help in read-loop
558 (setq org-index--display-short-help (eq command 'short-help))
559 (setq command (org-index--read-command))
560 (if org-index--prefix-arg (setq arg (or arg '(4))))
561 (setq org-index--display-short-help nil))
564 ;; Get search string, if required; process possible sources one after
565 ;; another (lisp argument, prefix argument, user input).
568 ;; Try prefix, if no lisp argument given
569 (if (and (not search-ref)
570 (numberp arg))
571 (setq search-ref (format "%s%d%s" org-index--head arg org-index--tail)))
573 ;; These actions really need a search string and may even prompt for it
574 (when (memq command '(index head find-ref))
576 ;; search from surrounding text ?
577 (unless search-ref
578 (if org-index--within-index-node
580 (if (org-at-table-p)
581 (setq search-ref (org-index--get-or-set-field 'ref)))
583 (if (and org-index--below-cursor
584 (string-match (concat "\\(" org-index--ref-regex "\\)")
585 org-index--below-cursor))
586 (setq search-ref (match-string 1 org-index--below-cursor)))))
588 ;; If we still do not have a search string, ask user explicitly
589 (unless search-ref
590 (if (eq command 'index)
591 (let ((r (org-index--read-search-for-index)))
592 (setq search-ref (cl-first r))
593 (setq search-id (cl-second r))
594 (setq search-fingerprint (cl-third r)))
595 (unless (and (eq command 'head)
596 org-index--within-index-node
597 (org-at-table-p))
598 (setq search-ref (read-from-minibuffer "Search reference number: ")))))
600 ;; Clean up search string
601 (when search-ref
602 (setq search-ref (org-trim search-ref))
603 (if (string-match "^[0-9]+$" search-ref)
604 (setq search-ref (concat org-index--head search-ref org-index--tail)))
605 (if (string= search-ref "") (setq search-ref nil)))
607 (if (and (not search-ref)
608 (not (eq command 'index))
609 (not (and (eq command 'head)
610 org-index--within-index-node
611 (org-at-table-p))))
612 (error "Command %s needs a reference number" command)))
616 ;; Command sort needs to know in advance, what to sort for
619 (when (eq command 'sort)
620 (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))))
624 ;; Enter table
627 ;; Arrange for beeing able to return
628 (when (and (memq command '(occur head index example sort maintain focus))
629 (not (string= (buffer-name) org-index--occur-buffer-name)))
630 (org-mark-ring-push))
632 ;; These commands will leave user in index table after they are finished
633 (when (or (memq command '(index maintain))
634 (and (eq command 'sort)
635 (eq sort-what 'index)))
637 (pop-to-buffer-same-window org-index--buffer)
638 (goto-char org-index--point)
639 (org-index--unfold-buffer))
643 ;; Actually do, what is requested
646 (cond
648 ((eq command 'help)
650 ;; bring up help-buffer for this function
651 (describe-function 'org-index))
654 ((eq command 'short-help)
656 (org-index--display-short-help))
659 ((eq command 'find-ref)
661 ;; Construct list of all org-buffers
662 (let (org-buffers)
663 (dolist (buff (buffer-list))
664 (set-buffer buff)
665 (if (string= major-mode "org-mode")
666 (setq org-buffers (cons buff org-buffers))))
668 ;; Do multi-occur
669 (multi-occur org-buffers (org-index--make-guarded-search search-ref))
671 ;; Present results
672 (if (get-buffer "*Occur*")
673 (progn
674 (setq message-text (format "Found '%s'" search-ref))
675 (other-window 1)
676 (toggle-truncate-lines 1))
677 (setq message-text (format "Did not find '%s'" search-ref)))))
680 ((eq command 'add)
682 (let ((r (org-index--do-add-or-update (if (equal arg '(4)) t nil)
683 (if (numberp arg) arg nil))))
684 (setq message-text (car r))
685 (setq kill-new-text (cdr r))))
688 ((eq command 'kill)
689 (setq message-text (org-index--do-kill)))
692 ((eq command 'head)
694 (if (and org-index--within-index-node
695 (org-at-table-p))
696 (setq search-id (org-index--get-or-set-field 'id)))
698 (if (and (not search-id) search-ref)
699 (setq search-id (org-index--id-from-ref search-ref)))
701 (setq message-text
702 (if search-id
703 (org-index--find-id search-id)
704 "Current line has no id")))
707 ((eq command 'index)
709 (goto-char org-index--below-hline)
711 (setq message-text
713 (if search-ref
714 (if (org-index--go 'ref search-ref)
715 (progn
716 (org-index--update-current-line)
717 (org-table-goto-column (org-index--column-num 'ref))
718 (format "Found index line '%s'" search-ref))
719 (format "Did not find index line with reference '%s'" search-ref))
721 (if search-id
722 (if (org-index--go 'id search-id)
723 (progn
724 (org-index--update-current-line)
725 (org-table-goto-column (org-index--column-num 'ref))
726 (format "Found index line '%s'" (org-index--get-or-set-field 'ref)))
727 (format "Did not find index line with id '%s'" search-id))
729 (if search-fingerprint
730 (if (org-index--go 'fingerprint org-index--last-fingerprint)
731 (progn
732 (org-index--update-current-line)
733 (beginning-of-line)
734 (format "Found latest index line"))
735 (format "Did not find index line"))
737 ;; simply go into table
738 "At index table"))))
740 (recenter))
743 ((eq command 'ping)
745 (let ((moved-up 0) id info reached-top done)
747 (unless (string= major-mode "org-mode") (error "No node at point"))
748 ;; take id from current node or reference
749 (setq id (if search-ref
750 (org-index--id-from-ref search-ref)
751 (org-id-get)))
753 ;; move up until we find a node in index
754 (save-excursion
755 (outline-back-to-heading)
756 (while (not done)
757 (if id
758 (setq info (org-index--on 'id id
759 (mapcar (lambda (x) (org-index--get-or-set-field x))
760 (list 'keywords 'count 'created 'last-accessed 'category 'ref)))))
762 (setq reached-top (= (org-outline-level) 1))
764 (if (or info reached-top)
765 (setq done t)
766 (outline-up-heading 1 t)
767 (cl-incf moved-up))
769 (setq id (org-id-get))))
771 (if info
772 (progn
773 (setq message-text
774 (apply 'format
775 (append (list "'%s'%s has been accessed %s times between %s and %s; category is '%s', reference is '%s'"
776 (pop info)
777 (if (> moved-up 0) (format " (parent node, %d level up)" moved-up) ""))
778 info)))
779 (setq kill-new-text (car (last info))))
780 (setq message-text "Neither this node nor any of its parents is part of index"))))
783 ((eq command 'occur)
785 (set-buffer org-index--buffer)
786 (org-index--do-occur (if (numberp arg) arg nil)))
789 ((eq command 'ref)
791 (let (args newref)
793 (setq args (org-index--collect-values-from-user org-index-edit-on-ref))
794 (setq newref (org-index--get-save-maxref))
795 (setq args (plist-put args 'ref newref))
796 (apply 'org-index--do-new-line args)
798 (setq kill-new-text newref)
800 (setq message-text (format "Added new row with ref '%s'" newref))))
803 ((eq command 'yank)
805 (let (args)
807 (setq args (org-index--collect-values-from-user org-index-edit-on-yank))
808 (if (plist-get args 'yank)
809 (plist-put args 'yank (replace-regexp-in-string "|" "\\vert" (plist-get args 'yank) nil 'literal)))
810 (setq args (plist-put args 'category "yank"))
811 (apply 'org-index--do-new-line args)
813 (setq message-text "Added new row with text to yank")))
816 ((eq command 'column)
818 (if (and org-index--within-index-node
819 (org-at-table-p))
820 (let (char col num)
821 (setq char (read-char "Please specify, which column to go to (r=ref, k=keywords, c=category, y=yank): "))
822 (unless (memq char (list ?r ?k ?c ?y))
823 (error (format "Invalid char '%c', cannot goto this column" char)))
824 (setq col (cdr (assoc char '((?r . ref) (?k . keywords) (?c . category) (?y . yank)))))
825 (setq num (org-index--column-num col))
826 (if num
827 (progn
828 (org-table-goto-column num)
829 (setq message-text (format "At column %s" (symbol-name col))))
831 (error (format "Column '%s' is not present" col))))
832 (error "Need to be in index table to go to a specific column")))
835 ((eq command 'edit)
837 (setq message-text (org-index--do-edit)))
840 ((eq command 'sort)
842 (let ((sorts (list "count" "last-accessed" "mixed" "id" "ref"))
843 sort groups-and-counts)
845 (cond
846 ((eq sort-what 'index)
847 (setq sort
848 (intern
849 (completing-read
850 "Please choose column to sort index table: "
851 (cl-copy-list sorts)
852 nil t nil nil (symbol-name org-index-sort-by))))
854 (org-index--do-sort-index sort)
855 (org-table-goto-column (org-index--column-num (if (eq sort 'mixed) 'last-access sort)))
856 ;; When saving index, it should again be sorted correctly
857 (with-current-buffer org-index--buffer
858 (add-hook 'before-save-hook 'org-index--sort-silent t))
860 (setq message-text
861 (format
862 (concat "Your index has been sorted temporarily by %s and will be sorted again by %s after %d seconds of idle time"
863 (if groups-and-counts
864 "; %d groups with equal %s and a total of %d lines have been found"
865 ""))
866 (symbol-name sort)
867 org-index-sort-by
868 org-index-idle-delay
869 (cl-second groups-and-counts)
870 (symbol-name sort)
871 (cl-third groups-and-counts))))
873 ((memq sort-what '(region buffer))
874 (org-index--do-sort-lines sort-what)
875 (setq message-text (format "Sorted %s by contained references" sort-what))))))
878 ((eq command 'highlight)
880 (let ((where "buffer"))
881 (save-excursion
882 (save-restriction
883 (when (and transient-mark-mode
884 mark-active)
885 (narrow-to-region (region-beginning) (region-end))
886 (setq where "region"))
888 (if arg
889 (progn
890 (unhighlight-regexp org-index--ref-regex)
891 (setq message-text (format "Removed highlights for references in %s" where)))
892 (highlight-regexp org-index--ref-regex 'isearch)
893 (setq message-text (format "Highlighted references in %s" where)))))))
896 ((eq command 'focus)
897 (setq message-text (if arg
898 (org-index--more-focus-commands)
899 (org-index--goto-focus))))
902 ((eq command 'maintain)
903 (setq message-text (org-index--do-maintain)))
906 ((eq command 'example)
908 (if (y-or-n-p "This assistant will help you to create a temporary index with detailed comments.\nDo you want to proceed ? ")
909 (org-index--create-index t)))
912 ((not command) (setq message-text "No command given"))
915 (t (error "Unknown subcommand '%s'" command)))
918 ;; tell, what we have done and what can be yanked
919 (if kill-new-text (setq kill-new-text
920 (substring-no-properties kill-new-text)))
921 (if (string= kill-new-text "") (setq kill-new-text nil))
922 (let ((m (concat
923 message-text
924 (if (and message-text kill-new-text)
925 " and r"
926 (if kill-new-text "R" ""))
927 (if kill-new-text (format "eady to yank '%s'." kill-new-text) (if message-text "." "")))))
928 (unless (string= m "")
929 (message m)
930 (setq org-index--message-text m)))
931 (if kill-new-text (kill-new kill-new-text)))))
934 (defun org-index-dispatch (&optional arg)
935 "Read additional chars and call subcommands of `org-index'.
936 Can be bound in global keyboard map as central entry point.
937 Optional argument ARG is passed on."
938 (interactive "P")
939 (let (char command (c-u-text (if arg " C-u " "")))
940 (while (not char)
941 (if (sit-for 1)
942 (message (concat "org-index (? for detailed prompt) -" c-u-text)))
943 (setq char (key-description (read-key-sequence nil)))
944 (if (string= char "C-g") (keyboard-quit))
945 (if (string= char "SPC") (setq char "?"))
946 (when (string= char (upcase char))
947 (setq char (downcase char))
948 (setq arg (or arg '(4))))
949 (when (string= char "C-u")
950 (setq arg (or arg '(4)))
951 (setq c-u-text " C-u ")
952 (setq char nil)))
953 (setq command (cdr (assoc char (org-index--get-shortcut-chars))))
954 (unless command
955 (message "No subcommand for '%s'; switching to detailed prompt" char)
956 (sit-for 1)
957 (setq command 'short-help))
958 (org-index command nil arg)))
961 (defun org-index-new-line (&rest keys-values)
962 "Create a new line within the index table, returning its reference.
964 The function takes a varying number of argument pairs; each pair
965 is a symbol for an existing column heading followed by its value.
966 The return value is the new reference.
968 Example:
970 (message \"Created reference %s\"
971 (org-index-new-line 'keywords \"foo bar\" 'category \"baz\"))
973 Optional argument KEYS-VALUES specifies content of new line."
975 (let ((ref (plist-get keys-values 'ref)))
976 (org-index--verify-id)
977 (org-index--parse-table)
978 (if (not (memq ref '(t nil)))
979 (error "Column 'ref' accepts only 't' or 'nil'"))
980 (when ref
981 (setq ref (org-index--get-save-maxref))
982 (setq keys-values (plist-put keys-values 'ref ref)))
984 (apply 'org-index--do-new-line keys-values)
985 ref))
988 (defun org-index--read-command (&optional with-short-help)
989 "Read subcommand for ‘org-index’ from minibuffer.
990 Optional argument WITH-SHORT-HELP displays help screen upfront."
991 (let (minibuffer-scroll-window
992 minibuffer-setup-fun
993 command)
994 (setq org-index--short-help-displayed nil)
995 (setq org-index--prefix-arg nil)
996 (add-hook 'minibuffer-setup-hook 'org-index--minibuffer-setup-function)
997 (add-hook 'minibuffer-exit-hook 'org-index--minibuffer-exit-function)
998 (unwind-protect
999 (setq command
1000 (completing-read
1001 (concat
1002 "Please choose"
1003 (if org-index--display-short-help "" " (? for short help)")
1004 ": ")
1005 (append (mapcar 'symbol-name org-index--commands)
1006 (mapcar 'upcase-initials (mapcar 'symbol-name org-index--commands)))
1007 nil t))
1008 (remove-hook 'minibuffer-setup-hook 'org-index--minibuffer-setup-function)
1009 (remove-hook 'minibuffer-exit-hook 'org-index--minibuffer-exit-function)
1010 (unless (string= command (downcase command))
1011 (setq command (downcase command))
1012 (setq org-index--prefix-arg '(4)))
1013 (setq command (intern command))
1014 (when org-index--short-help-displayed
1015 (quit-windows-on org-index--short-help-buffer-name)))
1016 command))
1019 (defun org-index--minibuffer-setup-function ()
1020 "Prepare minibuffer for `org-index--read-command'."
1021 (setq org-index--minibuffer-saved-key (local-key-binding (kbd "?")))
1022 (local-set-key (kbd "?") 'org-index--display-short-help)
1023 (local-set-key (kbd "C-u") (lambda () (interactive)
1024 (setq org-index--prefix-arg t)
1025 (message "C-u")))
1026 (if org-index--display-short-help (org-index--display-short-help)))
1029 (defun org-index--minibuffer-exit-function ()
1030 "Restore minibuffer after `org-index--read-command'."
1031 (local-set-key (kbd "?") org-index--minibuffer-saved-key)
1032 (local-set-key (kbd "C-u") 'universal-argument)
1033 (setq org-index--minibuffer-saved-key nil))
1036 (defun org-index--display-short-help ()
1037 "Helper function to show help in minibuffer."
1038 (interactive)
1040 (with-temp-buffer-window
1041 org-index--short-help-buffer-name nil nil
1042 (setq org-index--short-help-displayed t)
1043 (princ "Short help; shortcuts in []; capital letter acts like C-u.\n")
1044 (princ (org-index--get-short-help-text)))
1045 (with-current-buffer org-index--short-help-buffer-name
1046 (let ((inhibit-read-only t)
1047 height-before height-after win)
1048 (setq win (get-buffer-window))
1049 (setq height-before (window-height win))
1050 (shrink-window-if-larger-than-buffer win)
1051 (setq height-after (window-height win))
1052 (goto-char (point-min))
1053 (end-of-line)
1054 (goto-char (point-min)))))
1057 (defun org-index--get-short-help-text ()
1058 "Extract text for short help message from long help."
1059 (or org-index--short-help-text
1060 (with-temp-buffer
1061 (insert (documentation 'org-index))
1062 (goto-char (point-min))
1063 (search-forward (concat " " (symbol-name (cl-first org-index--commands)) ": "))
1064 (forward-line 0)
1065 (kill-region (point-min) (point))
1066 (search-forward (concat " " (symbol-name (car (last org-index--commands))) ": "))
1067 (forward-line 1)
1068 (kill-region (point) (point-max))
1069 (keep-lines "^ [-a-z]+:" (point-min) (point-max))
1070 (align-regexp (point-min) (point-max) "\\(\\s-*\\):")
1071 (goto-char (point-min))
1072 (while (re-search-forward "\\. *$" nil t)
1073 (replace-match "" nil nil))
1074 (goto-char (point-min))
1075 (re-search-forward "short-help")
1076 (end-of-line)
1077 (insert " (this text)")
1078 (goto-char (point-min))
1079 (unless (= (line-number-at-pos (point-max)) (1+ (length org-index--commands)))
1080 (error "Internal error, unable to properly extract one-line descriptions of subcommands"))
1081 (setq org-index--short-help-text (buffer-string)))))
1084 (defun org-index--get-shortcut-chars ()
1085 "Collect shortcut chars from short help message."
1086 (or org-index--shortcut-chars
1087 (with-temp-buffer
1088 (insert (org-index--get-short-help-text))
1089 (goto-char (point-min))
1090 (while (< (point) (point-max))
1091 (when (looking-at "^ \\([-a-z]+\\)[ \t]+: +\\[\\([a-z?]\\)\\] ")
1092 (setq org-index--shortcut-chars
1093 (cons (cons (match-string 2) (intern (match-string 1)))
1094 org-index--shortcut-chars)))
1095 (forward-line 1))
1096 (unless (> (length org-index--shortcut-chars) 0)
1097 (error "Internal error, did not find shortcut chars"))
1098 org-index--shortcut-chars)))
1101 (defun org-index--goto-focus ()
1102 "Goto focus node, one after the other."
1103 (if org-index--ids-focused-nodes
1104 (let (last-id next-id here-id recent marker)
1105 (setq recent (or (not org-index--set-focus-time)
1106 (< (- (float-time (current-time))
1107 (float-time org-index--set-focus-time))
1108 org-index--after-focus-delay)))
1109 (setq last-id (or org-index--id-last-goto-focus
1110 (car (last org-index--ids-focused-nodes))))
1111 (setq here-id (org-id-get))
1112 (setq next-id
1113 (if (and recent
1114 here-id
1115 (string= here-id last-id))
1116 (car (or (cdr-safe (member last-id
1117 (append org-index--ids-focused-nodes
1118 org-index--ids-focused-nodes)))
1119 org-index--ids-focused-nodes))
1120 last-id))
1121 (unless (setq marker (org-id-find next-id 'marker))
1122 (setq org-index--id-last-goto-focus nil)
1123 (error "Could not find focus-node with id %s" next-id))
1125 (pop-to-buffer-same-window (marker-buffer marker))
1126 (goto-char (marker-position marker))
1127 (org-index--unfold-buffer)
1128 (move-marker marker nil)
1129 (setq org-index--set-focus-time (current-time))
1130 (when org-index-clock-into-focus
1131 (if org-index--after-focus-timer (cancel-timer org-index--after-focus-timer))
1132 (setq org-index--after-focus-context
1133 (cons (point-marker)
1134 next-id))
1135 (setq org-index--after-focus-timer
1136 (run-at-time org-index--after-focus-delay nil
1137 (lambda ()
1138 (if org-index-clock-into-focus
1139 (with-current-buffer (marker-buffer (car org-index--after-focus-context))
1140 (org-with-point-at (marker-position (car org-index--after-focus-context)))
1141 (org-clock-in)))
1142 (org-index--update-line (cdr org-index--after-focus-context) t)
1143 (move-marker (car org-index--after-focus-context) nil)
1144 (setq org-index--after-focus-context nil)))))
1145 (setq org-index--id-last-goto-focus next-id)
1146 (if (cdr org-index--ids-focused-nodes)
1147 (format "Jumped %s focus-node (out of %d)"
1148 (if recent "to next" "back to current")
1149 (length org-index--ids-focused-nodes))
1150 "Jumped to single focus-node"))
1151 "No nodes in focus, use set-focus"))
1154 (defun org-index--more-focus-commands ()
1155 "More commands for handling focused nodes."
1156 (let (id text char prompt)
1158 (setq prompt "Please specify action on the list focused nodes: set, append, delete (s,a,d or ? for short help) - ")
1159 (while (not (memq char (list ?s ?a ?d)))
1160 (setq char (read-char prompt))
1161 (setq prompt "Actions on list of focused nodes: s)et single focus on this node, a)ppend this node to list, d)elete this node from list. Please choose - "))
1162 (setq text
1163 (cond
1165 ((eq char ?s)
1166 (setq id (org-id-get-create))
1167 (setq org-index--ids-focused-nodes (list id))
1168 (setq org-index--id-last-goto-focus id)
1169 (if org-index-clock-into-focus (org-clock-in))
1170 "Focus has been set on current node (1 node in focus)")
1172 ((eq char ?a)
1173 (setq id (org-id-get-create))
1174 (unless (member id org-index--ids-focused-nodes)
1175 (setq org-index--ids-focused-nodes (cons id org-index--ids-focused-nodes)))
1176 (setq org-index--id-last-goto-focus id)
1177 (setq org-index--id-last-goto-focus id)
1178 (if org-index-clock-into-focus (org-clock-in))
1179 "Current node has been appended to list of focused nodes (%d node%s in focus)")
1181 ((eq char ?d)
1182 (setq id (org-id-get))
1183 (if (and id (member id org-index--ids-focused-nodes))
1184 (progn
1185 (setq org-index--id-last-goto-focus
1186 (or (car-safe (cdr-safe (member id (reverse (append org-index--ids-focused-nodes
1187 org-index--ids-focused-nodes)))))
1188 org-index--id-last-goto-focus))
1189 (setq org-index--ids-focused-nodes (delete id org-index--ids-focused-nodes))
1190 (setq org-index--id-last-goto-focus nil)
1191 "Current node has been removed from list of focused nodes (%d node%s in focus)")
1192 "Current node has not been in list of focused nodes (%d node%s in focus)"))))
1194 (with-current-buffer org-index--buffer
1195 (org-entry-put org-index--point "ids-focused-nodes" (string-join org-index--ids-focused-nodes " ")))
1197 (format text (length org-index--ids-focused-nodes) (if (cdr org-index--ids-focused-nodes) "s" ""))))
1200 (defun org-index--do-edit ()
1201 "Perform command edit."
1202 (let ((maxlen 0) cols-vals buffer-keymap field-keymap keywords-pos val)
1204 (setq org-index--context-node nil)
1205 (setq org-index--context-occur nil)
1207 ;; change to index, if whithin occur
1208 (if org-index--within-occur
1209 (let ((pos (get-text-property (point) 'org-index-lbp)))
1210 (org-index--occur-test-stale pos)
1211 (setq org-index--context-occur (cons (point) (org-index--line-in-canonical-form)))
1212 (set-buffer org-index--buffer)
1213 (goto-char pos))
1215 ;; change to index, if still not within
1216 (if (not org-index--within-index-node)
1217 (let ((id (org-id-get)))
1218 (setq org-index--context-node (cons (current-buffer) (point)))
1219 (set-buffer org-index--buffer)
1220 (unless (and id (org-index--go 'id id))
1221 (setq org-index--context-node nil)
1222 (error "This node is not in index")))))
1224 ;; retrieve current content of index line
1225 (dolist (col (mapcar 'car (reverse org-index--columns)))
1226 (if (> (length (symbol-name col)) maxlen)
1227 (setq maxlen (length (symbol-name col))))
1228 (setq val (org-index--get-or-set-field col))
1229 (if (and val (eq col 'yank)) (setq val (replace-regexp-in-string (regexp-quote "\\vert") "|" val nil 'literal)))
1230 (setq cols-vals (cons (cons col val)
1231 cols-vals)))
1233 ;; we need two different keymaps
1234 (setq buffer-keymap (make-sparse-keymap))
1235 (set-keymap-parent buffer-keymap widget-keymap)
1236 (define-key buffer-keymap (kbd "C-c C-c") 'org-index--edit-accept)
1237 (define-key buffer-keymap (kbd "C-c C-k") 'org-index--edit-abort)
1239 (setq field-keymap (make-sparse-keymap))
1240 (set-keymap-parent field-keymap widget-field-keymap)
1241 (define-key field-keymap (kbd "C-c C-c") 'org-index--edit-accept)
1242 (define-key field-keymap (kbd "C-c C-k") 'org-index--edit-abort)
1244 ;; prepare buffer
1245 (setq org-index--context-index (cons (point) (org-index--line-in-canonical-form)))
1246 (if (get-buffer org-index--edit-buffer-name) (kill-buffer org-index--edit-buffer-name))
1247 (switch-to-buffer (get-buffer-create org-index--edit-buffer-name))
1249 ;; create and fill widgets
1250 (setq org-index--edit-widgets nil)
1251 (widget-insert "Edit this line from index; type C-c C-c when done, C-c C-k to abort.\n\n")
1252 (dolist (col-val cols-vals)
1253 (if (eq (car col-val) 'keywords) (setq keywords-pos (point)))
1254 (setq org-index--edit-widgets (cons
1255 (cons (car col-val)
1256 (widget-create 'editable-field
1257 :format (format (format "%%%ds: %%%%v" maxlen) (symbol-name (car col-val)))
1258 :keymap field-keymap
1259 (or (cdr col-val) "")))
1260 org-index--edit-widgets)))
1262 (widget-setup)
1263 (goto-char keywords-pos)
1264 (beginning-of-line)
1265 (forward-char (+ maxlen 2))
1266 (use-local-map buffer-keymap)
1267 "Editing a single line from index"))
1270 (defun org-index--edit-accept ()
1271 "Function to accept editing in Edit buffer."
1272 (interactive)
1274 (let ((obuf (get-buffer org-index--occur-buffer-name))
1275 val line)
1277 ;; Time might have passed
1278 (org-index--refresh-parse-table)
1280 (with-current-buffer org-index--buffer
1282 ;; check, if buffer has become stale
1283 (save-excursion
1284 (goto-char (car org-index--context-index))
1285 (unless (string= (cdr org-index--context-index)
1286 (org-index--line-in-canonical-form))
1287 (switch-to-buffer org-index--edit-buffer-name)
1288 (error "Index table has changed: Cannot find line, that this buffer is editing")))
1290 (pop-to-buffer-same-window org-index--buffer)
1291 (goto-char (car org-index--context-index))
1293 ;; write back line to index
1294 (dolist (col-widget org-index--edit-widgets)
1295 (setq val (widget-value (cdr col-widget)))
1296 (if (eq (car col-widget) 'yank) (setq val (replace-regexp-in-string "|" (regexp-quote "\\vert") val)))
1297 (org-index--get-or-set-field (car col-widget) val))
1299 (setq line (org-index--align-and-fontify-current-line))
1300 (beginning-of-line))
1302 ;; write line to occur if appropriate
1303 (if org-index--context-occur
1304 (if obuf
1305 (if (string= (cdr org-index--context-index)
1306 (cdr org-index--context-occur))
1307 (progn
1308 (pop-to-buffer-same-window obuf)
1309 (goto-char (car org-index--context-occur))
1310 (beginning-of-line)
1311 (let ((inhibit-read-only t))
1312 (delete-region (line-beginning-position) (line-end-position))
1313 (insert line)
1314 (put-text-property (line-beginning-position) (line-end-position)
1315 'org-index-lbp (car org-index--context-index))))
1316 (error "Occur buffer and index buffer do not match any longer"))
1317 (message "Occur buffer has gone, cannot switch back."))
1318 (setq org-index--context-occur nil))
1320 ;; return to node, if invoked from there
1321 (when org-index--context-node
1322 (pop-to-buffer-same-window (car org-index--context-node))
1323 (goto-char (cdr org-index--context-node)))
1325 ;; clean up
1326 (kill-buffer org-index--edit-buffer-name)
1327 (setq org-index--context-index nil)
1328 (setq org-index--edit-widgets nil)
1329 (beginning-of-line)
1330 (message "Index line has been edited.")))
1333 (defun org-index--edit-abort ()
1334 "Function to abort editing in Edit buffer."
1335 (interactive)
1336 (kill-buffer org-index--edit-buffer-name)
1337 (setq org-index--context-index nil)
1338 (setq org-index--edit-widgets nil)
1339 (beginning-of-line)
1340 (message "Edit aborted."))
1343 (defun org-index--do-new-line (&rest keys-values)
1344 "Do the work for `org-index-new-line'.
1345 Optional argument KEYS-VALUES specifies content of new line."
1347 (org-index--retrieve-context)
1348 (with-current-buffer org-index--buffer
1349 (goto-char org-index--point)
1351 ;; check arguments early; they might come from userland
1352 (let ((kvs keys-values)
1353 k v)
1354 (while kvs
1355 (setq k (car kvs))
1356 (setq v (cadr kvs))
1357 (if (or (not (symbolp k))
1358 (and (symbolp v) (not (eq v t)) (not (eq v nil))))
1359 (error "Arguments must be alternation of key and value"))
1360 (unless (org-index--column-num k)
1361 (error "Unknown column or column not defined in table: '%s'" (symbol-name k)))
1362 (setq kvs (cddr kvs))))
1364 (let (yank)
1365 ;; create new line
1366 (org-index--create-new-line)
1368 ;; fill columns
1369 (let ((kvs keys-values)
1370 k v)
1371 (while kvs
1372 (setq k (car kvs))
1373 (setq v (cadr kvs))
1374 (org-table-goto-column (org-index--column-num k))
1375 (insert (org-trim (or v "")))
1376 (setq kvs (cddr kvs))))
1378 ;; align and fontify line
1379 (org-index--promote-current-line)
1380 (org-index--align-and-fontify-current-line)
1382 ;; remember fingerprint to be able to return
1383 (setq org-index--last-fingerprint (org-index--get-or-set-field 'fingerprint))
1385 ;; get column to yank
1386 (setq yank (org-index--get-or-set-field org-index-yank-after-add))
1388 yank)))
1391 (defun org-index-get-line (column value)
1392 "Retrieve an existing line within the index table by ref or id.
1393 Return its contents as a property list.
1395 The function `plist-get' may be used to retrieve specific elements
1396 from the result.
1398 Example:
1400 (plist-get (org-index-get-line 'ref \"R12\") 'count)
1402 retrieves the value of the count-column for reference number 12.
1404 Argument COLUMN is a symbol, either ref or id,
1405 argument VALUE specifies the value to search for."
1406 ;; check arguments
1407 (unless (memq column '(ref id keywords 'yank))
1408 (error "Argument column can only be 'ref', 'id', 'keywords' or 'yank'"))
1410 (unless value
1411 (error "Need a value to search for"))
1413 (org-index--verify-id)
1414 (org-index--parse-table)
1416 (org-index--get-line column value))
1419 (defun org-index--get-line (column value)
1420 "Find a line by ID, return its contents.
1421 Argument COLUMN and VALUE specify line to get."
1422 (let (content)
1423 (org-index--on
1424 column value
1425 (mapc (lambda (x)
1426 (if (and (numberp (cdr x))
1427 (> (cdr x) 0))
1428 (setq content (cons (car x) (cons (or (org-index--get-or-set-field (car x)) "") content)))))
1429 (reverse org-index--columns)))
1430 content))
1433 (defun org-index--ref-from-id (id)
1434 "Get reference from line ID."
1435 (org-index--on 'id id (org-index--get-or-set-field 'ref)))
1438 (defun org-index--id-from-ref (ref)
1439 "Get id from line REF."
1440 (org-index--on 'ref ref (org-index--get-or-set-field 'id)))
1443 (defun org-index--get-fingerprint ()
1444 "Get fingerprint of current line."
1445 (replace-regexp-in-string
1446 "\\s " ""
1447 (mapconcat (lambda (x) (org-index--get-or-set-field x)) '(id ref yank keywords created) "")))
1450 (defun org-index--read-search-for-index ()
1451 "Special input routine for command index."
1453 ;; Accept single char commands or switch to reading a sequence of digits
1454 (let (char prompt search-ref search-id search-fingerprint)
1456 ;; start with short prompt but give more help on next iteration
1457 (setq prompt "Please specify, where to go in index (0-9,.,space,backspace,return or ? for short help) - ")
1459 ;; read one character
1460 (while (not (memq char (append (number-sequence ?0 ?9) (list ?\d ?\b ?\r ?\j ?\s ?.))))
1461 (setq char (read-char prompt))
1462 (setq prompt "Go to specific position in index table. Digits specify a reference number, <space> goes to top of index, <backspace> or <delete> to last line created and <return> or `.' to index line of current node. Please choose - "))
1464 (if (memq char (number-sequence ?0 ?9))
1465 ;; read rest of digits
1466 (setq search-ref (read-from-minibuffer "Search reference number: " (char-to-string char))))
1467 ;; decode single chars
1468 (if (memq char '(?\r ?\n ?.)) (setq search-id (org-id-get)))
1469 (if (memq char '(?\d ?\b)) (setq search-fingerprint org-index--last-fingerprint))
1471 (list search-ref search-id search-fingerprint)))
1474 (defun org-index--verify-id ()
1475 "Check, that we have a valid id."
1477 ;; Check id
1478 (unless org-index-id
1479 (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")))
1480 (if (string= answer "create-index")
1481 (org-index--create-missing-index "Variable org-index-id is not set, so probably no index table has been created yet.")
1482 (describe-function 'org-index)
1483 (throw 'new-index nil))))
1485 ;; Find node
1486 (let (marker)
1487 (setq marker (org-id-find org-index-id 'marker))
1488 (unless marker (org-index--create-missing-index "Cannot find the node with id \"%s\" (as specified by variable org-index-id)." org-index-id))
1489 ; Try again with new node
1490 (setq marker (org-id-find org-index-id 'marker))
1491 (unless marker (error "Could not create node"))
1492 (setq org-index--buffer (marker-buffer marker)
1493 org-index--point (marker-position marker))
1494 (move-marker marker nil)))
1497 (defun org-index--retrieve-context ()
1498 "Collect context information before starting with command."
1500 ;; Get the content of the active region or the word under cursor
1501 (setq org-index--active-region
1502 (if (and transient-mark-mode mark-active)
1503 (buffer-substring (region-beginning) (region-end))
1504 nil))
1505 (setq org-index--below-cursor (thing-at-point 'symbol))
1507 ;; get category of current node
1508 (setq org-index--category-before
1509 (save-excursion ; workaround: org-get-category does not give category when at end of buffer
1510 (beginning-of-line)
1511 (org-get-category (point) t)))
1513 ;; Find out, if we are within index table or occur buffer
1514 (setq org-index--within-index-node (string= (org-id-get) org-index-id))
1515 (setq org-index--within-occur (string= (buffer-name) org-index--occur-buffer-name)))
1518 (defun org-index--parse-table (&optional num-lines-to-format check-sort-mixed)
1519 "Parse content of index table.
1520 Optional argument NUM-LINES-TO-FORMAT limits formatting effort and duration.
1521 Optional argument CHECK-SORT-MIXED triggers resorting if mixed and stale."
1523 (let (ref-field
1524 ref-num
1525 initial-point
1526 end-of-headings
1527 start-of-headings)
1529 (unless num-lines-to-format (setq num-lines-to-format 0))
1531 (with-current-buffer org-index--buffer
1533 (setq initial-point (point))
1535 (org-index--go-below-hline)
1536 (org-reveal)
1538 ;; if table is sorted mixed and it was sorted correctly yesterday, it could still be wrong today; so check
1539 (when (and check-sort-mixed (eq org-index-sort-by 'mixed))
1540 (goto-char org-index--below-hline)
1541 (let (count-first-line count-second-line)
1542 (setq count-first-line (string-to-number (concat (org-index--get-or-set-field 'count) " 0")))
1543 (forward-line)
1544 (setq count-second-line (string-to-number (concat (org-index--get-or-set-field 'count) " 0")))
1545 (forward-line -1)
1546 (if (and (string< (org-index--get-or-set-field 'last-accessed)
1547 (org-index--get-mixed-time))
1548 (< count-first-line count-second-line))
1549 (org-index--do-sort-index org-index-sort-by)))
1550 (org-index--go-below-hline))
1552 ;; align and fontify table once for this emacs session
1553 (when (> num-lines-to-format org-index--aligned)
1554 (org-index--go-below-hline)
1555 (message "Aligning and fontifying %s lines of index table (once per emacs session)..."
1556 (if (= num-lines-to-format most-positive-fixnum) "all" (format "%d" num-lines-to-format)))
1557 (save-restriction
1558 (let (from to)
1559 (forward-line -3)
1560 (setq from (point))
1561 (setq to (org-table-end))
1562 (when (< num-lines-to-format most-positive-fixnum)
1563 (forward-line (+ 3 num-lines-to-format))
1564 (narrow-to-region from (point))
1565 (setq to (min (point) to)))
1566 (goto-char org-index--below-hline)
1567 (org-table-align)
1568 (setq to (min (point-max) to))
1569 (font-lock-fontify-region from to)))
1570 (setq org-index--aligned num-lines-to-format)
1571 (org-index--go-below-hline)
1572 (message "Done."))
1574 (beginning-of-line)
1576 ;; get headings to display during occur
1577 (setq end-of-headings (point))
1578 (goto-char (org-table-begin))
1579 (setq start-of-headings (point))
1580 (setq org-index--headings-visible (substring-no-properties (org-index--copy-visible start-of-headings end-of-headings)))
1581 (setq org-index--headings (buffer-substring start-of-headings end-of-headings))
1583 ;; count columns
1584 (org-table-goto-column 100)
1585 (setq org-index--numcols (- (org-table-current-column) 1))
1587 ;; go to top of table
1588 (goto-char (org-table-begin))
1590 ;; parse line of headings
1591 (org-index--parse-headings)
1593 ;; read property or go through table to find maximum number
1594 (goto-char org-index--below-hline)
1595 (setq ref-field (or (org-entry-get org-index--point "max-ref")
1596 (org-index--migrate-maxref-to-property)))
1598 (unless org-index--head (org-index--get-decoration-from-ref-field ref-field))
1599 (setq org-index--maxrefnum (org-index--extract-refnum ref-field))
1601 ;; Get ids of focused node (if any)
1602 (setq org-index--ids-focused-nodes (split-string (or (org-entry-get nil "ids-focused-nodes") "")))
1603 (org-entry-delete (point) "id-focused-node") ; migrate (kind of) from previous versions
1605 ;; save position below hline
1606 (org-index--go-below-hline)
1607 ;; go back to initial position
1608 (goto-char initial-point))))
1611 (defun org-index--get-decoration-from-ref-field (ref-field)
1612 "Extract decoration from a REF-FIELD."
1613 (unless (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\([^0-9]*\\)$" ref-field)
1614 (org-index--report-index-error
1615 "Reference in index table ('%s') does not contain a number" ref-field))
1617 ;; These are the decorations used within the first ref of index
1618 (setq org-index--head (match-string 1 ref-field))
1619 (setq org-index--tail (match-string 3 ref-field))
1620 (setq org-index--ref-regex (concat (regexp-quote org-index--head)
1621 "\\([0-9]+\\)"
1622 (regexp-quote org-index--tail)))
1623 (setq org-index--ref-format (concat org-index--head "%d" org-index--tail)))
1626 (defun org-index--extract-refnum (ref-field)
1627 "Extract the number from a complete reference REF-FIELD like 'R102'."
1628 (unless (string-match org-index--ref-regex ref-field)
1629 (org-index--report-index-error
1630 "Reference '%s' is not formatted properly (does not match '%s')" ref-field org-index--ref-regex))
1631 (string-to-number (match-string 1 ref-field)))
1634 (defun org-index--migrate-maxref-to-property ()
1635 "One-time migration: No property; need to go through whole table once to find max."
1636 (org-index--go-below-hline)
1637 (let (ref-field ref-num ref)
1638 (message "One-time migration to set index-property maxref...")
1639 (unless org-index--maxrefnum (setq org-index--maxrefnum 0))
1640 (while (org-at-table-p)
1641 (setq ref-field (org-index--get-or-set-field 'ref))
1642 (when ref-field
1643 (unless org-index--head (org-index--get-decoration-from-ref-field ref-field))
1644 (setq ref-num (org-index--extract-refnum ref-field))
1645 (if (> ref-num org-index--maxrefnum) (setq org-index--maxrefnum ref-num)))
1646 (forward-line))
1647 (unless org-index--maxrefnum
1648 (org-index--report-index-error "No reference found in property max-ref and none in index"))
1649 (setq ref (org-index--get-save-maxref t))
1650 (org-index--go-below-hline)
1651 (message "Done.")
1652 ref))
1655 (defun org-index--get-save-maxref (&optional no-inc)
1656 "Get next reference, increment number and store it in index.
1657 Optional argument NO-INC skips automatic increment on maxref."
1658 (let (ref)
1659 (unless no-inc (setq org-index--maxrefnum (1+ org-index--maxrefnum)))
1660 (setq ref (format org-index--ref-format org-index--maxrefnum))
1661 (with-current-buffer org-index--buffer
1662 (org-entry-put org-index--point "max-ref" ref))
1663 ref))
1666 (defun org-index--refresh-parse-table ()
1667 "Fast refresh of selected results of parsing index table."
1669 (setq org-index--point (marker-position (org-id-find org-index-id 'marker)))
1670 (with-current-buffer org-index--buffer
1671 (save-excursion
1672 (org-index--go-below-hline))))
1675 (defun org-index--do-maintain ()
1676 "Choose among and perform some tasks to maintain index."
1677 (let ((check-what) (max-mini-window-height 1.0) message-text)
1678 (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 (ref or id)\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")))
1679 (message nil)
1681 (cond
1682 ((eq check-what 'check)
1683 (setq message-text (or (org-index--check-ids)
1684 "No problems found")))
1686 ((eq check-what 'statistics)
1687 (setq message-text (org-index--do-statistics)))
1689 ((eq check-what 'duplicates)
1690 (setq message-text (org-index--find-duplicates)))
1692 ((eq check-what 'clean)
1693 (let ((lines 0))
1694 (org-map-entries
1695 (lambda ()
1696 (when (org-entry-get (point) "org-index-ref")
1697 (cl-incf lines)
1698 (org-entry-delete (point) "org-index-ref")))
1699 nil 'agenda)
1700 (setq message-text (format "Removed property 'org-index-ref' from %d lines" lines))))
1702 ((eq check-what 'update)
1703 (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 invoking `add'. Are you SURE to proceed for ALL INDEX LINES ? ")
1704 (setq message-text (org-index--update-all-lines))
1705 (setq message-text "Canceled."))))
1706 message-text))
1709 (defun org-index--get-mixed-time ()
1710 "Get timestamp for sorting order mixed."
1711 (format-time-string
1712 (org-time-stamp-format t t)
1713 (apply 'encode-time (append '(0 0 0) (nthcdr 3 (decode-time))))))
1716 (defun org-index--do-sort-index (sort)
1717 "Sort index table according to SORT."
1719 (let ((is-modified (buffer-modified-p))
1721 bottom
1722 mixed-time)
1724 (unless buffer-read-only
1726 (message "Sorting index table for %s..." (symbol-name sort))
1727 (undo-boundary)
1729 (let ((message-log-max nil)) ; we have just issued a message, dont need those of sort-subr
1731 ;; if needed for mixed sort
1732 (if (eq sort 'mixed)
1733 (setq mixed-time (org-index--get-mixed-time)))
1735 ;; get boundaries of table
1736 (org-index--go-below-hline)
1737 (forward-line 0)
1738 (setq top (point))
1739 (goto-char (org-table-end))
1741 ;; kill all empty rows at bottom
1742 (while (progn
1743 (forward-line -1)
1744 (org-table-goto-column 1)
1745 (and
1746 (not (org-index--get-or-set-field 'ref))
1747 (not (org-index--get-or-set-field 'id))
1748 (not (org-index--get-or-set-field 'yank))))
1749 (org-table-kill-row))
1750 (forward-line 1)
1751 (setq bottom (point))
1753 ;; sort lines
1754 (save-restriction
1755 (narrow-to-region top bottom)
1756 (goto-char top)
1757 (sort-subr t
1758 'forward-line
1759 'end-of-line
1760 (lambda ()
1761 (org-index--get-sort-key sort t mixed-time))
1763 'string<)
1764 (goto-char (point-min))
1766 ;; restore modification state
1767 (set-buffer-modified-p is-modified)))
1769 (setq org-index--last-sort-assumed sort))))
1772 (defun org-index--do-sort-lines (what)
1773 "Sort lines in WHAT according to contained reference."
1774 (save-restriction
1775 (cond
1776 ((eq what 'region)
1777 (if (region-active-p)
1778 (narrow-to-region (region-beginning) (region-end))
1779 (error "No active region, cannot sort")))
1780 ((eq what 'buffer)
1781 (unless (y-or-n-p "Sort whole current buffer ? ")
1782 (error "Canceled"))
1783 (narrow-to-region (point-min) (point-max))))
1785 (goto-char (point-min))
1786 (sort-subr nil 'forward-line 'end-of-line
1787 (lambda ()
1788 (if (looking-at (concat ".*"
1789 (org-index--make-guarded-search org-index--ref-regex 'dont-quote)))
1790 (string-to-number (match-string 1))
1791 0)))))
1794 (defun org-index--go-below-hline ()
1795 "Move below hline in index-table."
1797 (let ((errstring (format "index table within node %s" org-index-id)))
1799 (goto-char org-index--point)
1801 ;; go to heading of node
1802 (while (not (org-at-heading-p)) (forward-line -1))
1803 (forward-line 1)
1805 ;; go to first table, but make sure we do not get into another node
1806 (while (and (not (org-at-table-p))
1807 (not (org-at-heading-p))
1808 (not (eobp)))
1809 (forward-line))
1811 ;; check, if there really is a table
1812 (unless (org-at-table-p)
1813 (org-index--create-missing-index "Cannot find %s." errstring))
1815 ;; go just after hline
1816 (while (and (not (org-at-table-hline-p))
1817 (org-at-table-p))
1818 (forward-line))
1819 (forward-line)
1821 ;; and check
1822 (unless (org-at-table-p)
1823 (org-index--report-index-error "Cannot find a hline within %s" errstring))
1825 (org-table-goto-column 1)
1826 (setq org-index--below-hline (point))))
1829 (defun org-index--parse-headings ()
1830 "Parse headings of index table."
1832 (let (field ;; field content
1833 field-symbol) ;; and as a symbol
1835 (setq org-index--columns nil)
1837 ;; For each column
1838 (dotimes (col org-index--numcols)
1840 (setq field (substring-no-properties (downcase (org-trim (org-table-get-field (+ col 1))))))
1842 (if (string= field "")
1843 (error "Heading of column cannot be empty"))
1844 (if (and (not (string= (substring field 0 1) "."))
1845 (not (member (intern field) org-index--valid-headings)))
1846 (error "Column name '%s' is not a valid heading (custom headings may start with a dot, e.g. '.foo')" field))
1848 (setq field-symbol (intern field))
1850 ;; check if heading has already appeared
1851 (if (assoc field-symbol org-index--columns)
1852 (org-index--report-index-error
1853 "'%s' appears two times as column heading" (downcase field))
1854 ;; add it to list at front, reverse later
1855 (setq org-index--columns (cons (cons field-symbol (+ col 1)) org-index--columns)))))
1857 (setq org-index--columns (reverse org-index--columns))
1859 ;; check if all necessary headings have appeared
1860 (mapc (lambda (head)
1861 (unless (cdr (assoc head org-index--columns))
1862 (org-index--report-index-error "No column has heading '%s'" head)))
1863 org-index--valid-headings))
1866 (defun org-index--create-missing-index (&rest reasons)
1867 "Create a new empty index table with detailed explanation. Argument REASONS explains why."
1869 (org-index--ask-before-create-index "Cannot find index table: "
1870 "new permanent" "."
1871 reasons)
1872 (org-index--create-index))
1875 (defun org-index--report-index-error (&rest reasons)
1876 "Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with."
1878 (when org-index--buffer
1879 (pop-to-buffer-same-window org-index--buffer)
1880 (goto-char org-index--below-hline)
1881 (org-reveal t))
1882 (org-index--ask-before-create-index "The existing index contains this error: "
1883 "temporary" ", to compare with."
1884 reasons)
1885 (org-index--create-index t t))
1888 (defun org-index--ask-before-create-index (explanation type for-what reasons)
1889 ; checkdoc-params: (explanation type for-what reasons)
1890 "Ask the user before creating an index or throw error. Arguments specify bits of issued message."
1891 (let (reason prompt)
1893 (setq reason (apply 'format reasons))
1895 (setq prompt (concat explanation reason "\n"
1896 "However, this assistant can help you to create a "
1897 type " index with detailed comments" for-what "\n\n"
1898 "Do you want to proceed ?"))
1900 (unless (let ((max-mini-window-height 1.0))
1901 (y-or-n-p prompt))
1902 (error (concat explanation reason)))))
1905 (defun org-index--create-index (&optional temporary compare)
1906 "Create a new empty index table with detailed explanation.
1907 specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index."
1908 (let (buffer
1909 title
1910 firstref
1913 (if temporary
1914 (let ((file-name (concat temporary-file-directory "org-index--example-index.org"))
1915 (buffer-name "*org-index-example-index*"))
1916 (setq buffer (get-buffer-create buffer-name))
1917 (with-current-buffer buffer
1918 ;; but it needs a file for its index to be found
1919 (unless (string= (buffer-file-name) file-name)
1920 (set-visited-file-name file-name))
1921 (rename-buffer buffer-name) ; name is change by line above
1923 (erase-buffer)
1924 (org-mode)))
1926 (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))))))
1928 (setq title (read-from-minibuffer "Please enter the title of the index node (leave empty for default 'index'): "))
1929 (if (string= title "") (setq title "index"))
1931 (while (progn
1932 (setq firstref (read-from-minibuffer "Please enter your first reference-number. This is an integer number preceeded by some and optionally followed by some non-numeric 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 frequently within your existing notes, to avoid too many false hits when searching.\n\nPlease choose (leave empty for default 'R1'): "))
1933 (if (string= firstref "") (setq firstref "R1"))
1934 (let (desc)
1935 (when (string-match "[[:blank:]]" firstref)
1936 (setq desc "Contains whitespace"))
1937 (when (string-match "[[:cntrl:]]" firstref)
1938 (setq desc "Contains control characters"))
1939 (unless (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)
1940 ;; firstref not okay, report details
1941 (setq desc
1942 (cond ((string= firstref "") "is empty")
1943 ((not (string-match "^[^0-9]+" firstref)) "starts with a digit")
1944 ((not (string-match "^[^0-9]+[0-9]+" firstref)) "does not contain a number")
1945 ((not (string-match "^[^0-9]+[0-9]+[^0-9]*$" firstref)) "contains more than one sequence of digits")
1948 (if desc
1949 (progn
1950 (read-from-minibuffer (format "Your input '%s' does not meet the requirements because it %s.\nPlease hit RET and try again: " firstref desc))
1952 nil))))
1954 (with-current-buffer buffer
1955 (goto-char (point-max))
1956 (insert (format "* %s %s\n" firstref title))
1957 (org-entry-put org-index--point "max-ref" firstref)
1958 (if temporary
1959 (insert "
1960 Below you find your temporary index table, which WILL NOT LAST LONGER
1961 THAN YOUR CURRENT EMACS SESSION; please use it only for evaluation.
1963 (insert "
1964 Below you find your initial index table, which will grow over time.
1966 (insert " You may start using it by adding some lines. Just
1967 move to another heading within org, invoke `org-index' and
1968 choose the command 'add'. After adding a few nodes, try the
1969 command 'occur' to search among them.
1971 To gain further insight you may invoke the subcommand 'help', or
1972 (same content) read the help of `org-index'.
1974 Within the index table below, the sequence of columns does not
1975 matter. You may reorder them in any way you like. You may also
1976 add your own columns, which should start with a dot
1977 (e.g. '.my-column').
1979 Invoke `org-customize' to tweak the behaviour of org-index
1980 (see the group org-index).
1982 This node needs not be a top level node; its name is completely
1983 at your choice; it is found through its ID only.
1985 (unless temporary
1986 (insert "
1987 Remark: These lines of explanation can be removed at any time.
1990 (setq id (org-id-get-create))
1991 (insert (format "
1993 | ref | category | keywords | tags | count | level | last-accessed | created | id | yank |
1994 | | | | | | | | | <4> | <4> |
1995 |-----+----------+----------+------+-------+-------+---------------+---------+-----+------|
1996 | %s | | %s | | | | | %s | %s | |
1999 firstref
2000 title
2001 (with-temp-buffer (org-insert-time-stamp nil nil t))
2002 id))
2004 ;; make sure, that node can be found
2005 (org-id-add-location id (buffer-file-name))
2006 (setq buffer-save-without-query t)
2007 (basic-save-buffer)
2009 (while (not (org-at-table-p)) (forward-line -1))
2010 (unless buffer-read-only (org-table-align))
2011 (while (not (org-at-heading-p)) (forward-line -1))
2013 ;; read back some info about new index
2014 (let ((org-index-id id))
2015 (org-index--verify-id))
2017 ;; remember at least for this session
2018 (setq org-index-id id)
2020 ;; present results to user
2021 (if temporary
2022 (progn
2023 ;; Present existing and temporary index together
2024 (when compare
2025 (pop-to-buffer-same-window org-index--buffer)
2026 (goto-char org-index--point)
2027 (org-index--unfold-buffer)
2028 (delete-other-windows)
2029 (select-window (split-window-vertically)))
2030 ;; show new index
2031 (pop-to-buffer-same-window buffer)
2032 (org-id-goto id)
2033 (org-index--unfold-buffer)
2034 (if compare
2035 (progn
2036 (message "Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index")
2037 (throw 'new-index nil))
2038 (message "This is your new temporary index, use command add to populate, occur to search.")))
2039 (progn
2040 ;; Only show the new index
2041 (pop-to-buffer-same-window buffer)
2042 (delete-other-windows)
2043 (org-id-goto id)
2044 (org-index--unfold-buffer)
2045 (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 ? ")
2046 (progn
2047 (customize-save-variable 'org-index-id id)
2048 (message "Saved org-index-id '%s' to %s." id (or custom-file
2049 user-init-file))
2050 (throw 'new-index nil))
2051 (let (sq)
2052 (setq sq (format "(setq org-index-id \"%s\")" id))
2053 (kill-new sq)
2054 (message "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)
2055 (throw 'new-index nil))))))))
2058 (defun org-index--unfold-buffer ()
2059 "Helper function to unfold buffer."
2060 (org-show-context)
2061 (org-show-subtree)
2062 (recenter 1)
2063 (save-excursion
2064 (org-back-to-heading)
2065 (forward-line) ;; on property drawer
2066 (org-cycle)))
2069 (defun org-index--update-line (&optional id-or-pos no-error)
2070 "Update columns count and last-accessed in line ID-OR-POS.
2071 Optional argument NO-ERROR suppresses error."
2073 (let (initial)
2075 (with-current-buffer org-index--buffer
2076 (unless buffer-read-only
2078 (setq initial (point))
2080 (if (if (integerp id-or-pos)
2081 (goto-char id-or-pos)
2082 (org-index--go 'id id-or-pos))
2083 (org-index--update-current-line)
2084 (unless no-error (error "Did not find reference or id '%s'" (list id-or-pos))))
2086 (goto-char initial)))))
2089 (defun org-index--update-current-line ()
2090 "Update current lines columns count and last-accessed."
2091 (let (newcount (count-field (org-index--get-or-set-field 'count)))
2093 ;; update count field only if number or empty
2094 (when (or (not count-field)
2095 (string-match "^[0-9]+$" count-field))
2096 (setq newcount (+ 1 (string-to-number (or count-field "0"))))
2097 (org-index--get-or-set-field 'count
2098 (number-to-string newcount)))
2100 ;; update timestamp
2101 (org-table-goto-column (org-index--column-num 'last-accessed))
2102 (org-table-blank-field)
2103 (org-insert-time-stamp nil t t)
2105 ;; move line according to new content
2106 (org-index--promote-current-line)
2107 (org-index--align-and-fontify-current-line)))
2110 (defun org-index--align-and-fontify-current-line (&optional num)
2111 "Make current line (or NUM lines) blend well among others."
2112 (let (lines lines-fontified)
2113 ;; get current content
2114 (unless num (setq num 1))
2115 (setq lines (delete-and-extract-region (line-beginning-position) (line-end-position num)))
2116 ;; create minimum table with fixed-width columns to align and fontify new line
2117 (insert
2118 (setq
2119 lines-fontified
2120 (with-temp-buffer
2121 (org-set-font-lock-defaults)
2122 (insert org-index--headings-visible)
2123 ;; fill columns, so that aligning cannot shrink them
2124 (goto-char (point-min))
2125 (search-forward "|")
2126 (while (search-forward " " (line-end-position) t)
2127 (replace-match "." nil t))
2128 (goto-char (point-min))
2129 (while (search-forward ".|." (line-end-position) t)
2130 (replace-match " | " nil t))
2131 (goto-char (point-min))
2132 (while (search-forward "|." (line-end-position) t)
2133 (replace-match "| " nil t))
2134 (goto-char (point-max))
2135 (insert lines)
2136 (forward-line 0)
2137 (let ((start (point)))
2138 (while (re-search-forward "^\s +|-" nil t)
2139 (replace-match "| -"))
2140 (goto-char start))
2141 (org-mode)
2142 (org-table-align)
2143 (font-lock-fontify-region (point-min) (point-max))
2144 (goto-char (point-max))
2145 (if (eq -1 (skip-chars-backward "\n"))
2146 (delete-char 1))
2147 (forward-line (- 1 num))
2148 (buffer-substring (line-beginning-position) (line-end-position num)))))
2149 lines-fontified))
2152 (defun org-index--promote-current-line ()
2153 "Move current line up in table according to changed sort fields."
2154 (let (begin end key
2155 (to-skip 0))
2157 (forward-line 0) ; stay at beginning of line
2159 (setq key (org-index--get-sort-key))
2160 (setq begin (point))
2161 (setq end (line-beginning-position 2))
2163 (forward-line -1)
2164 (while (and (org-at-table-p)
2165 (not (org-at-table-hline-p))
2166 (string< (org-index--get-sort-key) key))
2168 (cl-incf to-skip)
2169 (forward-line -1))
2170 (forward-line 1)
2172 ;; insert line at new position
2173 (when (> to-skip 0)
2174 (insert (delete-and-extract-region begin end))
2175 (forward-line -1))))
2178 (defun org-index--get-sort-key (&optional sort with-ref mixed-time)
2179 "Get value for sorting from column SORT, optional WITH-REF; if mixes use MIXED-TIME."
2180 (let (ref
2181 ref-field
2182 key)
2184 (unless sort (setq sort org-index--last-sort-assumed)) ; use default value
2186 (when (or with-ref
2187 (eq sort 'ref))
2188 ;; get reference with leading zeroes, so it can be
2189 ;; sorted as text
2190 (setq ref-field (org-index--get-or-set-field 'ref))
2191 (if ref-field
2192 (progn
2193 (string-match org-index--ref-regex ref-field)
2194 (setq ref (format
2195 "%06d"
2196 (string-to-number
2197 (match-string 1 ref-field)))))
2198 (setq ref "000000")))
2200 (setq key
2201 (cond
2202 ((eq sort 'count)
2203 (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))
2204 ((eq sort 'mixed)
2205 (let ((last-accessed (org-index--get-or-set-field 'last-accessed)))
2206 (unless mixed-time (setq mixed-time (org-index--get-mixed-time)))
2207 (concat
2208 (if (string< mixed-time last-accessed) last-accessed mixed-time)
2209 (format "%08d" (string-to-number (or (org-index--get-or-set-field 'count) ""))))))
2210 ((eq sort 'ref)
2211 ref)
2212 ((memq sort '(id last-accessed created))
2213 (org-index--get-or-set-field sort))
2214 (t (error "This is a bug: unmatched case '%s'" sort))))
2216 (if with-ref (setq key (concat key ref)))
2218 key))
2221 (defun org-index--get-or-set-field (key &optional value)
2222 "Retrieve field KEY from index table or set it to VALUE."
2223 (let (field)
2224 (save-excursion
2225 (if (eq key 'fingerprint)
2226 (progn
2227 (if value (error "Internal error, pseudo-column fingerprint cannot be set"))
2228 (setq field (org-index--get-fingerprint)))
2229 (setq field (org-trim (org-table-get-field (cdr (assoc key org-index--columns)) value))))
2230 (if (string= field "") (setq field nil))
2232 (org-no-properties field))))
2235 (defun org-index--column-num (key)
2236 "Return number of column KEY."
2237 (if (numberp key)
2239 (cdr (assoc key org-index--columns))))
2242 (defun org-index--make-guarded-search (ref &optional dont-quote)
2243 "Make robust search string from REF; DONT-QUOTE it, if requested."
2244 (concat "\\_<" (if dont-quote ref (regexp-quote ref)) "\\_>"))
2247 (defun org-index--find-duplicates ()
2248 "Find duplicate references or ids in index table."
2249 (let (ref-duplicates id-duplicates)
2251 (setq ref-duplicates (org-index--find-duplicates-helper 'ref))
2252 (setq id-duplicates (org-index--find-duplicates-helper 'id))
2253 (goto-char org-index--below-hline)
2254 (if (or ref-duplicates id-duplicates)
2255 (progn
2256 ;; show results
2257 (pop-to-buffer-same-window
2258 (get-buffer-create "*org-index-duplicates*"))
2259 (when ref-duplicates
2260 (insert "These references appear more than once:\n")
2261 (mapc (lambda (x) (insert " " x "\n")) ref-duplicates)
2262 (insert "\n\n"))
2263 (when id-duplicates
2264 (insert "These ids appear more than once:\n")
2265 (mapc (lambda (x) (insert " " x "\n")) id-duplicates))
2267 "Some references or ids are duplicates")
2268 "No duplicate references or ids found")))
2271 (defun org-index--find-duplicates-helper (column)
2272 "Helper for `org-index--find-duplicates': Go through table and count given COLUMN."
2273 (let (counts duplicates field found)
2275 ;; go through table
2276 (goto-char org-index--below-hline)
2277 (while (org-at-table-p)
2279 ;; get column
2280 (setq field (org-index--get-or-set-field column))
2282 ;; and increment
2283 (setq found (assoc field counts))
2284 (if found
2285 (cl-incf (cdr found))
2286 (setq counts (cons (cons field 1) counts)))
2288 (forward-line))
2290 (mapc (lambda (x) (if (and (> (cdr x) 1)
2291 (car x))
2292 (setq duplicates (cons (car x) duplicates)))) counts)
2294 duplicates))
2297 (defun org-index--do-statistics ()
2298 "Compute statistics about index table."
2299 (let ((total-lines 0) (total-refs 0)
2300 ref ref-field min max message)
2302 ;; go through table
2303 (goto-char org-index--below-hline)
2304 (while (org-at-table-p)
2306 ;; get ref
2307 (setq ref-field (org-index--get-or-set-field 'ref))
2309 (when ref-field
2310 (string-match org-index--ref-regex ref-field)
2311 (setq ref (string-to-number (match-string 1 ref-field)))
2313 ;; record min and max
2314 (if (or (not min) (< ref min)) (setq min ref))
2315 (if (or (not max) (> ref max)) (setq max ref))
2317 (setq total-refs (1+ total-refs)))
2319 ;; count
2320 (setq total-lines (1+ total-lines))
2322 (forward-line))
2324 (setq message (format "%d Lines in index table. First reference is %s, last %s; %d of them are used (%d percent)"
2325 total-lines
2326 (format org-index--ref-format min)
2327 (format org-index--ref-format max)
2328 total-refs
2329 (truncate (* 100 (/ (float total-refs) (1+ (- max min)))))))
2331 (goto-char org-index--below-hline)
2332 message))
2335 (defun org-index--do-add-or-update (&optional create-ref tag-with-ref)
2336 "For current node or current line in index, add or update in index table.
2337 CREATE-REF and TAG-WITH-REF if given."
2339 (let* (id id-from-index ref args yank ret)
2341 (org-index--save-positions)
2342 (unless (or org-index--within-index-node
2343 org-index--within-occur)
2344 (org-back-to-heading))
2346 ;; try to do the same things from within index and from outside
2347 (if org-index--within-index-node
2349 (progn
2350 (unless (org-at-table-p)
2351 (error "Within index node but not on table"))
2353 (setq id (org-index--get-or-set-field 'id))
2354 (setq ref (org-index--get-or-set-field 'ref))
2355 (setq args (org-index--collect-values-for-add-update-remote id))
2356 (org-index--write-fields args)
2357 (setq yank (org-index--get-or-set-field org-index-yank-after-add))
2359 (setq ret
2360 (if ref
2361 (cons (format "Updated index line %s" ref) yank)
2362 (cons "Updated index line" nil))))
2364 (setq id (org-id-get-create))
2365 (org-index--refresh-parse-table)
2366 (setq id-from-index (org-index--on 'id id id))
2367 (setq ref (org-index--on 'id id (org-index--get-or-set-field 'ref)))
2369 (if tag-with-ref
2370 (org-toggle-tag (format "%s%d%s" org-index--head tag-with-ref org-index--tail) 'on))
2371 (setq args (org-index--collect-values-for-add-update id))
2373 (when (and create-ref
2374 (not ref))
2375 (setq ref (org-index--get-save-maxref))
2376 (setq args (plist-put args 'ref ref)))
2379 (if id-from-index
2380 ;; already have an id in index, find it and update fields
2381 (progn
2383 (org-index--on
2384 'id id
2385 (org-index--write-fields args)
2386 (setq yank (org-index--get-or-set-field org-index-yank-after-add)))
2388 (setq ret
2389 (if ref
2390 (cons (format "Updated index line %s" ref) yank)
2391 (cons "Updated index line" nil))))
2393 ;; no id here, create new line in index
2394 (if ref (setq args (plist-put args 'ref ref)))
2395 (setq yank (apply 'org-index--do-new-line args))
2397 (setq ret
2398 (if ref
2399 (cons
2400 (format "Added new index line %s" ref)
2401 (concat yank " "))
2402 (cons
2403 "Added new index line"
2404 nil)))))
2406 (org-index--restore-positions)
2408 ret))
2411 (defun org-index--check-ids ()
2412 "Check, that ids really point to a node."
2414 (let ((lines 0)
2415 id ids marker)
2417 (goto-char org-index--below-hline)
2419 (catch 'problem
2420 (while (org-at-table-p)
2422 (when (setq id (org-index--get-or-set-field 'id))
2424 ;; check for double ids
2425 (when (member id ids)
2426 (org-table-goto-column (org-index--column-num 'id))
2427 (throw 'problem "This id appears twice in index; please use command 'maintain' to check for duplicate ids"))
2428 (cl-incf lines)
2429 (setq ids (cons id ids))
2431 ;; check, if id is valid
2432 (setq marker (org-id-find id t))
2433 (unless marker
2434 (org-table-goto-column (org-index--column-num 'id))
2435 (throw 'problem "This id cannot be found")))
2437 (forward-line))
2439 (goto-char org-index--below-hline)
2440 nil)))
2443 (defun org-index--update-all-lines ()
2444 "Update all lines of index at once."
2446 (let ((lines 0)
2447 id ref kvs)
2449 ;; check for double ids
2451 (org-index--check-ids)
2453 (progn
2454 (goto-char org-index--below-hline)
2455 (while (org-at-table-p)
2457 ;; update single line
2458 (when (setq id (org-index--get-or-set-field 'id))
2459 (setq ref (org-index--get-or-set-field 'ref))
2460 (setq kvs (org-index--collect-values-for-add-update-remote id))
2461 (org-index--write-fields kvs)
2462 (cl-incf lines))
2463 (forward-line))
2465 (goto-char org-index--below-hline)
2466 (org-table-align)
2467 (format "Updated %d lines" lines)))))
2470 (defun org-index--collect-values-for-add-update (id &optional silent category)
2471 "Collect values for adding or updating line specified by ID, do not ask if SILENT, use CATEGORY, if given."
2473 (let ((args (list 'id id))
2474 content)
2476 (dolist (col (mapcar 'car org-index--columns))
2478 (setq content "")
2480 (cond
2481 ((eq col 'keywords)
2482 (if org-index-copy-heading-to-keywords
2483 (setq content (nth 4 (org-heading-components))))
2485 ;; Shift ref and timestamp ?
2486 (if org-index-strip-ref-and-date-from-heading
2487 (dotimes (i 2)
2488 (if (or (string-match (concat "^\\s-*" org-index--ref-regex) content)
2489 (string-match (concat "^\\s-*" org-ts-regexp-both) content))
2490 (setq content (substring content (match-end 0)))))))
2492 ((eq col 'category)
2493 (setq content (or category org-index--category-before)))
2495 ((eq col 'level)
2496 (setq content (number-to-string (org-outline-level))))
2498 ((eq col 'tags)
2499 (setq content (org-get-tags-string))))
2501 (unless (string= content "")
2502 (setq args (plist-put args col content))))
2504 (if (not silent)
2505 (let ((args-edited (org-index--collect-values-from-user org-index-edit-on-add args)))
2506 (setq args (append args-edited args))))
2508 args))
2511 (defun org-index--collect-values-for-add-update-remote (id)
2512 "Wrap `org-index--collect-values-for-add-update' by prior moving to remote node identified by ID."
2514 (let (marker point args)
2516 (setq marker (org-id-find id t))
2517 ;; enter buffer and collect information
2518 (with-current-buffer (marker-buffer marker)
2519 (setq point (point))
2520 (goto-char marker)
2521 (setq args (org-index--collect-values-for-add-update id t (org-get-category (point) t)))
2522 (goto-char point))
2524 args))
2527 (defun org-index--collect-values-from-user (cols &optional defaults)
2528 "Collect values for adding a new line.
2529 Argument COLS gives list of columns to edit.
2530 Optional argument DEFAULTS gives default values."
2532 (let (content args)
2534 (dolist (col cols)
2536 (setq content "")
2538 (setq content (read-from-minibuffer
2539 (format "Enter text for column '%s': " (symbol-name col))
2540 (plist-get col defaults)))
2542 (unless (string= content "")
2543 (setq args (plist-put args col content))))
2544 args))
2547 (defun org-index--write-fields (kvs)
2548 "Update current line with values from KVS (keys-values)."
2549 (while kvs
2550 (org-index--get-or-set-field (car kvs) (org-trim (cadr kvs)))
2551 (setq kvs (cddr kvs))))
2554 (defun org-index--do-kill ()
2555 "Perform command kill from within occur, index or node."
2557 (let (id ref chars-deleted-index text-deleted-from pos-in-index)
2559 (org-index--save-positions)
2560 (unless (or org-index--within-index-node
2561 org-index--within-occur)
2562 (org-back-to-heading))
2564 ;; Collect information: What should be deleted ?
2565 (if (or org-index--within-occur
2566 org-index--within-index-node)
2568 (progn
2569 (if org-index--within-index-node
2570 ;; In index
2571 (setq pos-in-index (point))
2572 ;; In occur
2573 (setq pos-in-index (get-text-property (point) 'org-index-lbp))
2574 (org-index--occur-test-stale pos-in-index)
2575 (set-buffer org-index--buffer)
2576 (goto-char pos-in-index))
2577 ;; In Index (maybe moved there)
2578 (setq id (org-index--get-or-set-field 'id))
2579 (setq ref (org-index--get-or-set-field 'ref)))
2581 ;; At a headline
2582 (setq id (org-entry-get (point) "ID"))
2583 (setq ref (org-index--ref-from-id id))
2584 (setq pos-in-index (org-index--on 'id id (point)))
2585 (unless pos-in-index (error "This node is not in index")))
2587 ;; Remark: Current buffer is not certain here, but we have all the information to delete
2589 ;; Delete from node
2590 (when id
2591 (let ((m (org-id-find id 'marker)))
2592 (set-buffer (marker-buffer m))
2593 (goto-char m)
2594 (move-marker m nil)
2595 (unless (string= (org-id-get) id)
2596 (error "Could not find node with id %s" id)))
2598 (org-index--delete-any-ref-from-tags)
2599 (if ref (org-index--delete-ref-from-heading ref))
2600 (setq text-deleted-from (cons "node" text-deleted-from)))
2602 ;; Delete from index
2603 (set-buffer org-index--buffer)
2604 (unless pos-in-index "Internal error, pos-in-index should be defined here")
2605 (goto-char pos-in-index)
2606 (setq chars-deleted-index (length (delete-and-extract-region (line-beginning-position) (line-beginning-position 2))))
2607 (setq text-deleted-from (cons "index" text-deleted-from))
2609 ;; Delete from occur only if we started there, accept that it will be stale otherwise
2610 (if org-index--within-occur
2611 (let ((inhibit-read-only t))
2612 (set-buffer org-index--occur-buffer-name)
2613 (delete-region (line-beginning-position) (line-beginning-position 2))
2614 ;; correct positions
2615 (while (org-at-table-p)
2616 (put-text-property (line-beginning-position) (line-end-position) 'org-index-lbp
2617 (- (get-text-property (point) 'org-index-lbp) chars-deleted-index))
2618 (forward-line))
2619 (setq text-deleted-from (cons "occur" text-deleted-from))))
2621 (org-index--restore-positions)
2622 (concat "Deleted from: " (mapconcat 'identity (sort text-deleted-from 'string<) ","))))
2625 (defun org-index--save-positions ()
2626 "Save current buffer and positions in index- and current buffer; not in occur-buffer."
2628 (let (cur-buf cur-mrk idx-pnt idx-mrk)
2629 (setq cur-buf (current-buffer))
2630 (setq cur-mrk (point-marker))
2631 (set-buffer org-index--buffer)
2632 (if (string= (org-id-get) org-index-id)
2633 (setq idx-pnt (point))
2634 (setq idx-mrk (point-marker)))
2635 (set-buffer cur-buf)
2636 (setq org-index--saved-positions (list cur-buf cur-mrk idx-pnt idx-mrk))))
2639 (defun org-index--restore-positions ()
2640 "Restore positions as saved by `org-index--save-positions'."
2642 (cl-multiple-value-bind
2643 (cur-buf cur-mrk idx-pnt idx-mrk buf)
2644 org-index--saved-positions
2645 (setq buf (current-buffer))
2646 (set-buffer cur-buf)
2647 (goto-char cur-mrk)
2648 (set-buffer org-index--buffer)
2649 (goto-char (or idx-pnt idx-mrk))
2650 (set-buffer buf))
2651 (setq org-index--saved-positions nil))
2654 (defun org-index--delete-ref-from-heading (ref)
2655 "Delete given REF from current heading."
2656 (save-excursion
2657 (end-of-line)
2658 (let ((end (point)))
2659 (beginning-of-line)
2660 (when (search-forward ref end t)
2661 (delete-char (- (length ref)))
2662 (just-one-space)))))
2665 (defun org-index--delete-any-ref-from-tags ()
2666 "Delete any reference from list of tags."
2667 (let (new-tags)
2668 (mapc (lambda (tag)
2669 (unless (string-match org-index--ref-regex tag)
2670 (setq new-tags (cons tag new-tags) )))
2671 (org-get-tags))
2672 (org-set-tags-to new-tags)))
2675 (defun org-index--go (column value)
2676 "Position cursor on index line where COLUMN equals VALUE.
2677 Return t or nil, leave point on line or at top of table, needs to be in buffer initially."
2678 (let (found)
2680 (unless (eq (current-buffer) org-index--buffer)
2681 (error "This is a bug: Not in index buffer"))
2683 (unless value
2684 (error "Cannot search for nil"))
2686 (if (string= value "")
2687 (error "Cannot search for empty string"))
2689 (if (<= (length value) 2)
2690 (warn "Searching for short string '%s' will be slow" value))
2692 (goto-char org-index--below-hline)
2693 (forward-line 0)
2694 (save-restriction
2695 (narrow-to-region (point) (org-table-end))
2696 (while (and (not found)
2697 (search-forward value nil t))
2698 (setq found (string= value (org-index--get-or-set-field column)))))
2700 ;; return value
2701 (if found
2703 (goto-char org-index--below-hline)
2704 nil)))
2707 (defun org-index--find-id (id &optional other)
2708 "Perform command head: Find node with ID and present it.
2709 If OTHER in separate window."
2711 (let (message marker)
2713 (setq marker (org-id-find id t))
2715 (if marker
2716 (progn
2717 (org-index--update-line id)
2718 (if other
2719 (progn
2720 (pop-to-buffer (marker-buffer marker)))
2721 (pop-to-buffer-same-window (marker-buffer marker)))
2723 (goto-char marker)
2724 (org-reveal t)
2725 (org-show-entry)
2726 (recenter)
2727 (unless (string= (org-id-get) id)
2728 (setq message (format "Could not go to node with id %s (narrowed ?)" id)))
2729 (setq message "Found headline"))
2730 (setq message (format "Did not find node with %s" id)))
2731 message))
2734 (defun org-index--do-occur (&optional days)
2735 "Perform command occur; optional narrow to DAYS back."
2736 (let ((word "") ; last word to search for growing and shrinking on keystrokes
2737 (prompt "Search for: ")
2738 (these-commands " NOTE: If you invoke the subcommands edit (`e') or kill (`C-c i k') from within this buffer, the index is updated accordingly")
2739 (lines-wanted (window-body-height))
2740 (lines-found 0) ; number of lines found
2741 words ; list words that should match
2742 occur-buffer
2743 begin ; position of first line
2744 help-text ; cons with help text short and long
2745 search-text ; description of text to search for
2746 done ; true, if loop is done
2747 in-c-backspace ; true, while processing C-backspace
2748 help-overlay ; Overlay with help text
2749 last-point ; Last position before end of search
2750 initial-frame ; Frame when starting occur
2751 key ; input from user in various forms
2752 key-sequence
2753 key-sequence-raw
2754 days-clause) ; clause to display for days back search
2757 ;; make and show buffer
2758 (if (get-buffer org-index--occur-buffer-name)
2759 (kill-buffer org-index--occur-buffer-name))
2760 (setq occur-buffer (make-indirect-buffer org-index--buffer org-index--occur-buffer-name))
2761 (pop-to-buffer-same-window occur-buffer)
2762 (setq initial-frame (selected-frame))
2764 ;; avoid modifying direct buffer
2765 (setq buffer-read-only t)
2766 (toggle-truncate-lines 1)
2767 (setq font-lock-keywords-case-fold-search t)
2768 (setq case-fold-search t)
2770 ;; reset stack and overlays
2771 (setq org-index--occur-stack nil)
2772 (setq org-index--occur-tail-overlay nil)
2774 ;; narrow to table rows and one line before
2775 (goto-char org-index--below-hline)
2776 (forward-line 0)
2777 (setq begin (point))
2778 (forward-line -1)
2779 (narrow-to-region (point) (org-table-end))
2780 (forward-line)
2782 ;; initialize help text
2783 (setq days-clause (if days (format " (%d days back)" days) ""))
2784 (setq help-text (cons
2785 (concat
2786 (propertize (format "Incremental occur%s" days-clause) 'face 'org-todo)
2787 (propertize "; ? toggles help and headlines.\n" 'face 'org-agenda-dimmed-todo-face))
2788 (concat
2789 (propertize
2790 (org-index--wrap
2791 (concat
2792 "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, <S-return> jumps to matching line in index; all other keys end search." these-commands "\n"))
2793 'face 'org-agenda-dimmed-todo-face)
2794 org-index--headings)))
2796 ;; insert overlays for help text and to cover unsearched lines
2797 (setq help-overlay (make-overlay (point-min) begin))
2798 (overlay-put help-overlay 'display (car help-text))
2799 (setq org-index--occur-tail-overlay (make-overlay (point-max) (point-max)))
2800 (overlay-put org-index--occur-tail-overlay 'invisible t)
2802 ;; do not enter loop if number of days is requested
2803 (when days
2804 (goto-char begin)
2805 (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted days))
2806 (move-overlay org-index--occur-tail-overlay
2807 (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
2808 (point-max))
2809 (point-max))
2811 (goto-char begin)
2812 (setq done t))
2814 ;; main loop
2815 (while (not done)
2817 (if in-c-backspace
2818 (setq key "<backspace>")
2819 (setq search-text (mapconcat 'identity (reverse (cons word words)) ","))
2821 ;; read key, if selected frame has not changed
2822 (if (eq initial-frame (selected-frame))
2823 (progn
2824 (setq key-sequence
2825 (let ((echo-keystrokes 0)
2826 (full-prompt (format "%s%s%s"
2827 prompt
2828 search-text
2829 (if (string= search-text "") "" " "))))
2830 (read-key-sequence full-prompt nil nil t t)))
2831 (setq key (key-description key-sequence))
2832 (setq key-sequence-raw (this-single-command-raw-keys)))
2833 (setq done t)
2834 (setq key-sequence nil)
2835 (setq key nil)
2836 (setq key-sequence-raw nil)))
2839 (cond
2842 ((string= key "<C-backspace>")
2843 (setq in-c-backspace t))
2846 ((member key (list "<backspace>" "DEL")) ; erase last char
2848 (if (= (length word) 0)
2850 ;; nothing more to delete from current word; try next
2851 (progn
2852 (setq word (car words))
2853 (setq words (cdr words))
2854 (setq in-c-backspace nil))
2856 ;; unhighlight longer match
2857 (unhighlight-regexp (regexp-quote word))
2859 ;; some chars are left; shorten word
2860 (setq word (substring word 0 -1))
2861 (when (= (length word) 0) ; when nothing left, use next word from list
2862 (setq word (car words))
2863 (setq words (cdr words))
2864 (setq in-c-backspace nil))
2866 ;; free top list of overlays and remove list
2867 (setq lines-found (or (org-index--unhide) lines-wanted))
2868 (move-overlay org-index--occur-tail-overlay
2869 (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
2870 (point-max))
2871 (point-max))
2873 ;; highlight shorter word
2874 (unless (= (length word) 0)
2875 (highlight-regexp (regexp-quote word) 'isearch))
2877 ;; make sure, point is still visible
2878 (goto-char begin)))
2881 ((member key (list "SPC" ",")) ; space or comma: enter an additional search word
2883 ;; push current word and clear, no need to change display
2884 (unless (string= word "")
2885 (setq words (cons word words))
2886 (setq word "")))
2889 ((string= key "?") ; question mark: toggle display of headlines and help
2890 (setq help-text (cons (cdr help-text) (car help-text)))
2891 (overlay-put help-overlay 'display (car help-text)))
2893 ((and (= (length key) 1)
2894 (aref printable-chars (elt key 0))) ; any printable char: add to current search word
2896 ;; unhighlight short word
2897 (unless (= (length word) 0)
2898 (unhighlight-regexp (regexp-quote word)))
2900 ;; add to word
2901 (setq word (concat word key))
2903 ;; make overlays to hide lines, that do not match longer word any more
2904 (goto-char begin)
2905 (setq lines-found (org-index--hide-with-overlays (cons word words) lines-wanted days))
2906 (move-overlay org-index--occur-tail-overlay
2907 (if org-index--occur-stack (cdr (assoc :end-of-visible (car org-index--occur-stack)))
2908 (point-max))
2909 (point-max))
2911 (goto-char begin)
2913 ;; highlight longer word
2914 (highlight-regexp (regexp-quote word) 'isearch)
2916 ;; make sure, point is on a visible line
2917 (line-move -1 t)
2918 (line-move 1 t))
2920 ;; anything else terminates input loop
2921 (t (setq done t))))
2923 ;; put back input event, that caused the loop to end
2924 (unless (string= key "C-g")
2925 (setq unread-command-events (listify-key-sequence key-sequence-raw))
2926 (message key))
2928 ;; postprocessing
2929 (setq last-point (point))
2931 ;; For performance reasons do not show matching lines for rest of table. So no code here.
2933 ;; make permanent copy
2934 ;; copy visible lines
2935 (let ((lines-collected 0)
2936 keymap line all-lines all-lines-lbp header-lines lbp)
2938 (setq cursor-type t)
2939 (goto-char begin)
2941 ;; collect all visible lines
2942 (while (and (not (eobp))
2943 (< lines-collected lines-wanted))
2944 ;; skip over invisible lines
2945 (while (and (invisible-p (point))
2946 (not (eobp)))
2947 (goto-char (1+ (overlay-end (car (overlays-at (point)))))))
2948 (setq lbp (line-beginning-position))
2949 (setq line (buffer-substring-no-properties lbp (line-end-position)))
2950 (unless (string= line "")
2951 (cl-incf lines-collected)
2952 (setq all-lines (cons (concat line
2953 "\n")
2954 all-lines))
2955 (setq all-lines-lbp (cons lbp all-lines-lbp)))
2956 (forward-line 1))
2958 (kill-buffer org-index--occur-buffer-name) ; cannot keep this buffer; might become stale soon
2960 ;; create new buffer
2961 (setq occur-buffer (get-buffer-create org-index--occur-buffer-name))
2962 (pop-to-buffer-same-window occur-buffer)
2963 (insert org-index--headings)
2964 (setq header-lines (line-number-at-pos))
2966 ;; insert into new buffer
2967 (save-excursion
2968 (apply 'insert (reverse all-lines))
2969 (if (= lines-collected lines-wanted)
2970 (insert "\n(more lines omitted)\n")))
2971 (setq org-index--occur-lines-collected lines-collected)
2973 (org-mode)
2974 (setq truncate-lines t)
2975 (if all-lines (org-index--align-and-fontify-current-line (length all-lines)))
2976 (when (fboundp 'font-lock-ensure)
2977 (font-lock-ensure)
2978 (font-lock-flush))
2979 (when all-lines-lbp
2980 (while (not (org-at-table-p))
2981 (forward-line -1))
2982 (while all-lines-lbp
2983 (put-text-property (line-beginning-position) (line-end-position) 'org-index-lbp (car all-lines-lbp))
2984 (setq all-lines-lbp (cdr all-lines-lbp))
2985 (forward-line -1)))
2987 ;; prepare help text
2988 (goto-char (point-min))
2989 (forward-line (1- header-lines))
2990 (setq org-index--occur-help-overlay (make-overlay (point-min) (point)))
2991 (setq org-index--occur-help-text
2992 (cons
2993 (org-index--wrap
2994 (propertize (format "Search is done%s; ? toggles help and headlines.\n" days-clause) 'face 'org-agenda-dimmed-todo-face))
2995 (concat
2996 (org-index--wrap
2997 (propertize
2998 (format
2999 (concat (format "Search is done%s." days-clause)
3000 (if (< lines-collected lines-wanted)
3001 " Showing all %d matches for "
3002 " Showing one window of matches for ")
3003 "\"" search-text
3004 "\". <return> jumps to heading, <tab> jumps to heading in other window, <S-return> jumps to matching line in index, <space> increments count.\n" these-commands "\n")
3005 (length all-lines))
3006 'face 'org-agenda-dimmed-todo-face))
3007 org-index--headings)))
3009 (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))
3011 ;; highlight words
3012 (setq case-fold-search t)
3013 (setq font-lock-keywords-case-fold-search t)
3014 (mapc (lambda (w) (unless (or (not w) (string= w "")) (highlight-regexp (regexp-quote w) 'isearch)))
3015 (cons word words))
3017 (setq buffer-read-only t)
3019 ;; install keyboard-shortcuts
3020 (setq keymap (make-sparse-keymap))
3021 (set-keymap-parent keymap org-mode-map)
3023 (mapc (lambda (x) (define-key keymap (kbd x)
3024 (lambda () (interactive)
3025 (message "%s" (org-index--occur-action)))))
3026 (list "<return>" "RET"))
3028 (define-key keymap (kbd "<tab>")
3029 (lambda () (interactive)
3030 (message (org-index--occur-action t))))
3032 (define-key keymap (kbd "e")
3033 (lambda () (interactive)
3034 (message (org-index 'edit))))
3036 (define-key keymap (kbd "SPC")
3037 (lambda () (interactive)
3038 (org-index--refresh-parse-table)
3039 ;; increment in index
3040 (let ((ref (org-index--get-or-set-field 'ref))
3041 count)
3042 (org-index--on
3043 'ref ref
3044 (setq count (+ 1 (string-to-number (org-index--get-or-set-field 'count))))
3045 (org-index--get-or-set-field 'count (number-to-string count))
3046 (org-index--promote-current-line)
3047 (org-index--align-and-fontify-current-line))
3048 ;; increment in this buffer
3049 (let ((inhibit-read-only t))
3050 (org-index--get-or-set-field 'count (number-to-string count)))
3051 (message "Incremented count to %d" count))))
3053 (define-key keymap (kbd "<S-return>")
3054 (lambda () (interactive)
3055 (let ((pos (get-text-property (point) 'org-index-lbp)))
3056 (org-index--refresh-parse-table)
3057 (org-index--occur-test-stale pos)
3058 (pop-to-buffer org-index--buffer)
3059 (goto-char pos)
3060 (org-reveal t)
3061 (org-index--update-current-line)
3062 (beginning-of-line))))
3064 (define-key keymap (kbd "?")
3065 (lambda () (interactive)
3066 (org-index--refresh-parse-table)
3067 (setq-local org-index--occur-help-text (cons (cdr org-index--occur-help-text) (car org-index--occur-help-text)))
3068 (overlay-put org-index--occur-help-overlay 'display (car org-index--occur-help-text))))
3070 (use-local-map keymap))))
3073 (defun org-index--occur-test-stale (pos)
3074 "Test, if current line in occur buffer has become stale at POS."
3075 (let (here there)
3076 (org-index--refresh-parse-table)
3077 (setq here (org-index--line-in-canonical-form))
3078 (with-current-buffer org-index--buffer
3079 (goto-char pos)
3080 (setq there (org-index--line-in-canonical-form)))
3081 (unless (string= here there)
3082 (error "Occur buffer has become stale; please repeat search"))))
3085 (defun org-index--line-in-canonical-form ()
3086 "Return current line in its canonical form."
3087 (org-trim (substring-no-properties (replace-regexp-in-string "\s +" " " (buffer-substring (line-beginning-position) (line-beginning-position 2))))))
3090 (defun org-index--wrap (text)
3091 "Wrap TEXT at fill column."
3092 (with-temp-buffer
3093 (insert text)
3094 (fill-region (point-min) (point-max) nil t)
3095 (buffer-string)))
3098 (defun org-index--occur-action (&optional other)
3099 "Helper for `org-index--occur', find heading with ref or id; if OTHER, in other window; or copy yank column."
3100 (if (org-at-table-p)
3101 (let ((id (org-index--get-or-set-field 'id))
3102 (ref (org-index--get-or-set-field 'ref))
3103 (yank (org-index--get-or-set-field 'yank)))
3104 (if id
3105 (org-index--find-id id other)
3106 (if ref
3107 (progn
3108 (org-mark-ring-goto)
3109 (format "Found reference %s (no node is associated)" ref))
3110 (if yank
3111 (progn
3112 (org-index--update-line (get-text-property (point) 'org-index-lbp))
3113 (setq yank (replace-regexp-in-string (regexp-quote "\\vert") "|" yank nil 'literal))
3114 (kill-new yank)
3115 (org-mark-ring-goto)
3116 (if (and (>= (length yank) 4) (string= (substring yank 0 4) "http"))
3117 (progn
3118 (browse-url yank)
3119 (format "Opened '%s' in browser (and copied it too)" yank))
3120 (format "Copied '%s' (no node is associated)" yank)))
3121 (error "Internal error, this line contains neither id, nor reference, nor text to yank")))))
3122 (message "Not at table")))
3125 (defun org-index--hide-with-overlays (words lines-wanted days)
3126 "Hide text that is currently visible and does not match WORDS by creating overlays;
3127 leave LINES-WANTED lines visible.
3128 Argument DAYS hides older lines."
3129 (let ((lines-found 0)
3130 (end-of-visible (point))
3131 overlay overlays start matched)
3133 ;; main loop
3134 (while (and (not (eobp))
3135 (< lines-found lines-wanted))
3137 ;; skip invisible lines
3138 (while (and (not (eobp))
3139 (and
3140 (invisible-p (point))
3141 (< (point) (overlay-start org-index--occur-tail-overlay))))
3142 (goto-char (overlay-end (car (overlays-at (point))))))
3144 ;; find stretch of lines, that are currently visible but should be invisible now
3145 (setq matched nil)
3146 (setq start (point))
3147 (while (and (not (eobp))
3148 (not (and
3149 (invisible-p (point))
3150 (< (point) (overlay-start org-index--occur-tail-overlay))))
3151 ;; either regard words or days, but not both
3152 (if days
3153 (let ((last-accessed (org-index--get-or-set-field 'last-accessed)))
3154 (if last-accessed
3155 (not (and
3156 (<= (- (time-to-days (current-time))
3157 (time-to-days (org-read-date nil t last-accessed nil)))
3158 days)
3159 (setq matched t))) ; for its side effect
3161 (not (and (org-index--test-words words)
3162 (setq matched t))))) ; for its side effect
3163 (forward-line 1))
3165 ;; create overlay to hide this stretch
3166 (when (< start (point)) ; avoid creating an empty overlay
3167 (setq overlay (make-overlay start (point)))
3168 (overlay-put overlay 'invisible t)
3169 (setq overlays (cons overlay overlays)))
3171 ;; skip and count line, that matched
3172 (when matched
3173 (forward-line 1)
3174 (setq end-of-visible (point))
3175 (cl-incf lines-found)))
3177 ;; put new list on top of stack
3178 (setq org-index--occur-stack
3179 (cons (list (cons :overlays overlays)
3180 (cons :end-of-visible end-of-visible)
3181 (cons :lines lines-found))
3182 org-index--occur-stack))
3184 lines-found))
3187 (defun org-index--unhide ()
3188 "Unhide text that does has been hidden by `org-index--hide-with-overlays'."
3189 (when org-index--occur-stack
3190 ;; delete overlays and make visible again
3191 (mapc (lambda (y)
3192 (delete-overlay y))
3193 (cdr (assoc :overlays (car org-index--occur-stack))))
3194 ;; remove from stack
3195 (setq org-index--occur-stack (cdr org-index--occur-stack))
3196 ;; return number of lines, that are now visible
3197 (if org-index--occur-stack (cdr (assoc :lines (car org-index--occur-stack))))))
3200 (defun org-index--test-words (words)
3201 "Test current line for match against WORDS."
3202 (let (line)
3203 (setq line (downcase (buffer-substring (line-beginning-position) (line-beginning-position 2))))
3204 (catch 'not-found
3205 (dolist (w words)
3206 (or (cl-search w line)
3207 (throw 'not-found nil)))
3208 t)))
3211 (defun org-index--create-new-line ()
3212 "Do the common work for `org-index-new-line' and `org-index'."
3214 ;; insert ref or id as last or first line, depending on sort-column
3215 (goto-char org-index--below-hline)
3216 (if (eq org-index-sort-by 'count)
3217 (progn
3218 (goto-char (org-table-end))
3219 (forward-line -1)
3220 (org-table-insert-row t))
3221 (org-table-insert-row))
3223 ;; insert some of the standard values
3224 (org-table-goto-column (org-index--column-num 'created))
3225 (org-insert-time-stamp nil nil t)
3226 (org-table-goto-column (org-index--column-num 'count))
3227 (insert "1"))
3230 (defun org-index--sort-silent ()
3231 "Sort index for default column to remove any effects of temporary sorting."
3232 (save-excursion
3233 (org-index--verify-id)
3234 (org-index--parse-table)
3235 (with-current-buffer org-index--buffer
3236 (save-excursion
3237 (goto-char org-index--below-hline)
3238 (org-index--do-sort-index org-index-sort-by)
3239 (remove-hook 'before-save-hook 'org-index--sort-silent)))))
3242 (defun org-index--idle-prepare ()
3243 "For parsing table when idle."
3244 (org-index--verify-id)
3245 (org-index--parse-table most-positive-fixnum t))
3248 (defun org-index--copy-visible (beg end)
3249 "Copy the visible parts of the region between BEG and END without adding it to `kill-ring'; copy of `org-copy-visible'."
3250 (let (snippets s)
3251 (save-excursion
3252 (save-restriction
3253 (narrow-to-region beg end)
3254 (setq s (goto-char (point-min)))
3255 (while (not (= (point) (point-max)))
3256 (goto-char (org-find-invisible))
3257 (push (buffer-substring s (point)) snippets)
3258 (setq s (goto-char (org-find-visible))))))
3259 (apply 'concat (nreverse snippets))))
3262 (provide 'org-index)
3264 ;; Local Variables:
3265 ;; fill-column: 75
3266 ;; comment-column: 50
3267 ;; End:
3269 ;;; org-index.el ends here