Can't assign keys programmatically to bookmarks in a loop - emacs

Suppose, we have n bookmarks in the list (n <= 9)
% Bookmark
- --------
1 mark-1 ~/work/file-1.txt
2 mark-2 ~/work/file-2.txt
...
9 mark-9 ~/work/file-9.txt
I wanted to programmatically attach key <n> to jump to n-th bookmark.
Code
;get a sorted list of bookmarks
(let ((bookmarks (sort (bookmark-all-names) 'string<))
(i 1))
(while bookmarks
(setq key (number-to-string i))
;try to assign key i -> jump-to-bookmark(bookmarks[i])
(define-key bookmark-bmenu-mode-map
(kbd key)
(lambda () (interactive) (bookmark-jump (car bookmarks))))
;next bookmark
(setq bookmarks (cdr bookmarks))
(setq i (+ i 1))
))
Unfortunately, it gives an error:
(wrong-type-argument integer-or-marker-p key)

The issue here is related to the fact that you are using dynamic binding, but trying to access from a lambda a variable that is not in scope anymore (bookmarks). You can fix it by defining the lambda this way:
`(lambda () (interactive) (bookmark-jump (car ',bookmarks))))
You can see an explanation here.
(btw, code will fail when you have more than 9 bookmarks)
This is the modified code that works fine for me:
(let ((bookmarks (sort (bookmark-all-names) 'string<))
(i 1)
key)
(while bookmarks
(setq key (number-to-string i))
(define-key bookmark-bmenu-mode-map
(kbd key)
`(lambda () (interactive) (bookmark-jump (car ',bookmarks))))
(setq bookmarks (cdr bookmarks))
(setq i (+ i 1))))

Related

Emacs jump to next annotated words or phrases

When using Emacs, I notice that words or phrases in a buffer can be annotated or highlighted by many minor modes like hi-lock-mode, flyspell-mode, flycheck-mode...
Is there any uniform way to jump to the highlighted words or phrases created by all these minor modes? Specifically, is there any package or function support jumping to the next and previous highlighted phrases?
When using Eclipse, I can do it by pressing Ctrl-. and Ctrl-,. However, when switching to Emacs, so far, I haven't found an equivalent feature.
Developing a mode which aims to tackle that kind of tasks
https://github.com/andreas-roehler/werkstatt/tree/master/general-key
Facilitates the setting of a general command.
Than this command gets different bindings according to modes - which needs to be edited by hand once. Afterwards it allows to set/change a key at one place for all related/bound commands.
See for example inside
https://github.com/andreas-roehler/werkstatt/blob/master/general-key/general-key-python-mode.el
It's alpha still notably for the install process. Bug reports resp. feature requests welcome.
Not surprisingly, #Drew has answered something related to this.
You can programmatically use isearch with something like:
(defun foo (regexp)
(interactive (list (read-regexp "Regexp: ")))
(isearch-mode t t)
(let ((isearch-regexp nil))
(isearch-yank-string regexp)))
This will pull your previous regexp history, including those from hi-lock. I imagine it would be a fun exercise to modify this to use hi-lock-regexp-history.
If you use swiper, you can restrict the search candidates to lines with highlighted patterns by hi-lock-mode.
Here is a simple wrapper of swiper:
(require 'cl-lib)
(defun swiper-over-highlights-simple ()
(interactive)
(let ((original-swiper--candidates (symbol-function 'swiper--candidates)))
(cl-letf (((symbol-function 'swiper--candidates)
(lambda ()
(let ((pattern (mapconcat #'car hi-lock-interactive-patterns "\\|")))
(cl-remove-if-not (lambda (x) (string-match-p pattern x))
(funcall original-swiper--candidates))))))
(swiper))))
In addition, you can change ivy-read's preselect argument, which initializes the first matched line inside swiper.
The following fuction, modified from swiper, finds the closest next line with a highlighted pattern:
(defun swiper-over-highlights (&optional initial-input)
(interactive)
(let ((original-swiper--candidates (symbol-function 'swiper--candidates))
(pattern (mapconcat #'car hi-lock-interactive-patterns "\\|")))
(cl-letf (((symbol-function 'swiper--candidates)
(lambda ()
(cl-remove-if-not (lambda (x) (string-match-p pattern x))
(funcall original-swiper--candidates)))))
(let ((candidates (swiper--candidates)))
(swiper--init)
(setq swiper-invocation-face
(plist-get (text-properties-at (point)) 'face))
(let ((preselect
(save-excursion
(search-forward-regexp pattern nil t)
(let* ((current-line-value (current-line))
(candidate-line-numbers (mapcar (lambda (x) (cadr (text-properties-at 0 x)))
candidates))
(preselect-line-num (cl-find-if (lambda (x) (<= current-line-value x))
candidate-line-numbers)))
(- (length candidate-line-numbers)
(length (member preselect-line-num candidate-line-numbers))))))
(minibuffer-allow-text-properties t)
res)
(unwind-protect
(and
(setq res
(ivy-read
"Swiper: "
candidates
:initial-input initial-input
:keymap swiper-map
:preselect preselect
:require-match t
:action #'swiper--action
:re-builder #'swiper--re-builder
:history 'swiper-history
:extra-props (list :fname (buffer-file-name))
:caller 'swiper))
(point))
(unless (or res swiper-stay-on-quit)
(goto-char swiper--opoint))
(isearch-clean-overlays)
(unless (or res (string= ivy-text ""))
(cl-pushnew ivy-text swiper-history))
(setq swiper--current-window-start nil)
(when swiper--reveal-mode
(reveal-mode 1))))))))

Is is possible to bind a key plus a variable in Emacs?

I have buffers named *terminal<1>*, *terminal<2>*, *terminal<3>*, etc. Is there a way to bind a key combination that will take an argument for the number? That is, I want to bind C-c 1 to switch to *terminal<1>* and C-c 2 to switch to *terminal<2>*, and so on. If I can't do this directly, is it possible to do metaprogramming in Elisp that will define all the functions for me?
In this suggestion, the interactive switch-to-terminal will take either a prefix argument C-u 2 for example, or prompt the user.
The macro then makes setting up the key bindings a little easier.
Finally I bind C-c 1 to C-c 4 to switch to *terminal<1>* to *terminal<4>*.
(defun switch-to-terminal (buf-num)
(interactive "NNumber of buffer to visist: ")
(let* ((buf-name (format "*terminal<%d>*" buf-num))
(buf (get-buffer buf-name)))
(unless buf
(error "No buffer %s" buf-name))
(switch-to-buffer buf)))
(defmacro bind-switch-to-terminal (num)
`(global-set-key
,(kbd (format "C-c %d" num))
(lambda ()
(interactive)
(switch-to-terminal ,num))))
(bind-switch-to-terminal 1)
(bind-switch-to-terminal 2)
(bind-switch-to-terminal 3)
(bind-switch-to-terminal 4)
This change uses the same switch-to-terminal function, but replaces bind-switch-to-terminal with a function. The lexical-let* allows for the creation of a closure to create unique terminal switching functions, the dotimes loop then binds for C-c 1 to C-c 9.
(defun bind-switch-to-terminal (num)
(lexical-let* ((buf-num num)
(switch-func
(lambda ()
(interactive)
(switch-to-terminal buf-num))))
(global-set-key
(kbd (format "C-c %d" buf-num))
switch-func)))
(dotimes (num 9)
(bind-switch-to-terminal (1+ num)))
You can bind keys as usual:
(global-set-key (kbd "C-c 1") (lambda ()
(interactive)
(switch-to-buffer "*terminal<1>*")))
To create all the shortcuts from 1 to 9 we would use macros.
edit: This buggy version may put you on tracks. I give up :(
(defmacro gototerminal (count)
`(global-set-key (kbd ,(concat "C-c " (number-to-string count)))
;; with the comma I want to evaluate what is inside concat
(lambda () (interactive)
(switch-to-buffer (concat "*terminal<" ,count ">*"))))
)
(progn (setq count 1)
(while (< count 10)
(gototerminal count)
(setq count (1+ count))
))
ps: an elisp debugger is edebug. Set it with C-u C-M-x
I would write a function that calls interactive with an n argument which indicates that the function reads a number from the mini buffer:
(defun test (x)
(interactive "nNumber of buffer to visit: ")
(message (concat "received number: " (number-to-string x))))
Binding this to a key will let you enter a number in the mini buffer.
Another way is to use a numerical argument:
(defun test (x)
(interactive "P")
(message (concat "received number: " (number-to-string x))))
Say you bind this function to C-c c, you can then pass it the number 2 as an argument by pressing C-u 2 C-c c.
If you avoid the usage of an existing prefix key like C-c you can trigger a command with one keypress e.g. F9.
This command can have a single key as input.
Example:
(defun test (k)
(interactive "K")
(message "Pressed key: %d" (- (aref k 0) ?0)))
(local-set-key [f9] 'test)

Enumerate all tags in org-mode

How do I generate an enumerated list of all tags (e.g., :tag:) in an org-mode file? Say I have a list of the form:
* Head1 :foo:bar:
** Subhead1 :foo:
* Head2
** Subhead2 :foo:bar:
I want to generate a list of all tags in this file as well as how many times each tag was used. Say something like,
:foo: 3
:bar: 2
Here is a shorter version.
(defun get-tag-counts ()
(let ((all-tags '()))
(org-map-entries
(lambda ()
(let ((tag-string (car (last (org-heading-components)))))
(when tag-string
(setq all-tags
(append all-tags (split-string tag-string ":" t)))))))
;; now get counts
(loop for tag in (-uniq all-tags)
collect (cons tag (cl-count tag all-tags :test 'string=)))))
I could not make use of the code posted by John Kitchin, as it requires an interactive function. bpalmer from IRC freenode/#emacs was so kind to help me out. Please find a working example that spits out all tags below the respective tree.
; use this in order to be able to use loop on its own
(require 'cl)
;; count tags (see John's answer)
(defun get-tag-counts ()
(let ((all-tags '()))
(org-map-entries
(lambda ()
(let ((tag-string (car (last (org-heading-components)))))
(when tag-string
(setq all-tags
(append all-tags (split-string tag-string ":" t)))))))
;; now get counts
(loop for tag in (seq-uniq all-tags)
collect (cons tag (cl-count tag all-tags :test 'string=)))))
;; wrap get-tag-counts in an interactive function
(defun create-tag-counts-buffer ()
(interactive)
(let ((tags (get-tag-counts)) (b (get-buffer-create "*Org Tag Count*")))
(dolist (tag tags) (insert (car tag)) (insert "\n")) (display-buffer b)))
Here is an approach.
(setq my-hash (make-hash-table :test 'equal))
(org-map-entries
(lambda ()
(let ((tag-string (car (last (org-heading-components))))
(current-count))
(when tag-string
(dolist (tag (split-string tag-string ":" t))
(setq current-count (gethash tag my-hash))
(if current-count;
(puthash tag (+ 1 current-count) my-hash)
(puthash tag 1 my-hash))
)
)
)
)
)
;; https://github.com/Wilfred/ht.el
(require 'ht)
(ht-map
(lambda (key value)
(list key value))
my-hash)

Emacs: swapping replace queries

Using query-replace, the minibuffer says this (having saved the previous arguments):
Query replace (default FROM -> TO)
is there a command to swap the args? To get this:
Query replace (default TO -> FROM)
AFAIK, nothing out of the box does that for you. But IMHO you don't need that.
All you need to do is use M-p. Use it once to get the last TO you used. Then repeat M-p a couple times to get the last FROM you used. Very quick.
After that, you can use C-x ESC ESC (or C-x M-: or C-x M-ESC), possibly followed by M-p, to repeat either combination (TO -> FROM or FROM -> TO).
(defun swap-query-replace-defaults ()
"Swap the initial expressions offered by `query-replace'. "
(interactive)
(let* ((erg query-replace-defaults)
(first (car erg))
(second (cdr erg)))
(setq query-replace-defaults (cons second first))
(when (interactive-p) (message "%s" query-replace-defaults))
query-replace-defaults))
Made a feature-request:
http://lists.gnu.org/archive/html/bug-gnu-emacs/2013-10/msg00102.html
There's a shortcut to swap the args: M-p RET M-p M-p RET
I use this:
;; Redefine `query-replace-read-from' to add a custom keymap when
;; replacing strings. Now, C-u ENTER does the reverse suggested
;; replacement.
(defvar query-replace-keymap
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map [remap exit-minibuffer]
(lambda ()
(interactive)
(if (and current-prefix-arg query-replace-defaults)
(setq query-replace-defaults
(cons
(cdr query-replace-defaults)
(car query-replace-defaults))))
(exit-minibuffer)))
map))
(defun query-replace-read-from (prompt regexp-flag)
"Query and return the `from' argument of a query-replace operation.
The return value can also be a pair (FROM . TO) indicating that the user
wants to replace FROM with TO."
(if query-replace-interactive
(car (if regexp-flag regexp-search-ring search-ring))
(let* ((history-add-new-input nil)
(query-replace-defaults query-replace-defaults)
(prompt
(if query-replace-defaults
(format "%s (default %s -> %s): " prompt
(query-replace-descr (car query-replace-defaults))
(query-replace-descr (cdr query-replace-defaults)))
(format "%s: " prompt)))
(from
;; The save-excursion here is in case the user marks and copies
;; a region in order to specify the minibuffer input.
;; That should not clobber the region for the query-replace itself.
(save-excursion
(if regexp-flag
(read-regexp prompt nil query-replace-from-history-variable)
(read-from-minibuffer
prompt nil query-replace-keymap nil query-replace-from-history-variable
(car (if regexp-flag regexp-search-ring search-ring)) t)))))
(if (and (zerop (length from)) query-replace-defaults)
(cons (car query-replace-defaults)
(query-replace-compile-replacement
(cdr query-replace-defaults) regexp-flag))
(add-to-history query-replace-from-history-variable from nil t)
;; Warn if user types \n or \t, but don't reject the input.
(and regexp-flag
(string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
(let ((match (match-string 3 from)))
(cond
((string= match "\\n")
(message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
((string= match "\\t")
(message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
(sit-for 2)))
from))))

Defun with optional args inside the lambda?

I want to call defun with optional args inside the lambda:
(global-set-key (kbd "M-S-v")
(lambda ()
(interactive) (bk-cleans-text nil 2)))
The function works as standalone:
(bk-cleans-text nil 2)
But when called inside the lambda it complains on cond:
(defun bk-cleans-text (&optional killIt ParsSepBy)
(interactive)
(cond
((= 1 ParsSepBy)
(setq Find "\n+")
(setq Replace "\n"))
((= 2 ParsSepBy)
(setq Find "\n\n+")
(setq Replace "\n\n"))
(t
(setq Find "\n+")
(setq Replace "")))
(message "F: %s, R: %s" Find Replace))
The problem is in the key combination that you have chosen. I used another one and it worked.