I utilize the counsel-rg to search the pattern of "~[a-z]+~", it displays the desired results but omit long matching line:
How could enable it to get long-matching-line displayed?
Here is an example usage:
;; counsel-rg (&optional initial-input initial-directory extra-rg-args rg-prompt)
(defun ugt-counsel-rg ()
"Add initial search text into minibuffer."
(interactive)
(let ((initial-input "^\\*+ ") ;; Search only org headers
(initial-directory "~/org")
(extra-rg-args "--max-columns 300") ;; <== THIS
(rg-prompt "rg: Search org file headers (narrow with =S-SPC= or =!keyword=: "))
(counsel-rg initial-input
initial-directory
extra-rg-args
rg-prompt)))
(global-set-key (kbd "s-u") 'ugt-counsel-rg)
Related
I use Emacs' org-mode to take notes in class, which I then export in latex. I found org-mode's default markers for bold and italic (* and / respectively) quite useful, but I had to stop using them to avoid confusing the org exporter. I would like to have a function to italicize a word selection. In other words, the function should append \textit{ and } respectively to the beginning and to the end of a selection.
Org has a function, defined in org.el, that does this, called org-emphasize (the code is posted below), but it is constructed to work only with the org markers. Unfortunately, I am not very proficient in elisp to modify it to do what I want.
Any suggestions on how to go about building my desired function, would be greatly appreciated. Thank you, all!
(defun org-emphasize (&optional char)
"Insert or change an emphasis, i.e. a font like bold or italic.
If there is an active region, change that region to a new emphasis.
If there is no region, just insert the marker characters and position
the cursor between them.
CHAR should be the marker character. If it is a space, it means to
remove the emphasis of the selected region.
If CHAR is not given (for example in an interactive call) it will be
prompted for."
(interactive)
(let ((erc org-emphasis-regexp-components)
(string "") beg end move s)
(if (org-region-active-p)
(setq beg (region-beginning)
end (region-end)
string (buffer-substring beg end))
(setq move t))
(unless char
(message "Emphasis marker or tag: [%s]"
(mapconcat #'car org-emphasis-alist ""))
(setq char (read-char-exclusive)))
(if (equal char ?\s)
(setq s ""
move nil)
(unless (assoc (char-to-string char) org-emphasis-alist)
(user-error "No such emphasis marker: \"%c\"" char))
(setq s (char-to-string char)))
(while (and (> (length string) 1)
(equal (substring string 0 1) (substring string -1))
(assoc (substring string 0 1) org-emphasis-alist))
(setq string (substring string 1 -1)))
(setq string (concat s string s))
(when beg (delete-region beg end))
(unless (or (bolp)
(string-match (concat "[" (nth 0 erc) "\n]")
(char-to-string (char-before (point)))))
(insert " "))
(unless (or (eobp)
(string-match (concat "[" (nth 1 erc) "\n]")
(char-to-string (char-after (point)))))
(insert " ") (backward-char 1))
(insert string)
(and move (backward-char 1))))
I do not know if it worth defining a new lisp function. This is mainly a LaTeX problem as described here: https://tex.stackexchange.com/questions/258394/make-block-of-text-italicized
You can transpose this solution to Org as follow. First, observe how Org special blocks:
#+BEGIN_mySpecialBlock
...
#+END_mySpecialBlock
are exported to LaTeX
\begin{mySpecialBlock}
...
\end{mySpecialBlock}
(see Org doc Special-blocks-in-LaTeX-export for further details)
Then define new LaTeX environments as:
#+LATEX_HEADER: \newenvironment{itquote} {\begin{quote}\itshape} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{itpars} {\par\itshape} {\par}
#+LATEX_HEADER: \newenvironment{bfquote} {\begin{quote}\bfseries} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{bfpars} {\par\bfseries} {\par}
The lipsum LaTeX package is used to generate some dummy text.
#+LATEX_HEADER: \usepackage{lipsum}
You can test the approach with this Org document
#+LATEX: \lipsum[66]
#+BEGIN_bfquote
#+LATEX: \lipsum[66]
#+END_bfquote
#+BEGIN_bfpars
#+LATEX: \lipsum[66]
#+END_bfpars
#+BEGIN_itquote
#+LATEX: \lipsum[66]
#+END_itquote
#+BEGIN_itpars
#+LATEX: \lipsum[66]
#+END_itpars
After LaTeX export, C-c C-e l o, you will get something like:
For a better clarity here is the complete Org document. For a real document remove all references to lipsum which is only the text generator.
#+LATEX_HEADER: \newenvironment{itquote} {\begin{quote}\itshape} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{itpars} {\par\itshape} {\par}
#+LATEX_HEADER: \newenvironment{bfquote} {\begin{quote}\bfseries} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{bfpars} {\par\bfseries} {\par}
#+LATEX_HEADER: \usepackage{lipsum}
#+TITLE: My Title
* My section
#+LATEX: \lipsum[66]
#+BEGIN_bfquote
#+LATEX: \lipsum[66]
#+END_bfquote
#+BEGIN_bfpars
#+LATEX: \lipsum[66]
#+END_bfpars
#+BEGIN_itquote
#+LATEX: \lipsum[66]
#+END_itquote
#+BEGIN_itpars
#+LATEX: \lipsum[66]
#+END_itpars
If I want to make my own function which among other thing calls wg-save (workgroups.el - save workgroups) then I do something like this:
(defun foo ()
(interactive)
...
(call-interactively 'wg-save)
)
(global-set-key (kbd "my binding") 'foo)
What about the following scenario (I will use eyebrowse.el as an example):
eyebrowse uses C-c C-w 'number' to move to different window configurations e.g. C-c C-w 1 to move to 1 or C-c C-w 2 to move to 2.
How can I write a similar function like the 'foo' since now I need to pass to 'call-interactively' a 'number' as parameter?
EDIT: C-c C-w 1 calls eyebrowse-switch-to-window-config-1.
So I need to make a 'foo' function like the above that will 'call-interactively'
'eyebrowse-switch-to-window-config-1' when the key binding is 'C-c C-w 1', 'eyebrowse-switch-to-window-config-2' when the key binding is 'C-c C-w 2' etc.
Something like the following (if it makes sense):
(defun foo ()
(interactive)
...
(call-interactively 'eyebrowse-switch-to-window-config-"number")
)
(global-set-key (kbd "C-c C-w 'number'") 'foo)
From the documentation, read with C-h f call-interactively RET:
Optional third arg KEYS, if given, specifies the sequence of events to
supply, as a vector, if the command inquires which events were used to
invoke it. If KEYS is omitted or nil, the return value of
`this-command-keys-vector' is used.
So to pass arguments to call-interactively, give it a vector with the arguments, like so
(call-interactively 'my-fn t (vector arg1 arg2))
That way you don't need to call eyebrowse-switch-to-config-window-nwhere n is a number, you call the function they rely on, eyebrowse-switch-to-window-config, and give it a number as argument.
You can get a number for argument like this: (see help of "interactive" )
(defun foo (arg)
(interactive "nWindow? ")
(call-interactively 'my-fn t (vector arg))
)
But did you read the source of eyebrowse ? It will give ideas.
(defun eyebrowse-switch-to-window-config-0 ()
"Switch to window configuration 0."
(interactive)
(eyebrowse-switch-to-window-config 0))
You can do something like this I believe:
(defun foo (NUM)
"Enter the NUM of the screen to go to.
If the key pressed is not a number go to screen 0."
(interactive
(list (read-key-sequence "Which window: ")))
((eyebrowse-switch-to-window-config 3) (string-to-number NUM)))
The key is the read-key-sequence function
To save time and space, you can use a loop to define all the keybindings you'll use.
For example, for eyebrowse instead of defining each keybinding you could do this instead:
(load-library "eyebrowse")
(dotimes (i 10)
(global-set-key
(kbd (concat "C-c C-w " (number-to-string i)))
`(lambda ()
(interactive)
(eyebrowse-switch-to-window-config ,i))))
I'm making my own minor mode for emacs. Now I want to add button to modeline. Click on this button must сause pop-up menu appear. The items of this menu depend on user's actions. I know that there is a way to add a function button to modeline with `minor-mode-alist', but I have no idea how to make dynamic menu.
Ok. Solution founded.:)
First: define some keymap:
(defconst my-mode-line-map
(let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1]
(make-sparse-keymap))
map))
Second: append list with propertized string to modeline:
(setq global-mode-string
(append global-mode-string
(list
(propertize string-name
'local-map my-mode-line-map
'mouse-face 'mode-line-highlight))))
Third: Now you can add items with
(define-key my-mode-line-map
(vconcat [mode-line down-mouse-1]
(list some_generated_id_for_future_use))
(cons name function))
...and remove with
(define-key my-mode-line-map
(vconcat [mode-line down-mouse-1]
(list id_of_button_that_u_gave_when_add))
nil)
I found more proper way:
When you define minor mode, you can specify :lighter param
(define-minor-mode my-minor-mode
"docstring"
nil
:lighter (:eval (format "%s%.5s" "#" "some code if you want dynamic label"))
;or just string :lighter "static string"
:keymap my-minor-mode-map
... ... ... rest of code ....
then you can use easymenu:
(require 'easymenu)
(easy-menu-define my-minor-mode-menu
my-minor-mode-map
"Menu for my-minor-mode"
'("some text"
"-")) ;separator
; and then add menu items with easy-menu-add-item and remove with easy-menu-remove-item
; it's nothing hard. Read the docs ;)
This menu will be added to global menu-bar and it will pop up if you click on auto added(cause you specified :lighter param) minor-mode button on modeline.
In C - I want that when I type a { and then } emacs will insert a new line between them and then set the cursor in between them. For example:
int main() {
now I type } and the following happens:
int main()
{
//cursor is here
}
Edit: forgot to mention - I want emacs to know that when defining a function that it should do what was described above but when doing a for loop, or if statement for example I want it to do the following:
if (bla bla) {
type } and... :
if (bla bla) {
//cursor here
}
If you don't mind that the behaviour will be only almost, but not exactly the way you described it, there is a built-in way to do that. It's the auto-newline feature, that can be activated with the key combination C-c C-a or this line your .emacs:
(c-toggle-auto-newline 1)
The difference is that it will do the reformatting right after entering the opening brace {. When you finally enter the closing brace, it will indent it the right way, too.
You also need to set the right CC Mode style. The style "cc-mode" seems to define things the way you described it. You can activate it with the key combination C-c . and then choosing cc-mode, or the .emacs line
(c-set-style "cc-mode")
The c-mode functions are autoloaded and will therefore usually not be available while loading the .emacs file. Therefore you should wrap them in a hook for c-mode, like this
(add-hook 'c-mode-hook
(lambda ()
(c-toggle-auto-newline 1)
(c-set-style "cc-mode")))
As for the { stuff:
(define-minor-mode c-helpers-minor-mode
"This mode contains little helpers for C developement"
nil
""
'(((kbd "{") . insert-c-block-parentheses))
)
(defun insert-c-block-parentheses ()
(interactive)
(insert "{")
(newline)
(newline)
(insert "}")
(indent-for-tab-command)
(previous-line)
(indent-for-tab-command)
)
Paste the above into your .emacs. You can activate it with c-helpers-minor-mode.
Edit: The above inserts everything by just pressing {. The script below should do it if you type {}:
(defun insert-latex-brackets (opening closing) ; prototype function for all enclosing things
(interactive)
(insert opening)
(insert " ")
(insert closing)
(backward-char (+ 1 (length closing )))
)
(defun check-char-and-insert (char opening closing)
(interactive)
(if (equal char (char-to-string (char-before (point))))
(progn (delete-backward-char 1)
(insert-latex-brackets opening closing))
(insert char)
)
)
(local-set-key (kbd "}") 'check-char-and-insert)
One last note: You could try using yasnippet, which can be a real time saver used properly.
I am developing an emacs major mode for a language (aka mydsl). However, using the techniques on xahlee's site doesn't seem to be working for some reason (possibly older emacs dialect..)
The key issues I am fighting with are (1) highlighting comments is not working and (2), the use of regexp-opt lines is not working.
I've reviewed the GNU manual and looked over cc-mode and elisp mode... those are significantly more complicated than I need.
;;;Standard # to newline comment
;;;Eventually should also have %% to %% multiline block comments
(defun mydsl-comment-dwim (arg)
"comment or uncomment"
(interactive "*P")
(require 'newcomment)
(let
((deactivate-mark nil)
(comment-start "#")
(comment-end "")
comment-dwim arg)))
(defvar mydsl-events
'("reservedword1"
"reservedword2"))
(defvar mydsl-keywords
'("other-keyword" "another-keyword"))
;;Highlight various elements
(setq mydsl-hilite
'(
; stuff between "
("\"\\.\\*\\?" . font-lock-string-face)
; : , ; { } => # $ = are all special elements
(":\\|,\\|;\\|{\\|}\\|=>\\|#\\|$\\|=" . font-lock-keyword-face)
( ,(regexp-opt mydsl-keywords 'words) . font-lock-builtin-face)
( ,(regexp-opt mydsl-events 'words) . font-lock-constant-face)
))
(defvar mydsl-tab-width nil "Width of a tab for MYDSL mode")
(define-derived-mode mydsl-mode fundamental-mode
"MYDSL mode is a major mode for editing MYDSL files"
;Recommended by manual
(kill-all-local-variables)
(setq mode-name "MYDSL script")
(setq font-lock-defaults '((mydsl-hilite)))
(if (null mydsl-tab-width)
(setq tab-width mydsl-tab-width)
(setq tab-width default-tab-width)
)
;Comment definitions
(define-key mydsl-mode-map [remap comment-dwim] 'mydsl-comment-dwim)
(modify-syntax-entry ?# "< b" mydsl-mode-syntax-table)
(modify-syntax-entry ?\n "> b" mydsl-mode-syntax-table)
;;A gnu-correct program will have some sort of hook call here.
)
(provide 'mydsl-mode)
You have a couple of syntactic problems in your code, but you got it nearly correct. Here's my edited version which appears to do the right thing for a buffer in mydsl-mode:
; No changes to the simple vars
(defvar mydsl-events
'("reservedword1"
"reservedword2"))
(defvar mydsl-keywords
'("other-keyword" "another-keyword"))
;; I'd probably put in a default that you want, as opposed to nil
(defvar mydsl-tab-width nil "Width of a tab for MYDSL mode")
;; Two small edits.
;; First is to put an extra set of parens () around the list
;; which is the format that font-lock-defaults wants
;; Second, you used ' (quote) at the outermost level where you wanted ` (backquote)
;; you were very close
(defvar mydsl-font-lock-defaults
`((
;; stuff between "
("\"\\.\\*\\?" . font-lock-string-face)
;; ; : , ; { } => # $ = are all special elements
(":\\|,\\|;\\|{\\|}\\|=>\\|#\\|$\\|=" . font-lock-keyword-face)
( ,(regexp-opt mydsl-keywords 'words) . font-lock-builtin-face)
( ,(regexp-opt mydsl-events 'words) . font-lock-constant-face)
)))
(define-derived-mode mydsl-mode fundamental-mode "MYDSL script"
"MYDSL mode is a major mode for editing MYDSL files"
;; fundamental-mode kills all local variables, no need to do it again
(setq mode-name "MYDSL script")
;; you again used quote when you had '((mydsl-hilite))
;; I just updated the variable to have the proper nesting (as noted above)
;; and use the value directly here
(setq font-lock-defaults mydsl-font-lock-defaults)
;; when there's an override, use it
;; otherwise it gets the default value
(when mydsl-tab-width
(setq tab-width mydsl-tab-width))
;; for comments
;; overriding these vars gets you what (I think) you want
;; they're made buffer local when you set them
(setq comment-start "#")
(setq comment-end "")
(modify-syntax-entry ?# "< b" mydsl-mode-syntax-table)
(modify-syntax-entry ?\n "> b" mydsl-mode-syntax-table)
;;A gnu-correct program will have some sort of hook call here.
)
(provide 'mydsl-mode)