[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[AUCTeX] Drag and drop support

From: Michal Sojka
Subject: [AUCTeX] Drag and drop support
Date: Tue, 14 Dec 2010 14:10:48 +0100
User-agent: Notmuch/0.5-38-g923b170 ( Emacs/23.2.1 (x86_64-pc-linux-gnu)


I worked on a LaTeX document that was full of various figures. The
figures had to be selected from approximately 300 files sored in a
fileststem. I selected the figure to insert in a filemanager, but it was
too tedious to copy the name of the figure and insert
\includegraphics{<filename>} by hand. So I quickly hacked the following
piece of elisp to accomplish this automatically when a file is
drag-and-dropped to Emacs window. I think, that it would be useful if
something like this becomes a part of Auctex. What do you think?


(defcustom auctex-dnd-format "\\includegraphics{%s}"
  "What to insert, when a file is dropped on Emacs window. %s is
replaced by the actual file name. If the filename is located
under the directory of .tex document, only the part of the name
relative to that directory in used."
  :type 'string
  :group 'auctex-dnd)

(defun auctex-dnd-includegraphics (uri action)
  "Insert the text defined by `auctex-dnd-format' when a file is
dropped on Emacs window."
  (let ((file (dnd-get-local-file-name uri t)))
    (when (and file (file-regular-p file))
      (let* ((curdir (file-name-directory buffer-file-name))
             (path (if (string-match curdir file)
                       (replace-match "" t t file)
        (insert (format auctex-dnd-format path))))))

(defcustom auctex-dnd-protocol-alist
  '(("^file:///" . auctex-dnd-includegraphics)
    ("^file://"  . dnd-open-file)
    ("^file:"    . auctex-dnd-includegraphics))
  "The functions to call when a drop in `mml-mode' is made.
See `dnd-protocol-alist' for more information.  When nil, behave
as in other buffers."
  :type '(choice (repeat (cons (regexp) (function)))
                 (const :tag "Behave as in other buffers" nil))
  :version "22.1" ;; Gnus 5.10.9
  :group 'message)

(define-minor-mode auctex-dnd-mode
  "Minor mode to inser some text (\includegraphics by defaul)
when a file is dopped on Emacs window."
  :lighter " DND"
  (when (boundp 'dnd-protocol-alist)
    (if auctex-dnd-mode
        (set (make-local-variable 'dnd-protocol-alist)
             (append auctex-dnd-protocol-alist dnd-protocol-alist))
      (kill-local-variable 'dnd-protocol-alist))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]