How to add perl-mode.el to .emacs? - linux

If I do M-x load-file RET /root/.elisp/perl-mode.el RET then this perl-mode.el get loaded correctly.
If I in .emacs add any of these, it doesn't work
(load "/root/.elisp/perl-mode.el")
(load-file "/root/.elisp/perl-mode.el")
I am using emacs 24.1.1.
The error I get is
File mode specification error: (void-function setq-local)
Question
What is the correct way to load perl-mode.el from .emacs?

The macro setq-local was introduced in Emacs 24.3, so this version of perl-mode is too new for the Emacs you're currently running (24.1).
You could upgrade Emacs, or you could just put the definition of setq-local into your .emacs (from here):
(defmacro setq-local (var val)
"Set variable VAR to value VAL in current buffer."
;; Can't use backquote here, it's too early in the bootstrap.
(list 'set (list 'make-local-variable (list 'quote var)) val))

Related

Vim Quickfix prefixes double-bar "||" — explain?

I use the Quickfix view in Vim often.
The text in there always has a prefix of || added to it.
So, for instance, when I copy/paste out of that buffer, etc. I get those characters included by default.
Is there a way to disable this?
I haven't had luck finding any documentation or configuration for this...
Quickfix buffer is supposed to be used for parsing specially formatted strings (like compiler messages). This is done with the help of :h 'errorformat' option. And those "bars" are output separators between "filename", "line number" and "the message body".
If you have only "double bars" at the beginning of a line then you either have errorformat set wrong, or you misuse the quickfix buffer.
UPD. If you're interested, "Bars" are hardcoded in Vim's source (src/quickfix.c):
static int
qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
{
...
if (qfp->qf_module != NULL)
...
if (len < IOSIZE - 1)
IObuff[len++] = '|';
if (qfp->qf_lnum > 0)
...
if (len < IOSIZE - 2)
{
IObuff[len++] = '|';
IObuff[len++] = ' ';
}
...
}
It is now possible to customize the display of the quickfix window.
vim has introduced the quickfixtextfunc (:h qftf).
It allows exactly to customize the rendering of the quickfix window. The documentation includes an example, you can also see an example in the nvim-bqf README, although it's neovim/lua based.
You can see an example in the vim documentation in :h quickfix-window-function.
To implement a general-purpose qftf (not a specific one as in the vim documentation), you should start similarly than in the nvim-bqf readme, meaning, check if the info parameter quickfix field is 1, you should display items from getqflist, otherwise items from getloclist

Vim - Overwrite Plugin Scoped Function

I've a plugin in Vim and I don't like the behavior of a single function within it. But it isn't rly a case to open a pull request, but more an extension for it.
I know that overwriting a function ist possible by using a bang as postfix, as soon as the new definition comes after the previous one. But how can I do such thing, if this method is scoped to a script within a plugin?
I wasn't able to find a hint in _Vim_s help, nor by request a search engine. Anybody aware of this topic, at least if he can say that it is simply not possible.
A short example:
plugin/autoload/plugin.vim
...
function! s:foo() {
// behavior I would like to adjust
}
...
~/.vimrc
function! foo() {
// the "correct" behavior
}
Thanks for any help!
Actually it is possible. But as #romainl said, you'd better suggest your patch to the plugin maintainer or ask for a variation point.
Regarding the how.
First, you'll need to identify the script number of this autoload plugin. Let's say that :scriptname says it's 210. In order to do that automatically I have a lh#askvim#scriptid() function in my library plugin that does the job -- see the current definition at the end of the answer.
Then, to override the s:foo() function, you'll need to provide a new definition for
function! <SNR>210_Foo()
new definition
endfunction
(I've just tested it with vim 8.0-1157)
IOW, we can override a script-local function. However, I haven't found how to override a script-local variable directly without a reference to its s: dictionary. We could inject setter/getter functions to a specific variable or a function that returns the local s: dictionary.
lh#askvim#scriptid() current definition is the following
" Function: lh#askvim#execute(command) {{{3
" #since Version 4.0.0
if exists('*execute')
function! lh#askvim#execute(command) abort
return split(execute(a:command), "\n")
endfunction
else
function! lh#askvim#execute(command) abort
return s:beware_running_through_client_server ? [] : split(lh#askvim#exe(a:command), "\n")
endfunction
endif
" Function: lh#askvim#scriptnames() {{{3
function! lh#askvim#scriptnames() abort
let scripts = lh#askvim#execute('scriptnames')
let s:scripts = map(copy(scripts), 'split(v:val, "\\v:=\\s+")')
call lh#list#map_on(s:scripts, 1, 'fnamemodify(v:val, ":p")')
return s:scripts
endfunction
" Function: lh#askvim#scriptid(name) {{{3
function! lh#askvim#scriptid(name, ...) abort
let last_change = get(a:, 1, 0)
if last_change || !exists('s:scripts')
call lh#askvim#scriptnames()
endif
let matches = filter(copy(s:scripts), 'v:val[1] =~ a:name')
if len(matches) > 1
throw "Too many scripts match `".a:name."`: ".string(matches)
elseif empty(matches)
if last_change
throw "No script match `".a:name."`"
else
return lh#askvim#scriptid(a:name, 1)
endif
endif
return matches[0][0]
endfunction
That is not possible.
s:foo() is scoped to the script it belongs to (see :help s:) so it can't be accessed from anywhere else.
Fork it.
Make the desired changes to your fork.
Use your fork instead of the original.
Consider submitting a pull request.

Search in current folder with helm-do-grep

I have so small problem about in Emacs. I bind helm-do-grep command for Emacs. It's really useful.
I want to search something in current folder.
I searched some codes about that both of them are working but I don't have ability to fix them like what I want.
If you fix them for me I'll be happy thank you.
(defvar my/book-notes-directory "~/Dropbox/books")
(defun my/helm-do-grep-book-notes ()
"Search my book notes."
(interactive)
(helm-do-grep-1 (list my/book-notes-directory)))
(defun my-dir-locals-dir ()
"Return the directory local variables directory.
Code taken from `hack-dir-local-variables'."
(let ((variables-file (dir-locals-find-file (or (buffer-file-name) default-directory)))
(dir-name nil))
(cond,
((stringp variables-file)
(setq dir-name (file-name-directory variables-file)))
((consp variables-file)
(setq dir-name (nth 0 variables-file))))
dir-name))
How about something like this?
(defun my/helm-do-grep-current-directory-tree ()
"Recursively search current directory.
If a parent directory has a `dir-locals-file', use that as the
root instead."
(interactive)
(let ((variables-file (dir-locals-find-file
(or (buffer-file-name) default-directory))))
(helm-do-grep-1
(list
(cond
((stringp variables-file)
(file-name-directory variables-file))
((consp variables-file)
(nth 0 variables-file))
(t default-directory)))
t nil '("*"))))
By the way, if you ask on http://emacs.stackexchange.com , you might get better answers. (And faster, too!) =)

Clojure - Docjure: Method works in REPL but not in File

I just try to read the content of an excel file in clojure. I use the docjure library. When I use the sample code in the REPL, the output is as I wanted it. But after inserting it into the file I got an Wrong number of args - Error for the spreadsheet/select-sheet method.
Here is the code:
(use 'dk.ative.docjure.spreadsheet)
(->> (load-workbook (str (System/getProperty "user.dir") "/resources/public/xls/test.xls")
(select-sheet "menu")
(select-columns {:A :number, :D :name})
))
The args for this method are [name ^Workbook workbook]. Why does it only need one argument in the REPL but two in the file?
Just as Alex said in comments, you messed with parens.
Right now your code code evaluates into:
(load-workbook (str (System/getProperty "user.dir")
"/resources/public/xls/test.xls")
(select-sheet "menu")
(select-columns {:A :number, :D :name}))
Here is how your actual code should look like:
(->> "/resources/public/xls/test.xls"
(str (System/getProperty "user.dir")) ; prefix it with user.dir
load-workbook ; load .xls workbook
(select-sheet "menu") ; select menu sheet
(select-columns {:A :number, :D :name})) ; select some columns
Which evaluates into:
(select-columns {:A :number, :D :name}
(select-sheet "menu"
(load-workbook (str (System/getProperty "user.dir")
"/resources/public/xls/test.xls"))))
As you can see, both select-sheet and select-columns are called with two arguments here.
To better understand how thread-last macro ->> works, see its documentation.

Vim Create File(s) Using Template(s)

I'm tending to rely on vim more than a full IDE for working on projects, and one of the things I find myself doing on a regular basis is creating a new file(s) with derived values.
For example, creating a new c++ class involves creating a .hpp file and a .cpp file, adding file comments, the license, the author, ctor/dtor, copy, assign, move, etc...
.hpp
class %Object% {
public:
explicit %Object%() = default;
~%Object%() = default;
%Object%(%Object%&& rhs) = default;
%Object%(const %Object%& rhs) = default;
%Object%& operator=(%Object%&& rhs) = default;
%Object%& operator=(const %Object%& rhs) = default;
protected:
private:
}
.cpp
#include "%Object%.hpp"
Another example would be a .h and a .c file in c.
I'm a little familiar with UltiSnips and muTemplate, which both seem to cut down on boilerplate a lot. However, I'm not clear if there's a way to use these, or something else, outside of a file scope. I wrote a really quick and dirty set of bash scripts to do it, and I'm getting ready to re-implement it in python, but I'd rather use an existing plugin.
Is there a way to do this with UltiSnips, muTemplate, or something else? If not, is there a good way to extend an existing plugin?
Discl. I'm the maintainer of mu-template and lh-cpp. Unfortunately I'm just seeing your question now -- I would say that you shouldn't have hesitated to drop me an email/an issue/... I'm not sure the question is still opened. I'm not even sure to have exactly grasped what you were looking for.
Since the version you've experimented with, I've added many templates/snippets/wizards in lh-cpp to generate classes according to their semantics. You can now either:
expand things like value-class, base-class, etc
or call a function to expand the same wizard/snippet/template and give it parameters. For instance, expanding a value class with a pointer-like parameter will trigger the generation of the copy constructor and assignment operator (otherwise, they would be defaulted, explicitly or implicitly depending on options and on the C++ flavour detected (C++98/03, C++11 or more -- rule of all or nothing still has to be enforced). Alas this approach is not very ergonomic at the moment. I have to find a way to simplify this task. You can find example of use in the test/spec directory of lh-cpp.
Note that the c++ template for C++ files are also highly customizable -- on a per project basis. Usual licence texts are ready to include. A new C++ file knows how to include its associated header file (if detected).
Add this to one of your start up file:
" Function to substitute the class names in a file
function! SubstituteClassName()
execute "1,$s/%Object%/" . expand("%:t:r") . "/g"
endfunction
" Function to create the skeleton of a header file
function! CreateHeaderFile()
1
insert
#pragma once
#ifndef %Object%_H
#define %Object%_H
class %Object% {
public:
explicit %Object%() = default;
~%Object%() = default;
%Object%(%Object%&& rhs) = default;
%Object%(const %Object%& rhs) = default;
%Object%& operator=(%Object%&& rhs) = default;
%Object%& operator=(const %Object%& rhs) = default;
protected:
private:
}
.
call SubstituteClassName()
endfunction
" Function to create the skeleton of a source file
function! CreateSourceFile()
1
insert
#include "%Object%.hpp"
.
call SubstituteClassName()
endfunction
function! CreateClassFiles(name)
" Open the header file.
execute "edit " . a:name . ".hpp"
" Create the skeleton of the header file
call CreateHeaderFile()
" Write the file
wa
" Open the source file.
execute "edit " . a:name . ".cpp"
" Create the skeleton of the header file
call CreateSourceFile()
" Write the file
wa
endfunction
Now you can create the skeleton .hpp and .cpp files using
call CreateClassFiles("myclassname")

Resources