2014-01-03 12:38:17 -05:00
;;; org-index.el --- A personal index for org and beyond
2015-01-19 15:25:58 -05:00
;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
2014-01-03 12:38:17 -05:00
;; Author: Marc Ihm <org-index@2484.de>
2015-01-19 15:25:58 -05:00
;; Version: 3.1.0
2014-12-07 09:18:11 -05:00
;; Keywords: outlines index
2014-01-03 12:38:17 -05:00
;; This file is not part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Purpose:
;;
2015-01-19 15:25:58 -05:00
;; Help to navigate org. Mark and find your favorite org-headings easily:
;; Create and update an index table of references and links. This table is
;; sorted by usage count, so that the builtin incremental occur presents
;; often used entries first.
2014-01-03 12:38:17 -05:00
;;
2015-01-19 15:25:58 -05:00
;; References are essentially small numbers (e.g. "R237" or "-455-"), as
;; created by this package; they are well suited to be used outside of
;; org (e.g. within folder names). Links are normal org-mode links.
2014-01-03 12:38:17 -05:00
;;
;;
;; Setup:
;;
2014-12-07 09:18:11 -05:00
;; - Add these lines to your .emacs (modify appropriately):
2014-01-03 12:38:17 -05:00
;;
;; (require 'org-index)
2014-12-07 09:18:11 -05:00
;; (org-index-default-keybindings) ; optional
2014-01-03 12:38:17 -05:00
;;
2014-12-07 09:18:11 -05:00
;; - Restart your Emacs to make these lines effective.
2014-01-03 12:38:17 -05:00
;;
;; - Invoke `org-index', which will assist in creating your index
2014-12-07 09:18:11 -05:00
;; table. The variable org-index-id will be persisted within your
2014-01-03 12:38:17 -05:00
;; customization file (typically .emacs).
;;
;;
;; Further reading:
;;
;; See the documentation of `org-index', which can also be read
2014-12-07 09:18:11 -05:00
;; by invoking `org-index' and choosing the help-command.
2014-01-03 12:38:17 -05:00
;;
;;
;; Updates:
;;
2014-01-03 13:58:38 -05:00
;; The latest tested version of this file can always be found at:
2014-01-03 12:38:17 -05:00
;;
2014-01-28 16:42:54 -05:00
;; http://orgmode.org/w/org-mode.git?p=org-mode.git;a=blob_plain;f=contrib/lisp/org-index.el;hb=HEAD
2014-01-03 12:38:17 -05:00
;;; Change Log:
2015-01-19 15:25:58 -05:00
;; [2015-01-19 Mo] Version 3.1.0:
;; - Rewrote command "occur" with overlays in an indirect buffer
;; - Removed function `org-index-copy-references-from-heading-to-property'
;; - introduced variable org-index-version
;;
2014-12-14 11:21:14 -05:00
;; [2014-12-14 Su] Version 3.0.2:
;; - Bugfixes in occur mode
;; - New function `org-index-copy-references-from-heading-to-property'
;;
2014-12-10 16:20:55 -05:00
;; [2014-12-10 We] Version 3.0.1:
;; - Bugfixes related with assistant
;; - Fix for editing of category
;;
2014-12-07 09:18:11 -05:00
;; [2014-12-07 Sa] Version 3.0.0:
;; - New commands "add" and "delete" to easily add and remove
;; the current node to or from your index.
;; - New command "example" to create an example index.
;; - Moved flags to a list within the same node as the index table;
;; this breaks compatibility to prior versions of the package.
;; - Several new flags that are explained within index node.
;; - Removed commands "reuse", "missing", "put", "goto",
;; "update", "link", "fill", "unhighlight"
;; - New function `org-index-default-keybindings'
;;
2014-04-26 16:48:09 -04:00
;; [2014-04-26 Sa] Version 2.4.3:
2014-12-07 09:18:11 -05:00
;; - Some Bug fixes and enhancements for occur-command
2014-04-26 16:48:09 -04:00
;; - Fixes for assistant to create index table
;;
2014-02-01 17:11:18 -05:00
;; [2014-02-01 Sa] Version 2.4.2:
2014-01-28 16:42:54 -05:00
;; - Follow mode in occur-buffer
2014-02-01 17:11:18 -05:00
;; - Reorder for x-columns
2014-01-28 16:42:54 -05:00
;;
2014-01-03 12:38:17 -05:00
;; [2014-01-02 Th] Version 2.4.0:
;; - New command "put" to store a nodes reference in a property
2014-12-07 09:18:11 -05:00
;; - New functions org-index-new-line and org-index-get-line
2014-01-03 12:38:17 -05:00
;; offer access to org-index from other lisp programs
2014-02-01 17:11:18 -05:00
;; - New flag p, new columns x1,x2 and x3
2014-01-03 12:38:17 -05:00
;; - Major Code refactoring
;; - Regression tests with ert
2014-12-07 09:18:11 -05:00
;; - Lots of bug fixes
2014-01-03 12:38:17 -05:00
;;
;; [2013-10-04 Fr] Version 2.3.2:
2014-12-07 09:18:11 -05:00
;; - Bug fix: index-table created by assistant is found after
;; restart of emacs instead of invoking assistant again
2014-01-03 12:38:17 -05:00
;;
;; [2013-07-20 Sa] Version 2.3.0:
;; - Renamed from "org-favtable" to "org-index"
2014-12-07 09:18:11 -05:00
;; - Added an assistant to set up the index table
2014-01-03 12:38:17 -05:00
;; - occur is now incremental, searching as you type
;; - simplified the documentation and help-system
;; - Saving keystrokes, as "+g237" is now valid input
2014-12-07 09:18:11 -05:00
;; - Many bug fixes
2014-01-03 12:38:17 -05:00
;;
;; [2013-02-28 Th] Version 2.2.0:
;; - Allowed shortcuts like "h237" for command "head" with argument "237"
;; - Integrated with org-mark-ring-goto
;;
;; [2013-01-25 Fr] Version 2.1.0:
;; - Added full support for links
2014-12-07 09:18:11 -05:00
;; - New commands "statistics"
2014-01-03 12:38:17 -05:00
;; - Renamed the package from "org-reftable" to "org-favtable"
;; - Additional columns are required (e.g. "link"). Error messages will
;; guide you
;;
;; [2012-12-07 Fr] Version 2.0.0:
;; - The format of the table of favorites has changed ! You need to bring
;; your existing table into the new format by hand (which however is
;; easy and explained below)
;; - Reference table can be sorted after usage count or date of last access
;; - Ask user explicitly, which command to invoke
;; - Renamed the package from "org-refer-by-number" to "org-reftable"
;;
;; [2012-09-22 Sa] Version 1.5.0:
;; - New command "sort" to sort a buffer or region by reference number
;; - New commands "highlight" and "unhighlight" to mark references
;;
;; [2012-07-13 Fr] Version 1.4.0:
;; - New command "head" to find a headline with a reference number
;;
;; [2012-04-28 Sa] Version 1.3.0:
;; - New commands occur and multi-occur
;; - All commands can now be invoked explicitly
;; - New documentation
;; - Many bugfixes
;;
;; [2011-12-10 Sa] Version 1.2.0:
;; - Fixed a bug, which lead to a loss of newly created reference numbers
;; - Introduced single and double prefix arguments
;; - Started this Change Log
;;; Code:
( require 'org-table )
( require 'cl )
2014-12-07 09:18:11 -05:00
( defcustom org-index-id nil
2014-01-03 12:38:17 -05:00
" Id of the Org-mode node, which contains the index table. "
:group 'org
:group 'org-index )
2015-01-19 15:25:58 -05:00
;; Version of this package
( defvar org-index-version " 3.1.0 " " Version of `org-index' , format is major.minor.bugfix, where \" major \" is a change in index-table and \" minor \" are new features. " )
2014-01-03 12:38:17 -05:00
;; Variables to hold the configuration of the index table
2014-12-07 09:18:11 -05:00
( defvar org-index--maxref nil " Maximum number from reference table (e.g. \" 153 \" ). " )
( defvar org-index--head nil " Any header before number (e.g. \" R \" ). " )
( defvar org-index--tail nil " Tail after number (e.g. \" } \" or \" ) \" . " )
( defvar org-index--numcols nil " Number of columns in index table. " )
( defvar org-index--ref-regex nil " Regular expression to match a reference. " )
( defvar org-index--ref-format nil " Format, that can print a reference. " )
( defvar org-index--columns nil " Columns of index-table. " )
( defvar org-index--special-columns nil " Columns with flags, that may appear only once. " )
( defvar org-index--flagged-columns nil " Columns with flags, that may appear multiple times. " )
( defvar org-index--buffer nil " Buffer of index table. " )
( defvar org-index--point nil " Position at start of headline of index table. " )
( defvar org-index--below-hline nil " Position of first cell in first line below hline. " )
( defvar org-index--headings nil " Headlines of index-table as a string. " )
( defvar org-index-map nil " Keymap for shortcuts for some commands of `org-index' . Can be activated and filled by org-index-default-keybings. " )
2014-01-03 12:38:17 -05:00
;; Variables to hold context and state
2014-12-07 09:18:11 -05:00
( defvar org-index--last-action nil " Last action performed by `org-index' . " )
( defvar org-index--text-to-yank nil " Text, that can be yanked after call (mostly a reference). " )
( defvar org-index--last-ref nil " Last reference created or visited. " )
( defvar org-index--point-before nil " Point in buffer with index table. " )
( defvar org-index--point-saved nil " Saved point if we want to return. " )
( defvar org-index--category-before nil " Category of node before. " )
( defvar org-index--silent nil " Set to t, if user should not be queried. " )
( defvar org-index--active-region nil " Active region, initially. I.e. what has been marked. " )
( defvar org-index--below-cursor nil " Word below cursor. " )
( defvar org-index--within-node nil " True, if we are within node of the index table. " )
( defvar org-index--active-window-index nil " Active window with index table (if any). " )
( defvar org-index--message-text nil " Text that was issued as an explanation; helpful for regression tests. " )
2015-01-19 15:25:58 -05:00
( defvar org-index--occur-help-text nil " Text for help in occur buffer. " )
( defvar org-index--occur-help-overlay nil " Overlay for help in occur buffer. " )
2014-12-07 09:18:11 -05:00
;; static information for this program package
( defconst org-index--commands ' ( occur add delete head enter leave ref help example reorder sort multi-occur highlight statistics ) " List of commands available. " )
( defconst org-index--required-flags ' ( sort ) " Flags that are required. " )
( defconst org-index--single-flags ' ( sort point-on-add yank-after-add ) " Flags, that may only appear once; these can appear as special-columns. " )
( defconst org-index--multiple-flags ' ( edit-on-add get-category-on-add get-heading-on-add ) " Flags, that might appear multiple times. " )
( defconst org-index--all-flags ( append org-index--single-flags org-index--multiple-flags ) " All flags. " )
( defconst org-index--valid-headings ' ( ref link created last-accessed count keywords ) " All valid headings. " )
( defconst org-index--required-headings org-index--valid-headings " All required headings. " )
2015-01-19 15:25:58 -05:00
( defconst org-index--occur-buffer-name " *org-index-occur* " " Name of occur buffer. " )
2014-12-07 09:18:11 -05:00
( defconst org-index--sample-flags
"
- columns-and-flags : : associate columns of index table with flags
- ref
- yank-after-add
- . category
- get-category-on-add
- edit-on-add
- keywords
- get-heading-on-add
- edit-on-add
- point-on-add
- count
- sort
- last-accessed
- created
- link
- all-columns-explained : : All columns of the index table and their meaning
- ref : : The reference number ; will be generated automatically
- link : : link to the node, that this line represents
- created : : When has this entry been created ?
- last-accessed : : When has this entry been accessed last ?
- count : : How many times has this entry been picked ?
- keywords : : List of keywords, which may match your input during occur
- all-flags-explained : : All flags, that can be associated with columns
- sort : : Sort whole table after this column
- yank-after-add : : Let this column be yanked after picking this line
- edit-on-add : : Edit this line when adding a new one
- point-on-add : : Point will land here, when adding a new line
- get-category-on-add : : This column will receive the nodes category during add
- get-heading-on-add : : This column will receive the nodes heading during add "
" A sample string of flags. " )
2015-01-19 15:25:58 -05:00
( defun org-index ( &optional command search )
2014-01-03 12:38:17 -05:00
" Mark and find your favorite things and org-locations easily:
2014-12-07 09:18:11 -05:00
Create and update an index table of references and links. When
searching, frequently used entries appear at the top and entering
some keywords narrows down to matching entries only, so that the
right one can be spotted easily.
2014-01-03 12:38:17 -05:00
References are essentially small numbers ( e.g. \"R237\" or \"-455-\" ) ,
2014-12-07 09:18:11 -05:00
as created by this package ; they are well suited to be used
outside of org. Links are normal ` org-mode ' links.
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
This is version 3.1.0 of org-index.el .
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
The function ` org-index ' operates on a dedicated table, the index
table, which lives within its own Org-mode node. The table and
its containing node will be created, when you first invoke
` org-index '. The node will have some comments, describing the
columns of the index table and their associated flags. The index
table is found through the id of the containing node, which is
stored within the variable ` org-index-id '.
2014-01-03 12:38:17 -05:00
The function ` org-index ' is the only interactive function of this
2014-12-07 09:18:11 -05:00
package and its main entry point ; once invoked it asks, which of
it subcommands to execute:
2014-01-03 12:38:17 -05:00
occur: Incremental search, that shows matching lines from the
2014-12-07 09:18:11 -05:00
index table, updated after every keystroke. You may enter a
2015-01-19 15:25:58 -05:00
list of words seperated by space or comma ( ` , ' ) , to select
2014-01-03 12:38:17 -05:00
lines that contain all of the given words.
2014-12-07 09:18:11 -05:00
add: Add the current node to your index, so that it can be
found through the subcommand \"occur\".
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
delete: Delete the current node from your index.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
head: Ask for a reference number and search for an entry, which
either has this reference contained in its heading or within
its property org-index-ref.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
enter: Enter index table and maybe go to a specific reference.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
leave: Leave the index table and return to your previous location.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
ref: Create a new reference.
2014-01-03 12:38:17 -05:00
help: Show this text.
2014-12-07 09:18:11 -05:00
example: Create a temporary index, that will not be saved, but
may serve as an example.
2014-01-03 12:38:17 -05:00
reorder: Temporarily reorder the index table, e.g. by count,
reference or last access.
sort: Sort a set of lines ( either from the active region or the
whole buffer ) by references found in each line.
2014-12-07 09:18:11 -05:00
multi-occur: Apply Emacs standard ` multi-occur ' operation on all
` org-mode ' buffers to search for the given reference.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
highlight: Highlight or unhiglight references in active region or buffer.
2014-01-03 12:38:17 -05:00
statistics : Show some statistics ( e.g. minimum and maximum
reference ) about index table.
2014-12-07 09:18:11 -05:00
If you invoke ` org-index ' for the first time, an assistant will be
invoked, that helps you to create your own, commented index.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Use ` org-index-default-keybindings ' to establish convenient
keyboard shortcuts.
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
Optional argument COMMAND is a symbol naming the command to execute ;
SEARCH specifies search string for commands that need one. "
2014-01-03 12:38:17 -05:00
( interactive " P " )
( let ( ( org-index--silent nil ) ; t, if user can be asked
2014-12-07 09:18:11 -05:00
prefix-arg ; prefix arg
2014-01-03 12:38:17 -05:00
link-id ; link of starting node, if required
guarded-search ; with guard against additional digits
search-ref ; search, if search is a reference
search-link ; search, if search is a link
reorder-once ; column to use for single time sorting
kill-new-text ; text that will be appended to kill ring
initial-ref-or-link ; initial position in index table
2014-12-07 09:18:11 -05:00
message-text ) ; text that will be issued as an explanation
2014-01-03 12:38:17 -05:00
;;
;; Initialize and parse
;;
;; creates index table, if necessary
2014-12-07 09:18:11 -05:00
( org-index--verify-id )
2014-01-03 12:38:17 -05:00
;; Get configuration of index table
( org-index--parse-table )
2014-12-07 09:18:11 -05:00
;; store context information
( org-index--retrieve-context )
2014-01-03 12:38:17 -05:00
;;
;; Find out, what we are supposed to do
;;
2014-12-07 09:18:11 -05:00
( when ( equal command ' ( 4 ) )
( setq prefix-arg command )
( setq command nil ) )
( if command
( unless ( memq command org-index--commands )
( error " Unknown command '%s' passed as argument, valid choices are any of these symbols: %s "
command ( mapconcat 'symbol-name org-index--commands " , " ) ) )
( let ( ( r ( org-index--read-command ) ) ) ; query user if not from argument
( setq command ( car r ) )
( setq reorder-once ( cdr r ) ) ) )
2014-01-03 12:38:17 -05:00
;;
;; Get search, if required
;;
;; These actions need a search string:
2015-01-19 15:25:58 -05:00
( when ( memq command ' ( enter head multi-occur ) )
2014-01-03 12:38:17 -05:00
;; Maybe we've got a search string from the arguments
2014-12-07 09:18:11 -05:00
( setq search ( org-index--get-or-read-search search command ) )
2014-01-03 12:38:17 -05:00
( when search
2014-12-07 09:18:11 -05:00
( when ( string-match org-index--ref-regex search )
2014-01-03 12:38:17 -05:00
( setq search-ref search )
( setq guarded-search ( org-index--make-guarded-search search ) ) )
( when ( string-match " ^[a-fA-F0-9] \\ {8 \\ }-[a-fA-F0-9] \\ {4 \\ }-[a-fA-F0-9] \\ {4 \\ }-[a-fA-F0-9] \\ {4 \\ }-[a-fA-F0-9] \\ {12 \\ }$ " search )
( setq search-link search ) ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;;
;; Check for invalid combinations of arguments; try to be helpful
2014-12-07 09:18:11 -05:00
;;
( when ( and ( eq command 'head )
2014-01-03 12:38:17 -05:00
( not search-ref )
( not search-link ) )
2014-12-07 09:18:11 -05:00
( error " Can do 'head' only for a reference or link (not '%s'), try 'occur' to search for text " search ) )
2014-01-03 12:38:17 -05:00
;;
;; Sort and enter table
;;
;; Get link if required before moving in
2014-12-07 09:18:11 -05:00
( if ( eq command 'add )
( setq link-id ( org-id-get-create ) ) )
2014-01-03 12:38:17 -05:00
;; Save initial ref or link for later return
( if ( and org-index--within-node
( org-at-table-p ) )
2014-12-07 09:18:11 -05:00
( setq initial-ref-or-link
( or ( org-index--get-field 'ref )
( org-index--get-field 'link ) ) ) )
2014-01-03 12:38:17 -05:00
;; These commands enter index table only temporarily
2014-12-07 09:18:11 -05:00
( when ( memq command ' ( occur multi-occur statistics example ) )
2014-01-03 12:38:17 -05:00
( set-buffer org-index--buffer )
( goto-char org-index--point )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; Sort and align
2014-12-07 09:18:11 -05:00
( org-index--sort reorder-once )
2014-01-03 12:38:17 -05:00
( org-index--align ) )
;; These commands will leave user in index table after they are finished
2014-12-07 09:18:11 -05:00
( when ( memq command ' ( enter ref ) )
2014-01-03 12:38:17 -05:00
;; Support orgmode-standard of going back (buffer and position)
( org-mark-ring-push )
2015-01-19 15:25:58 -05:00
( pop-to-buffer-same-window org-index--buffer )
2014-01-03 12:38:17 -05:00
( goto-char org-index--point )
2014-12-07 09:18:11 -05:00
( org-index--unfold-buffer )
2014-01-03 12:38:17 -05:00
;; Sort and align
( org-index--sort reorder-once )
2014-04-26 16:48:09 -04:00
( org-index--align )
2014-12-07 09:18:11 -05:00
2014-04-26 16:48:09 -04:00
;; Remember position for leave
( if org-index--point-before
( setq org-index--point-saved org-index--point-before ) ) )
2014-12-07 09:18:11 -05:00
2014-04-26 16:48:09 -04:00
;; prepare to return to initial position in index table
2014-01-03 12:38:17 -05:00
( when initial-ref-or-link
( while ( and ( org-at-table-p )
( not ( or
2014-12-07 09:18:11 -05:00
( string= initial-ref-or-link ( org-index--get-field 'ref ) )
( string= initial-ref-or-link ( org-index--get-field 'link ) ) ) ) )
2014-01-03 12:38:17 -05:00
( forward-line ) )
;; did not find ref, go back to top
( if ( not ( org-at-table-p ) ) ( goto-char org-index--point ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;;
;; Actually do, what is requested
;;
( cond
2014-12-07 09:18:11 -05:00
( ( eq command 'help )
2014-01-03 12:38:17 -05:00
;; bring up help-buffer for this function
( describe-function 'org-index ) )
2014-12-07 09:18:11 -05:00
( ( eq command 'multi-occur )
2015-01-19 15:25:58 -05:00
( org-index--update-line search-ref )
2014-01-03 12:38:17 -05:00
;; Construct list of all org-buffers
( let ( buff org-buffers )
( dolist ( buff ( buffer-list ) )
( set-buffer buff )
( if ( string= major-mode " org-mode " )
( setq org-buffers ( cons buff org-buffers ) ) ) )
;; Do multi-occur
( multi-occur org-buffers guarded-search )
;; Present results
( if ( get-buffer " *Occur* " )
2014-12-07 09:18:11 -05:00
( progn
2014-01-03 12:38:17 -05:00
( setq message-text ( format " multi-occur for '%s' " search ) )
( other-window 1 )
( toggle-truncate-lines 1 ) )
( setq message-text ( format " Did not find '%s' " search ) ) ) ) )
2014-12-07 09:18:11 -05:00
( ( eq command 'add )
( setq kill-new-text ( org-index--do-add link-id ) ) )
( ( eq command 'delete )
( org-index--do-delete ) )
( ( eq command 'head )
2014-01-03 12:38:17 -05:00
( let ( link )
( if ( and org-index--within-node
( org-at-table-p ) )
2014-12-14 11:21:14 -05:00
( setq link ( org-index--get-field 'link ) ) )
2014-12-07 09:18:11 -05:00
2014-12-14 11:21:14 -05:00
( setq message-text ( org-index--do-head search-ref ( or link search-link ) ) ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( ( eq command 'leave )
2014-01-03 12:38:17 -05:00
( setq kill-new-text org-index--text-to-yank )
( setq org-index--text-to-yank nil )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; If "leave" has been called two times in succession, make
;; org-mark-ring-goto believe it has been called two times too
2014-12-07 09:18:11 -05:00
( if ( eq org-index--last-action 'leave )
2014-01-03 12:38:17 -05:00
( let ( ( this-command nil ) ( last-command nil ) )
( org-mark-ring-goto 1 ) )
( org-mark-ring-goto ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; Return to saved position in index buffer
2014-04-26 16:48:09 -04:00
( when org-index--point-saved
2014-01-03 12:38:17 -05:00
;; buffer displayed in window need to set point there first
( if ( eq ( window-buffer org-index--active-window-index )
org-index--buffer )
2014-04-26 16:48:09 -04:00
( set-window-point org-index--active-window-index ( marker-position org-index--point-saved ) ) )
2014-01-03 12:38:17 -05:00
;; set position in buffer in any case and second
( with-current-buffer org-index--buffer
2014-12-07 09:18:11 -05:00
( goto-char org-index--point-saved ) ) )
2014-04-26 16:48:09 -04:00
( setq org-index--point-saved nil ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( ( eq command 'enter )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( goto-char org-index--below-hline )
2015-01-19 15:25:58 -05:00
2014-12-07 09:18:11 -05:00
( if search
2015-01-19 15:25:58 -05:00
;; Go downward in table to requested reference
( setq message-text ( org-index--find-in-index search search-link ) )
2014-12-07 09:18:11 -05:00
;; simply go into table
( setq message-text " At index table " ) )
( recenter ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( ( eq command 'occur )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( org-index--do-occur ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( ( eq command 'ref )
2014-01-03 12:38:17 -05:00
( let ( new )
2014-12-07 09:18:11 -05:00
;; add a new row
( setq new ( org-index--create-new-line ( eq command 'ref ) ) )
2014-01-03 12:38:17 -05:00
;; fill special columns with standard values
2014-12-07 09:18:11 -05:00
( org-table-goto-column ( org-index--column-num 'ref ) )
( insert new )
( setq org-index--last-ref new )
2014-01-03 12:38:17 -05:00
( org-index--align )
2014-12-07 09:18:11 -05:00
;; goto point-field or first empty one or first field
( if ( org-index--special-column 'point-on-add )
( org-table-goto-column ( org-index--column-num ( org-index--special-column 'point-on-add ) ) )
( unless ( catch 'empty
( dotimes ( col org-index--numcols )
( org-table-goto-column ( + col 1 ) )
( if ( string= ( org-trim ( org-table-get-field ) ) " " )
( throw 'empty t ) ) ) )
;; none found, goto first
( org-table-goto-column 1 ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( if org-index--active-region ( setq kill-new-text org-index--active-region ) )
( setq message-text ( format " Adding a new row with ref '%s' " new ) ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( ( eq command 'sort )
2014-01-03 12:38:17 -05:00
;; sort lines according to contained reference
( let ( begin end where )
( catch 'aborted
;; either active region or whole buffer
( if ( and transient-mark-mode
mark-active )
;; sort only region
( progn
( setq begin ( region-beginning ) )
( setq end ( region-end ) )
( setq where " region " ) )
;; sort whole buffer
( setq begin ( point-min ) )
( setq end ( point-max ) )
( setq where " whole buffer " )
;; make sure
2014-12-07 09:18:11 -05:00
( unless ( y-or-n-p " Sort whole buffer? " )
2014-01-03 12:38:17 -05:00
( setq message-text " Sort aborted " )
( throw 'aborted nil ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( save-excursion
( save-restriction
( goto-char ( point-min ) )
( narrow-to-region begin end )
2014-12-07 09:18:11 -05:00
( sort-subr nil 'forward-line 'end-of-line
2014-01-03 12:38:17 -05:00
( lambda ( )
2014-12-07 09:18:11 -05:00
( if ( looking-at ( concat " .* "
2014-01-03 12:38:17 -05:00
( org-index--make-guarded-search org-index--ref-regex 'dont-quote ) ) )
( string-to-number ( match-string 1 ) )
0 ) ) ) )
( highlight-regexp org-index--ref-regex 'isearch )
2014-12-07 09:18:11 -05:00
( setq message-text ( format " Sorted %s from character %d to %d, %d lines "
2014-01-03 12:38:17 -05:00
where begin end
( count-lines begin end ) ) ) ) ) ) )
2014-12-07 09:18:11 -05:00
( ( eq command 'highlight )
2014-01-03 12:38:17 -05:00
( let ( ( where " buffer " ) )
( save-excursion
( save-restriction
( when ( and transient-mark-mode
mark-active )
( narrow-to-region ( region-beginning ) ( region-end ) )
( setq where " region " ) )
2014-12-07 09:18:11 -05:00
( if prefix-arg
2014-01-03 12:38:17 -05:00
( progn
2014-12-07 09:18:11 -05:00
( unhighlight-regexp org-index--ref-regex )
( setq message-text ( format " Removed highlights for references in %s " where ) ) )
( highlight-regexp org-index--ref-regex 'isearch )
( setq message-text ( format " Highlighted references in %s " where ) ) ) ) ) ) )
( ( eq command 'statistics )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq message-text ( org-index--do-statistics ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( ( eq command 'example )
( if ( y-or-n-p " This assistand will help you to create a temporary index with detailed comments. \n Do you want to proceed ? " )
( org-index--create-index t ) ) )
( t ( error " This is a bug: unmatched case '%s' " command ) ) )
2014-01-03 12:38:17 -05:00
;; remember what we have done for next time
2014-12-07 09:18:11 -05:00
( setq org-index--last-action command )
2014-01-03 12:38:17 -05:00
;; tell, what we have done and what can be yanked
2014-12-07 09:18:11 -05:00
( if kill-new-text ( setq kill-new-text
2014-01-03 12:38:17 -05:00
( substring-no-properties kill-new-text ) ) )
( if ( string= kill-new-text " " ) ( setq kill-new-text nil ) )
2014-12-07 09:18:11 -05:00
( let ( ( m ( concat
2014-01-03 12:38:17 -05:00
message-text
2014-12-07 09:18:11 -05:00
( if ( and message-text kill-new-text )
" and r "
2014-01-03 12:38:17 -05:00
( if kill-new-text " R " " " ) )
( if kill-new-text ( format " eady to yank '%s' " kill-new-text ) " " ) ) ) )
2014-12-07 09:18:11 -05:00
( unless ( string= m " " )
( message m )
( setq org-index--message-text m ) ) )
2014-01-03 12:38:17 -05:00
( if kill-new-text ( kill-new kill-new-text ) ) ) )
2014-12-07 09:18:11 -05:00
( defun org-index-default-keybindings ( )
" Set default keybindings for `org-index' .
Establish the common prefix key ` C-c i ' which is followed by the
first letter of a subcommand, so that ` C-c i a ' invokes the
subcommand \"add\". Subcommands available are occur, add, delete,
head, enter, leave and ref. As a special case ` C-c i i ' invokes
` org-index ' to let you choose. "
( define-prefix-command 'org-index-map )
( global-set-key ( kbd " C-c i " ) 'org-index-map )
( define-key org-index-map ( kbd " i " ) ( lambda ( ) ( interactive ) ( org-index ) ) )
( define-key org-index-map ( kbd " o " ) ( lambda ( ) ( interactive ) ( org-index 'occur ) ) )
( define-key org-index-map ( kbd " a " ) ( lambda ( ) ( interactive ) ( org-index 'add ) ) )
( define-key org-index-map ( kbd " d " ) ( lambda ( ) ( interactive ) ( org-index 'delete ) ) )
( define-key org-index-map ( kbd " h " ) ( lambda ( ) ( interactive ) ( org-index 'head ) ) )
( define-key org-index-map ( kbd " e " ) ( lambda ( ) ( interactive ) ( org-index 'enter ) ) )
( define-key org-index-map ( kbd " l " ) ( lambda ( ) ( interactive ) ( org-index 'leave ) ) )
( define-key org-index-map ( kbd " r " ) ( lambda ( ) ( interactive ) ( org-index 'ref ) ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index-new-line ( &rest keys-values )
" Create a new line within the index table, returning its reference.
2014-12-07 09:18:11 -05:00
The function takes a varying number of argument pairs ; each pair
2014-01-03 12:38:17 -05:00
is a symbol for an existing column heading followed by its value.
Example:
2014-12-07 09:18:11 -05:00
( org-index-new-line 'ref t 'link \"7f480c3e\" )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Passing \" 'ref t\" will make the function create a new reference
within the new line.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Optional argument KEYS-VALUES specifies content of new line. "
( org-index--verify-id )
( org-index--parse-table )
( car ( org-index--do-new-line keys-values ) ) )
( defun org-index--do-new-line ( &rest keys-values )
" Do the work for `org-index-new-line' .
Optional argument KEYS-VALUES specifies content of new line. "
2014-01-03 12:38:17 -05:00
( let ( ( org-index--silent t ) )
( save-excursion
( org-index--retrieve-context )
( with-current-buffer org-index--buffer
( goto-char org-index--point )
;; check arguments early
2014-12-07 09:18:11 -05:00
( let ( ( kvs keys-values )
2014-01-03 12:38:17 -05:00
k v )
( while kvs
( setq k ( car kvs ) )
( setq v ( cadr kvs ) )
2014-12-07 09:18:11 -05:00
( if ( eq k 'ref )
2014-01-03 12:38:17 -05:00
( unless ( memq v ' ( t nil ) )
2015-01-19 15:25:58 -05:00
( error " Column 'ref' accepts only \" t \" or \" nil \" " ) )
2014-12-07 09:18:11 -05:00
( if ( or ( not ( symbolp k ) )
( and ( symbolp v ) ( not ( eq v t ) ) ( not ( eq v nil ) ) ) )
2014-01-03 12:38:17 -05:00
( error " Arguments must be alternation of key and value " ) ) )
2014-12-07 09:18:11 -05:00
( unless ( org-index--column-num k )
2014-01-03 12:38:17 -05:00
( error " Unknown column or column not defined in table: '%s' " ( symbol-name k ) ) )
( setq kvs ( cddr kvs ) ) ) )
2014-12-07 09:18:11 -05:00
( if ( and ( not ( plist-get keys-values 'ref ) )
( not ( stringp ( plist-get keys-values 'link ) ) ) )
2014-01-03 12:38:17 -05:00
( error " Need a link when not creating a ref " ) )
2014-12-07 09:18:11 -05:00
( let ( ref yank )
2014-01-03 12:38:17 -05:00
;; create new line
2014-12-07 09:18:11 -05:00
( setq ref ( org-index--create-new-line ( plist-get keys-values 'ref ) ) )
( plist-put keys-values 'ref ( or ref " " ) )
2014-01-03 12:38:17 -05:00
;; fill columns
2014-12-07 09:18:11 -05:00
( let ( ( kvs keys-values )
2014-01-03 12:38:17 -05:00
k v n )
( while kvs
( setq k ( car kvs ) )
( setq v ( cadr kvs ) )
( setq n ( org-index--column-num k ) )
( org-table-goto-column n )
( insert v )
( setq kvs ( cddr kvs ) ) ) )
2014-12-07 09:18:11 -05:00
;; get column to yank
( setq yank ( org-trim ( org-table-get-field ( org-index--column-num ( org-index--special-column 'yank-after-add ) ) ) ) )
2014-01-03 12:38:17 -05:00
( org-index--sort )
2014-12-07 09:18:11 -05:00
( cons ref yank ) ) ) ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index-get-line ( type value )
" Retrieve an existing line within the index table by ref or link.
Return its contents as a property list.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
The function ` plist-get ' may be used to retrieve specific elements
from the result.
2014-01-03 12:38:17 -05:00
Example:
2014-12-14 11:21:14 -05:00
( plist-get ( org-index-get-line 'ref \"R12\" ) 'count )
2014-01-03 12:38:17 -05:00
2014-12-14 11:21:14 -05:00
retrieves the value of the count-column for reference number 12.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Argument TYPE is a symbol, either ref or link,
argument VALUE specifies the value to search for. "
;; check arguments
( unless ( memq type ' ( ref link ) )
( error " Argument can only be 'ref' or 'link' " ) )
( org-index--verify-id )
( org-index--parse-table )
( org-index--get-or-delete-line 'get type value ) )
( defun org-index--get-or-delete-line ( command type value )
" Get or delete (according to COMMAND) a line by TYPE and VALUE. "
2014-01-03 12:38:17 -05:00
( let ( ( org-index--silent t )
found )
( save-excursion
( org-index--retrieve-context )
( with-current-buffer org-index--buffer
( goto-char org-index--point )
( goto-char org-index--below-hline )
2014-12-07 09:18:11 -05:00
( while ( and ( not found )
2014-01-03 12:38:17 -05:00
( org-at-table-p ) )
2014-12-07 09:18:11 -05:00
( when ( string= ( org-index--get-field type )
2014-01-03 12:38:17 -05:00
value )
2014-12-07 09:18:11 -05:00
;; found matching line
( if ( eq command 'get )
;; get its fields
( mapc ( lambda ( x )
( if ( and ( numberp ( cdr x ) )
( > ( cdr x ) 0 ) )
( setq found ( cons ( car x ) ( cons ( or ( org-index--get-field ( car x ) ) " " ) found ) ) )
) ) ( reverse org-index--columns ) )
;; or delete it
( let ( ( start ( point ) ) )
( beginning-of-line )
( forward-line )
( delete-region start ( point ) ) ) ) )
( forward-line ) ) ) )
found ) )
( defun org-index--read-command ( )
" Find out, what we are supposed to do. "
( let ( reorder-once ; Column to use for single time sorting
command
input )
2014-01-03 12:38:17 -05:00
;; Ask user, what to do
2014-12-07 09:18:11 -05:00
( while ( progn
( setq input
( org-completing-read
" Please choose: "
( mapcar 'symbol-name org-index--commands )
nil nil ) )
;; convert to symbol
( setq command ( intern input ) )
;; ask for reorder in loop, because we may have to ask for command right again
( if ( eq command 'reorder )
( setq reorder-once
( intern
( org-icompleting-read
" Please choose column to reorder index table once: "
( list " ref " " count " " created " " last-accessed " )
nil t ) ) ) )
;; maybe ask initial question again
( eq command 'reorder ) ) )
( cons command reorder-once ) ) )
( defun org-index--get-or-read-search ( search command )
" Get SEARCH string, maybe read from user; respect COMMAND that will be executed. "
2014-01-03 12:38:17 -05:00
( let ( search-from-table
search-from-cursor )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( unless search
;; Search string can come from several sources:
;; From link or ref columns of table
2014-04-26 16:48:09 -04:00
( when ( and org-index--within-node
( org-at-table-p ) )
2014-12-07 09:18:11 -05:00
( setq search-from-table ( or ( org-index--get-field 'link )
( org-index--get-field 'ref ) ) ) )
2014-01-03 12:38:17 -05:00
;; From string below cursor
( when ( and ( not org-index--within-node )
org-index--below-cursor
2014-12-07 09:18:11 -05:00
( string-match ( concat " \\ ( " org-index--ref-regex " \\ ) " )
2014-01-03 12:38:17 -05:00
org-index--below-cursor ) )
( setq search-from-cursor ( match-string 1 org-index--below-cursor ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; Depending on requested action, get search from one of the sources above
2014-12-07 09:18:11 -05:00
( cond ( ( eq command 'enter )
( setq search search-from-cursor ) )
( ( memq command ' ( head occur ) )
( setq search ( or search-from-table search-from-cursor ) ) ) ) )
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
;; From occur-buffer into index ?
( unless search
( if ( and ( string= ( buffer-name ) org-index--occur-buffer-name )
( org-at-table-p ) )
( setq search ( org-index--get-field 'ref ) ) ) )
2014-01-03 12:38:17 -05:00
;; If we still do not have a search string, ask user explicitly
( unless search
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( if org-index--silent ( error " Need to specify search, if silence is required " ) )
2015-01-19 15:25:58 -05:00
( if ( eq command 'enter )
;; accept single char commands or switch to reading a sequence of digits
( let ( char prompt )
;; read one character
( while ( not ( memq char ( append ( number-sequence ?0 ?9 ) ( list ?c ?l ?. ?\C -m ) ) ) )
;; start with short prompt but give more help on next iteration
( setq prompt " Please specify, where to go (0-9.l<return> or ?): " )
( setq char ( read-char prompt ) )
( setq prompt " Digits specify a reference number to got to, `.' goes to index line of current node, `l' to last line created and <return> to top of index. Please choose: " ) )
2014-12-07 09:18:11 -05:00
2015-01-19 15:25:58 -05:00
( if ( memq char ( number-sequence ?0 ?9 ) )
;; read rest of digits
( setq search ( read-from-minibuffer " Search reference number: " ( char-to-string char ) ) )
;; decode single chars
( if ( eq char ?. ) ( setq search ( org-id-get ) ) )
( if ( eq char ?\C -m ) ( setq search nil ) )
( if ( eq char ?l ) ( setq search ( number-to-string org-index--maxref ) ) ) ) )
2014-12-07 09:18:11 -05:00
2015-01-19 15:25:58 -05:00
( setq search ( read-from-minibuffer " Search reference number: " ) ) ) )
2014-01-03 12:38:17 -05:00
;; Clean up and examine search string
2014-12-07 09:18:11 -05:00
( when search
2014-01-03 12:38:17 -05:00
( setq search ( org-trim search ) )
( if ( string= search " " ) ( setq search nil ) )
( when search
( if ( string-match " ^[0-9]+$ " search )
( setq search ( concat org-index--head search org-index--tail ) ) ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
search ) )
( defun org-index--verify-id ( )
2014-12-07 09:18:11 -05:00
" Check, that we have a valid id. "
2014-01-03 12:38:17 -05:00
;; Check id
( unless org-index-id
2014-12-07 09:18:11 -05:00
( org-index--create-missing-index " Variable org-index-id is not set, so probably no index table has been created yet. " ) )
2014-01-03 12:38:17 -05:00
;; Find node
2014-12-07 09:18:11 -05:00
( let ( marker )
2014-01-03 12:38:17 -05:00
( setq marker ( org-id-find org-index-id 'marker ) )
2014-12-07 09:18:11 -05:00
( unless marker ( org-index--create-missing-index " Cannot find the node with id \" %s \" (as specified by variable org-index-id). " org-index-id ) )
2014-01-03 12:38:17 -05:00
; Try again with new node
2014-12-07 09:18:11 -05:00
( setq marker ( org-id-find org-index-id 'marker ) )
2014-01-03 12:38:17 -05:00
( unless marker ( error " Could not create node " ) )
2014-12-07 09:18:11 -05:00
( setq org-index--buffer ( marker-buffer marker )
2014-01-03 12:38:17 -05:00
org-index--point ( marker-position marker ) )
( move-marker marker nil ) ) )
( defun org-index--retrieve-context ( )
2014-12-07 09:18:11 -05:00
" Collect context information before starting with command. "
2014-01-03 12:38:17 -05:00
;; Get the content of the active region or the word under cursor
2014-12-07 09:18:11 -05:00
( setq org-index--active-region
2014-01-03 12:38:17 -05:00
( if ( and transient-mark-mode mark-active )
( buffer-substring ( region-beginning ) ( region-end ) )
nil ) )
( setq org-index--below-cursor ( thing-at-point 'symbol ) )
2014-12-07 09:18:11 -05:00
;; get category of current node
( setq org-index--category-before
( save-excursion ; workaround: org-get-category does not give category when at end of buffer
( beginning-of-line )
( org-get-category ) ) )
;; Find out, if we are within index table or not
2014-01-03 12:38:17 -05:00
( setq org-index--within-node ( string= ( org-id-get ) org-index-id ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; Check and remember, if active window contains buffer with index table
( if ( eq ( window-buffer ) org-index--buffer )
( setq org-index--active-window-index ( selected-window ) ) )
;; get current position in index-buffer
( with-current-buffer org-index--buffer
2014-12-07 09:18:11 -05:00
( setq org-index--point-before
2014-04-26 16:48:09 -04:00
( if ( string= ( org-id-get ) org-index-id )
nil
( point-marker ) ) ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index--parse-table ( )
2014-12-07 09:18:11 -05:00
" Parse content of index table. "
2014-01-03 12:38:17 -05:00
( let ( ref-field
link-field
initial-point
2014-12-07 09:18:11 -05:00
end-of-headings
start-of-headings )
2014-01-03 12:38:17 -05:00
( with-current-buffer org-index--buffer
( setq org-index--maxref 0 )
( setq initial-point ( point ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( org-index--go-below-hline )
( beginning-of-line )
2014-12-07 09:18:11 -05:00
;; get headings to display during occur
( setq end-of-headings ( point ) )
2014-01-03 12:38:17 -05:00
( while ( org-at-table-p ) ( forward-line -1 ) )
( forward-line )
2014-12-07 09:18:11 -05:00
( setq start-of-headings ( point ) )
( setq org-index--headings ( buffer-substring start-of-headings end-of-headings ) )
2014-01-03 12:38:17 -05:00
;; count columns
( org-table-goto-column 100 )
( setq org-index--numcols ( - ( org-table-current-column ) 1 ) )
2014-12-07 09:18:11 -05:00
;; go to top of table
( while ( org-at-table-p )
( forward-line -1 ) )
( forward-line )
;; parse line of headings
2014-01-03 12:38:17 -05:00
( org-index--parse-headings )
2014-12-07 09:18:11 -05:00
( forward-line 2 )
;; parse list of flags
( goto-char org-index--point )
( org-index--parse-flags )
( org-index--go-below-hline )
( setq org-index--below-hline ( point-marker ) )
2014-01-03 12:38:17 -05:00
;; Retrieve any decorations around the number within the first nonempty ref-field
( goto-char org-index--below-hline )
( while ( and ( org-at-table-p )
2014-12-07 09:18:11 -05:00
( not ( setq ref-field ( org-index--get-field 'ref ) ) ) )
2014-01-03 12:38:17 -05:00
( forward-line ) )
;; Some Checking
( unless ref-field
2014-12-07 09:18:11 -05:00
( org-index--report-index-error " Reference column is empty " ) )
2014-01-03 12:38:17 -05:00
( unless ( string-match " ^ \\ ([^0-9]* \\ ) \\ ([0-9]+ \\ ) \\ ([^0-9]* \\ )$ " ref-field )
2014-12-07 09:18:11 -05:00
( org-index--report-index-error
" First reference in index table ('%s') does not contain a number " ref-field ) )
2014-01-03 12:38:17 -05:00
;; These are the decorations used within the first ref of index
( setq org-index--head ( match-string 1 ref-field ) )
( setq org-index--tail ( match-string 3 ref-field ) )
( setq org-index--ref-regex ( concat ( regexp-quote org-index--head )
2014-12-07 09:18:11 -05:00
" \\ ([0-9]+ \\ ) "
2014-01-03 12:38:17 -05:00
( regexp-quote org-index--tail ) ) )
( setq org-index--ref-format ( concat org-index--head " %d " org-index--tail ) )
;; Go through table to find maximum number and do some checking
( let ( ( ref 0 ) )
2014-12-07 09:18:11 -05:00
( while ( org-at-table-p )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq ref-field ( org-index--get-field 'ref ) )
( setq link-field ( org-index--get-field 'link ) )
2014-01-03 12:38:17 -05:00
( when ( and ( not ref-field )
( not link-field ) )
2014-12-07 09:18:11 -05:00
( kill-whole-line )
( message " Removing line from index-table with both ref and link empty " ) )
2014-01-03 12:38:17 -05:00
( if ref-field
( if ( string-match org-index--ref-regex ref-field )
;; grab number
( setq ref ( string-to-number ( match-string 1 ref-field ) ) )
2014-12-07 09:18:11 -05:00
( kill-whole-line )
( message " Removing line from index-table whose ref does not contain a number " ) ) )
2014-01-03 12:38:17 -05:00
;; check, if higher ref
( if ( > ref org-index--maxref ) ( setq org-index--maxref ref ) )
( forward-line 1 ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; go back to initial position
( goto-char initial-point ) ) ) )
( defun org-index--sort ( &optional sort-column )
2014-12-07 09:18:11 -05:00
" Sort index table maybe according to SORT-COLUMN. "
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( unless sort-column ( setq sort-column ( org-index--special-column 'sort ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( let ( ( is-modified ( buffer-modified-p ) )
top
2014-01-03 12:38:17 -05:00
bottom
ref-field
2014-12-07 09:18:11 -05:00
count-field )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( unless buffer-read-only
2014-01-03 12:38:17 -05:00
;; get boundaries of table
( goto-char org-index--below-hline )
( forward-line 0 )
( setq top ( point ) )
( while ( org-at-table-p ) ( forward-line ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; Kill all empty rows at bottom
( while ( progn
( forward-line -1 )
( org-table-goto-column 1 )
( and
2014-12-07 09:18:11 -05:00
( not ( org-index--get-field 'ref ) )
( not ( org-index--get-field 'link ) ) ) )
2014-01-03 12:38:17 -05:00
( org-table-kill-row ) )
( forward-line 1 )
( setq bottom ( point ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( save-restriction
( narrow-to-region top bottom )
( goto-char top )
( sort-subr t
2014-12-07 09:18:11 -05:00
'forward-line
'end-of-line
2014-01-03 12:38:17 -05:00
( lambda ( )
( let ( ref
2014-12-07 09:18:11 -05:00
( ref-field ( or ( org-index--get-field 'ref ) " " ) )
( count-field ( or ( org-index--get-field 'count ) " " ) ) )
2014-01-03 12:38:17 -05:00
;; get reference with leading zeroes, so it can be
;; sorted as text
( string-match org-index--ref-regex ref-field )
2014-12-07 09:18:11 -05:00
( setq ref ( format
" %06d "
( string-to-number
2014-01-03 12:38:17 -05:00
( or ( match-string 1 ref-field )
" 0 " ) ) ) )
;; Construct different sort-keys according to
2014-12-07 09:18:11 -05:00
;; requested sort column
( cond
( ( eq sort-column 'count )
( concat ( format
" %08d "
( string-to-number ( or ( org-index--get-field 'count )
" " ) ) )
2014-01-03 12:38:17 -05:00
ref ) )
2014-12-07 09:18:11 -05:00
( ( eq sort-column 'ref )
ref )
( ( eq sort-column 'last-accessed )
( concat ( org-index--get-field sort-column )
" "
2014-01-03 12:38:17 -05:00
ref ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( t ( error " This is a bug: unmatched case '%s' " sort-column ) ) ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
nil 'string< ) )
2014-12-07 09:18:11 -05:00
;; restore modification state
( set-buffer-modified-p is-modified ) ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index--go-below-hline ( )
2014-12-07 09:18:11 -05:00
" Move below hline in index-table. "
( let ( ( count 0 )
( errstring ( format " index table within node %s " org-index-id ) ) )
( goto-char org-index--point )
;; go to heading of node
( while ( not ( org-at-heading-p ) ) ( forward-line -1 ) )
( forward-line 1 )
;; go to first table, but make sure we do not get into another node
( while ( and ( not ( org-at-table-p ) )
( not ( org-at-heading-p ) )
( not ( eobp ) ) )
( forward-line ) )
;; check, if there really is a table
( unless ( org-at-table-p )
( org-index--create-missing-index " Cannot find %s. " errstring ) )
;; go just after hline
( while ( and ( not ( org-at-table-hline-p ) )
( org-at-table-p ) )
( forward-line ) )
( forward-line )
;; and check
( unless ( org-at-table-p )
( org-index--report-index-error " Cannot find a hline within %s " errstring ) )
( org-table-goto-column 1 ) ) )
( defun org-index--align ( )
" Align index table without setting its buffer modified. "
( let ( ( is-modified ( buffer-modified-p ) ) )
( unless buffer-read-only ( org-table-align ) )
( set-buffer-modified-p is-modified )
( org-index--go-below-hline )
( setq org-index--below-hline ( point-marker ) ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--parse-headings ( )
" Parse headings of index table. "
( let ( field ;; field content
field-symbol ;; and as a symbol
found )
( setq org-index--columns nil )
;; For each column
( dotimes ( col org-index--numcols )
( setq field ( substring-no-properties ( downcase ( org-trim ( org-table-get-field ( + col 1 ) ) ) ) ) )
( if ( string= field " " )
( error " Column name cannot be empty " ) )
( if ( and ( not ( string= ( substring field 0 1 ) " . " ) )
( not ( member ( intern field ) org-index--valid-headings ) ) )
( error " Column name '%s' is not a valid heading (custom headings may start with a dot (e.g. '.foo') " field ) )
( setq field-symbol ( intern field ) )
;; check if heading has already appeared
( if ( assoc field-symbol org-index--columns )
( org-index--report-index-error
" '%s' appears two times as column heading " ( downcase field ) )
;; add it to list at front, reverse later
( setq org-index--columns ( cons ( cons field-symbol ( + col 1 ) ) org-index--columns ) ) ) ) )
( setq org-index--columns ( reverse org-index--columns ) )
;; check if all necessary headings have appeared
( mapc ( lambda ( head )
( unless ( cdr ( assoc head org-index--columns ) )
( org-index--report-index-error " No column has heading '%s' " head ) ) )
org-index--required-headings ) )
( defun org-index--parse-flags ( )
" Parse list of flags in index table. "
( let ( parent parent-is-comment child )
;; reset configuration variables
( setq org-index--special-columns nil )
( setq org-index--flagged-columns nil )
( org-index--goto-list " columns-and-flags " t )
( forward-line 1 )
;; outer loop over columns
( while ( and ( setq parent ( org-index--parse-list-item ) )
parent
( > ( cdr ( assoc :indent parent ) ) 0 ) )
( setq parent-is-comment ( member ( cdr ( assoc :text parent ) ) ' ( " all-columns-explained " " all-flags-explained " ) ) )
;; check, that we have a valid heading
( unless ( or parent-is-comment
( assoc ( cdr ( assoc :sym parent ) ) org-index--columns ) )
( org-index--report-index-error " '%s' is not a valid column " ( cdr ( assoc :text parent ) ) ) )
;; inner loop over children
( while ( and ( forward-line 1 )
( setq child ( org-index--parse-list-item ) )
child
( > ( cdr ( assoc :indent child ) )
( cdr ( assoc :indent parent ) ) ) )
( unless parent-is-comment
;; check, that we have a valid flag
( unless ( memq ( cdr ( assoc :sym child ) ) org-index--all-flags )
( org-index--report-index-error " '%s' is not a valid flag " ( cdr ( assoc :text child ) ) ) )
;; process flag with respect to current index-column
( if ( memq ( cdr ( assoc :sym child ) ) org-index--single-flags )
;; Check, that none of org-index--single-flags appears twice
( if ( assoc ( cdr ( assoc :sym child ) ) org-index--special-columns )
( org-index--report-index-error
" More than one column is marked with flag '%s' " ( cdr ( assoc :text child ) ) )
;; add it to list
( setq org-index--special-columns ( cons ( cons ( cdr ( assoc :sym child ) ) ( cdr ( assoc :sym parent ) ) )
org-index--special-columns ) ) ) )
;; all flags are stored in org-index--flagged-columns
( let ( ( l ( assoc ( cdr ( assoc :sym child ) ) org-index--flagged-columns ) ) ) ;; list of flag and columns, that carry this flag
( unless l
;; no list of columns with this flag is present, create one
( setq org-index--flagged-columns
( cons ( cons ( cdr ( assoc :sym child ) ) nil )
org-index--flagged-columns ) )
( setq l ( car org-index--flagged-columns ) ) )
;; prepend this column to list of columns with this flag
( setcdr l ( cons ( cdr ( assoc :sym parent ) ) ( cdr l ) ) ) ) ) ) )
;; check, that all needed flags have been specified
( mapc ( lambda ( x )
( unless ( assoc x org-index--special-columns )
( org-index--report-index-error " Required flag '%s' does not appear " ( substring ( symbol-name x ) 1 ) ) ) )
org-index--required-flags ) ) )
( defun org-index--goto-list ( name &optional required non-top )
" Goto list NAME (maybe NON-TOP Level) in index node, err if REQUIRED list is not present. "
2014-01-03 12:38:17 -05:00
( goto-char org-index--point )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; go to heading of node
( while ( not ( org-at-heading-p ) ) ( forward-line -1 ) )
( forward-line 1 )
2014-12-07 09:18:11 -05:00
;; go to named list
( while ( and ( not ( let ( ( item ( org-index--parse-list-item ) ) )
( if item
( and ( or non-top ( = ( cdr ( assoc :indent item ) ) 0 ) ) ;; accept only toplevel ?
( string= ( cdr ( assoc :text item ) ) name ) ) ;; with requested name
nil ) ) )
2014-01-03 12:38:17 -05:00
( not ( org-at-table-p ) )
2014-12-07 09:18:11 -05:00
( not ( org-at-heading-p ) )
( not ( eobp ) ) )
2014-01-03 12:38:17 -05:00
( forward-line 1 ) )
2014-12-07 09:18:11 -05:00
( if ( org-at-item-p )
t
( if required
( org-index--report-index-error " Could not find required list '%s' " name )
nil ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--parse-list-item ( )
" Parse a list item into an assoc array (indent, checkbox, text, value). "
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( if ( looking-at org-list-full-item-re )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
;; retrieve interesting parts of list item from match data
( let ( alist indent checkbox text value )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq indent ( save-excursion
( goto-char ( match-beginning 1 ) )
( current-column ) ) )
( decf indent ( + ( save-match-data ( org-current-level ) ) 1 ) )
( add-to-list 'alist ( cons :indent indent ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq checkbox ( match-string 3 ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq text ( match-string 4 ) )
( setq value ( buffer-substring
( match-end 0 )
( save-excursion ( end-of-line ) ( point ) ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( when ( not text )
( setq text value )
( setq value nil ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( add-to-list 'alist ( cons :text text ) )
( add-to-list 'alist ( cons :value value ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( add-to-list 'alist ( cons :sym ( intern text ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
;; clean up strings
( mapc ( lambda ( x ) ( if ( stringp ( cdr x ) ) ( setf ( cdr x ) ( org-trim ( substring-no-properties ( cdr x ) ) ) ) ) ) alist )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
alist )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
nil ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--create-missing-index ( &rest reasons )
" Create a new empty index table with detailed explanation. Argument REASONS explains why. "
( org-index--ask-before-create-index " Cannot find your index table: "
" new permanent " " . "
reasons )
( org-index--create-index ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--report-index-error ( &rest reasons )
" Report an error (explained by REASONS) with the existing index and offer to create a valid one to compare with. "
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( org-index--ask-before-create-index " The existing index contains this error: "
" temporary " " , to compare with. "
reasons )
( org-index--create-index t t ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--ask-before-create-index ( explanation type for-what reasons )
; checkdoc-params: (explanation type for-what reasons)
" Ask the user before creating an index or throw error. Arguments specify bits of issued message. "
( let ( reason prompt )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq reason ( apply 'format reasons ) )
( if org-index--silent ( error ( concat explanation reason ) ) ) ; cannot proceed without querying the user
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( setq prompt ( concat explanation reason " \n \n "
" However, this assistant can help you to create a "
type " index with detailed comments " for-what " \n \n "
" Do you want to proceed ? " ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( unless ( let ( ( max-mini-window-height 1.0 ) )
( y-or-n-p prompt ) )
( error explanation reason ) ) ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--create-index ( &optional temporary compare )
" Create a new empty index table with detailed explanation.
specify flag TEMPORARY for th new table temporary, maybe COMPARE it with existing index. "
( let ( buffer
title
firstref
id )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( if temporary
( let ( ( file-name ( concat temporary-file-directory " org-index--example-index.org " ) )
( buffer-name " *org-index-example-index* " ) )
( setq buffer ( get-buffer-create buffer-name ) )
( with-current-buffer buffer
;; but it needs a file for its index to be found
( unless ( string= ( buffer-file-name ) file-name )
( set-visited-file-name file-name ) )
( rename-buffer buffer-name ) ; name is change by line above
( erase-buffer )
( org-mode ) ) )
( 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 \n Buffer: " ( mapcar 'buffer-name ( org-buffer-list ) ) nil nil ) ) ) )
( setq title ( read-from-minibuffer " Please enter the title of the index node: " ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( while ( progn
( setq firstref ( read-from-minibuffer " Please enter your first reference-number. This is a number preceeded by some non-digit chars and optionally followed by some more non-digit chars, e.g. 'R1', '-1-' or '#1#' (and your initial number does not need to be '1'). The format of your reference-numbers only needs to make sense for yourself, so that you can spot it easily in your texts or write it on a piece of paper; it should however not already appear to frequently within your existing notes, to avoid too many false hits when searching. \n \n Please choose: " ) )
( let ( desc )
( when ( string-match " [[:blank:]] " firstref )
( setq desc " Contains whitespace " ) )
( when ( string-match " [[:cntrl:]] " firstref )
( setq desc " Contains control characters " ) )
( unless ( string-match " ^[^0-9]+[0-9]+[^0-9]*$ " firstref )
;; firstref not okay, report details
( setq desc
( cond ( ( string= firstref " " ) " is empty " )
( ( not ( string-match " ^[^0-9]+ " firstref ) ) " starts with a digit " )
( ( not ( string-match " ^[^0-9]+[0-9]+ " firstref ) ) " does not contain a number " )
( ( not ( string-match " ^[^0-9]+[0-9]+[^0-9]*$ " firstref ) ) " contains more than one sequence of digits " )
) ) )
( if desc
( progn
( read-from-minibuffer ( format " Your input '%s' does not meet the requirements because it %s. \n Please hit RET and try again: " firstref desc ) )
t )
nil ) ) ) )
( with-current-buffer buffer
( goto-char ( point-max ) )
( insert ( format " * %s %s \n " firstref title ) )
( if temporary
( insert "
Below you find your temporary index table, which will not last longer
than your current emacs session.
" )
( insert "
Below you find your initial index table, which will grow over time.
" ))
( insert "
You may start using it by adding some lines. Just move to
another heading, invoke ` org-index ' and choose the command
\"add\". After adding a few nodes, try the command \"occur\"
to search among them.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
To gain further insight you may invoke the subcommand \"help\", or
read the description of ` org-index '.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Within the index table below, dhe sequence of columns does not
matter. You may reorder them any way you like. Columns are
found by their heading. You may also add your own columns.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Following these explanations there is the item-list
` columns-and-flags ', which influences the behaviour of
` org-index '. See the explanations which are part of this list.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
This node needs not be a top level node ; its name is completely
at your choice ; it is found through its ID only.
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
Remark: These lines of explanation can be removed at any time.
2014-01-03 12:38:17 -05:00
" )
2014-12-07 09:18:11 -05:00
( setq id ( org-id-get-create ) )
( insert ( format "
%s
| ref | . category | keywords | count | last-accessed | created | link |
| | | | | | | <4> |
|-----+-----------+----------+-------+---------------+---------+------|
| %s | | %s | | | %s | %s |
"
org-index--sample-flags
firstref
" This node "
( with-temp-buffer ( org-insert-time-stamp nil nil t ) )
id ) )
;; make sure, that node can be found
( org-id-add-location id ( buffer-file-name ) )
( setq buffer-save-without-query t )
( basic-save-buffer )
2014-01-03 12:38:17 -05:00
( while ( not ( org-at-table-p ) ) ( forward-line -1 ) )
( unless buffer-read-only ( org-table-align ) )
( while ( not ( org-at-heading-p ) ) ( forward-line -1 ) )
2014-12-07 09:18:11 -05:00
2014-12-10 16:20:55 -05:00
;; read back some info about new index
( let ( ( org-index-id id ) )
( org-index--verify-id ) )
2014-01-03 12:38:17 -05:00
;; present results to user
2014-12-07 09:18:11 -05:00
( if temporary
( progn
;; Present existing and temporary index together
( when compare
2015-01-19 15:25:58 -05:00
( pop-to-buffer-same-window org-index--buffer )
2014-12-07 09:18:11 -05:00
( goto-char org-index--point )
( org-index--unfold-buffer )
( delete-other-windows )
( select-window ( split-window-vertically ) ) )
;; show new index
2015-01-19 15:25:58 -05:00
( pop-to-buffer-same-window buffer )
2014-01-03 12:38:17 -05:00
( org-id-goto id )
2014-12-07 09:18:11 -05:00
( org-index--unfold-buffer )
( if compare
( error " Please compare your existing index (upper window) and a temporary new one (lower window) to fix your index " )
( message " This is your new temporary index. " ) ) )
( progn
;; Only show the new index
2015-01-19 15:25:58 -05:00
( pop-to-buffer-same-window buffer )
2014-12-07 09:18:11 -05:00
( delete-other-windows )
( org-id-goto id )
( org-index--unfold-buffer )
( setq org-index-id id )
( 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 ? " )
( progn
( customize-save-variable 'org-index-id id )
2014-12-10 16:20:55 -05:00
( error " Saved org-index-id '%s' to %s " id ( or custom-file
user-init-file ) ) )
2014-12-07 09:18:11 -05:00
( let ( sq )
( setq sq ( format " (setq org-index-id \" %s \" ) " id ) )
( kill-new sq )
( error " Did not make the id of this new index permanent; you may want to put \n \n %s \n \n into your own initialization; it is copied already, just yank it " sq ) ) ) ) ) ) ) )
( defun org-index--unfold-buffer ( )
" Helper function to unfold buffer. "
( org-show-context )
( org-show-subtree )
( recenter 1 )
( save-excursion
( org-back-to-heading )
( forward-line ) ;; on property drawer
( org-cycle )
( org-index--goto-list " columns-and-flags " )
( org-cycle ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index--update-line ( ref-or-link )
2014-12-07 09:18:11 -05:00
" Update columns count and last-accessed in line REF-OR-LINK. "
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( let ( ( newcount 0 )
2014-04-26 16:48:09 -04:00
initial )
2014-01-03 12:38:17 -05:00
( with-current-buffer org-index--buffer
( unless buffer-read-only
;; search reference or link, if given (or assume, that we are already positioned right)
( when ref-or-link
( setq initial ( point ) )
( goto-char org-index--below-hline )
( while ( and ( org-at-table-p )
2014-12-07 09:18:11 -05:00
( not ( or ( string= ref-or-link ( org-index--get-field 'ref ) )
( string= ref-or-link ( org-index--get-field 'link ) ) ) ) )
2014-01-03 12:38:17 -05:00
( forward-line ) ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( if ( not ( org-at-table-p ) )
( error " Did not find reference or link '%s' " ref-or-link )
2014-04-26 16:48:09 -04:00
( org-index--update-current-line ) )
2014-12-07 09:18:11 -05:00
2014-04-26 16:48:09 -04:00
( if initial ( goto-char initial ) ) ) ) ) )
( defun org-index--update-current-line ( )
2014-12-07 09:18:11 -05:00
" Update current lines columns count and last-accessed. "
( let ( newcount ( count-field ( org-index--get-field 'count ) ) )
;; update count field only if number or empty
2014-04-26 16:48:09 -04:00
( when ( or ( not count-field )
( string-match " ^[0-9]+$ " count-field ) )
( setq newcount ( + 1 ( string-to-number ( or count-field " 0 " ) ) ) )
2014-12-07 09:18:11 -05:00
( org-index--get-field 'count
2014-04-26 16:48:09 -04:00
( number-to-string newcount ) ) )
2014-12-07 09:18:11 -05:00
2014-04-26 16:48:09 -04:00
;; update timestamp
2014-12-07 09:18:11 -05:00
( org-table-goto-column ( org-index--column-num 'last-accessed ) )
2014-04-26 16:48:09 -04:00
( org-table-blank-field )
( org-insert-time-stamp nil t t ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index--get-field ( key &optional value )
2014-12-07 09:18:11 -05:00
" Retrieve field KEY from index table or set it to VALUE. "
2014-01-03 12:38:17 -05:00
( let ( field )
2014-04-26 16:48:09 -04:00
( save-excursion
( setq field ( org-trim ( org-table-get-field ( cdr ( assoc key org-index--columns ) ) value ) ) )
( if ( string= field " " ) ( setq field nil ) )
2014-12-07 09:18:11 -05:00
2014-04-26 16:48:09 -04:00
( org-no-properties field ) ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index--column-num ( key )
2014-12-07 09:18:11 -05:00
" Return number of column KEY. "
( if ( numberp key )
2014-01-03 12:38:17 -05:00
key
( cdr ( assoc key org-index--columns ) ) ) )
( defun org-index--special-column ( key )
2014-12-07 09:18:11 -05:00
" Return column (not a number) for special column KEY. "
2014-01-03 12:38:17 -05:00
( cdr ( assoc key org-index--special-columns ) ) )
2014-12-07 09:18:11 -05:00
( defun org-index--flag-p ( flag column )
" Check if COLUMN has FLAG set. "
( unless ( memq flag org-index--all-flags )
( error ( format " Internal error: unknown flag %s " ( symbol-name flag ) ) ) )
( memq column ( assoc flag org-index--flagged-columns ) ) )
2014-01-03 12:38:17 -05:00
( defun org-index--make-guarded-search ( ref &optional dont-quote )
2014-12-07 09:18:11 -05:00
" Make robust search string from REF; DONT-QUOTE it, if requested. "
2014-01-03 12:38:17 -05:00
( concat " \\ _< " ( if dont-quote ref ( regexp-quote ref ) ) " \\ _> " ) )
2014-12-07 09:18:11 -05:00
( defun org-index--do-statistics ( )
" Compute statistics about index table. "
2014-01-03 12:38:17 -05:00
( let ( ( total 0 )
ref-field
ref
min
max
2014-12-07 09:18:11 -05:00
message )
2014-01-03 12:38:17 -05:00
;; go through table and remove all refs, that we see
( goto-char org-index--below-hline )
( while ( org-at-table-p )
;; get ref-field and number
2014-12-07 09:18:11 -05:00
( setq ref-field ( org-index--get-field 'ref ) )
( if ( and ref-field
2014-01-03 12:38:17 -05:00
( string-match org-index--ref-regex ref-field ) )
( setq ref ( string-to-number ( match-string 1 ref-field ) ) ) )
2014-12-07 09:18:11 -05:00
;; record min and max
2014-01-03 12:38:17 -05:00
( if ( or ( not min ) ( < ref min ) ) ( setq min ref ) )
( if ( or ( not max ) ( > ref max ) ) ( setq max ref ) )
;; count
( setq total ( 1+ total ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
( forward-line ) )
2014-12-07 09:18:11 -05:00
( setq message ( format " Found %d references from %s to %s. "
total
( format org-index--ref-format min )
( format org-index--ref-format max ) ) )
message ) )
( defun org-index--do-add ( link-id )
" For current node (with id LINK-ID): add a new line to index table. "
( let ( ( args ( list 'ref t 'link link-id ) )
ref-and-yank content )
( unless ( org-at-heading-p )
( error " Not at headline " ) )
;; some fields want to be edited
( dolist ( col-num org-index--columns )
( setq content " " )
;; copy heading ?
( if ( org-index--flag-p 'get-heading-on-add ( car col-num ) )
( setq content ( nth 4 ( org-heading-components ) ) ) )
;; copy category ?
( if ( org-index--flag-p 'get-category-on-add ( car col-num ) )
( setq content org-index--category-before ) )
( if ( org-index--flag-p 'edit-on-add ( car col-num ) )
2014-12-10 16:20:55 -05:00
( setq content ( read-from-minibuffer
( format " Edit text for column '%s': " ( symbol-name ( car col-num ) ) )
content ) ) )
2014-12-07 09:18:11 -05:00
( if ( not ( string= content " " ) )
( setq args ( append ( list ( car col-num ) content ) args ) ) ) )
;; new line in index table
( setq ref-and-yank ( apply 'org-index--do-new-line args ) )
;; insert reference
( org-entry-put ( point ) " org-index-ref " ( car ref-and-yank ) )
( cdr ref-and-yank ) ) )
( defun org-index--do-delete ( )
" Perform command delete. "
( unless ( org-at-heading-p )
( error " Not at headline " ) )
( let ( ( ref ( org-entry-get ( point ) " org-index-ref " ) ) )
;; delete from index table
( org-index--get-or-delete-line 'delete 'ref ref )
;; delete from property
( org-entry-delete ( point ) " org-index-ref " )
;; maybe delete from heading
( save-excursion
( end-of-line )
( let ( ( end ( point ) ) )
( beginning-of-line )
( when ( search-forward ref end t )
( delete-char ( - ( length ref ) ) )
( just-one-space ) ) ) ) ) )
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
( defun org-index--find-in-index ( search &optional search-link )
" Find index line with ref or link SEARCH (decided by SEARCH-LINK). "
( let ( ( initial ( point ) )
found text )
( while ( and ( not found )
( forward-line )
( org-at-table-p ) )
( save-excursion
( setq found
( string= search
( org-index--get-field
( if search-link 'link 'ref ) ) ) ) ) )
( if found
( progn
( setq text ( format " Found index line '%s' " search ) )
( org-index--update-line nil )
( org-table-goto-column ( org-index--column-num 'ref ) )
( if ( looking-back " " ) ( backward-char ) )
;; remember string to copy
( setq org-index--text-to-yank
( org-trim ( org-table-get-field ( org-index--column-num 'copy ) ) ) ) )
( setq text ( format " Did not find index line '%s' " search ) )
( goto-char initial )
( forward-line ) )
text ) )
2014-01-28 16:42:54 -05:00
( defun org-index--do-head ( ref link &optional other )
2014-12-07 09:18:11 -05:00
" Perform command head: Find node with REF or LINK and present it; if OTHER in separate window. "
2014-01-03 12:38:17 -05:00
( if ref ( setq org-index--last-ref ref ) )
2014-12-14 11:21:14 -05:00
( let ( message marker )
2014-12-07 09:18:11 -05:00
2014-12-14 11:21:14 -05:00
;; Prefer link if available
2014-01-03 12:38:17 -05:00
( if link
2014-12-14 11:21:14 -05:00
( setq marker ( org-id-find link t ) )
( setq marker
( catch 'found
( message ( format " Scanning headlines for '%s' ... " ref ) )
( org-map-entries
( lambda ( )
( when ( string= ref ( org-entry-get ( point ) " org-index-ref " ) )
( throw 'found ( point-marker ) ) ) )
nil 'agenda )
nil ) ) )
( if marker
2014-12-07 09:18:11 -05:00
( progn
2014-12-14 11:21:14 -05:00
( org-index--update-line ( or link ref ) )
( if link
( setq message " Followed link " )
2015-01-19 15:25:58 -05:00
( setq message ( format " Found headline '%s' " ref ) ) )
( let ( cb )
( if other
( progn
( setq cb ( current-buffer ) )
( pop-to-buffer ( marker-buffer marker ) ) )
( pop-to-buffer-same-window ( marker-buffer marker ) ) )
2014-12-14 11:21:14 -05:00
( goto-char marker )
( org-reveal t )
2015-01-19 15:25:58 -05:00
( org-show-entry )
2014-12-14 11:21:14 -05:00
( recenter ) ) )
( if link
( setq message ( format " Did not find link '%s' " link ) )
2015-01-19 15:25:58 -05:00
( setq message ( format " Did not find headline '%s'. " ref ) ) ) )
2014-12-14 11:21:14 -05:00
2014-12-07 09:18:11 -05:00
message ) )
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--do-occur ( )
" Perform command occur. "
2015-01-19 15:25:58 -05:00
( let ( ( word " " ) ; last word to search for growing and shrinking on keystrokes
2014-01-03 12:38:17 -05:00
( prompt " Search for: " )
2015-01-19 15:25:58 -05:00
( lines-wanted ( window-body-height ) )
( lines-found 0 ) ; number of lines found
words ; list words that should match
2014-12-07 09:18:11 -05:00
occur-buffer
2015-01-19 15:25:58 -05:00
stack ; stack of lists of structs with overlays for hiding; used within called functions
begin ; position of first line
narrow ; start of narrowed buffer
help-text ; cons with help text short and long
key-help ; for keys with special function
search-text ; description of text to search for
done ; true, if loop is done
in-c-backspace ; true, while processing C-backspace
show-headings ; true, if headings should be shown
help-overlay ; Overlay with help text
tail-overlay ; To cover unsearched tail
last-point ; Last position before end of search
key ; input from user
key-sequence ) ; as a sequence
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
;; make and show buffer
( if ( get-buffer org-index--occur-buffer-name )
( kill-buffer org-index--occur-buffer-name ) )
( setq occur-buffer ( make-indirect-buffer org-index--buffer org-index--occur-buffer-name ) )
( pop-to-buffer-same-window occur-buffer )
;; avoid modifying direct buffer
( setq buffer-read-only t )
2014-01-03 12:38:17 -05:00
( toggle-truncate-lines 1 )
2015-01-19 15:25:58 -05:00
( setq font-lock-keywords-case-fold-search t )
( setq case-fold-search t )
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
;; narrow to table rows and one line before
( goto-char ( marker-position org-index--below-hline ) )
( forward-line 0 )
( setq begin ( point ) )
( forward-line -1 )
( setq narrow ( point ) )
( while ( org-at-table-p )
2014-01-03 12:38:17 -05:00
( forward-line ) )
2015-01-19 15:25:58 -05:00
( narrow-to-region narrow ( point ) )
( goto-char ( point-min ) )
( forward-line )
2014-12-07 09:18:11 -05:00
2015-01-19 15:25:58 -05:00
;; initialize help text
( setq help-text ( cons
" Incremental occur; `?' toggles help and headlines. \n "
( concat
( org-index--wrap
( concat
" Normal keys add to search word; <space> or <comma> start additional word; <backspace> erases last char, <C-backspace> last word; <return> jumps to heading, <tab> jumps to heading in other window; all other keys end search. \n " ) )
org-index--headings ) ) )
;; insert overlay for help text and to cover unsearched lines
( setq help-overlay ( make-overlay ( point-min ) begin ) )
( overlay-put help-overlay 'display ( car help-text ) )
( overlay-put help-overlay 'face 'org-agenda-dimmed-todo-face )
( setq tail-overlay ( make-overlay ( point-max ) ( point-max ) ) )
( overlay-put tail-overlay 'invisible t )
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
( while ( not done )
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
( if in-c-backspace
( setq key " <backspace> " )
( setq search-text ( mapconcat 'identity ( reverse ( cons word words ) ) " , " ) )
;; read key
( setq key-sequence
( vector ( read-key
( format " %s%s%s "
prompt
search-text
( if ( string= search-text " " ) " " " " ) ) ) ) )
( setq key ( key-description key-sequence ) ) )
2014-01-28 16:42:54 -05:00
2015-01-19 15:25:58 -05:00
( cond
2014-01-03 12:38:17 -05:00
2015-01-19 15:25:58 -05:00
( ( string= key " <C-backspace> " )
( setq in-c-backspace t ) )
2014-04-26 16:48:09 -04:00
2015-01-19 15:25:58 -05:00
( ( member key ( list " <backspace> " " DEL " ) ) ; erase last char
( if ( = ( length word ) 0 )
;; nothing more to delete from current word; try next
( progn
( setq word ( car words ) )
( setq words ( cdr words ) )
( setq in-c-backspace nil ) )
;; unhighlight longer match
( unhighlight-regexp ( regexp-quote word ) )
;; some chars are left; shorten word
( setq word ( substring word 0 -1 ) )
( when ( = ( length word ) 0 ) ; when nothing left, use next word from list
( setq word ( car words ) )
( setq words ( cdr words ) )
( setq in-c-backspace nil ) )
;; free top list of overlays and remove list
( setq lines-found ( or ( org-index--unhide stack ) lines-wanted ) )
( move-overlay tail-overlay
( if stack ( cdr ( assoc :end-of-visible ( car stack ) ) )
( point-max ) )
( point-max ) )
;; highlight shorter word
( unless ( = ( length word ) 0 )
( highlight-regexp ( regexp-quote word ) 'isearch ) )
;; make sure, point is still visible
( goto-char begin ) ) )
( ( member key ( list " SPC " " , " ) ) ; space or comma: enter an additional search word
;; push current word and clear, no need to change display
( setq words ( cons word words ) )
( setq word " " ) )
( ( string= key " ? " ) ; question mark: toggle display of headlines and help
( setq help-text ( cons ( cdr help-text ) ( car help-text ) ) )
( overlay-put help-overlay 'display ( car help-text ) ) )
( ( and ( = ( length key ) 1 )
( aref printable-chars ( elt key 0 ) ) ) ; any printable char: add to current search word
;; unhighlight short word
( unless ( = ( length word ) 0 )
( unhighlight-regexp ( regexp-quote word ) ) )
;; add to word
( setq word ( concat word key ) )
;; make overlays to hide lines, that do not match longer word any more
( goto-char begin )
( setq lines-found ( org-index--hide-with-overlays ( cons word words ) lines-wanted stack tail-overlay ) )
( move-overlay tail-overlay
( if stack ( cdr ( assoc :end-of-visible ( car stack ) ) )
( point-max ) )
( point-max ) )
( goto-char begin )
;; highlight longer word
( highlight-regexp ( regexp-quote word ) 'isearch )
;; make sure, point is on a visible line
( line-move -1 t )
( line-move 1 t ) )
;; anything else terminates loop
( t ( setq done t ) ) ) )
;; put back input event, that caused the loop to end
( unless ( string= key " C-g " )
( setq unread-command-events ( listify-key-sequence key-sequence ) )
( message key ) )
;; postprocessing
( setq last-point ( point ) )
;; For performance reasons do not show matching lines for rest of table. So not code here.
;; make permanent copy
;; copy visible lines
( let ( ( lines-collected 0 )
keymap line all-lines )
( setq cursor-type t )
( goto-char begin )
;; collect all visible lines
( while ( and ( not ( eobp ) )
( < lines-collected lines-wanted ) )
;; skip over invisible lines
( while ( and ( invisible-p ( point ) )
( not ( eobp ) ) )
( goto-char ( 1+ ( overlay-end ( car ( overlays-at ( point ) ) ) ) ) ) )
( setq line ( buffer-substring-no-properties ( line-beginning-position ) ( line-end-position ) ) )
( unless ( string= line " " )
( incf lines-collected )
( setq all-lines ( cons ( concat line
" \n " )
all-lines ) ) )
( forward-line 1 ) )
( kill-buffer org-index--occur-buffer-name ) ; cannot keep this buffer; might become stale soon
;; create new buffer
( setq occur-buffer ( get-buffer-create org-index--occur-buffer-name ) )
( pop-to-buffer-same-window occur-buffer )
( insert " \n " )
;; prepare help text
( setq org-index--occur-help-overlay ( make-overlay ( point-min ) ( point-max ) ) )
( setq org-index--occur-help-text
( cons
( org-index--wrap
( concat " Search is done; `?' toggles help and headlines. \n " ) )
( concat
( org-index--wrap ( format ( concat " Search is done. "
( if ( < lines-collected lines-wanted )
" Showing all %d matches for "
" Showing one window of matches for " )
" \" " search-text
" \" . <return> jumps to heading, <tab> jumps to heading in other window, subcommand \" enter \" to matching line in index. \n " )
( length all-lines ) ) )
org-index--headings ) ) )
( overlay-put org-index--occur-help-overlay 'display ( car org-index--occur-help-text ) )
( overlay-put org-index--occur-help-overlay 'face 'org-agenda-dimmed-todo-face )
;; insert into new buffer
( save-excursion
( apply 'insert ( reverse all-lines ) )
( if ( = lines-collected lines-wanted )
( insert " \n (more lines omitted) \n " ) ) )
( org-mode )
( setq truncate-lines t )
( font-lock-fontify-buffer )
;; highlight words
( setq case-fold-search t )
( setq font-lock-keywords-case-fold-search t )
( mapc ( lambda ( w ) ( unless ( or ( not w ) ( string= w " " ) ) ( highlight-regexp ( regexp-quote w ) 'isearch ) ) )
( cons word words ) )
( setq buffer-read-only t )
;; install keyboard-shortcuts
( setq keymap ( make-sparse-keymap ) )
( set-keymap-parent keymap org-mode-map )
( mapc ( lambda ( x ) ( define-key keymap ( kbd x )
( lambda ( ) ( interactive )
( message ( org-index--occur-to-head ) ) ) ) )
( list " <return> " " RET " ) )
( define-key keymap ( kbd " <tab> " )
( lambda ( ) ( interactive )
( message ( org-index--occur-to-head t ) ) ) )
( define-key keymap ( kbd " ? " )
( lambda ( ) ( interactive )
( setq-local org-index--occur-help-text ( cons ( cdr org-index--occur-help-text ) ( car org-index--occur-help-text ) ) )
( overlay-put org-index--occur-help-overlay 'display ( car org-index--occur-help-text ) ) ) )
( use-local-map keymap ) ) ) )
( defun org-index--wrap ( text )
" Wrap TEXT at fill column. "
( with-temp-buffer
( insert text )
( fill-region ( point-min ) ( point-max ) nil t )
( buffer-string ) ) )
( defun org-index--occur-to-head ( &optional other )
" Helper for `org-index--occur' , find heading with ref or link; if OTHER, in other window. "
( org-index--do-head ( org-index--get-field 'ref )
( org-index--get-field 'link )
other ) )
( defun org-index--hide-with-overlays ( words lines-wanted stack tail-overlay )
" Hide text that is currently visible and does not match WORDS by creating overlays and add them to STACK; TAIL-OVERLAY gives end of visible region.Leave LINES-WANTED lines visible. "
( let ( ( symbol ( intern ( format " org-index-%d " ( length stack ) ) ) )
( lines-found 0 )
( end-of-visible ( point ) )
overlay overlays start matched )
;; main loop
( while ( and ( not ( eobp ) )
( < lines-found lines-wanted ) )
;; skip invisible lines
( while ( and ( not ( eobp ) )
( and
( invisible-p ( point ) )
( < ( point ) ( overlay-start tail-overlay ) ) ) )
( goto-char ( overlay-end ( car ( overlays-at ( point ) ) ) ) ) )
;; find stretch of lines, that are currently visible but should be invisible now
( setq matched nil )
( setq start ( point ) )
( while ( and ( not ( eobp ) )
( not
( and
( invisible-p ( point ) )
( < ( point ) ( overlay-start tail-overlay ) ) ) )
( not ( and ( org-index--test-words words )
( setq matched t ) ) ) ) ; for its side effect
( forward-line 1 ) )
;; create overlay to hide this stretch
( when ( < start ( point ) ) ; avoid creating an empty overlay
( setq overlay ( make-overlay start ( point ) ) )
( overlay-put overlay 'invisible symbol )
( setq overlays ( cons overlay overlays ) ) )
;; skip and count line, that matched
( when matched
( forward-line 1 )
( setq end-of-visible ( point ) )
( incf lines-found ) ) )
;; put new list on top of stack
( setq stack
( cons ( list ( cons :symbol symbol )
( cons :overlays overlays )
( cons :end-of-visible end-of-visible )
( cons :lines lines-found ) )
stack ) )
;; make lines invisible
( add-to-invisibility-spec symbol )
lines-found ) )
( defun org-index--unhide ( stack )
" Unhide text that does has been hidden by `org-index--hide-with-overlays' remove them from STACK. "
( when stack
;; make text visible again
( remove-from-invisibility-spec ( cdr ( assoc :symbol ( car stack ) ) ) )
;; delete overlays
( mapc ( lambda ( y )
( delete-overlay y ) )
( cdr ( assoc :overlays ( car stack ) ) ) )
;; remove from stack
( setq stack ( cdr stack ) )
;; return number of lines, that are now visible
( if stack ( cdr ( assoc :lines ( car stack ) ) ) ) ) )
( defun org-index--test-words ( words )
" Test current line for match against WORDS. "
( let ( line )
( setq line ( downcase ( buffer-substring ( line-beginning-position ) ( line-beginning-position 2 ) ) ) )
( catch 'not-found
( dolist ( w words )
( or ( search w line )
( throw 'not-found nil ) ) )
t ) ) )
2014-01-28 16:42:54 -05:00
2014-01-03 12:38:17 -05:00
2014-12-07 09:18:11 -05:00
( defun org-index--create-new-line ( create-ref )
" Do the common work for `org-index-new-line' and `org-index' . CREATE-REF asks for new reference. "
2014-01-03 12:38:17 -05:00
( let ( new )
( when create-ref
2014-12-07 09:18:11 -05:00
;; construct new reference
( unless new
2014-01-03 12:38:17 -05:00
( setq new ( format " %s%d%s " org-index--head ( 1+ org-index--maxref ) org-index--tail ) ) )
;; remember for org-mark-ring-goto
( setq org-index--text-to-yank new ) )
2014-12-07 09:18:11 -05:00
2014-01-03 12:38:17 -05:00
;; insert ref or link as very first row
( goto-char org-index--below-hline )
( org-table-insert-row )
;; insert some of the standard values
2014-12-07 09:18:11 -05:00
( org-table-goto-column ( org-index--column-num 'created ) )
2014-01-03 12:38:17 -05:00
( org-insert-time-stamp nil nil t )
2014-12-07 09:18:11 -05:00
( org-table-goto-column ( org-index--column-num 'count ) )
2014-01-03 12:38:17 -05:00
( insert " 1 " )
new ) )
( defadvice org-mark-ring-goto ( after org-index--advice-text-to-yank activate )
2014-12-07 09:18:11 -05:00
" Make text from `org-index' available for yank. "
2014-01-03 12:38:17 -05:00
( when org-index--text-to-yank
( kill-new org-index--text-to-yank )
( message ( format " Ready to yank '%s' " org-index--text-to-yank ) )
( setq org-index--text-to-yank nil ) ) )
( provide 'org-index )
;; Local Variables:
;; fill-column: 75
;; comment-column: 50
2015-01-19 15:25:58 -05:00
;; lexical-binding: t
2014-01-03 12:38:17 -05:00
;; End:
;;; org-index.el ends here