Skip to content

Commit bae4737

Browse files
committed
Insert buffer-file-name as FILE if view mode is active
- checks if pdf-view-mode is active - checks if doc-view-mode is active - if either one is active it inserts the file name of the currently loaded pdf as FILE
1 parent a4ca0a1 commit bae4737

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

README.org

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ is displayed, the matching regions are highlighted.
1616
(pdfgrep-mode)
1717
#+end_src
1818

19+
* Running tests
20+
21+
#+begin_src bash
22+
emacs --batch -l pdfgrep.el -l pdfgrep-tests.el -f ert-run-tests-batch-and-exit
23+
#+end_src
24+
1925
* Example
2026

2127
[[./screenshot.png]]

pdfgrep-tests.el

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
;;; pdfgrep-tests.el --- Test for pdf-grep -*- lexical-binding: t; -*-
2+
3+
4+
;;; Commentary:
5+
6+
;;; Code:
7+
8+
9+
(require 'ert)
10+
(require 'pdfgrep)
11+
12+
(defun pth/pos-at-end (cmd)
13+
"Position at end of CMD. Counting begins at 1."
14+
(1+ (length cmd)))
15+
16+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/ignore-case ()
17+
(let ((cmd "pdfgrep -H -n -i "))
18+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end cmd))))))
19+
20+
21+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/no-ignore-case ()
22+
(let ((pdfgrep-ignore-case 'nil)
23+
(cmd "pdfgrep -H -n "))
24+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end cmd))))))
25+
26+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/no-ignore-case-pdf-view-mode ()
27+
(let* ((pdfgrep-ignore-case 'nil)
28+
(major-mode 'pdf-view-mode)
29+
(buffer-file-name "/some/path/to/test.pdf")
30+
(base-cmd "pdfgrep -H -n ")
31+
(cmd (concat base-cmd buffer-file-name)))
32+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end cmd))))))
33+
34+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/no-ignore-case-doc-view-mode ()
35+
(let* ((pdfgrep-ignore-case 'nil)
36+
(major-mode 'doc-view-mode)
37+
(buffer-file-name "/some/path/to/test.pdf")
38+
(base-cmd "pdfgrep -H -n ")
39+
(cmd (concat base-cmd buffer-file-name)))
40+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end cmd))))))
41+
42+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/ignore-case-ignore-errors ()
43+
(let* ((pdfgrep-ignore-errors t)
44+
(base-cmd "pdfgrep -H -n -i ")
45+
(ignore-errors " 2>/dev/null")
46+
(cmd (concat base-cmd ignore-errors)))
47+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end base-cmd))))))
48+
49+
50+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/no-ignore-case-ignore-errors ()
51+
(let* ((pdfgrep-ignore-errors t)
52+
(pdfgrep-ignore-case 'nil)
53+
(base-cmd "pdfgrep -H -n ")
54+
(ignore-errors " 2>/dev/null")
55+
(cmd (concat base-cmd ignore-errors)))
56+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end base-cmd))))))
57+
58+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/no-ignore-case-pdf-view-mode ()
59+
(let* ((pdfgrep-ignore-errors t)
60+
(pdfgrep-ignore-case 'nil)
61+
(major-mode 'pdf-view-mode)
62+
(buffer-file-name "/some/path/to/test.pdf")
63+
(base-cmd "pdfgrep -H -n ")
64+
(ignore-errors " 2>/dev/null")
65+
(cmd (concat base-cmd buffer-file-name ignore-errors)))
66+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end cmd))))))
67+
68+
(ert-deftest pdfgrep-tests/pdfgrep-default-command/no-ignore-case-doc-view-mode ()
69+
(let* ((pdfgrep-ignore-errors t)
70+
(pdfgrep-ignore-case 'nil)
71+
(major-mode 'doc-view-mode)
72+
(buffer-file-name "/some/path/to/test.pdf")
73+
(base-cmd "pdfgrep -H -n ")
74+
(ignore-errors " 2>/dev/null")
75+
(cmd (concat base-cmd buffer-file-name ignore-errors)))
76+
(should (equal (pdfgrep-default-command) `(,cmd . ,(pth/pos-at-end cmd))))))
77+
78+
(provide 'pdfgrep-tests)
79+
80+
;;; pdfgrep-tests.el ends here

pdfgrep.el

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,20 @@ Not including `pdfgrep-ignore-case'."
6161

6262
(defun pdfgrep-default-command ()
6363
"Compute the default pdfgrep command for `pdfgrep'."
64-
(let ((cmd (concat pdfgrep-program pdfgrep-options
65-
(when pdfgrep-ignore-case
66-
"-i "))))
64+
(let* ((supported-major-modes '(pdf-view-mode doc-view-mode))
65+
(is-in-supported-major-mode (member major-mode supported-major-modes))
66+
(cmd (concat pdfgrep-program pdfgrep-options
67+
(when pdfgrep-ignore-case
68+
"-i ")
69+
(when is-in-supported-major-mode
70+
buffer-file-name))))
6771
(if pdfgrep-ignore-errors
68-
(cons (concat cmd " 2>/dev/null") (1+ (length cmd)))
69-
cmd)))
72+
(let ((cmd-ignore-errors (concat cmd " 2>/dev/null")))
73+
(cons cmd-ignore-errors
74+
(if is-in-supported-major-mode
75+
(+ 1 (length cmd-ignore-errors)) ;; we already inserted the file name, set position end of cmd
76+
(1+ (length cmd))))) ;; position at FILE position
77+
(cons cmd (1+ (length cmd))))))
7078

7179
(defun pdfgrep (command-args)
7280
"Run pdfgrep with user-specified COMMAND-ARGS, collect output in a buffer.

0 commit comments

Comments
 (0)