Skip to content

Commit

Permalink
Add citar-add-file-to-library
Browse files Browse the repository at this point in the history
Replace the original pdf-oriented function with a more general one.

Close #528
  • Loading branch information
bdarcus committed May 14, 2022
1 parent 9a6fc6d commit 3d697d5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions citar.el
Expand Up @@ -1167,6 +1167,42 @@ With prefix, rebuild the cache before offering candidates."
(error "Make sure 'citar-library-paths' is a list of paths"))
(citar--library-file-action key-entry 'attach)))

(defun citar--add-file-to-library (key)
"Add a file to the library for KEY.
The FILE can be added from an open buffer, a file path, or a
URL."
(let* ((source
(char-to-string
(read-char-choice
"Add file from [b]uffer, [f]ile, or [u]rl? " '(?b ?f ?u))))
(directory (if (cdr citar-library-paths)
(completing-read "Directory: " citar-library-paths)
(car citar-library-paths)))
(file-path
;; Create the path without extension here.
(expand-file-name key directory)))
;; TODO confirm path before saving, add extensions to all options
(pcase source
("b"
(with-current-buffer (read-buffer-to-switch "Add file buffer: ")
(let ((extension (file-name-extension (buffer-file-name))))
(write-file (concat file-path "." extension)))))
("f"
(copy-file
(expand-file-name
(read-file-name "Add file: " nil nil t)) file-path))
("u"
(url-copy-file (read-string "Add file URL: ") file-path)))))

;;;###autoload
(defun citar-add-file-to-library (key-entry)
"Add a file to the library for KEY-ENTRY.
The FILE can be added either from an open buffer, a file, or a
URL."
(interactive (list (citar-select-ref
:rebuild-cache current-prefix-arg)))
(citar--add-file-to-library (car key-entry)))

;;;###autoload
(defun citar-run-default-action (keys-entries)
"Run the default action `citar-default-action' on KEYS-ENTRIES."
Expand Down

0 comments on commit 3d697d5

Please sign in to comment.