]> git.eshelyaron.com Git - emacs.git/commitdiff
Add dwim function for inserting @ref variants
authorRobert Pluim <rpluim@gmail.com>
Tue, 5 Feb 2019 19:38:39 +0000 (20:38 +0100)
committerRobert Pluim <rpluim@gmail.com>
Wed, 6 Feb 2019 17:59:01 +0000 (18:59 +0100)
* lisp/textmodes/texinfo.el (texinfo-insert-dwim-@ref): New function.
Insert @ref variant based on surrounding context.
(texinfo-mode-map): Add binding for texinfo-insert-dwim-@ref.

* etc/NEWS: Describe new texinfo dwim reference functionality.

etc/NEWS
lisp/textmodes/texinfo.el

index 72a6b385a8229bed9644ad85f44f5fc5ccd1e81d..406ed6e3788c9e0fd6a8859626035a3a5a15669f 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -466,6 +466,15 @@ To enable it, set the new defcustom 'diff-font-lock-prettify' to t.
 of the file under version control if point is on an old changed line,
 or to the new revision of the file otherwise.
 
+** Texinfo
+
++++
+*** New function for inserting @pxref, @xref, or @ref commands.
+The function 'texinfo-insert-dwim-@ref', bound to 'C-c C-c r' by
+default, inserts one of three types of references based on the text
+surrounding point, namely @pxref near a parenthesis, @xref at the
+start of a sentence or at (point-min), else @ref.
+
 ** Browse-url
 
 *** The function 'browse-url-emacs' can now visit a URL in selected window.
index 1a900122f948b46072bdae23df79dce29444662f..71cdcab57efdda92c1364fd4c7f2c7a064344c08 100644 (file)
@@ -470,6 +470,7 @@ Subexpression 1 is what goes into the corresponding `@end' statement.")
     (define-key map "\C-c\C-cu"    'texinfo-insert-@uref)
     (define-key map "\C-c\C-ct"    'texinfo-insert-@table)
     (define-key map "\C-c\C-cs"    'texinfo-insert-@samp)
+    (define-key map "\C-c\C-cr"    'texinfo-insert-dwim-@ref)
     (define-key map "\C-c\C-cq"    'texinfo-insert-@quotation)
     (define-key map "\C-c\C-co"    'texinfo-insert-@noindent)
     (define-key map "\C-c\C-cn"    'texinfo-insert-@node)
@@ -825,6 +826,38 @@ Leave point after `@node'."
   "Insert the string `@quotation' in a Texinfo buffer."
   \n "@quotation" \n _ \n)
 
+(define-skeleton texinfo-insert-dwim-@ref
+  "Insert appropriate `@pxref{...}', `@xref{}', or `@ref{}' command.
+
+Looks at text around point to decide what to insert; an unclosed
+preceding open parenthesis results in '@pxref{}', point at the
+beginning of a sentence or at (point-min) yields '@xref{}', any
+other location (including inside a word), will result in '@ref{}'
+at the nearest previous whitespace or beginning-of-line.  A
+numeric argument says how many words the braces should surround.
+The default is not to surround any existing words with the
+braces."
+  nil
+  (cond
+   ;; parenthesis
+   ((looking-back "([^)]*" (point-at-bol 0))
+    "@pxref{")
+   ;; beginning of sentence or buffer
+   ((or (looking-back (sentence-end) (point-at-bol 0))
+        (= (point) (point-min)))
+    "@xref{")
+   ;; bol or eol
+   ((looking-at "^\\|$")
+    "@ref{")
+   ;; inside word
+   ((not (eq (char-syntax (char-after)) ? ))
+    (skip-syntax-backward "^ " (point-at-bol))
+    "@ref{")
+   ;; everything else
+   (t
+    "@ref{"))
+  _ "}")
+
 (define-skeleton texinfo-insert-@samp
   "Insert a `@samp{...}' command in a Texinfo buffer.
 A numeric argument says how many words the braces should surround.