forked from 11111000000/pen.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pen-find-file.el
154 lines (137 loc) · 6.08 KB
/
pen-find-file.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(defun open-doc-as-text (&optional fp)
(if (not fp)
(setq fp (get-path)))
(let* ((dn (f-dirname fp))
(bn (basename fp))
(fn (file-name-sans-extension bn))
(ext (file-name-extension bn)))
(cond ((or (string-equal "pdf" ext)
(string-equal "PDF" ext))
(if (>= (prefix-numeric-value current-prefix-arg) 16)
(progn
(pen-sn "pdfs2txt" nil dn)
(let ((b (current-buffer)))
(find-file (concat dn "/" fn ".txt"))
(kill-buffer b)))
(progn
(if (yes-or-no-p "Add to calibredb?")
(pen-calibre-add fp)))
(if (or (>= (prefix-numeric-value current-prefix-arg) 4)
(not (yes-or-no-p "Open text form?")))
(progn
(kill-buffer (current-buffer))
;; (r fp)
(pen-sn (concat "z " (pen-q fp)) nil nil nil t))
(progn
(pen-sn "pdfs2txt" nil dn)
(let ((b (current-buffer)))
(find-file (concat dn "/" fn ".txt"))
(kill-buffer b))))))
((string-equal "ipynb" ext)
(progn
(pen-sn (concat "ipynb2py " (pen-q bn)) nil dn)
(let ((b (current-buffer)))
(find-file (concat dn "/" fn ".py"))
(kill-buffer b))))
((string-equal "emacsprofiler" ext)
(let ((b (current-buffer)))
(profiler-find-profile fp)
(kill-buffer b)))
;; edbi-sqlite
((or (string-equal "sqlite" ext)
(string-equal "sqlite3" ext)
(string-equal "sqlitedb" ext)
(string-equal "db" ext)
(string-equal "db3" ext))
(progn
(let ((b (current-buffer)))
(edbi-sqlite fp)
(kill-buffer b))))
((istr-match-p "html" ext)
(find-file fp))
((string-equal "svg" ext)
(if (>= (prefix-numeric-value current-prefix-arg) 4)
(find-file fp)
(progn
(eval `(run-with-timer 0.1 nil (lambda () (kill-buffer (find-file ,fp)))))
(snd (concat "fehdf " (pen-q fp))))))
((string-equal "jpg" ext)
(let ((b (current-buffer)))
(pen-term-nsfa (concat "win ie " (pen-q fp)))
(kill-buffer b)))
((string-equal "pkl" ext)
(let ((b (current-buffer)))
(pen-sps (concat "orpy " (pen-q fp)))
(kill-buffer b))))))
;; It matters where in the list it is
;; The sooner the better
(remove-hook 'find-file-hooks 'open-doc-as-text)
(add-hook 'find-file-hooks 'open-doc-as-text)
(defun find-file-change-dir ()
(let ((cwd (cwd))
(fn (f-basename (get-path nil t)))
(pdn (f-basename (f-dirname (get-path nil t))))
(gdp (pen-umn "$HOME/var/smulliga/source/git/mullikine/glossaries")))
(cond
((and (string-equal fn "glossary.txt")
(f-file-p (f-join (pen-umn gdp) (concat pdn ".txt"))))
(cd gdp)))
t))
(add-hook 'find-file-hooks 'find-file-change-dir)
(defun open-pat (pat &optional ext a_dir)
(interactive (list (read-string-hist "open-pat: ")))
(let* ((dir (or
(if (>= (prefix-numeric-value current-prefix-arg) 4)
(get-top-level))
a_dir
(get-dir)))
(found (sor (fz (chomp (pen-sn (pen-cmd "pen-grep-pos" pat) nil dir)) nil nil nil nil t) nil)))
(if found
(let ((path (s-replace-regexp "^\\([^:]+\\).*" "\\1" found))
(pos (s-replace-regexp "^[^:]+:\\([0-9]+\\):.*" "\\1" found)))
(with-current-buffer (find-file (if dir
(concat dir "/" path)
(concat dir path)))
(goto-byte (string-to-number pos))))
(message "Pattern " pat " not found"))))
(defun open-main ()
(interactive)
(let* ((cwd (get-dir))
(dir (if (>= (prefix-numeric-value current-prefix-arg) 4)
(get-top-level)))
(found (sor (fz (chomp (pen-sn "open-main" nil dir)) nil nil nil nil t) nil)))
(if found
(let ((path (s-replace-regexp "^\\([^:]+\\).*" "\\1" found))
(pos (s-replace-regexp "^[^:]+:\\([0-9]+\\):.*" "\\1" found)))
(with-current-buffer (find-file (if dir
(concat dir "/" path)
(concat cwd path)))
(goto-byte (string-to-number pos))))
(message "Main function not found"))))
(defun find-repo-by-ext (&optional ext)
(interactive (list (read-string-hist "find-repo-by-ext ext: ")))
(if (sor ext)
(let ((repo (chomp (fz (pen-sn (concat "unbuffer pen-ci list-repos-with-ext " ext))))))
(if repo
(e (concat "$MYGIT/" repo))))))
(defun find-repo-by-content (&optional content)
(interactive (list (read-string-hist "find-repo-by-content content: ")))
(if (sor content)
(let ((repo (chomp (fz (pen-sn (concat "unbuffer pen-ci list-repos-containing " content))))))
(if repo
(e (concat "$MYGIT/" repo))))))
(defun list-mygit-for-paths (paths-string)
(-non-nil (mapcar 'sor (str2list (chomp (pen-sn "grep -P \"^/home/shane/source/git\" | sed \"s=^/home/shane/source/git/==\" | path-firsttwo | uniqnosort" paths-string))))))
(defun find-repo-by-path (&optional path-pat)
(interactive (list (read-string-hist "find-repo-by-path pattern: ")))
(if (sor path-pat)
(let ((repo (fz (list-mygit-for-paths (pen-sn (concat "unbuffer pen-ci locate -r " path-pat " | pen-mnm | pen-umn"))))))
(if repo
(e (concat "$MYGIT/" repo))))))
(define-key global-map (kbd "H-o") 'open-main)
(define-key global-map (kbd "H-E") 'find-repo-by-ext)
(define-key global-map (kbd "H-t e") 'find-repo-by-ext)
(define-key global-map (kbd "H-t t") 'find-repo-by-content)
(define-key global-map (kbd "H-t c") 'find-repo-by-content)
(define-key global-map (kbd "H-t f") 'find-repo-by-path)
(provide 'pen-find-file)