Fuzzy Search with Counsel - ivy

I have this function:
crux-rename-file-and-buffer
, but when I do M-x rename, why isn't this shown as an alternative?
I thought ivy was supposed to assist with that, using a fuzzy search?
Why is it not picking this up?
My config to enable fuzzy:
(use-package counsel
:ensure t
:config
(use-package flx
:ensure t)
(ivy-mode 1)
(setq ivy-height 20)
(setq ivy-re-builders-alist '((t . ivy--regex-fuzzy))))
Any pointers?;)

Related

How do I control into which directory Emacs makes backup files dynamically?

I've seen the post How do I control how Emacs makes backup files? And a few similar post showing very similar solutions. I'm well aware of this approach. But it doesn't do quite what I would like it to do. I would like each file that I'm going to back up have its own personalized backup directory.
For example, let's say I have the following files in the current directory, /Users/me/project_a/
apple.txt
banana.txt
coconut.txt
when I edit these files, I would like them to have their backups stored in directories as follows:
/Users/me/project_a/.backups/apple.txt/
/Users/me/project_a/.backups/banana.txt/
/Users/me/project_a/.backups/coconut.txt/
If I another project directory, say /Users/me/project_b/, with files
needle.doc
thread.doc
thimble.doc
Then, their respective backups should be located as
/Users/me/project_b/.backups/needle.doc/
/Users/me/project_b/.backups/thread.doc/
/Users/me/project_b/.backups/thimble.doc/
Yes, I'm using the name of the file being backed up as part of the path name for the directory into which it's been saved. So, if I have three previous versions of thimble.doc, the full path name for the backups would be:
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~1~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~2~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~3~
I have a work around to accomplish that (see below). Ideally, I would name the backups as:
/Users/me/project_b/.backups/thimble.doc/bak.~1~
/Users/me/project_b/.backups/thimble.doc/bak.~2~
/Users/me/project_b/.backups/thimble.doc/bak.~3~
I haven't yet figured out how to get there. (Any suggestions?)
Here's how I'm able to accomplish the less ideal version of this using the .dir-locals.el file with the following code for saving .txt and .tex files.
(let (a b)
(dolist (ae-fh (directory-files-recursively "." "\\.\\(txt\\|tex\\)$"))
(setq a (file-name-nondirectory ae-fh))
(setq a (replace-regexp-in-string "\\." "\\\\." a))
(setq b (concat "./.backups/" (file-name-nondirectory ae-fh)))
(add-to-list 'backup-directory-alist (cons a b))
))
I've already made backup-directory-alist a buffer-local variable.
This accomplishes what I want, but I don't really like this approach. I would like to altogether avoid using .dir-locals.el as a solution to this problem. But also, I would like to avoid junking up the backup-directory-alist. It would be nice if there were a hook I could apply to the backup process which would inform emacs on the fly how it should name the backup file.
Does anyone know how to do this or know where to point me? I've considered trying to redefine make-backup-file-name-function, but I don't entirely understand what I'm doing with the elisp and right now I've got projects that need to be worked on.
What can folks tell me?
I have a partial solution.
It avoids using .dir-locals.el as I want.
It somewhat creates the path on the fly as I want too.
It still uses backup-directory-alist
I add the following to my emacs init file:
(add-hook 'after-change-major-mode-hook
'(lambda ()
(let (a b)
(if (stringp (buffer-file-name))
(progn
(setq a (file-name-nondirectory (buffer-file-name)))
(setq a (replace-regexp-in-string "\\." "\\\\." a))
(setq b (concat "./.backups/" (file-name-nondirectory (buffer-file-name))))
(add-to-list 'backup-directory-alist (cons a b))
)
)
)))
I don't quite know enough about this hook. So, I'm not sure that this will always work. And, I should probably consider whether there might be files for which this convention really is undesirable.
The (stringp (buffer-file-name)) is minimally necessary in the case of buffers for which there is no underlying file: such as *scratch* or even the mini-buffers.
I am still interested in what solutions others might come up with. Ideally, the solution would be more like
/Users/me/project_b/.backups/thimble.doc/bak.~1~
/Users/me/project_b/.backups/thimble.doc/bak.~2~
/Users/me/project_b/.backups/thimble.doc/bak.~3~
than
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~1~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~2~
/Users/me/project_b/.backups/thimble.doc/thimble.doc.~3~
UPDATE: Persistence will get you places!!!!
I finally did a google search on: "emacs how do i redefine make-backup-file-name". I found this description of an approach which I adapted to my situation. Namely, I did the following:
(defun z:backup:truncate.backup.name (file)
(concat (file-name-directory file) "bak"))
(advice-add 'make-backup-file-name-1 :filter-return #'z:backup:truncate.backup.name)
I'm not entirely sure of what everything is about.
I don't know what the #' syntax does.
I'm not sure what :filter-return does, though I can guess.
I should point out that I still need mae add-hook for 'after-change-major-mode-hook. Without it, I get errors. Apparently, I just can't write the advice function as
(defun z:backup:truncate.backup.name (file)
(concat (file-name-directory file) (file-name-nondirectory) "/bak"))
I tried it and got an error about not being able to back things up. And I think the reason for this is that backup-directory-alist is used to determine the directory path. I've got some ideas for some tweaks here. But this is where things stand currently.
UPDATE: no more need for my add-hook to after-change-major-mode-hook
I've gotten rid of the issue with the backup directory alist not necessarily being set for the correct directory. This is my final form for z:backup:truncate.backup.name
(defun z:backup:truncate.backup.name ()
(let* (
(dir (file-name-directory (buffer-file-name)))
(fh (file-name-nondirectory (buffer-file-name)))
(qdir (concat dir ".backups/" fh))
)
(if (not (file-directory-p qdir))
(progn
(if (file-directory-p dir)
(progn
(make-directory qdir)
))))
(concat qdir "/bak")))
So now more having to tweak backup-directory-alist. I've accomplished everything I wanted. YEAH.

How can i make the spacemacs compilation buffer split horizontally?

I am setting (setq split-width-threshold 100) in my dotspacemacs/user-config, in order to make various buffers split horizontally when the window is wide enough. This works as intended for magit status etc.
However, the compilation log buffer seems to disregard this, and always opens on the bottom.
How can i make the compilation buffer adhere to the split-width-threshold? Alternatively, how can get it to always split horizontally?
I am quite new to both emacs and spacemacs.
The reason for the compilation not to obey your settings is because spacemacs comes with purpose-mode enabled by default. If you use that, then it is a matter of modifying the purpose layouts as you wish. However if you are not using the purpose-mode, then disabling it fixes the issue for me. To just try it out you can do SPC SPC purpose-mode RET and then (with only one window opened) run the compilation.
Here's one way to do it (SPC f e d to get to your config file, then you could put this in the existing dotspacemacs-user-config function) -- here I've shown how to get both grep and compilation buffers popping up on the right:
(require 'dash)
(defun my/popwin-on-right (alist-item)
(let ((props-alist (seq-partition (cdr alist-item) 2)))
(setf (alist-get :position props-alist) '(right))
(setf (alist-get :height props-alist) '(1.0))
(setf (alist-get :width props-alist) '(0.5))
(let ((flattened (apply #'append props-alist)))
(cons (car alist-item) flattened))))
(custom-set-variables
'(popwin:special-display-config
(--map-when (-contains? '("*compilation*" "*grep*") (car it))
(my/popwin-on-right it)
popwin:special-display-config)))
or you could just set popwin:special-display-config more directly, replacing the --map-when call there with a literal list. Just view the variable's existing value e.g. using SPC SPC ielm <RET> to get nice formatting, then cut and paste it in (you'll need to quote the list using '). Or you could do what I do when I want to set a customizable variable as a literal value: use SPC SPC customize, let that update the end of your spacemacs config file with its blob of generated code, then copy out the custom-set-variables it generates into your dotspacemacs-user-config, and delete the blob of code that customize generated).
From another answer
(setq split-height-threshold nil)
(setq split-width-threshold 0)
If you want these settings to only affect compile
(defadvice compile (around split-horizontally activate)
(let ((split-width-threshold 0)
(split-height-threshold nil))
ad-do-it))

Invoke `racket` in a Racket script

General question:
Can I invoke the current racket executable from within a running Racket script?
Basically, I'd like a replacement for (system "racket ...") in the case that (find-executable-path "racket") does not return a path to the Racket executable I'm currently using.
Context:
What I really want is to try compiling a few expressions and assert that they raise compilation errors. This is for unit testing.
I don't believe you need to step outside of the executable here. Try this:
#lang racket
(require syntax/modread)
;; define a namespace anchor to attach a namespace to:
(define-namespace-anchor anchor)
;; define a namespace for expansion:
(define target-namespace (namespace-anchor->namespace anchor))
(define program-to-compile
"#lang racket
(+ 3 4)")
;; go ahead and expand
(with-module-reading-parameterization
(λ()
(parameterize ([current-namespace target-namespace])
(expand
(read-syntax
"bogus-filename"
(open-input-string program-to-compile))))))
I think I'm correct when I say that Racket is singularly clean in its ability to provide the compiler to running programs in a disciplined way.
If your goal is just to compile some racket expressions, you can do that just with either compile or compile-syntax. An example file would be:
#lang racket
(require rackunit)
(define tests
(list #'(+ 1 "3")
#'(void void)
#'(string-append 4)))
(for/list ([t (in-list test)])
(check-exn exn:fail?
(lambda () (compile t))))
Where exn:fail? is whatever exception you are looking for.
Furthermore, if you have some common syntax context you want to run your test in, you can use #` #,. So your code would end up something like this:
#lang racket
(require rackunit)
(define tests
(list #'(+ 1 "3")
#'(void void)
#'(string-append 4)))
(for/list ([t (in-list test)])
(check-exn exn:fail?
(lambda () (compile #`(module anonymous racket
#,t)))))
Finally, if your code is stored on your computer, you can use John's solution, while using file->string to convert the file into a string.
For small tests, you can also use convert-compile-time-error from the syntax/macro-testing library. It turns an expression that causes a compile-time error into an expression that raises a run-time error when evaluated. The expression uses the environment where it occurs in the module, including local bindings; you don't have to fiddle with namespaces and eval.
(check-exn #rx"bad syntax"
(lambda () (convert-compile-time-error (lambda))))
There's also convert-syntax-error (on the same page).

What is the correct syntax for .emacs file in emacs 23?

I am trying to get solarized color theme in emacs 23 as per http://david.rothlis.net/emacs/customize_colors.html . I have put all folders and files in ~/.emacs.d . Then I added:
(add-to-list 'load-path "~/.emacs.d/color-theme-6.6.0"')
(add-to-list 'load-path "~/.emacs.d/emacs-color-theme-solarized-master"')
(require 'color-theme)
(require 'color-theme-solarized)
To my .emacs file. This however gives me the following error:
Warning (initialization): An error occurred while loading `/home/brain/.emacs':
Invalid read syntax: )
With debugging thats:
Debugger entered--Lisp error: (invalid-read-syntax ")")
eval-buffer(#<buffer *load*> nil "/home/brain/.emacs" nil t) ; Reading at buffer position 80
load-with-code-conversion("/home/brain/.emacs" "/home/brain/.emacs" t t)
load("~/.emacs" t t)
#[nil "\205\264
I have been at this for a long time now and I just cant seem to find the right way to do this either by myself, on http://www.gnu.org/software/emacs/manual/html_node/emacs/Init-Syntax.html#Init-Syntax, on SO or elsewhere. I am quite new to Linux, though I dont think that is the cause of this. Any help is greatly appreciated. Thanks.
PS this is how my whole .emacs looks:
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:inherit nil :stipple nil :background "white" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 83 :width normal :foundry "unknown" :family "DejaVu Sans Mono")))))
(add-to-list 'load-path "~/.emacs.d/color-theme-6.6.0"')
(add-to-list 'load-path "~/.emacs.d/emacs-color-theme-solarized-master"')
(require 'color-theme)
(require 'color-theme-solarized)
Why are you quoting the whole thing?
(add-to-list 'load-path "~/.emacs.d/color-theme-6.6.0"')
(add-to-list'load-path "~/.emacs.d/emacs-color-theme-solarized-master"')
You do not want the last '.
You are only suppose to quote load-path so that it is passed to the add-to-list with out
being evaluated.
Try using this:
(add-to-list 'load-path "~/.emacs.d/color-theme-6.6.0")
(add-to-list 'load-path "~/.emacs.d/emacs-color-theme-solarized-master")
For more information See:
add-to-list
load-path
In addition to above you may want to add the following to your init.el
put it somewhere after ` (require 'color-theme) '
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
;; Load solarized at startup
(color-theme-solarized)))
The code above is from the color-theme site, my reputation is not high enough
to post more that one link else I would have include it.

Racket/Scribble dynamic pdf generation

Hello all I am trying to dynamically build pdf files based on switches in a global defines file.
In file global_defines.rkt i have
#lang racket/base
(provide (all-defined-out))
(define alpha #f)
(define beta #t)
(define gamma #f)
in file foo.scrbl
#lang scribble/base
#(require "../../pdf_gen/global_defines.rkt")
#title{Getting Started}
#if[(or alpha)
#para{Test text}
""]
#if[(or beta)
(begin
#dynamic-require["bar.scrbl" 'doc]
doc)
""]
and in file bar.scrbl
#lang scribble/base
#(require "../../../pdf_gen/global_defines.rkt")
#(provide (all-defined-out))
#title{BAR}
happy marbles
so with the switches as they are currently i would expect to get something similar to the following out
Getting Started
1.BAR
happy marbles
while there are definitly other ways i can do it i would prefer to stick with scribble as it makes formatting and everything much easier than the other methods i can come up with right now. My main concerns are keeping the switches in one place and being able to pick and choose the content that is triggered by the switches being active, as there is content that will be common between several of the documents but not all of them, and quite a bit of content that only belongs in one or 2 places.
Although this answer isn't as elegant as I'd like, it works.
Basically I think you're talking about conditional compilation. In C you'd use a macro for this. In Racket let's use a macro, too (a macro which is just as brain-dead simple as a C macro).
Also we need a macro because Scribble's include-section is syntax (not a function) and must appear at the top-level. As a result, you can't use it inside of an if or when.
Given that:
define.rkt
#lang racket/base
(provide (all-defined-out))
;; Macros to conditionally include literal text.
;; Each of these should return `text`,
;; or (void) for nothing
(define-syntax-rule (when-alpha text)
text)
(define-syntax-rule (when-beta text)
(void))
;; Macros to conditionally include a .scrbl file
;; Each of these should return include-section,
;; or (void) for nothing
(require scribble/base) ;for include-section
(define-syntax-rule (when-alpha/include mod-path)
(include-section mod-path))
(define-syntax-rule (when-beta/include mod-path)
(void) #;(include-section mod-path))
Currently, this is set to show things for "alpha" but omit them for "beta". Unfortunately there's not a simple #t or #f to toggle. You need to edit the body of each one a bit more than that, as explained in the comments.
manual.scrbl example file conditionally including text and another file
#lang scribble/base
#(require "defines.rkt")
#title{Getting Started}
#when-alpha{#para{Text for alpha}}
#(when-beta/include "includee.scrbl")
includee.scrbl example file being conditionally included
#lang scribble/base
#title{BETA TEXT}
I am text for beta version.
What I don't love about this solution is that you have to create/change a pair of macros for each condition -- one for including literal text, and another for include-section-ing a .scrbl file.