Browse Source

Improved directory switching

* Added Index management module for switching directories to search
and index
* Reformatted search
* Got rid of default directory for Codesearch alternate
codepath. .csearchindex will always be in the same directory as the
one being indexed from now on
master
Macoy Madson 2 years ago
parent
commit
793f4cdeee
  1. 57
      Emacs/desktop-management.el
  2. 27
      Emacs/index-management.el
  3. 1
      Emacs/packages.el
  4. 292
      Emacs/search.el

57
Emacs/desktop-management.el

@ -20,56 +20,49 @@
;;
(setq macoy-desktop-dir "~/.emacs.d/macoy-desktops/")
(unless (file-exists-p macoy-desktop-dir)
(make-directory macoy-desktop-dir)
)
(make-directory macoy-desktop-dir))
(defun macoy-save-desktop (new-desktop-name)
"Save a desktop to the desktop registry for easy switching"
(interactive "sNew desktop name: ")
(let ((new-desktop (concat macoy-desktop-dir new-desktop-name)))
(make-directory new-desktop)
(desktop-save new-desktop)
(customize-save-variable 'macoy-selected-desktop new-desktop)
(message "Created desktop at %s" new-desktop)
(setq macoy-has-ever-selected-desktop-this-session t)
))
(make-directory new-desktop)
(desktop-save new-desktop)
(customize-save-variable 'macoy-selected-desktop new-desktop)
(message "Created desktop at %s" new-desktop)
(setq macoy-has-ever-selected-desktop-this-session t)))
(defun macoy-get-desktop-list ()
(let ((desktop-list (remove "."
(remove ".."
(directory-files macoy-desktop-dir)))))
;; If we've never picked a desktop this session, put the last used desktop at the top of the
;; list. This is a workaround while I don't know how to make my desktop switch happen after
;; the regular emacs behavior picks the desktop during startup.
(if (and (not macoy-has-ever-selected-desktop-this-session)
macoy-selected-desktop)
(progn
(setq desktop-list (remove (file-name-nondirectory macoy-selected-desktop) desktop-list))
(add-to-list 'desktop-list (file-name-nondirectory macoy-selected-desktop))
)
desktop-list
)))
(remove ".."
(directory-files macoy-desktop-dir)))))
;; If we've never picked a desktop this session, put the last used desktop at the top of the
;; list. This is a workaround while I don't know how to make my desktop switch happen after
;; the regular emacs behavior picks the desktop during startup.
(if (and (not macoy-has-ever-selected-desktop-this-session)
macoy-selected-desktop)
(progn
(setq desktop-list (remove (file-name-nondirectory macoy-selected-desktop) desktop-list))
(add-to-list 'desktop-list (file-name-nondirectory macoy-selected-desktop)))
desktop-list)))
(defun macoy-switch-desktop ()
"Use ido to list desktops to switch to"
(interactive)
(let ((selected-desktop
(concat macoy-desktop-dir
(ido-completing-read "Switch Desktop: "
(macoy-get-desktop-list)))))
(message "%s" selected-desktop)
(customize-save-variable 'macoy-selected-desktop selected-desktop)
(desktop-change-dir selected-desktop)
(setq macoy-has-ever-selected-desktop-this-session t)
)
)
(concat macoy-desktop-dir
(ido-completing-read "Switch Desktop: "
(macoy-get-desktop-list)))))
(message "%s" selected-desktop)
(customize-save-variable 'macoy-selected-desktop selected-desktop)
(desktop-change-dir selected-desktop)
(setq macoy-has-ever-selected-desktop-this-session t)))
(defun macoy-reload-last-desktop ()
"Load the last desktop which was saved or switched to via the macoy-desktop system"
(interactive)
(desktop-change-dir macoy-selected-desktop)
(message "Restored desktop %s" macoy-selected-desktop)
(setq macoy-has-ever-selected-desktop-this-session t)
)
(setq macoy-has-ever-selected-desktop-this-session t))
(global-set-key (kbd "M-b") 'macoy-reload-last-desktop)

27
Emacs/index-management.el

@ -0,0 +1,27 @@
;; Index management: determine which index files to use for things like tags, codesearch, etc.
(defun macoy-set-index-directories (&optional code-dir data-dir)
"Change which directories Codesearch, tags, and other utilities create their indexes for"
(interactive)
(setq macoy-active-code-dir
(if code-dir
code-dir
;; Strip the trailing '/'
(substring (read-directory-name
(format "Code index directory (was %s): " macoy-active-code-dir)) 0 -1)))
(setq macoy-active-data-dir
(if data-dir
data-dir
;; Strip the trailing '/'
(substring (read-directory-name
(format "Data index directory (was %s): " macoy-active-data-dir)) 0 -1)))
;; Follow up by updating things which rely on this statically
(setq tortoise-svn-repository-path macoy-active-code-dir)
(setq macoy-active-data-dir-escaped (macoy-unix-path-to-windows-path macoy-active-data-dir))
;; Narrow the search a bit
(setq macoy-active-data-search-dir (format "%s/data" macoy-active-data-dir))
(setq codesearch-dir-to-index macoy-active-code-dir)
(setq codesearch-cindex-args (list "-reset" "-exclude" (format "%s/csearchIgnorePatterns" macoy-active-code-dir)))
(setq macoy-codesearch-search-data-dir macoy-active-data-search-dir))
;; TODO: Make codesearch support multiple index files. Is that possible?

1
Emacs/packages.el

@ -31,6 +31,7 @@
base16-theme
better-defaults
clang-format
csharp-mode
darktooth-theme
diminish
dired-narrow

292
Emacs/search.el

@ -40,8 +40,7 @@ If there's a string at point, offer that as a default."
"Using `search-function', execute the search"
(interactive)
(kill-buffer macoy-select-search-buf-name)
(apply search-function macoy-select-search-search after-search-args)
)
(apply search-function macoy-select-search-search after-search-args))
;; TODO: Make generic system (e.g. refresh cache selector)
(setq macoy-select-search-key-descriptions "Select search:
@ -57,28 +56,28 @@ If there's a string at point, offer that as a default."
(defvar macoy-select-search-minor-mode-map
(let ((map (make-sparse-keymap)))
;; Ag with default directory
(define-key map (kbd "a") (lambda () (interactive) (macoy-select-do-search
'ag default-directory)))
;; Ag with browse directory
(define-key map (kbd "b") (lambda () (interactive) (macoy-select-do-search
'ag (read-directory-name "Directory: "))))
;; Ag with project filter
(define-key map (kbd "f") (lambda () (interactive) (macoy-select-do-search
'macoy-ag-with-project-filter (read-directory-name "Directory: "))))
;; Codesearch source with filter (directory defined by `codesearch-dir-to-index', aka use ~/.csearchindex)
(define-key map (kbd "c") (lambda () (interactive) (macoy-select-do-search
'macoy-codesearch-search-src)))
;; Codesearch data
(define-key map (kbd "d") (lambda () (interactive) (macoy-select-do-search
'macoy-codesearch-search-with-filter-directory macoy-codesearch-search-data-dir)))
;; Swiper all
(define-key map (kbd "s") (lambda () (interactive) (macoy-select-do-search
'swiper-all)))
;; Internet search
(define-key map (kbd "i") (lambda () (interactive) (macoy-select-do-search
'engine/search-duckduckgo)))
(define-key map (kbd "q") (lambda () (interactive) (kill-buffer macoy-select-search-buf-name)))
(define-key map (kbd "a") (lambda () (interactive) (macoy-select-do-search
'ag default-directory)))
;; Ag with browse directory
(define-key map (kbd "b") (lambda () (interactive) (macoy-select-do-search
'ag (read-directory-name "Directory: "))))
;; Ag with project filter
(define-key map (kbd "f") (lambda () (interactive) (macoy-select-do-search
'macoy-ag-with-project-filter (read-directory-name "Directory: "))))
;; Codesearch source with filter (directory defined by `codesearch-dir-to-index', aka use ~/.csearchindex)
(define-key map (kbd "c") (lambda () (interactive) (macoy-select-do-search
'macoy-codesearch-search-src)))
;; Codesearch data
(define-key map (kbd "d") (lambda () (interactive) (macoy-select-do-search
'macoy-codesearch-search-with-filter-directory macoy-codesearch-search-data-dir)))
;; Swiper all
(define-key map (kbd "s") (lambda () (interactive) (macoy-select-do-search
'swiper-all)))
;; Internet search
(define-key map (kbd "i") (lambda () (interactive) (macoy-select-do-search
'engine/search-duckduckgo)))
(define-key map (kbd "q") (lambda () (interactive) (kill-buffer macoy-select-search-buf-name)))
map)
"macoy-select-search-minor-mode keymap.")
@ -91,25 +90,24 @@ If there's a string at point, offer that as a default."
(interactive (list (macoy-read-from-minibuffer "Search string")))
;; Only search if search string
(when string
;; Hang on to the search string
(setq macoy-select-search-search string)
;; Create a buffer `macoy-select-search-buf-name'
(get-buffer-create macoy-select-search-buf-name)
;; Switch to the `macoy-select-search-buf-name' buffer
;; (switch-to-buffer-other-window macoy-select-search-buf-name)
(switch-to-buffer macoy-select-search-buf-name)
(insert macoy-select-search-key-descriptions)
;; Set its major mode to `special-mode'
(special-mode) ; a read-only major mode
;; As you are now in the `macoy-select-search-buf-name' buffer, you can do the below:
;; - hit `q' to quit the current window *without* killing `macoy-select-search-buf-name'
;; - hit `C-u q' to quit the current window *and* kill `macoy-select-search-buf-name'
(macoy-select-search-minor-mode 1)
))
;; Hang on to the search string
(setq macoy-select-search-search string)
;; Create a buffer `macoy-select-search-buf-name'
(get-buffer-create macoy-select-search-buf-name)
;; Switch to the `macoy-select-search-buf-name' buffer
;; (switch-to-buffer-other-window macoy-select-search-buf-name)
(switch-to-buffer macoy-select-search-buf-name)
(insert macoy-select-search-key-descriptions)
;; Set its major mode to `special-mode'
(special-mode) ; a read-only major mode
;; As you are now in the `macoy-select-search-buf-name' buffer, you can do the below:
;; - hit `q' to quit the current window *without* killing `macoy-select-search-buf-name'
;; - hit `C-u q' to quit the current window *and* kill `macoy-select-search-buf-name'
(macoy-select-search-minor-mode 1)))
(global-set-key (kbd "C-e") 'macoy-select-search)
@ -122,18 +120,15 @@ If there's a string at point, offer that as a default."
"Conditionally filter ag results based on whether I know I should filter certain files (i.e. I'm in a project)"
(interactive)
(if (projectile-project-p)
(setq ag-arguments '("--smart-case" "--stats" "-G"
"(\\.txt|\\.org|\\.cpp|\\.c|\\.h|\\.inl|\\.html|\\.css|\\.lua|\\.js|\\.py|\\.cdm|\\.el)"
"--ignore" "AutoGen"))
(setq ag-arguments '("--smart-case" "--stats"))
)
(setq ag-arguments '("--smart-case" "--stats" "-G"
"(\\.txt|\\.org|\\.cpp|\\.c|\\.h|\\.inl|\\.html|\\.css|\\.lua|\\.js|\\.py|\\.cdm|\\.el)"
"--ignore" "AutoGen"))
(setq ag-arguments '("--smart-case" "--stats")))
(if (and pattern directory)
(ag pattern directory)
(call-interactively 'ag)
)
(ag pattern directory)
(call-interactively 'ag))
;; Reset the arguments so we don't unintentionally have a filter
(setq ag-arguments '("--smart-case" "--stats"))
)
(setq ag-arguments '("--smart-case" "--stats")))
;; (global-set-key (kbd "C-M-f") 'macoy-ag-with-project-filter)
;; (global-set-key (kbd "C-M-F") 'ag)
@ -148,7 +143,7 @@ If there's a string at point, offer that as a default."
;;
;; Always rescan all folders when indexing
(if codesearch-filter-supported
(setq codesearch-cindex-args (list "-reset" "-exclude" "f:/CJUNCTIONS/src/csearchIgnorePatterns"))
(setq codesearch-cindex-args (list "-reset" "-exclude" "f:/CJUNCTIONS/src/csearchIgnorePatterns"))
;; Use if junkblocker codesearch fork is not used
(setq codesearch-cindex-args (list "-reset")))
@ -158,19 +153,19 @@ If there's a string at point, offer that as a default."
(setq codesearch-csearch-exe "c:/Users/mmadson/go/bin/csearch.exe")
(setq codesearch-cindex-exe "c:/Users/mmadson/go/bin/cindex.exe")
(setq codesearch-dir-to-index "f:/CJUNCTIONS/src")
(setq codesearch-index-file "~/.csearchindex")
(setq codesearch-temp-file "c:/.temp-codesearch.txt")
;; (setq macoy-codesearch-search-data-dir "D:/Magic/data")
)
(setq codesearch-temp-file "c:/.temp-codesearch.txt"))
;; (setq macoy-codesearch-search-data-dir "D:/Magic/data"))
(when (string-equal (user-login-name) "macoy")
(setq codesearch-csearch-exe "csearch")
(setq codesearch-cindex-exe "cindex")
(setq codesearch-dir-to-index "/home/macoy/Development/code/repositories")
(setq codesearch-index-file "~/.csearchindex")
(setq codesearch-temp-file "~/.temp-codesearch.txt")
(setq macoy-codesearch-search-data-dir nil)
)
(setq macoy-codesearch-search-data-dir nil))
;; Make it possible to index multiple directories at the same time
(defun macoy-codesearch-name-function (name-of-mode)
(format "*Codesearch index* %s" compilation-environment))
;; TODO: Rename compilation buffer and add a finished message?
(defun macoy-codesearch-index-directory (directory)
@ -178,11 +173,12 @@ If there's a string at point, offer that as a default."
(interactive)
(message "Running Codesearch index on %s" directory)
(set (make-local-variable 'compilation-environment)
(list (format "%s%s%s" "CSEARCHINDEX=" directory "/.csearchindex")))
(compile (string-join
(append (list codesearch-cindex-exe)
codesearch-cindex-args
(list directory)) " ")))
(list (format "%s%s%s" "CSEARCHINDEX=" directory "/.csearchindex")))
(let ((compilation-buffer-name-function 'macoy-codesearch-name-function))
(compile (string-join
(append (list codesearch-cindex-exe)
codesearch-cindex-args
(list directory)) " "))))
(defun macoy-codesearch-index-default ()
(interactive)
@ -191,19 +187,16 @@ If there's a string at point, offer that as a default."
;; This is okay too because having an existing index doesn't speed things up all that much
;; Commented because this is no longer needed due to using -reset
;; (when (file-exists-p codesearch-index-file)
;; (delete-file codesearch-index-file)
;; )
;; (delete-file codesearch-index-file))
;; We won't use this way for now so that we can run two indexes at the same time
;; (macoy-codesearch-index-directory codesearch-dir-to-index)
(let ((codesearch-proc (apply 'start-process
(append (list "CodesearchIndex" "*Codesearch-Index*" codesearch-cindex-exe)
codesearch-cindex-args
(list codesearch-dir-to-index)))))
(set-process-sentinel codesearch-proc
(lambda (codesearch-proc _string)
(message "Codesearch finished building index")))
)
)
(macoy-codesearch-index-directory codesearch-dir-to-index))
;; (let ((codesearch-proc (apply 'start-process
;; (append (list "CodesearchIndex" "*Codesearch-Index*" codesearch-cindex-exe)
;; codesearch-cindex-args
;; (list codesearch-dir-to-index)))))
;; (set-process-sentinel codesearch-proc
;; (lambda (codesearch-proc _string)
;; (message "Codesearch finished building index")))))
;; TODO: There is no way to undo after doing this; you have to repeat the search
(defun macoy-filter-buffer ()
@ -214,15 +207,13 @@ If there's a string at point, offer that as a default."
(read-only-mode 0)
(call-interactively 'keep-lines)
;; Start with the cursor on the first result
(compilation-next-error 1)
)
(compilation-next-error 1))
;; Refer to ag.el for customization
(define-compilation-mode macoy-codesearch-mode "Codesearch"
"Codesearch results compilation mode"
;; This is so you can delete results with C-S-k. This doesn't break n and p which is cool
(read-only-mode 0)
)
(read-only-mode 0))
(define-key macoy-codesearch-mode-map (kbd "p") #'compilation-previous-error)
(define-key macoy-codesearch-mode-map (kbd "n") #'compilation-next-error)
@ -241,34 +232,30 @@ If there's a string at point, offer that as a default."
;; Args explanation: -n (Line numbers) -i (ignore case)
;; Output Codesearch results to a temporary file, filter out lines, then clean up the temp file
(if directory
(set (make-local-variable 'compilation-environment) (list (format "%s%s%s" "CSEARCHINDEX=" directory "/.csearchindex")))
(set (make-local-variable 'compilation-environment) nil)
)
(set (make-local-variable 'compilation-environment) (list (format "%s%s%s" "CSEARCHINDEX=" directory "/.csearchindex")))
(set (make-local-variable 'compilation-environment) nil))
(if codesearch-filter-supported
(compilation-start (format "%s -n -i \"%s\""
codesearch-csearch-exe
;; (regexp-quote pattern) ;; This doesn't work because more escape(\) chars are added
pattern)
#'macoy-codesearch-mode
`(lambda (mode-name) , "*Codesearch*")
)
;; This temp-file thing sucks but seems necessary for Windows
(compilation-start (format "%s -n -i \"%s\" > %s && grep -i -E -v \"%s\" %s && rm %s"
codesearch-csearch-exe
;; (regexp-quote pattern) ;; This doesn't work because more escape(\) chars are added
pattern
codesearch-temp-file
macoy-codesearch-ignore-lines-pattern
codesearch-temp-file
codesearch-temp-file)
#'macoy-codesearch-mode
`(lambda (mode-name) , "*Codesearch*")
)))
(compilation-start (format "%s -n -i \"%s\""
codesearch-csearch-exe
;; (regexp-quote pattern) ;; This doesn't work because more escape(\) chars are added
pattern)
#'macoy-codesearch-mode
`(lambda (mode-name) , "*Codesearch*"))
;; This temp-file thing sucks but seems necessary for Windows
(compilation-start (format "%s -n -i \"%s\" > %s && grep -i -E -v \"%s\" %s && rm %s"
codesearch-csearch-exe
;; (regexp-quote pattern) ;; This doesn't work because more escape(\) chars are added
pattern
codesearch-temp-file
macoy-codesearch-ignore-lines-pattern
codesearch-temp-file
codesearch-temp-file)
#'macoy-codesearch-mode
`(lambda (mode-name) , "*Codesearch*"))))
(defun macoy-codesearch-search-src (pattern)
(interactive (list (macoy-read-from-minibuffer "Search string")))
(macoy-codesearch-search-with-filter-directory pattern nil)
)
(macoy-codesearch-search-with-filter-directory pattern codesearch-dir-to-index))
(global-set-key (kbd "C-S-f") 'macoy-codesearch-search-src)
@ -276,8 +263,8 @@ If there's a string at point, offer that as a default."
(defun macoy-isearch-yank-clipboard ()
"Insert the contents of the clipboard into isearch. We do this because we don't use the yank stuff"
(interactive)
(isearch-yank-string (simpleclip-get-contents))
)
(isearch-yank-string (simpleclip-get-contents)))
(define-key isearch-mode-map (kbd "C-v") 'macoy-isearch-yank-clipboard)
;; Go to next/previous result with arrow keys
(define-key isearch-mode-map (kbd "<up>") 'isearch-repeat-backward)
@ -293,16 +280,14 @@ If there's a string at point, offer that as a default."
;; End the search
(isearch-done)
;; Select the results list buffer
(switch-to-buffer-other-window "*Occur*")
)
(switch-to-buffer-other-window "*Occur*"))
(define-key isearch-mode-map (kbd "C-a") 'macoy-isearch-occur)
;; We're going to use smooth scrolling only for isearch so there's a good margin between a search
;; result which would've ended up at the very bottom of the window without smooth scrolling
(require 'smooth-scrolling)
(defun macoy-isearch-end-hook ()
(smooth-scrolling-mode 0)
)
(smooth-scrolling-mode 0))
(setq isearch-mode-end-hook 'macoy-isearch-end-hook)
;; If marked, use swiper to search mark
@ -315,48 +300,44 @@ If there's a string at point, offer that as a default."
(call-interactively 'isearch-forward)
(when (use-region-p)
;; (isearch-search)
;; (call-interactively 'isearch-forward)
(isearch-yank-string (buffer-substring (region-beginning) (region-end)))
;; Clear the mark for two reasons:
;; 1. Occur will only search the region, but whenever I use occur I expect
;; it to show me all results in the entire file
;; 2. I don't like how it looks visually when the mark is expanding while I'm going
;; through results.
;; I never use isearch to change my region, so this seemed like an acceptable tradeoff
(setq mark-active nil)
)
)
;; (isearch-search)
;; (call-interactively 'isearch-forward)
(isearch-yank-string (buffer-substring (region-beginning) (region-end)))
;; Clear the mark for two reasons:
;; 1. Occur will only search the region, but whenever I use occur I expect
;; it to show me all results in the entire file
;; 2. I don't like how it looks visually when the mark is expanding while I'm going
;; through results.
;; I never use isearch to change my region, so this seemed like an acceptable tradeoff
(setq mark-active nil)))
(global-set-key (kbd "C-f") 'macoy-isearch-search-mark)
;; Swiper customizations
(when (require 'swiper)
;; If marked, use swiper to search mark
(defun macoy-swiper-search-mark ()
"If marked, use swiper to search mark. Otherwise, open swiper normally"
(interactive)
;; This isn't sufficient for my purposes; it's nice to search e.g. thing->thing
;;(swiper (thing-at-point 'symbol))
(if (use-region-p)
(swiper (buffer-substring (region-beginning) (region-end)))
(swiper)
))
;; If marked, use swiper to search mark
(defun macoy-swiper-all-search-mark ()
"If marked, use swiper-all to search mark. Otherwise, open swiper-all normally"
(interactive)
;; This isn't sufficient for my purposes; it's nice to search e.g. thing->thing
;;(swiper (thing-at-point 'symbol))
(if (use-region-p)
(swiper-all (buffer-substring (region-beginning) (region-end)))
(swiper-all)
))
;; Use swiper for search instead of isearch (use e.g. space to fuzzy search)
;; (global-set-key (kbd "C-f") 'macoy-swiper-search-mark)
(global-set-key (kbd "M-f") 'macoy-swiper-all-search-mark)
)
;; (when (require 'swiper)
;; ;; If marked, use swiper to search mark
;; (defun macoy-swiper-search-mark ()
;; "If marked, use swiper to search mark. Otherwise, open swiper normally"
;; (interactive)
;; ;; This isn't sufficient for my purposes; it's nice to search e.g. thing->thing
;; ;;(swiper (thing-at-point 'symbol))
;; (if (use-region-p)
;; (swiper (buffer-substring (region-beginning) (region-end)))
;; (swiper)))
;; ;; If marked, use swiper to search mark
;; (defun macoy-swiper-all-search-mark ()
;; "If marked, use swiper-all to search mark. Otherwise, open swiper-all normally"
;; (interactive)
;; ;; This isn't sufficient for my purposes; it's nice to search e.g. thing->thing
;; ;;(swiper (thing-at-point 'symbol))
;; (if (use-region-p)
;; (swiper-all (buffer-substring (region-beginning) (region-end)))
;; (swiper-all)))
;; ;; Use swiper for search instead of isearch (use e.g. space to fuzzy search)
;; ;; (global-set-key (kbd "C-f") 'macoy-swiper-search-mark)
;; (global-set-key (kbd "M-f") 'macoy-swiper-all-search-mark))
;; Quickly search the web
;; See https://github.com/hrs/engine-mode for more browsers
@ -364,8 +345,7 @@ If there's a string at point, offer that as a default."
;; (engine-mode t)
(defengine duckduckgo
"https://duckduckgo.com/?q=%s"
:keybinding "d")
"https://duckduckgo.com/?q=%s"
:keybinding "d")
(global-set-key (kbd "M-l") 'engine/search-duckduckgo)
)
(global-set-key (kbd "M-l") 'engine/search-duckduckgo))

Loading…
Cancel
Save